Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:18:22

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 double[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] = hltPrescaleProvider_->prescaleValue<double>(iEvent, eventSetup, trigName);
0140 
0141       if (accept) {
0142         trigflag[itrig] = 1;
0143       } else {
0144         trigflag[itrig] = 0;
0145       }
0146 
0147       if (_Debug) {
0148         if (_Debug)
0149           std::cout << "%HLTInfo --  Number of HLT Triggers: " << ntrigs << std::endl;
0150         std::cout << "%HLTInfo --  HLTTrigger(" << itrig << "): " << trigName << " = " << accept << std::endl;
0151       }
0152     }
0153   } else {
0154     if (_Debug)
0155       std::cout << "%HLTInfo -- No Trigger Result" << std::endl;
0156   }
0157 
0158   //==============L1 information=======================================
0159 
0160   // L1 Triggers from Menu
0161   L1GtUtils const& l1GtUtils = hltPrescaleProvider_->l1GtUtils();
0162 
0163   //  m_l1GtUtils.retrieveL1EventSetup(eventSetup);
0164   //m_l1GtUtils.getL1GtRunCache(iEvent,eventSetup,useL1EventSetup,useL1GtTriggerMenuLite);
0165   /*
0166   unsigned long long id = eventSetup.get<L1TUtmTriggerMenuRcd>().cacheIdentifier();
0167   
0168   if (id != cache_id_) {
0169     cache_id_ = id; 
0170   */
0171   auto const& menu = eventSetup.getHandle(l1tUtmTriggerMenuToken_);
0172 
0173   //std::map<std::string, L1TUtmAlgorithm> const & algorithmMap_ = &(menu->getAlgorithmMap());
0174   /*
0175   // get the bit/name association
0176   for (auto const & keyval: menu->getAlgorithmMap()) {
0177     std::string const & name  = keyval.second.getName();
0178     unsigned int        index = keyval.second.getIndex();
0179     std::cerr << "bit: " << index << "\tname: " << name << std::endl;
0180   }
0181   */
0182   //} // end get menu
0183 
0184   int iErrorCode = -1;
0185   L1GtUtils::TriggerCategory trigCategory = L1GtUtils::AlgorithmTrigger;
0186   const int pfSetIndexAlgorithmTrigger = l1GtUtils.prescaleFactorSetIndex(iEvent, trigCategory, iErrorCode);
0187   if (iErrorCode == 0) {
0188     if (_Debug)
0189       std::cout << "%Prescale set index: " << pfSetIndexAlgorithmTrigger << std::endl;
0190   } else {
0191     std::cout << "%Could not extract Prescale set index from event record. Error code: " << iErrorCode << std::endl;
0192   }
0193 
0194   // 1st event : Book as many branches as trigger paths provided in the input...
0195   if (l1results.isValid()) {
0196     int ntrigs = l1results->size();
0197     if (ntrigs == 0) {
0198       std::cout << "%L1Results -- No trigger name given in TriggerResults of the input " << std::endl;
0199     }
0200     /*
0201     edm::TriggerNames const& triggerNames = iEvent.triggerNames(&results);
0202     // 1st event : Book as many branches as trigger paths provided in the input...
0203     */
0204     if (L1EvtCnt == 0) {
0205       // get the bit/name association
0206       for (auto const& keyval : menu->getAlgorithmMap()) {
0207         std::string const& trigName = keyval.second.getName();
0208         unsigned int index = keyval.second.getIndex();
0209         if (_Debug)
0210           std::cerr << "bit: " << index << "\tname: " << trigName << std::endl;
0211 
0212         int itrig = index;
0213         algoBitToName[itrig] = TString(trigName);
0214 
0215         TString l1trigName = static_cast<const char*>(algoBitToName[itrig]);
0216         std::string l1triggername = static_cast<const char*>(algoBitToName[itrig]);
0217 
0218         HltTree->Branch(l1trigName, l1flag + itrig, l1trigName + "/I");
0219         HltTree->Branch(l1trigName + "_Prescl", l1Prescl + itrig, l1trigName + "_Prescl/I");
0220 
0221       }  // end algo Map
0222 
0223       L1EvtCnt++;
0224     }  // end l1evtCnt=0
0225 
0226     GlobalAlgBlk const& result = l1results->at(0, 0);
0227 
0228     // get the individual decisions from the GlobalAlgBlk
0229     for (unsigned int itrig = 0; itrig < result.maxPhysicsTriggers; ++itrig) {
0230       //      std::cerr << "bit: " << itrig << "\tresult: " << results.getAlgoDecisionFinal(itrig) << std::endl;
0231 
0232       bool myflag = result.getAlgoDecisionFinal(itrig);
0233       if (myflag) {
0234         l1flag[itrig] = 1;
0235       } else {
0236         l1flag[itrig] = 0;
0237       }
0238 
0239       std::string l1triggername = static_cast<const char*>(algoBitToName[itrig]);
0240       l1Prescl[itrig] = l1GtUtils.prescaleFactor(iEvent, l1triggername, iErrorCode);
0241 
0242       if (_Debug)
0243         std::cout << "L1 TD: " << itrig << " " << algoBitToName[itrig] << " " << l1flag[itrig] << " " << l1Prescl[itrig]
0244                   << std::endl;
0245     }
0246 
0247     //    L1EvtCnt++;
0248     if (_Debug)
0249       std::cout << "%L1Info -- Done with routine" << std::endl;
0250 
0251   }  // l1results.isValid
0252   else {
0253     if (_Debug)
0254       std::cout << "%L1Results -- No Trigger Result" << std::endl;
0255   }
0256 }