246 JParser<> zap(
"General purpose plot program for 1D ROOT objects.");
248 zap[
'f'] =
make_field(inputFile,
"<input file>:<object name>");
250 zap[
'w'] =
make_field(canvas,
"size of canvas <nx>x<ny> [pixels]") =
JCanvas(500, 500);
253 zap[
'L'] =
make_field(legend,
"position legend e.g. TR [factor]") = JLegend(), JLegend(
"TL"), JLegend(
"TR"), JLegend(
"BR"), JLegend(
"BL");
254 zap[
'x'] =
make_field(X,
"abscissa range") = JRange_t::DEFAULT_RANGE();
255 zap[
'y'] =
make_field(Y,
"ordinate range") = JRange_t::DEFAULT_RANGE();
256 zap[
'z'] =
make_field(Z,
"ordinate range of projection)") = JRange_t::DEFAULT_RANGE();
257 zap[
'X'] =
make_field(logx,
"logarithmic x-axis (-XX log10 axis)");
258 zap[
'Y'] =
make_field(logy,
"logarithmic y-axis");
259 zap[
'Z'] =
make_field(logz,
"logarithmic y-axis; after projection");
260 zap[
'P'] =
make_field(project,
"projection") =
'\0',
'x',
'X',
'y',
'Y';
261 zap[
'>'] =
make_field(xLabel,
"x-axis label") =
"";
262 zap[
'^'] =
make_field(yLabel,
"y-axis label") =
"";
263 zap[
'C'] =
make_field(drawLine,
"draw line (-C black-and-white -CC colour)");
264 zap[
'F'] =
make_field(fillArea,
"fill area") = 0;
265 zap[
'l'] =
make_field(lineWidth,
"line width") = 2;
266 zap[
'S'] =
make_field(markerSize,
"marker size") = 1.0;
267 zap[
'O'] =
make_field(option,
"plotting option") =
"";
269 zap[
'B'] =
make_field(batch,
"batch processing");
270 zap[
'T'] =
make_field(title,
"graphics title ("
271 <<
"\"" << JName_t <<
"\" -> ROOT name; "
272 <<
"\"" << JTitle_t <<
"\" -> ROOT title)") =
"KM3NeT preliminary";
274 zap[
'g'] =
make_field(group,
"group colour codes of objects") = 1;
275 zap[
't'] =
make_field(xTimeFormat,
"set time format for x-axis, e.g. \%d\\/\%m\\/\\%y%F1970-01-01 00:00:00") =
"";
280 catch(
const exception &error) {
281 FATAL(error.what() << endl);
285 gROOT->SetBatch(batch);
287 TApplication* tp =
new TApplication(
"user", NULL, NULL);
288 TCanvas* cv =
new TCanvas(
"c1",
"c1", canvas.
x, canvas.
y);
290 unique_ptr<TStyle> gStyle(
new JStyle(
"gplot", cv->GetWw(), cv->GetWh(), parameters));
293 gStyle->SetTitleOffset(gStyle->GetTitleOffset() * 1.3,
"Y");
296 gROOT->SetStyle(
"gplot");
299 cv->SetFillStyle(4000);
300 cv->SetFillColor(kWhite);
305 JMarkerAttributes::getInstance().setMarkerSize(markerSize);
306 JLineAttributes ::getInstance().setLineWidth (lineWidth);
309 Double_t xmin = numeric_limits<double>::max();
310 Double_t xmax = numeric_limits<double>::lowest();
312 Double_t ymin = numeric_limits<double>::max();
313 Double_t ymax = numeric_limits<double>::lowest();
318 const bool px = (project ==
'x' || project ==
'X');
319 const bool py = (project ==
'y' || project ==
'Y');
320 const bool pz = (project ==
'z' || project ==
'Z');
322 logy = (logy || logz);
335 for (vector<JRootObjectID>::const_iterator input = inputFile.begin(); input != inputFile.end(); ++input) {
337 DEBUG(
"Input: " << *input << endl);
339 TDirectory* dir = getDirectory(*input);
342 ERROR(
"File: " << input->getFullFilename() <<
" not opened." << endl);
346 const TRegexp regexp(input->getObjectName());
348 TIter iter(dir->GetListOfKeys());
350 for (TKey* key; (key = (TKey*) iter.Next()) != NULL; ) {
352 const TString tag(key->GetName());
354 DEBUG(
"Key: " << tag <<
" match = " << tag.Contains(regexp) << endl);
358 if (tag.Contains(regexp) && isTObject(key)) {
360 if (title == JName_t) {
361 title = key->GetName();
362 }
else if (title == JTitle_t) {
363 title = key->GetTitle();
368 TAttMarker marker = JMarkerAttributes::getInstance().get(0);
369 TAttLine line = JLineAttributes ::getInstance().get(0);
372 marker.SetMarkerColor(JMarkerAttributes::getInstance().get(listOfObjects.size()/group).GetMarkerColor());
376 line = JLineAttributes::getInstance().get(listOfObjects.size());
378 line.SetLineColor(marker.GetMarkerColor());
384 TProfile& h1 =
dynamic_cast<TProfile&
>(*object);
386 object = h1.ProjectionX();
392 TH2& h2 =
dynamic_cast<TH2&
>(*object);
397 object = h2.ProjectionX(
MAKE_CSTRING(h2.GetName() <<
"_px" << LABEL_TERMINATOR << listOfObjects.size()),
398 h2.GetYaxis()->FindBin(Z.getLowerLimit()),
399 h2.GetYaxis()->FindBin(Z.getUpperLimit()) - 1);
401 object = h2.ProjectionX(
MAKE_CSTRING(h2.GetName() <<
"_px" << LABEL_TERMINATOR << listOfObjects.size()),
403 h2.GetYaxis()->GetNbins());
408 object = h2.ProjectionY(
MAKE_CSTRING(h2.GetName() <<
"_py" << LABEL_TERMINATOR << listOfObjects.size()),
409 h2.GetXaxis()->FindBin(Z.getLowerLimit()),
410 h2.GetXaxis()->FindBin(Z.getUpperLimit()) - 1);
412 object = h2.ProjectionY(
MAKE_CSTRING(h2.GetName() <<
"_py" << LABEL_TERMINATOR << listOfObjects.size()),
414 h2.GetXaxis()->GetNbins());
418 ERROR(
"For 2D histograms, use option option -P for projections or use JPlot2D" << endl);
427 TH3& h3 =
dynamic_cast<TH3&
>(*object);
431 object = h3.ProjectionX(
MAKE_CSTRING(h3.GetName() <<
"_px" << LABEL_TERMINATOR << listOfObjects.size()));
435 object = h3.ProjectionY(
MAKE_CSTRING(h3.GetName() <<
"_py" << LABEL_TERMINATOR << listOfObjects.size()));
439 object = h3.ProjectionZ(
MAKE_CSTRING(h3.GetName() <<
"_pz" << LABEL_TERMINATOR << listOfObjects.size()));
443 ERROR(
"For 3D histograms, use option option -P for projections or use JPlot2D -P <projection>" << endl);
453 if (
dynamic_cast<TMarker*
>(
object.get()) == NULL) {
454 dynamic_cast<TAttMarker&
>(*object) = marker;
460 if (
dynamic_cast<TLine*
>(
object.get()) == NULL) {
461 dynamic_cast<TAttLine&
> (*object) = line;
470 TAttFill& fill =
dynamic_cast<TAttFill&
>(*object);
472 if (!isTAttLine(
object) || fillArea < 0) {
473 fill.SetFillColor(marker.GetMarkerColor());
474 fill.SetFillStyle(abs(fillArea));
486 TH1& h1 =
dynamic_cast<TH1&
>(*object);
488 for (
int i = 1; i <= h1.GetNbinsX(); ++i) {
489 h1.SetBinError(i, 0.0);
496 TGraphErrors&
g1 =
dynamic_cast<TGraphErrors&
>(*object);
498 for (Int_t i = 0; i !=
g1.GetN(); ++i) {
510 TH1& h1 =
dynamic_cast<TH1&
>(*object);
512 h1.SetStats(stats != -1);
514 xmin = min(xmin, h1.GetXaxis()->GetXmin());
515 xmax = max(xmax, h1.GetXaxis()->GetXmax());
516 ymin = min(ymin, logy ? h1.GetMinimum(0.0) : h1.GetMinimum());
517 ymax = max(ymax, h1.GetMaximum());
519 if (!logy && h1.GetListOfFunctions() != NULL) {
520 for (unique_ptr<TIterator>
iterator(h1.GetListOfFunctions()->MakeIterator()); TF1* f1 = (TF1*)
iterator->Next(); ) {
521 ymin = min(ymin, f1->GetMinimum(h1.GetXaxis()->GetXmin(), h1.GetXaxis()->GetXmax()));
522 ymax = max(ymax, f1->GetMaximum(h1.GetXaxis()->GetXmin(), h1.GetXaxis()->GetXmax()));
530 TGraph&
g1 =
dynamic_cast<TGraph&
>(*object);
532 for (Int_t i = 0; i !=
g1.GetN(); ++i) {
534 xmin = min(xmin,
g1.GetX()[i]);
535 xmax = max(xmax,
g1.GetX()[i]);
537 if (!logy ||
g1.GetY()[i] > 0.0) {
538 ymin = min(ymin,
g1.GetY()[i]);
539 ymax = max(ymax,
g1.GetY()[i]);
547 TGraphErrors&
g1 =
dynamic_cast<TGraphErrors&
>(*object);
549 for (Int_t i = 0; i !=
g1.GetN(); ++i) {
550 if (!logy ||
g1.GetY()[i] -
g1.GetEY()[i] > 0.0) { ymin = min(ymin,
g1.GetY()[i] -
g1.GetEY()[i]); }
551 if (!logy ||
g1.GetY()[i] +
g1.GetEY()[i] > 0.0) { ymax = max(ymax,
g1.GetY()[i] +
g1.GetEY()[i]); }
558 TMultiGraph& m1 =
dynamic_cast<TMultiGraph&
>(*object);
560 for (TIter i1(m1.GetListOfGraphs()); TGraph*
g1 =
dynamic_cast<TGraph*
>(i1()); ) {
562 for (Int_t i = 0; i !=
g1->GetN(); ++i) {
564 xmin = min(xmin,
g1->GetX()[i]);
565 xmax = max(xmax,
g1->GetX()[i]);
567 if (!logy ||
g1->GetY()[i] > 0.0) {
568 ymin = min(ymin,
g1->GetY()[i]);
569 ymax = max(ymax,
g1->GetY()[i]);
578 TF2& f2 =
dynamic_cast<TF2&
>(*object);
581 TString formula = f2.GetExpFormula();
582 TString _z_ = TString::Format(
"%f", 0.5 * (Z.getLowerLimit() + Z.getUpperLimit()));
589 f2.GetRange(__xmin, __ymin, __xmax, __ymax);
593 formula.ReplaceAll(
"y", _z_);
595 f1 =
new TF1(
MAKE_CSTRING(f2.GetName() <<
"_px" << LABEL_TERMINATOR << listOfObjects.size()), formula);
597 f1->SetRange(__xmin, __xmax);
601 formula.ReplaceAll(
"x", _z_);
602 formula.ReplaceAll(
"y",
"x");
604 f1 =
new TF1(
MAKE_CSTRING(f2.GetName() <<
"_py" << LABEL_TERMINATOR << listOfObjects.size()), formula);
606 f1->SetRange(__ymin, __ymax);
610 ERROR(
"For 2D functions, use option option -P for projections or use JPlot2D" << endl);
615 DEBUG(
"TF1: " << f1->GetExpFormula() << endl);
617 f1->SetParameters(f2.GetParameters());
625 TF1& f1 =
dynamic_cast<TF1&
>(*object);
630 f1.GetRange(__xmin, __xmax);
632 xmin = min(xmin, __xmin);
633 xmax = max(xmax, __xmax);
634 ymin = min(ymin, f1.GetMinimum());
635 ymax = max(ymax, f1.GetMaximum());
641 THStack& hs =
dynamic_cast<THStack&
>(*object);
643 NOTICE(
"THStack" << endl);
645 unique_ptr<TIterator>
iterator(hs.GetHists()->MakeIterator());
649 TH1& h1 =
dynamic_cast<TH1&
>(*i);
651 NOTICE(
"TH1[" << index <<
"] " << h1.GetName() << endl);
653 xmin = min(xmin, h1.GetXaxis()->GetXmin());
654 xmax = max(xmax, h1.GetXaxis()->GetXmax());
656 ymin = min(ymin, logy ? h1.GetMinimum(0.0) : h1.GetMinimum());
657 ymax = max(ymax, h1.GetMaximum());
660 h1.SetLineColor(kBlack);
662 h1.SetFillColor(JMarkerAttributes::getInstance().get(index).GetMarkerColor());
669 TLine& h1 =
dynamic_cast<TLine&
>(*object);
671 xmin = min(xmin, h1.GetX1());
672 xmax = max(xmax, h1.GetX2());
673 ymin = min(ymin, h1.GetY1());
674 ymax = max(ymax, h1.GetY2());
680 for (TString buffer[] = {
object.getLabel(), input->getFilename().c_str(),
"" }, *i = buffer; *i !=
""; ++i) {
682 *i = (*i)(TRegexp(
"\\[.*\\]"));
684 if (i->Length() > 2) {
685 object.setLabel((*i)(1, i->Length() - 2));
689 DEBUG(
"Add object: " << tag <<
" with label <" <<
object.getLabel() <<
">" << endl);
691 if (master == NULL) {
692 master =
dynamic_cast<TH1*
>(
object.get());
695 listOfObjects.push_back(
object);
700 if (listOfObjects.empty()) {
701 ERROR(
"Nothing to draw." << endl);
710 TH1* h1 =
dynamic_cast<TH1*
> (i->get());
711 TGraph*
g1 =
dynamic_cast<TGraph*
> (i->get());
712 TMultiGraph* m1 =
dynamic_cast<TMultiGraph*
>(i->get());
713 TAttLine*
ls =
dynamic_cast<TAttLine*
> (i->get());
721 iterator.reset(h1->GetListOfFunctions()->MakeIterator());
723 }
else if (
g1 != NULL) {
725 iterator.reset(
g1->GetListOfFunctions()->MakeIterator());
727 }
else if (m1 != NULL) {
729 for (TIter i1(m1->GetListOfGraphs()); TGraph* gi =
dynamic_cast<TGraph*
>(i1()); ) {
730 for (TIter i2(gi->GetListOfFunctions()); TF1* fi =
dynamic_cast<TF1*
>(i2()); ) {
740 Double_t x1[] = { numeric_limits<Double_t>::max(), numeric_limits<Double_t>::max() };
741 Double_t x2[] = { numeric_limits<Double_t>::lowest(), numeric_limits<Double_t>::lowest() };
743 for (
int nf = 0, ns = 0, nc = 1; TF1* f1 = (TF1*)
iterator->Next(); ++nf) {
745 f1->GetRange(x1[1], x2[1]);
748 dynamic_cast<TAttLine&
>(*f1) = JLineAttributes::getInstance().get(0);
750 if (listOfObjects.size() == 1) {
752 if (x1[0] == x1[1] &&
758 f1->SetLineStyle(JLineAttributes ::getInstance().get(ns).GetLineStyle());
759 f1->SetLineColor(JMarkerAttributes::getInstance().get(nc).GetMarkerColor());
765 f1->SetLineColor(
ls->GetLineColor());
766 f1->SetLineStyle(JLineAttributes::getInstance().get(ns++).GetLineStyle());
789 xmin = X.getLowerLimit();
790 xmax = X.getUpperLimit();
794 ymin = Y.getLowerLimit();
795 ymax = Y.getUpperLimit();
796 }
else if (ymax > ymin) {
797 setRange(ymin, ymax, logy);
802 if (!listOfObjects.empty()) {
804 if (master == NULL) {
806 master =
new TH1D(MASTER.c_str(), NULL, 1000, xmin, xmax);
808 master->SetStats(kFALSE);
810 for (Int_t i = 1; i <= master->GetXaxis()->GetNbins(); ++i) {
811 master->SetBinContent(i, ymin);
816 if (master == NULL) {
818 TText* p =
new TText(0.5, 0.5,
MAKE_CSTRING(
"No data"));
826 if (logx) { gPad->SetLogx(); }
827 if (logy) { gPad->SetLogy(); }
829 master->SetTitle(title.c_str());
831 master->GetXaxis()->SetRangeUser(xmin, xmax);
833 if (logx > 1) { setLogarithmicX(master); }
835 if (logx > 2) { master->GetXaxis()->SetNoExponent(); }
837 master->SetMinimum(ymin);
838 master->SetMaximum(ymax);
840 if (xLabel !=
"") { master->GetXaxis()->SetTitle(xLabel.c_str()); master->GetXaxis()->CenterTitle(
true); }
841 if (yLabel !=
"") { master->GetYaxis()->SetTitle(yLabel.c_str()); master->GetYaxis()->CenterTitle(
true); }
843 master->GetXaxis()->SetMoreLogLabels((logx == 1 && log10(xmax/xmin) < 2) ||
844 (logx > 1 && (xmax-xmin) < 2));
846 master->GetYaxis()->SetMoreLogLabels( logy && log10(ymax/ymin) < 2);
847 master->GetYaxis()->SetNoExponent ( logy && log10(ymax/ymin) < 2);
850 master->SetNdivisions(i->second, i->first.c_str());
853 if (xTimeFormat !=
"") {
855 master->GetXaxis()->SetTimeDisplay(1);
857 if (xTimeFormat ==
"utc") {
858 master->GetXaxis()->SetTimeFormat(
"#splitline{}{#splitline{%d-%m-%y}{ %H:%M}}");
859 master->GetXaxis()->SetTimeOffset(0.0,
"gmt");
860 }
else if (xTimeFormat ==
"UTC") {
861 master->GetXaxis()->SetTimeFormat(
"%d-%m-%y");
862 master->GetXaxis()->SetTimeOffset(0.0,
"gmt");
864 master->GetXaxis()->SetTimeFormat(xTimeFormat.c_str());
868 master->Draw(option.c_str());
873 if (
dynamic_cast<TH1*
>(i->get()) != master) {
874 if (setLogX<TH1> (i->get())) {}
875 else if (setLogX<TF1> (i->get())) {}
876 else if (setLogX<TGraphErrors>(i->get())) {}
877 else if (setLogX<TGraph> (i->get())) {}
878 else if (setLogX<TMultiGraph> (i->get())) {}
879 else if (setLogX<TLine> (i->get())) {}
880 else if (setLogX<TEllipse> (i->get())) {}
885 if (grid.count(
'x') || grid.count(
'X')) { gPad->SetGridx(); }
886 if (grid.count(
'y') || grid.count(
'Y')) { gPad->SetGridy(); }
889 gStyle->SetOptStat(stats);
891 gStyle->SetOptFit(kFALSE);
894 for (vector<JRootObject>::const_iterator i = listOfObjects.begin(); i != listOfObjects.end(); ++i) {
896 DEBUG(
"Draw " << (*i)->GetName() <<
' ' << (*i)->GetTitle() << endl);
898 string buffer(option);
906 TF1* f1 =
dynamic_cast<TF1*
> (i->get());
907 TGraph*
g1 =
dynamic_cast<TGraph*
> (i->get());
908 TMultiGraph* q1 =
dynamic_cast<TMultiGraph*
>(i->get());
915 if (
g1->GetN() > 1 && drawLine)
917 else if (fillArea == 0 ||
918 (
dynamic_cast<TGraphErrors*
> (
g1) == NULL &&
919 dynamic_cast<TGraphAsymmErrors*
>(
g1) == NULL))
925 for (TIter i1(q1->GetListOfGraphs()); TGraph* gi =
dynamic_cast<TGraph*
>(i1()); ) {
927 string zbuf = buffer;
929 if (gi->GetN() > 1 && drawLine)
934 gi->Draw(zbuf.c_str());
939 (*i).Draw(buffer.c_str());
945 if (legend.is_valid()) {
948 group = listOfObjects.size();
951 Ssiz_t height = listOfObjects.size()/group;
954 for (vector<JRootObject>::const_iterator i = listOfObjects.begin(); i != listOfObjects.end(); ++i) {
955 width = max(width, getWidth(i->getLabel()));
958 TLegend* lg = getLegend(width, height, legend.location, legend.factor);
960 for (
size_t i = 0; i < listOfObjects.size(); i += group) {
964 if (
dynamic_cast<TMarker*
>(
object.get()) == NULL &&
965 dynamic_cast<TLine*
> (
object.get()) == NULL &&
966 dynamic_cast<TText*
> (
object.get()) == NULL) {
967 lg->AddEntry(
object,
" " +
object.getLabel(), isTAttLine(
object) ?
"L" : isTAttFill(
object) && fillArea != 0 ?
"F" :
"P");
984 return (master != NULL ? 0 : 1);