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