|
| 1 | +#ifndef CondFormats_HGCalObjects_interface_HGCalMappingCellIndexerTrigger_h |
| 2 | +#define CondFormats_HGCalObjects_interface_HGCalMappingCellIndexerTrigger_h |
| 3 | + |
| 4 | +#include <iostream> |
| 5 | +#include <cstdint> |
| 6 | +#include <vector> |
| 7 | +#include <numeric> |
| 8 | +#include "CondFormats/Serialization/interface/Serializable.h" |
| 9 | +#include "CondFormats/HGCalObjects/interface/HGCalDenseIndexerBase.h" |
| 10 | + |
| 11 | +/** |
| 12 | + @short utility class to assign dense readout trigger cell indexing |
| 13 | + */ |
| 14 | +class HGCalMappingCellIndexerTrigger { |
| 15 | +public: |
| 16 | + // typedef HGCalDenseIndexerBase WaferDenseIndexerBase; |
| 17 | + |
| 18 | + HGCalMappingCellIndexerTrigger() = default; |
| 19 | + |
| 20 | + /** |
| 21 | + adds to map of type codes (= module types) to handle and updatest the max. number of eRx |
| 22 | + */ |
| 23 | + void processNewCell(std::string const& typecode, uint16_t ROC, uint16_t trLink, uint16_t trCell) { |
| 24 | + // Skip if trLink, trCell not connected |
| 25 | + if (trLink == uint16_t(-1) || trCell == uint16_t(-1)) |
| 26 | + return; |
| 27 | + //assign index to this typecode and resize the max vectors |
| 28 | + if (typeCodeIndexer_.count(typecode) == 0) { |
| 29 | + typeCodeIndexer_[typecode] = typeCodeIndexer_.size(); |
| 30 | + maxROC_.resize(typeCodeIndexer_.size(), 0); |
| 31 | + maxTrLink_.resize(typeCodeIndexer_.size(), 0); |
| 32 | + maxTCPerLink_.resize(typeCodeIndexer_.size(), 0); |
| 33 | + } |
| 34 | + |
| 35 | + size_t idx = typeCodeIndexer_[typecode]; |
| 36 | + |
| 37 | + /*High density modules have links {0, 2} and low {0, 1, 2, 3} so to make it work I need to divide by 2*/ |
| 38 | + if (typecode[1] == 'H') |
| 39 | + trLink /= 2; |
| 40 | + /*SiPM tiles have trCells indexed from 1 instead from 0*/ |
| 41 | + if (typecode[0] == 'T') |
| 42 | + trCell--; |
| 43 | + maxROC_[idx] = std::max(maxROC_[idx], static_cast<uint16_t>(ROC + 1)); |
| 44 | + maxTrLink_[idx] = std::max(maxTrLink_[idx], static_cast<uint16_t>(trLink + 1)); |
| 45 | + maxTCPerLink_[idx] = std::max(maxTCPerLink_[idx], static_cast<uint16_t>(trCell + 1)); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + @short process the current list of type codes handled and updates the dense indexers |
| 50 | + */ |
| 51 | + void update() { |
| 52 | + uint32_t n = typeCodeIndexer_.size(); |
| 53 | + offsets_ = std::vector<uint32_t>(n, 0); |
| 54 | + di_ = std::vector<HGCalDenseIndexerBase>(n, HGCalDenseIndexerBase(3)); /* The indices are {ROC, trLink, TC}*/ |
| 55 | + for (uint32_t idx = 0; idx < n; idx++) { |
| 56 | + uint16_t maxROCs = maxROC_[idx]; |
| 57 | + uint16_t maxLinks = maxTrLink_[idx]; |
| 58 | + uint16_t maxTCPerLink = maxTCPerLink_[idx]; |
| 59 | + di_[idx].updateRanges({{maxROCs, maxLinks, maxTCPerLink}}); |
| 60 | + if (idx < n - 1) |
| 61 | + offsets_[idx + 1] = di_[idx].maxIndex() + offsets_[idx]; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + @short gets index given typecode string |
| 67 | + */ |
| 68 | + size_t getEnumFromTypecode(std::string const& typecode) const { |
| 69 | + auto it = typeCodeIndexer_.find(typecode); |
| 70 | + if (it == typeCodeIndexer_.end()) |
| 71 | + throw cms::Exception("ValueError") << " unable to find typecode=" << typecode << " in cell indexer"; |
| 72 | + return it->second; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + @short checks if there is a typecode corresponding to an index |
| 77 | + */ |
| 78 | + std::string getTypecodeFromEnum(size_t idx) const { |
| 79 | + for (const auto& it : typeCodeIndexer_) |
| 80 | + if (it.second == idx) |
| 81 | + return it.first; |
| 82 | + throw cms::Exception("ValueError") << " unable to find typecode corresponding to idx=" << idx; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + @short returns the dense indexer for a typecode |
| 87 | + */ |
| 88 | + HGCalDenseIndexerBase getDenseIndexFor(std::string const& typecode) const { |
| 89 | + return getDenseIndexerFor(getEnumFromTypecode(typecode)); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + @short returns the dense indexer for a given internal index |
| 94 | + */ |
| 95 | + HGCalDenseIndexerBase getDenseIndexerFor(size_t idx) const { |
| 96 | + if (idx >= di_.size()) |
| 97 | + throw cms::Exception("ValueError") << " index requested for cell dense indexer (i=" << idx |
| 98 | + << ") is larger than allocated"; |
| 99 | + return di_[idx]; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + @short builders for the dense index |
| 104 | + */ |
| 105 | + uint16_t denseIndex(std::string const& typecode, uint32_t ROC, uint32_t trLink, uint32_t trCell) const { |
| 106 | + return denseIndex(getEnumFromTypecode(typecode), ROC, trLink, trCell); |
| 107 | + } |
| 108 | + uint32_t denseIndex(size_t idx, uint32_t ROC, uint32_t trLink, uint32_t trCell) const { |
| 109 | + return di_[idx].denseIndex({{ROC, trLink, trCell}}) + offsets_[idx]; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + @short returns the max. dense index expected |
| 114 | + */ |
| 115 | + uint32_t maxDenseIndex() const { |
| 116 | + size_t i = maxTrLink_.size(); |
| 117 | + if (i == 0) |
| 118 | + return 0; |
| 119 | + return offsets_.back() + maxROC_.back() * maxTrLink_.back() * maxTCPerLink_.back(); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + @short gets the number of words (cells) for a given typecode |
| 124 | + Note : some partials are rounded to the closest multiplie of 16 or 8 depending on the density |
| 125 | + That is done because not all the TrigLinks,TrgCells possible are assigned in practice |
| 126 | + e.g.: ML-T has 22 TCs but this will return 32 or MH-T has 19 but it will return 24 |
| 127 | + It results in a small mem overhead over the totall memory needed to be allocated |
| 128 | + */ |
| 129 | + size_t getNWordsExpectedFor(std::string const& typecode) const { |
| 130 | + auto it = getEnumFromTypecode(typecode); |
| 131 | + return getNWordsExpectedFor(it); |
| 132 | + } |
| 133 | + size_t getNWordsExpectedFor(size_t typecodeidx) const { return getDenseIndexerFor(typecodeidx).maxIndex(); } |
| 134 | + |
| 135 | + /** |
| 136 | + @short gets the number of Trigger Links for a given typecode |
| 137 | + */ |
| 138 | + size_t getNTrLinkExpectedFor(std::string const& typecode) const { |
| 139 | + auto it = getEnumFromTypecode(typecode); |
| 140 | + return getNTrLinkExpectedFor(it); |
| 141 | + } |
| 142 | + size_t getNTrLinkExpectedFor(size_t typecodeidx) const { return maxTrLink_[typecodeidx] * maxROC_[typecodeidx]; } |
| 143 | + |
| 144 | + std::unordered_map<std::string, size_t> typeCodeIndexer_; |
| 145 | + std::vector<uint16_t> maxROC_; |
| 146 | + std::vector<uint16_t> maxTrLink_; |
| 147 | + std::vector<uint16_t> maxTCPerLink_; |
| 148 | + std::vector<uint32_t> offsets_; |
| 149 | + std::vector<HGCalDenseIndexerBase> di_; |
| 150 | + |
| 151 | + ~HGCalMappingCellIndexerTrigger() = default; |
| 152 | + |
| 153 | + COND_SERIALIZABLE; |
| 154 | +}; |
| 155 | + |
| 156 | +#endif |
0 commit comments