Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:24:11

0001 #include <cmath>
0002 #include "DataFormats/Math/interface/deltaR.h"
0003 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0004 #include "FWCore/ServiceRegistry/interface/Service.h"
0005 #include "FWCore/Framework/interface/MakerMacros.h"
0006 #include "HLTrigger/HLTcore/interface/HLTConfigProvider.h"
0007 
0008 #include <string>
0009 
0010 template <class object>
0011 TriggerCandProducer<object>::TriggerCandProducer(const edm::ParameterSet& iConfig)
0012     : hltPrescaleProvider_(iConfig, consumesCollector(), *this) {
0013   _inputProducerToken = consumes<edm::View<object>>(_inputProducer);
0014 
0015   // **************** Trigger ******************* //
0016   const edm::InputTag dTriggerEventTag("hltTriggerSummaryAOD", "", "HLT");
0017   triggerEventToken_ = consumes<trigger::TriggerEvent>(triggerEventTag_);
0018   const edm::InputTag dTriggerResults("TriggerResults", "", "HLT");
0019 
0020   // By default, trigger results are labeled "TriggerResults" with process name "HLT" in the event.
0021   triggerResultsTag_ = iConfig.getUntrackedParameter<edm::InputTag>("triggerResultsTag", dTriggerResults);
0022   triggerResultsToken_ = consumes<edm::TriggerResults>(triggerResultsTag_);
0023 
0024   // Trigger-matching window. We use some default deltaR.
0025   delRMatchingCut_ = iConfig.getUntrackedParameter<double>("triggerDelRMatch", 0.30);
0026 
0027   // ******************************************** //
0028   //Whether using HLT trigger path name or the actual trigger filter name. Trigger path is default.
0029   objEtMin_ = iConfig.getUntrackedParameter<double>("objEtMin", 0.0);
0030   objEtaMax_ = iConfig.getUntrackedParameter<double>("objEtaMax", 1000.0);
0031   isTriggerOR_ = iConfig.getUntrackedParameter<bool>("isTriggerOR", false);
0032   isFilter_ = iConfig.getUntrackedParameter<bool>("isTriggerFilter", false);
0033   antiSelect_ = iConfig.getUntrackedParameter<bool>("antiSelect", false);
0034   noHltFiring_ = iConfig.getUntrackedParameter<bool>("noHltFiring", false);
0035 
0036   // By default, do not print debugging information for each event.
0037   printIndex_ = iConfig.getUntrackedParameter<bool>("verbose", false);
0038 
0039   // By default, perform trigger-matching with only unprescaled trigger.
0040   matchUnprescaledTriggerOnly_ = iConfig.getUntrackedParameter<bool>("matchUnprescaledTriggerOnly", true);
0041 
0042   //  produces< edm::PtrVector<object> >();
0043   produces<edm::RefToBaseVector<object>>();
0044 }
0045 
0046 template <class object>
0047 TriggerCandProducer<object>::~TriggerCandProducer() {}
0048 
0049 //
0050 // member functions
0051 //
0052 
0053 // ------------ method called to produce the data  ------------
0054 template <class object>
0055 void TriggerCandProducer<object>::produce(edm::Event& event, const edm::EventSetup& eventSetup) {
0056   using namespace std;
0057   using namespace edm;
0058   using namespace reco;
0059   using namespace trigger;
0060 
0061   HLTConfigProvider const& hltConfig = hltPrescaleProvider_.hltConfigProvider();
0062 
0063   // Create the output collection
0064   auto outColRef = std::make_unique<edm::RefToBaseVector<object>>();
0065   //auto outColPtr = std::make_unique<edm::PtrVector<object>>();
0066 
0067   //skip event if HLT paths do not have identical process names
0068   if (skipEvent_) {
0069     event.put(std::move(outColRef));
0070     //event.put(std::move(outColPtr));
0071     return;
0072   }
0073 
0074   // if user is using prescaled path, print out some loud warning message.
0075   if (!matchUnprescaledTriggerOnly_)
0076     edm::LogInfo("info")
0077         << "******** Warning: You are using a prescaled trigger for matching: Are you sure you want to do this ??? ";
0078 
0079   // Get the input collection
0080   edm::Handle<edm::View<object>> candHandle;
0081   try {
0082     event.getByToken(_inputProducerToken, candHandle);
0083   } catch (cms::Exception& ex) {
0084     edm::LogError("TriggerCandProducer") << "Error! Can't get collection: " << _inputProducer;
0085     throw ex;
0086   }
0087 
0088   // Trigger Info
0089   edm::Handle<trigger::TriggerEvent> trgEvent;
0090   event.getByToken(triggerEventToken_, trgEvent);
0091   edm::Handle<edm::TriggerResults> pTrgResults;
0092   event.getByToken(triggerResultsToken_, pTrgResults);
0093 
0094   //gracefully choose the single appropriate HLT path from the list of desired paths
0095   std::vector<std::string> activeHLTPathsInThisEvent = hltConfig.triggerNames();
0096   std::map<std::string, bool> triggerInMenu;
0097   std::map<std::string, bool> triggerUnprescaled;
0098   for (std::vector<edm::InputTag>::const_iterator iMyHLT = hltTags_.begin(); iMyHLT != hltTags_.end(); ++iMyHLT) {
0099     if (triggerInMenu.find((*iMyHLT).label()) == triggerInMenu.end())
0100       triggerInMenu[(*iMyHLT).label()] = false;
0101     if (triggerUnprescaled.find((*iMyHLT).label()) == triggerUnprescaled.end())
0102       triggerUnprescaled[(*iMyHLT).label()] = false;
0103   }
0104   for (std::vector<std::string>::const_iterator iHLT = activeHLTPathsInThisEvent.begin();
0105        iHLT != activeHLTPathsInThisEvent.end();
0106        ++iHLT) {
0107     // cout << "######## " << *iHLT << endl;
0108     for (std::vector<edm::InputTag>::const_iterator iMyHLT = hltTags_.begin(); iMyHLT != hltTags_.end(); ++iMyHLT) {
0109       if ((*iMyHLT).label() == *iHLT) {
0110         triggerInMenu[(*iMyHLT).label()] = true;
0111         if (hltPrescaleProvider_.prescaleValue<double>(event, eventSetup, *iHLT) == 1)
0112           triggerUnprescaled[(*iMyHLT).label()] = true;
0113       }
0114     }
0115   }
0116   unsigned int numOKHLTPaths = 0;
0117   for (std::map<std::string, bool>::const_iterator iMyHLT = triggerInMenu.begin(); iMyHLT != triggerInMenu.end();
0118        ++iMyHLT) {
0119     if (iMyHLT->second && triggerUnprescaled[iMyHLT->first]) {
0120       theRightHLTTag_ = edm::InputTag(iMyHLT->first, "", hltTags_[0].process());
0121       ++numOKHLTPaths;
0122     }
0123   }
0124 
0125   // in case user wants to perform matching with prescaled trigger, then choose prescale trigger
0126   if (numOKHLTPaths == 0 && !(matchUnprescaledTriggerOnly_)) {
0127     for (std::map<std::string, bool>::const_iterator iMyHLT = triggerInMenu.begin(); iMyHLT != triggerInMenu.end();
0128          ++iMyHLT) {
0129       if (iMyHLT->second) {
0130         theRightHLTTag_ = edm::InputTag(iMyHLT->first, "", hltTags_[0].process());
0131         ++numOKHLTPaths;
0132       }
0133     }
0134   }
0135 
0136   if (isFilter_) {
0137     for (std::vector<edm::InputTag>::const_iterator iMyHLT = hltTags_.begin(); iMyHLT != hltTags_.end(); ++iMyHLT) {
0138       if (theRightHLTTag_.label() == (*iMyHLT).label()) {
0139         theRightHLTTag_ = edm::InputTag((*iMyHLT).label(), (*iMyHLT).instance(), (*iMyHLT).process());
0140         // std::cout << "Found it " << (*iMyHLT).label() << "\t" <<(*iMyHLT).instance() << "\t" << (*iMyHLT).process() << std::endl;
0141       }
0142     }
0143   }
0144 
0145   // some sanity checks
0146   if (numOKHLTPaths == 0) {
0147     stringstream err;
0148     err << "Error: No unprescaled HLT paths in ";
0149     err << "run " << event.run() << ", event " << event.id().event() << ", lumi section ";
0150     err << event.getLuminosityBlock().luminosityBlock() << std::endl;
0151     err << "Menu name: " << hltConfig.tableName() << endl;
0152     std::cerr << err.str();
0153     edm::LogError("TriggerCandProducer") << err.str();
0154     throw cms::Exception("GarbageInGarbageOut") << err.str();
0155   }
0156   if (numOKHLTPaths > 1 && !isTriggerOR_) {
0157     stringstream err;
0158     err << "****** Error: There are multiple trigger paths. I cannot figure out how to perform trigger matching :("
0159         << "   Multiple HLT paths in ";
0160     err << "run " << event.run() << ", event " << event.id().event() << ", lumi section ";
0161     err << event.getLuminosityBlock().luminosityBlock() << std::endl;
0162     err << "Menu name: " << hltConfig.tableName() << endl;
0163     std::cerr << err.str();
0164     edm::LogError("TriggerCandProducer") << err.str();
0165     throw cms::Exception("GarbageInGarbageOut") << err.str();
0166   }
0167   //else std::cout << "theRightHLTTag_ = " << theRightHLTTag_ << std::endl;
0168 
0169   // Some sanity checks
0170   if (not trgEvent.isValid()) {
0171     edm::LogInfo("info") << "******** Following Trigger Summary Object Not Found: " << triggerEventTag_;
0172 
0173     event.put(std::move(outColRef));
0174     // event.put(std::move(outColPtr));
0175     return;
0176   }
0177 
0178   //---------------------------------------------------------------------------
0179 
0180   edm::InputTag filterTag;
0181   // loop over these objects to see whether they match
0182   const trigger::TriggerObjectCollection& TOC(trgEvent->getObjects());
0183 
0184   int index = 9999;
0185 
0186   if (isFilter_) {
0187     //-----------------------------------------------------------------------
0188     filterName_ = theRightHLTTag_.label();
0189     for (int i = 0; i != trgEvent->sizeFilters(); ++i) {
0190       std::string label(trgEvent->filterTag(i).label());
0191       if (label == theRightHLTTag_.label())
0192         index = i;
0193     }
0194     //-----------------------------------------------------------------------
0195     if (printIndex_)
0196       std::cout << "filter search: " << theRightHLTTag_.label() << "   index: " << index << std::endl;
0197   } else {
0198     std::vector<std::string> filters;
0199     try {
0200       filters = hltConfig.moduleLabels(theRightHLTTag_.label());
0201     } catch (std::exception const&) {
0202       cout << "bad trigger\n";
0203     }
0204     for (std::vector<std::string>::iterator filter = filters.begin(); filter != filters.end(); ++filter) {
0205       edm::InputTag testTag(*filter, "", theRightHLTTag_.process());
0206 
0207       int testindex = trgEvent->filterIndex(testTag);
0208       if (!(testindex >= trgEvent->sizeFilters())) {
0209         filterName_ = *filter;
0210         filterTag = testTag;
0211       }
0212     }
0213 
0214     index = trgEvent->filterIndex(filterTag);
0215   }
0216 
0217   if (printIndex_)
0218     std::cout << "theRightHLTTag_.label() " << theRightHLTTag_.label() << "   filter name " << filterName_ << "  index "
0219               << index << "  sizeFilters " << trgEvent->sizeFilters() << std::endl;
0220 
0221   // Loop over the candidate collection
0222   unsigned int counter = 0;
0223   bool foundMatch = false;
0224   bool foundPath = false;
0225 
0226   for (typename edm::View<object>::const_iterator j = candHandle->begin(); j != candHandle->end(); ++j, ++counter) {
0227     //     std::cout << counter << " " << j->eta() << " " << j->phi() << std::endl;
0228 
0229     foundMatch = false;
0230     //      for (std::vector<edm::InputTag>::const_iterator iMyHLT = hltTags_.begin();
0231     //        iMyHLT != hltTags_.end(); ++iMyHLT)
0232     //       {
0233     //         std::cout << "theRightHLTTag_.label() list " <<
0234     //           (*iMyHLT).label() << "   filter name " <<
0235     //           (*iMyHLT).instance() << std::endl;
0236     //       }
0237     for (std::vector<edm::InputTag>::const_iterator iMyHLT = hltTags_.begin(); iMyHLT != hltTags_.end(); ++iMyHLT) {
0238       if (foundMatch)
0239         break;
0240       if (isTriggerOR_ && isFilter_) {
0241         theRightHLTTag_ = edm::InputTag((*iMyHLT).label(), (*iMyHLT).instance(), (*iMyHLT).process());
0242         filterName_ = theRightHLTTag_.instance();
0243         //  std::cout << "theRightHLTTag_.label() test " <<
0244         //  theRightHLTTag_.label() << "   filter name " <<
0245         //  filterName_ << std::endl;
0246       }
0247 
0248       edm::InputTag l1testTag(filterName_, "", theRightHLTTag_.process());
0249       const int index = trgEvent->filterIndex(l1testTag);
0250 
0251       if (index >= trgEvent->sizeFilters()) {
0252         //   std::cout << "theRightHLTTag_.label() fail " <<
0253         //  theRightHLTTag_.label() << "   filter name " <<
0254         //  filterName_ << std::endl;
0255 
0256         if (isTriggerOR_)
0257           continue;
0258         //    edm::LogInfo("info")<< "******** Following TRIGGER Name Not in Dataset: " <<
0259         //     theRightHLTTag_.label();
0260         //     event.put(std::move(outColRef));
0261         //     return;
0262       } else {
0263         foundPath = true;
0264         // find how many objects there are
0265         const trigger::Keys& KEYS(trgEvent->filterKeys(index));
0266         const size_type nK(KEYS.size());
0267 
0268         //did this event fire the HLT?
0269         const edm::TriggerNames& trgNames = event.triggerNames(*pTrgResults);
0270         const unsigned int trgIndex = trgNames.triggerIndex(theRightHLTTag_.label());
0271         bool firedHLT = (trgIndex < trgNames.size()) && (pTrgResults->accept(trgIndex));
0272         bool founddRMatch = false;
0273         // Get cut decision for each candidate
0274         // Did this candidate cause a HLT trigger?
0275         for (int ipart = 0; ipart != nK; ++ipart) {
0276           const trigger::TriggerObject& TO = TOC[KEYS[ipart]];
0277           double dRval = deltaR(j->eta(), j->phi(), TO.eta(), TO.phi());
0278           //     std::cout << "theRightHLTTag_.label() test " <<
0279           //     theRightHLTTag_.label() << "   filter name " <<
0280           //     filterName_ << "  index " << index << " " << counter << " " << j->eta() << " " << TO.eta() << " " << j->phi() << " " << TO.phi() << std::endl;
0281           founddRMatch = dRval < delRMatchingCut_ && TO.et() > objEtMin_ && fabs(TO.eta()) < objEtaMax_;
0282           if (founddRMatch)
0283             break;
0284         }
0285         foundMatch = founddRMatch && (firedHLT || noHltFiring_);
0286         if (founddRMatch && (firedHLT || noHltFiring_) && !antiSelect_) {
0287           outColRef->push_back(candHandle->refAt(counter));
0288           //     std::cout << "theRightHLTTag_.label() select" <<
0289           //     theRightHLTTag_.label() << "   filter name " <<
0290           //     filterName_ << "  index " << index << " " << counter << " " << j->eta() << std::endl;
0291         }
0292       }
0293     }
0294 
0295     if (antiSelect_ && !foundMatch) {
0296       outColRef->push_back(candHandle->refAt(counter));
0297       //     std::cout << "theRightHLTTag_.label() antiselect" <<
0298       //     theRightHLTTag_.label() << "   filter name " <<
0299       //     filterName_ << "  index " << index << " " << counter << " " << j->eta() << std::endl;
0300     }
0301   }
0302 
0303   if (!foundPath) {
0304     edm::LogInfo("info") << "******** Following TRIGGER Name Not in Dataset: " << theRightHLTTag_.label();
0305     event.put(std::move(outColRef));
0306     return;
0307   }
0308 
0309   event.put(std::move(outColRef));
0310   // event.put(std::move(outColPtr));
0311 }
0312 
0313 // ---- method called once each job just before starting event loop  ---
0314 template <class object>
0315 void TriggerCandProducer<object>::beginRun(edm::Run const& iRun, edm::EventSetup const& iSetup) {
0316   //verify that the process names of all the requested HLT paths are identical
0317   skipEvent_ = false;
0318   bool identical = true;
0319   std::vector<edm::InputTag>::const_iterator iMyHLT = hltTags_.begin();
0320   edm::InputTag lastTag = *iMyHLT;
0321   while ((iMyHLT != hltTags_.end()) && identical) {
0322     if ((*iMyHLT).process() == lastTag.process())
0323       identical = true;
0324     else
0325       identical = false;
0326     lastTag = *iMyHLT;
0327     ++iMyHLT;
0328   }
0329   if (!identical)
0330     skipEvent_ = true;
0331 
0332   //std::cout << "calling init(" << "iRun" << ", " << "iSetup" << ", " << hltTags_[0].process() << ", " << "changed_" << ") in beginRun()" << std::endl;
0333   if (!hltPrescaleProvider_.init(iRun, iSetup, hltTags_[0].process(), changed_)) {
0334     edm::LogError("TriggerCandProducer") << "Error! Can't initialize HLTConfigProvider";
0335     throw cms::Exception("HLTConfigProvider::init() returned non 0");
0336   }
0337   if (printIndex_ && changed_)
0338     std::cout << "HLT configuration changed !" << std::endl;
0339 }