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  ~EventSelector() override {}
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 
31  ~RunLumiSelector() override {}
32  bool accept(const fwlite::EventBase &ev) const override { return accept(ev.id().run(), ev.luminosityBlock()); }
33  void add(int run, int firstLumi = 0, int lastLumi = 9999999) {
34  runs.push_back(run);
35  firstLumis.push_back(firstLumi);
36  lastLumis.push_back(lastLumi);
37  }
38  void clear() {
39  runs.clear();
40  firstLumis.clear();
41  lastLumis.clear();
42  }
43  bool accept(int run, int luminosityBlock) const {
44  if (runs.empty())
45  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]))
49  return true;
50  }
51  }
52  return false;
53  }
54 
55  private:
56  std::vector<int> runs, firstLumis, lastLumis;
57  };
58 
59  template <typename Collection>
61  public:
63  const char *instance,
64  const char *process,
65  const char *cut,
66  int minNumber = 1,
67  int maxNumber = -1)
68  : label_(label),
70  min_(minNumber),
71  max_(maxNumber),
72  scanner(
73  new helper::ScannerBase(helper::Parser::elementType(edm::TypeWithDict(HandleT::TempWrapT::typeInfo())))) {
74  scanner->setCut(cut);
76  }
77  ~ObjectCountSelector() override { delete scanner; }
78  bool accept(const fwlite::EventBase &ev) const override {
79  int nfound = 0;
80  HandleT handle; // here, not as a datamember, otherwise CINT segfaults (!?)
81  handle.getByLabel(ev, label_.c_str(), instance_.c_str(), process_.c_str());
82  const Collection &vals = *handle;
83  for (size_t j = 0, n = vals.size(); j < n; ++j) {
84  if (scanner->test(&vals[j]))
85  nfound++;
86  }
87  return (nfound >= min_ && (max_ == -1 || nfound <= max_));
88  }
89  void setCut(const char *cut) { scanner->setCut(cut); }
90  void setMin(int minNumber) { min_ = minNumber; }
91  void setMax(int maxNumber) { max_ = maxNumber; }
92  void setIgnoreExceptions(bool ignoreThem = true) { scanner->setIgnoreExceptions(ignoreThem); }
93 
94  protected:
97  int min_, max_;
98  helper::ScannerBase *scanner; // has to be a pointer, otherwise CINT segfaults in setCut (!?)
99  // prevent copy c-tor and assignment
102  };
103 } // namespace fwlite
104 #endif // PhysicsTools_FWLite_EventSelectors_h
RunLumiSelector(int run, int firstLumi=0, int lastLumi=9999999)
std::vector< int > firstLumis
bool accept(const fwlite::EventBase &ev) const override
Definition: helper.py:1
ObjectCountSelector & operator=(const fwlite::ObjectCountSelector< Collection > &other)
void add(int run, int firstLumi=0, int lastLumi=9999999)
static PFTauRenderPlugin instance
void setIgnoreExceptions(bool ignoreThem)
helper::ScannerBase * scanner
RunLumiSelector(const char *name="", const char *title="")
bool test(const void *obj, size_t icut=0) const
void setMax(int maxNumber)
char const * label
std::vector< int > runs
fwlite::Handle< Collection > HandleT
virtual bool accept(const fwlite::EventBase &ev) const =0
std::vector< int > lastLumis
bool accept(int run, int luminosityBlock) const
void setIgnoreExceptions(bool ignoreThem=true)
bool setCut(const char *cut)
Set the default cut that is applied to the events.
void setMin(int minNumber)
HLT enums.
bool accept(const fwlite::EventBase &ev) const override
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)