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 |
#include "FWCore/Common/interface/TriggerNames.h"
|
11 |
#include "DataFormats/Common/interface/TriggerResults.h"
|
12 |
#else
|
13 |
#ifndef UserCode_GPetrucc_CINT_load_library
|
14 |
#define UserCode_GPetrucc_CINT_load_library
|
15 |
// load the library that contains the dictionaries
|
16 |
int _load_UserCodeGPetrucc = gSystem->Load("libUserCodeGPetrucc.so");
|
17 |
#endif
|
18 |
#endif
|
19 |
|
20 |
namespace fwlite {
|
21 |
class EventSelector : public TNamed {
|
22 |
public:
|
23 |
EventSelector(const char *name="", const char *title="") : TNamed(name,title) {}
|
24 |
virtual ~EventSelector() {}
|
25 |
virtual bool accept(const fwlite::EventBase &ev) const = 0;
|
26 |
};
|
27 |
|
28 |
class RunLumiSelector : public EventSelector {
|
29 |
public:
|
30 |
RunLumiSelector(const char *name="", const char *title="") : EventSelector(name,title) {}
|
31 |
RunLumiSelector(int run, int firstLumi=0, int lastLumi = 9999999) :
|
32 |
EventSelector(TString::Format("run%d_lumi%d_%d", run, firstLumi, lastLumi),
|
33 |
TString::Format("Run %d, Lumi range [%d, %d]", run, firstLumi, lastLumi))
|
34 |
{ add(run, firstLumi, lastLumi); }
|
35 |
|
36 |
virtual ~RunLumiSelector() {}
|
37 |
virtual bool accept(const fwlite::EventBase &ev) const {
|
38 |
return accept(ev.id().run(), ev.luminosityBlock());
|
39 |
}
|
40 |
void add(int run, int firstLumi=0, int lastLumi = 9999999) {
|
41 |
runs.push_back(run);
|
42 |
firstLumis.push_back(firstLumi);
|
43 |
lastLumis.push_back(lastLumi);
|
44 |
}
|
45 |
void clear() {
|
46 |
runs.clear();
|
47 |
firstLumis.clear();
|
48 |
lastLumis.clear();
|
49 |
}
|
50 |
bool accept(int run, int luminosityBlock) const {
|
51 |
if (runs.empty()) return true;
|
52 |
for (int i = 0, n = runs.size(); i < n; ++i) {
|
53 |
if (runs[i] == run) {
|
54 |
if ((firstLumis[i] <= luminosityBlock) && (luminosityBlock <= lastLumis[i])) return true;
|
55 |
}
|
56 |
}
|
57 |
return false;
|
58 |
}
|
59 |
|
60 |
private:
|
61 |
std::vector<int> runs, firstLumis, lastLumis;
|
62 |
};
|
63 |
|
64 |
template<typename T>
|
65 |
class ObjectCountSelector : public EventSelector {
|
66 |
public:
|
67 |
ObjectCountSelector(const char *label, const char *instance, const char *process,
|
68 |
const char *cut, int minNumber=1, int maxNumber=-1) :
|
69 |
label_(label), instance_(instance),
|
70 |
min_(minNumber), max_(maxNumber),
|
71 |
scanner(new helper::ScannerBase(helper::Parser::elementType(Reflex::Type::ByTypeInfo(HandleT::TempWrapT::typeInfo()))))
|
72 |
{
|
73 |
scanner->setCut(cut);
|
74 |
scanner->setIgnoreExceptions(true);
|
75 |
}
|
76 |
~ObjectCountSelector() { delete scanner; }
|
77 |
virtual bool accept(const fwlite::EventBase &ev) const {
|
78 |
int nfound = 0;
|
79 |
HandleT handle; // here, not as a datamember, otherwise CINT segfaults (!?)
|
80 |
handle.getByLabel(ev, label_.c_str(), instance_.c_str(), process_.c_str());
|
81 |
const std::vector<T> & vals = *handle;
|
82 |
for (size_t j = 0, n = vals.size(); j < n; ++j) {
|
83 |
if (scanner->test(&vals[j])) nfound++;
|
84 |
}
|
85 |
return (nfound >= min_ && (max_ == -1 || nfound <= max_));
|
86 |
}
|
87 |
void setCut(const char *cut) { scanner->setCut(cut); }
|
88 |
void setMin(int minNumber) { min_ = minNumber; }
|
89 |
void setMax(int maxNumber) { max_ = maxNumber; }
|
90 |
void setIgnoreExceptions(bool ignoreThem=true) { scanner->setIgnoreExceptions(ignoreThem); }
|
91 |
protected:
|
92 |
typedef fwlite::Handle<std::vector<T> > HandleT;
|
93 |
std::string label_, instance_, process_;
|
94 |
int min_, max_;
|
95 |
helper::ScannerBase *scanner; // has to be a pointer, otherwise CINT segfaults in setCut (!?)
|
96 |
// prevent copy c-tor and assignment
|
97 |
ObjectCountSelector(const fwlite::ObjectCountSelector<T> &other) ;
|
98 |
ObjectCountSelector & operator=(const fwlite::ObjectCountSelector<T> &other) ;
|
99 |
};
|
100 |
|
101 |
class TriggerSelector : public EventSelector {
|
102 |
public:
|
103 |
TriggerSelector(const char *process) : process_(process), retvalue(true), ignoreMissingPaths(false) {}
|
104 |
virtual bool accept(const fwlite::EventBase &ev) const {
|
105 |
fwlite::Handle<edm::TriggerResults> hTriggerResults;
|
106 |
hTriggerResults.getByLabel(ev,"TriggerResults","",process_.c_str());
|
107 |
edm::TriggerNames const& triggerNames = ev.triggerNames(*hTriggerResults);
|
108 |
int notfound = triggerNames.size();
|
109 |
for (size_t i = 0; i < triggers.size(); ++i) {
|
110 |
int idx = triggerNames.triggerIndex(triggers[i]);
|
111 |
if (idx != notfound) {
|
112 |
if (hTriggerResults->accept(idx)) return retvalue;
|
113 |
} else if (!ignoreMissingPaths) {
|
114 |
std::cerr << "Trigger " << triggers[i] << " not found. Remove it or call setIgnoreMissingPaths(true)." << std::endl;
|
115 |
return false;
|
116 |
}
|
117 |
}
|
118 |
return !retvalue;
|
119 |
}
|
120 |
void setIgnoreMissingPaths(bool ignoreThem=true) { ignoreMissingPaths = ignoreThem; }
|
121 |
void addTrigger(const std::string &trigger) { triggers.push_back(trigger); }
|
122 |
void clearTriggers() { triggers.clear(); }
|
123 |
void setInvert(bool invert) { retvalue = !invert; }
|
124 |
protected:
|
125 |
std::string process_;
|
126 |
bool retvalue, ignoreMissingPaths;
|
127 |
std::vector<std::string> triggers;
|
128 |
};
|
129 |
}
|