Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-01-18 03:41:50

0001 #include "CondFormats/HGCalObjects/interface/HGCalMappingModuleIndexer.h"
0002 
0003 //
0004 void HGCalMappingModuleIndexer::processNewModule(uint32_t fedid,
0005                                                  uint16_t captureblockIdx,
0006                                                  uint16_t econdIdx,
0007                                                  uint32_t typecodeIdx,
0008                                                  uint32_t nerx,
0009                                                  uint32_t nwords,
0010                                                  std::string const& typecode) {
0011   //add fed if needed
0012   if (fedid >= fedReadoutSequences_.size()) {
0013     fedReadoutSequences_.resize(fedid + 1);
0014   }
0015   HGCalFEDReadoutSequence& frs = fedReadoutSequences_[fedid];
0016   frs.id = fedid;
0017 
0018   //assign position, resize if needed, and fill the type code
0019   uint32_t idx = modFedIndexer_.denseIndex({{captureblockIdx, econdIdx}});
0020   if (idx >= frs.readoutTypes_.size()) {
0021     frs.readoutTypes_.resize(idx + 1, -1);
0022   }
0023   frs.readoutTypes_[idx] = typecodeIdx;
0024 
0025   //count another typecodein the global list
0026   if (typecodeIdx >= globalTypesCounter_.size()) {
0027     globalTypesCounter_.resize(typecodeIdx + 1, 0);
0028     globalTypesNErx_.resize(typecodeIdx + 1, 0);
0029     globalTypesNWords_.resize(typecodeIdx + 1, 0);
0030     dataOffsets_.resize(typecodeIdx + 1, 0);
0031   }
0032   globalTypesCounter_[typecodeIdx]++;
0033   globalTypesNErx_[typecodeIdx] = nerx;
0034   globalTypesNWords_[typecodeIdx] = nwords;
0035 
0036   //add typecode string to map to retrieve fedId & modId later
0037   if (!typecode.empty()) {
0038     if (typecodeMap_.find(typecode) != typecodeMap_.end()) {     // found key
0039       const auto& [fedid_, modid_] = typecodeMap_.at(typecode);  // (fedId,modId)
0040       edm::LogWarning("HGCalMappingModuleIndexer")
0041           << "Found typecode " << typecode << " already in map (fedid,modid)=(" << fedid_ << "," << modid_
0042           << ")! Overwriting with (" << fedid << "," << idx << ")...";
0043     }
0044     LogDebug("HGCalMappingModuleIndexer")
0045         << "HGCalMappingModuleIndexer::processNewModule: Adding typecode=\"" << typecode << "\" with fedid=" << fedid
0046         << ", idx=" << idx << " (will be re-indexed after finalize)";
0047     typecodeMap_[typecode] = std::make_pair(fedid, idx);
0048   }
0049 }
0050 
0051 //
0052 void HGCalMappingModuleIndexer::finalize() {
0053   //max indices at different levels
0054   nfeds_ = fedReadoutSequences_.size();
0055   maxModulesIdx_ = std::accumulate(globalTypesCounter_.begin(), globalTypesCounter_.end(), 0);
0056   maxErxIdx_ = std::inner_product(globalTypesCounter_.begin(), globalTypesCounter_.end(), globalTypesNErx_.begin(), 0);
0057   maxDataIdx_ =
0058       std::inner_product(globalTypesCounter_.begin(), globalTypesCounter_.end(), globalTypesNWords_.begin(), 0);
0059 
0060   //compute the global offset to assign per board type, eRx and channel data
0061   moduleOffsets_.resize(maxModulesIdx_, 0);
0062   erxOffsets_.resize(maxModulesIdx_, 0);
0063   dataOffsets_.resize(maxModulesIdx_, 0);
0064   for (size_t i = 1; i < globalTypesCounter_.size(); i++) {
0065     moduleOffsets_[i] = globalTypesCounter_[i - 1];
0066     erxOffsets_[i] = globalTypesCounter_[i - 1] * globalTypesNErx_[i - 1];
0067     dataOffsets_[i] = globalTypesCounter_[i - 1] * globalTypesNWords_[i - 1];
0068   }
0069   std::partial_sum(moduleOffsets_.begin(), moduleOffsets_.end(), moduleOffsets_.begin());
0070   std::partial_sum(erxOffsets_.begin(), erxOffsets_.end(), erxOffsets_.begin());
0071   std::partial_sum(dataOffsets_.begin(), dataOffsets_.end(), dataOffsets_.begin());
0072 
0073   //now go through the FEDs and ascribe the offsets per module in the readout sequence
0074   std::vector<uint32_t> typeCounters(globalTypesCounter_.size(), 0);
0075   for (auto& fedit : fedReadoutSequences_) {
0076     //assign the final indexing in the look-up table depending on which ECON-D's are really present
0077     size_t nconn(0);
0078     fedit.moduleLUT_.resize(fedit.readoutTypes_.size(), -1);
0079     for (size_t i = 0; i < fedit.readoutTypes_.size(); i++) {
0080       if (fedit.readoutTypes_[i] == -1)
0081         continue;  //unexisting
0082 
0083       reassignTypecodeLocation(fedit.id, i, nconn);
0084       fedit.moduleLUT_[i] = nconn;
0085       nconn++;
0086     }
0087 
0088     //remove unexisting ECONs building a final compact readout sequence
0089     fedit.readoutTypes_.erase(
0090         std::remove_if(
0091             fedit.readoutTypes_.begin(), fedit.readoutTypes_.end(), [&](int val) -> bool { return val == -1; }),
0092         fedit.readoutTypes_.end());
0093 
0094     //resize vectors to their final size and set final values
0095     size_t nmods = fedit.readoutTypes_.size();
0096     fedit.modOffsets_.resize(nmods, 0);
0097     fedit.erxOffsets_.resize(nmods, 0);
0098     fedit.chDataOffsets_.resize(nmods, 0);
0099     fedit.enabledErx_.resize(nmods, 0);
0100 
0101     for (size_t i = 0; i < nmods; i++) {
0102       int type_val = fedit.readoutTypes_[i];
0103 
0104       //module offset : global offset for this type + current index for this type
0105       uint32_t baseMod_offset = moduleOffsets_[type_val] + typeCounters[type_val];
0106       fedit.modOffsets_[i] = baseMod_offset;  // + internalMod_offset;
0107 
0108       //erx-level offset : global offset of e-Rx of this type + #e-Rrx * current index for this type
0109       uint32_t baseErx_offset = erxOffsets_[type_val];
0110       uint32_t internalErx_offset = globalTypesNErx_[type_val] * typeCounters[type_val];
0111       fedit.erxOffsets_[i] = baseErx_offset + internalErx_offset;
0112 
0113       //channel data offset: global offset for data of this type + #words * current index for this type
0114       uint32_t baseData_offset = dataOffsets_[type_val];
0115       uint32_t internalData_offset = globalTypesNWords_[type_val] * typeCounters[type_val];
0116       fedit.chDataOffsets_[i] = baseData_offset + internalData_offset;
0117 
0118       //enabled erx flags
0119       //FIXME: assume all eRx are enabled now
0120       fedit.enabledErx_[i] = (0b1 << globalTypesNErx_[type_val]) - 0b1;
0121       typeCounters[type_val]++;
0122     }
0123   }
0124 }
0125 
0126 //
0127 std::pair<uint32_t, uint32_t> HGCalMappingModuleIndexer::getIndexForFedAndModule(std::string const& typecode) const {
0128   auto it = typecodeMap_.find(typecode);
0129   if (it == typecodeMap_.end()) {       // did not find key
0130     std::size_t nmax = 100;             // maximum number of keys to print
0131     auto maxit = typecodeMap_.begin();  // limit printout to prevent gigantic print out
0132     std::advance(maxit, std::min(typecodeMap_.size(), nmax));
0133     std::string allkeys = std::accumulate(
0134         std::next(typecodeMap_.begin()), maxit, typecodeMap_.begin()->first, [](const std::string& a, const auto& b) {
0135           return a + ',' + b.first;
0136         });
0137     if (typecodeMap_.size() > nmax)
0138       allkeys += ", ...";
0139     throw cms::Exception("HGCalMappingModuleIndexer")
0140         << "Could not find typecode '" << typecode << "' in map (size=" << typecodeMap_.size()
0141         << ")! Found the following modules (from the module locator file): " << allkeys;
0142   }
0143   return it->second;  // (fedid,modid)
0144 };