11 #include "TApplication.h"
15 #include "TAttMarker.h"
22 #include "TGraphErrors.h"
23 #include "TMultiGraph.h"
47 const char*
const JName_t =
"?";
48 const char*
const JTitle_t =
"%";
58 int main(
int argc,
char **argv)
94 JParser<> zap(
"General purpose plot program for 1D ROOT objects.");
96 zap[
'f'] =
make_field(inputFile,
"<input file>:<object name>");
98 zap[
'w'] =
make_field(canvas,
"size of canvas <nx>x<ny> [pixels]") = JCanvas(500, 500);
100 zap[
'L'] =
make_field(legend,
"position legend e.g. TR") =
"";
104 zap[
'X'] =
make_field(logx,
"logarithmic x-axis (-XX log10 axis)");
105 zap[
'Y'] =
make_field(logy,
"logarithmic y-axis");
106 zap[
'Z'] =
make_field(logz,
"logarithmic y-axis; after projection");
107 zap[
'P'] =
make_field(project,
"projection") =
'\0',
'x',
'X',
'y',
'Y';
108 zap[
'>'] =
make_field(xLabel,
"x-axis label") =
"";
109 zap[
'^'] =
make_field(yLabel,
"y-axis label") =
"";
112 zap[
'l'] =
make_field(lineWidth,
"line width") = 2;
113 zap[
'S'] =
make_field(markerSize,
"marker size") = 1.0;
114 zap[
'O'] =
make_field(option,
"plotting option") =
"";
116 zap[
'B'] =
make_field(batch,
"batch processing");
117 zap[
'T'] =
make_field(title,
"title") =
"KM3NeT preliminary";
119 zap[
'g'] =
make_field(group,
"group colour codes of objects") = 1;
120 zap[
't'] =
make_field(xTimeFormat,
"set time format for x-axis, e.g. \%d\\/\%m\\/\\%y%F1970-01-01 00:00:00") =
"";
125 catch(
const exception &error) {
126 FATAL(error.what() << endl);
130 gROOT->SetBatch(batch);
132 TApplication* tp =
new TApplication(
"user", NULL, NULL);
133 TCanvas* cv =
new TCanvas(
"c1",
"c1", canvas.x, canvas.y);
135 JSinglePointer<TStyle> gStyle(
new JStyle(
"gplot", cv->GetWw(), cv->GetWh()));
137 gROOT->SetStyle(
"gplot");
141 cv->SetFillStyle(4000);
142 cv->SetFillColor(kWhite);
151 Double_t xmin = +numeric_limits<double>::max();
152 Double_t xmax = -numeric_limits<double>::max();
154 Double_t ymin = +numeric_limits<double>::max();
155 Double_t ymax = -numeric_limits<double>::max();
160 const bool px = (project ==
'x' || project ==
'X');
161 const bool py = (project ==
'y' || project ==
'Y');
163 logy = (logy || logz);
178 DEBUG(
"Input: " << *input << endl);
183 ERROR(
"File: " << input->getFullFilename() <<
" not opened." << endl);
187 const TRegexp regexp(input->getObjectName());
189 TIter iter(dir->GetListOfKeys());
191 for (TKey* key; (key = (TKey*) iter.Next()) != NULL; ) {
193 const TString tag(key->GetName());
195 DEBUG(
"Key: " << tag <<
" match = " << tag.Contains(regexp) << endl);
199 if (tag.Contains(regexp)) {
201 if (title == JName_t) {
202 title = key->GetName();
203 }
else if (title == JTitle_t) {
204 title = key->GetTitle();
207 JRootObject object(key->ReadObj());
220 line.SetLineColor(marker.GetMarkerColor());
224 TProfile&
h1 =
dynamic_cast<TProfile&
>(*object);
226 object = h1.ProjectionX();
232 TH2& h2 =
dynamic_cast<TH2&
>(*object);
238 h2.GetYaxis()->FindBin(Z.getLowerLimit()),
239 h2.GetYaxis()->FindBin(Z.getUpperLimit()) - 1);
243 h2.GetYaxis()->GetNbins());
249 h2.GetXaxis()->FindBin(Z.getLowerLimit()),
250 h2.GetXaxis()->FindBin(Z.getUpperLimit()) - 1);
254 h2.GetXaxis()->GetNbins());
258 ERROR(
"For 2D histograms, use option option -P for projections or use JPlot2D" << endl);
267 dynamic_cast<TAttMarker&
>(*object) = marker;
273 dynamic_cast<TAttLine&
> (*object) = line;
279 TH1&
h1 =
dynamic_cast<TH1&
>(*object);
281 h1.SetStats(stats != -1);
283 xmin = min(xmin, h1.GetXaxis()->GetXmin());
284 xmax = max(xmax, h1.GetXaxis()->GetXmax());
286 ymin = min(ymin, logy ? h1.GetMinimum(0.0) : h1.GetMinimum());
287 ymax = max(ymax, h1.GetMaximum());
290 for (
int i = 1; i <= h1.GetNbinsX(); ++i) {
291 h1.SetBinError(i, 0.0);
301 TAttFill& fill =
dynamic_cast<TAttFill&
>(*object);
303 fill.SetFillColor(marker.GetMarkerColor());
310 TGraph&
g1 =
dynamic_cast<TGraph&
>(*object);
312 for (Int_t i = 0; i != g1.GetN(); ++i) {
314 xmin = min(xmin, g1.GetX()[i]);
315 xmax = max(xmax, g1.GetX()[i]);
317 if (!logy || g1.GetY()[i] > 0.0) {
318 ymin = min(ymin, g1.GetY()[i]);
319 ymax = max(ymax, g1.GetY()[i]);
327 TGraphErrors&
g1 =
dynamic_cast<TGraphErrors&
>(*object);
330 for (Int_t i = 0; i != g1.GetN(); ++i) {
340 TMultiGraph& mg =
dynamic_cast<TMultiGraph&
>(*object);
342 TIter next(mg.GetListOfGraphs());
344 while (TGraph*
g1 = dynamic_cast<TGraph*>(next())) {
346 for (Int_t
j = 0;
j !=
g1->GetN(); ++
j) {
348 xmin = min(xmin,
g1->GetX()[
j]);
349 xmax = max(xmax,
g1->GetX()[
j]);
351 if (!logy ||
g1->GetY()[
j] > 0.0) {
352 ymin = min(ymin,
g1->GetY()[
j]);
353 ymax = max(ymax,
g1->GetY()[
j]);
362 TF1& f1 =
dynamic_cast<TF1&
>(*object);
364 if (xmin != +numeric_limits<double>::max() &&
365 xmax != -numeric_limits<double>::max()) {
366 ymin = min(ymin, f1.GetMinimum(xmin, xmax));
367 ymax = max(ymax, f1.GetMaximum(xmin, xmax));
375 THStack& hs =
dynamic_cast<THStack&
>(*object);
377 NOTICE(
"THStack" << endl);
379 TIterator*
iterator = hs.GetHists()->MakeIterator();
381 for (
size_t index = 1;
TObject* i = iterator->Next(); ++index) {
383 TH1&
h1 =
dynamic_cast<TH1&
>(*i);
385 NOTICE(
"TH1[" << index <<
"] " << h1.GetName() << endl);
387 xmin = min(xmin, h1.GetXaxis()->GetXmin());
388 xmax = max(xmax, h1.GetXaxis()->GetXmax());
390 ymin = min(ymin, logy ? h1.GetMinimum(0.0) : h1.GetMinimum());
391 ymax = max(ymax, h1.GetMaximum());
394 h1.SetLineColor(kBlack);
401 for (TString buffer[] = {
object.getLabel(), input->getFilename().c_str(),
"" }, *i = buffer; *i !=
""; ++i) {
403 *i = (*i)(TRegexp(
"\\[.*\\]"));
405 DEBUG(
"Label: <" << *i <<
">" << endl);
407 if (i->Length() > 2) {
408 object.setLabel((*i)(1, i->Length() - 2));
412 DEBUG(
"Add object: " << tag <<
" with label " <<
object.
getLabel() << endl);
415 master =
dynamic_cast<TH1*
>(
object.get());
418 listOfObjects.push_back(
object);
423 if (listOfObjects.empty()) {
424 ERROR(
"Nothing to draw." << endl);
429 TH1*
h1 =
dynamic_cast<TH1*
> (i->get());
430 TGraph*
g1 =
dynamic_cast<TGraph*
> (i->get());
431 TMultiGraph* mg =
dynamic_cast<TMultiGraph*
>(i->get());
432 TAttLine* ls =
dynamic_cast<TAttLine*
> (i->get());
438 iterator = h1->GetListOfFunctions()->MakeIterator();
440 iterator = g1->GetListOfFunctions()->MakeIterator();
441 else if (mg != NULL) {
443 TIter next1(mg->GetListOfGraphs());
445 while (TGraph* gi = dynamic_cast<TGraph*>(next1())) {
447 TIter next2(gi->GetListOfFunctions());
449 while (TF1 *fi = dynamic_cast<TF1*>(next2())) {
454 iterator = list.MakeIterator();
459 Double_t x1 = +numeric_limits<Double_t>::max();
460 Double_t x2 = -numeric_limits<Double_t>::max();
462 for (
int ns = 0, nc = 1; TF1* f1 = (TF1*) iterator->Next(); ) {
467 f1->GetRange(__x1, __x2);
472 if (listOfObjects.size() == 1) {
476 if (x1 != +numeric_limits<Double_t>::max() &&
477 x2 != -numeric_limits<Double_t>::max()) {
492 f1->SetLineColor(ls->GetLineColor());
496 ymin = min(ymin, f1->GetMinimum(xmin, xmax));
497 ymax = max(ymax, f1->GetMaximum(xmin, xmax));
508 xmin = X.getLowerLimit();
509 xmax = X.getUpperLimit();
513 ymin = Y.getLowerLimit();
514 ymax = Y.getUpperLimit();
525 master =
new TH1D(
"__H__", NULL, 100, X.getLowerLimit(), X.getUpperLimit());
529 }
else if (xmin < xmax) {
531 master =
new TH1D(
"__H__", NULL, 100, xmin, xmax);
537 TText* p =
new TText(0.5, 0.5,
MAKE_CSTRING(
"No data"));
547 if (logx) { gPad->SetLogx(); }
548 if (logy) { gPad->SetLogy(); }
550 master->GetXaxis()->SetRangeUser(xmin, xmax);
556 master->SetTitle(title.c_str());
561 if (xLabel !=
"") {
master->GetXaxis()->SetTitle(xLabel.c_str());
master->GetXaxis()->CenterTitle(
true); }
562 if (yLabel !=
"") {
master->GetYaxis()->SetTitle(yLabel.c_str());
master->GetYaxis()->CenterTitle(
true); }
564 master->GetXaxis()->SetMoreLogLabels((logx == 1 && log10(xmax/xmin) < 2) ||
565 (logx > 1 && xmax-xmin < 2));
567 master->GetYaxis()->SetMoreLogLabels( logy && log10(ymax/ymin) < 2);
568 master->GetYaxis()->SetNoExponent ( logy && log10(ymax/ymin) < 2);
570 if (Ndivisions.first !=
"") {
571 master->SetNdivisions(Ndivisions.second, Ndivisions.first.c_str());
574 if (xTimeFormat !=
"") {
575 master->GetXaxis()->SetTimeDisplay(1);
576 master->GetXaxis()->SetTimeFormat(xTimeFormat.c_str());
579 master->Draw(option.c_str());
586 TH1*
h1 =
dynamic_cast<TH1*
>(i->get());
590 if (logx > 0 && h1 !=
master) {
594 TIter iter(h1->GetListOfFunctions());
596 const TRegexp key(
"x[^a-zA-Z]");
597 const TString rep(
"log10(x)");
599 for (TF1* f1; (f1 = (TF1*) iter.Next()) != NULL; ) {
601 TString formula(f1->GetExpFormula());
603 for (Ssiz_t pos = 0; (pos = formula.Index(key, pos)) != -1; pos += rep.Length()) {
604 formula.Replace(pos, 1, rep);
607 TF1 f2(f1->GetName(), formula);
609 f2.SetParameters(f1->GetParameters());
610 f2.SetRange(f1->GetXmin(), f1->GetXmax());
612 static_cast<TAttLine&
>(f2) = *f1;
616 if (f1->GetXmin() != 0.0 && f1->GetXmax() != 1.0)
617 f1->SetRange(pow(10.0,f1->GetXmin()), pow(10.0,f1->GetXmax()));
619 f1->SetRange(pow(10.0,xmin), pow(10.0,xmax));
625 if (grid.count(
'x') || grid.count(
'X')) { gPad->SetGridx(); }
626 if (grid.count(
'y') || grid.count(
'Y')) { gPad->SetGridy(); }
629 gStyle->SetOptStat(stats);
631 gStyle->SetOptFit(kFALSE);
636 DEBUG(
"Draw " << (*i)->GetName() <<
' ' << (*i)->GetTitle() << endl);
638 string buffer(option);
646 TGraph*
p1 =
dynamic_cast<TGraph*
> (i->get());
647 TMultiGraph* q1 =
dynamic_cast<TMultiGraph*
>(i->get());
651 if (p1->GetN() > 1 && drawLine)
656 p1->Draw(buffer.c_str());
658 }
else if (q1 != NULL) {
660 TIter next(q1->GetListOfGraphs());
662 while (TGraph* gi = dynamic_cast<TGraph*>(next())) {
664 if (gi->GetN() > 1 && drawLine)
669 gi->Draw(buffer.c_str());
674 (*i).Draw(buffer.c_str());
682 Ssiz_t height = listOfObjects.size();
686 width = max(width, i->getLabel().Length());
689 TLegend* lg =
getLegend(width, height, legend);
692 lg->AddEntry(*i,
" " + i->getLabel(),
isTAttLine(*i) ?
"L" :
"LPF");
Utility class to parse command line options.
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)...
TLegend * getLegend(const Int_t width, const Int_t height, const std::string option="TR")
Get legend.
void setLogarithm(TAxis *axis)
Make axis logarithmic (e.g.
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
JRange< Double_t > JRange_t
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.
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.
int main(int argc, char *argv[])