5 |
|
#include <TString.h> |
6 |
|
#include <TObjString.h> |
7 |
|
#include <TObjArray.h> |
8 |
+ |
#include <TEnv.h> |
9 |
|
|
10 |
|
#if !defined(__CINT__) && !defined(__MAKECINT__) |
11 |
|
#include "UserCode/GPetrucc/interface/fwliteHelpers.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 |
+ |
#include "UserCode/GPetrucc/interface/fwlite/EventSelectors.h" |
21 |
|
|
22 |
|
namespace fwlite { |
23 |
|
template<typename T> |
25 |
|
public: |
26 |
|
typedef fwlite::Handle<std::vector<T> > HandleT; |
27 |
|
Scanner(fwlite::EventBase *ev, const char *label, const char *instance = "", const char *process="") : |
28 |
< |
//helper::ScannerBase(Reflex::Type::ByTypeInfo(typeid(T))), |
29 |
< |
event_(ev), label_(label), instance_(instance), printFullEventId_(ev->isRealData()) |
28 |
> |
event_(ev), label_(label), instance_(instance), |
29 |
> |
printFullEventId_(ev->isRealData()), |
30 |
> |
ignoreExceptions_(false), |
31 |
> |
exprSep_(":") |
32 |
|
{ |
33 |
< |
Reflex::Type wrapperType = Reflex::Type::ByTypeInfo(HandleT::TempWrapT::typeInfo()); |
34 |
< |
objType = helpme.elementType(Reflex::Type::ByTypeInfo(HandleT::TempWrapT::typeInfo())); |
33 |
> |
objType = helper::Parser::elementType(Reflex::Type::ByTypeInfo(HandleT::TempWrapT::typeInfo())); |
34 |
> |
eventSelectors_.SetOwner(); |
35 |
|
} |
36 |
|
|
37 |
|
|
38 |
|
void scan(const char *exprs, const char *cut="", int nmax=20) { |
39 |
|
helper::ScannerBase scanner(objType); |
40 |
+ |
scanner.setIgnoreExceptions(ignoreExceptions_); |
41 |
|
|
42 |
< |
TObjArray *exprArray = TString(exprs).Tokenize(":"); |
42 |
> |
TObjArray *exprArray = TString(exprs).Tokenize(exprSep_); |
43 |
|
int rowline = 0; |
44 |
|
if (printFullEventId_) { |
45 |
|
printf(" : %9s : %4s : %9s : %3s", "RUN", "LUMI", "EVENT", "#IT"); |
51 |
|
for (int i = 0; i < exprArray->GetEntries(); ++i) { |
52 |
|
const char *ex = ((TObjString *)(*exprArray)[i])->GetString(); |
53 |
|
scanner.addExpression(ex); |
54 |
< |
printf(" : %8.8s", ex); |
54 |
> |
printf(" : %8s", (strlen(ex)>8 ? ex+strlen(ex)-8 : ex)); // the rightmost part is usually the more interesting one |
55 |
|
rowline += 3+8; |
56 |
|
} |
57 |
|
std::cout << " :" << std::endl; |
64 |
|
if (strlen(cut)) scanner.setCut(cut); |
65 |
|
|
66 |
|
int iev = 0; |
67 |
< |
for (event_->toBegin(); (iev < nmax) && !event_->atEnd(); ++iev, ++(*event_)) { |
67 |
> |
for (event_->toBegin(); (iev != nmax) && !event_->atEnd(); ++iev, ++(*event_)) { |
68 |
> |
if (!selectEvent(*event_)) continue; |
69 |
|
handle_.getByLabel(*event_, label_.c_str(), instance_.c_str(), process_.c_str()); |
70 |
|
const std::vector<T> & vals = *handle_; |
71 |
|
for (size_t j = 0, n = vals.size(); j < n; ++j) { |
82 |
|
} |
83 |
|
std::cout << std::endl; |
84 |
|
} |
85 |
+ |
|
86 |
+ |
|
87 |
+ |
TH1 * draw(const char *expr, const char *cut = "", TString drawopt = "", TH1 *hist = 0) { |
88 |
+ |
// prep the machinery |
89 |
+ |
helper::ScannerBase scanner(objType); |
90 |
+ |
scanner.setIgnoreExceptions(ignoreExceptions_); |
91 |
+ |
scanner.addExpression(expr); |
92 |
+ |
if (strlen(cut)) scanner.setCut(cut); |
93 |
+ |
|
94 |
+ |
// make histo, if needed |
95 |
+ |
if (hist == 0) { |
96 |
+ |
hist = new TH1F("htemp", |
97 |
+ |
(strlen(cut) ? TString(expr)+"{"+cut+"}" : TString(expr)), |
98 |
+ |
gEnv->GetValue("Hist.Binning.1D.x",100), 0, 0); |
99 |
+ |
hist->SetBit(TH1::kCanRebin); |
100 |
+ |
} |
101 |
+ |
|
102 |
+ |
// fill histogram |
103 |
+ |
for (event_->toBegin(); !event_->atEnd(); ++(*event_)) { |
104 |
+ |
if (!selectEvent(*event_)) continue; |
105 |
+ |
handle_.getByLabel(*event_, label_.c_str(), instance_.c_str(), process_.c_str()); |
106 |
+ |
const std::vector<T> & vals = *handle_; |
107 |
+ |
for (size_t j = 0, n = vals.size(); j < n; ++j) { |
108 |
+ |
scanner.fill1D(&vals[j], hist); |
109 |
+ |
} |
110 |
+ |
} |
111 |
+ |
|
112 |
+ |
if (!TString(drawopt).Contains("goff",TString::kIgnoreCase)) hist->Draw(drawopt); |
113 |
+ |
return hist; |
114 |
+ |
} |
115 |
+ |
TH1 * draw(const char *expr, int bins, double xlow, double xhigh, const char *cut = "", const char *drawopt = "") { |
116 |
+ |
TH1 * htemp = new TH1F("htemp", (strlen(cut) ? TString(expr)+"{"+cut+"}" : TString(expr)), bins, xlow, xhigh); |
117 |
+ |
return draw(expr,cut,drawopt,htemp); |
118 |
+ |
} |
119 |
+ |
|
120 |
+ |
TProfile * drawProf(TString xexpr, TString yexpr, const char *cut = "", TString drawopt = "", TProfile *hist = 0) { |
121 |
+ |
// prep the machinery |
122 |
+ |
helper::ScannerBase scanner(objType); |
123 |
+ |
scanner.setIgnoreExceptions(ignoreExceptions_); |
124 |
+ |
scanner.addExpression(xexpr); |
125 |
+ |
scanner.addExpression(yexpr); |
126 |
+ |
if (strlen(cut)) scanner.setCut(cut); |
127 |
+ |
|
128 |
+ |
// make histo, if needed |
129 |
+ |
if (hist == 0) { |
130 |
+ |
hist = new TProfile("htemp", |
131 |
+ |
(strlen(cut) ? yexpr+":"+xexpr+"{"+cut+"}" : yexpr+":"+xexpr), |
132 |
+ |
gEnv->GetValue("Hist.Binning.1D.x",100), 0., 0.); |
133 |
+ |
hist->SetBit(TH1::kCanRebin); |
134 |
+ |
} |
135 |
+ |
|
136 |
+ |
// fill histogram |
137 |
+ |
for (event_->toBegin(); !event_->atEnd(); ++(*event_)) { |
138 |
+ |
if (!selectEvent(*event_)) continue; |
139 |
+ |
handle_.getByLabel(*event_, label_.c_str(), instance_.c_str(), process_.c_str()); |
140 |
+ |
const std::vector<T> & vals = *handle_; |
141 |
+ |
for (size_t j = 0, n = vals.size(); j < n; ++j) { |
142 |
+ |
scanner.fillProf(&vals[j], hist); |
143 |
+ |
} |
144 |
+ |
} |
145 |
+ |
|
146 |
+ |
if (!TString(drawopt).Contains("goff",TString::kIgnoreCase)) hist->Draw(drawopt); |
147 |
+ |
return hist; |
148 |
+ |
} |
149 |
+ |
TProfile * drawProf(TString xexpr, int bins, double xlow, double xhigh, TString yexpr, const char *cut = "", const char *drawopt = "") { |
150 |
+ |
TProfile * htemp = new TProfile("htemp", (strlen(cut) ? yexpr+":"+xexpr+"{"+cut+"}" : yexpr+":"+xexpr), bins, xlow, xhigh); |
151 |
+ |
return drawProf(xexpr,yexpr,cut,drawopt,htemp); |
152 |
+ |
} |
153 |
+ |
|
154 |
+ |
TH2 * draw2D(TString xexpr, TString yexpr, const char *cut = "", TString drawopt = "", TH2 *hist = 0) { |
155 |
+ |
// prep the machinery |
156 |
+ |
helper::ScannerBase scanner(objType); |
157 |
+ |
scanner.setIgnoreExceptions(ignoreExceptions_); |
158 |
+ |
scanner.addExpression(xexpr); |
159 |
+ |
scanner.addExpression(yexpr); |
160 |
+ |
if (strlen(cut)) scanner.setCut(cut); |
161 |
+ |
|
162 |
+ |
// make histo, if needed |
163 |
+ |
if (hist == 0) { |
164 |
+ |
hist = new TH2F("htemp", |
165 |
+ |
(strlen(cut) ? yexpr+":"+xexpr+"{"+cut+"}" : yexpr+":"+xexpr), |
166 |
+ |
gEnv->GetValue("Hist.Binning.2D.x",20), 0, 0, |
167 |
+ |
gEnv->GetValue("Hist.Binning.2D.y",20), 0, 0); |
168 |
+ |
hist->SetBit(TH1::kCanRebin); |
169 |
+ |
} |
170 |
+ |
|
171 |
+ |
// fill histogram |
172 |
+ |
for (event_->toBegin(); !event_->atEnd(); ++(*event_)) { |
173 |
+ |
if (!selectEvent(*event_)) continue; |
174 |
+ |
handle_.getByLabel(*event_, label_.c_str(), instance_.c_str(), process_.c_str()); |
175 |
+ |
const std::vector<T> & vals = *handle_; |
176 |
+ |
for (size_t j = 0, n = vals.size(); j < n; ++j) { |
177 |
+ |
scanner.fill2D(&vals[j], hist); |
178 |
+ |
} |
179 |
+ |
} |
180 |
+ |
|
181 |
+ |
if (!strlen(hist->GetXaxis()->GetTitle())) hist->GetXaxis()->SetTitle(xexpr); |
182 |
+ |
if (!strlen(hist->GetYaxis()->GetTitle())) hist->GetYaxis()->SetTitle(yexpr); |
183 |
+ |
if (!TString(drawopt).Contains("goff",TString::kIgnoreCase)) hist->Draw(drawopt); |
184 |
+ |
return hist; |
185 |
+ |
} |
186 |
+ |
TH2 * draw2D(TString xexpr, int xbins, double xlow, double xhigh, |
187 |
+ |
TString yexpr, int ybins, double ylow, double yhigh, |
188 |
+ |
const char *cut = "", const char *drawopt = "") { |
189 |
+ |
TH2 * htemp = new TH2F("htemp", (strlen(cut) ? yexpr+":"+xexpr+"{"+cut+"}" : yexpr+":"+xexpr), |
190 |
+ |
xbins, xlow, xhigh, ybins,ylow,yhigh); |
191 |
+ |
return draw2D(xexpr,yexpr,cut,drawopt,htemp); |
192 |
+ |
} |
193 |
+ |
|
194 |
|
void setPrintFullEventId(bool printIt=true) { printFullEventId_ = printIt; } |
195 |
+ |
void setExpressionSeparator(TString separator) { exprSep_ = separator; } |
196 |
+ |
void setIgnoreExceptions(bool ignoreThem) { ignoreExceptions_ = ignoreThem; } |
197 |
+ |
|
198 |
+ |
void addEventSelector(fwlite::EventSelector *selector) { eventSelectors_.Add(selector); } |
199 |
+ |
void clearEventSelector() { eventSelectors_.Clear(); } |
200 |
+ |
TObjArray & eventSelectors() { return eventSelectors_; } |
201 |
+ |
bool selectEvent(const fwlite::EventBase &ev) const { |
202 |
+ |
for (int i = 0, n = eventSelectors_.GetEntries(); i < n; ++i) { |
203 |
+ |
if (!((fwlite::EventSelector *)(eventSelectors_[i]))->accept(ev)) return false; |
204 |
+ |
} |
205 |
+ |
return true; |
206 |
+ |
} |
207 |
|
private: |
208 |
|
fwlite::EventBase *event_; |
209 |
|
std::string label_, instance_, process_; |
210 |
|
bool printFullEventId_; |
211 |
+ |
bool ignoreExceptions_; |
212 |
+ |
TString exprSep_; |
213 |
|
HandleT handle_; |
214 |
< |
helper::Parser helpme; |
215 |
< |
Reflex::Type objType; |
214 |
> |
Reflex::Type objType; |
215 |
> |
|
216 |
> |
TObjArray eventSelectors_; |
217 |
|
|
218 |
|
}; |
219 |
|
} |