CMS 3D CMS Logo

TriggerSelector.cc
Go to the documentation of this file.
1 #include "TriggerSelector.h"
2 
6 
7 #include <iostream>
8 #include <boost/regex.hpp>
9 #include <algorithm>
10 #include <cassert>
11 
12 namespace dqmservices {
13 
14 // compatibility constructor
15 
17  : useOld_(true) {
18  acceptAll_ = false;
19  eventSelector_.reset(new edm::EventSelector(pathspecs, names));
20 }
21 
23  Strings const& triggernames, bool old_)
24  : useOld_(old_) {
25  acceptAll_ = false;
26  if (old_) {
27  // old mode forced
28  acceptAll_ = false;
29  eventSelector_.reset(new edm::EventSelector(config, triggernames));
30  return;
31  }
32  if (!config.empty()) {
33  // let's see if non empty TriggerSelector is present
34  try {
35  std::string myPath =
36  trim(config.getParameter<std::string>("TriggerSelector"));
37  if (!myPath.empty()) {
38  init(myPath, triggernames);
39  return;
40  }
41  } catch (...) {
42  }
43 
44  // now try with the SelectEvents
45  try {
46  Strings paths;
47  paths = config.getParameter<Strings>("SelectEvents");
48  if (!paths.empty()) {
49  useOld_ = true;
50  eventSelector_.reset(new edm::EventSelector(config, triggernames));
51  return;
52  }
53  } catch (...) {
54  }
55  }
56  // if selection parameters aren't present, don't do selection
57  // log
58  acceptAll_ = true;
59 }
60 
62  Strings const& triggernames)
63  : useOld_(false) {
64  init(trim(expression), triggernames);
65 }
66 
67 void TriggerSelector::init(std::string const& expression,
68  Strings const& triggernames) {
69  // debug_ = true;
70  if (expression.empty()) {
71  acceptAll_ = true;
72  return;
73  }
74  if (expression.size() == 1 && expression.at(0) == '*')
75  acceptAll_ = true;
76  else
77  acceptAll_ = false;
78 
79  // replace all possible alternate operators (.AND. and .OR.)
80  {
81  using namespace boost;
83  temp = regex_replace(expression, regex(".AND."), "&&");
84  expression_ = regex_replace(temp, regex(".and."), "&&");
85  temp = regex_replace(expression_, regex(".OR."), "||");
86  expression_ = regex_replace(temp, regex(".or."), "||");
87  }
88 
89  // build decision tree
90  masterElement_.reset(new TreeElement(expression_, triggernames));
91 }
92 
93 /*
94  Obsolete
95 */
97  edm::ParameterSet const& pset) {
99 }
100 
102  if (useOld_) {
103  return eventSelector_->acceptEvent(tr);
104  }
105 
106  if (acceptAll_) return true;
107 
108  return masterElement_->returnStatus(tr);
109 }
110 
111 bool TriggerSelector::acceptEvent(unsigned char const* array_of_trigger_results,
112  int number_of_trigger_paths) const {
113  if (useOld_)
114  return eventSelector_->acceptEvent(array_of_trigger_results,
115  number_of_trigger_paths);
116 
117  if (acceptAll_) return true;
118 
119  // Form HLTGlobalStatus object to represent the array_of_trigger_results
120  edm::HLTGlobalStatus tr(number_of_trigger_paths);
121  int byteIndex = 0;
122  int subIndex = 0;
123  for (int pathIndex = 0; pathIndex < number_of_trigger_paths; ++pathIndex) {
124  int state = array_of_trigger_results[byteIndex] >> (subIndex * 2);
125  state &= 0x3;
126  edm::HLTPathStatus pathStatus(static_cast<edm::hlt::HLTState>(state));
127  tr[pathIndex] = pathStatus;
128  ++subIndex;
129  if (subIndex == 4) {
130  ++byteIndex;
131  subIndex = 0;
132  }
133  }
134  // Now make the decision, based on the HLTGlobalStatus tr,
135  // which we have created from the supplied array of results
136  masterElement_->returnStatus(tr);
137  return masterElement_->returnStatus(tr);
138 }
139 
141  Strings const& tr,
142  TreeElement* parentElement)
143  : op_(NonInit), trigBit_(-1) {
144  std::string str_ = trim(inputString);
145  children_.clear();
146  parent_ = parentElement;
147 
148  size_t offset_ = 0;
149  bool occurrences_ = false;
150 
151  if (str_.empty())
153  << "Syntax Error (empty element)" << std::endl;
154 
155  static const size_t bopsSize_ = 2;
156  static const std::string binaryOperators_[bopsSize_] = {"||", "&&"};
157 
158  for (size_t opr = 0; opr < bopsSize_; opr++) {
159  bool exitloop_ = false;
160  while (!exitloop_) {
161  size_t t_end_;
162 
163  std::string tmpStr = str_.substr(offset_);
164  t_end_ = tmpStr.find(binaryOperators_[opr]);
165  if (debug_)
166  std::cout << "offset: " << offset_ << " length: " << t_end_
167  << " string: " << tmpStr << std::endl;
168 
169  if (t_end_ == std::string::npos) {
170  // right side element
171  if (occurrences_)
172  children_.push_back(new TreeElement(tmpStr, tr, this));
173  break;
174  }
175  t_end_ += offset_;
176  if (t_end_ == 0 || t_end_ + 2 >= str_.size())
178  << "Syntax Error (operator is not unary)\n";
179  else {
180  // count bracket in preceeding part
181  size_t brackets_ = 0;
182  for (size_t k = offset_; k < t_end_; k++) {
183  if (str_.at(k) == '(') {
184  brackets_++;
185  } else if (str_.at(k) == ')') {
186  if (brackets_ == 0) {
188  << "Syntax Error (brackets)\n";
189  } else {
190  brackets_--;
191  }
192  }
193  }
194  if (brackets_ == 0) {
195  std::string next = str_.substr(offset_, t_end_ - offset_);
196  children_.push_back(new TreeElement(next, tr, this));
197  occurrences_ = true;
198  offset_ = t_end_ + 2;
199  } else {
200  // operator is inside brackets, find another
201  int bracketcnt_ = 0;
202  for (size_t k = offset_; true; k++) {
203  if (k >= str_.size()) {
204  if (bracketcnt_ != 0)
206  << "Syntax Error (brackets)\n";
207  exitloop_ = true;
208  if (occurrences_) {
209  children_.push_back(
210  new TreeElement(str_.substr(offset_), tr, this));
211  }
212  break;
213  }
214  // look for another operator
215  if (k >= t_end_ + 2 && bracketcnt_ == 0) {
216  std::string temp = str_.substr(k);
217  size_t pos = temp.find(binaryOperators_[opr]);
218  if (pos == std::string::npos) {
219  exitloop_ = true;
220  if (occurrences_) {
221  children_.push_back(
222  new TreeElement(str_.substr(offset_), tr, this));
223  }
224  break;
225  } else {
226  int brcount_ = 0;
227  for (size_t s = 0; s < pos; s++) {
228  // counting check of brackets from last position to operator
229  if (temp.at(pos) == '(') {
230  brcount_++;
231  } else if (temp.at(pos) == ')') {
232  if (brcount_ == 0) {
234  << "Syntax error (brackets)\n";
235  } else {
236  brcount_--;
237  }
238  }
239  }
240  if (brcount_ != 0)
242  << "Syntax error (brackets)\n";
243 
244  children_.push_back(
245  new TreeElement(str_.substr(offset_, pos + k), tr, this));
246  offset_ = k + pos + 2;
247  occurrences_ = true;
248  if (offset_ >= str_.size())
250  << "Syntax Error (operator is not unary)\n";
251  break;
252  }
253  }
254 
255  if (str_.at(k) == '(') bracketcnt_++;
256  if (str_.at(k) == ')') bracketcnt_--;
257  }
258  }
259  }
260  }
261  if (occurrences_) {
262  if (opr == 0)
263  op_ = OR;
264  else
265  op_ = AND;
266  return;
267  }
268  }
269 
270  if (str_.empty()) {
271  op_ = AND;
272  if (debug_)
273  std::cout << "warning: empty element (will return true)" << std::endl;
274  return;
275  }
276 
277  if (str_.at(0) == '!') {
278  op_ = NOT;
279  std::string next = str_.substr(1);
280  children_.push_back(new TreeElement(next, tr, this));
281  return;
282  }
283  size_t beginBlock_ = str_.find('(');
284  size_t endBlock_ = str_.rfind(')');
285  bool found_lbracket = (beginBlock_ != std::string::npos);
286  bool found_rbracket = (endBlock_ != std::string::npos);
287 
288  if (found_lbracket != found_rbracket) {
290  << "Syntax Error (brackets)\n";
291  } else if (found_lbracket && found_rbracket) {
292  if (beginBlock_ >= endBlock_) {
294  << "Syntax Error (brackets)\n";
295  }
296  if (beginBlock_ != 0 || endBlock_ != str_.size() - 1)
298  << "Syntax Error (invalid character)\n";
299 
300  std::string next =
301  str_.substr(beginBlock_ + 1, endBlock_ - beginBlock_ - 1);
302 
303  children_.push_back(new TreeElement(next, tr, this));
304  op_ = BR; // a bracket
305  return;
306  } else if (!found_lbracket &&
307  !found_rbracket) // assume single trigger or wildcard (parsing)
308  {
309  bool ignore_if_missing = true;
310  size_t chr_pos = str_.find("@");
311  if (chr_pos != std::string::npos) {
312  ignore_if_missing = false;
313  str_ = str_.substr(0, chr_pos);
314  }
315 
316  std::vector<Strings::const_iterator> matches = edm::regexMatch(tr, str_);
317  if (matches.empty()) {
318  if (!ignore_if_missing) // && !edm::is_glob(str_))
320  << "Trigger name (or match) not present" << std::endl;
321  else {
322  if (debug_)
323  std::cout << "TriggerSelector: Couldn't match any triggers from: "
324  << str_ << std::endl
325  << " Node will not be added " << std::endl;
326  op_ = OR;
327  return;
328  }
329  }
330  if (matches.size() == 1) {
331  // Single Trigger match
332  trigBit_ = distance(tr.begin(), matches[0]);
333  if (debug_) std::cout << "added trigger path: " << trigBit_ << std::endl;
334  return;
335  }
336  if (matches.size() > 1) {
337  op_ = OR;
338  for (size_t l = 0; l < matches.size(); l++)
339  children_.push_back(new TreeElement(*(matches[l]), tr, this));
340  }
341  }
342 }
343 
345  if (!input.empty()) {
346  std::string::size_type pos = input.find_first_not_of(" ");
347  if (pos != std::string::npos) input.erase(0, pos);
348 
349  pos = input.find_last_not_of(" ");
350  if (pos != std::string::npos) input.erase(pos + 1);
351  }
352  return input;
353 }
354 
357  if (!input.empty()) {
358  for (size_t pos = 0; pos < input.size(); pos++) {
359  char ch = input.at(pos);
360  if (ch == '&')
361  output.append("&amp;");
362  else
363  output.append(1, ch);
364  }
365  }
366  return output;
367 }
368 
370  edm::HLTGlobalStatus const& trStatus) const {
371  if (children_.empty()) {
372  if (op_ == OR || op_ == NOT) return false;
373  if (op_ == AND || op_ == BR) return true;
374 
375  if (trigBit_ < 0 || (unsigned int)trigBit_ >= trStatus.size())
377  << "Internal Error: array out of bounds " << std::endl;
378 
379  if ((trStatus[trigBit_]).state() == edm::hlt::Pass) return true;
380  // else if ((trStatus[trigBit]).state() == edm::hlt::Fail) return false;
381 
382  return false;
383  }
384  if (op_ == NOT) { // NEGATION
385  return !children_[0]->returnStatus(trStatus);
386  }
387  if (op_ == BR) { // BRACKET
388  return children_[0]->returnStatus(trStatus);
389  }
390  if (op_ == AND) { // AND
391  bool status = true;
392  for (size_t i = 0; i < children_.size(); i++)
393  status = status && children_[i]->returnStatus(trStatus);
394  return status;
395  } else if (op_ == OR) { // OR
396  bool status = false;
397  for (size_t i = 0; i < children_.size(); i++)
398  status = status || children_[i]->returnStatus(trStatus);
399  return status;
400  }
402  << "Internal error: reached end of returnStatus(...) op:state= " << op_
403  << std::endl;
404  return false;
405 }
406 
408  for (std::vector<TreeElement*>::iterator it = children_.begin();
409  it != children_.end(); it++)
410  delete *it;
411  children_.clear();
412 }
413 }
T getParameter(std::string const &) const
bool empty() const
Definition: ParameterSet.h:218
Definition: CLHEP.h:16
static const HistoName names[]
static std::string trim(std::string input)
Definition: config.py:1
boost::shared_ptr< TreeElement > masterElement_
uint16_t size_type
static std::string const input
Definition: EdmProvDump.cc:44
static std::string makeXMLString(std::string const &input)
accept
Definition: HLTenums.h:19
unsigned int size() const
Get number of paths stored.
static std::vector< std::string > getEventSelectionVString(edm::ParameterSet const &pset)
TriggerSelector(Strings const &pathspecs, Strings const &names)
int k[5][pyjets_maxn]
boost::shared_ptr< edm::EventSelector > eventSelector_
void init(std::string const &path, Strings const &triggernames)
std::vector< std::vector< std::string >::const_iterator > regexMatch(std::vector< std::string > const &strings, std::regex const &regexp)
Definition: RegexMatch.cc:30
static std::vector< std::string > getEventSelectionVString(edm::ParameterSet const &pset)
bool acceptEvent(edm::TriggerResults const &) const
std::vector< TreeElement * > children_
TreeElement(std::string const &inputString, Strings const &tr, TreeElement *parentElement=NULL)
std::vector< std::string > Strings
bool returnStatus(edm::HLTGlobalStatus const &trStatus) const