Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:22:18

0001 /**
0002  * \class ConvertObjectMapRecord
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 "ConvertObjectMapRecord.h"
0017 
0018 #include <limits>
0019 #include <memory>
0020 #include <string>
0021 #include <vector>
0022 
0023 #include "DataFormats/Common/interface/Handle.h"
0024 #include "DataFormats/L1GlobalTrigger/interface/L1GlobalTriggerObjectMap.h"
0025 #include "DataFormats/L1GlobalTrigger/interface/L1GlobalTriggerObjectMapFwd.h"
0026 #include "DataFormats/L1GlobalTrigger/interface/L1GlobalTriggerObjectMaps.h"
0027 #include "DataFormats/L1GlobalTrigger/interface/L1GtLogicParser.h"
0028 #include "FWCore/Framework/interface/Event.h"
0029 #include "FWCore/Framework/interface/MakerMacros.h"
0030 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0031 #include "FWCore/Utilities/interface/Algorithms.h"
0032 #include "FWCore/Utilities/interface/Exception.h"
0033 
0034 ConvertObjectMapRecord::ConvertObjectMapRecord(const edm::ParameterSet &pset)
0035     : m_l1GtObjectMapToken(
0036           consumes<L1GlobalTriggerObjectMapRecord>(pset.getParameter<edm::InputTag>("L1GtObjectMapTag"))) {
0037   produces<L1GlobalTriggerObjectMaps>();
0038 }
0039 
0040 ConvertObjectMapRecord::~ConvertObjectMapRecord() {}
0041 
0042 void ConvertObjectMapRecord::produce(edm::Event &event, const edm::EventSetup &es) {
0043   // Read in the existing object from the data
0044   edm::Handle<L1GlobalTriggerObjectMapRecord> gtObjectMapRecord;
0045   event.getByToken(m_l1GtObjectMapToken, gtObjectMapRecord);
0046 
0047   if (!gtObjectMapRecord.isValid()) {
0048     return;
0049   }
0050 
0051   // Create the new object we are going to copy the information to
0052   std::unique_ptr<L1GlobalTriggerObjectMaps> gtObjectMaps(new L1GlobalTriggerObjectMaps);
0053 
0054   // get the algorithm bit numbers and sort them
0055   std::vector<int> algoBitNumbers;
0056   std::vector<L1GlobalTriggerObjectMap> const &vectorInRecord = gtObjectMapRecord->gtObjectMap();
0057   algoBitNumbers.reserve(vectorInRecord.size());
0058   for (std::vector<L1GlobalTriggerObjectMap>::const_iterator i = vectorInRecord.begin(), iEnd = vectorInRecord.end();
0059        i != iEnd;
0060        ++i) {
0061     algoBitNumbers.push_back(i->algoBitNumber());
0062   }
0063   edm::sort_all(algoBitNumbers);
0064 
0065   gtObjectMaps->reserveForAlgorithms(algoBitNumbers.size());
0066 
0067   if (!algoBitNumbers.empty() && algoBitNumbers[0] < 0) {
0068     cms::Exception ex("L1GlobalTrigger");
0069     ex << "Negative algorithm bit number";
0070     ex.addContext("Calling ConvertObjectMapRecord::produce");
0071     throw ex;
0072   }
0073 
0074   unsigned sizeOfNamesVector = 0;
0075   if (!algoBitNumbers.empty()) {
0076     sizeOfNamesVector = static_cast<unsigned>(algoBitNumbers.back()) + 1;
0077   }
0078   std::vector<std::string> savedNames(sizeOfNamesVector);
0079 
0080   // Loop over the object map record and copy the algorithm information
0081   // Just count the condition and index information so we can reserve
0082   // memory for them before filling them.
0083   unsigned startIndexOfConditions = 0;
0084   unsigned nIndexes = 0;
0085 
0086   for (std::vector<int>::const_iterator iBit = algoBitNumbers.begin(), endBits = algoBitNumbers.end(); iBit != endBits;
0087        ++iBit) {
0088     L1GlobalTriggerObjectMap const *objMap = gtObjectMapRecord->getObjectMap(*iBit);
0089 
0090     gtObjectMaps->pushBackAlgorithm(startIndexOfConditions, objMap->algoBitNumber(), objMap->algoGtlResult());
0091 
0092     savedNames.at(static_cast<unsigned>(*iBit)) = objMap->algoName();
0093 
0094     std::vector<L1GtLogicParser::OperandToken> const &operandTokens = objMap->operandTokenVector();
0095 
0096     startIndexOfConditions += operandTokens.size();
0097 
0098     int tokenCounter = 0;
0099     for (std::vector<L1GtLogicParser::OperandToken>::const_iterator iToken = operandTokens.begin(),
0100                                                                     endTokens = operandTokens.end();
0101          iToken != endTokens;
0102          ++iToken, ++tokenCounter) {
0103       if (tokenCounter != iToken->tokenNumber) {
0104         cms::Exception ex("L1GlobalTrigger");
0105         ex << "Token numbers not sequential";
0106         ex.addContext("Calling ConvertObjectMapRecord::produce");
0107         throw ex;
0108       }
0109 
0110       CombinationsInCond const *combos = objMap->getCombinationsInCond(iToken->tokenNumber);
0111       for (CombinationsInCond::const_iterator iCombo = combos->begin(), endCombos = combos->end(); iCombo != endCombos;
0112            ++iCombo) {
0113         for (std::vector<int>::const_iterator iIndex = iCombo->begin(), endIndexes = iCombo->end();
0114              iIndex != endIndexes;
0115              ++iIndex) {
0116           ++nIndexes;
0117         }
0118       }
0119     }
0120   }
0121   gtObjectMaps->reserveForConditions(startIndexOfConditions);
0122   gtObjectMaps->reserveForObjectIndexes(nIndexes);
0123 
0124   edm::ParameterSet namesPset;
0125   namesPset.addParameter<std::vector<std::string>>(std::string("@algorithmNames"), savedNames);
0126 
0127   // Now loop a second time and fill the condition and index
0128   // information.
0129   unsigned startIndexOfCombinations = 0;
0130   for (std::vector<int>::const_iterator iBit = algoBitNumbers.begin(), endBits = algoBitNumbers.end(); iBit != endBits;
0131        ++iBit) {
0132     L1GlobalTriggerObjectMap const *objMap = gtObjectMapRecord->getObjectMap(*iBit);
0133 
0134     std::vector<L1GtLogicParser::OperandToken> const &operandTokens = objMap->operandTokenVector();
0135 
0136     savedNames.clear();
0137     if (savedNames.capacity() < operandTokens.size()) {
0138       savedNames.reserve(operandTokens.size());
0139     }
0140 
0141     for (std::vector<L1GtLogicParser::OperandToken>::const_iterator iToken = operandTokens.begin(),
0142                                                                     endTokens = operandTokens.end();
0143          iToken != endTokens;
0144          ++iToken) {
0145       savedNames.push_back(iToken->tokenName);
0146 
0147       unsigned short nObjectsPerCombination = 0;
0148       bool first = true;
0149       unsigned nIndexesInCombination = 0;
0150 
0151       CombinationsInCond const *combos = objMap->getCombinationsInCond(iToken->tokenNumber);
0152       for (CombinationsInCond::const_iterator iCombo = combos->begin(), endCombos = combos->end(); iCombo != endCombos;
0153            ++iCombo) {
0154         if (first) {
0155           if (iCombo->size() > std::numeric_limits<unsigned short>::max()) {
0156             cms::Exception ex("L1GlobalTrigger");
0157             ex << "Number of objects per combination out of range";
0158             ex.addContext("Calling ConvertObjectMapRecord::produce");
0159             throw ex;
0160           }
0161           nObjectsPerCombination = iCombo->size();
0162           first = false;
0163         } else {
0164           if (nObjectsPerCombination != iCombo->size()) {
0165             cms::Exception ex("L1GlobalTrigger");
0166             ex << "inconsistent number of objects per condition";
0167             ex.addContext("Calling ConvertObjectMapRecord::produce");
0168             throw ex;
0169           }
0170         }
0171 
0172         for (std::vector<int>::const_iterator iIndex = iCombo->begin(), endIndexes = iCombo->end();
0173              iIndex != endIndexes;
0174              ++iIndex) {
0175           if (*iIndex < 0 || *iIndex > std::numeric_limits<unsigned char>::max()) {
0176             cms::Exception ex("L1GlobalTrigger");
0177             ex << "object index too large, out of range";
0178             ex.addContext("Calling ConvertObjectMapRecord::produce");
0179             throw ex;
0180           }
0181           gtObjectMaps->pushBackObjectIndex(*iIndex);
0182           ++nIndexesInCombination;
0183         }
0184       }
0185       gtObjectMaps->pushBackCondition(startIndexOfCombinations, nObjectsPerCombination, iToken->tokenResult);
0186       startIndexOfCombinations += nIndexesInCombination;
0187     }
0188     namesPset.addParameter<std::vector<std::string>>(objMap->algoName(), savedNames);
0189   }
0190   namesPset.registerIt();
0191   gtObjectMaps->setNamesParameterSetID(namesPset.id());
0192 
0193   gtObjectMaps->consistencyCheck();
0194   event.put(std::move(gtObjectMaps));
0195 }
0196 
0197 DEFINE_FWK_MODULE(ConvertObjectMapRecord);