Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-03-25 23:59:50

0001 #include <iostream>
0002 #include <sstream>
0003 #include <istream>
0004 #include <fstream>
0005 #include <iomanip>
0006 #include <string>
0007 #include <cmath>
0008 #include <functional>
0009 #include <cstdlib>
0010 #include <cstring>
0011 
0012 #include "HLTInfo.h"
0013 #include "FWCore/Common/interface/TriggerNames.h"
0014 
0015 // L1 related
0016 #include "HLTrigger/HLTcore/interface/HLTConfigProvider.h"
0017 #include "L1Trigger/GlobalTriggerAnalyzer/interface/L1GtUtils.h"
0018 
0019 //static const bool useL1EventSetup(true);
0020 //static const bool useL1GtTriggerMenuLite(false);
0021 
0022 HLTInfo::HLTInfo() {
0023   //set parameter defaults
0024   _Debug = false;
0025   _OR_BXes = false;
0026   UnpackBxInEvent = 1;
0027 }
0028 
0029 void HLTInfo::beginRun(const edm::Run& run, const edm::EventSetup& c) {
0030   bool changed(true);
0031   if (hltPrescaleProvider_->init(run, c, processName_, changed)) {
0032     // if init returns TRUE, initialisation has succeeded!
0033     if (changed) {
0034       // The HLT config has actually changed wrt the previous Run, hence rebook your
0035       // histograms or do anything else dependent on the revised HLT config
0036       std::cout << "Initalizing HLTConfigProvider" << std::endl;
0037     }
0038   } else {
0039     // if init returns FALSE, initialisation has NOT succeeded, which indicates a problem
0040     // with the file and/or code and needs to be investigated!
0041     std::cout << " HLT config extraction failure with process name " << processName_ << std::endl;
0042     // In this case, all access methods will return empty values!
0043   }
0044 }
0045 
0046 /*  Setup the analysis to put the branch-variables into the tree. */
0047 void HLTInfo::setup(const edm::ParameterSet& pSet, TTree* HltTree) {
0048   processName_ = pSet.getParameter<std::string>("HLTProcessName");
0049 
0050   edm::ParameterSet myHltParams = pSet.getParameter<edm::ParameterSet>("RunParameters");
0051   std::vector<std::string> parameterNames = myHltParams.getParameterNames();
0052 
0053   for (auto& parameterName : parameterNames) {
0054     if (parameterName == "Debug")
0055       _Debug = myHltParams.getParameter<bool>(parameterName);
0056   }
0057 
0058   dummyBranches_ = pSet.getUntrackedParameter<std::vector<std::string> >("dummyBranches", std::vector<std::string>(0));
0059 
0060   HltEvtCnt = 0;
0061   const int kMaxTrigFlag = 10000;
0062   trigflag = new int[kMaxTrigFlag];
0063   trigPrescl = new int[kMaxTrigFlag];
0064 
0065   L1EvtCnt = 0;
0066   const int kMaxL1Flag = 10000;
0067   l1flag = new int[kMaxL1Flag];
0068   l1flag5Bx = new int[kMaxTrigFlag];
0069   l1Prescl = new int[kMaxL1Flag];
0070 
0071   l1techflag = new int[kMaxL1Flag];
0072   //  l1techflag5Bx = new int[kMaxTrigFlag];
0073   l1techPrescl = new int[kMaxTrigFlag];
0074 
0075   const int kMaxHLTPart = 10000;
0076   hltppt = new float[kMaxHLTPart];
0077   hltpeta = new float[kMaxHLTPart];
0078 
0079   algoBitToName = new TString[512];
0080   techBitToName = new TString[512];
0081 }
0082 
0083 /* **Analyze the event** */
0084 void HLTInfo::analyze(const edm::Handle<edm::TriggerResults>& hltresults,
0085                       const edm::Handle<GlobalAlgBlkBxCollection>& l1results,
0086                       edm::EventSetup const& eventSetup,
0087                       edm::Event const& iEvent,
0088                       TTree* HltTree) {
0089   //   std::cout << " Beginning HLTInfo " << std::endl;
0090 
0091   /////////// Analyzing HLT Trigger Results (TriggerResults) //////////
0092   if (hltresults.isValid()) {
0093     int ntrigs = hltresults->size();
0094     if (ntrigs == 0) {
0095       std::cout << "%HLTInfo -- No trigger name given in TriggerResults of the input " << std::endl;
0096     }
0097 
0098     edm::TriggerNames const& triggerNames = iEvent.triggerNames(*hltresults);
0099 
0100     // 1st event : Book as many branches as trigger paths provided in the input...
0101     if (HltEvtCnt == 0) {
0102       for (int itrig = 0; itrig != ntrigs; ++itrig) {
0103         TString trigName = triggerNames.triggerName(itrig);
0104         HltTree->Branch(trigName, trigflag + itrig, trigName + "/I");
0105         HltTree->Branch(trigName + "_Prescl", trigPrescl + itrig, trigName + "_Prescl/I");
0106       }
0107 
0108       int itdum = ntrigs;
0109       for (auto& dummyBranche : dummyBranches_) {
0110         TString trigName(dummyBranche.data());
0111         bool addThisBranch = true;
0112         for (int itrig = 0; itrig != ntrigs; ++itrig) {
0113           TString realTrigName = triggerNames.triggerName(itrig);
0114           if (trigName == realTrigName)
0115             addThisBranch = false;
0116         }
0117         if (addThisBranch) {
0118           HltTree->Branch(trigName, trigflag + itdum, trigName + "/I");
0119           HltTree->Branch(trigName + "_Prescl", trigPrescl + itdum, trigName + "_Prescl/I");
0120           trigflag[itdum] = 0;
0121           trigPrescl[itdum] = 0;
0122           ++itdum;
0123         }
0124       }
0125 
0126       HltEvtCnt++;
0127     }
0128     // ...Fill the corresponding accepts in branch-variables
0129 
0130     //std::cout << "Number of prescale sets: " << hltConfig_.prescaleSize() << std::endl;
0131     //std::cout << "Number of HLT paths: " << hltConfig_.size() << std::endl;
0132     //int presclSet = hltConfig_.prescaleSet(iEvent, eventSetup);
0133     //std::cout<<"\tPrescale set number: "<< presclSet <<std::endl;
0134 
0135     for (int itrig = 0; itrig != ntrigs; ++itrig) {
0136       const std::string& trigName = triggerNames.triggerName(itrig);
0137       bool accept = hltresults->accept(itrig);
0138 
0139       //trigPrescl[itrig] = hltConfig_.prescaleValue(iEvent, eventSetup, trigName);
0140       trigPrescl[itrig] = hltPrescaleProvider_->prescaleValue(iEvent, eventSetup, trigName);
0141 
0142       if (accept) {
0143         trigflag[itrig] = 1;
0144       } else {
0145         trigflag[itrig] = 0;
0146       }
0147 
0148       if (_Debug) {
0149         if (_Debug)
0150           std::cout << "%HLTInfo --  Number of HLT Triggers: " << ntrigs << std::endl;
0151         std::cout << "%HLTInfo --  HLTTrigger(" << itrig << "): " << trigName << " = " << accept << std::endl;
0152       }
0153     }
0154   } else {
0155     if (_Debug)
0156       std::cout << "%HLTInfo -- No Trigger Result" << std::endl;
0157   }
0158 
0159   //==============L1 information=======================================
0160 
0161   // L1 Triggers from Menu
0162   L1GtUtils const& l1GtUtils = hltPrescaleProvider_->l1GtUtils();
0163 
0164   //  m_l1GtUtils.retrieveL1EventSetup(eventSetup);
0165   //m_l1GtUtils.getL1GtRunCache(iEvent,eventSetup,useL1EventSetup,useL1GtTriggerMenuLite);
0166   /*
0167   unsigned long long id = eventSetup.get<L1TUtmTriggerMenuRcd>().cacheIdentifier();
0168   
0169   if (id != cache_id_) {
0170     cache_id_ = id; 
0171   */
0172   auto const& menu = eventSetup.getHandle(l1tUtmTriggerMenuToken_);
0173 
0174   //std::map<std::string, L1TUtmAlgorithm> const & algorithmMap_ = &(menu->getAlgorithmMap());
0175   /*
0176   // get the bit/name association
0177   for (auto const & keyval: menu->getAlgorithmMap()) {
0178     std::string const & name  = keyval.second.getName();
0179     unsigned int        index = keyval.second.getIndex();
0180     std::cerr << "bit: " << index << "\tname: " << name << std::endl;
0181   }
0182   */
0183   //} // end get menu
0184 
0185   int iErrorCode = -1;
0186   L1GtUtils::TriggerCategory trigCategory = L1GtUtils::AlgorithmTrigger;
0187   const int pfSetIndexAlgorithmTrigger = l1GtUtils.prescaleFactorSetIndex(iEvent, trigCategory, iErrorCode);
0188   if (iErrorCode == 0) {
0189     if (_Debug)
0190       std::cout << "%Prescale set index: " << pfSetIndexAlgorithmTrigger << std::endl;
0191   } else {
0192     std::cout << "%Could not extract Prescale set index from event record. Error code: " << iErrorCode << std::endl;
0193   }
0194 
0195   // 1st event : Book as many branches as trigger paths provided in the input...
0196   if (l1results.isValid()) {
0197     int ntrigs = l1results->size();
0198     if (ntrigs == 0) {
0199       std::cout << "%L1Results -- No trigger name given in TriggerResults of the input " << std::endl;
0200     }
0201     /*
0202     edm::TriggerNames const& triggerNames = iEvent.triggerNames(&results);
0203     // 1st event : Book as many branches as trigger paths provided in the input...
0204     */
0205     if (L1EvtCnt == 0) {
0206       // get the bit/name association
0207       for (auto const& keyval : menu->getAlgorithmMap()) {
0208         std::string const& trigName = keyval.second.getName();
0209         unsigned int index = keyval.second.getIndex();
0210         if (_Debug)
0211           std::cerr << "bit: " << index << "\tname: " << trigName << std::endl;
0212 
0213         int itrig = index;
0214         algoBitToName[itrig] = TString(trigName);
0215 
0216         TString l1trigName = static_cast<const char*>(algoBitToName[itrig]);
0217         std::string l1triggername = static_cast<const char*>(algoBitToName[itrig]);
0218 
0219         HltTree->Branch(l1trigName, l1flag + itrig, l1trigName + "/I");
0220         HltTree->Branch(l1trigName + "_Prescl", l1Prescl + itrig, l1trigName + "_Prescl/I");
0221 
0222       }  // end algo Map
0223 
0224       L1EvtCnt++;
0225     }  // end l1evtCnt=0
0226 
0227     GlobalAlgBlk const& result = l1results->at(0, 0);
0228 
0229     // get the individual decisions from the GlobalAlgBlk
0230     for (unsigned int itrig = 0; itrig < result.maxPhysicsTriggers; ++itrig) {
0231       //      std::cerr << "bit: " << itrig << "\tresult: " << results.getAlgoDecisionFinal(itrig) << std::endl;
0232 
0233       bool myflag = result.getAlgoDecisionFinal(itrig);
0234       if (myflag) {
0235         l1flag[itrig] = 1;
0236       } else {
0237         l1flag[itrig] = 0;
0238       }
0239 
0240       std::string l1triggername = static_cast<const char*>(algoBitToName[itrig]);
0241       l1Prescl[itrig] = l1GtUtils.prescaleFactor(iEvent, l1triggername, iErrorCode);
0242 
0243       if (_Debug)
0244         std::cout << "L1 TD: " << itrig << " " << algoBitToName[itrig] << " " << l1flag[itrig] << " " << l1Prescl[itrig]
0245                   << std::endl;
0246     }
0247 
0248     //    L1EvtCnt++;
0249     if (_Debug)
0250       std::cout << "%L1Info -- Done with routine" << std::endl;
0251 
0252   }  // l1results.isValid
0253   else {
0254     if (_Debug)
0255       std::cout << "%L1Results -- No Trigger Result" << std::endl;
0256   }
0257 }