ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/GPetrucc/interface/fwlite/Scanner.h
Revision: 1.6
Committed: Tue Feb 2 18:11:54 2010 UTC (15 years, 3 months ago) by gpetrucc
Content type: text/plain
Branch: MAIN
Changes since 1.5: +24 -5 lines
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 <TObjString.h>
7     #include <TObjArray.h>
8 gpetrucc 1.4 #include <TEnv.h>
9 gpetrucc 1.1
10     #if !defined(__CINT__) && !defined(__MAKECINT__)
11     #include "UserCode/GPetrucc/interface/fwliteHelpers.h"
12     #else
13 gpetrucc 1.6 #ifndef UserCode_GPetrucc_CINT_load_library
14     #define UserCode_GPetrucc_CINT_load_library
15 gpetrucc 1.1 // load the library that contains the dictionaries
16     int _load_UserCodeGPetrucc = gSystem->Load("libUserCodeGPetrucc.so");
17     #endif
18 gpetrucc 1.6 #endif
19    
20     #include "UserCode/GPetrucc/interface/fwlite/EventSelectors.h"
21 gpetrucc 1.1
22     namespace fwlite {
23     template<typename T>
24     class Scanner {
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 gpetrucc 1.4 event_(ev), label_(label), instance_(instance),
29     printFullEventId_(ev->isRealData()),
30 gpetrucc 1.5 ignoreExceptions_(false),
31 gpetrucc 1.4 exprSep_(":")
32 gpetrucc 1.1 {
33 gpetrucc 1.6 objType = helper::Parser::elementType(Reflex::Type::ByTypeInfo(HandleT::TempWrapT::typeInfo()));
34     eventSelectors_.SetOwner();
35 gpetrucc 1.1 }
36    
37    
38     void scan(const char *exprs, const char *cut="", int nmax=20) {
39     helper::ScannerBase scanner(objType);
40 gpetrucc 1.5 scanner.setIgnoreExceptions(ignoreExceptions_);
41 gpetrucc 1.1
42 gpetrucc 1.4 TObjArray *exprArray = TString(exprs).Tokenize(exprSep_);
43 gpetrucc 1.1 int rowline = 0;
44     if (printFullEventId_) {
45     printf(" : %9s : %4s : %9s : %3s", "RUN", "LUMI", "EVENT", "#IT");
46     rowline += 3*4+9+4+9+3-1; // -1 as first char remain blank
47     } else {
48 gpetrucc 1.2 printf(" : %5s : %3s", "EVENT", "#IT");
49 gpetrucc 1.1 rowline += 3+6+3+3-1; // -1 as first char remain blank
50     }
51     for (int i = 0; i < exprArray->GetEntries(); ++i) {
52     const char *ex = ((TObjString *)(*exprArray)[i])->GetString();
53     scanner.addExpression(ex);
54 gpetrucc 1.3 printf(" : %8s", (strlen(ex)>8 ? ex+strlen(ex)-8 : ex)); // the rightmost part is usually the more interesting one
55 gpetrucc 1.1 rowline += 3+8;
56     }
57     std::cout << " :" << std::endl;
58     rowline += 2;
59     delete exprArray;
60    
61     TString rule('-', rowline);
62     std::cout << " " << rule << " " << std::endl;
63    
64     if (strlen(cut)) scanner.setCut(cut);
65    
66     int iev = 0;
67 gpetrucc 1.6 for (event_->toBegin(); (iev != nmax) && !event_->atEnd(); ++iev, ++(*event_)) {
68     if (!selectEvent(*event_)) continue;
69 gpetrucc 1.1 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) {
72     if (!scanner.test(&vals[j])) continue;
73     if (printFullEventId_) {
74     const edm::EventAuxiliary &id = event_->eventAuxiliary();
75     printf(" : %9d : %4d : %9d : %3d", id.run(), id.luminosityBlock(), id.event(), j);
76     } else {
77     printf(" : %5d : %3d", iev, j);
78     }
79     scanner.print(&vals[j]);
80     std::cout << " :" << std::endl;
81     }
82     }
83     std::cout << std::endl;
84     }
85 gpetrucc 1.4
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 gpetrucc 1.5 scanner.setIgnoreExceptions(ignoreExceptions_);
91 gpetrucc 1.4 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 gpetrucc 1.6 if (!selectEvent(*event_)) continue;
105 gpetrucc 1.4 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 gpetrucc 1.5 scanner.setIgnoreExceptions(ignoreExceptions_);
124 gpetrucc 1.4 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 gpetrucc 1.6 if (!selectEvent(*event_)) continue;
139 gpetrucc 1.4 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 gpetrucc 1.5 scanner.setIgnoreExceptions(ignoreExceptions_);
158 gpetrucc 1.4 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 gpetrucc 1.6 if (!selectEvent(*event_)) continue;
174 gpetrucc 1.4 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 gpetrucc 1.1 void setPrintFullEventId(bool printIt=true) { printFullEventId_ = printIt; }
195 gpetrucc 1.4 void setExpressionSeparator(TString separator) { exprSep_ = separator; }
196 gpetrucc 1.5 void setIgnoreExceptions(bool ignoreThem) { ignoreExceptions_ = ignoreThem; }
197 gpetrucc 1.6
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 gpetrucc 1.1 private:
208 gpetrucc 1.2 fwlite::EventBase *event_;
209     std::string label_, instance_, process_;
210 gpetrucc 1.1 bool printFullEventId_;
211 gpetrucc 1.5 bool ignoreExceptions_;
212 gpetrucc 1.4 TString exprSep_;
213 gpetrucc 1.1 HandleT handle_;
214 gpetrucc 1.4 Reflex::Type objType;
215 gpetrucc 1.1
216 gpetrucc 1.6 TObjArray eventSelectors_;
217    
218 gpetrucc 1.1 };
219     }