CMS 3D CMS Logo

EventSelectors.h
Go to the documentation of this file.
1 #ifndef PhysicsTools_FWLite_EventSelectors_h
2 #define PhysicsTools_FWLite_EventSelectors_h
3 // these includes are FWLite-safe
6 // these are from ROOT, so they're safe too
7 #include <TString.h>
8 #include <TNamed.h>
9 
10 #if !defined(__CINT__) && !defined(__MAKECINT__)
12 #endif
13 
14 namespace fwlite {
15  class EventSelector : public TNamed {
16  public:
17  EventSelector(const char *name="", const char *title="") : TNamed(name,title) {}
18  virtual ~EventSelector() {}
19  virtual bool accept(const fwlite::EventBase &ev) const = 0;
20  };
21 
22  class RunLumiSelector : public EventSelector {
23  public:
24  RunLumiSelector(const char *name="", const char *title="") : EventSelector(name,title) {}
25  RunLumiSelector(int run, int firstLumi=0, int lastLumi = 9999999) :
26  EventSelector(TString::Format("run%d_lumi%d_%d", run, firstLumi, lastLumi),
27  TString::Format("Run %d, Lumi range [%d, %d]", run, firstLumi, lastLumi))
28  { add(run, firstLumi, lastLumi); }
29 
30  virtual ~RunLumiSelector() {}
31  virtual bool accept(const fwlite::EventBase &ev) const {
32  return accept(ev.id().run(), ev.luminosityBlock());
33  }
34  void add(int run, int firstLumi=0, int lastLumi = 9999999) {
35  runs.push_back(run);
36  firstLumis.push_back(firstLumi);
37  lastLumis.push_back(lastLumi);
38  }
39  void clear() {
40  runs.clear();
41  firstLumis.clear();
42  lastLumis.clear();
43  }
44  bool accept(int run, int luminosityBlock) const {
45  if (runs.empty()) return true;
46  for (int i = 0, n = runs.size(); i < n; ++i) {
47  if (runs[i] == run) {
48  if ((firstLumis[i] <= luminosityBlock) && (luminosityBlock <= lastLumis[i])) return true;
49  }
50  }
51  return false;
52  }
53 
54  private:
55  std::vector<int> runs, firstLumis, lastLumis;
56  };
57 
58  template<typename Collection>
60  public:
61  ObjectCountSelector(const char *label, const char *instance, const char *process,
62  const char *cut, int minNumber=1, int maxNumber=-1) :
63  label_(label), instance_(instance),
64  min_(minNumber), max_(maxNumber),
65  scanner(new helper::ScannerBase(helper::Parser::elementType(edm::TypeWithDict(HandleT::TempWrapT::typeInfo()))))
66  {
67  scanner->setCut(cut);
68  scanner->setIgnoreExceptions(true);
69  }
70  ~ObjectCountSelector() { delete scanner; }
71  virtual bool accept(const fwlite::EventBase &ev) const {
72  int nfound = 0;
73  HandleT handle; // here, not as a datamember, otherwise CINT segfaults (!?)
74  handle.getByLabel(ev, label_.c_str(), instance_.c_str(), process_.c_str());
75  const Collection & vals = *handle;
76  for (size_t j = 0, n = vals.size(); j < n; ++j) {
77  if (scanner->test(&vals[j])) nfound++;
78  }
79  return (nfound >= min_ && (max_ == -1 || nfound <= max_));
80  }
81  void setCut(const char *cut) { scanner->setCut(cut); }
82  void setMin(int minNumber) { min_ = minNumber; }
83  void setMax(int maxNumber) { max_ = maxNumber; }
84  void setIgnoreExceptions(bool ignoreThem=true) { scanner->setIgnoreExceptions(ignoreThem); }
85  protected:
87  std::string label_, instance_, process_;
88  int min_, max_;
89  helper::ScannerBase *scanner; // has to be a pointer, otherwise CINT segfaults in setCut (!?)
90  // prevent copy c-tor and assignment
93  };
94 }
95 #endif // PhysicsTools_FWLite_EventSelectors_h
RunNumber_t run() const
Definition: EventID.h:39
RunLumiSelector(int run, int firstLumi=0, int lastLumi=9999999)
Definition: helper.py:1
void add(int run, int firstLumi=0, int lastLumi=9999999)
static PFTauRenderPlugin instance
edm::LuminosityBlockNumber_t luminosityBlock() const
Definition: EventBase.h:63
bool ev
helper::ScannerBase * scanner
RunLumiSelector(const char *name="", const char *title="")
void setMax(int maxNumber)
void getByLabel(const P &iP, const char *iModuleLabel, const char *iProductInstanceLabel=0, const char *iProcessLabel=0)
Definition: Handle.h:91
char const * label
bool accept(int run, int luminosityBlock) const
virtual bool accept(const fwlite::EventBase &ev) const
std::vector< int > runs
fwlite::Handle< Collection > HandleT
virtual bool accept(const fwlite::EventBase &ev) const =0
void add(std::map< std::string, TH1 * > &h, TH1 *hist)
void setIgnoreExceptions(bool ignoreThem=true)
void setMin(int minNumber)
edm::EventID id() const
Definition: EventBase.h:60
HLT enums.
virtual bool accept(const fwlite::EventBase &ev) const
void setCut(const char *cut)
EventSelector(const char *name="", const char *title="")
ObjectCountSelector(const char *label, const char *instance, const char *process, const char *cut, int minNumber=1, int maxNumber=-1)