CMS 3D CMS Logo

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