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