CMS 3D CMS Logo

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