ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/GPetrucc/interface/fwlite/Scanner.h
Revision: 1.10
Committed: Wed Feb 24 11:50:12 2010 UTC (15 years, 2 months ago) by gpetrucc
Content type: text/plain
Branch: MAIN
Changes since 1.9: +22 -6 lines
Log Message:
Improvements and some new stuff

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 <TObjString.h>
7     #include <TObjArray.h>
8 gpetrucc 1.7 #include <TDirectory.h>
9 gpetrucc 1.4 #include <TEnv.h>
10 gpetrucc 1.8 #include <TClass.h>
11 gpetrucc 1.1
12     #if !defined(__CINT__) && !defined(__MAKECINT__)
13     #include "UserCode/GPetrucc/interface/fwliteHelpers.h"
14     #else
15 gpetrucc 1.6 #ifndef UserCode_GPetrucc_CINT_load_library
16     #define UserCode_GPetrucc_CINT_load_library
17 gpetrucc 1.1 // load the library that contains the dictionaries
18     int _load_UserCodeGPetrucc = gSystem->Load("libUserCodeGPetrucc.so");
19     #endif
20 gpetrucc 1.6 #endif
21    
22     #include "UserCode/GPetrucc/interface/fwlite/EventSelectors.h"
23 gpetrucc 1.1
24     namespace fwlite {
25     template<typename T>
26     class Scanner {
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 gpetrucc 1.4 event_(ev), label_(label), instance_(instance),
31     printFullEventId_(ev->isRealData()),
32 gpetrucc 1.5 ignoreExceptions_(false),
33 gpetrucc 1.7 exprSep_(":"),
34 gpetrucc 1.10 maxEntries_(-1),
35 gpetrucc 1.7 maxLinesToPrint_(50)
36 gpetrucc 1.1 {
37 gpetrucc 1.6 objType = helper::Parser::elementType(Reflex::Type::ByTypeInfo(HandleT::TempWrapT::typeInfo()));
38 gpetrucc 1.9 //eventSelectors_.SetOwner(); // better a few leaks for lazy users than a few crashes for unweary ones
39 gpetrucc 1.1 }
40    
41    
42 gpetrucc 1.7 void scan(const char *exprs, const char *cut="", int nmax=-1) {
43 gpetrucc 1.1 helper::ScannerBase scanner(objType);
44 gpetrucc 1.5 scanner.setIgnoreExceptions(ignoreExceptions_);
45 gpetrucc 1.1
46 gpetrucc 1.4 TObjArray *exprArray = TString(exprs).Tokenize(exprSep_);
47 gpetrucc 1.1 int rowline = 0;
48     if (printFullEventId_) {
49     printf(" : %9s : %4s : %9s : %3s", "RUN", "LUMI", "EVENT", "#IT");
50     rowline += 3*4+9+4+9+3-1; // -1 as first char remain blank
51     } else {
52 gpetrucc 1.2 printf(" : %5s : %3s", "EVENT", "#IT");
53 gpetrucc 1.1 rowline += 3+6+3+3-1; // -1 as first char remain blank
54     }
55     for (int i = 0; i < exprArray->GetEntries(); ++i) {
56 gpetrucc 1.8 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 gpetrucc 1.1 rowline += 3+8;
66     }
67     std::cout << " :" << std::endl;
68     rowline += 2;
69     delete exprArray;
70    
71     TString rule('-', rowline);
72     std::cout << " " << rule << " " << std::endl;
73    
74     if (strlen(cut)) scanner.setCut(cut);
75    
76 gpetrucc 1.7 int iev = 0, line = 0;
77 gpetrucc 1.6 for (event_->toBegin(); (iev != nmax) && !event_->atEnd(); ++iev, ++(*event_)) {
78     if (!selectEvent(*event_)) continue;
79 gpetrucc 1.1 handle_.getByLabel(*event_, label_.c_str(), instance_.c_str(), process_.c_str());
80 gpetrucc 1.8 if (handle_.failedToGet()) {
81     if (ignoreExceptions_) continue;
82     }
83 gpetrucc 1.1 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;
86     if (printFullEventId_) {
87     const edm::EventAuxiliary &id = event_->eventAuxiliary();
88     printf(" : %9d : %4d : %9d : %3d", id.run(), id.luminosityBlock(), id.event(), j);
89     } else {
90     printf(" : %5d : %3d", iev, j);
91     }
92     scanner.print(&vals[j]);
93     std::cout << " :" << std::endl;
94 gpetrucc 1.7 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 gpetrucc 1.1 }
102     }
103     std::cout << std::endl;
104     }
105 gpetrucc 1.8
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 gpetrucc 1.10 int iev = 0;
114     for (event_->toBegin(); !event_->atEnd(); ++(*event_), ++iev) {
115     if (maxEntries_ > -1 && iev > maxEntries_) break;
116 gpetrucc 1.8 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 gpetrucc 1.4 TH1 * draw(const char *expr, const char *cut = "", TString drawopt = "", TH1 *hist = 0) {
128     // prep the machinery
129     helper::ScannerBase scanner(objType);
130 gpetrucc 1.5 scanner.setIgnoreExceptions(ignoreExceptions_);
131 gpetrucc 1.7 if (!scanner.addExpression(expr)) return 0;
132 gpetrucc 1.4 if (strlen(cut)) scanner.setCut(cut);
133    
134     // make histo, if needed
135     if (hist == 0) {
136 gpetrucc 1.8 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 gpetrucc 1.4 }
149    
150     // fill histogram
151 gpetrucc 1.10 int iev = 0;
152     for (event_->toBegin(); !event_->atEnd(); ++(*event_), ++iev) {
153     if (maxEntries_ > -1 && iev > maxEntries_) break;
154 gpetrucc 1.6 if (!selectEvent(*event_)) continue;
155 gpetrucc 1.4 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 gpetrucc 1.7 htempDelete();
167 gpetrucc 1.4 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 gpetrucc 1.5 scanner.setIgnoreExceptions(ignoreExceptions_);
175 gpetrucc 1.9 if (!scanner.addExpression(xexpr.Data())) return 0;
176     if (!scanner.addExpression(yexpr.Data())) return 0;
177 gpetrucc 1.4 if (strlen(cut)) scanner.setCut(cut);
178    
179     // make histo, if needed
180     if (hist == 0) {
181 gpetrucc 1.7 htempDelete();
182 gpetrucc 1.4 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 gpetrucc 1.10 int iev = 0;
190     for (event_->toBegin(); !event_->atEnd(); ++(*event_), ++iev) {
191     if (maxEntries_ > -1 && iev > maxEntries_) break;
192 gpetrucc 1.6 if (!selectEvent(*event_)) continue;
193 gpetrucc 1.4 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 gpetrucc 1.7 htempDelete();
205 gpetrucc 1.4 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 gpetrucc 1.5 scanner.setIgnoreExceptions(ignoreExceptions_);
213 gpetrucc 1.7 if (!scanner.addExpression((const char *)xexpr)) return 0;
214     if (!scanner.addExpression((const char *)yexpr)) return 0;
215 gpetrucc 1.4 if (strlen(cut)) scanner.setCut(cut);
216    
217 gpetrucc 1.10 int iev = 0;
218 gpetrucc 1.4 // make histo, if needed
219     if (hist == 0) {
220 gpetrucc 1.7 // ok this is much more a hack than for the 1D case
221     double xmin = 0, xmax = -1, ymin = 0, ymax = -1;
222 gpetrucc 1.10 for (event_->toBegin(), iev = 0; !event_->atEnd(); ++(*event_), ++iev) {
223     if (maxEntries_ > -1 && iev > maxEntries_) break;
224 gpetrucc 1.7 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 gpetrucc 1.4 hist = new TH2F("htemp",
239     (strlen(cut) ? yexpr+":"+xexpr+"{"+cut+"}" : yexpr+":"+xexpr),
240 gpetrucc 1.7 gEnv->GetValue("Hist.Binning.2D.x",20), xmin, xmax,
241     gEnv->GetValue("Hist.Binning.2D.y",20), ymin, ymax);
242 gpetrucc 1.4 }
243    
244     // fill histogram
245 gpetrucc 1.10 for (event_->toBegin(), iev = 0; !event_->atEnd(); ++(*event_), ++iev) {
246     if (maxEntries_ > -1 && iev > maxEntries_) break;
247 gpetrucc 1.6 if (!selectEvent(*event_)) continue;
248 gpetrucc 1.4 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 gpetrucc 1.7 htempDelete();
264 gpetrucc 1.4 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 gpetrucc 1.7
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 gpetrucc 1.10 int iev = 0;
287     for (event_->toBegin(); !event_->atEnd(); ++(*event_), ++iev) {
288     if (maxEntries_ > -1 && iev > maxEntries_) break;
289 gpetrucc 1.7 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 gpetrucc 1.1 void setPrintFullEventId(bool printIt=true) { printFullEventId_ = printIt; }
304 gpetrucc 1.4 void setExpressionSeparator(TString separator) { exprSep_ = separator; }
305 gpetrucc 1.5 void setIgnoreExceptions(bool ignoreThem) { ignoreExceptions_ = ignoreThem; }
306 gpetrucc 1.7 void setMaxLinesToPrint(int lines) { maxLinesToPrint_ = (lines > 0 ? lines : 2147483647); }
307    
308 gpetrucc 1.6 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 gpetrucc 1.10
318     void setMaxEntries(int max) { maxEntries_ = max; }
319 gpetrucc 1.1 private:
320 gpetrucc 1.2 fwlite::EventBase *event_;
321     std::string label_, instance_, process_;
322 gpetrucc 1.1 bool printFullEventId_;
323 gpetrucc 1.5 bool ignoreExceptions_;
324 gpetrucc 1.4 TString exprSep_;
325 gpetrucc 1.1 HandleT handle_;
326 gpetrucc 1.4 Reflex::Type objType;
327 gpetrucc 1.1
328 gpetrucc 1.6 TObjArray eventSelectors_;
329    
330 gpetrucc 1.10 int maxEntries_;
331    
332 gpetrucc 1.7 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 gpetrucc 1.1 };
352     }