11 #include "TApplication.h"
15 #include "TAttMarker.h"
23 #include "TGraphErrors.h"
24 #include "TMultiGraph.h"
48 const char*
const JName_t =
"?";
49 const char*
const JTitle_t =
"%";
59 int main(
int argc,
char **argv)
64 typedef JRange<double> JRange_t;
95 JParser<> zap(
"General purpose plot program for 1D ROOT objects.");
97 zap[
'f'] =
make_field(inputFile,
"<input file>:<object name>");
99 zap[
'w'] =
make_field(canvas,
"size of canvas <nx>x<ny> [pixels]") = JCanvas(500, 500);
101 zap[
'L'] =
make_field(legend,
"position legend e.g. TR") =
"";
102 zap[
'x'] =
make_field(X,
"abscissa range") = JRange_t();
103 zap[
'y'] =
make_field(
Y,
"ordinate range") = JRange_t();
104 zap[
'z'] =
make_field(Z,
"ordinate range of projection)") = JRange_t();
105 zap[
'X'] =
make_field(logx,
"logarithmic x-axis (-XX log10 axis)");
106 zap[
'Y'] =
make_field(logy,
"logarithmic y-axis");
107 zap[
'Z'] =
make_field(logz,
"logarithmic y-axis; after projection");
108 zap[
'P'] =
make_field(project,
"projection") =
'\0',
'x',
'X',
'y',
'Y';
109 zap[
'>'] =
make_field(xLabel,
"x-axis label") =
"";
110 zap[
'^'] =
make_field(yLabel,
"y-axis label") =
"";
113 zap[
'l'] =
make_field(lineWidth,
"line width") = 2;
114 zap[
'S'] =
make_field(markerSize,
"marker size") = 1.0;
115 zap[
'O'] =
make_field(option,
"plotting option") =
"";
117 zap[
'B'] =
make_field(batch,
"batch processing");
118 zap[
'T'] =
make_field(title,
"title") =
"KM3NeT preliminary";
120 zap[
'g'] =
make_field(group,
"group colour codes of objects") = 1;
121 zap[
't'] =
make_field(xTimeFormat,
"set time format for x-axis, e.g. \%d\\/\%m\\/\\%y%F1970-01-01 00:00:00") =
"";
126 catch(
const exception &error) {
127 FATAL(error.what() << endl);
131 gROOT->SetBatch(batch);
133 TApplication* tp =
new TApplication(
"user", NULL, NULL);
134 TCanvas* cv =
new TCanvas(
"c1",
"c1", canvas.x, canvas.y);
136 JSinglePointer<TStyle> gStyle(
new JStyle(
"gplot", cv->GetWw(), cv->GetWh()));
138 gROOT->SetStyle(
"gplot");
142 cv->SetFillStyle(4000);
143 cv->SetFillColor(kWhite);
152 Double_t xmin = numeric_limits<double>::max();
153 Double_t xmax = numeric_limits<double>::lowest();
155 Double_t ymin = numeric_limits<double>::max();
156 Double_t ymax = numeric_limits<double>::lowest();
161 const bool px = (project ==
'x' || project ==
'X');
162 const bool py = (project ==
'y' || project ==
'Y');
163 const bool pz = (project ==
'z' || project ==
'Z');
165 logy = (logy || logz);
180 DEBUG(
"Input: " << *input << endl);
185 ERROR(
"File: " << input->getFullFilename() <<
" not opened." << endl);
189 const TRegexp regexp(input->getObjectName());
191 TIter iter(dir->GetListOfKeys());
193 for (TKey* key; (key = (TKey*) iter.Next()) != NULL; ) {
195 const TString tag(key->GetName());
197 DEBUG(
"Key: " << tag <<
" match = " << tag.Contains(regexp) << endl);
201 if (tag.Contains(regexp)) {
203 if (title == JName_t) {
204 title = key->GetName();
205 }
else if (title == JTitle_t) {
206 title = key->GetTitle();
209 JRootObject object(key->ReadObj());
222 line.SetLineColor(marker.GetMarkerColor());
226 TProfile&
h1 =
dynamic_cast<TProfile&
>(*object);
228 object = h1.ProjectionX();
234 TH2& h2 =
dynamic_cast<TH2&
>(*object);
240 h2.GetYaxis()->FindBin(Z.getLowerLimit()),
241 h2.GetYaxis()->FindBin(Z.getUpperLimit()) - 1);
245 h2.GetYaxis()->GetNbins());
251 h2.GetXaxis()->FindBin(Z.getLowerLimit()),
252 h2.GetXaxis()->FindBin(Z.getUpperLimit()) - 1);
256 h2.GetXaxis()->GetNbins());
260 ERROR(
"For 2D histograms, use option option -P for projections or use JPlot2D" << endl);
269 TH3& h3 =
dynamic_cast<TH3&
>(*object);
285 ERROR(
"For 3D histograms, use option option -P for projections or use JPlot2D -P <projection>" << endl);
294 dynamic_cast<TAttMarker&
>(*object) = marker;
300 dynamic_cast<TAttLine&
> (*object) = line;
306 TH1&
h1 =
dynamic_cast<TH1&
>(*object);
308 h1.SetStats(stats != -1);
310 xmin = min(xmin, h1.GetXaxis()->GetXmin());
311 xmax = max(xmax, h1.GetXaxis()->GetXmax());
312 ymin = min(ymin, logy ? h1.GetMinimum(0.0) : h1.GetMinimum());
313 ymax = max(ymax, h1.GetMaximum());
316 for (
int i = 1; i <= h1.GetNbinsX(); ++i) {
317 h1.SetBinError(i, 0.0);
327 TAttFill& fill =
dynamic_cast<TAttFill&
>(*object);
329 fill.SetFillColor(marker.GetMarkerColor());
336 TGraph&
g1 =
dynamic_cast<TGraph&
>(*object);
338 for (Int_t i = 0; i != g1.GetN(); ++i) {
340 xmin = min(xmin, g1.GetX()[i]);
341 xmax = max(xmax, g1.GetX()[i]);
343 if (!logy || g1.GetY()[i] > 0.0) {
344 ymin = min(ymin, g1.GetY()[i]);
345 ymax = max(ymax, g1.GetY()[i]);
353 TGraphErrors&
g1 =
dynamic_cast<TGraphErrors&
>(*object);
356 for (Int_t i = 0; i != g1.GetN(); ++i) {
361 for (Int_t i = 0; i != g1.GetN(); ++i) {
362 if (!logy || g1.GetY()[i] - g1.GetEY()[i] > 0.0) { ymin = min(ymin, g1.GetY()[i] - g1.GetEY()[i]); }
363 if (!logy || g1.GetY()[i] + g1.GetEY()[i] > 0.0) { ymax = max(ymax, g1.GetY()[i] + g1.GetEY()[i]); }
371 TMultiGraph& m1 =
dynamic_cast<TMultiGraph&
>(*object);
373 for (TIter i1(m1.GetListOfGraphs()); TGraph*
g1 =
dynamic_cast<TGraph*
>(i1()); ) {
375 for (Int_t i = 0; i != g1->GetN(); ++i) {
377 xmin = min(xmin, g1->GetX()[i]);
378 xmax = max(xmax, g1->GetX()[i]);
380 if (!logy || g1->GetY()[i] > 0.0) {
381 ymin = min(ymin, g1->GetY()[i]);
382 ymax = max(ymax, g1->GetY()[i]);
391 TF1& f1 =
dynamic_cast<TF1&
>(*object);
396 f1.GetRange(__xmin, __xmax);
398 xmin = min(xmin, __xmin);
399 xmax = max(xmax, __xmax);
400 ymin = min(ymin, f1.GetMinimum());
401 ymax = max(ymax, f1.GetMaximum());
408 THStack& hs =
dynamic_cast<THStack&
>(*object);
410 NOTICE(
"THStack" << endl);
412 TIterator*
iterator = hs.GetHists()->MakeIterator();
414 for (
size_t index = 1;
TObject* i = iterator->Next(); ++index) {
416 TH1&
h1 =
dynamic_cast<TH1&
>(*i);
418 NOTICE(
"TH1[" << index <<
"] " << h1.GetName() << endl);
420 xmin = min(xmin, h1.GetXaxis()->GetXmin());
421 xmax = max(xmax, h1.GetXaxis()->GetXmax());
423 ymin = min(ymin, logy ? h1.GetMinimum(0.0) : h1.GetMinimum());
424 ymax = max(ymax, h1.GetMaximum());
427 h1.SetLineColor(kBlack);
434 for (TString buffer[] = {
object.getLabel(), input->getFilename().c_str(),
"" }, *i = buffer; *i !=
""; ++i) {
436 *i = (*i)(TRegexp(
"\\[.*\\]"));
438 DEBUG(
"Label: <" << *i <<
">" << endl);
440 if (i->Length() > 2) {
441 object.setLabel((*i)(1, i->Length() - 2));
445 DEBUG(
"Add object: " << tag <<
" with label " <<
object.
getLabel() << endl);
448 master =
dynamic_cast<TH1*
>(
object.get());
451 listOfObjects.push_back(
object);
456 if (listOfObjects.empty()) {
457 ERROR(
"Nothing to draw." << endl);
462 TH1*
h1 =
dynamic_cast<TH1*
> (i->get());
463 TGraph*
g1 =
dynamic_cast<TGraph*
> (i->get());
464 TMultiGraph* m1 =
dynamic_cast<TMultiGraph*
>(i->get());
465 TAttLine* ls =
dynamic_cast<TAttLine*
> (i->get());
472 iterator = h1->GetListOfFunctions()->MakeIterator();
474 }
else if (g1 != NULL) {
476 iterator = g1->GetListOfFunctions()->MakeIterator();
478 }
else if (m1 != NULL) {
480 for (TIter i1(m1->GetListOfGraphs()); TGraph* gi =
dynamic_cast<TGraph*
>(i1()); ) {
481 for (TIter i2(gi->GetListOfFunctions()); TF1* fi =
dynamic_cast<TF1*
>(i2()); ) {
486 iterator = list.MakeIterator();
489 if (iterator != NULL) {
491 Double_t x1 = numeric_limits<Double_t>::max();
492 Double_t x2 = numeric_limits<Double_t>::lowest();
494 for (
int ns = 0, nc = 1; TF1* f1 = (TF1*) iterator->Next(); ) {
499 f1->GetRange(__x1, __x2);
504 if (listOfObjects.size() == 1) {
508 if (x1 != numeric_limits<Double_t>::max() &&
509 x2 != numeric_limits<Double_t>::lowest()) {
524 f1->SetLineColor(ls->GetLineColor());
531 f1->GetRange(__xmin, __xmax);
533 ymin = min(ymin, f1->GetMinimum(__xmin, __xmax));
534 ymax = max(ymax, f1->GetMaximum(__xmin, __xmax));
544 if (X != JRange_t()) {
545 xmin = X.getLowerLimit();
546 xmax = X.getUpperLimit();
549 if (
Y != JRange_t()) {
550 ymin =
Y.getLowerLimit();
551 ymax =
Y.getUpperLimit();
558 if (!listOfObjects.empty()) {
560 if (
master == NULL ||
master->GetXaxis()->GetXmin() > xmin ||
master->GetXaxis()->GetXmax() < xmax) {
562 if (X != JRange_t()) {
564 master =
new TH1D(
"__H__", NULL, 100, X.getLowerLimit(), X.getUpperLimit());
568 }
else if (xmin < xmax) {
570 master =
new TH1D(
"__H__", NULL, 100, xmin, xmax);
579 TText* p =
new TText(0.5, 0.5,
MAKE_CSTRING(
"No data"));
587 if (logx) { gPad->SetLogx(); }
588 if (logy) { gPad->SetLogy(); }
590 master->GetXaxis()->SetRangeUser(xmin, xmax);
596 master->SetTitle(title.c_str());
601 if (xLabel !=
"") {
master->GetXaxis()->SetTitle(xLabel.c_str());
master->GetXaxis()->CenterTitle(
true); }
602 if (yLabel !=
"") {
master->GetYaxis()->SetTitle(yLabel.c_str());
master->GetYaxis()->CenterTitle(
true); }
604 master->GetXaxis()->SetMoreLogLabels((logx == 1 && log10(xmax/xmin) < 2) ||
605 (logx > 1 && (xmax-xmin) < 2));
607 master->GetYaxis()->SetMoreLogLabels( logy && log10(ymax/ymin) < 2);
608 master->GetYaxis()->SetNoExponent ( logy && log10(ymax/ymin) < 2);
610 if (Ndivisions.first !=
"") {
611 master->SetNdivisions(Ndivisions.second, Ndivisions.first.c_str());
614 if (xTimeFormat !=
"") {
616 master->GetXaxis()->SetTimeDisplay(1);
618 if (xTimeFormat ==
"utc") {
619 master->GetXaxis()->SetTimeFormat(
"#splitline{}{#splitline{%d-%m-%y}{ %H:%M}}");
620 master->GetXaxis()->SetTimeOffset(0.0,
"gmt");
621 }
else if (xTimeFormat ==
"UTC") {
622 master->GetXaxis()->SetTimeFormat(
"%d-%m-%y");
623 master->GetXaxis()->SetTimeOffset(0.0,
"gmt");
625 master->GetXaxis()->SetTimeFormat(xTimeFormat.c_str());
629 master->Draw(option.c_str());
634 if (dynamic_cast<TH1*>(i->get()) !=
master) {
643 if (grid.count(
'x') || grid.count(
'X')) { gPad->SetGridx(); }
644 if (grid.count(
'y') || grid.count(
'Y')) { gPad->SetGridy(); }
647 gStyle->SetOptStat(stats);
649 gStyle->SetOptFit(kFALSE);
654 DEBUG(
"Draw " << (*i)->GetName() <<
' ' << (*i)->GetTitle() << endl);
656 string buffer(option);
664 TF1* f1 =
dynamic_cast<TF1*
> (i->get());
665 TGraph*
g1 =
dynamic_cast<TGraph*
> (i->get());
666 TMultiGraph* q1 =
dynamic_cast<TMultiGraph*
>(i->get());
673 if (g1->GetN() > 1 && drawLine)
681 for (TIter i1(q1->GetListOfGraphs()); TGraph* gi =
dynamic_cast<TGraph*
>(i1()); ) {
683 string zbuf = buffer;
685 if (gi->GetN() > 1 && drawLine)
690 gi->Draw(zbuf.c_str());
695 (*i).Draw(buffer.c_str());
703 Ssiz_t height = listOfObjects.size();
707 width = max(width, i->getLabel().Length());
710 TLegend* lg =
getLegend(width, height, legend);
713 lg->AddEntry(*i,
" " + i->getLabel(),
isTAttLine(*i) ?
"L" :
"LPF");
729 return (
master != NULL ? 0 : 1);
Utility class to parse command line options.
int main(int argc, char *argv[])
void setLogarithmicX(TF1 *f1)
Make parameter x of function logarithmic (e.g. after filling with log10()).
std::string getLabel(const JLocation &location)
Get module label for monitoring and other applications.
then echo Test string reversed by master(hit< return > to continue)." JProcess -c "JEcho" -rC fi if (( 1 ))
#define MAKE_CSTRING(A)
Make C-string.
then for HISTOGRAM in h0 h1
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
then fatal Wrong number of arguments fi set_variable STRING $argv[1] set_variable DETECTORXY_TXT $WORKDIR $DETECTORXY_TXT tail read X Y CHI2 RMS printf optimum n $X $Y $CHI2 $RMS awk v Y
bool isTAttLine(const TObject *object)
Get drawing option of TH1.
I/O formatting auxiliaries.
T & getInstance(const T &object)
Get static instance from temporary object.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
General purpose messaging.
static const char LABEL_TERMINATOR
label terminator
void setRange(double &xmin, double &xmax, const bool logx)
Set axis range.
Auxiliary class to define a range between two values.
Utility class to parse command line options.
TLegend * getLegend(const Int_t width, const Int_t height, const std::string option="TR")
Get legend.
Auxiliary class to handle multiple boolean-like I/O.
TDirectory * getDirectory(const JRootObjectID &id)
Get TDirectory pointer.
#define DEBUG(A)
Message macros.
Double_t g1(const Double_t x)
Function.