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, parsed_path_spec_t& output) {
32  std::string trimmed_path_spec(path_spec);
33  remove_whitespace(trimmed_path_spec);
34 
35  std::string::size_type colon = trimmed_path_spec.find(':');
36  if (colon == std::string::npos) {
37  output.first = trimmed_path_spec;
38  } else {
39  output.first = trimmed_path_spec.substr(0, colon);
40  output.second = trimmed_path_spec.substr(colon + 1, trimmed_path_spec.size());
41  }
42  }
43 
44  void test_parse_path_spec() {
45  std::vector<std::string> paths;
46  paths.push_back("a:p1");
47  paths.push_back("b:p2");
48  paths.push_back(" c");
49  paths.push_back("ddd\t:p3");
50  paths.push_back("eee: p4 ");
51 
52  std::vector<parsed_path_spec_t> parsed(paths.size());
53  for (size_t i = 0; i < paths.size(); ++i) {
54  parse_path_spec(paths[i], parsed[i]);
55  }
56 
57  assert(parsed[0].first == "a");
58  assert(parsed[0].second == "p1");
59  assert(parsed[1].first == "b");
60  assert(parsed[1].second == "p2");
61  assert(parsed[2].first == "c");
62  assert(parsed[2].second.empty());
63  assert(parsed[3].first == "ddd");
64  assert(parsed[3].second == "p3");
65  assert(parsed[4].first == "eee");
66  assert(parsed[4].second == "p4");
67  }
68 } // namespace
69 
70 namespace edm {
71  namespace test {
73  test_remove_whitespace();
74  test_parse_path_spec();
75  }
76  } // namespace test
77 
78  namespace detail {
79 
81  std::string const& iProcessName,
82  std::vector<std::string> const& iAllTriggerNames,
85  // If selectevents is an emtpy ParameterSet, then we are to write
86  // all events, or one which contains a vstrig 'SelectEvents' that
87  // is empty, we are to write all events. We have no need for any
88  // EventSelectors.
89  if (iPSet.empty()) {
90  oSelector.setupDefault();
91  return true;
92  }
93 
94  std::vector<std::string> path_specs = iPSet.getParameter<std::vector<std::string> >("SelectEvents");
95 
96  if (path_specs.empty()) {
97  oSelector.setupDefault();
98  return true;
99  }
100 
101  // If we get here, we have the possibility of having to deal with
102  // path_specs that look at more than one process.
103  std::vector<parsed_path_spec_t> parsed_paths(path_specs.size());
104  for (size_t i = 0; i < path_specs.size(); ++i) {
105  parse_path_spec(path_specs[i], parsed_paths[i]);
106  }
107  oSelector.setup(parsed_paths, iAllTriggerNames, iProcessName, std::move(iC));
108 
109  return false;
110  }
111 
112  // typedef detail::NamedEventSelector NES;
113 
115 
117 
118  void TriggerResultsBasedEventSelector::setup(std::vector<parsed_path_spec_t> const& path_specs,
119  std::vector<std::string> const& triggernames,
120  std::string const& process_name,
121  ConsumesCollector&& iC) {
122  // paths_for_process maps each PROCESS names to a sequence of
123  // PATH names
124  std::map<std::string, std::vector<std::string> > paths_for_process;
125  for (auto const& path_spec : path_specs) {
126  // Default to current process if none specified
127  if (path_spec.second.empty()) {
128  paths_for_process[process_name].push_back(path_spec.first);
129  } else {
130  paths_for_process[path_spec.second].push_back(path_spec.first);
131  }
132  }
133  // Now go through all the PROCESS names, and create a
134  // NamedEventSelector for each.
135  for (auto const& path : paths_for_process) {
136  // For the current process we know the trigger names
137  // from the configuration file
138  if (path.first == process_name) {
139  selectors_.emplace_back(path.first, EventSelector(path.second, triggernames), std::move(iC));
140  } else {
141  // For previous processes we do not know the trigger
142  // names yet.
143  selectors_.emplace_back(path.first, EventSelector(path.second), std::move(iC));
144  }
145  }
146  }
147 
149  if (wantAllEvents_) {
150  return true;
151  }
152  for (auto& selector : selectors_) {
154  ev.getByToken<TriggerResults>(selector.token(), handle);
155  bool match = selector.match(*handle);
156  if (match) {
157  return true;
158  }
159  }
160  return false;
161  }
162 
164  edm::ParameterSet const& iInitial,
165  std::string const& iLabel,
166  std::map<std::string, std::vector<std::pair<std::string, int> > > const& outputModulePathPositions,
167  bool anyProductProduced) {
168  ParameterSet selectEventsInfo;
169  selectEventsInfo.copyForModify(iInitial);
170  selectEventsInfo.addParameter<bool>("InProcessHistory", anyProductProduced);
171  std::vector<std::string> endPaths;
172  std::vector<int> endPathPositions;
173 
174  // The label will be empty if and only if this is a SubProcess
175  // SubProcess's do not appear on any end path
176  if (!iLabel.empty()) {
177  std::map<std::string, std::vector<std::pair<std::string, int> > >::const_iterator iter =
178  outputModulePathPositions.find(iLabel);
179  assert(iter != outputModulePathPositions.end());
180  for (auto const& item : iter->second) {
181  endPaths.push_back(item.first);
182  endPathPositions.push_back(item.second);
183  }
184  }
185  selectEventsInfo.addParameter<std::vector<std::string> >("EndPaths", endPaths);
186  selectEventsInfo.addParameter<std::vector<int> >("EndPathPositions", endPathPositions);
187  if (!selectEventsInfo.exists("SelectEvents")) {
188  selectEventsInfo.addParameter<std::vector<std::string> >("SelectEvents", std::vector<std::string>());
189  }
190  selectEventsInfo.registerIt();
191 
192  return selectEventsInfo.id();
193  }
194 
195  } // namespace detail
196 } // namespace edm
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
Definition: Hash.h:43
static const edm::TypeID s_TrigResultsType(typeid(edm::TriggerResults))
bool exists(std::string const &parameterName) const
checks if a parameter exists
assert(be >=bs)
uint16_t size_type
U second(std::pair< T, U > const &p)
ParameterSetID id() const
ParameterSet const & registerIt()
bool empty() const
Definition: ParameterSet.h:201
void addParameter(std::string const &name, T const &value)
Definition: ParameterSet.h:135
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:118
def remove(d, key, TELL=False)
Definition: MatrixUtil.py:223
HLT enums.
double a
Definition: hdecay.h:119
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)
def move(src, dest)
Definition: eostools.py:511
std::string match(BranchDescription const &a, BranchDescription const &b, std::string const &fileName)