File indexing completed on 2024-04-06 12:02:41
0001 #include "CondFormats/SiStripObjects/interface/SiStripLatency.h"
0002 #include "FWCore/Utilities/interface/Exception.h"
0003 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0004
0005 #include <algorithm>
0006 #include <iterator>
0007 #include <iostream>
0008 #include <sstream>
0009
0010 bool SiStripLatency::put(const uint32_t detId, const uint16_t apv, const uint16_t latency, const uint16_t mode) {
0011 if (detId > 536870911) {
0012 std::stringstream error;
0013 error << "ERROR: the detId = " << detId
0014 << " is bigger than the maximum acceptable value = 2^(29) - 1 = " << 536870911 << std::endl;
0015 error << "Since we are using 29 bits for the detId and 3 bits for the apv value. The maximum tracker detId at the "
0016 "moment"
0017 << std::endl;
0018 error
0019 << "of the writing of this class was 47017836 as defined in CalibTracker/SiStripCommon/data/SiStripDetInfo.dat."
0020 << std::endl;
0021 error << "If the maximum value has changed a revision of this calss is needed, possibly changing the detIdAndApv "
0022 "value from"
0023 << std::endl;
0024 error << "from uint32_t to uint64_t." << std::endl;
0025 edm::LogError("SiStripLatency::put") << error.str();
0026 throw cms::Exception("InsertFailure");
0027 }
0028
0029
0030 uint32_t detIdAndApv = (detId << 3) | apv;
0031 latIt pos = lower_bound(latencies_.begin(), latencies_.end(), detIdAndApv, OrderByDetIdAndApv());
0032
0033 if (pos != latencies_.end() && pos->detIdAndApv == detIdAndApv) {
0034 std::cout << "Value already inserted, skipping insertion" << std::endl;
0035 return false;
0036 }
0037
0038 latencies_.insert(pos, Latency(detIdAndApv, latency, mode));
0039
0040 return true;
0041 }
0042
0043 void SiStripLatency::compress() {
0044 latIt lat = latencies_.begin();
0045 while (lat != latencies_.end()) {
0046
0047 if (((lat + 1) != latencies_.end()) && ((lat + 1)->mode == lat->mode) && ((lat + 1)->latency == lat->latency)) {
0048 lat = latencies_.erase(lat);
0049 } else {
0050 ++lat;
0051 }
0052 }
0053 }
0054
0055 uint16_t SiStripLatency::latency(const uint32_t detId, const uint16_t apv) const {
0056 const latConstIt& pos = position(detId, apv);
0057 if (pos == latencies_.end()) {
0058 return 255;
0059 }
0060 return pos->latency;
0061 }
0062
0063 uint16_t SiStripLatency::mode(const uint32_t detId, const uint16_t apv) const {
0064 const latConstIt& pos = position(detId, apv);
0065 if (pos == latencies_.end()) {
0066 return 0;
0067 }
0068 return pos->mode;
0069 }
0070
0071 std::pair<uint16_t, uint16_t> SiStripLatency::latencyAndMode(const uint32_t detId, const uint16_t apv) const {
0072 const latConstIt& pos = position(detId, apv);
0073 if (pos == latencies_.end()) {
0074 return std::make_pair(255, 0);
0075 }
0076 return std::make_pair(pos->latency, pos->mode);
0077 }
0078
0079 uint16_t SiStripLatency::singleLatency() const {
0080 if (latencies_.size() == 1) {
0081 return latencies_[0].latency;
0082 }
0083 int differentLatenciesNum = 0;
0084
0085 for (latConstIt it = latencies_.begin(); it != latencies_.end() - 1; ++it) {
0086 if (it->latency != (it + 1)->latency) {
0087 ++differentLatenciesNum;
0088 }
0089 }
0090 if (differentLatenciesNum == 0) {
0091 return latencies_[0].latency;
0092 }
0093 return 255;
0094 }
0095
0096 uint16_t SiStripLatency::singleMode() const {
0097 if (latencies_.size() == 1) {
0098 return latencies_[0].mode;
0099 }
0100 int differentModesNum = 0;
0101
0102 for (latConstIt it = latencies_.begin(); it != latencies_.end() - 1; ++it) {
0103 if (it->mode != (it + 1)->mode) {
0104 ++differentModesNum;
0105 }
0106 }
0107 if (differentModesNum == 0) {
0108 return latencies_[0].mode;
0109 }
0110 return 0;
0111 }
0112
0113 void SiStripLatency::allModes(std::vector<uint16_t>& allModesVector) const {
0114 for (latConstIt it = latencies_.begin(); it != latencies_.end(); ++it) {
0115 allModesVector.push_back(it->mode);
0116 }
0117
0118 sort(allModesVector.begin(), allModesVector.end());
0119 allModesVector.erase(unique(allModesVector.begin(), allModesVector.end()), allModesVector.end());
0120 }
0121
0122 int16_t SiStripLatency::singleReadOutMode() const {
0123 uint16_t mode = singleMode();
0124 if (mode != 0) {
0125 if ((mode & READMODEMASK) == READMODEMASK)
0126 return 1;
0127 if ((mode & READMODEMASK) == 0)
0128 return 0;
0129 } else {
0130
0131 bool allInPeakMode = true;
0132 bool allInDecoMode = true;
0133 std::vector<uint16_t> allModesVector;
0134 allModes(allModesVector);
0135 std::vector<uint16_t>::const_iterator it = allModesVector.begin();
0136 if (allModesVector.size() == 1 && allModesVector[0] == 0)
0137 allInPeakMode = false;
0138 else {
0139 for (; it != allModesVector.end(); ++it) {
0140 if ((*it) % 2 == 0)
0141 continue;
0142 if (((*it) & READMODEMASK) == READMODEMASK)
0143 allInDecoMode = false;
0144 if (((*it) & READMODEMASK) == 0)
0145 allInPeakMode = false;
0146 }
0147 }
0148 if (allInPeakMode)
0149 return 1;
0150 if (allInDecoMode)
0151 return 0;
0152 }
0153 return -1;
0154 }
0155
0156 void SiStripLatency::allLatencies(std::vector<uint16_t>& allLatenciesVector) const {
0157 for (latConstIt it = latencies_.begin(); it != latencies_.end(); ++it) {
0158 allLatenciesVector.push_back(it->latency);
0159 }
0160
0161 sort(allLatenciesVector.begin(), allLatenciesVector.end());
0162 allLatenciesVector.erase(unique(allLatenciesVector.begin(), allLatenciesVector.end()), allLatenciesVector.end());
0163 }
0164
0165 std::vector<SiStripLatency::Latency> SiStripLatency::allUniqueLatencyAndModes() {
0166 std::vector<Latency> latencyCopy(latencies_);
0167 sort(latencyCopy.begin(), latencyCopy.end(), OrderByLatencyAndMode());
0168 latencyCopy.erase(unique(latencyCopy.begin(), latencyCopy.end(), SiStripLatency::EqualByLatencyAndMode()),
0169 latencyCopy.end());
0170 return latencyCopy;
0171 }
0172
0173 void SiStripLatency::printSummary(std::stringstream& ss, const TrackerTopology* trackerTopo) const {
0174 ss << std::endl;
0175 if (singleReadOutMode() == 1) {
0176 ss << "SingleReadOut = PEAK" << std::endl;
0177 } else if (singleReadOutMode() == 0) {
0178 ss << "SingleReadOut = DECO" << std::endl;
0179 } else {
0180 ss << "SingleReadOut = MIXED" << std::endl;
0181 }
0182 uint16_t lat = singleLatency();
0183 if (lat != 255) {
0184 ss << "All the Tracker has the same latency = " << lat << std::endl;
0185 } else {
0186 std::vector<uint16_t> allLatenciesVector;
0187 allLatencies(allLatenciesVector);
0188 if (allLatenciesVector.size() > 1) {
0189 ss << "There is more than one latency value in the Tracker" << std::endl;
0190 } else {
0191 ss << "Latency value is " << lat << " that means invalid" << std::endl;
0192 }
0193 }
0194 ss << "Total number of ranges = " << latencies_.size() << std::endl;
0195 printDebug(ss, trackerTopo);
0196 }
0197
0198 void SiStripLatency::printDebug(std::stringstream& ss, const TrackerTopology* ) const {
0199 ss << "List of all the latencies and modes for the " << latencies_.size() << " ranges in the object:" << std::endl;
0200 for (latConstIt it = latencies_.begin(); it != latencies_.end(); ++it) {
0201 int detId = it->detIdAndApv >> 3;
0202 int apv = it->detIdAndApv & 7;
0203 ss << "for detId = " << detId << " and apv pair = " << apv << " latency = " << int(it->latency)
0204 << " and mode = " << int(it->mode) << std::endl;
0205 }
0206 }
0207
0208 #undef READMODEMASK