ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/GPetrucc/interface/fwlite/EventSelectors.h
Revision: 1.1
Committed: Tue Feb 2 18:11:54 2010 UTC (15 years, 3 months ago) by gpetrucc
Content type: text/plain
Branch: MAIN
Log Message:
- cleanup parser helper (make methods static)
- allow event selectors to be used with the scanner
  (e.g. req a number of objects passing a selection, or run/lumi)

File Contents

# User Rev Content
1 gpetrucc 1.1 // these includes are FWLite-safe
2     #include "DataFormats/FWLite/interface/Handle.h"
3     #include "DataFormats/FWLite/interface/Event.h"
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__)
9     #include "UserCode/GPetrucc/interface/fwliteHelpers.h"
10     #else
11     #ifndef UserCode_GPetrucc_CINT_load_library
12     #define UserCode_GPetrucc_CINT_load_library
13     // load the library that contains the dictionaries
14     int _load_UserCodeGPetrucc = gSystem->Load("libUserCodeGPetrucc.so");
15     #endif
16     #endif
17    
18     namespace fwlite {
19     class EventSelector : public TNamed {
20     public:
21     EventSelector(const char *name="", const char *title="") : TNamed(name,title) {}
22     virtual ~EventSelector() {}
23     virtual bool accept(const fwlite::EventBase &ev) const = 0;
24     };
25    
26     class RunLumiSelector : public EventSelector {
27     public:
28     RunLumiSelector(const char *name="", const char *title="") : EventSelector(name,title) {}
29     RunLumiSelector(int run, int firstLumi=0, int lastLumi = 9999999) :
30     EventSelector(TString::Format("run%d_lumi%d_%d", run, firstLumi, lastLumi),
31     TString::Format("Run %d, Lumi range [%d, %d]", run, firstLumi, lastLumi))
32     { add(run, firstLumi, lastLumi); }
33    
34     virtual ~RunLumiSelector() {}
35     virtual bool accept(const fwlite::EventBase &ev) const {
36     return accept(ev.id().run(), ev.luminosityBlock());
37     }
38     void add(int run, int firstLumi=0, int lastLumi = 9999999) {
39     runs.push_back(run);
40     firstLumis.push_back(firstLumi);
41     lastLumis.push_back(lastLumi);
42     }
43     void clear() {
44     runs.clear();
45     firstLumis.clear();
46     lastLumis.clear();
47     }
48     bool accept(int run, int luminosityBlock) const {
49     if (runs.empty()) return true;
50     for (int i = 0, n = runs.size(); i < n; ++i) {
51     if (runs[i] == run) {
52     if ((firstLumis[i] <= luminosityBlock) && (luminosityBlock <= lastLumis[i])) return true;
53     }
54     }
55     return false;
56     }
57    
58     private:
59     std::vector<int> runs, firstLumis, lastLumis;
60     };
61    
62     template<typename T>
63     class ObjectCountSelector : public EventSelector {
64     public:
65     ObjectCountSelector(const char *label, const char *instance, const char *process,
66     const char *cut, int minNumber=1, int maxNumber=-1) :
67     label_(label), instance_(instance),
68     min_(minNumber), max_(maxNumber),
69     scanner(new helper::ScannerBase(helper::Parser::elementType(Reflex::Type::ByTypeInfo(HandleT::TempWrapT::typeInfo()))))
70     {
71     scanner->setCut(cut);
72     }
73     ~ObjectCountSelector() { delete scanner; }
74     virtual bool accept(const fwlite::EventBase &ev) const {
75     int nfound = 0;
76     HandleT handle; // here, not as a datamember, otherwise CINT segfaults (!?)
77     handle.getByLabel(ev, label_.c_str(), instance_.c_str(), process_.c_str());
78     const std::vector<T> & vals = *handle;
79     for (size_t j = 0, n = vals.size(); j < n; ++j) {
80     if (scanner->test(&vals[j])) nfound++;
81     }
82     return (nfound >= min_ && (max_ == -1 || nfound <= max_));
83     }
84     void setCut(const char *cut) { scanner->setCut(cut); }
85     void setMin(int minNumber) { min_ = minNumber; }
86     void setMax(int maxNumber) { max_ = maxNumber; }
87     private:
88     typedef fwlite::Handle<std::vector<T> > HandleT;
89     std::string label_, instance_, process_;
90     int min_, max_;
91     helper::ScannerBase *scanner; // has to be a pointer, otherwise CINT segfaults in setCut (!?)
92     // prevent copy c-tor and assignment
93     ObjectCountSelector(const fwlite::ObjectCountSelector<T> &other) ;
94     ObjectCountSelector & operator=(const fwlite::ObjectCountSelector<T> &other) ;
95     };
96     }