CMS 3D CMS Logo

TriggerResultsBasedEventSelector.cc
Go to the documentation of this file.
1 #include <algorithm>
2 
7 
9 
10 namespace {
11  //--------------------------------------------------------
12  // Remove whitespace (spaces and tabs) from a std::string.
13  void remove_whitespace(std::string& s) {
14  s.erase(std::remove(s.begin(), s.end(), ' '), s.end());
15  s.erase(std::remove(s.begin(), s.end(), '\t'), s.end());
16  }
17 
18  void test_remove_whitespace() {
19  std::string a("noblanks");
20  std::string b("\t no blanks \t");
21 
22  remove_whitespace(b);
23  assert(a == b);
24  }
25 
26  //--------------------------------------------------------
27  // Given a path-spec (std::string of the form "a:b", where the ":b" is
28  // optional), return a parsed_path_spec_t containing "a" and "b".
29 
30  typedef std::pair<std::string, std::string> parsed_path_spec_t;
31  void parse_path_spec(std::string const& path_spec,
32  parsed_path_spec_t& output) {
33  std::string trimmed_path_spec(path_spec);
34  remove_whitespace(trimmed_path_spec);
35 
36  std::string::size_type colon = trimmed_path_spec.find(":");
37  if(colon == std::string::npos) {
38  output.first = trimmed_path_spec;
39  } else {
40  output.first = trimmed_path_spec.substr(0, colon);
41  output.second = trimmed_path_spec.substr(colon + 1,
42  trimmed_path_spec.size());
43  }
44  }
45 
46  void test_parse_path_spec() {
47  std::vector<std::string> paths;
48  paths.push_back("a:p1");
49  paths.push_back("b:p2");
50  paths.push_back(" c");
51  paths.push_back("ddd\t:p3");
52  paths.push_back("eee: p4 ");
53 
54  std::vector<parsed_path_spec_t> parsed(paths.size());
55  for(size_t i = 0; i < paths.size(); ++i) {
56  parse_path_spec(paths[i], parsed[i]);
57  }
58 
59  assert(parsed[0].first == "a");
60  assert(parsed[0].second == "p1");
61  assert(parsed[1].first == "b");
62  assert(parsed[1].second == "p2");
63  assert(parsed[2].first == "c");
64  assert(parsed[2].second == "");
65  assert(parsed[3].first == "ddd");
66  assert(parsed[3].second == "p3");
67  assert(parsed[4].first == "eee");
68  assert(parsed[4].second == "p4");
69  }
70 }
71 
72 namespace edm
73 {
74  namespace test {
76  test_remove_whitespace();
77  test_parse_path_spec();
78  }
79  }
80 
81  namespace detail
82  {
83 
85  std::string const& iProcessName,
86  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();
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();
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, std::move(iC));
113 
114  return false;
115  }
116 
117  // typedef detail::NamedEventSelector NES;
118 
119  TriggerResultsBasedEventSelector::TriggerResultsBasedEventSelector() :
120  selectors_(),
121  wantAllEvents_(false)
122  { }
123 
124  void
126  wantAllEvents_ = true;
127  }
128 
129  void
130  TriggerResultsBasedEventSelector::setup(std::vector<parsed_path_spec_t> const& path_specs,
131  std::vector<std::string> const& triggernames,
132  std::string const& process_name,
133  ConsumesCollector&& iC) {
134  // paths_for_process maps each PROCESS names to a sequence of
135  // PATH names
136  std::map<std::string, std::vector<std::string> > paths_for_process;
137  for (auto const& path_spec : path_specs) {
138  // Default to current process if none specified
139  if (path_spec.second == "") {
140  paths_for_process[process_name].push_back(path_spec.first);
141  }
142  else {
143  paths_for_process[path_spec.second].push_back(path_spec.first);
144  }
145  }
146  // Now go through all the PROCESS names, and create a
147  // NamedEventSelector for each.
148  for (auto const& path : paths_for_process) {
149  // For the current process we know the trigger names
150  // from the configuration file
151  if (path.first == process_name) {
152  selectors_.emplace_back(path.first, EventSelector(path.second, triggernames), std::move(iC));
153  } else {
154  // For previous processes we do not know the trigger
155  // names yet.
156  selectors_.emplace_back(path.first, EventSelector(path.second), std::move(iC));
157  }
158  }
159  }
160 
161  bool
163  if(wantAllEvents_) {
164  return true;
165  }
166  for(auto& selector : selectors_) {
168  ev.getByToken<TriggerResults>(selector.token(), handle);
169  bool match = selector.match(*handle);
170  if(match) {
171  return true;
172  }
173  }
174  return false;
175  }
176 
179  std::string const& iLabel,
180  std::map<std::string, std::vector<std::pair<std::string, int> > > const& outputModulePathPositions,
181  bool anyProductProduced) {
182  ParameterSet selectEventsInfo;
183  selectEventsInfo.copyForModify(iInitial);
184  selectEventsInfo.addParameter<bool>("InProcessHistory", anyProductProduced);
185  std::vector<std::string> endPaths;
186  std::vector<int> endPathPositions;
187 
188  // The label will be empty if and only if this is a SubProcess
189  // SubProcess's do not appear on any end path
190  if (!iLabel.empty()) {
191  std::map<std::string, std::vector<std::pair<std::string, int> > >::const_iterator iter = outputModulePathPositions.find(iLabel);
192  assert(iter != outputModulePathPositions.end());
193  for(auto const& item : iter->second) {
194  endPaths.push_back(item.first);
195  endPathPositions.push_back(item.second);
196  }
197  }
198  selectEventsInfo.addParameter<std::vector<std::string> >("EndPaths", endPaths);
199  selectEventsInfo.addParameter<std::vector<int> >("EndPathPositions", endPathPositions);
200  if (!selectEventsInfo.exists("SelectEvents")) {
201  selectEventsInfo.addParameter<std::vector<std::string> >("SelectEvents", std::vector<std::string>());
202  }
203  selectEventsInfo.registerIt();
204 
205  return selectEventsInfo.id();
206  }
207 
208  } // namespace detail
209 } // namespace edm
T getParameter(std::string const &) const
bool empty() const
Definition: ParameterSet.h:218
ParameterSetID id() const
static const edm::TypeID s_TrigResultsType(typeid(edm::TriggerResults))
bool exists(std::string const &parameterName) const
checks if a parameter exists
bool ev
uint16_t size_type
U second(std::pair< T, U > const &p)
bool getByToken(EDGetToken token, TypeID const &typeID, BasicHandle &result) const
void addParameter(std::string const &name, T const &value)
Definition: ParameterSet.h:144
bool configureEventSelector(edm::ParameterSet const &iPSet, std::string const &iProcessName, std::vector< std::string > const &iAllTriggerNames, edm::detail::TriggerResultsBasedEventSelector &oSelector, ConsumesCollector &&iC)
void copyForModify(ParameterSet const &other)
double b
Definition: hdecay.h:120
def remove(d, key, TELL=False)
Definition: MatrixUtil.py:211
HLT enums.
double a
Definition: hdecay.h:121
void setup(std::vector< parsed_path_spec_t > const &path_specs, std::vector< std::string > const &triggernames, std::string const &process_name, ConsumesCollector &&iC)
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)
ParameterSet const & registerIt()
def move(src, dest)
Definition: eostools.py:510
std::string match(BranchDescription const &a, BranchDescription const &b, std::string const &fileName)