CMS 3D CMS Logo

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