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