Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-10-03 05:26:48

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   // Store all the values in the vectors
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   // std::cout << "Filling with: latency = " << latency << ", mode = " << mode << std::endl;
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     // If it is not the last and it has the same latency and mode as the next one remove it
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_.empty()) {
0081     return 255;
0082   }
0083   if (latencies_.size() == 1) {
0084     return latencies_[0].latency;
0085   }
0086   int differentLatenciesNum = 0;
0087   // Count the number of different latencies
0088   for (latConstIt it = latencies_.begin(); it != latencies_.end() - 1; ++it) {
0089     if (it->latency != (it + 1)->latency) {
0090       ++differentLatenciesNum;
0091     }
0092   }
0093   if (differentLatenciesNum == 0) {
0094     return latencies_[0].latency;
0095   }
0096   return 255;
0097 }
0098 
0099 uint16_t SiStripLatency::singleMode() const {
0100   if (latencies_.empty()) {
0101     return 0;
0102   }
0103   if (latencies_.size() == 1) {
0104     return latencies_[0].mode;
0105   }
0106   int differentModesNum = 0;
0107   // Count the number of different modes
0108   for (latConstIt it = latencies_.begin(); it != latencies_.end() - 1; ++it) {
0109     if (it->mode != (it + 1)->mode) {
0110       ++differentModesNum;
0111     }
0112   }
0113   if (differentModesNum == 0) {
0114     return latencies_[0].mode;
0115   }
0116   return 0;
0117 }
0118 
0119 void SiStripLatency::allModes(std::vector<uint16_t>& allModesVector) const {
0120   for (latConstIt it = latencies_.begin(); it != latencies_.end(); ++it) {
0121     allModesVector.push_back(it->mode);
0122   }
0123   // The Latencies are sorted by DetIdAndApv, we need to sort the modes again and then remove duplicates
0124   sort(allModesVector.begin(), allModesVector.end());
0125   allModesVector.erase(unique(allModesVector.begin(), allModesVector.end()), allModesVector.end());
0126 }
0127 
0128 int16_t SiStripLatency::singleReadOutMode() const {
0129   uint16_t mode = singleMode();
0130   if (mode != 0) {
0131     if ((mode & READMODEMASK) == READMODEMASK)
0132       return 1;
0133     if ((mode & READMODEMASK) == 0)
0134       return 0;
0135   } else {
0136     // If we are here the Tracker is not in single mode. Check if it is in single Read-out mode.
0137     bool allInPeakMode = true;
0138     bool allInDecoMode = true;
0139     std::vector<uint16_t> allModesVector;
0140     allModes(allModesVector);
0141     std::vector<uint16_t>::const_iterator it = allModesVector.begin();
0142     if (allModesVector.size() == 1 && allModesVector[0] == 0)
0143       allInPeakMode = false;
0144     else {
0145       for (; it != allModesVector.end(); ++it) {
0146         if ((*it) % 2 == 0)
0147           continue;
0148         if (((*it) & READMODEMASK) == READMODEMASK)
0149           allInDecoMode = false;
0150         if (((*it) & READMODEMASK) == 0)
0151           allInPeakMode = false;
0152       }
0153     }
0154     if (allInPeakMode)
0155       return 1;
0156     if (allInDecoMode)
0157       return 0;
0158   }
0159   return -1;
0160 }
0161 
0162 void SiStripLatency::allLatencies(std::vector<uint16_t>& allLatenciesVector) const {
0163   for (latConstIt it = latencies_.begin(); it != latencies_.end(); ++it) {
0164     allLatenciesVector.push_back(it->latency);
0165   }
0166   // The Latencies are sorted by DetIdAndApv, we need to sort the latencies again and then remove duplicates
0167   sort(allLatenciesVector.begin(), allLatenciesVector.end());
0168   allLatenciesVector.erase(unique(allLatenciesVector.begin(), allLatenciesVector.end()), allLatenciesVector.end());
0169 }
0170 
0171 std::vector<SiStripLatency::Latency> SiStripLatency::allUniqueLatencyAndModes() {
0172   std::vector<Latency> latencyCopy(latencies_);
0173   sort(latencyCopy.begin(), latencyCopy.end(), OrderByLatencyAndMode());
0174   latencyCopy.erase(unique(latencyCopy.begin(), latencyCopy.end(), SiStripLatency::EqualByLatencyAndMode()),
0175                     latencyCopy.end());
0176   return latencyCopy;
0177 }
0178 
0179 void SiStripLatency::printSummary(std::stringstream& ss, const TrackerTopology* trackerTopo) const {
0180   ss << std::endl;
0181   if (singleReadOutMode() == 1) {
0182     ss << "SingleReadOut = PEAK" << std::endl;
0183   } else if (singleReadOutMode() == 0) {
0184     ss << "SingleReadOut = DECO" << std::endl;
0185   } else {
0186     ss << "SingleReadOut = MIXED" << std::endl;
0187   }
0188   uint16_t lat = singleLatency();
0189   if (lat != 255) {
0190     ss << "All the Tracker has the same latency = " << lat << std::endl;
0191   } else {
0192     std::vector<uint16_t> allLatenciesVector;
0193     allLatencies(allLatenciesVector);
0194     if (allLatenciesVector.size() > 1) {
0195       ss << "There is more than one latency value in the Tracker" << std::endl;
0196     } else {
0197       ss << "Latency value is " << lat << " that means invalid" << std::endl;
0198     }
0199   }
0200   ss << "Total number of ranges = " << latencies_.size() << std::endl;
0201   printDebug(ss, trackerTopo);
0202 }
0203 
0204 void SiStripLatency::printDebug(std::stringstream& ss, const TrackerTopology* /*trackerTopo*/) const {
0205   ss << "List of all the latencies and modes for the " << latencies_.size() << " ranges in the object:" << std::endl;
0206   for (latConstIt it = latencies_.begin(); it != latencies_.end(); ++it) {
0207     int detId = it->detIdAndApv >> 3;
0208     int apv = it->detIdAndApv & 7;  // 7 is 0...0111
0209     ss << "for detId = " << detId << " and apv pair = " << apv << " latency = " << int(it->latency)
0210        << " and mode = " << int(it->mode) << std::endl;
0211   }
0212 }
0213 
0214 #undef READMODEMASK