CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Selections.h
Go to the documentation of this file.
1 #ifndef Selections_H
2 #define Selections_H
3 
6 #include <cstdlib>
7 #include <iomanip>
8 #include <iostream>
9 #include <sstream>
10 #include "TFormula.h"
11 
12 class Filter {
13 public:
14  Filter() = default;
18  dump_ = iConfig.dump();
19  if (!iConfig.empty()) {
20  const std::string d("name");
22  std::string componentName = iConfig.getParameter<std::string>("selector");
23  selector_ = EventSelectorFactoryFromHelper::get()->create(componentName, iConfig, iC);
24  if (iConfig.exists("description"))
25  description_ = iConfig.getParameter<std::vector<std::string> >("description");
26  else
27  description_ = selector_->description();
28  }
29  }
30  virtual ~Filter() {}
31 
32  Filter(const Filter&) = delete;
33  Filter& operator=(const Filter&) = delete;
34  Filter(Filter&&) = default;
35  Filter& operator=(Filter&&) = default;
36 
37  const std::string& name() { return name_; }
38  const std::string& dump() { return dump_; }
39  const std::vector<std::string> description() { return description_; }
42  for (unsigned int i = 0; i != description_.size(); ++i)
43  text += description_[i] + "\n";
44  text += dump() + "\n";
45  return text;
46  }
47 
48  virtual bool accept(edm::Event& iEvent) {
49  bool decision = false;
51  eventCacheID_ != iEvent.cacheIdentifier()) {
52  eventCacheID_ = iEvent.cacheIdentifier();
53 
54  if (selector_)
55  decision = selector_->select(iEvent);
56  else
57  decision = true;
58  cached_decision_ = decision;
59  } else {
60  decision = cached_decision_;
61  }
62  return decision;
63  }
64 
65 protected:
67  std::vector<std::string> description_;
68  std::unique_ptr<EventSelector> selector_;
69  mutable bool cached_decision_;
72 };
73 
74 // will behave like a filter*
75 class SFilter {
76 public:
77  SFilter(Filter* f, bool i) : filter_(f), inverted_(i){};
78  ~SFilter(){};
79 
80  Filter& operator*() { return *filter_; }
81  Filter* operator->() { return filter_; }
82  bool inverted() { return inverted_; }
83 
84 private:
86  bool inverted_;
87 };
88 
89 class FilterOR : public Filter {
90 public:
91  ~FilterOR() override {}
92  FilterOR(const std::string& filterORlist, const std::map<std::string, Filter*>& filters) {
93  std::string filterORlistCopy = filterORlist;
94  name_ = filterORlist;
95  std::stringstream ss;
96  ss << "Filter doing an OR of: ";
97  //split the OR-separated string into vector of strings
98  unsigned int size = 0;
99  bool OK = true;
100  while (OK) {
101  size_t orPos = filterORlistCopy.find("_OR_");
102  if (orPos == std::string::npos && !filterORlistCopy.empty()) {
103  size = filterORlistCopy.size();
104  OK = false;
105  } else
106  size = orPos;
107 
108  std::string filter = filterORlistCopy.substr(0, size);
109  //remove the filter name and the OR (4 characters) from the string
110  if (OK)
111  filterORlistCopy = filterORlistCopy.substr(0 + size + 4);
112 
113  std::map<std::string, Filter*>::const_iterator it = filters.find(filter);
114  if (it == filters.end()) {
115  edm::LogError("FilterOR") << "cannot do an OR of: " << filter << " OR expression is: " << filterORlist;
116  break;
117  }
118  filters_.push_back(std::make_pair(it->first, it->second));
119  ss << it->first << " ";
120  }
121  description_.push_back(ss.str());
122  }
123  bool accept(edm::Event& iEvent) override {
124  for (unsigned int i = 0; i != filters_.size(); ++i)
125  if (filters_[i].second->accept(iEvent))
126  return true;
127  return false;
128  }
129 
130 private:
131  std::vector<std::pair<std::string, Filter*> > filters_;
132 };
133 
134 //forward declaration for friendship
135 class FilterSelections;
136 
137 class FilterSelection : public Filter {
138 public:
139  typedef std::vector<SFilter>::iterator iterator;
140  friend class FilterSelections;
141 
143  : name_(name),
144  ntuplize_(iConfig.getParameter<bool>("ntuplize")),
145  makeContentPlots_(iConfig.getParameter<bool>("makeContentPlots")),
146  makeFinalPlots_(iConfig.getParameter<bool>("makeFinalPlots")),
147  makeCumulativePlots_(iConfig.getParameter<bool>("makeCumulativePlots")),
148  makeAllButOnePlots_(iConfig.getParameter<bool>("makeAllButOnePlots")),
149  nSeen_(0),
150  makeSummaryTable_(iConfig.getParameter<bool>("makeSummaryTable")),
151  makeDetailledPrintout_(iConfig.exists("detailledPrintoutCategory")) {
153  Filter::description_.push_back(std::string("See definition of the corresponding selection"));
154  if (iConfig.exists("nMonitor"))
155  nMonitor_ = iConfig.getParameter<unsigned int>("nMonitor");
156  else
157  nMonitor_ = 0;
158 
160  detailledPrintoutCategory_ = iConfig.getParameter<std::string>("detailledPrintoutCategory");
161  }
162 
163  FilterSelection(const FilterSelection&) = delete;
164  FilterSelection& operator=(const FilterSelection&) = delete;
165  FilterSelection(FilterSelection&&) = default;
167 
168  const std::string& name() { return name_; }
169  iterator begin() { return filters_.begin(); }
170  iterator end() { return filters_.end(); }
171 
172  bool accept(edm::Event& iEvent) override {
174  eventCacheID_ != iEvent.cacheIdentifier()) {
175  this->acceptMap(iEvent);
176  }
177  return cached_decision_;
178  }
179 
180  std::map<std::string, bool> acceptMap(edm::Event& iEvent) {
181  nSeen_++;
182  if (nMonitor_ != 0 && nSeen_ % nMonitor_ == 0) {
183  if (nSeen_ == nMonitor_)
184  print();
185  else
186  print(false);
187  }
188  std::map<std::string, bool> ret;
189  bool global = true;
190  for (iterator filter = begin(); filter != end(); ++filter) {
191  const std::string& fName = (*filter)->name();
192  Count& count = counts_[fName];
193  count.nSeen_++;
194  bool decision = (*filter)->accept(iEvent);
195  bool inverted = (*filter).inverted();
196  if (inverted)
197  decision = !decision;
198  ret[fName] = decision;
199  if (decision)
200  count.nPass_++;
201  global = global && decision;
202  if (global)
203  count.nCumulative_++;
204  }
205 
207  std::stringstream summary;
208  summary << std::setw(20) << name().substr(0, 19) << " : " << std::setw(10) << iEvent.id().run() << " : "
209  << std::setw(10) << iEvent.id().event();
210  for (iterator filter = begin(); filter != end(); ++filter) {
211  const std::string& fName = (*filter)->name();
212  summary << " : " << std::setw(10) << (ret[fName] ? "pass" : "reject");
213  }
215  }
216 
217  cached_decision_ = global;
218  eventCacheID_ = iEvent.cacheIdentifier();
219  return ret;
220  }
221 
224  std::stringstream summary;
225  summary << std::setw(20) << " selection name "
226  << " : " << std::setw(10) << " run "
227  << " : " << std::setw(10) << " event ";
228  for (iterator filter = begin(); filter != end(); ++filter) {
229  summary << " : " << std::setw(10) << (*filter)->name().substr(0, 9);
230  }
232  }
233  }
234  //print to LogVerbatim("Selections|<name()>")
235  void print(bool description = true) {
236  if (!makeSummaryTable_)
237  return;
238 
239  unsigned int maxFnameSize = 20;
240  for (iterator filter = begin(); filter != end(); ++filter) {
241  if ((*filter)->name().size() > maxFnameSize)
242  maxFnameSize = (*filter)->name().size() + 1;
243  }
244 
245  // const std::string category ="Selections|"+name();
246  const std::string category = "Selections";
247  std::stringstream summary;
248  summary << " Summary table for selection: " << name() << " with: " << nSeen_ << " events run." << std::endl;
249  if (nSeen_ == 0)
250  return;
251  if (description) {
252  for (iterator filter = begin(); filter != end(); ++filter) {
253  const std::string& fName = (*filter)->name();
254  summary << "filter: " << std::right << std::setw(10) << fName << "\n" << (*filter)->descriptionText() << "\n";
255  }
256  }
257  summary << " filter stand-alone pass: " << std::endl;
258  summary << std::right << std::setw(maxFnameSize) << "total read"
259  << ": " << std::right << std::setw(10) << nSeen_ << std::endl;
260  for (iterator filter = begin(); filter != end(); ++filter) {
261  std::string fName = (*filter)->name();
262  const Count& count = counts_[fName];
263  if ((*filter).inverted())
264  fName = '!' + fName;
265  summary << std::right << std::setw(maxFnameSize) << fName << ": " << std::right << std::setw(10) << count.nPass_
266  << " passed events. " << std::right << std::setw(10) << std::setprecision(5)
267  << (count.nPass_ / (float)count.nSeen_) * 100. << " [%]" << std::endl;
268  }
269  summary << " filter cumulative pass:" << std::endl;
270  summary << std::right << std::setw(maxFnameSize) << "total read"
271  << ": " << std::right << std::setw(10) << nSeen_ << std::endl;
272  unsigned int lastCount = nSeen_;
273  for (iterator filter = begin(); filter != end(); ++filter) {
274  std::string fName = (*filter)->name();
275  const Count& count = counts_[fName];
276  if ((*filter).inverted())
277  fName = '!' + fName;
278  summary << std::right << std::setw(maxFnameSize) << fName << ": " << std::right << std::setw(10)
279  << count.nCumulative_ << " passed events. " << std::right << std::setw(10) << std::setprecision(5)
280  << (count.nCumulative_ / (float)count.nSeen_) * 100. << " [%]";
281  if (lastCount != 0)
282  summary << " (to previous count) " << std::right << std::setw(10) << std::setprecision(5)
283  << (count.nCumulative_ / (float)lastCount) * 100. << " [%]";
284  summary << std::endl;
285 
286  lastCount = count.nCumulative_;
287  }
288  summary << "-------------------------------------\n";
289  edm::LogVerbatim(category) << summary.str();
290  std::cout << summary.str();
291  };
292 
293  bool ntuplize() { return ntuplize_; }
295  bool makeFinalPlots() { return makeFinalPlots_; }
299 
300 private:
302  std::vector<SFilter> filters_; // this is the local list of filters belonging to the selection
303 
304  //some options
305  bool ntuplize_;
310 
311  unsigned int nSeen_;
312  unsigned int nMonitor_;
313 
314  struct Count {
315  unsigned int nPass_;
316  unsigned int nSeen_;
317  unsigned int nCumulative_;
318  };
319  std::map<std::string, Count> counts_;
323 };
324 
326 public:
327  typedef std::vector<FilterSelection>::iterator iterator;
328 
330  : filtersPSet_(iConfig.getParameter<edm::ParameterSet>("filters")),
331  selectionPSet_(iConfig.getParameter<edm::ParameterSet>("selections")) {
332  //FIXME. what about nested filters
333  // not so needed since we are explicitely spelling all modules
334  //make all configured filters
335  std::vector<std::string> filterNames;
336  unsigned int nF = filtersPSet_.getParameterSetNames(filterNames);
337  for (unsigned int iF = 0; iF != nF; iF++) {
339  filters_.insert(std::make_pair(filterNames[iF],
340  new Filter(filterNames[iF], pset, iC))); // this is the global pool of filters
341  }
342 
343  //parse all configured selections
344  std::vector<std::string> selectionNames;
345  std::map<std::string, std::vector<std::string> > selectionFilters;
346  unsigned int nS = selectionPSet_.getParameterSetNames(selectionNames);
347  for (unsigned int iS = 0; iS != nS; iS++) {
349  // JR-2014 : the filters are not expanded here
350  selections_.push_back(FilterSelection(selectionNames[iS], pset));
351  // selections_.insert(std::make_pair(selectionNames[iS],Selection(selectionNames[iS],pset)));
352  //keep track of list of filters for this selection for further dependency resolution
353  selectionFilters[selectionNames[iS]] = pset.getParameter<std::vector<std::string> >("filterOrder");
354  }
355 
356  //watch out of recursive dependency
357  // unsigned int nestedDepth=0; //FIXME not taken care of
358 
359  //resolving dependencies
360  for (std::map<std::string, std::vector<std::string> >::iterator sIt = selectionFilters.begin();
361  sIt != selectionFilters.end();
362  ++sIt) {
363  //parse the vector of filterNames
364  for (std::vector<std::string>::iterator fOrS = sIt->second.begin(); fOrS != sIt->second.end(); ++fOrS) {
365  if (filters_.find(*fOrS) == filters_.end()) {
366  //not a know filter names uncountered : either Selection of _OR_.
367  if (fOrS->find("_OR_") != std::string::npos) {
368  filters_.insert(std::make_pair((*fOrS), new FilterOR((*fOrS), filters_)));
369  } //_OR_ filter
370  else {
371  // look for a selection name
372  std::map<std::string, std::vector<std::string> >::iterator s = selectionFilters.find(*fOrS);
373  if (s == selectionFilters.end()) {
374  //error.
375  if ((*fOrS)[0] != '!') {
376  edm::LogError("SelectionHelper") << "unresolved filter/selection name: " << *fOrS;
377  }
378  } //not a Selection name.
379  else {
380  // JR-2014 : don't do anything here, and move on : in fact no, replace it to have the details in the histograming tool
381  //remove the occurence
382  std::vector<std::string>::iterator newLoc = sIt->second.erase(fOrS);
383  //insert the list of filters corresponding to this selection in there
384  sIt->second.insert(newLoc, s->second.begin(), s->second.end());
385  //decrement selection iterator to come back to it
386  sIt--;
387  break;
388  } //a Selection name
389  }
390  } //the name is not a simple filter name : either Selection of _OR_.
391 
392  } //loop over the strings in "filterOrder"
393  } //loop over all defined Selection
394 
395  //finally, configure the Selections
396  //loop the selections instanciated
397  // for (std::map<std::string, FilterSelection>::iterator sIt=selections_.begin();sIt!=selections_.end();++sIt)
398  // const std::string & sName=sIt->first;
399  //FilterSelection & selection =sIt->second;
400  for (std::vector<FilterSelection>::iterator sIt = selections_.begin(); sIt != selections_.end(); ++sIt) {
401  const std::string& sName = sIt->name();
402  FilterSelection& selection = *sIt;
403 
404  //parse the vector of filterNames
405  std::vector<std::string>& listOfFilters = selectionFilters[sName];
406  for (std::vector<std::string>::iterator fIt = listOfFilters.begin(); fIt != listOfFilters.end(); ++fIt) {
407  std::string fOsName = *fIt;
408  bool inverted = false;
409  if (fOsName[0] == '!') {
410  inverted = true;
411  fOsName = fOsName.substr(1);
412  }
413  std::map<std::string, Filter*>::iterator filterInstance = filters_.find(fOsName);
414  if (filterInstance == filters_.end()) {
415  // JR-2014 include the selection here, directly !
416  bool replaceBySelection = false;
417  //find an existing selection that match that name
418  for (std::vector<FilterSelection>::iterator sit = selections_.begin(); sit != selections_.end(); ++sit) {
419  if (fOsName == sit->name_) {
420  selection.filters_.push_back(SFilter(&(*sit), inverted));
421  replaceBySelection = true;
422  }
423  }
424  if (!replaceBySelection) {
425  //error
426  edm::LogError("Selections") << "cannot resolve: " << fOsName;
427  }
428  } else {
429  //actually increment the filter
430  selection.filters_.push_back(SFilter(filterInstance->second, inverted));
431  }
432  }
433  }
434 
435  for (iterator sIt = begin(); sIt != end(); ++sIt)
436  sIt->printDetailledPrintoutHeader();
437  }
438 
439  iterator begin() { return selections_.begin(); }
440  iterator end() { return selections_.end(); }
441 
442  //print each selection
443  void print() {
444  for (std::vector<FilterSelection>::iterator sIt = selections_.begin(); sIt != selections_.end(); ++sIt)
445  sIt->print();
446  }
447 
448 private:
450  std::map<std::string, Filter*> filters_; // the global collection of available filters to pick from
451 
453  std::vector<FilterSelection> selections_;
454 };
455 
456 #endif
RunNumber_t run() const
Definition: EventID.h:38
edm::ParameterSet selectionPSet_
Definition: Selections.h:452
Log< level::Info, true > LogVerbatim
EventNumber_t event() const
Definition: EventID.h:40
unsigned long CacheIdentifier_t
Definition: Event.h:117
bool empty() const
Definition: ParameterSet.h:201
std::vector< std::string > description_
Definition: Selections.h:67
tuple ret
prodAgent to be discontinued
std::vector< FilterSelection >::iterator iterator
Definition: Selections.h:327
std::string dump_
Definition: Selections.h:71
~SFilter()
Definition: Selections.h:78
void printDetailledPrintoutHeader()
Definition: Selections.h:222
bool makeCumulativePlots_
Definition: Selections.h:308
virtual bool accept(edm::Event &iEvent)
Definition: Selections.h:48
std::vector< FilterSelection > selections_
Definition: Selections.h:453
unsigned int nMonitor_
Definition: Selections.h:312
static const char category[]
std::string dump(unsigned int indent=0) const
std::vector< std::pair< std::string, Filter * > > filters_
Definition: Selections.h:131
edm::Event::CacheIdentifier_t eventCacheID_
Definition: Selections.h:70
bool accept(edm::Event &iEvent) override
Definition: Selections.h:123
FilterSelections(const edm::ParameterSet &iConfig, edm::ConsumesCollector &&iC)
Definition: Selections.h:329
std::vector< SFilter > filters_
Definition: Selections.h:302
std::map< std::string, bool > acceptMap(edm::Event &iEvent)
Definition: Selections.h:180
bool makeSummaryTable_
Definition: Selections.h:320
selection
main part
Definition: corrVsCorr.py:100
unsigned int nSeen_
Definition: Selections.h:316
bool exists(std::string const &parameterName) const
checks if a parameter exists
bool makeSummaryTable()
Definition: Selections.h:298
edm::ParameterSet filtersPSet_
Definition: Selections.h:449
Filter * filter_
Definition: Selections.h:85
Log< level::Error, false > LogError
bool makeAllButOnePlots_
Definition: Selections.h:309
FilterSelection & operator=(const FilterSelection &)=delete
const std::string & dump()
Definition: Selections.h:38
std::vector< SFilter >::iterator iterator
Definition: Selections.h:139
std::vector< TPRegexp > filters
Definition: eve_filter.cc:22
bool inverted_
Definition: Selections.h:86
bool makeContentPlots()
Definition: Selections.h:294
const std::string & name()
Definition: Selections.h:168
std::string detailledPrintoutCategory_
Definition: Selections.h:322
U second(std::pair< T, U > const &p)
tuple d
Definition: ztail.py:151
int iEvent
Definition: GenABIO.cc:224
bool accept(edm::Event &iEvent) override
Definition: Selections.h:172
unsigned int nCumulative_
Definition: Selections.h:317
Filter()=default
bool makeCumulativePlots()
Definition: Selections.h:296
bool cached_decision_
Definition: Selections.h:69
~FilterOR() override
Definition: Selections.h:91
iterator begin()
Definition: Selections.h:169
dd4hep::Filter Filter
CacheIdentifier_t cacheIdentifier() const
Definition: Event.cc:37
FilterOR(const std::string &filterORlist, const std::map< std::string, Filter * > &filters)
Definition: Selections.h:92
iterator end()
Definition: Selections.h:440
const std::vector< std::string > description()
Definition: Selections.h:39
Filter(std::string name, edm::ParameterSet &iConfig, edm::ConsumesCollector &iC)
Definition: Selections.h:16
const std::string & name()
Definition: Selections.h:37
tuple text
Definition: runonSM.py:43
bool makeFinalPlots()
Definition: Selections.h:295
const std::string descriptionText()
Definition: Selections.h:40
iterator end()
Definition: Selections.h:170
bool inverted()
Definition: Selections.h:82
std::pair< int, edm::FunctionWithDict > OK
Definition: findMethod.cc:126
Filter * operator->()
Definition: Selections.h:81
void addUntrackedParameter(std::string const &name, T const &value)
Definition: ParameterSet.h:192
virtual ~Filter()
Definition: Selections.h:30
std::unique_ptr< EventSelector > selector_
Definition: Selections.h:68
std::string name_
Definition: Selections.h:66
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
bool makeContentPlots_
Definition: Selections.h:306
unsigned int nPass_
Definition: Selections.h:315
edm::EventID id() const
Definition: EventBase.h:59
Filter & operator=(const Filter &)=delete
Filter & operator*()
Definition: Selections.h:80
iterator begin()
Definition: Selections.h:439
SFilter(Filter *f, bool i)
Definition: Selections.h:77
FilterSelection(std::string name, const edm::ParameterSet &iConfig)
Definition: Selections.h:142
tuple cout
Definition: gather_cfg.py:144
std::string name_
Definition: Selections.h:301
std::map< std::string, Count > counts_
Definition: Selections.h:319
bool makeFinalPlots_
Definition: Selections.h:307
unsigned int nSeen_
Definition: Selections.h:311
#define get
bool makeAllButOnePlots()
Definition: Selections.h:297
size_t getParameterSetNames(std::vector< std::string > &output, bool trackiness=true) const
void print(bool description=true)
Definition: Selections.h:235
bool makeDetailledPrintout_
Definition: Selections.h:321
tuple size
Write out results.
std::map< std::string, Filter * > filters_
Definition: Selections.h:450