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.6 by gpetrucc, Tue Feb 2 18:11:54 2010 UTC vs.
Revision 1.8 by gpetrucc, Thu Feb 11 10:12:09 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"
# Line 28 | Line 30 | namespace fwlite {
30                  event_(ev), label_(label), instance_(instance),
31                  printFullEventId_(ev->isRealData()),
32                  ignoreExceptions_(false),
33 <                exprSep_(":")
33 >                exprSep_(":"),
34 >                maxLinesToPrint_(50)
35              {
36                  objType = helper::Parser::elementType(Reflex::Type::ByTypeInfo(HandleT::TempWrapT::typeInfo()));
37                  eventSelectors_.SetOwner();
38              }
39  
40              
41 <            void scan(const char *exprs, const char *cut="", int nmax=20) {
41 >            void scan(const char *exprs, const char *cut="", int nmax=-1) {
42                  helper::ScannerBase scanner(objType);
43                  scanner.setIgnoreExceptions(ignoreExceptions_);
44  
# Line 49 | Line 52 | namespace fwlite {
52                      rowline += 3+6+3+3-1;  // -1 as first char remain blank
53                  }
54                  for (int i = 0; i < exprArray->GetEntries(); ++i) {
55 <                    const char *ex = ((TObjString *)(*exprArray)[i])->GetString();
56 <                    scanner.addExpression(ex);
57 <                    printf(" : %8s", (strlen(ex)>8 ? ex+strlen(ex)-8 : ex)); // the rightmost part is usually the more interesting one
55 >                    TString str = ((TObjString *)(*exprArray)[i])->GetString();
56 >                    std::string lb = str.Data();
57 >                    std::string ex = str.Data();
58 >                    if ((ex[0] == '@') && (ex.find('=') != std::string::npos)) {
59 >                        lb = lb.substr(1,ex.find('=')-1);
60 >                        ex = ex.substr(ex.find('=')+1);    
61 >                    }
62 >                    scanner.addExpression(ex.c_str());
63 >                    printf(" : %8s", (lb.size()>8 ? lb.substr(lb.size()-8) : lb).c_str()); // the rightmost part is usually the more interesting one
64                      rowline += 3+8;
65                  }
66                  std::cout << " :" << std::endl;
# Line 63 | Line 72 | namespace fwlite {
72  
73                  if (strlen(cut)) scanner.setCut(cut);
74  
75 <                int iev = 0;
75 >                int iev = 0, line = 0;
76                  for (event_->toBegin(); (iev != nmax) && !event_->atEnd(); ++iev, ++(*event_)) {
77                      if (!selectEvent(*event_)) continue;
78                      handle_.getByLabel(*event_, label_.c_str(), instance_.c_str(), process_.c_str());
79 +                    if (handle_.failedToGet()) {
80 +                        if (ignoreExceptions_) continue;
81 +                    }
82                      const std::vector<T> & vals = *handle_;
83                      for (size_t j = 0, n = vals.size(); j < n; ++j) {
84                          if (!scanner.test(&vals[j])) continue;
# Line 78 | Line 90 | namespace fwlite {
90                          }
91                          scanner.print(&vals[j]);
92                          std::cout << " :" << std::endl;
93 +                        if (++line == maxLinesToPrint_) {
94 +                            line = 0;
95 +                            if (!wantMore()) {
96 +                                iev = nmax-1; // this is to exit the outer loop
97 +                                break;        // and this to exit the inner one
98 +                             }
99 +                        }
100                      }
101                  }
102                  std::cout << std::endl;
103              }
104 +  
105 +            size_t count(const char *cut) {
106 +                helper::ScannerBase scanner(objType);
107 +                scanner.setIgnoreExceptions(ignoreExceptions_);
108  
109 +                scanner.setCut(cut);
110  
111 +                size_t npass = 0;
112 +                for (event_->toBegin(); !event_->atEnd(); ++(*event_)) {
113 +                    if (!selectEvent(*event_)) continue;
114 +                    handle_.getByLabel(*event_, label_.c_str(), instance_.c_str(), process_.c_str());
115 +                    const std::vector<T> & vals = *handle_;
116 +                    for (size_t j = 0, n = vals.size(); j < n; ++j) {
117 +                        if (scanner.test(&vals[j])) npass++;
118 +                    }
119 +                }
120 +
121 +                return npass;
122 +            }
123 +
124              TH1 * draw(const char *expr, const char *cut = "", TString drawopt = "", TH1 *hist = 0) {
125                  // prep the machinery
126                  helper::ScannerBase scanner(objType);
127                  scanner.setIgnoreExceptions(ignoreExceptions_);
128 <                scanner.addExpression(expr);
128 >                if (!scanner.addExpression(expr)) return 0;
129                  if (strlen(cut)) scanner.setCut(cut);
130  
131                  // make histo, if needed
132                  if (hist == 0) {
133 <                    hist = new TH1F("htemp",
134 <                            (strlen(cut) ? TString(expr)+"{"+cut+"}" : TString(expr)),
135 <                            gEnv->GetValue("Hist.Binning.1D.x",100), 0, 0);
136 <                    hist->SetBit(TH1::kCanRebin);
133 >                    if (TString(drawopt).Contains("same",TString::kIgnoreCase) &&
134 >                        gDirectory && gDirectory->Get("htemp") != 0 &&
135 >                        gDirectory->Get("htemp")->IsA()->InheritsFrom(TH1::Class())) {
136 >                        hist = (TH1*) gDirectory->Get("htemp")->Clone();
137 >                        hist->Reset();
138 >                    } else {
139 >                        htempDelete();
140 >                        hist = new TH1F("htemp",
141 >                                (strlen(cut) ? TString(expr)+"{"+cut+"}" : TString(expr)),
142 >                                gEnv->GetValue("Hist.Binning.1D.x",100), 0, 0);
143 >                        hist->SetBit(TH1::kCanRebin);
144 >                    }
145                  }
146  
147                  // fill histogram
# Line 113 | Line 158 | namespace fwlite {
158                  return hist;
159              }
160              TH1 * draw(const char *expr, int bins, double xlow, double xhigh, const char *cut = "", const char *drawopt = "") {
161 +                htempDelete();
162                  TH1 * htemp = new TH1F("htemp", (strlen(cut) ? TString(expr)+"{"+cut+"}" : TString(expr)), bins, xlow, xhigh);
163                  return draw(expr,cut,drawopt,htemp);
164              }
# Line 121 | Line 167 | namespace fwlite {
167                  // prep the machinery
168                  helper::ScannerBase scanner(objType);
169                  scanner.setIgnoreExceptions(ignoreExceptions_);
170 <                scanner.addExpression(xexpr);
171 <                scanner.addExpression(yexpr);
170 >                if (!scanner.addExpression(xexpr)) return 0;
171 >                if (!scanner.addExpression(yexpr)) return 0;
172                  if (strlen(cut)) scanner.setCut(cut);
173  
174                  // make histo, if needed
175                  if (hist == 0) {
176 +                    htempDelete();
177                      hist = new TProfile("htemp",
178                              (strlen(cut) ? yexpr+":"+xexpr+"{"+cut+"}" : yexpr+":"+xexpr),
179                              gEnv->GetValue("Hist.Binning.1D.x",100), 0., 0.);
# Line 147 | Line 194 | namespace fwlite {
194                  return hist;
195              }
196              TProfile * drawProf(TString xexpr, int bins, double xlow, double xhigh, TString yexpr, const char *cut = "", const char *drawopt = "") {
197 +                htempDelete();
198                  TProfile * htemp = new TProfile("htemp", (strlen(cut) ? yexpr+":"+xexpr+"{"+cut+"}" : yexpr+":"+xexpr), bins, xlow, xhigh);
199                  return drawProf(xexpr,yexpr,cut,drawopt,htemp);
200              }
# Line 155 | Line 203 | namespace fwlite {
203                  // prep the machinery
204                  helper::ScannerBase scanner(objType);
205                  scanner.setIgnoreExceptions(ignoreExceptions_);
206 <                scanner.addExpression(xexpr);
207 <                scanner.addExpression(yexpr);
206 >                if (!scanner.addExpression((const char *)xexpr)) return 0;
207 >                if (!scanner.addExpression((const char *)yexpr)) return 0;
208                  if (strlen(cut)) scanner.setCut(cut);
209  
210                  // make histo, if needed
211                  if (hist == 0) {
212 +                    // ok this is much more a hack than for the 1D case
213 +                    double xmin = 0, xmax = -1, ymin = 0, ymax = -1;
214 +                    for (event_->toBegin(); !event_->atEnd(); ++(*event_)) {
215 +                        if (!selectEvent(*event_)) continue;
216 +                        handle_.getByLabel(*event_, label_.c_str(), instance_.c_str(), process_.c_str());
217 +                        const std::vector<T> & vals = *handle_;
218 +                        for (size_t j = 0, n = vals.size(); j < n; ++j) {
219 +                            if (!scanner.test(&vals[j])) continue;
220 +                            double x = scanner.eval(&vals[j],0);
221 +                            double y = scanner.eval(&vals[j],1);
222 +                            if ((xmax == -1) || (x >= xmax)) xmax = x;
223 +                            if ((xmin ==  0) || (x <= xmin)) xmin = x;
224 +                            if ((ymax == -1) || (y >= ymax)) ymax = y;
225 +                            if ((ymin ==  0) || (y <= ymin)) ymin = y;
226 +                        }
227 +                    }
228 +                    htempDelete();
229                      hist = new TH2F("htemp",
230                              (strlen(cut) ? yexpr+":"+xexpr+"{"+cut+"}" : yexpr+":"+xexpr),
231 <                            gEnv->GetValue("Hist.Binning.2D.x",20), 0, 0,
232 <                            gEnv->GetValue("Hist.Binning.2D.y",20), 0, 0);
168 <                   hist->SetBit(TH1::kCanRebin);
231 >                            gEnv->GetValue("Hist.Binning.2D.x",20), xmin, xmax,
232 >                            gEnv->GetValue("Hist.Binning.2D.y",20), ymin, ymax);
233                  }
234  
235                  // fill histogram
# Line 186 | Line 250 | namespace fwlite {
250              TH2 * draw2D(TString xexpr, int xbins, double xlow, double xhigh,
251                           TString yexpr, int ybins, double ylow, double yhigh,
252                           const char *cut = "", const char *drawopt = "") {
253 +                htempDelete();
254                  TH2 * htemp = new TH2F("htemp", (strlen(cut) ? yexpr+":"+xexpr+"{"+cut+"}" : yexpr+":"+xexpr),
255                                          xbins, xlow, xhigh, ybins,ylow,yhigh);
256                  return draw2D(xexpr,yexpr,cut,drawopt,htemp);
257              }
258  
259 +
260 +            TGraph * drawGraph(TString xexpr, TString yexpr, const char *cut = "", TString drawopt = "AP", TGraph *graph = 0) {
261 +                // prep the machinery
262 +                helper::ScannerBase scanner(objType);
263 +                scanner.setIgnoreExceptions(ignoreExceptions_);
264 +                if (!scanner.addExpression((const char *)xexpr)) return 0;
265 +                if (!scanner.addExpression((const char *)yexpr)) return 0;
266 +                if (strlen(cut)) scanner.setCut(cut);
267 +
268 +                // make graph, if needed
269 +                if (graph == 0) {
270 +                    htempDelete();
271 +                    graph = new TGraph();
272 +                    graph->SetNameTitle("htemp", (strlen(cut) ? yexpr+":"+xexpr+"{"+cut+"}" : yexpr+":"+xexpr));
273 +                }
274 +
275 +                // fill graph
276 +                for (event_->toBegin(); !event_->atEnd(); ++(*event_)) {
277 +                    if (!selectEvent(*event_)) continue;
278 +                    handle_.getByLabel(*event_, label_.c_str(), instance_.c_str(), process_.c_str());
279 +                    const std::vector<T> & vals = *handle_;
280 +                    for (size_t j = 0, n = vals.size(); j < n; ++j) {
281 +                        scanner.fillGraph(&vals[j], graph);
282 +                    }
283 +                }
284 +
285 +                if (!strlen(graph->GetXaxis()->GetTitle())) graph->GetXaxis()->SetTitle(xexpr);
286 +                if (!strlen(graph->GetYaxis()->GetTitle())) graph->GetYaxis()->SetTitle(yexpr);
287 +                if (!TString(drawopt).Contains("goff",TString::kIgnoreCase)) graph->Draw(drawopt);
288 +                return graph;
289 +            }
290 +
291              void setPrintFullEventId(bool printIt=true) { printFullEventId_ = printIt; }
292              void setExpressionSeparator(TString separator) { exprSep_ = separator; }
293              void setIgnoreExceptions(bool ignoreThem) { ignoreExceptions_ = ignoreThem; }
294 <
294 >            void setMaxLinesToPrint(int lines) { maxLinesToPrint_ = (lines > 0 ? lines : 2147483647); }
295 >    
296              void addEventSelector(fwlite::EventSelector *selector) { eventSelectors_.Add(selector); }
297              void clearEventSelector() { eventSelectors_.Clear(); }
298              TObjArray & eventSelectors() { return eventSelectors_; }
# Line 215 | Line 313 | namespace fwlite {
313  
314              TObjArray eventSelectors_;
315  
316 +            int maxLinesToPrint_;
317 +            bool wantMore() const {
318 +                // ask if user wants more
319 +                fprintf(stderr,"Type <CR> to continue or q to quit ==> ");
320 +                // read first char
321 +                int readch = getchar(), answer = readch;
322 +                // poll out remaining chars from buffer
323 +                while (readch != '\n' && readch != EOF) readch = getchar();
324 +                // check first char
325 +                return !(answer == 'q' || answer == 'Q');
326 +            }
327 +
328 +            void htempDelete() {
329 +                if (gDirectory) {
330 +                    TObject *obj = gDirectory->Get("htemp");
331 +                    if (obj) obj->Delete();
332 +                }
333 +            }
334 +
335      };
336   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines