CMS 3D CMS Logo

HGCalMappingModuleIndexer.h
Go to the documentation of this file.
1 #ifndef CondFormats_HGCalObjects_interface_HGCalMappingParameterIndex_h
2 #define CondFormats_HGCalObjects_interface_HGCalMappingParameterIndex_h
3 
4 #include <cstdint>
5 #include <vector>
6 #include <algorithm>
7 
13 
20  uint32_t id;
22  std::vector<int> moduleLUT_;
24  std::vector<int> readoutTypes_;
26  std::vector<uint32_t> modOffsets_, erxOffsets_, chDataOffsets_;
28 };
29 
35 public:
37 
38  ~HGCalMappingModuleIndexer() = default;
39 
46  void processNewModule(uint32_t fedid,
47  uint16_t captureblockIdx,
48  uint16_t econdIdx,
49  uint32_t typecodeIdx,
50  uint32_t nerx,
51  uint32_t nwords) {
52  //add fed if needed
53  if (fedid >= fedReadoutSequences_.size()) {
54  fedReadoutSequences_.resize(fedid + 1);
55  }
57  frs.id = fedid;
58 
59  //assign position, resize if needed, and fill the type code
60  uint32_t idx = modFedIndexer_.denseIndex({{captureblockIdx, econdIdx}});
61  if (idx >= frs.readoutTypes_.size()) {
62  frs.readoutTypes_.resize(idx + 1, -1);
63  }
64  frs.readoutTypes_[idx] = typecodeIdx;
65 
66  //count another typecodein the global list
67  if (typecodeIdx >= globalTypesCounter_.size()) {
68  globalTypesCounter_.resize(typecodeIdx + 1, 0);
69  globalTypesNErx_.resize(typecodeIdx + 1, 0);
70  globalTypesNWords_.resize(typecodeIdx + 1, 0);
71  dataOffsets_.resize(typecodeIdx + 1, 0);
72  }
73  globalTypesCounter_[typecodeIdx]++;
74  globalTypesNErx_[typecodeIdx] = nerx;
75  globalTypesNWords_[typecodeIdx] = nwords;
76  }
77 
81  void finalize() {
82  //max indices at different levels
84  maxModulesIdx_ = std::accumulate(globalTypesCounter_.begin(), globalTypesCounter_.end(), 0);
85  maxErxIdx_ =
86  std::inner_product(globalTypesCounter_.begin(), globalTypesCounter_.end(), globalTypesNErx_.begin(), 0);
87  maxDataIdx_ =
88  std::inner_product(globalTypesCounter_.begin(), globalTypesCounter_.end(), globalTypesNWords_.begin(), 0);
89 
90  //compute the global offset to assign per board type, eRx and channel data
91  moduleOffsets_.resize(maxModulesIdx_, 0);
92  erxOffsets_.resize(maxModulesIdx_, 0);
93  dataOffsets_.resize(maxModulesIdx_, 0);
94  for (size_t i = 1; i < globalTypesCounter_.size(); i++) {
98  }
99  std::partial_sum(moduleOffsets_.begin(), moduleOffsets_.end(), moduleOffsets_.begin());
100  std::partial_sum(erxOffsets_.begin(), erxOffsets_.end(), erxOffsets_.begin());
101  std::partial_sum(dataOffsets_.begin(), dataOffsets_.end(), dataOffsets_.begin());
102 
103  //now go through the FEDs and ascribe the offsets per module in the readout sequence
104  std::vector<uint32_t> typeCounters(globalTypesCounter_.size(), 0);
105  for (auto &fedit : fedReadoutSequences_) {
106  //assign the indexing in the look-up table
107  size_t nconn(0);
108  fedit.moduleLUT_.resize(fedit.readoutTypes_.size(), -1);
109  for (size_t i = 0; i < fedit.readoutTypes_.size(); i++) {
110  if (fedit.readoutTypes_[i] == -1)
111  continue; //unexisting
112  fedit.moduleLUT_[i] = nconn;
113  nconn++;
114  }
115 
116  //remove unexisting ECONs building a final compact readout sequence
117  std::remove_if(
118  fedit.readoutTypes_.begin(), fedit.readoutTypes_.end(), [&](int val) -> bool { return val == -1; });
119 
120  //assign the final offsets at the different levels
121  size_t nmods = fedit.readoutTypes_.size();
122  fedit.modOffsets_.resize(nmods, 0);
123  fedit.erxOffsets_.resize(nmods, 0);
124  fedit.chDataOffsets_.resize(nmods, 0);
125  for (size_t i = 0; i < nmods; i++) {
126  uint32_t type_val = fedit.readoutTypes_[i];
127 
128  //module offset : global offset for this type + current index for this type
129  uint32_t baseMod_offset = moduleOffsets_[type_val] + typeCounters[type_val];
130  fedit.modOffsets_[i] = baseMod_offset; // + internalMod_offset;
131 
132  //erx-level offset : global offset of e-Rx of this type + #e-Rrx * current index for this type
133  uint32_t baseErx_offset = erxOffsets_[type_val];
134  uint32_t internalErx_offset = globalTypesNErx_[type_val] * typeCounters[type_val];
135  fedit.erxOffsets_[i] = baseErx_offset + internalErx_offset;
136 
137  //channel data offset: global offset for data of this type + #words * current index for this type
138  uint32_t baseData_offset = dataOffsets_[type_val];
139  uint32_t internalData_offset = globalTypesNWords_[type_val] * typeCounters[type_val];
140  fedit.chDataOffsets_[i] = baseData_offset + internalData_offset;
141 
142  typeCounters[type_val]++;
143  }
144  }
145  }
146 
151  static std::pair<bool, int> convertTypeCode(std::string_view typecode) {
152  if (typecode.size() < 5)
153  throw cms::Exception("InvalidHGCALTypeCode") << typecode << " is invalid for decoding readout cell type";
154 
155  bool isSiPM = {typecode.find("TM") != std::string::npos ? true : false};
156  int celltype;
157  if (isSiPM) {
158  celltype = 0; // Assign SiPM type coarse or molded with next version of modulelocator
159  } else {
160  celltype = {typecode[4] == '1' ? 0 : typecode[4] == '2' ? 1 : 2};
161  }
162  return std::pair<bool, bool>(isSiPM, celltype);
163  }
164 
170  uint32_t getIndexForModule(uint32_t fedid, uint32_t nmod) const {
171  return fedReadoutSequences_[fedid].modOffsets_[nmod];
172  };
173  uint32_t getIndexForModule(uint32_t fedid, uint16_t captureblockIdx, uint16_t econdIdx) const {
174  uint32_t nmod = denseIndexingFor(fedid, captureblockIdx, econdIdx);
175  return getIndexForModule(fedid, nmod);
176  };
177  uint32_t getIndexForModuleErx(uint32_t fedid, uint32_t nmod, uint32_t erxidx) const {
178  return fedReadoutSequences_[fedid].erxOffsets_[nmod] + erxidx;
179  };
180  uint32_t getIndexForModuleErx(uint32_t fedid, uint16_t captureblockIdx, uint16_t econdIdx, uint32_t erxidx) const {
181  uint32_t nmod = denseIndexingFor(fedid, captureblockIdx, econdIdx);
182  return getIndexForModuleErx(fedid, nmod, erxidx);
183  }
184  uint32_t getIndexForModuleData(uint32_t fedid, uint32_t nmod, uint32_t erxidx, uint32_t chidx) const {
185  return fedReadoutSequences_[fedid].chDataOffsets_[nmod] + erxidx * HGCalMappingCellIndexer::maxChPerErx_ + chidx;
186  };
188  uint32_t fedid, uint16_t captureblockIdx, uint16_t econdIdx, uint32_t erxidx, uint32_t chidx) const {
189  uint32_t nmod = denseIndexingFor(fedid, captureblockIdx, econdIdx);
190  return getIndexForModuleData(fedid, nmod, erxidx, chidx);
191  };
192 
193  int getTypeForModule(uint32_t fedid, uint32_t nmod) const { return fedReadoutSequences_[fedid].readoutTypes_[nmod]; }
194  int getTypeForModule(uint32_t fedid, uint16_t captureblockIdx, uint16_t econdIdx) const {
195  uint32_t nmod = denseIndexingFor(fedid, captureblockIdx, econdIdx);
196  return getTypeForModule(fedid, nmod);
197  }
198 
202  std::vector<HGCalFEDReadoutSequence_t> fedReadoutSequences_;
206  std::vector<uint32_t> moduleOffsets_, erxOffsets_, dataOffsets_;
209 
211  constexpr static uint32_t maxCBperFED_ = 10;
213  constexpr static uint32_t maxECONDperCB_ = 12;
214 
215 private:
219  uint32_t denseIndexingFor(uint32_t fedid, uint16_t captureblockIdx, uint16_t econdIdx) const {
220  if (fedid > nfeds_)
221  throw cms::Exception("ValueError") << "FED ID=" << fedid << " is unknown to current mapping";
222  uint32_t idx = modFedIndexer_.denseIndex({{captureblockIdx, econdIdx}});
223  auto dense_idx = fedReadoutSequences_[fedid].moduleLUT_[idx];
224  if (dense_idx < 0)
225  throw cms::Exception("ValueError") << "FED ID=" << fedid << " capture block=" << captureblockIdx
226  << " econ=" << econdIdx << "has not been assigned a dense indexing"
227  << std::endl;
228  return uint32_t(dense_idx);
229  }
230 
232 };
233 
234 #endif
static constexpr uint16_t maxChPerErx_
std::vector< uint32_t > moduleOffsets_
std::vector< uint32_t > erxOffsets_
~HGCalMappingModuleIndexer()=default
std::vector< int > moduleLUT_
>look-up table (capture block, econd idx) -> internal dense index
std::vector< uint32_t > erxOffsets_
utility class to assign dense readout module indexing the class holds the information on the expected...
int getTypeForModule(uint32_t fedid, uint16_t captureblockIdx, uint16_t econdIdx) const
internal indexer
uint32_t getIndexForModule(uint32_t fedid, uint16_t captureblockIdx, uint16_t econdIdx) const
static constexpr uint32_t maxCBperFED_
max number of ECON-Ds processed by a main buffer/capture block
uint32_t getIndexForModuleData(uint32_t fedid, uint32_t nmod, uint32_t erxidx, uint32_t chidx) const
std::vector< uint32_t > modOffsets_
>dense sequence of offsets for modules, e-Rx and channel data
static std::pair< bool, int > convertTypeCode(std::string_view typecode)
decodes silicon or sipm type and cell type for the detector id from the typecode string ...
std::vector< int > readoutTypes_
>dense sequence of modules in the readout: the type is the one in use in the cell mapping ...
uint32_t maxModulesIdx_
max number of main buffers/capture blocks per FED
std::vector< HGCalFEDReadoutSequence_t > fedReadoutSequences_
global counters for types of modules, number of e-Rx and words
static constexpr uint32_t maxECONDperCB_
std::vector< uint32_t > chDataOffsets_
bool isSiPM(int ieta, int iphi, int depth)
std::vector< uint32_t > dataOffsets_
global counters (sizes of vectors)
this structure holds the indices and types in the readout sequence as the 12 capture blocks may not a...
void processNewModule(uint32_t fedid, uint16_t captureblockIdx, uint16_t econdIdx, uint32_t typecodeIdx, uint32_t nerx, uint32_t nwords)
for a new module it adds it&#39;s type to the readaout sequence vector if the fed id is not yet existing ...
std::vector< uint32_t > globalTypesNWords_
base offsets to apply per module type with different granularity : module, e-Rx, channel data ...
uint32_t getIndexForModuleErx(uint32_t fedid, uint16_t captureblockIdx, uint16_t econdIdx, uint32_t erxidx) const
std::vector< uint32_t > globalTypesNErx_
HGCalDenseIndexerBase modFedIndexer_
the sequence of FED readout sequence descriptors
uint32_t getIndexForModule(uint32_t fedid, uint32_t nmod) const
returns the index for the n-th module in the readout sequence of a FED if the index in the readout se...
this is a simple class that takes care of building a dense index for a set of categories the maximum ...
uint32_t getIndexForModuleData(uint32_t fedid, uint16_t captureblockIdx, uint16_t econdIdx, uint32_t erxidx, uint32_t chidx) const
int getTypeForModule(uint32_t fedid, uint32_t nmod) const
uint32_t getIndexForModuleErx(uint32_t fedid, uint32_t nmod, uint32_t erxidx) const
uint32_t denseIndex(std::vector< uint32_t > v) const
#define COND_SERIALIZABLE
Definition: Serializable.h:39
uint32_t denseIndexingFor(uint32_t fedid, uint16_t captureblockIdx, uint16_t econdIdx) const
given capture block and econd indices returns the dense indexer
std::vector< uint32_t > globalTypesCounter_