CbmRoot
Loading...
Searching...
No Matches
CbmRichAddress.cxx
Go to the documentation of this file.
1/* Copyright (C) 2024 Justus-Liebig-Universitaet Giessen, Giessen
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Martin Beyer [committer] */
4
10
11#include "CbmRichAddress.h"
12
14#include "CbmDefs.h"
15
16#include <cassert>
17#include <sstream>
18
19int32_t CbmRichAddress::GetAddress(uint32_t camera, uint32_t strip, uint32_t backplane, uint32_t pmt, uint32_t pixel,
20 uint32_t version)
21{
22 using namespace Detail;
23
24 assert(version <= kCurrentVersion);
25
26 // Catch overrun of allowed ranges
27 if (camera > static_cast<uint32_t>(kMask[version][static_cast<int32_t>(RichElementLevel::kCamera)])) {
28 LOG(error) << "CbmRichAddress::GetAddress: Camera Id " << camera << " exceeds maximum "
29 << kMask[version][static_cast<int32_t>(RichElementLevel::kCamera)];
30 return 0;
31 }
32 if (strip > static_cast<uint32_t>(kMask[version][static_cast<int32_t>(RichElementLevel::kStrip)])) {
33 LOG(error) << "CbmRichAddress::GetAddress: Strip Id " << strip << " exceeds maximum "
34 << kMask[version][static_cast<int32_t>(RichElementLevel::kStrip)];
35 return 0;
36 }
37 if (backplane > static_cast<uint32_t>(kMask[version][static_cast<int32_t>(RichElementLevel::kBackplane)])) {
38 LOG(error) << "CbmRichAddress::GetAddress: Backplane Id " << backplane << " exceeds maximum "
39 << kMask[version][static_cast<int32_t>(RichElementLevel::kBackplane)];
40 return 0;
41 }
42 if (pmt > static_cast<uint32_t>(kMask[version][static_cast<int32_t>(RichElementLevel::kPmt)])) {
43 LOG(error) << "CbmRichAddress::GetAddress: Pmt Id " << pmt << " exceeds maximum "
44 << kMask[version][static_cast<int32_t>(RichElementLevel::kPmt)];
45 return 0;
46 }
47 if (pixel > static_cast<uint32_t>(kMask[version][static_cast<int32_t>(RichElementLevel::kPixel)])) {
48 LOG(error) << "CbmRichAddress::GetAddress: Pixel Id " << pixel << " exceeds maximum "
49 << kMask[version][static_cast<int32_t>(RichElementLevel::kPixel)];
50 return 0;
51 }
52
53 return ToIntegralType(ECbmModuleId::kRich) << kShift[version][static_cast<int32_t>(RichElementLevel::kSystem)]
54 | camera << kShift[version][static_cast<int32_t>(RichElementLevel::kCamera)]
55 | strip << kShift[version][static_cast<int32_t>(RichElementLevel::kStrip)]
56 | backplane << kShift[version][static_cast<int32_t>(RichElementLevel::kBackplane)]
57 | pmt << kShift[version][static_cast<int32_t>(RichElementLevel::kPmt)]
58 | pixel << kShift[version][static_cast<int32_t>(RichElementLevel::kPixel)] | version << kVersionShift;
59}
60
61int32_t CbmRichAddress::GetMotherAddress(int32_t address, int32_t level)
62{
63 using namespace Detail;
64 if (!(level >= static_cast<int32_t>(RichElementLevel::kSystem)
65 && level < static_cast<int32_t>(RichElementLevel::kNumLevels))) {
66 throw std::out_of_range(std::string("CbmRichAddress::GetMotherAddress: Illegal element level ")
67 + std::to_string(level));
68 return 0;
69 }
70 if (level == static_cast<int32_t>(RichElementLevel::kNumLevels) - 1) return address;
71 uint32_t version = GetVersion(address);
72 int32_t motherAdd = (address & ((1 << kShift[version][level + 1]) - 1));
73 motherAdd = motherAdd | (version << kVersionShift);
74 return motherAdd;
75}
76
77uint32_t CbmRichAddress::GetElementId(int32_t address, int32_t level)
78{
79 using namespace Detail;
80 if (!(level >= static_cast<int32_t>(RichElementLevel::kSystem)
81 && level < static_cast<int32_t>(RichElementLevel::kNumLevels))) {
82 throw std::out_of_range(std::string("CbmRichAddress::GetElementId: Illegal element level ")
83 + std::to_string(level));
84 return 0;
85 }
86 uint32_t version = GetVersion(address);
87 return (address & (kMask[version][level] << kShift[version][level])) >> kShift[version][level];
88}
89
90uint32_t CbmRichAddress::GetSystemId(int32_t address)
91{
92 return GetElementId(address, static_cast<uint32_t>(RichElementLevel::kSystem));
93}
94
95uint32_t CbmRichAddress::GetVersion(int32_t address)
96{
97 return uint32_t((address & (kVersionMask << kVersionShift)) >> kVersionShift);
98}
99
100int32_t CbmRichAddress::SetElementId(int32_t address, int32_t level, uint32_t newId)
101{
102 using namespace Detail;
103 if (!(level >= static_cast<int32_t>(RichElementLevel::kSystem)
104 && level < static_cast<int32_t>(RichElementLevel::kNumLevels))) {
105 throw std::out_of_range(std::string("CbmRichAddress::SetElementId: Illegal element level ")
106 + std::to_string(level));
107 return 0;
108 }
109 uint32_t version = GetVersion(address);
110 uint32_t maxId = (1 << kBits[version][level]) - 1;
111 if (newId > maxId) {
112 LOG(fatal) << "CbmRichAddress::SetElementId: Id " << newId << " for RICH level " << level << " exceeds maximum "
113 << maxId;
114 return 0;
115 }
116 return (address & (~(kMask[version][level] << kShift[version][level]))) | (newId << kShift[version][level]);
117}
118
119std::string CbmRichAddress::ToString(int32_t address)
120{
121 std::stringstream ss;
122
123 ss << "RichAddress: address " << address << " (version " << GetVersion(address) << ")"
124 << ": system " << GetElementId(address, static_cast<int32_t>(RichElementLevel::kSystem)) << ", camera "
125 << GetElementId(address, static_cast<int32_t>(RichElementLevel::kCamera)) << ", strip "
126 << GetElementId(address, static_cast<int32_t>(RichElementLevel::kStrip)) << ", photo detector "
127 << GetElementId(address, static_cast<int32_t>(RichElementLevel::kBackplane)) << ", pmt "
128 << GetElementId(address, static_cast<int32_t>(RichElementLevel::kPmt)) << ", pixel "
129 << GetElementId(address, static_cast<int32_t>(RichElementLevel::kPixel));
130 return ss.str();
131}
XPU_D constexpr auto ToIntegralType(T enumerator) -> typename std::underlying_type< T >::type
Converts an element of enum class to its underlying integral type.
Definition CbmDefs.h:33
@ kRich
Ring-Imaging Cherenkov Detector.
Definition CbmDefs.h:49
int32_t GetMotherAddress(int32_t address, int32_t level)
Construct the address of an element from the address of a descendant element.
int32_t SetElementId(int32_t address, int32_t level, uint32_t newId)
Set the index of an element, leaving the other element levels untouched.
uint32_t GetVersion(int32_t address)
Extract version number.
constexpr int32_t kVersionMask
constexpr uint32_t kCurrentVersion
uint32_t GetSystemId(int32_t address)
Get system Id (should be integer value of ECbmModuleId::kRich)
uint32_t GetElementId(int32_t address, int32_t level)
Get the index of an element.
constexpr int32_t kVersionShift
std::string ToString(int32_t address)
String output.
int32_t GetAddress(uint32_t camera=0, uint32_t strip=0, uint32_t backplane=0, uint32_t pmt=0, uint32_t pixel=0, uint32_t version=kCurrentVersion)
Construct address.