5 |
|
#include <TString.h> |
6 |
|
#include <TObjString.h> |
7 |
|
#include <TObjArray.h> |
8 |
+ |
#include <TDirectory.h> |
9 |
+ |
#include <TEnv.h> |
10 |
+ |
#include <TClass.h> |
11 |
|
|
12 |
|
#if !defined(__CINT__) && !defined(__MAKECINT__) |
13 |
|
#include "UserCode/GPetrucc/interface/fwliteHelpers.h" |
14 |
|
#else |
15 |
+ |
#ifndef UserCode_GPetrucc_CINT_load_library |
16 |
+ |
#define UserCode_GPetrucc_CINT_load_library |
17 |
|
// load the library that contains the dictionaries |
18 |
|
int _load_UserCodeGPetrucc = gSystem->Load("libUserCodeGPetrucc.so"); |
19 |
|
#endif |
20 |
+ |
#endif |
21 |
+ |
|
22 |
+ |
#include "UserCode/GPetrucc/interface/fwlite/EventSelectors.h" |
23 |
|
|
24 |
|
namespace fwlite { |
25 |
|
template<typename T> |
27 |
|
public: |
28 |
|
typedef fwlite::Handle<std::vector<T> > HandleT; |
29 |
|
Scanner(fwlite::EventBase *ev, const char *label, const char *instance = "", const char *process="") : |
30 |
< |
//helper::ScannerBase(Reflex::Type::ByTypeInfo(typeid(T))), |
31 |
< |
event_(ev), label_(label), instance_(instance), printFullEventId_(ev->isRealData()) |
30 |
> |
event_(ev), label_(label), instance_(instance), |
31 |
> |
printFullEventId_(ev->isRealData()), |
32 |
> |
ignoreExceptions_(false), |
33 |
> |
exprSep_(":"), |
34 |
> |
maxEntries_(-1), |
35 |
> |
maxLinesToPrint_(50) |
36 |
|
{ |
37 |
< |
Reflex::Type wrapperType = Reflex::Type::ByTypeInfo(HandleT::TempWrapT::typeInfo()); |
38 |
< |
objType = helpme.elementType(Reflex::Type::ByTypeInfo(HandleT::TempWrapT::typeInfo())); |
37 |
> |
objType = helper::Parser::elementType(Reflex::Type::ByTypeInfo(HandleT::TempWrapT::typeInfo())); |
38 |
> |
//eventSelectors_.SetOwner(); // better a few leaks for lazy users than a few crashes for unweary ones |
39 |
|
} |
40 |
|
|
41 |
|
|
42 |
< |
void scan(const char *exprs, const char *cut="", int nmax=20) { |
42 |
> |
void scan(const char *exprs, const char *cut="", int nmax=-1) { |
43 |
|
helper::ScannerBase scanner(objType); |
44 |
+ |
scanner.setIgnoreExceptions(ignoreExceptions_); |
45 |
|
|
46 |
< |
TObjArray *exprArray = TString(exprs).Tokenize(":"); |
46 |
> |
TObjArray *exprArray = TString(exprs).Tokenize(exprSep_); |
47 |
|
int rowline = 0; |
48 |
|
if (printFullEventId_) { |
49 |
|
printf(" : %9s : %4s : %9s : %3s", "RUN", "LUMI", "EVENT", "#IT"); |
53 |
|
rowline += 3+6+3+3-1; // -1 as first char remain blank |
54 |
|
} |
55 |
|
for (int i = 0; i < exprArray->GetEntries(); ++i) { |
56 |
< |
const char *ex = ((TObjString *)(*exprArray)[i])->GetString(); |
57 |
< |
scanner.addExpression(ex); |
58 |
< |
printf(" : %8.8s", ex); |
56 |
> |
TString str = ((TObjString *)(*exprArray)[i])->GetString(); |
57 |
> |
std::string lb = str.Data(); |
58 |
> |
std::string ex = str.Data(); |
59 |
> |
if ((ex[0] == '@') && (ex.find('=') != std::string::npos)) { |
60 |
> |
lb = lb.substr(1,ex.find('=')-1); |
61 |
> |
ex = ex.substr(ex.find('=')+1); |
62 |
> |
} |
63 |
> |
scanner.addExpression(ex.c_str()); |
64 |
> |
printf(" : %8s", (lb.size()>8 ? lb.substr(lb.size()-8) : lb).c_str()); // the rightmost part is usually the more interesting one |
65 |
|
rowline += 3+8; |
66 |
|
} |
67 |
|
std::cout << " :" << std::endl; |
73 |
|
|
74 |
|
if (strlen(cut)) scanner.setCut(cut); |
75 |
|
|
76 |
< |
int iev = 0; |
77 |
< |
for (event_->toBegin(); (iev < nmax) && !event_->atEnd(); ++iev, ++(*event_)) { |
76 |
> |
int iev = 0, line = 0; |
77 |
> |
for (event_->toBegin(); (iev != nmax) && !event_->atEnd(); ++iev, ++(*event_)) { |
78 |
> |
if (!selectEvent(*event_)) continue; |
79 |
|
handle_.getByLabel(*event_, label_.c_str(), instance_.c_str(), process_.c_str()); |
80 |
+ |
if (handle_.failedToGet()) { |
81 |
+ |
if (ignoreExceptions_) continue; |
82 |
+ |
} |
83 |
|
const std::vector<T> & vals = *handle_; |
84 |
|
for (size_t j = 0, n = vals.size(); j < n; ++j) { |
85 |
|
if (!scanner.test(&vals[j])) continue; |
91 |
|
} |
92 |
|
scanner.print(&vals[j]); |
93 |
|
std::cout << " :" << std::endl; |
94 |
+ |
if (++line == maxLinesToPrint_) { |
95 |
+ |
line = 0; |
96 |
+ |
if (!wantMore()) { |
97 |
+ |
iev = nmax-1; // this is to exit the outer loop |
98 |
+ |
break; // and this to exit the inner one |
99 |
+ |
} |
100 |
+ |
} |
101 |
|
} |
102 |
|
} |
103 |
|
std::cout << std::endl; |
104 |
|
} |
105 |
+ |
|
106 |
+ |
size_t count(const char *cut) { |
107 |
+ |
helper::ScannerBase scanner(objType); |
108 |
+ |
scanner.setIgnoreExceptions(ignoreExceptions_); |
109 |
+ |
|
110 |
+ |
scanner.setCut(cut); |
111 |
+ |
|
112 |
+ |
size_t npass = 0; |
113 |
+ |
int iev = 0; |
114 |
+ |
for (event_->toBegin(); !event_->atEnd(); ++(*event_), ++iev) { |
115 |
+ |
if (maxEntries_ > -1 && iev > maxEntries_) break; |
116 |
+ |
if (!selectEvent(*event_)) continue; |
117 |
+ |
handle_.getByLabel(*event_, label_.c_str(), instance_.c_str(), process_.c_str()); |
118 |
+ |
const std::vector<T> & vals = *handle_; |
119 |
+ |
for (size_t j = 0, n = vals.size(); j < n; ++j) { |
120 |
+ |
if (scanner.test(&vals[j])) npass++; |
121 |
+ |
} |
122 |
+ |
} |
123 |
+ |
|
124 |
+ |
return npass; |
125 |
+ |
} |
126 |
+ |
|
127 |
+ |
TH1 * draw(const char *expr, const char *cut = "", TString drawopt = "", TH1 *hist = 0) { |
128 |
+ |
// prep the machinery |
129 |
+ |
helper::ScannerBase scanner(objType); |
130 |
+ |
scanner.setIgnoreExceptions(ignoreExceptions_); |
131 |
+ |
if (!scanner.addExpression(expr)) return 0; |
132 |
+ |
if (strlen(cut)) scanner.setCut(cut); |
133 |
+ |
|
134 |
+ |
// make histo, if needed |
135 |
+ |
if (hist == 0) { |
136 |
+ |
if (TString(drawopt).Contains("same",TString::kIgnoreCase) && |
137 |
+ |
gDirectory && gDirectory->Get("htemp") != 0 && |
138 |
+ |
gDirectory->Get("htemp")->IsA()->InheritsFrom(TH1::Class())) { |
139 |
+ |
hist = (TH1*) gDirectory->Get("htemp")->Clone(); |
140 |
+ |
hist->Reset(); |
141 |
+ |
} else { |
142 |
+ |
htempDelete(); |
143 |
+ |
hist = new TH1F("htemp", |
144 |
+ |
(strlen(cut) ? TString(expr)+"{"+cut+"}" : TString(expr)), |
145 |
+ |
gEnv->GetValue("Hist.Binning.1D.x",100), 0, 0); |
146 |
+ |
hist->SetBit(TH1::kCanRebin); |
147 |
+ |
} |
148 |
+ |
} |
149 |
+ |
|
150 |
+ |
// fill histogram |
151 |
+ |
int iev = 0; |
152 |
+ |
for (event_->toBegin(); !event_->atEnd(); ++(*event_), ++iev) { |
153 |
+ |
if (maxEntries_ > -1 && iev > maxEntries_) break; |
154 |
+ |
if (!selectEvent(*event_)) continue; |
155 |
+ |
handle_.getByLabel(*event_, label_.c_str(), instance_.c_str(), process_.c_str()); |
156 |
+ |
const std::vector<T> & vals = *handle_; |
157 |
+ |
for (size_t j = 0, n = vals.size(); j < n; ++j) { |
158 |
+ |
scanner.fill1D(&vals[j], hist); |
159 |
+ |
} |
160 |
+ |
} |
161 |
+ |
|
162 |
+ |
if (!TString(drawopt).Contains("goff",TString::kIgnoreCase)) hist->Draw(drawopt); |
163 |
+ |
return hist; |
164 |
+ |
} |
165 |
+ |
TH1 * draw(const char *expr, int bins, double xlow, double xhigh, const char *cut = "", const char *drawopt = "") { |
166 |
+ |
htempDelete(); |
167 |
+ |
TH1 * htemp = new TH1F("htemp", (strlen(cut) ? TString(expr)+"{"+cut+"}" : TString(expr)), bins, xlow, xhigh); |
168 |
+ |
return draw(expr,cut,drawopt,htemp); |
169 |
+ |
} |
170 |
+ |
|
171 |
+ |
TProfile * drawProf(TString xexpr, TString yexpr, const char *cut = "", TString drawopt = "", TProfile *hist = 0) { |
172 |
+ |
// prep the machinery |
173 |
+ |
helper::ScannerBase scanner(objType); |
174 |
+ |
scanner.setIgnoreExceptions(ignoreExceptions_); |
175 |
+ |
if (!scanner.addExpression(xexpr.Data())) return 0; |
176 |
+ |
if (!scanner.addExpression(yexpr.Data())) return 0; |
177 |
+ |
if (strlen(cut)) scanner.setCut(cut); |
178 |
+ |
|
179 |
+ |
// make histo, if needed |
180 |
+ |
if (hist == 0) { |
181 |
+ |
htempDelete(); |
182 |
+ |
hist = new TProfile("htemp", |
183 |
+ |
(strlen(cut) ? yexpr+":"+xexpr+"{"+cut+"}" : yexpr+":"+xexpr), |
184 |
+ |
gEnv->GetValue("Hist.Binning.1D.x",100), 0., 0.); |
185 |
+ |
hist->SetBit(TH1::kCanRebin); |
186 |
+ |
} |
187 |
+ |
|
188 |
+ |
// fill histogram |
189 |
+ |
int iev = 0; |
190 |
+ |
for (event_->toBegin(); !event_->atEnd(); ++(*event_), ++iev) { |
191 |
+ |
if (maxEntries_ > -1 && iev > maxEntries_) break; |
192 |
+ |
if (!selectEvent(*event_)) continue; |
193 |
+ |
handle_.getByLabel(*event_, label_.c_str(), instance_.c_str(), process_.c_str()); |
194 |
+ |
const std::vector<T> & vals = *handle_; |
195 |
+ |
for (size_t j = 0, n = vals.size(); j < n; ++j) { |
196 |
+ |
scanner.fillProf(&vals[j], hist); |
197 |
+ |
} |
198 |
+ |
} |
199 |
+ |
|
200 |
+ |
if (!TString(drawopt).Contains("goff",TString::kIgnoreCase)) hist->Draw(drawopt); |
201 |
+ |
return hist; |
202 |
+ |
} |
203 |
+ |
TProfile * drawProf(TString xexpr, int bins, double xlow, double xhigh, TString yexpr, const char *cut = "", const char *drawopt = "") { |
204 |
+ |
htempDelete(); |
205 |
+ |
TProfile * htemp = new TProfile("htemp", (strlen(cut) ? yexpr+":"+xexpr+"{"+cut+"}" : yexpr+":"+xexpr), bins, xlow, xhigh); |
206 |
+ |
return drawProf(xexpr,yexpr,cut,drawopt,htemp); |
207 |
+ |
} |
208 |
+ |
|
209 |
+ |
TH2 * draw2D(TString xexpr, TString yexpr, const char *cut = "", TString drawopt = "", TH2 *hist = 0) { |
210 |
+ |
// prep the machinery |
211 |
+ |
helper::ScannerBase scanner(objType); |
212 |
+ |
scanner.setIgnoreExceptions(ignoreExceptions_); |
213 |
+ |
if (!scanner.addExpression((const char *)xexpr)) return 0; |
214 |
+ |
if (!scanner.addExpression((const char *)yexpr)) return 0; |
215 |
+ |
if (strlen(cut)) scanner.setCut(cut); |
216 |
+ |
|
217 |
+ |
int iev = 0; |
218 |
+ |
// make histo, if needed |
219 |
+ |
if (hist == 0) { |
220 |
+ |
// ok this is much more a hack than for the 1D case |
221 |
+ |
double xmin = 0, xmax = -1, ymin = 0, ymax = -1; |
222 |
+ |
for (event_->toBegin(), iev = 0; !event_->atEnd(); ++(*event_), ++iev) { |
223 |
+ |
if (maxEntries_ > -1 && iev > maxEntries_) break; |
224 |
+ |
if (!selectEvent(*event_)) continue; |
225 |
+ |
handle_.getByLabel(*event_, label_.c_str(), instance_.c_str(), process_.c_str()); |
226 |
+ |
const std::vector<T> & vals = *handle_; |
227 |
+ |
for (size_t j = 0, n = vals.size(); j < n; ++j) { |
228 |
+ |
if (!scanner.test(&vals[j])) continue; |
229 |
+ |
double x = scanner.eval(&vals[j],0); |
230 |
+ |
double y = scanner.eval(&vals[j],1); |
231 |
+ |
if ((xmax == -1) || (x >= xmax)) xmax = x; |
232 |
+ |
if ((xmin == 0) || (x <= xmin)) xmin = x; |
233 |
+ |
if ((ymax == -1) || (y >= ymax)) ymax = y; |
234 |
+ |
if ((ymin == 0) || (y <= ymin)) ymin = y; |
235 |
+ |
} |
236 |
+ |
} |
237 |
+ |
htempDelete(); |
238 |
+ |
hist = new TH2F("htemp", |
239 |
+ |
(strlen(cut) ? yexpr+":"+xexpr+"{"+cut+"}" : yexpr+":"+xexpr), |
240 |
+ |
gEnv->GetValue("Hist.Binning.2D.x",20), xmin, xmax, |
241 |
+ |
gEnv->GetValue("Hist.Binning.2D.y",20), ymin, ymax); |
242 |
+ |
} |
243 |
+ |
|
244 |
+ |
// fill histogram |
245 |
+ |
for (event_->toBegin(), iev = 0; !event_->atEnd(); ++(*event_), ++iev) { |
246 |
+ |
if (maxEntries_ > -1 && iev > maxEntries_) break; |
247 |
+ |
if (!selectEvent(*event_)) continue; |
248 |
+ |
handle_.getByLabel(*event_, label_.c_str(), instance_.c_str(), process_.c_str()); |
249 |
+ |
const std::vector<T> & vals = *handle_; |
250 |
+ |
for (size_t j = 0, n = vals.size(); j < n; ++j) { |
251 |
+ |
scanner.fill2D(&vals[j], hist); |
252 |
+ |
} |
253 |
+ |
} |
254 |
+ |
|
255 |
+ |
if (!strlen(hist->GetXaxis()->GetTitle())) hist->GetXaxis()->SetTitle(xexpr); |
256 |
+ |
if (!strlen(hist->GetYaxis()->GetTitle())) hist->GetYaxis()->SetTitle(yexpr); |
257 |
+ |
if (!TString(drawopt).Contains("goff",TString::kIgnoreCase)) hist->Draw(drawopt); |
258 |
+ |
return hist; |
259 |
+ |
} |
260 |
+ |
TH2 * draw2D(TString xexpr, int xbins, double xlow, double xhigh, |
261 |
+ |
TString yexpr, int ybins, double ylow, double yhigh, |
262 |
+ |
const char *cut = "", const char *drawopt = "") { |
263 |
+ |
htempDelete(); |
264 |
+ |
TH2 * htemp = new TH2F("htemp", (strlen(cut) ? yexpr+":"+xexpr+"{"+cut+"}" : yexpr+":"+xexpr), |
265 |
+ |
xbins, xlow, xhigh, ybins,ylow,yhigh); |
266 |
+ |
return draw2D(xexpr,yexpr,cut,drawopt,htemp); |
267 |
+ |
} |
268 |
+ |
|
269 |
+ |
|
270 |
+ |
TGraph * drawGraph(TString xexpr, TString yexpr, const char *cut = "", TString drawopt = "AP", TGraph *graph = 0) { |
271 |
+ |
// prep the machinery |
272 |
+ |
helper::ScannerBase scanner(objType); |
273 |
+ |
scanner.setIgnoreExceptions(ignoreExceptions_); |
274 |
+ |
if (!scanner.addExpression((const char *)xexpr)) return 0; |
275 |
+ |
if (!scanner.addExpression((const char *)yexpr)) return 0; |
276 |
+ |
if (strlen(cut)) scanner.setCut(cut); |
277 |
+ |
|
278 |
+ |
// make graph, if needed |
279 |
+ |
if (graph == 0) { |
280 |
+ |
htempDelete(); |
281 |
+ |
graph = new TGraph(); |
282 |
+ |
graph->SetNameTitle("htemp", (strlen(cut) ? yexpr+":"+xexpr+"{"+cut+"}" : yexpr+":"+xexpr)); |
283 |
+ |
} |
284 |
+ |
|
285 |
+ |
// fill graph |
286 |
+ |
int iev = 0; |
287 |
+ |
for (event_->toBegin(); !event_->atEnd(); ++(*event_), ++iev) { |
288 |
+ |
if (maxEntries_ > -1 && iev > maxEntries_) break; |
289 |
+ |
if (!selectEvent(*event_)) continue; |
290 |
+ |
handle_.getByLabel(*event_, label_.c_str(), instance_.c_str(), process_.c_str()); |
291 |
+ |
const std::vector<T> & vals = *handle_; |
292 |
+ |
for (size_t j = 0, n = vals.size(); j < n; ++j) { |
293 |
+ |
scanner.fillGraph(&vals[j], graph); |
294 |
+ |
} |
295 |
+ |
} |
296 |
+ |
|
297 |
+ |
if (!strlen(graph->GetXaxis()->GetTitle())) graph->GetXaxis()->SetTitle(xexpr); |
298 |
+ |
if (!strlen(graph->GetYaxis()->GetTitle())) graph->GetYaxis()->SetTitle(yexpr); |
299 |
+ |
if (!TString(drawopt).Contains("goff",TString::kIgnoreCase)) graph->Draw(drawopt); |
300 |
+ |
return graph; |
301 |
+ |
} |
302 |
+ |
|
303 |
|
void setPrintFullEventId(bool printIt=true) { printFullEventId_ = printIt; } |
304 |
+ |
void setExpressionSeparator(TString separator) { exprSep_ = separator; } |
305 |
+ |
void setIgnoreExceptions(bool ignoreThem) { ignoreExceptions_ = ignoreThem; } |
306 |
+ |
void setMaxLinesToPrint(int lines) { maxLinesToPrint_ = (lines > 0 ? lines : 2147483647); } |
307 |
+ |
|
308 |
+ |
void addEventSelector(fwlite::EventSelector *selector) { eventSelectors_.Add(selector); } |
309 |
+ |
void clearEventSelector() { eventSelectors_.Clear(); } |
310 |
+ |
TObjArray & eventSelectors() { return eventSelectors_; } |
311 |
+ |
bool selectEvent(const fwlite::EventBase &ev) const { |
312 |
+ |
for (int i = 0, n = eventSelectors_.GetEntries(); i < n; ++i) { |
313 |
+ |
if (!((fwlite::EventSelector *)(eventSelectors_[i]))->accept(ev)) return false; |
314 |
+ |
} |
315 |
+ |
return true; |
316 |
+ |
} |
317 |
+ |
|
318 |
+ |
void setMaxEntries(int max) { maxEntries_ = max; } |
319 |
|
private: |
320 |
|
fwlite::EventBase *event_; |
321 |
|
std::string label_, instance_, process_; |
322 |
|
bool printFullEventId_; |
323 |
+ |
bool ignoreExceptions_; |
324 |
+ |
TString exprSep_; |
325 |
|
HandleT handle_; |
326 |
< |
helper::Parser helpme; |
327 |
< |
Reflex::Type objType; |
326 |
> |
Reflex::Type objType; |
327 |
> |
|
328 |
> |
TObjArray eventSelectors_; |
329 |
> |
|
330 |
> |
int maxEntries_; |
331 |
> |
|
332 |
> |
int maxLinesToPrint_; |
333 |
> |
bool wantMore() const { |
334 |
> |
// ask if user wants more |
335 |
> |
fprintf(stderr,"Type <CR> to continue or q to quit ==> "); |
336 |
> |
// read first char |
337 |
> |
int readch = getchar(), answer = readch; |
338 |
> |
// poll out remaining chars from buffer |
339 |
> |
while (readch != '\n' && readch != EOF) readch = getchar(); |
340 |
> |
// check first char |
341 |
> |
return !(answer == 'q' || answer == 'Q'); |
342 |
> |
} |
343 |
> |
|
344 |
> |
void htempDelete() { |
345 |
> |
if (gDirectory) { |
346 |
> |
TObject *obj = gDirectory->Get("htemp"); |
347 |
> |
if (obj) obj->Delete(); |
348 |
> |
} |
349 |
> |
} |
350 |
|
|
351 |
|
}; |
352 |
|
} |