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 
26 
27 // needed for trigger bits from EventSetup as in ALCARECO paths
30 
31 
33 #include "HLTHighLevel.h"
34 
35 //
36 // constructors and destructor
37 //
39  inputTag_ (iConfig.getParameter<edm::InputTag> ("TriggerResultsTag")),
40  inputToken_ (consumes<edm::TriggerResults>(inputTag_)),
41  triggerNamesID_ (),
42  andOr_ (iConfig.getParameter<bool> ("andOr")),
43  throw_ (iConfig.getParameter<bool> ("throw")),
44  eventSetupPathsKey_(iConfig.getParameter<std::string>("eventSetupPathsKey")),
45  watchAlCaRecoTriggerBitsRcd_(nullptr),
46  HLTPatterns_ (iConfig.getParameter<std::vector<std::string> >("HLTPaths")),
47  HLTPathsByName_(),
48  HLTPathsByIndex_()
49 {
50  // names and slot numbers are computed during the event loop,
51  // as they need to access the TriggerNames object via the TriggerResults
52 
53  if (!eventSetupPathsKey_.empty()) {
54  // If paths come from eventsetup, we must watch for IOV changes.
55  if (!HLTPatterns_.empty()) {
56  // We do not want double trigger path setting, so throw!
57  throw cms::Exception("Configuration")
58  << " HLTHighLevel instance: "<< iConfig.getParameter<std::string>("@module_label")
59  << "\n configured with " << HLTPatterns_.size() << " HLTPaths and\n"
60  << " eventSetupPathsKey " << eventSetupPathsKey_ << ", choose either of them.";
61  }
63  }
64 }
65 
67 {
68  delete watchAlCaRecoTriggerBitsRcd_; // safe on null pointer...
69 }
70 
71 //
72 // member functions
73 //
74 
75 void
78  desc.add<edm::InputTag>("TriggerResultsTag",edm::InputTag("TriggerResults","","HLT"));
79  std::vector<std::string> hltPaths(0);
80  // # provide list of HLT paths (or patterns) you want
81  desc.add<std::vector<std::string> >("HLTPaths",hltPaths);
82  // # not empty => use read paths from AlCaRecoTriggerBitsRcd via this key
83  desc.add<std::string>("eventSetupPathsKey","");
84  // # how to deal with multiple triggers: True (OR) accept if ANY is true, False (AND) accept if ALL are true
85  desc.add<bool>("andOr",true);
86  // # throw exception on unknown path names
87  desc.add<bool>("throw",true);
88  descriptions.add("hltHighLevel", desc);
89 }
90 
91 // Initialize the internal trigger path representation (names and indices) from the
92 // patterns specified in the configuration.
93 // This needs to be called once at startup, whenever the trigger table has changed
94 // or in case of paths from eventsetup and IOV changed
96  const edm::Event &event,
97  const edm::EventSetup& iSetup,
98  const edm::TriggerNames & triggerNames )
99 {
100  unsigned int n;
101 
102  // clean up old data
103  HLTPathsByName_.clear();
104  HLTPathsByIndex_.clear();
105 
106  // Overwrite paths from EventSetup via AlCaRecoTriggerBitsRcd if configured:
107  if (!eventSetupPathsKey_.empty()) {
108  HLTPatterns_ = this->pathsFromSetup(eventSetupPathsKey_, event, iSetup);
109  }
110 
111  if (HLTPatterns_.empty()) {
112  // for empty input vector, default to all HLT trigger paths
113  n = result.size();
114  HLTPathsByName_.resize(n);
115  HLTPathsByIndex_.resize(n);
116  for (unsigned int i = 0; i < n; ++i) {
117  HLTPathsByName_[i] = triggerNames.triggerName(i);
118  HLTPathsByIndex_[i] = i;
119  }
120  } else {
121  // otherwise, expand wildcards in trigger names...
122  for(auto const& pattern : HLTPatterns_) {
123  if (edm::is_glob(pattern)) {
124  // found a glob pattern, expand it
125  std::vector< std::vector<std::string>::const_iterator > matches = edm::regexMatch(triggerNames.triggerNames(), pattern);
126  if (matches.empty()) {
127  // pattern does not match any trigger paths
128  if (throw_)
129  throw cms::Exception("Configuration") << "requested pattern \"" << pattern << "\" does not match any HLT paths";
130  else
131  edm::LogInfo("Configuration") << "requested pattern \"" << pattern << "\" does not match any HLT paths";
132  } else {
133  // store the matching patterns
134  for(auto const& match : matches)
135  HLTPathsByName_.push_back(*match);
136  }
137  } else {
138  // found a trigger name, just copy it
139  HLTPathsByName_.push_back(pattern);
140  }
141  }
142  n = HLTPathsByName_.size();
143 
144  // ...and get hold of trigger indices
145  bool valid = false;
146  HLTPathsByIndex_.resize(n);
147  for (unsigned int i = 0; i < HLTPathsByName_.size(); i++) {
148  HLTPathsByIndex_[i] = triggerNames.triggerIndex(HLTPathsByName_[i]);
149  if (HLTPathsByIndex_[i] < result.size()) {
150  valid = true;
151  } else {
152  // trigger path not found
153  HLTPathsByIndex_[i] = (unsigned int) -1;
154  if (throw_)
155  throw cms::Exception("Configuration") << "requested HLT path \"" << HLTPathsByName_[i] << "\" does not exist";
156  else
157  edm::LogInfo("Configuration") << "requested HLT path \"" << HLTPathsByName_[i] << "\" does not exist";
158  }
159  }
160 
161  if (not valid) {
162  // no point in throwing - if requested, it should have already happened
163  edm::LogWarning("Configuration") << "none of the requested paths and pattern match any HLT path - no events will be selected";
164  }
165 
166  }
167 
168  // report on what is finally used
169  LogDebug("HLTHighLevel") << "HLT trigger paths: " + inputTag_.encode()
170  << " - Number of paths: " << n
171  << " - andOr mode: " << andOr_
172  << " - throw mode: " << throw_;
173 
174  LogTrace("HLTHighLevel") << "The HLT trigger paths (# index name):";
175  for (unsigned int i = 0; i < n; ++i)
176  if (HLTPathsByIndex_[i] == (unsigned int) -1)
177  LogTrace("HLTHighLevel") << " n/a " << HLTPathsByName_[i];
178  else
179  LogTrace("HLTHighLevel") << " " << std::setw(4) << HLTPathsByIndex_[i] << " " << HLTPathsByName_[i];
180 
181 }
182 
183 // ------------ getting paths from EventSetup ------------
184 std::vector<std::string>
186 {
187  // Get map of strings to concatenated list of names of HLT paths from EventSetup:
189  iSetup.get<AlCaRecoTriggerBitsRcd>().get(triggerBits);
190  typedef std::map<std::string, std::string> TriggerMap;
191  const TriggerMap &triggerMap = triggerBits->m_alcarecoToTrig;
192 
193  auto listIter = triggerMap.find(key);
194  if (listIter == triggerMap.end()) {
195  throw cms::Exception("Configuration")
196  << " HLTHighLevel [instance: " << moduleLabel() << " - path: " << pathName(event)
197  << "]: No triggerList with key " << key << " in AlCaRecoTriggerBitsRcd";
198  }
199 
200  // We must avoid a map<string,vector<string> > in DB for performance reason,
201  // so the paths are mapped into one string that we have to decompose:
202  return triggerBits->decompose(listIter->second);
203 }
204 
205 // ------------ method called to produce the data ------------
206  bool
208 {
209  using namespace std;
210  using namespace edm;
211 
212  // get hold of TriggerResults Object
214  iEvent.getByToken(inputToken_, trh);
215  if (trh.isValid()) {
216  LogDebug("HLTHighLevel") << "TriggerResults found, number of HLT paths: " << trh->size();
217  } else {
218  LogError("HLTHighLevel") << "TriggerResults product " << inputTag_.encode() << " not found - returning result=false!";
219  return false;
220  }
221 
222  // init the TriggerNames with the TriggerResults
223  const edm::TriggerNames & triggerNames = iEvent.triggerNames(*trh);
224  bool config_changed = false;
225  if (triggerNamesID_ != triggerNames.parameterSetID()) {
226  triggerNamesID_ = triggerNames.parameterSetID();
227  config_changed = true;
228  }
229 
230  // (re)run the initialization stuff if
231  // - this is the first event
232  // - or the HLT table has changed
233  // - or selected trigger bits come from AlCaRecoTriggerBitsRcd and these changed
234  if (config_changed or (watchAlCaRecoTriggerBitsRcd_ and watchAlCaRecoTriggerBitsRcd_->check(iSetup))) {
235  this->init(*trh, iEvent, iSetup, triggerNames);
236  }
237  unsigned int n = HLTPathsByName_.size();
238  unsigned int nbad = 0;
239  unsigned int fired = 0;
240 
241  // count invalid and fired triggers
242  for (unsigned int i = 0; i < n; i++)
243  if (HLTPathsByIndex_[i] == (unsigned int) -1)
244  ++nbad;
245  else if (trh->accept(HLTPathsByIndex_[i]))
246  ++fired;
247 
248  if ((nbad > 0) and (config_changed or throw_)) {
249  // only generate the error message if it's actually going to be used
250  std::string message;
251 
252  for (unsigned int i = 0; i < n; i++)
253  if (HLTPathsByIndex_[i] == (unsigned int) -1)
254  message += HLTPathsByName_[i] + " ";
255 
256  if (config_changed) {
257  LogTrace("HLTHighLevel")
258  << " HLTHighLevel [instance: " << moduleLabel()
259  << " - path: " << pathName(iEvent)
260  << "] configured with " << nbad
261  << "/" << n
262  << " unknown HLT path names: " << message;
263  }
264 
265  if (throw_) {
266  throw cms::Exception("Configuration")
267  << " HLTHighLevel [instance: " << moduleLabel()
268  << " - path: " << pathName(iEvent)
269  << "] configured with " << nbad
270  << "/" << n
271  << " unknown HLT path names: " << message;
272  }
273  }
274 
275  // Boolean filter result (always at least one trigger)
276  const bool accept( (fired > 0) and ( andOr_ or (fired == n-nbad) ) );
277  LogDebug("HLTHighLevel") << "Accept = " << std::boolalpha << accept;
278 
279  return accept;
280 }
281 
282 
284  return event.moduleCallingContext()->placeInPathContext()->pathContext()->pathName();
285 }
286 
288  return moduleDescription().moduleLabel();
289 }
#define LogDebug(id)
T getParameter(std::string const &) const
std::map< std::string, std::string > m_alcarecoToTrig
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
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:95
bool accept() const
Has at least one path accepted the event?
#define nullptr
bool is_glob(std::string const &pattern)
Definition: RegexMatch.cc:17
std::vector< std::string > decompose(const std::string &concatPaths) const
Decompose one value of map from concatenated string.
bool andOr_
false = and-mode (all requested triggers), true = or-mode (at least one)
Definition: HLTHighLevel.h:68
bool accept(const edm::Event &event, const edm::TriggerResults &triggerTable, const std::string &triggerPath)
Definition: TopDQMHelpers.h:30
std::string const & moduleLabel() const
Strings const & triggerNames() const
Definition: TriggerNames.cc:20
std::string encode() const
Definition: InputTag.cc:159
std::string const & moduleLabel() const
ParameterSetID const & parameterSetID() const
Definition: TriggerNames.cc:33
int iEvent
Definition: GenABIO.cc:224
unsigned int triggerIndex(std::string const &name) const
Definition: TriggerNames.cc:24
unsigned int size() const
Get number of paths stored.
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:89
edm::ParameterSetID triggerNamesID_
HLT trigger names.
Definition: HLTHighLevel.h:65
~HLTHighLevel() override
Definition: HLTHighLevel.cc:66
ParameterDescriptionBase * add(U const &iLabel, T const &value)
bool isValid() const
Definition: HandleBase.h:74
#define LogTrace(id)
bool filter(edm::Event &, const edm::EventSetup &) override
edm::EDGetTokenT< edm::TriggerResults > inputToken_
Definition: HLTHighLevel.h:62
const std::string eventSetupPathsKey_
not empty => use read paths from AlCaRecoTriggerBitsRcd via this key
Definition: HLTHighLevel.h:78
std::string const & triggerName(unsigned int index) const
Definition: TriggerNames.cc:22
void add(std::string const &label, ParameterSetDescription const &psetDescription)
bool check(const edm::EventSetup &iSetup)
Definition: ESWatcher.h:52
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)
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:71
HLTHighLevel(const edm::ParameterSet &)
Definition: HLTHighLevel.cc:38
HLT enums.
ModuleDescription const & moduleDescription() const
Definition: EDFilter.h:56
T get() const
Definition: EventSetup.h:71
std::string const & pathName(const edm::Event &) const
stolen from HLTFilter
std::vector< std::string > HLTPathsByName_
list of required HLT triggers by HLT name
Definition: HLTHighLevel.h:86
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:76
std::vector< std::string > HLTPatterns_
input patterns that will be expanded into trigger names
Definition: HLTHighLevel.h:83
edm::TriggerNames const & triggerNames(edm::TriggerResults const &triggerResults) const override
Definition: Event.cc:256
Definition: event.py:1
edm::InputTag inputTag_
HLT TriggerResults EDProduct.
Definition: HLTHighLevel.h:61
edm::ESWatcher< AlCaRecoTriggerBitsRcd > * watchAlCaRecoTriggerBitsRcd_
Watcher to be created and used if &#39;eventSetupPathsKey_&#39; non empty:
Definition: HLTHighLevel.h:80