CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TriggerResultsBasedEventSelector.cc
Go to the documentation of this file.
1 #include <algorithm>
2 
3 #include "boost/bind.hpp"
4 
8 
10 
11 namespace {
12  //--------------------------------------------------------
13  // Remove whitespace (spaces and tabs) from a std::string.
14  void remove_whitespace(std::string& s) {
15  s.erase(std::remove(s.begin(), s.end(), ' '), s.end());
16  s.erase(std::remove(s.begin(), s.end(), '\t'), s.end());
17  }
18 
19  void test_remove_whitespace() {
20  std::string a("noblanks");
21  std::string b("\t no blanks \t");
22 
23  remove_whitespace(b);
24  assert(a == b);
25  }
26 
27  //--------------------------------------------------------
28  // Given a path-spec (std::string of the form "a:b", where the ":b" is
29  // optional), return a parsed_path_spec_t containing "a" and "b".
30 
31  typedef std::pair<std::string, std::string> parsed_path_spec_t;
32  void parse_path_spec(std::string const& path_spec,
33  parsed_path_spec_t& output) {
34  std::string trimmed_path_spec(path_spec);
35  remove_whitespace(trimmed_path_spec);
36 
37  std::string::size_type colon = trimmed_path_spec.find(":");
38  if(colon == std::string::npos) {
39  output.first = trimmed_path_spec;
40  } else {
41  output.first = trimmed_path_spec.substr(0, colon);
42  output.second = trimmed_path_spec.substr(colon + 1,
43  trimmed_path_spec.size());
44  }
45  }
46 
47  void test_parse_path_spec() {
48  std::vector<std::string> paths;
49  paths.push_back("a:p1");
50  paths.push_back("b:p2");
51  paths.push_back(" c");
52  paths.push_back("ddd\t:p3");
53  paths.push_back("eee: p4 ");
54 
55  std::vector<parsed_path_spec_t> parsed(paths.size());
56  for(size_t i = 0; i < paths.size(); ++i) {
57  parse_path_spec(paths[i], parsed[i]);
58  }
59 
60  assert(parsed[0].first == "a");
61  assert(parsed[0].second == "p1");
62  assert(parsed[1].first == "b");
63  assert(parsed[1].second == "p2");
64  assert(parsed[2].first == "c");
65  assert(parsed[2].second == "");
66  assert(parsed[3].first == "ddd");
67  assert(parsed[3].second == "p3");
68  assert(parsed[4].first == "eee");
69  assert(parsed[4].second == "p4");
70  }
71 }
72 
73 namespace edm
74 {
75  namespace test {
77  test_remove_whitespace();
78  test_parse_path_spec();
79  }
80  }
81 
82  namespace detail
83  {
84 
86  std::string const& iProcessName,
87  std::vector<std::string> const& iAllTriggerNames,
89  // If selectevents is an emtpy ParameterSet, then we are to write
90  // all events, or one which contains a vstrig 'SelectEvents' that
91  // is empty, we are to write all events. We have no need for any
92  // EventSelectors.
93  if(iPSet.empty()) {
94  oSelector.setupDefault(iAllTriggerNames);
95  return true;
96  }
97 
98  std::vector<std::string> path_specs =
99  iPSet.getParameter<std::vector<std::string> >("SelectEvents");
100 
101  if(path_specs.empty()) {
102  oSelector.setupDefault(iAllTriggerNames);
103  return true;
104  }
105 
106  // If we get here, we have the possibility of having to deal with
107  // path_specs that look at more than one process.
108  std::vector<parsed_path_spec_t> parsed_paths(path_specs.size());
109  for(size_t i = 0; i < path_specs.size(); ++i) {
110  parse_path_spec(path_specs[i], parsed_paths[i]);
111  }
112  oSelector.setup(parsed_paths, iAllTriggerNames, iProcessName);
113 
114  return false;
115  }
116 
117 
121  inputTag_,
122  nullptr,
123  mcc);
124  convert_handle(std::move(h),product_);
125  }
126 
128 
129 
131  fillDone_(false),
132  numberFound_(0),
133  selectors_()
134  { }
135 
136  void
137  TriggerResultsBasedEventSelector::setupDefault(std::vector<std::string> const& triggernames)
138  {
139 
140  // Set up one NamedEventSelector, with default configuration
141  std::vector<std::string> paths;
142  EventSelector es(paths, triggernames);
143  selectors_.emplace_back("", es);
144  //selectors_.push_back(NES("", EventSelector("",triggernames)));
145  }
146 
147  void
148  TriggerResultsBasedEventSelector::setup(std::vector<parsed_path_spec_t> const& path_specs,
149  std::vector<std::string> const& triggernames,
150  const std::string& process_name)
151  {
152  // paths_for_process maps each PROCESS names to a sequence of
153  // PATH names
154  std::map<std::string, std::vector<std::string> > paths_for_process;
155  for (std::vector<parsed_path_spec_t>::const_iterator
156  i = path_specs.begin(),
157  e = path_specs.end();
158  i != e;
159  ++i)
160  {
161  // Default to current process if none specified
162  if (i->second == "") {
163  paths_for_process[process_name].push_back(i->first);
164  }
165  else {
166  paths_for_process[i->second].push_back(i->first);
167  }
168  }
169  // Now go through all the PROCESS names, and create a
170  // NamedEventSelector for each.
171  for (std::map<std::string, std::vector<std::string> >::const_iterator
172  i = paths_for_process.begin(),
173  e = paths_for_process.end();
174  i != e;
175  ++i)
176  {
177  // For the current process we know the trigger names
178  // from the configuration file
179  if (i->first == process_name) {
180  selectors_.emplace_back(i->first,
181  EventSelector(i->second,
182  triggernames));
183  }
184  // For previous processes we do not know the trigger
185  // names yet.
186  else {
187  selectors_.emplace_back(i->first, EventSelector(i->second));
188  }
189  }
190  }
191 
194  {
195  fill(ev, mcc);
196  return returnOneHandleOrThrow();
197  }
198 
199  bool
201  {
202  // We have to get all the TriggerResults objects before we test
203  // any for a match, because we have to deal with the possibility
204  // of multiple TriggerResults objects --- note that the presence
205  // of more than one TriggerResults object in the event is
206  // intended to lead to an exception throw *unless* either the
207  // configuration has been set to match all events, or the
208  // configuration is set to use specific process names.
209 
210  fill(ev, mcc);
211 
212  // Now we go through and see if anyone matches...
213  iter i = selectors_.begin();
214  iter e = selectors_.end();
215  bool match_found = false;
216  while (!match_found && (i!=e))
217  {
218  match_found = i->match();
219  ++i;
220  }
221  return match_found;
222  }
223 
226  {
227  switch (numberFound_)
228  {
229  case 0:
231  "TooFewProducts")
232  << "TriggerResultsBasedEventSelector::returnOneHandleOrThrow: "
233  << " too few products found, "
234  << "exepcted one, got zero\n";
235  case 1:
236 
237  break;
238  default:
240  "TooManyMatches")
241  << "TriggerResultsBasedEventSelector::returnOneHandleOrThrow: "
242  << "too many products found, "
243  << "expected one, got " << numberFound_ << '\n';
244  }
245  return selectors_[0].product();
246  }
247 
250  {
251  if (!fillDone_)
252  {
253  fillDone_ = true;
254  for (iter i = selectors_.begin(), e = selectors_.end();
255  i != e; ++i)
256  {
257  i->fill(ev, mcc); // fill might throw...
258  ++numberFound_ ; // so numberFound_ might be less than expected
259  }
260  }
261  return numberFound_;
262  }
263 
264  void
266  {
267  for_all(selectors_, boost::bind(&NamedEventSelector::clear, _1));
268  fillDone_ = false;
269  numberFound_ = 0;
270  }
271 
272 
273 
276  std::string const& iLabel,
277  std::map<std::string, std::vector<std::pair<std::string, int> > > const& outputModulePathPositions,
278  bool anyProductProduced) {
279  ParameterSet selectEventsInfo;
280  selectEventsInfo.copyForModify(iInitial);
281  selectEventsInfo.addParameter<bool>("InProcessHistory", anyProductProduced);
282  std::vector<std::string> endPaths;
283  std::vector<int> endPathPositions;
284 
285  // The label will be empty if and only if this is a SubProcess
286  // SubProcess's do not appear on any end path
287  if (!iLabel.empty()) {
288  std::map<std::string, std::vector<std::pair<std::string, int> > >::const_iterator iter = outputModulePathPositions.find(iLabel);
289  assert(iter != outputModulePathPositions.end());
290  for (std::vector<std::pair<std::string, int> >::const_iterator i = iter->second.begin(), e = iter->second.end();
291  i != e; ++i) {
292  endPaths.push_back(i->first);
293  endPathPositions.push_back(i->second);
294  }
295  }
296  selectEventsInfo.addParameter<std::vector<std::string> >("EndPaths", endPaths);
297  selectEventsInfo.addParameter<std::vector<int> >("EndPathPositions", endPathPositions);
298  if (!selectEventsInfo.exists("SelectEvents")) {
299  selectEventsInfo.addParameter<std::vector<std::string> >("SelectEvents", std::vector<std::string>());
300  }
301  selectEventsInfo.registerIt();
302 
303  return selectEventsInfo.id();
304  }
305 
306 
307  } // namespace detail
308 } // namespace edm
T getParameter(std::string const &) const
bool empty() const
Definition: ParameterSet.h:216
int i
Definition: DBlmapReader.cc:9
ParameterSetID id() const
static const edm::TypeID s_TrigResultsType(typeid(edm::TriggerResults))
void fill(EventPrincipal const &e, ModuleCallingContext const *mcc)
bool exists(std::string const &parameterName) const
checks if a parameter exists
detail::NamedEventSelector NES
size_type fill(EventPrincipal const &ev, ModuleCallingContext const *)
uint16_t size_type
void setupDefault(std::vector< std::string > const &triggernames)
Func for_all(ForwardSequence &s, Func f)
wrapper for std::for_each
Definition: Algorithms.h:16
U second(std::pair< T, U > const &p)
void setup(std::vector< parsed_path_spec_t > const &path_specs, std::vector< std::string > const &triggernames, const std::string &process_name)
void addParameter(std::string const &name, T const &value)
Definition: ParameterSet.h:142
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
void copyForModify(ParameterSet const &other)
bool first
Definition: L1TdeRCT.cc:75
void convert_handle(BasicHandle &&bh, Handle< T > &result)
Definition: ConvertHandle.h:20
double b
Definition: hdecay.h:120
handle_t getOneTriggerResults(EventPrincipal const &e, ModuleCallingContext const *)
bool wantEvent(EventPrincipal const &e, ModuleCallingContext const *)
double a
Definition: hdecay.h:121
BasicHandle getByLabel(KindOfType kindOfType, TypeID const &typeID, InputTag const &inputTag, EDConsumerBase const *consumes, ModuleCallingContext const *mcc) const
Definition: Principal.cc:445
volatile std::atomic< bool > shutdown_flag false
ParameterSetID registerProperSelectionInfo(edm::ParameterSet const &iInitial, std::string const &iLabel, std::map< std::string, std::vector< std::pair< std::string, int > > > const &outputModulePathPositions, bool anyProductProduced)
bool configureEventSelector(edm::ParameterSet const &iPSet, std::string const &iProcessName, std::vector< std::string > const &iAllTriggerNames, edm::detail::TriggerResultsBasedEventSelector &oSelector)
ParameterSet const & registerIt()