Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:09:10

0001 // system includes
0002 #include <cassert>
0003 #include <vector>
0004 #include <string>
0005 #include <map>
0006 
0007 // user includes
0008 #include "DataFormats/Common/interface/TriggerResults.h"
0009 #include "DataFormats/HLTReco/interface/TriggerEvent.h"
0010 #include "FWCore/Common/interface/TriggerNames.h"
0011 #include "FWCore/Common/interface/TriggerResultsByName.h"
0012 #include "FWCore/Framework/interface/Event.h"
0013 #include "FWCore/Framework/interface/stream/EDFilter.h"
0014 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0015 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0016 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0017 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0018 #include "HLTrigger/HLTcore/interface/HLTConfigProvider.h"
0019 
0020 // ROOT includes
0021 #include "TPRegexp.h"
0022 
0023 class HLTPathSelector : public edm::stream::EDFilter<> {
0024 public:
0025   explicit HLTPathSelector(const edm::ParameterSet&);
0026   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0027 
0028 private:
0029   void beginRun(edm::Run const&, edm::EventSetup const&) override;
0030   bool filter(edm::Event&, edm::EventSetup const&) override;
0031   void endJob();
0032 
0033 private:
0034   // module config parameters
0035   const bool verbose_;
0036   const std::string processName_;
0037   const std::vector<std::string> hltPathsOfInterest_;
0038   const edm::InputTag triggerResultsTag_;
0039   const edm::InputTag triggerEventTag_;
0040   const edm::EDGetTokenT<edm::TriggerResults> triggerResultsToken_;
0041   const edm::EDGetTokenT<trigger::TriggerEvent> triggerEventToken_;
0042 
0043   HLTConfigProvider hltConfig_;
0044 
0045   std::map<std::string, unsigned int> hltPathsMap_;
0046   std::map<std::string, int> tmap_;
0047 };
0048 
0049 using namespace std;
0050 using namespace edm;
0051 
0052 void HLTPathSelector::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0053   edm::ParameterSetDescription desc;
0054   desc.addUntracked<bool>("verbose", false);
0055   desc.add<std::string>("processName", std::string(""));
0056   desc.add<std::vector<std::string> >("hltPathsOfInterest", {});
0057   desc.addUntracked<edm::InputTag>("triggerResults", edm::InputTag("TriggerResults", "", "HLT"));
0058   desc.addUntracked<edm::InputTag>("triggerEvent", edm::InputTag("hltTriggerSummaryAOD", "", "HLT"));
0059   descriptions.addWithDefaultLabel(desc);
0060 }
0061 
0062 HLTPathSelector::HLTPathSelector(const edm::ParameterSet& ps)
0063     : verbose_(ps.getUntrackedParameter<bool>("verbose", false)),
0064       processName_(ps.getParameter<std::string>("processName")),
0065       hltPathsOfInterest_(ps.getParameter<std::vector<std::string> >("hltPathsOfInterest")),
0066       triggerResultsTag_(
0067           ps.getUntrackedParameter<edm::InputTag>("triggerResults", edm::InputTag("TriggerResults", "", "HLT"))),
0068       triggerEventTag_(
0069           ps.getUntrackedParameter<edm::InputTag>("triggerEvent", edm::InputTag("hltTriggerSummaryAOD", "", "HLT"))),
0070       triggerResultsToken_(consumes<edm::TriggerResults>(triggerResultsTag_)),
0071       triggerEventToken_(consumes<trigger::TriggerEvent>(triggerEventTag_)) {}
0072 
0073 void HLTPathSelector::beginRun(edm::Run const& iRun, edm::EventSetup const& iSetup) {
0074   bool changed(true);
0075   if (hltConfig_.init(iRun, iSetup, processName_, changed)) {
0076     if (changed) {
0077       edm::LogInfo("HLTPathSelector") << "HLT initialised";
0078       hltConfig_.dump("PrescaleTable");
0079     }
0080     hltPathsMap_.clear();
0081     const unsigned int n(hltConfig_.size());
0082     const std::vector<std::string>& pathList = hltConfig_.triggerNames();
0083     for (const auto& path : pathList) {
0084       if (!hltPathsOfInterest_.empty()) {
0085         int nmatch = 0;
0086         for (const auto& kt : hltPathsOfInterest_)
0087           nmatch += TPRegexp(kt).Match(path);
0088         if (!nmatch)
0089           continue;
0090       }
0091       const unsigned int triggerIndex(hltConfig_.triggerIndex(path));
0092       // abort on invalid trigger name
0093       if (triggerIndex >= n) {
0094         edm::LogError("HLTPathSelector") << "path: " << path << " - not found!";
0095         continue;
0096       }
0097       hltPathsMap_[path] = triggerIndex;
0098     }
0099   } else
0100     edm::LogError("HLTPathSelector") << " config extraction failure with process name " << processName_;
0101 }
0102 
0103 bool HLTPathSelector::filter(edm::Event& iEvent, edm::EventSetup const& iSetup) {
0104   // get event products
0105   edm::Handle<edm::TriggerResults> triggerResultsHandle_;
0106   iEvent.getByToken(triggerResultsToken_, triggerResultsHandle_);
0107   if (!triggerResultsHandle_.isValid()) {
0108     edm::LogError("HLTPathSelector") << "Error in getting TriggerResults product from Event!";
0109     return false;
0110   }
0111 
0112   edm::Handle<trigger::TriggerEvent> triggerEventHandle_;
0113   iEvent.getByToken(triggerEventToken_, triggerEventHandle_);
0114   if (!triggerEventHandle_.isValid()) {
0115     edm::LogError("HLTPathSelector") << "Error in getting TriggerEvent product from Event!";
0116     return false;
0117   }
0118   // sanity check
0119   assert(triggerResultsHandle_->size() == hltConfig_.size());
0120 
0121   int flag = 0;
0122   for (auto const& it : hltPathsMap_) {
0123     const std::string path(it.first);
0124     const unsigned int triggerIndex(it.second);
0125     assert(triggerIndex == iEvent.triggerNames(*triggerResultsHandle_).triggerIndex(path));
0126 
0127     // Results from TriggerResults product
0128     if (verbose_)
0129       edm::LogInfo("HLTPathSelector") << " Trigger path <" << path << "> status:"
0130                                       << " WasRun=" << triggerResultsHandle_->wasrun(triggerIndex)
0131                                       << " Accept=" << triggerResultsHandle_->accept(triggerIndex)
0132                                       << " Error=" << triggerResultsHandle_->error(triggerIndex);
0133 
0134     if (triggerResultsHandle_->wasrun(triggerIndex) && triggerResultsHandle_->accept(triggerIndex)) {
0135       ++flag;
0136       if (tmap_.find(path) == tmap_.end())
0137         tmap_[path] = 1;
0138       else
0139         tmap_[path]++;
0140     }
0141   }
0142   if (flag > 0)
0143     return true;
0144   return false;
0145 }
0146 
0147 void HLTPathSelector::endJob() {
0148   edm::LogInfo("HLTPathSelector") << setw(32) << "HLT Path" << setw(9) << "ACCEPT";
0149   for (auto const& jt : tmap_)
0150     edm::LogInfo("HLTPathSelector") << setw(9) << jt.second;
0151 }
0152 // Define this as a plug-in
0153 #include "FWCore/Framework/interface/MakerMacros.h"
0154 DEFINE_FWK_MODULE(HLTPathSelector);