CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_13_patch3/src/DataFormats/L1GlobalTrigger/src/L1GlobalTriggerObjectMaps.cc

Go to the documentation of this file.
00001 
00018 #include "DataFormats/L1GlobalTrigger/interface/L1GlobalTriggerObjectMaps.h"
00019 
00020 #include <algorithm>
00021 #include <limits>
00022 
00023 #include "FWCore/Utilities/interface/Exception.h"
00024 
00025 void L1GlobalTriggerObjectMaps::swap(L1GlobalTriggerObjectMaps& rh) {
00026   m_algorithmResults.swap(rh.m_algorithmResults);
00027   m_conditionResults.swap(rh.m_conditionResults);
00028   m_combinations.swap(rh.m_combinations);
00029   m_namesParameterSetID.swap(rh.m_namesParameterSetID);
00030 }
00031 
00032 bool L1GlobalTriggerObjectMaps::algorithmExists(int algorithmBitNumber) const {
00033   std::vector<AlgorithmResult>::const_iterator i =
00034     std::lower_bound(m_algorithmResults.begin(),
00035                      m_algorithmResults.end(),
00036                      AlgorithmResult(0, algorithmBitNumber, false));
00037   if (i == m_algorithmResults.end() || i->algorithmBitNumber() != algorithmBitNumber) {
00038     return false;
00039   }
00040   return true;
00041 }
00042 
00043 bool L1GlobalTriggerObjectMaps::algorithmResult(int algorithmBitNumber) const {
00044   std::vector<AlgorithmResult>::const_iterator i =
00045     std::lower_bound(m_algorithmResults.begin(),
00046                      m_algorithmResults.end(),
00047                      AlgorithmResult(0, algorithmBitNumber, false));
00048   if (i == m_algorithmResults.end() || i->algorithmBitNumber() != algorithmBitNumber) {
00049     cms::Exception ex("L1GlobalTrigger");
00050     ex << "algorithmBitNumber not found";
00051     ex.addContext("Calling L1GlobalTriggerObjectMaps::algorithmResult");
00052     throw ex;
00053   }
00054   return i->algorithmResult();
00055 }
00056 
00057 void L1GlobalTriggerObjectMaps::
00058 updateOperandTokenVector(int algorithmBitNumber,
00059                          std::vector<L1GtLogicParser::OperandToken>& operandTokenVector) const {
00060   unsigned startIndex = 0;
00061   unsigned endIndex = 0;
00062   getStartEndIndex(algorithmBitNumber, startIndex, endIndex);
00063 
00064   unsigned length = endIndex - startIndex;
00065   if (length != operandTokenVector.size()) {
00066     cms::Exception ex("L1GlobalTrigger");
00067     ex << "operand token vector size does not match number of conditions";
00068     ex.addContext("Calling L1GlobalTriggerObjectMaps::updateOperandTokenVector");
00069     throw ex;
00070   }
00071 
00072   for (unsigned i = 0; i < length; ++i) {
00073     operandTokenVector[i].tokenResult = m_conditionResults[startIndex + i].conditionResult();
00074   }
00075 }
00076 
00077 void L1GlobalTriggerObjectMaps::
00078 getAlgorithmBitNumbers(std::vector<int>& algorithmBitNumbers) const {
00079   algorithmBitNumbers.clear();
00080   for (std::vector<AlgorithmResult>::const_iterator i = m_algorithmResults.begin(),
00081        iEnd = m_algorithmResults.end(); i != iEnd; ++i) {
00082     algorithmBitNumbers.push_back(i->algorithmBitNumber());
00083   }
00084 }
00085 
00086 unsigned L1GlobalTriggerObjectMaps::
00087 getNumberOfConditions(int algorithmBitNumber) const {
00088 
00089   unsigned startIndex = 0;
00090   unsigned endIndex = 0;
00091   getStartEndIndex(algorithmBitNumber, startIndex, endIndex);
00092   return endIndex - startIndex;
00093 }
00094 
00095 L1GlobalTriggerObjectMaps::ConditionsInAlgorithm L1GlobalTriggerObjectMaps::
00096 getConditionsInAlgorithm(int algorithmBitNumber) const {
00097   unsigned startIndex = 0;
00098   unsigned endIndex = 0;
00099   getStartEndIndex(algorithmBitNumber, startIndex, endIndex);
00100   return ConditionsInAlgorithm(&m_conditionResults[startIndex], endIndex - startIndex);
00101 }
00102 
00103 L1GlobalTriggerObjectMaps::CombinationsInCondition L1GlobalTriggerObjectMaps::
00104 getCombinationsInCondition(int algorithmBitNumber,
00105                            unsigned conditionNumber) const {
00106   unsigned startIndex = 0;
00107   unsigned endIndex = 0;
00108   getStartEndIndex(algorithmBitNumber, startIndex, endIndex);
00109 
00110   if (endIndex <= startIndex + conditionNumber) {
00111     cms::Exception ex("L1GlobalTrigger");
00112     ex << "Condition number is out of range";
00113     ex.addContext("Calling L1GlobalTriggerObjectMaps::getCombinationsInCondition");
00114     throw ex;
00115   }
00116 
00117   unsigned endObjectIndex = m_combinations.size();
00118   unsigned nextConditionIndex = startIndex + conditionNumber + 1U;
00119   if (nextConditionIndex < m_conditionResults.size()) {
00120     endObjectIndex = m_conditionResults[nextConditionIndex].startIndexOfCombinations();
00121   }
00122   unsigned beginObjectIndex = m_conditionResults[startIndex + conditionNumber].startIndexOfCombinations();
00123   unsigned short nObjectsPerCombination = m_conditionResults[startIndex + conditionNumber].nObjectsPerCombination();
00124 
00125   if (endObjectIndex == beginObjectIndex) {
00126     return CombinationsInCondition(0, 0, 0);
00127   }
00128   if (endObjectIndex < beginObjectIndex ||
00129       m_combinations.size() < endObjectIndex ||
00130       nObjectsPerCombination == 0 ||
00131       (endObjectIndex - beginObjectIndex) % nObjectsPerCombination != 0) {
00132     cms::Exception ex("L1GlobalTrigger");
00133     ex << "Indexes to combinations are invalid";
00134     ex.addContext("Calling L1GlobalTriggerObjectMaps::getCombinationsInCondition");
00135     throw ex;
00136   }
00137   return CombinationsInCondition(&m_combinations[beginObjectIndex],
00138                                  (endObjectIndex - beginObjectIndex) / nObjectsPerCombination,
00139                                   nObjectsPerCombination);
00140 }
00141 
00142 void L1GlobalTriggerObjectMaps::
00143 reserveForAlgorithms(unsigned n) {
00144   m_algorithmResults.reserve(n);
00145 }
00146 
00147 void L1GlobalTriggerObjectMaps::
00148 pushBackAlgorithm(unsigned startIndexOfConditions,
00149                   int algorithmBitNumber,
00150                   bool algorithmResult) {
00151   m_algorithmResults.push_back(AlgorithmResult(startIndexOfConditions,
00152                                                algorithmBitNumber,
00153                                                algorithmResult));
00154 }
00155 
00156 void L1GlobalTriggerObjectMaps::consistencyCheck() const {
00157   // None of these checks should ever fail unless there
00158   // is a bug in the code filling this object
00159   for (std::vector<AlgorithmResult>::const_iterator i = m_algorithmResults.begin(),
00160          iEnd = m_algorithmResults.end(); i != iEnd; ++i) {
00161     std::vector<AlgorithmResult>::const_iterator j = i;
00162     ++j;
00163     if (j != iEnd && !(*i < *j)) {
00164       cms::Exception ex("L1GlobalTrigger");
00165       ex << "AlgorithmResults should be sorted in increasing order of bit number with no duplicates. It is not.";
00166       ex.addContext("Calling L1GlobalTriggerObjectMaps::consistencyCheck");
00167       throw ex;      
00168     }
00169     unsigned endIndex = 
00170       (j != iEnd) ? j->startIndexOfConditions() : m_conditionResults.size();
00171 
00172     if (endIndex < i->startIndexOfConditions()) {
00173       cms::Exception ex("L1GlobalTrigger");
00174       ex << "startIndexOfConditions decreases or exceeds the size of m_conditionResults";
00175       ex.addContext("Calling L1GlobalTriggerObjectMaps::consistencyCheck");
00176       throw ex;
00177     }
00178   }
00179   for (std::vector<ConditionResult>::const_iterator i = m_conditionResults.begin(),
00180          iEnd = m_conditionResults.end(); i != iEnd; ++i) {
00181     std::vector<ConditionResult>::const_iterator j = i;
00182     ++j;
00183     unsigned endIndex = 
00184       (j != iEnd) ? j->startIndexOfCombinations() : m_combinations.size();
00185 
00186     if (endIndex < i->startIndexOfCombinations()) {
00187       cms::Exception ex("L1GlobalTrigger");
00188       ex << "startIndexOfCombinations decreases or exceeds the size of m_conditionResults";
00189       ex.addContext("Calling L1GlobalTriggerObjectMaps::consistencyCheck");
00190       throw ex;
00191     }
00192     unsigned length = endIndex - i->startIndexOfCombinations();
00193     if (length == 0U) {
00194       if (i->nObjectsPerCombination() != 0U) {
00195         cms::Exception ex("L1GlobalTrigger");
00196         ex << "Length is zero and nObjectsInCombination is not zero";
00197         ex.addContext("Calling L1GlobalTriggerObjectMaps::consistencyCheck");
00198         throw ex;
00199       }
00200     } else {
00201       if (i->nObjectsPerCombination() == 0 || length % i->nObjectsPerCombination() != 0) {
00202         cms::Exception ex("L1GlobalTrigger");
00203         ex << "Size indicated by startIndexOfCombinations is not a multiple of nObjectsInCombination";
00204         ex.addContext("Calling L1GlobalTriggerObjectMaps::consistencyCheck");
00205         throw ex;
00206       }
00207     }
00208   }
00209 }
00210 
00211 void L1GlobalTriggerObjectMaps::
00212 reserveForConditions(unsigned n) {
00213   m_conditionResults.reserve(n);
00214 }
00215 
00216 void L1GlobalTriggerObjectMaps::
00217 pushBackCondition(unsigned startIndexOfCombinations,
00218                   unsigned short nObjectsPerCombination,
00219                   bool conditionResult) {
00220   m_conditionResults.push_back(ConditionResult(startIndexOfCombinations,
00221                                                nObjectsPerCombination,
00222                                                conditionResult));
00223 }
00224 
00225 void L1GlobalTriggerObjectMaps::
00226 reserveForObjectIndexes(unsigned n) {
00227   m_combinations.reserve(n);
00228 }
00229 
00230 void L1GlobalTriggerObjectMaps::
00231 pushBackObjectIndex(unsigned char objectIndex) {
00232   m_combinations.push_back(objectIndex);
00233 }
00234 
00235 void L1GlobalTriggerObjectMaps::
00236 setNamesParameterSetID(edm::ParameterSetID const& psetID) {
00237   m_namesParameterSetID = psetID;
00238 }
00239 
00240 L1GlobalTriggerObjectMaps::AlgorithmResult::
00241 AlgorithmResult() :
00242   m_startIndexOfConditions(0),
00243   m_algorithmBitNumber(0),
00244   m_algorithmResult(false) {
00245 }
00246 
00247 L1GlobalTriggerObjectMaps::AlgorithmResult::
00248 AlgorithmResult(unsigned startIndexOfConditions,
00249                 int algorithmBitNumber,
00250                 bool algorithmResult) :
00251   m_startIndexOfConditions(startIndexOfConditions),
00252   m_algorithmResult(algorithmResult) {
00253 
00254   // We made the decision to try to save space in the data format
00255   // and fit this object into 8 bytes by making the persistent
00256   // algorithmBitNumber a short. This creates something very
00257   // ugly below.  In practice the range should never be exceeded.
00258   // In fact it is currently always supposed to be less than 128.
00259   // I hope this never comes back to haunt us for some unexpected reason.
00260   // I cringe when I look at it, but cannot think of any practical
00261   // harm ... It is probably a real bug if anyone ever
00262   // tries to shove a big int into here.
00263   if (algorithmBitNumber < std::numeric_limits<short>::min() ||
00264       algorithmBitNumber > std::numeric_limits<short>::max()) {
00265     cms::Exception ex("L1GlobalTrigger");
00266     ex << "algorithmBitNumber out of range of a short int";
00267     ex.addContext("Calling L1GlobalTriggerObjectMaps::AlgorithmResult::AlgorithmResult");
00268     throw ex;    
00269   }
00270   m_algorithmBitNumber = static_cast<short>(algorithmBitNumber);
00271 }
00272 
00273 L1GlobalTriggerObjectMaps::ConditionResult::
00274 ConditionResult() :
00275   m_startIndexOfCombinations(0),
00276   m_nObjectsPerCombination(0),
00277   m_conditionResult(false) {
00278 }
00279 
00280 L1GlobalTriggerObjectMaps::ConditionResult::
00281 ConditionResult(unsigned startIndexOfCombinations,
00282                 unsigned short nObjectsPerCombination,
00283                 bool conditionResult) :
00284   m_startIndexOfCombinations(startIndexOfCombinations),
00285   m_nObjectsPerCombination(nObjectsPerCombination),
00286   m_conditionResult(conditionResult) {
00287 }
00288 
00289 L1GlobalTriggerObjectMaps::ConditionsInAlgorithm::
00290 ConditionsInAlgorithm(ConditionResult const* conditionResults,
00291                       unsigned nConditions) :
00292   m_conditionResults(conditionResults),
00293   m_nConditions(nConditions) {
00294 }
00295 
00296 bool L1GlobalTriggerObjectMaps::ConditionsInAlgorithm::
00297 getConditionResult(unsigned condition) const {
00298   if (condition >= m_nConditions) {
00299     cms::Exception ex("L1GlobalTrigger");
00300     ex << "argument out of range";
00301     ex.addContext("Calling L1GlobalTriggerObjectMaps::ConditionsInAlgorithm::getConditionResult");
00302     throw ex;
00303   }
00304   return (m_conditionResults + condition)->conditionResult();
00305 }
00306 
00307 L1GlobalTriggerObjectMaps::CombinationsInCondition::
00308 CombinationsInCondition(unsigned char const* startOfObjectIndexes,
00309                         unsigned nCombinations,
00310                         unsigned short nObjectsPerCombination) :
00311   m_startOfObjectIndexes(startOfObjectIndexes),
00312   m_nCombinations(nCombinations),
00313   m_nObjectsPerCombination(nObjectsPerCombination) {
00314 }
00315 
00316 unsigned char L1GlobalTriggerObjectMaps::CombinationsInCondition::
00317 getObjectIndex(unsigned combination,
00318                unsigned object) const {
00319   if (combination >= m_nCombinations ||
00320       object >=  m_nObjectsPerCombination) {
00321     cms::Exception ex("L1GlobalTrigger");
00322     ex << "arguments out of range";
00323     ex.addContext("Calling L1GlobalTriggerObjectMaps::CombinationsInCondition::getObjectIndex");
00324     throw ex;
00325   }
00326   return m_startOfObjectIndexes[combination * m_nObjectsPerCombination + object];
00327 }
00328 
00329 void L1GlobalTriggerObjectMaps::
00330 getStartEndIndex(int algorithmBitNumber, unsigned& startIndex, unsigned& endIndex) const {
00331   std::vector<AlgorithmResult>::const_iterator iAlgo =
00332     std::lower_bound(m_algorithmResults.begin(),
00333                      m_algorithmResults.end(),
00334                      AlgorithmResult(0, algorithmBitNumber, false));
00335 
00336   if (iAlgo == m_algorithmResults.end() || iAlgo->algorithmBitNumber() != algorithmBitNumber) {
00337     cms::Exception ex("L1GlobalTrigger");
00338     ex << "algorithmBitNumber not found";
00339     ex.addContext("Calling L1GlobalTriggerObjectMaps::getStartEndIndex");
00340     throw ex;
00341   }
00342 
00343   startIndex = iAlgo->startIndexOfConditions();
00344   ++iAlgo;
00345   endIndex = (iAlgo != m_algorithmResults.end()) ? 
00346              iAlgo->startIndexOfConditions() : m_conditionResults.size();
00347 
00348   if (endIndex < startIndex || m_conditionResults.size() < endIndex) {
00349     cms::Exception ex("L1GlobalTrigger");
00350     ex << "index out of order or out of range";
00351     ex.addContext("Calling L1GlobalTriggerObjectMaps::getStartEndIndex");
00352     throw ex;
00353   }
00354 }