CMS 3D CMS Logo

HLTPathSelector.cc
Go to the documentation of this file.
1 // system includes
2 #include <cassert>
3 #include <vector>
4 #include <string>
5 #include <map>
6 
7 // user includes
19 
20 // ROOT includes
21 #include "TPRegexp.h"
22 
24 public:
25  explicit HLTPathSelector(const edm::ParameterSet&);
26  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
27 
28 private:
29  void beginRun(edm::Run const&, edm::EventSetup const&) override;
30  bool filter(edm::Event&, edm::EventSetup const&) override;
31  void endJob();
32 
33 private:
34  // module config parameters
35  const bool verbose_;
37  const std::vector<std::string> hltPathsOfInterest_;
42 
44 
45  std::map<std::string, unsigned int> hltPathsMap_;
46  std::map<std::string, int> tmap_;
47 };
48 
49 using namespace std;
50 using namespace edm;
51 
54  desc.addUntracked<bool>("verbose", false);
55  desc.add<std::string>("processName", std::string(""));
56  desc.add<std::vector<std::string> >("hltPathsOfInterest", {});
57  desc.addUntracked<edm::InputTag>("triggerResults", edm::InputTag("TriggerResults", "", "HLT"));
58  desc.addUntracked<edm::InputTag>("triggerEvent", edm::InputTag("hltTriggerSummaryAOD", "", "HLT"));
59  descriptions.addWithDefaultLabel(desc);
60 }
61 
63  : verbose_(ps.getUntrackedParameter<bool>("verbose", false)),
64  processName_(ps.getParameter<std::string>("processName")),
65  hltPathsOfInterest_(ps.getParameter<std::vector<std::string> >("hltPathsOfInterest")),
66  triggerResultsTag_(
67  ps.getUntrackedParameter<edm::InputTag>("triggerResults", edm::InputTag("TriggerResults", "", "HLT"))),
68  triggerEventTag_(
69  ps.getUntrackedParameter<edm::InputTag>("triggerEvent", edm::InputTag("hltTriggerSummaryAOD", "", "HLT"))),
70  triggerResultsToken_(consumes<edm::TriggerResults>(triggerResultsTag_)),
71  triggerEventToken_(consumes<trigger::TriggerEvent>(triggerEventTag_)) {}
72 
73 void HLTPathSelector::beginRun(edm::Run const& iRun, edm::EventSetup const& iSetup) {
74  bool changed(true);
75  if (hltConfig_.init(iRun, iSetup, processName_, changed)) {
76  if (changed) {
77  edm::LogInfo("HLTPathSelector") << "HLT initialised";
78  hltConfig_.dump("PrescaleTable");
79  }
80  hltPathsMap_.clear();
81  const unsigned int n(hltConfig_.size());
82  const std::vector<std::string>& pathList = hltConfig_.triggerNames();
83  for (const auto& path : pathList) {
84  if (!hltPathsOfInterest_.empty()) {
85  int nmatch = 0;
86  for (const auto& kt : hltPathsOfInterest_)
87  nmatch += TPRegexp(kt).Match(path);
88  if (!nmatch)
89  continue;
90  }
91  const unsigned int triggerIndex(hltConfig_.triggerIndex(path));
92  // abort on invalid trigger name
93  if (triggerIndex >= n) {
94  edm::LogError("HLTPathSelector") << "path: " << path << " - not found!";
95  continue;
96  }
97  hltPathsMap_[path] = triggerIndex;
98  }
99  } else
100  edm::LogError("HLTPathSelector") << " config extraction failure with process name " << processName_;
101 }
102 
104  // get event products
105  edm::Handle<edm::TriggerResults> triggerResultsHandle_;
106  iEvent.getByToken(triggerResultsToken_, triggerResultsHandle_);
107  if (!triggerResultsHandle_.isValid()) {
108  edm::LogError("HLTPathSelector") << "Error in getting TriggerResults product from Event!";
109  return false;
110  }
111 
112  edm::Handle<trigger::TriggerEvent> triggerEventHandle_;
113  iEvent.getByToken(triggerEventToken_, triggerEventHandle_);
114  if (!triggerEventHandle_.isValid()) {
115  edm::LogError("HLTPathSelector") << "Error in getting TriggerEvent product from Event!";
116  return false;
117  }
118  // sanity check
119  assert(triggerResultsHandle_->size() == hltConfig_.size());
120 
121  int flag = 0;
122  for (auto const& it : hltPathsMap_) {
123  const std::string path(it.first);
124  const unsigned int triggerIndex(it.second);
125  assert(triggerIndex == iEvent.triggerNames(*triggerResultsHandle_).triggerIndex(path));
126 
127  // Results from TriggerResults product
128  if (verbose_)
129  edm::LogInfo("HLTPathSelector") << " Trigger path <" << path << "> status:"
130  << " WasRun=" << triggerResultsHandle_->wasrun(triggerIndex)
131  << " Accept=" << triggerResultsHandle_->accept(triggerIndex)
132  << " Error=" << triggerResultsHandle_->error(triggerIndex);
133 
134  if (triggerResultsHandle_->wasrun(triggerIndex) && triggerResultsHandle_->accept(triggerIndex)) {
135  ++flag;
136  if (tmap_.find(path) == tmap_.end())
137  tmap_[path] = 1;
138  else
139  tmap_[path]++;
140  }
141  }
142  if (flag > 0)
143  return true;
144  return false;
145 }
146 
148  edm::LogInfo("HLTPathSelector") << setw(32) << "HLT Path" << setw(9) << "ACCEPT";
149  for (auto const& jt : tmap_)
150  edm::LogInfo("HLTPathSelector") << setw(9) << jt.second;
151 }
152 // Define this as a plug-in
const edm::InputTag triggerEventTag_
bool accept() const
Has at least one path accepted the event?
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
HLTConfigProvider hltConfig_
bool error() const
Has any path encountered an error (exception)
std::map< std::string, unsigned int > hltPathsMap_
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
bool filter(edm::Event &, edm::EventSetup const &) override
const edm::EDGetTokenT< edm::TriggerResults > triggerResultsToken_
Log< level::Error, false > LogError
assert(be >=bs)
const edm::InputTag triggerResultsTag_
bool wasrun() const
Was at least one path run?
const std::vector< std::string > hltPathsOfInterest_
unsigned int size() const
Get number of paths stored.
int iEvent
Definition: GenABIO.cc:224
void dump(const std::string &what) const
Dumping config info to cout.
const std::string processName_
unsigned int size() const
number of trigger paths in trigger table
void beginRun(edm::Run const &, edm::EventSetup const &) override
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
unsigned int triggerIndex(const std::string &triggerName) const
slot position of trigger path in trigger table (0 to size-1)
Log< level::Info, false > LogInfo
std::map< std::string, int > tmap_
const edm::EDGetTokenT< trigger::TriggerEvent > triggerEventToken_
bool init(const edm::Run &iRun, const edm::EventSetup &iSetup, const std::string &processName, bool &changed)
d&#39;tor
const bool verbose_
const std::vector< std::string > & triggerNames() const
names of trigger paths
bool isValid() const
Definition: HandleBase.h:70
HLTPathSelector(const edm::ParameterSet &)
HLT enums.
Definition: Run.h:45