Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:04:29

0001 /**
0002  * \class L1GlobalTriggerObjectMapRecord
0003  * 
0004  * 
0005  * Description: see header file.  
0006  *
0007  * Implementation:
0008  *    <TODO: enter implementation details>
0009  *   
0010  * \author: Vasile Mihai Ghete - HEPHY Vienna
0011  * \author: W. David Dagenhart
0012  * 
0013  *
0014  */
0015 
0016 #include "DataFormats/L1GlobalTrigger/interface/L1GlobalTriggerObjectMaps.h"
0017 
0018 #include <algorithm>
0019 #include <limits>
0020 
0021 #include "FWCore/Utilities/interface/Exception.h"
0022 
0023 void L1GlobalTriggerObjectMaps::swap(L1GlobalTriggerObjectMaps& rh) {
0024   m_algorithmResults.swap(rh.m_algorithmResults);
0025   m_conditionResults.swap(rh.m_conditionResults);
0026   m_combinations.swap(rh.m_combinations);
0027   m_namesParameterSetID.swap(rh.m_namesParameterSetID);
0028 }
0029 
0030 bool L1GlobalTriggerObjectMaps::algorithmExists(int algorithmBitNumber) const {
0031   std::vector<AlgorithmResult>::const_iterator i = std::lower_bound(
0032       m_algorithmResults.begin(), m_algorithmResults.end(), AlgorithmResult(0, algorithmBitNumber, false));
0033   if (i == m_algorithmResults.end() || i->algorithmBitNumber() != algorithmBitNumber) {
0034     return false;
0035   }
0036   return true;
0037 }
0038 
0039 bool L1GlobalTriggerObjectMaps::algorithmResult(int algorithmBitNumber) const {
0040   std::vector<AlgorithmResult>::const_iterator i = std::lower_bound(
0041       m_algorithmResults.begin(), m_algorithmResults.end(), AlgorithmResult(0, algorithmBitNumber, false));
0042   if (i == m_algorithmResults.end() || i->algorithmBitNumber() != algorithmBitNumber) {
0043     cms::Exception ex("L1GlobalTrigger");
0044     ex << "algorithmBitNumber not found";
0045     ex.addContext("Calling L1GlobalTriggerObjectMaps::algorithmResult");
0046     throw ex;
0047   }
0048   return i->algorithmResult();
0049 }
0050 
0051 void L1GlobalTriggerObjectMaps::updateOperandTokenVector(
0052     int algorithmBitNumber, std::vector<L1GtLogicParser::OperandToken>& operandTokenVector) const {
0053   unsigned startIndex = 0;
0054   unsigned endIndex = 0;
0055   getStartEndIndex(algorithmBitNumber, startIndex, endIndex);
0056 
0057   unsigned length = endIndex - startIndex;
0058   if (length != operandTokenVector.size()) {
0059     cms::Exception ex("L1GlobalTrigger");
0060     ex << "operand token vector size does not match number of conditions";
0061     ex.addContext("Calling L1GlobalTriggerObjectMaps::updateOperandTokenVector");
0062     throw ex;
0063   }
0064 
0065   for (unsigned i = 0; i < length; ++i) {
0066     operandTokenVector[i].tokenResult = m_conditionResults[startIndex + i].conditionResult();
0067   }
0068 }
0069 
0070 void L1GlobalTriggerObjectMaps::getAlgorithmBitNumbers(std::vector<int>& algorithmBitNumbers) const {
0071   algorithmBitNumbers.clear();
0072   for (std::vector<AlgorithmResult>::const_iterator i = m_algorithmResults.begin(), iEnd = m_algorithmResults.end();
0073        i != iEnd;
0074        ++i) {
0075     algorithmBitNumbers.push_back(i->algorithmBitNumber());
0076   }
0077 }
0078 
0079 unsigned L1GlobalTriggerObjectMaps::getNumberOfConditions(int algorithmBitNumber) const {
0080   unsigned startIndex = 0;
0081   unsigned endIndex = 0;
0082   getStartEndIndex(algorithmBitNumber, startIndex, endIndex);
0083   return endIndex - startIndex;
0084 }
0085 
0086 L1GlobalTriggerObjectMaps::ConditionsInAlgorithm L1GlobalTriggerObjectMaps::getConditionsInAlgorithm(
0087     int algorithmBitNumber) const {
0088   unsigned startIndex = 0;
0089   unsigned endIndex = 0;
0090   getStartEndIndex(algorithmBitNumber, startIndex, endIndex);
0091   return ConditionsInAlgorithm(&m_conditionResults[startIndex], endIndex - startIndex);
0092 }
0093 
0094 L1GlobalTriggerObjectMaps::CombinationsInCondition L1GlobalTriggerObjectMaps::getCombinationsInCondition(
0095     int algorithmBitNumber, unsigned conditionNumber) const {
0096   unsigned startIndex = 0;
0097   unsigned endIndex = 0;
0098   getStartEndIndex(algorithmBitNumber, startIndex, endIndex);
0099 
0100   if (endIndex <= startIndex + conditionNumber) {
0101     cms::Exception ex("L1GlobalTrigger");
0102     ex << "Condition number is out of range";
0103     ex.addContext("Calling L1GlobalTriggerObjectMaps::getCombinationsInCondition");
0104     throw ex;
0105   }
0106 
0107   unsigned endObjectIndex = m_combinations.size();
0108   unsigned nextConditionIndex = startIndex + conditionNumber + 1U;
0109   if (nextConditionIndex < m_conditionResults.size()) {
0110     endObjectIndex = m_conditionResults[nextConditionIndex].startIndexOfCombinations();
0111   }
0112   unsigned beginObjectIndex = m_conditionResults[startIndex + conditionNumber].startIndexOfCombinations();
0113   unsigned short nObjectsPerCombination = m_conditionResults[startIndex + conditionNumber].nObjectsPerCombination();
0114 
0115   if (endObjectIndex == beginObjectIndex) {
0116     return CombinationsInCondition(nullptr, 0, 0);
0117   }
0118   if (endObjectIndex < beginObjectIndex || m_combinations.size() < endObjectIndex || nObjectsPerCombination == 0 ||
0119       (endObjectIndex - beginObjectIndex) % nObjectsPerCombination != 0) {
0120     cms::Exception ex("L1GlobalTrigger");
0121     ex << "Indexes to combinations are invalid";
0122     ex.addContext("Calling L1GlobalTriggerObjectMaps::getCombinationsInCondition");
0123     throw ex;
0124   }
0125   return CombinationsInCondition(&m_combinations[beginObjectIndex],
0126                                  (endObjectIndex - beginObjectIndex) / nObjectsPerCombination,
0127                                  nObjectsPerCombination);
0128 }
0129 
0130 void L1GlobalTriggerObjectMaps::reserveForAlgorithms(unsigned n) { m_algorithmResults.reserve(n); }
0131 
0132 void L1GlobalTriggerObjectMaps::pushBackAlgorithm(unsigned startIndexOfConditions,
0133                                                   int algorithmBitNumber,
0134                                                   bool algorithmResult) {
0135   m_algorithmResults.push_back(AlgorithmResult(startIndexOfConditions, algorithmBitNumber, algorithmResult));
0136 }
0137 
0138 void L1GlobalTriggerObjectMaps::consistencyCheck() const {
0139   // None of these checks should ever fail unless there
0140   // is a bug in the code filling this object
0141   for (std::vector<AlgorithmResult>::const_iterator i = m_algorithmResults.begin(), iEnd = m_algorithmResults.end();
0142        i != iEnd;
0143        ++i) {
0144     std::vector<AlgorithmResult>::const_iterator j = i;
0145     ++j;
0146     if (j != iEnd && !(*i < *j)) {
0147       cms::Exception ex("L1GlobalTrigger");
0148       ex << "AlgorithmResults should be sorted in increasing order of bit number with no duplicates. It is not.";
0149       ex.addContext("Calling L1GlobalTriggerObjectMaps::consistencyCheck");
0150       throw ex;
0151     }
0152     unsigned endIndex = (j != iEnd) ? j->startIndexOfConditions() : m_conditionResults.size();
0153 
0154     if (endIndex < i->startIndexOfConditions()) {
0155       cms::Exception ex("L1GlobalTrigger");
0156       ex << "startIndexOfConditions decreases or exceeds the size of m_conditionResults";
0157       ex.addContext("Calling L1GlobalTriggerObjectMaps::consistencyCheck");
0158       throw ex;
0159     }
0160   }
0161   for (std::vector<ConditionResult>::const_iterator i = m_conditionResults.begin(), iEnd = m_conditionResults.end();
0162        i != iEnd;
0163        ++i) {
0164     std::vector<ConditionResult>::const_iterator j = i;
0165     ++j;
0166     unsigned endIndex = (j != iEnd) ? j->startIndexOfCombinations() : m_combinations.size();
0167 
0168     if (endIndex < i->startIndexOfCombinations()) {
0169       cms::Exception ex("L1GlobalTrigger");
0170       ex << "startIndexOfCombinations decreases or exceeds the size of m_conditionResults";
0171       ex.addContext("Calling L1GlobalTriggerObjectMaps::consistencyCheck");
0172       throw ex;
0173     }
0174     unsigned length = endIndex - i->startIndexOfCombinations();
0175     if (length == 0U) {
0176       if (i->nObjectsPerCombination() != 0U) {
0177         cms::Exception ex("L1GlobalTrigger");
0178         ex << "Length is zero and nObjectsInCombination is not zero";
0179         ex.addContext("Calling L1GlobalTriggerObjectMaps::consistencyCheck");
0180         throw ex;
0181       }
0182     } else {
0183       if (i->nObjectsPerCombination() == 0 || length % i->nObjectsPerCombination() != 0) {
0184         cms::Exception ex("L1GlobalTrigger");
0185         ex << "Size indicated by startIndexOfCombinations is not a multiple of nObjectsInCombination";
0186         ex.addContext("Calling L1GlobalTriggerObjectMaps::consistencyCheck");
0187         throw ex;
0188       }
0189     }
0190   }
0191 }
0192 
0193 void L1GlobalTriggerObjectMaps::reserveForConditions(unsigned n) { m_conditionResults.reserve(n); }
0194 
0195 void L1GlobalTriggerObjectMaps::pushBackCondition(unsigned startIndexOfCombinations,
0196                                                   unsigned short nObjectsPerCombination,
0197                                                   bool conditionResult) {
0198   m_conditionResults.push_back(ConditionResult(startIndexOfCombinations, nObjectsPerCombination, conditionResult));
0199 }
0200 
0201 void L1GlobalTriggerObjectMaps::reserveForObjectIndexes(unsigned n) { m_combinations.reserve(n); }
0202 
0203 void L1GlobalTriggerObjectMaps::pushBackObjectIndex(unsigned char objectIndex) {
0204   m_combinations.push_back(objectIndex);
0205 }
0206 
0207 void L1GlobalTriggerObjectMaps::setNamesParameterSetID(edm::ParameterSetID const& psetID) {
0208   m_namesParameterSetID = psetID;
0209 }
0210 
0211 L1GlobalTriggerObjectMaps::AlgorithmResult::AlgorithmResult()
0212     : m_startIndexOfConditions(0), m_algorithmBitNumber(0), m_algorithmResult(false) {}
0213 
0214 L1GlobalTriggerObjectMaps::AlgorithmResult::AlgorithmResult(unsigned startIndexOfConditions,
0215                                                             int algorithmBitNumber,
0216                                                             bool algorithmResult)
0217     : m_startIndexOfConditions(startIndexOfConditions), m_algorithmResult(algorithmResult) {
0218   // We made the decision to try to save space in the data format
0219   // and fit this object into 8 bytes by making the persistent
0220   // algorithmBitNumber a short. This creates something very
0221   // ugly below.  In practice the range should never be exceeded.
0222   // In fact it is currently always supposed to be less than 128.
0223   // I hope this never comes back to haunt us for some unexpected reason.
0224   // I cringe when I look at it, but cannot think of any practical
0225   // harm ... It is probably a real bug if anyone ever
0226   // tries to shove a big int into here.
0227   if (algorithmBitNumber < std::numeric_limits<short>::min() ||
0228       algorithmBitNumber > std::numeric_limits<short>::max()) {
0229     cms::Exception ex("L1GlobalTrigger");
0230     ex << "algorithmBitNumber out of range of a short int";
0231     ex.addContext("Calling L1GlobalTriggerObjectMaps::AlgorithmResult::AlgorithmResult");
0232     throw ex;
0233   }
0234   m_algorithmBitNumber = static_cast<short>(algorithmBitNumber);
0235 }
0236 
0237 L1GlobalTriggerObjectMaps::ConditionResult::ConditionResult()
0238     : m_startIndexOfCombinations(0), m_nObjectsPerCombination(0), m_conditionResult(false) {}
0239 
0240 L1GlobalTriggerObjectMaps::ConditionResult::ConditionResult(unsigned startIndexOfCombinations,
0241                                                             unsigned short nObjectsPerCombination,
0242                                                             bool conditionResult)
0243     : m_startIndexOfCombinations(startIndexOfCombinations),
0244       m_nObjectsPerCombination(nObjectsPerCombination),
0245       m_conditionResult(conditionResult) {}
0246 
0247 L1GlobalTriggerObjectMaps::ConditionsInAlgorithm::ConditionsInAlgorithm(ConditionResult const* conditionResults,
0248                                                                         unsigned nConditions)
0249     : m_conditionResults(conditionResults), m_nConditions(nConditions) {}
0250 
0251 bool L1GlobalTriggerObjectMaps::ConditionsInAlgorithm::getConditionResult(unsigned condition) const {
0252   if (condition >= m_nConditions) {
0253     cms::Exception ex("L1GlobalTrigger");
0254     ex << "argument out of range";
0255     ex.addContext("Calling L1GlobalTriggerObjectMaps::ConditionsInAlgorithm::getConditionResult");
0256     throw ex;
0257   }
0258   return (m_conditionResults + condition)->conditionResult();
0259 }
0260 
0261 L1GlobalTriggerObjectMaps::CombinationsInCondition::CombinationsInCondition(unsigned char const* startOfObjectIndexes,
0262                                                                             unsigned nCombinations,
0263                                                                             unsigned short nObjectsPerCombination)
0264     : m_startOfObjectIndexes(startOfObjectIndexes),
0265       m_nCombinations(nCombinations),
0266       m_nObjectsPerCombination(nObjectsPerCombination) {}
0267 
0268 unsigned char L1GlobalTriggerObjectMaps::CombinationsInCondition::getObjectIndex(unsigned combination,
0269                                                                                  unsigned object) const {
0270   if (combination >= m_nCombinations || object >= m_nObjectsPerCombination) {
0271     cms::Exception ex("L1GlobalTrigger");
0272     ex << "arguments out of range";
0273     ex.addContext("Calling L1GlobalTriggerObjectMaps::CombinationsInCondition::getObjectIndex");
0274     throw ex;
0275   }
0276   return m_startOfObjectIndexes[combination * m_nObjectsPerCombination + object];
0277 }
0278 
0279 void L1GlobalTriggerObjectMaps::getStartEndIndex(int algorithmBitNumber,
0280                                                  unsigned& startIndex,
0281                                                  unsigned& endIndex) const {
0282   std::vector<AlgorithmResult>::const_iterator iAlgo = std::lower_bound(
0283       m_algorithmResults.begin(), m_algorithmResults.end(), AlgorithmResult(0, algorithmBitNumber, false));
0284 
0285   if (iAlgo == m_algorithmResults.end() || iAlgo->algorithmBitNumber() != algorithmBitNumber) {
0286     cms::Exception ex("L1GlobalTrigger");
0287     ex << "algorithmBitNumber not found";
0288     ex.addContext("Calling L1GlobalTriggerObjectMaps::getStartEndIndex");
0289     throw ex;
0290   }
0291 
0292   startIndex = iAlgo->startIndexOfConditions();
0293   ++iAlgo;
0294   endIndex = (iAlgo != m_algorithmResults.end()) ? iAlgo->startIndexOfConditions() : m_conditionResults.size();
0295 
0296   if (endIndex < startIndex || m_conditionResults.size() < endIndex) {
0297     cms::Exception ex("L1GlobalTrigger");
0298     ex << "index out of order or out of range";
0299     ex.addContext("Calling L1GlobalTriggerObjectMaps::getStartEndIndex");
0300     throw ex;
0301   }
0302 }