LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
EventIDMatcher.cc
Go to the documentation of this file.
1 // vim: set sw=2 :
3 
6 
7 #include <cstdio>
8 #include <cstdlib>
9 #include <iostream>
10 #include <regex>
11 #include <sstream>
12 #include <string>
13 #include <vector>
14 
15 using namespace std;
16 
17 namespace art {
18 
19  EventIDMatcher::EventIDMatcher(std::string const& pattern)
20  : pattern_(), parsed_patterns_()
21  {
22  if (pattern.empty()) {
24  << "EventIDMatcher given an empty pattern!";
25  }
26  pattern_.push_back(pattern);
27  parsed_patterns_.resize(1);
28  parse_pattern();
29  }
30 
31  EventIDMatcher::EventIDMatcher(std::vector<std::string> const& patterns)
33  {
34  if (patterns.size() == 0) {
36  << "EventIDMatcher given an empty list of patterns!";
37  }
38  pattern_.reserve(patterns.size());
39  parsed_patterns_.resize(patterns.size());
40  for (auto const& val : patterns) {
41  if (val.empty()) {
43  << "EventIDMatcher given an empty pattern!";
44  }
45  pattern_.push_back(val);
46  }
47  parse_pattern();
48  }
49 
50  void
52  {
53  regex pat( // matches ('*' /*wildcard*/ | digits /*single*/ | digits -
54  // digits /*range*/)(',' /*list*/ | ':' /*part*/ | eol)
55  "(" // 1
56  "[[:blank:]]*"
57  "(" // 2
58  "([*])|" // 3
59  "([0-9]+)|" // 4
60  "([0-9]+)" // 5
61  "[[:blank:]]*-[[:blank:]]*"
62  "([0-9]+)" // 6
63  ")"
64  "[[:blank:]]*"
65  "([,:]|$)" // 7
66  ")");
67  int patno = -1;
68  for (auto const& given_pattern : pattern_) {
69  ++patno;
70  regex_iterator<string::const_iterator> I(
71  given_pattern.cbegin(), given_pattern.cend(), pat);
72  regex_iterator<string::const_iterator> E;
73  auto prev_pos = 0L;
74  auto prev_len = 0L;
75  char prev_sep = '\0';
76  parsed_patterns_[patno].resize(3);
77  // Note: 0: run, 1: subrun, 2: event
78  auto part_num = 0U;
79  for (; I != E; ++I) {
80  auto const& m = *I;
81  char sep = '\0';
82  if (m.str(7).size()) {
83  sep = m.str(7)[0];
84  }
85  // printf("%2ld, %2ld: ", m.position(), m.position() + m.length() - 1L);
86  if (m.position() != (prev_pos + prev_len)) {
87  // err, non-matching characters between
88  ostringstream buf;
89  buf << '\n';
90  buf << "Illegal character in pattern near here:\n";
91  buf << given_pattern;
92  buf << '\n';
93  for (auto i = 0L; i < (prev_pos + prev_len); ++i) {
94  buf << ' ';
95  }
96  buf << "^\n";
97  throw art::Exception(errors::LogicError) << buf.str();
98  }
99  if (m[3].matched) {
100  // wildcard
101  // printf("%s", m.str(3).c_str());
102  parsed_patterns_[patno][part_num].emplace_back(0U, 0U, true);
103  } else if (m[4].matched) {
104  // single num
105  // printf("%s", m.str(4).c_str());
106  auto num = 0U;
107  for (auto val : m.str(4)) {
108  num = (num * 10U) + (val - '0');
109  }
110  parsed_patterns_[patno][part_num].emplace_back(num, num, false);
111  } else {
112  // range
113  // printf("%s-%s", m.str(5).c_str(), m.str(6).c_str());
114  auto num_low = 0U;
115  for (auto val : m.str(5)) {
116  num_low = (num_low * 10U) + (val - '0');
117  }
118  auto num_high = 0U;
119  for (auto val : m.str(6)) {
120  num_high = (num_high * 10U) + (val - '0');
121  }
122  parsed_patterns_[patno][part_num].emplace_back(
123  num_low, num_high, false);
124  }
125  // printf("%s\n", m.str(7).c_str());
126  if (sep == ':') {
127  if (part_num == 0U) {
128  // at end of run part
129  } else if (part_num == 1U) {
130  // at end of subrun part
131  } else if (part_num == 2U) {
132  // error, event part ended with a ':'
133  ostringstream buf;
134  buf << '\n';
135  buf
136  << "Syntax error, event part of pattern ended with a ':' here:\n";
137  buf << given_pattern;
138  buf << '\n';
139  for (auto i = 0L; i < m.position(7); ++i) {
140  buf << ' ';
141  }
142  buf << "^\n";
143  throw art::Exception(errors::LogicError) << buf.str();
144  }
145  ++part_num;
146  } else if (sep == ',') {
147  // range continues
148  } else {
149  // at end of event part, and end of string
150  }
151  prev_pos = m.position();
152  prev_len = m.length();
153  prev_sep = sep;
154  }
155  if (prev_sep != '\0') {
156  // err, last match did not finish properly
157  ostringstream buf;
158  buf << '\n';
159  printf("pep: Syntax error, near here:\n");
160  buf << given_pattern;
161  buf << '\n';
162  for (auto i = 0L; i < (prev_pos + prev_len); ++i) {
163  buf << ' ';
164  }
165  buf << "^\n";
166  throw art::Exception(errors::LogicError) << buf.str();
167  }
168  if (static_cast<string::size_type>(prev_pos + prev_len) !=
169  given_pattern.size()) {
170  // err, did not match whole string
171  ostringstream buf;
172  buf << '\n';
173  printf("pep: Syntax error, near here:\n");
174  buf << given_pattern;
175  buf << '\n';
176  for (auto i = 0L; i < (prev_pos + prev_len); ++i) {
177  buf << ' ';
178  }
179  buf << "^\n";
180  throw art::Exception(errors::LogicError) << buf.str();
181  }
182  // for (auto i = 0U; i < 3; ++i) {
183  // printf("-----\n");
184  // for (auto const& val : parsed_patterns_[patno][i]) {
185  // if (val.wildcard_) {
186  // printf("*\n");
187  // }
188  // else if (val.low_ == val.high_) {
189  // printf("%u\n", val.low_);
190  // }
191  // else {
192  // printf("%u-%u\n", val.low_, val.high_);
193  // }
194  // }
195  //}
196  }
197  }
198 
199  bool
201  {
202  return match(eid);
203  }
204 
205  bool
206  EventIDMatcher::match(EventID const& eid) const
207  {
208  bool ret = false;
209  //*/printf("\nMatching pattern: %s\n", pattern_.c_str());
210  if (!eid.isValid() || eid.isFlush()) {
211  //*/printf("Decision: %d\n\n", ret);
212  return ret;
213  }
214  if (!eid.subRunID().isValid() || eid.subRunID().isFlush()) {
215  //*/printf("Decision: %d\n\n", ret);
216  return ret;
217  }
218  if (!eid.runID().isValid() || eid.runID().isFlush()) {
219  //*/printf("Decision: %d\n\n", ret);
220  return ret;
221  }
222  for (auto const& parsed_pattern : parsed_patterns_) {
223  for (auto i = 0U; i < 3; ++i) {
224  //*/printf("----- ");
225  //*/if (i == 0U) {
226  //*/printf(" run part -- ");
227  //*/}
228  //*/else if (i == 1U) {
229  //*/printf("subrun part -- ");
230  //*/}
231  //*/else {
232  //*/printf(" event part -- ");
233  //*/}
234  //*/printf("run: %u subRun: %u event: %u\n", eid.run(), eid.subRun(),
235  // eid.event());
236  for (auto const& val : parsed_pattern[i]) {
237  if (val.wildcard_) {
238  // Wildcards always match
239  //*/printf("*: matched\n");
240  ret = true;
241  break;
242  } else if (val.low_ == val.high_) {
243  // Single value match
244  if (i == 0U) {
245  // run
246  if (eid.run() == val.low_) {
247  //*/printf("%u: matched\n", val.low_);
248  ret = true;
249  break;
250  } else {
251  //*/printf("%u: no match\n", val.low_);
252  }
253  } else if (i == 1U) {
254  // subrun
255  if (eid.subRun() == val.low_) {
256  //*/printf("%u: matched\n", val.low_);
257  ret = true;
258  break;
259  } else {
260  //*/printf("%u: no match\n", val.low_);
261  }
262  } else {
263  // event
264  if (eid.event() == val.low_) {
265  //*/printf("%u: matched\n", val.low_);
266  ret = true;
267  break;
268  } else {
269  //*/printf("%u: no match\n", val.low_);
270  }
271  }
272  } else {
273  // Range match
274  //*/printf("%u-%u\n", val.low_, val.high_);
275  if (i == 0U) {
276  // run
277  if ((eid.run() >= val.low_) && (eid.run() <= val.high_)) {
278  //*/printf("%u-%u: matched\n", val.low_, val.high_);
279  ret = true;
280  break;
281  } else {
282  //*/printf("%u-%u: no match\n", val.low_, val.high_);
283  }
284  } else if (i == 1U) {
285  // subrun
286  if ((eid.subRun() >= val.low_) && (eid.subRun() <= val.high_)) {
287  //*/printf("%u-%u: matched\n", val.low_, val.high_);
288  ret = true;
289  break;
290  } else {
291  //*/printf("%u-%u: no match\n", val.low_, val.high_);
292  }
293  } else {
294  // event
295  if ((eid.event() >= val.low_) && (eid.event() <= val.high_)) {
296  //*/printf("%u-%u: matched\n", val.low_, val.high_);
297  ret = true;
298  break;
299  } else {
300  //*/printf("%u-%u: no match\n", val.low_, val.high_);
301  }
302  }
303  }
304  }
305  if (!ret) {
306  // nothing matched
307  break;
308  }
309  if (i != 2U) {
310  // Reset for next part.
311  ret = false;
312  }
313  }
314  //*/printf("Decision: %d\n\n", ret);
315  if (ret) {
316  // We matched, return immediately.
317  return ret;
318  }
319  }
320  // We never matched.
321  return ret;
322  }
323 
324 } // namespace art
bool isValid() const
Definition: EventID.h:123
RunID const & runID() const
Definition: EventID.h:93
bool isFlush() const
Definition: RunID.h:75
SubRunID const & subRunID() const
Definition: EventID.h:105
Float_t E
Definition: plot.C:23
STL namespace.
bool isFlush() const
Definition: EventID.h:129
RunNumber_t run() const
Definition: EventID.h:99
bool isValid() const
Definition: SubRunID.h:96
bool match(EventID const &) const
printf("%d Experimental points found\n", nlines)
std::vector< std::string > pattern_
bool operator()(EventID const &) const
bool isFlush() const
Definition: SubRunID.h:102
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
bool isValid() const
Definition: RunID.h:69
HLT enums.
EventIDMatcher(std::string const &pattern)
EventNumber_t event() const
Definition: EventID.h:117
std::vector< std::vector< std::vector< PatternRangeElement > > > parsed_patterns_
SubRunNumber_t subRun() const
Definition: EventID.h:111