1 #ifndef PhysicsTools_FWLite_Scanner_h
2 #define PhysicsTools_FWLite_Scanner_h
9 #include <TObjString.h>
10 #include <TObjArray.h>
11 #include <TDirectory.h>
15 #if !defined(__CINT__) && !defined(__MAKECINT__)
17 #include <RooArgList.h>
18 #include <RooDataSet.h>
19 #include <RooRealVar.h>
20 #include <RooCategory.h>
45 template <
typename Collection>
80 void scan(
const char *exprs,
const char *
cut =
"",
int nmax = -1) {
84 TObjArray *exprArray = TString(exprs).Tokenize(
exprSep_);
87 printf(
" : %9s : %4s : %9s : %3s",
"RUN",
"LUMI",
"EVENT",
"#IT");
88 rowline += 3 * 4 + 9 + 4 + 9 + 3 - 1;
90 printf(
" : %5s : %3s",
"EVENT",
"#IT");
91 rowline += 3 + 6 + 3 + 3 - 1;
93 for (
int i = 0;
i < exprArray->GetEntries(); ++
i) {
94 TString
str = ((TObjString *)(*exprArray)[
i])->GetString();
97 if ((ex[0] ==
'@') && (ex.find(
'=') != std::string::npos)) {
98 lb = lb.substr(1, ex.find(
'=') - 1);
99 ex = ex.substr(ex.find(
'=') + 1);
103 (lb.size() > 8 ? lb.substr(lb.size() - 8) : lb)
111 TString rule(
'-', rowline);
112 std::cout <<
" " << rule <<
" " << std::endl;
117 int iev = 0,
line = 0;
126 const Collection &vals = *
handle_;
127 for (
size_t j = 0,
n = vals.size();
j <
n; ++
j) {
128 if (!scanner.
test(&vals[
j]))
132 printf(
" : %9u : %4u : %9llu : %3lu",
id.
run(),
id.luminosityBlock(),
id.
event(), (
unsigned long)
j);
134 printf(
" : %5d : %3lu", iev, (
unsigned long)
j);
168 const Collection &vals = *
handle_;
169 for (
size_t j = 0,
n = vals.size();
j <
n; ++
j) {
170 if (scanner.
test(&vals[
j]))
209 if (
hist ==
nullptr) {
210 std::cerr <<
"Method draw(expr, cut, drawopt, hist) cannot be called with null 'hist'. Use the other draw "
224 const Collection &vals = *
handle_;
225 for (
size_t j = 0,
n = vals.size();
j <
n; ++
j) {
230 if (drawopt.Contains(
"NORM", TString::kIgnoreCase) && (
hist->Integral() != 0)) {
232 hist->Scale(1.0 /
hist->Integral());
234 drawopt(TRegexp(
"[Nn][Oo][Rr][Mm]")) =
"";
237 if (!drawopt.Contains(
"GOFF", TString::kIgnoreCase))
249 const char *
cut =
"",
250 TString drawopt =
"",
251 const char *hname =
"htemp",
252 const TH1 *htemplate =
nullptr) {
254 if (htemplate !=
nullptr) {
255 if ((strcmp(hname,
"htemp") == 0) && (strcmp(hname, htemplate->GetName()) != 0))
258 }
else if (drawopt.Contains(
"SAME", TString::kIgnoreCase)) {
263 if (
hist ==
nullptr) {
264 if (strcmp(hname,
"htemp") == 0)
266 hist =
new TH1F(hname,
"", gEnv->GetValue(
"Hist.Binning.1D.x", 100), 0, 0);
267 hist->SetCanExtend(TH1::kAllAxes);
280 const char *
cut =
"",
281 const char *drawopt =
"",
282 const char *hname =
"htemp") {
283 if (TString(drawopt).Contains(
"SAME", TString::kIgnoreCase)) {
284 std::cerr <<
"Binning is ignored when 'SAME' is specified." << std::endl;
288 if (strcmp(hname,
"htemp") == 0)
290 TH1 *htemp =
new TH1F(hname,
expr,
nbins, xlow, xhigh);
292 htemp->SetTitle(TString(
expr) +
"{" +
cut +
"}");
293 htemp->GetXaxis()->SetTitle(
expr);
301 const char *
cut =
"",
302 const char *drawopt =
"",
303 const char *hname =
"htemp") {
304 if (TString(drawopt).Contains(
"SAME", TString::kIgnoreCase)) {
305 std::cerr <<
"Binning is ignored when 'SAME' is specified." << std::endl;
309 if (strcmp(hname,
"htemp") == 0)
313 htemp->SetTitle(TString(
expr) +
"{" +
cut +
"}");
314 htemp->GetXaxis()->SetTitle(
expr);
320 TProfile *
drawProf(TString xexpr, TString yexpr,
const char *
cut, TString drawopt, TProfile *
hist) {
332 if (
hist ==
nullptr) {
333 std::cerr <<
"Method drawProf(xexpr, yexpr, cut, drawopt, hist) cannot be called with null 'hist'. Use the "
334 "other draw methods instead."
347 const Collection &vals = *
handle_;
348 for (
size_t j = 0,
n = vals.size();
j <
n; ++
j) {
353 if (!strlen(
hist->GetTitle()))
354 hist->SetTitle((strlen(
cut) ? yexpr +
":" + xexpr +
"{" +
cut +
"}" : yexpr +
":" + xexpr));
355 if (!strlen(
hist->GetXaxis()->GetTitle()))
356 hist->GetXaxis()->SetTitle(xexpr);
357 if (!strlen(
hist->GetYaxis()->GetTitle()))
358 hist->GetYaxis()->SetTitle(yexpr);
359 if (!TString(drawopt).Contains(
"GOFF", TString::kIgnoreCase))
366 const char *
cut =
"",
367 TString drawopt =
"",
368 const char *hname =
"htemp",
369 TProfile *htemplate =
nullptr) {
370 TProfile *
hist =
nullptr;
371 if (htemplate !=
nullptr) {
372 if ((strcmp(hname,
"htemp") == 0) && (strcmp(hname, htemplate->GetName()) != 0))
374 hist = (TProfile *)
hist->Clone(hname);
375 }
else if (drawopt.Contains(
"SAME", TString::kIgnoreCase)) {
380 if (
hist ==
nullptr) {
381 if (strcmp(hname,
"htemp") == 0)
383 hist =
new TProfile(hname,
"", gEnv->GetValue(
"Hist.Binning.1D.x", 100), 0., 0.);
384 hist->SetCanExtend(TH1::kAllAxes);
395 const char *
cut =
"",
396 const char *drawopt =
"",
397 const char *hname =
"htemp") {
398 if (TString(drawopt).Contains(
"SAME", TString::kIgnoreCase)) {
399 std::cerr <<
"Binning is ignored when 'SAME' is specified." << std::endl;
401 return drawProf(xexpr, yexpr,
cut, drawopt, hsame);
403 if (strcmp(hname,
"htemp") == 0)
405 TProfile *htemp =
new TProfile(hname,
"",
bins, xlow, xhigh);
406 return drawProf(xexpr, yexpr,
cut, drawopt, htemp);
411 TH2 *
draw2D(TString xexpr, TString yexpr,
const char *
cut, TString drawopt, TH2 *
hist) {
423 if (
hist ==
nullptr) {
424 std::cerr <<
"Method draw2D(xexpr, yexpr, cut, drawopt, hist) cannot be called with null 'hist'. Use the other "
425 "draw methods instead."
438 const Collection &vals = *
handle_;
439 for (
size_t j = 0,
n = vals.size();
j <
n; ++
j) {
444 if (!strlen(
hist->GetTitle()))
445 hist->SetTitle((strlen(
cut) ? yexpr +
":" + xexpr +
"{" +
cut +
"}" : yexpr +
":" + xexpr));
446 if (!strlen(
hist->GetXaxis()->GetTitle()))
447 hist->GetXaxis()->SetTitle(xexpr);
448 if (!strlen(
hist->GetYaxis()->GetTitle()))
449 hist->GetYaxis()->SetTitle(yexpr);
450 if (!TString(drawopt).Contains(
"GOFF", TString::kIgnoreCase))
458 const char *
cut =
"",
459 TString drawopt =
"",
460 const char *hname =
"htemp",
461 TH2 *htemplate =
nullptr) {
463 if (htemplate !=
nullptr) {
464 if ((strcmp(hname,
"htemp") == 0) && (strcmp(hname, htemplate->GetName()) != 0))
467 }
else if (drawopt.Contains(
"SAME", TString::kIgnoreCase)) {
472 if (
hist ==
nullptr) {
483 if (strcmp(hname,
"htemp") == 0)
494 const Collection &vals = *
handle_;
495 for (
size_t j = 0,
n = vals.size();
j <
n; ++
j) {
496 if (!scanner.
test(&vals[
j]))
498 double x = scanner.
eval(&vals[
j], 0);
499 double y = scanner.
eval(&vals[
j], 1);
510 hist =
new TH2F(hname,
512 gEnv->GetValue(
"Hist.Binning.2D.x", 20),
515 gEnv->GetValue(
"Hist.Binning.2D.y", 20),
531 const char *
cut =
"",
532 const char *drawopt =
"",
533 const char *hname =
"htemp") {
534 if (TString(drawopt).Contains(
"SAME", TString::kIgnoreCase)) {
535 std::cerr <<
"Binning is ignored when 'SAME' is specified." << std::endl;
537 return draw2D(xexpr, yexpr,
cut, drawopt, hsame);
539 if (strcmp(hname,
"htemp") == 0)
541 TH2 *htemp =
new TH2F(
"htemp",
"",
xbins, xlow, xhigh, ybins, ylow, yhigh);
542 return draw2D(xexpr, yexpr,
cut, drawopt, htemp);
546 TGraph *
drawGraph(TString xexpr, TString yexpr,
const char *
cut, TString drawopt, TGraph *graph) {
558 if (graph ==
nullptr) {
559 graph =
new TGraph();
560 graph->SetNameTitle(
"htemp", (strlen(
cut) ? yexpr +
":" + xexpr +
"{" +
cut +
"}" : yexpr +
":" + xexpr));
571 const Collection &vals = *
handle_;
572 for (
size_t j = 0,
n = vals.size();
j <
n; ++
j) {
577 if (!strlen(graph->GetTitle()))
578 graph->SetTitle((strlen(
cut) ? yexpr +
":" + xexpr +
"{" +
cut +
"}" : yexpr +
":" + xexpr));
579 if (!strlen(graph->GetXaxis()->GetTitle()))
580 graph->GetXaxis()->SetTitle(xexpr);
581 if (!strlen(graph->GetYaxis()->GetTitle()))
582 graph->GetYaxis()->SetTitle(yexpr);
583 if (!TString(drawopt).Contains(
"GOFF", TString::kIgnoreCase))
584 graph->Draw(drawopt);
590 TString xexpr, TString yexpr,
const char *
cut =
"", TString drawopt =
"AP",
const char *gname =
"htemp") {
591 if (strcmp(gname,
"htemp") == 0)
593 TGraph *graph =
new TGraph();
594 graph->SetNameTitle(gname, (strlen(
cut) ? yexpr +
":" + xexpr +
"{" +
cut +
"}" : yexpr +
":" + xexpr));
606 const char *boolvars,
607 const char *
cut =
"",
608 const char *
name =
"data") {
613 TObjArray *exprArray = TString(realvars).Tokenize(
exprSep_);
614 TObjArray *catArray = TString(boolvars).Tokenize(
exprSep_);
615 int nreals = exprArray->GetEntries();
616 int nbools = catArray->GetEntries();
617 for (
int i = 0;
i < nreals; ++
i) {
618 TString
str = ((TObjString *)(*exprArray)[
i])->GetString();
621 if ((ex[0] ==
'@') && (ex.find(
'=') != std::string::npos)) {
622 lb = lb.substr(1, ex.find(
'=') - 1);
623 ex = ex.substr(ex.find(
'=') + 1);
626 std::cerr <<
"Filed to define real variable '" << lb <<
"', expr = '" << ex <<
"'" << std::endl;
630 RooRealVar *
var =
new RooRealVar(lb.c_str(), lb.c_str(), 0.0);
633 for (
int i = 0;
i < nbools; ++
i) {
634 TString
str = ((TObjString *)(*catArray)[
i])->GetString();
637 if ((ex[0] ==
'@') && (ex.find(
'=') != std::string::npos)) {
638 lb = lb.substr(1, ex.find(
'=') - 1);
639 ex = ex.substr(ex.find(
'=') + 1);
642 std::cerr <<
"Filed to define bool variable '" << lb <<
"', cut = '" << ex <<
"'" << std::endl;
645 RooCategory *
cat =
new RooCategory(lb.c_str(), lb.c_str());
646 cat->defineType(
"fail", 0);
647 cat->defineType(
"pass", 1);
666 const Collection &vals = *
handle_;
667 for (
size_t j = 0,
n = vals.size();
j <
n; ++
j) {
668 if (!scanner.
test(&vals[
j]))
670 for (
int i = 0;
i < nreals; ++
i) {
671 RooRealVar *
var = (RooRealVar *)
vars.at(
i);
674 for (
int i = 0;
i < nbools; ++
i) {
675 RooCategory *
cat = (RooCategory *)
vars.at(
i + nreals);
676 cat->setIndex(
int(scanner.
test(&vals[
j],
i + 1)));
722 fprintf(
stderr,
"Type <CR> to continue or q to quit ==> ");
724 int readch = getchar(),
answer = readch;
726 while (readch !=
'\n' && readch != EOF)
734 TObject *
obj = gDirectory->Get(
"htemp");
743 if (gDirectory && gDirectory->Get(
"htemp") !=
nullptr &&
744 gDirectory->Get(
"htemp")->IsA()->InheritsFrom(
TH1::Class())) {
745 TH1 *
hist = (TH1 *)((TH1 *)gDirectory->Get(
"htemp"))->Clone(hname);
747 hist->SetLineColor(kBlack);
748 hist->SetMarkerColor(kBlack);
751 std::cerr <<
"There is no 'htemp' histogram from which to 'SAME'." << std::endl;
759 if (gDirectory && gDirectory->Get(
"htemp") !=
nullptr &&
760 gDirectory->Get(
"htemp")->IsA()->InheritsFrom(
TH2::Class())) {
761 TH2 *
hist = (TH2 *)((TH2 *)gDirectory->Get(
"htemp"))->Clone(hname);
763 hist->SetLineColor(kBlack);
764 hist->SetMarkerColor(kBlack);
767 std::cerr <<
"There is no 'htemp' histogram from which to 'SAME'." << std::endl;
775 if (gDirectory && gDirectory->Get(
"htemp") !=
nullptr &&
777 TProfile *
hist = (TProfile *)((TProfile *)gDirectory->Get(
"htemp"))->Clone(hname);
779 hist->SetLineColor(kBlack);
780 hist->SetMarkerColor(kBlack);
783 std::cerr <<
"There is no 'htemp' histogram from which to 'SAME'." << std::endl;
789 #endif // PhysicsTools_FWLite_Scanner_h