ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/GPetrucc/interface/fwlite/Scanner.h
(Generate patch)

Comparing UserCode/GPetrucc/interface/fwlite/Scanner.h (file contents):
Revision 1.2 by gpetrucc, Thu Jan 28 11:49:27 2010 UTC vs.
Revision 1.11 by gpetrucc, Wed Mar 3 09:29:53 2010 UTC

# Line 5 | Line 5
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>
# Line 19 | Line 27 | namespace fwlite {
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");
# Line 40 | Line 53 | namespace fwlite {
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;
# Line 54 | Line 73 | namespace fwlite {
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;
# Line 68 | Line 91 | namespace fwlite {
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 +                return npass;
124 +            }
125 +
126 +            TH1 * draw(const char *expr, const char *cut = "", TString drawopt = "", TH1 *hist = 0) {
127 +                // prep the machinery
128 +                helper::ScannerBase scanner(objType);
129 +                scanner.setIgnoreExceptions(ignoreExceptions_);
130 +                if (!scanner.addExpression(expr)) return 0;
131 +                if (strlen(cut)) scanner.setCut(cut);
132 +
133 +                // make histo, if needed
134 +                if (hist == 0) {
135 +                    if (TString(drawopt).Contains("same",TString::kIgnoreCase) &&
136 +                        gDirectory && gDirectory->Get("htemp") != 0 &&
137 +                        gDirectory->Get("htemp")->IsA()->InheritsFrom(TH1::Class())) {
138 +                        hist = (TH1*) gDirectory->Get("htemp")->Clone();
139 +                        hist->Reset();
140 +                        hist->SetLineColor(kBlack);
141 +                        hist->SetMarkerColor(kBlack);
142 +                    } else {
143 +                        htempDelete();
144 +                        hist = new TH1F("htemp",
145 +                                (strlen(cut) ? TString(expr)+"{"+cut+"}" : TString(expr)),
146 +                                gEnv->GetValue("Hist.Binning.1D.x",100), 0, 0);
147 +                        hist->SetBit(TH1::kCanRebin);
148 +                    }
149 +                }
150 +
151 +                // fill histogram
152 +                int iev = 0;
153 +                for (event_->toBegin(); !event_->atEnd(); ++(*event_), ++iev) {
154 +                    if (maxEntries_ > -1 && iev > maxEntries_) break;
155 +                    if (!selectEvent(*event_)) continue;
156 +                    handle_.getByLabel(*event_, label_.c_str(), instance_.c_str(), process_.c_str());
157 +                    const std::vector<T> & vals = *handle_;
158 +                    for (size_t j = 0, n = vals.size(); j < n; ++j) {
159 +                        scanner.fill1D(&vals[j], hist);
160 +                    }
161 +                }
162 +
163 +                if (TString(drawopt).Contains("NORM",TString::kIgnoreCase) && (hist->Integral() != 0)) hist->Scale(1.0/hist->Integral());
164 +
165 +                if (!TString(drawopt).Contains("goff",TString::kIgnoreCase)) hist->Draw(drawopt);
166 +                return hist;
167 +            }
168 +            TH1 * draw(const char *expr, int bins, double xlow, double xhigh, const char *cut = "", const char *drawopt = "") {
169 +                if (!TString(drawopt).Contains("same",TString::kIgnoreCase)) return drawProf(expr,cut,drawopt);
170 +                htempDelete();
171 +                TH1 * htemp = new TH1F("htemp", (strlen(cut) ? TString(expr)+"{"+cut+"}" : TString(expr)), bins, xlow, xhigh);
172 +                return draw(expr,cut,drawopt,htemp);
173 +            }
174 +
175 +            TProfile * drawProf(TString xexpr, TString yexpr, const char *cut = "", TString drawopt = "", TProfile *hist = 0) {
176 +                // prep the machinery
177 +                helper::ScannerBase scanner(objType);
178 +                scanner.setIgnoreExceptions(ignoreExceptions_);
179 +                if (!scanner.addExpression(xexpr.Data())) return 0;
180 +                if (!scanner.addExpression(yexpr.Data())) return 0;
181 +                if (strlen(cut)) scanner.setCut(cut);
182 +
183 +                // make histo, if needed
184 +                if (hist == 0) {
185 +                    htempDelete();
186 +                    hist = new TProfile("htemp",
187 +                            (strlen(cut) ? yexpr+":"+xexpr+"{"+cut+"}" : yexpr+":"+xexpr),
188 +                            gEnv->GetValue("Hist.Binning.1D.x",100), 0., 0.);
189 +                   hist->SetBit(TH1::kCanRebin);
190 +                }
191 +
192 +                // fill histogram
193 +                int iev = 0;
194 +                for (event_->toBegin(); !event_->atEnd(); ++(*event_), ++iev) {
195 +                    if (maxEntries_ > -1 && iev > maxEntries_) break;
196 +                    if (!selectEvent(*event_)) continue;
197 +                    handle_.getByLabel(*event_, label_.c_str(), instance_.c_str(), process_.c_str());
198 +                    const std::vector<T> & vals = *handle_;
199 +                    for (size_t j = 0, n = vals.size(); j < n; ++j) {
200 +                        scanner.fillProf(&vals[j], hist);
201 +                    }
202 +                }
203 +
204 +                if (!TString(drawopt).Contains("goff",TString::kIgnoreCase)) hist->Draw(drawopt);
205 +                return hist;
206 +            }
207 +            TProfile * drawProf(TString xexpr, int bins, double xlow, double xhigh, TString yexpr, const char *cut = "", const char *drawopt = "") {
208 +                htempDelete();
209 +                TProfile * htemp = new TProfile("htemp", (strlen(cut) ? yexpr+":"+xexpr+"{"+cut+"}" : yexpr+":"+xexpr), bins, xlow, xhigh);
210 +                return drawProf(xexpr,yexpr,cut,drawopt,htemp);
211 +            }
212 +
213 +            TH2 * draw2D(TString xexpr, TString yexpr, const char *cut = "", TString drawopt = "", TH2 *hist = 0) {
214 +                // prep the machinery
215 +                helper::ScannerBase scanner(objType);
216 +                scanner.setIgnoreExceptions(ignoreExceptions_);
217 +                if (!scanner.addExpression((const char *)xexpr)) return 0;
218 +                if (!scanner.addExpression((const char *)yexpr)) return 0;
219 +                if (strlen(cut)) scanner.setCut(cut);
220 +
221 +                int iev = 0;
222 +                // make histo, if needed
223 +                if (hist == 0) {
224 +                    // ok this is much more a hack than for the 1D case
225 +                    double xmin = 0, xmax = -1, ymin = 0, ymax = -1;
226 +                    for (event_->toBegin(), iev = 0; !event_->atEnd(); ++(*event_), ++iev) {
227 +                        if (maxEntries_ > -1 && iev > maxEntries_) break;
228 +                        if (!selectEvent(*event_)) continue;
229 +                        handle_.getByLabel(*event_, label_.c_str(), instance_.c_str(), process_.c_str());
230 +                        const std::vector<T> & vals = *handle_;
231 +                        for (size_t j = 0, n = vals.size(); j < n; ++j) {
232 +                            if (!scanner.test(&vals[j])) continue;
233 +                            double x = scanner.eval(&vals[j],0);
234 +                            double y = scanner.eval(&vals[j],1);
235 +                            if ((xmax == -1) || (x >= xmax)) xmax = x;
236 +                            if ((xmin ==  0) || (x <= xmin)) xmin = x;
237 +                            if ((ymax == -1) || (y >= ymax)) ymax = y;
238 +                            if ((ymin ==  0) || (y <= ymin)) ymin = y;
239 +                        }
240 +                    }
241 +                    htempDelete();
242 +                    hist = new TH2F("htemp",
243 +                            (strlen(cut) ? yexpr+":"+xexpr+"{"+cut+"}" : yexpr+":"+xexpr),
244 +                            gEnv->GetValue("Hist.Binning.2D.x",20), xmin, xmax,
245 +                            gEnv->GetValue("Hist.Binning.2D.y",20), ymin, ymax);
246 +                }
247 +
248 +                // fill histogram
249 +                for (event_->toBegin(), iev = 0; !event_->atEnd(); ++(*event_), ++iev) {
250 +                    if (maxEntries_ > -1 && iev > maxEntries_) break;
251 +                    if (!selectEvent(*event_)) continue;
252 +                    handle_.getByLabel(*event_, label_.c_str(), instance_.c_str(), process_.c_str());
253 +                    const std::vector<T> & vals = *handle_;
254 +                    for (size_t j = 0, n = vals.size(); j < n; ++j) {
255 +                        scanner.fill2D(&vals[j], hist);
256 +                    }
257 +                }
258 +
259 +                if (!strlen(hist->GetXaxis()->GetTitle())) hist->GetXaxis()->SetTitle(xexpr);
260 +                if (!strlen(hist->GetYaxis()->GetTitle())) hist->GetYaxis()->SetTitle(yexpr);
261 +                if (!TString(drawopt).Contains("goff",TString::kIgnoreCase)) hist->Draw(drawopt);
262 +                return hist;
263 +            }
264 +            TH2 * draw2D(TString xexpr, int xbins, double xlow, double xhigh,
265 +                         TString yexpr, int ybins, double ylow, double yhigh,
266 +                         const char *cut = "", const char *drawopt = "") {
267 +                htempDelete();
268 +                TH2 * htemp = new TH2F("htemp", (strlen(cut) ? yexpr+":"+xexpr+"{"+cut+"}" : yexpr+":"+xexpr),
269 +                                        xbins, xlow, xhigh, ybins,ylow,yhigh);
270 +                return draw2D(xexpr,yexpr,cut,drawopt,htemp);
271 +            }
272 +
273 +
274 +            TGraph * drawGraph(TString xexpr, TString yexpr, const char *cut = "", TString drawopt = "AP", TGraph *graph = 0) {
275 +                // prep the machinery
276 +                helper::ScannerBase scanner(objType);
277 +                scanner.setIgnoreExceptions(ignoreExceptions_);
278 +                if (!scanner.addExpression((const char *)xexpr)) return 0;
279 +                if (!scanner.addExpression((const char *)yexpr)) return 0;
280 +                if (strlen(cut)) scanner.setCut(cut);
281 +
282 +                // make graph, if needed
283 +                if (graph == 0) {
284 +                    htempDelete();
285 +                    graph = new TGraph();
286 +                    graph->SetNameTitle("htemp", (strlen(cut) ? yexpr+":"+xexpr+"{"+cut+"}" : yexpr+":"+xexpr));
287 +                }
288 +
289 +                // fill graph
290 +                int iev = 0;
291 +                for (event_->toBegin(); !event_->atEnd(); ++(*event_), ++iev) {
292 +                    if (maxEntries_ > -1 && iev > maxEntries_) break;
293 +                    if (!selectEvent(*event_)) continue;
294 +                    handle_.getByLabel(*event_, label_.c_str(), instance_.c_str(), process_.c_str());
295 +                    const std::vector<T> & vals = *handle_;
296 +                    for (size_t j = 0, n = vals.size(); j < n; ++j) {
297 +                        scanner.fillGraph(&vals[j], graph);
298 +                    }
299 +                }
300 +
301 +                if (!strlen(graph->GetXaxis()->GetTitle())) graph->GetXaxis()->SetTitle(xexpr);
302 +                if (!strlen(graph->GetYaxis()->GetTitle())) graph->GetYaxis()->SetTitle(yexpr);
303 +                if (!TString(drawopt).Contains("goff",TString::kIgnoreCase)) graph->Draw(drawopt);
304 +                return graph;
305 +            }
306 +
307              void setPrintFullEventId(bool printIt=true) { printFullEventId_ = printIt; }
308 +            void setExpressionSeparator(TString separator) { exprSep_ = separator; }
309 +            void setIgnoreExceptions(bool ignoreThem) { ignoreExceptions_ = ignoreThem; }
310 +            void setMaxLinesToPrint(int lines) { maxLinesToPrint_ = (lines > 0 ? lines : 2147483647); }
311 +    
312 +            void addEventSelector(fwlite::EventSelector *selector) { eventSelectors_.Add(selector); }
313 +            void clearEventSelector() { eventSelectors_.Clear(); }
314 +            TObjArray & eventSelectors() { return eventSelectors_; }
315 +            bool selectEvent(const fwlite::EventBase &ev) const {
316 +                for (int i = 0, n = eventSelectors_.GetEntries(); i < n; ++i) {
317 +                    if (!((fwlite::EventSelector *)(eventSelectors_[i]))->accept(ev)) return false;
318 +                }
319 +                return true;
320 +            }
321 +
322 +            void setMaxEntries(int max) { maxEntries_ = max; }
323          private:
324              fwlite::EventBase *event_;
325              std::string    label_, instance_, process_;
326              bool printFullEventId_;
327 +            bool ignoreExceptions_;
328 +            TString exprSep_;
329              HandleT        handle_;
330 <            helper::Parser helpme;
331 <            Reflex::Type objType;
330 >            Reflex::Type   objType;
331 >
332 >            TObjArray eventSelectors_;
333 >
334 >            int maxEntries_;
335 >
336 >            int maxLinesToPrint_;
337 >            bool wantMore() const {
338 >                // ask if user wants more
339 >                fprintf(stderr,"Type <CR> to continue or q to quit ==> ");
340 >                // read first char
341 >                int readch = getchar(), answer = readch;
342 >                // poll out remaining chars from buffer
343 >                while (readch != '\n' && readch != EOF) readch = getchar();
344 >                // check first char
345 >                return !(answer == 'q' || answer == 'Q');
346 >            }
347 >
348 >            void htempDelete() {
349 >                if (gDirectory) {
350 >                    TObject *obj = gDirectory->Get("htemp");
351 >                    if (obj) obj->Delete();
352 >                }
353 >            }
354  
355      };
356   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines