CMS 3D CMS Logo

HLTHighLevel.cc
Go to the documentation of this file.
1 
10 #include <vector>
11 #include <string>
12 #include <iostream>
13 #include <iomanip>
14 
25 
26 // needed for trigger bits from EventSetup as in ALCARECO paths
29 
31 #include "HLTHighLevel.h"
32 
33 //
34 // constructors and destructor
35 //
37  : inputTag_(iConfig.getParameter<edm::InputTag>("TriggerResultsTag")),
38  inputToken_(consumes<edm::TriggerResults>(inputTag_)),
39  triggerNamesID_(),
40  andOr_(iConfig.getParameter<bool>("andOr")),
41  throw_(iConfig.getParameter<bool>("throw")),
42  eventSetupPathsKey_(iConfig.getParameter<std::string>("eventSetupPathsKey")),
43  HLTPatterns_(iConfig.getParameter<std::vector<std::string> >("HLTPaths")),
44  HLTPathsByName_(),
45  HLTPathsByIndex_() {
46  // names and slot numbers are computed during the event loop,
47  // as they need to access the TriggerNames object via the TriggerResults
48 
49  if (!eventSetupPathsKey_.empty()) {
50  // If paths come from eventsetup, we must watch for IOV changes.
51  if (!HLTPatterns_.empty()) {
52  // We do not want double trigger path setting, so throw!
53  throw cms::Exception("Configuration")
54  << " HLTHighLevel instance: " << iConfig.getParameter<std::string>("@module_label") << "\n configured with "
55  << HLTPatterns_.size() << " HLTPaths and\n"
56  << " eventSetupPathsKey " << eventSetupPathsKey_ << ", choose either of them.";
57  }
58  alcaRecotriggerBitsToken_ = esConsumes<AlCaRecoTriggerBits, AlCaRecoTriggerBitsRcd>();
60  }
61 }
62 
63 //
64 // member functions
65 //
66 
69  desc.add<edm::InputTag>("TriggerResultsTag", edm::InputTag("TriggerResults", "", "HLT"));
70  std::vector<std::string> hltPaths(0);
71  // # provide list of HLT paths (or patterns) you want
72  desc.add<std::vector<std::string> >("HLTPaths", hltPaths);
73  // # not empty => use read paths from AlCaRecoTriggerBitsRcd via this key
74  desc.add<std::string>("eventSetupPathsKey", "");
75  // # how to deal with multiple triggers: True (OR) accept if ANY is true, False (AND) accept if ALL are true
76  desc.add<bool>("andOr", true);
77  // # throw exception on unknown path names
78  desc.add<bool>("throw", true);
79  descriptions.add("hltHighLevel", desc);
80 }
81 
82 // Initialize the internal trigger path representation (names and indices) from the
83 // patterns specified in the configuration.
84 // This needs to be called once at startup, whenever the trigger table has changed
85 // or in case of paths from eventsetup and IOV changed
87  const edm::Event& event,
88  const edm::EventSetup& iSetup,
90  unsigned int n;
91 
92  // clean up old data
93  HLTPathsByName_.clear();
94  HLTPathsByIndex_.clear();
95 
96  // Overwrite paths from EventSetup via AlCaRecoTriggerBitsRcd if configured:
97  if (!eventSetupPathsKey_.empty()) {
98  HLTPatterns_ = this->pathsFromSetup(eventSetupPathsKey_, event, iSetup);
99  }
100 
101  if (HLTPatterns_.empty()) {
102  // for empty input vector, default to all HLT trigger paths
103  n = result.size();
104  HLTPathsByName_.resize(n);
105  HLTPathsByIndex_.resize(n);
106  for (unsigned int i = 0; i < n; ++i) {
107  HLTPathsByName_[i] = triggerNames.triggerName(i);
108  HLTPathsByIndex_[i] = i;
109  }
110  } else {
111  // otherwise, expand wildcards in trigger names...
112  for (auto const& pattern : HLTPatterns_) {
113  if (edm::is_glob(pattern)) {
114  // found a glob pattern, expand it
115  std::vector<std::vector<std::string>::const_iterator> matches =
116  edm::regexMatch(triggerNames.triggerNames(), pattern);
117  if (matches.empty()) {
118  // pattern does not match any trigger paths
119  if (throw_)
120  throw cms::Exception("Configuration")
121  << "requested pattern \"" << pattern << "\" does not match any HLT paths";
122  else
123  edm::LogInfo("Configuration") << "requested pattern \"" << pattern << "\" does not match any HLT paths";
124  } else {
125  // store the matching patterns
126  for (auto const& match : matches)
127  HLTPathsByName_.push_back(*match);
128  }
129  } else {
130  // found a trigger name, just copy it
131  HLTPathsByName_.push_back(pattern);
132  }
133  }
134  n = HLTPathsByName_.size();
135 
136  // ...and get hold of trigger indices
137  bool valid = false;
138  HLTPathsByIndex_.resize(n);
139  for (unsigned int i = 0; i < HLTPathsByName_.size(); i++) {
140  HLTPathsByIndex_[i] = triggerNames.triggerIndex(HLTPathsByName_[i]);
141  if (HLTPathsByIndex_[i] < result.size()) {
142  valid = true;
143  } else {
144  // trigger path not found
145  HLTPathsByIndex_[i] = (unsigned int)-1;
146  if (throw_)
147  throw cms::Exception("Configuration") << "requested HLT path \"" << HLTPathsByName_[i] << "\" does not exist";
148  else
149  edm::LogInfo("Configuration") << "requested HLT path \"" << HLTPathsByName_[i] << "\" does not exist";
150  }
151  }
152 
153  if (not valid) {
154  // no point in throwing - if requested, it should have already happened
155  edm::LogWarning("Configuration")
156  << "none of the requested paths and pattern match any HLT path - no events will be selected";
157  }
158  }
159 
160  // report on what is finally used
161  LogDebug("HLTHighLevel") << "HLT trigger paths: " + inputTag_.encode() << " - Number of paths: " << n
162  << " - andOr mode: " << andOr_ << " - throw mode: " << throw_;
163 
164  LogTrace("HLTHighLevel") << "The HLT trigger paths (# index name):";
165  for (unsigned int i = 0; i < n; ++i)
166  if (HLTPathsByIndex_[i] == (unsigned int)-1)
167  LogTrace("HLTHighLevel") << " n/a " << HLTPathsByName_[i];
168  else
169  LogTrace("HLTHighLevel") << " " << std::setw(4) << HLTPathsByIndex_[i] << " " << HLTPathsByName_[i];
170 }
171 
172 // ------------ getting paths from EventSetup ------------
173 std::vector<std::string> HLTHighLevel::pathsFromSetup(const std::string& key,
174  const edm::Event& event,
175  const edm::EventSetup& iSetup) const {
176  // Get map of strings to concatenated list of names of HLT paths from EventSetup:
177  const auto& triggerBits = iSetup.getData(alcaRecotriggerBitsToken_);
178  typedef std::map<std::string, std::string> TriggerMap;
179  const TriggerMap& triggerMap = triggerBits.m_alcarecoToTrig;
180 
181  auto listIter = triggerMap.find(key);
182  if (listIter == triggerMap.end()) {
183  throw cms::Exception("Configuration")
184  << " HLTHighLevel [instance: " << moduleLabel() << " - path: " << pathName(event)
185  << "]: No triggerList with key " << key << " in AlCaRecoTriggerBitsRcd";
186  }
187 
188  // We must avoid a map<string,vector<string> > in DB for performance reason,
189  // so the paths are mapped into one string that we have to decompose:
190  return triggerBits.decompose(listIter->second);
191 }
192 
193 // ------------ method called to produce the data ------------
195  using namespace std;
196  using namespace edm;
197 
198  // get hold of TriggerResults Object
200  iEvent.getByToken(inputToken_, trh);
201  if (trh.isValid()) {
202  LogDebug("HLTHighLevel") << "TriggerResults found, number of HLT paths: " << trh->size();
203  } else {
204  LogError("HLTHighLevel") << "TriggerResults product " << inputTag_.encode()
205  << " not found - returning result=false!";
206  return false;
207  }
208 
209  // init the TriggerNames with the TriggerResults
210  const edm::TriggerNames& triggerNames = iEvent.triggerNames(*trh);
211  bool config_changed = false;
212  if (triggerNamesID_ != triggerNames.parameterSetID()) {
213  triggerNamesID_ = triggerNames.parameterSetID();
214  config_changed = true;
215  }
216 
217  // (re)run the initialization stuff if
218  // - this is the first event
219  // - or the HLT table has changed
220  // - or selected trigger bits come from AlCaRecoTriggerBitsRcd and these changed
221  if (config_changed or (watchAlCaRecoTriggerBitsRcd_ and watchAlCaRecoTriggerBitsRcd_->check(iSetup))) {
222  this->init(*trh, iEvent, iSetup, triggerNames);
223  }
224  unsigned int n = HLTPathsByName_.size();
225  unsigned int nbad = 0;
226  unsigned int fired = 0;
227 
228  // count invalid and fired triggers
229  for (unsigned int i = 0; i < n; i++)
230  if (HLTPathsByIndex_[i] == (unsigned int)-1)
231  ++nbad;
232  else if (trh->accept(HLTPathsByIndex_[i]))
233  ++fired;
234 
235  if ((nbad > 0) and (config_changed or throw_)) {
236  // only generate the error message if it's actually going to be used
237  std::string message;
238 
239  for (unsigned int i = 0; i < n; i++)
240  if (HLTPathsByIndex_[i] == (unsigned int)-1)
241  message += HLTPathsByName_[i] + " ";
242 
243  if (config_changed) {
244  LogTrace("HLTHighLevel") << " HLTHighLevel [instance: " << moduleLabel() << " - path: " << pathName(iEvent)
245  << "] configured with " << nbad << "/" << n << " unknown HLT path names: " << message;
246  }
247 
248  if (throw_) {
249  throw cms::Exception("Configuration")
250  << " HLTHighLevel [instance: " << moduleLabel() << " - path: " << pathName(iEvent) << "] configured with "
251  << nbad << "/" << n << " unknown HLT path names: " << message;
252  }
253  }
254 
255  // Boolean filter result (always at least one trigger)
256  const bool accept((fired > 0) and (andOr_ or (fired == n - nbad)));
257  LogDebug("HLTHighLevel") << "Accept = " << std::boolalpha << accept;
258 
259  return accept;
260 }
261 
263  return event.moduleCallingContext()->placeInPathContext()->pathContext()->pathName();
264 }
265 
bool accept() const
Has at least one path accepted the event?
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
std::string encode() const
Definition: InputTag.cc:159
void init(const edm::TriggerResults &results, const edm::Event &, const edm::EventSetup &iSetup, const edm::TriggerNames &triggerNames)
initialize the trigger conditions (call this if the trigger paths have changed)
Definition: HLTHighLevel.cc:86
bool is_glob(std::string const &pattern)
Definition: RegexMatch.cc:17
bool andOr_
false = and-mode (all requested triggers), true = or-mode (at least one)
Definition: HLTHighLevel.h:69
Log< level::Error, false > LogError
bool accept(const edm::Event &event, const edm::TriggerResults &triggerTable, const std::string &triggerPath)
Definition: TopDQMHelpers.h:31
#define LogTrace(id)
std::string const & moduleLabel() const
unsigned int size() const
Get number of paths stored.
int iEvent
Definition: GenABIO.cc:224
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e< void, edm::EventID const &, edm::Timestamp const & > We also list in braces which AR_WATCH_USING_METHOD_ is used for those or
Definition: Activities.doc:12
std::vector< unsigned int > HLTPathsByIndex_
list of required HLT triggers by HLT index
Definition: HLTHighLevel.h:92
edm::ParameterSetID triggerNamesID_
HLT trigger names.
Definition: HLTHighLevel.h:66
bool getData(T &iHolder) const
Definition: EventSetup.h:122
std::vector< std::string > pathsFromSetup(const std::string &key, const edm::Event &, const edm::EventSetup &iSetup) const
get HLTPaths with key &#39;key&#39; from EventSetup (AlCaRecoTriggerBitsRcd)
bool filter(edm::Event &, const edm::EventSetup &) override
Log< level::Info, false > LogInfo
std::optional< edm::ESWatcher< AlCaRecoTriggerBitsRcd > > watchAlCaRecoTriggerBitsRcd_
Watcher to be created and used if &#39;eventSetupPathsKey_&#39; non empty:
Definition: HLTHighLevel.h:81
edm::ESGetToken< AlCaRecoTriggerBits, AlCaRecoTriggerBitsRcd > alcaRecotriggerBitsToken_
ESGetToken to read AlCaRecoTriggerBits.
Definition: HLTHighLevel.h:83
edm::EDGetTokenT< edm::TriggerResults > inputToken_
Definition: HLTHighLevel.h:63
const std::string eventSetupPathsKey_
not empty => use read paths from AlCaRecoTriggerBitsRcd via this key
Definition: HLTHighLevel.h:79
void add(std::string const &label, ParameterSetDescription const &psetDescription)
bool isValid() const
Definition: HandleBase.h:70
std::vector< std::vector< std::string >::const_iterator > regexMatch(std::vector< std::string > const &strings, std::regex const &regexp)
Definition: RegexMatch.cc:26
bool throw_
throw on any requested trigger being unknown
Definition: HLTHighLevel.h:72
HLTHighLevel(const edm::ParameterSet &)
Definition: HLTHighLevel.cc:36
HLT enums.
std::vector< std::string > HLTPathsByName_
list of required HLT triggers by HLT name
Definition: HLTHighLevel.h:89
std::pair< typename Association::data_type::first_type, double > match(Reference key, Association association, bool bestMatchByMaxValue)
Generic matching function.
Definition: Utils.h:10
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Definition: HLTHighLevel.cc:67
Log< level::Warning, false > LogWarning
std::vector< std::string > HLTPatterns_
input patterns that will be expanded into trigger names
Definition: HLTHighLevel.h:86
std::string const & moduleLabel() const
Definition: event.py:1
edm::InputTag inputTag_
HLT TriggerResults EDProduct.
Definition: HLTHighLevel.h:62
#define LogDebug(id)
std::string const & pathName(const edm::Event &) const
stolen from HLTFilter
ModuleDescription const & moduleDescription() const
Definition: EDFilterBase.h:61