Jpp
Functions
JPlot1D.cc File Reference
#include <string>
#include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <cmath>
#include "TROOT.h"
#include "TFile.h"
#include "TClass.h"
#include "TApplication.h"
#include "TCanvas.h"
#include "TKey.h"
#include "TStyle.h"
#include "TAttMarker.h"
#include "TAttLine.h"
#include "TH1.h"
#include "TH2.h"
#include "TF1.h"
#include "THStack.h"
#include "TGraph.h"
#include "TGraphErrors.h"
#include "TProfile.h"
#include "TLegend.h"
#include "TString.h"
#include "TRegexp.h"
#include "TText.h"
#include "JTools/JRange.hh"
#include "JLang/JSinglePointer.hh"
#include "JGizmo/JStyle.hh"
#include "JGizmo/JCanvas.hh"
#include "JGizmo/JMarkerAttributes.hh"
#include "JGizmo/JLineAttributes.hh"
#include "JGizmo/JLegend.hh"
#include "JGizmo/JRootObjectID.hh"
#include "JGizmo/JRootObject.hh"
#include "JGizmo/JGizmoToolkit.hh"
#include "Jeep/JPrint.hh"
#include "Jeep/JParser.hh"
#include "Jeep/JMessage.hh"

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Detailed Description

General purpose plot program for 1D ROOT objects. The option -f corresponds to <file name>:<object name>.

Author
mdejong

Definition in file JPlot1D.cc.

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 51 of file JPlot1D.cc.

52 {
53  using namespace std;
54  using namespace JPP;
55 
56  typedef JRange<double> JRange_t;
57 
58  vector<JRootObjectID> inputFile;
59  string outputFile;
60  JCanvas canvas;
61  int stats;
62  string legend;
63  JRange_t X;
64  JRange_t Y;
65  JRange_t Z;
66  JCounter logx;
67  bool logy;
68  bool logz;
69  char project;
70  string xLabel;
71  string yLabel;
72  bool drawLine;
73  bool fillArea;
74  int lineWidth;
75  double markerSize;
76  string option;
77  set<char> grid;
78  bool batch;
79  string title;
80  pair<string, int> Ndivisions;
81  int group;
82  int debug;
83  string xTimeFormat;
84 
85  try {
86 
87  JParser<> zap("General purpose plot program for 1D ROOT objects.");
88 
89  zap['f'] = make_field(inputFile, "<input file>:<object name>");
90  zap['o'] = make_field(outputFile, "graphics output") = "";
91  zap['w'] = make_field(canvas, "size of canvas <nx>x<ny> [pixels]") = JCanvas(500, 500);
92  zap['s'] = make_field(stats) = -1;
93  zap['L'] = make_field(legend, "position legend e.g. TR") = "";
94  zap['x'] = make_field(X, "abscissa range") = JRange_t();
95  zap['y'] = make_field(Y, "ordinate range") = JRange_t();
96  zap['z'] = make_field(Z, "ordinate range of projection)") = JRange_t();
97  zap['X'] = make_field(logx, "logarithmic x-axis (-XX log10 axis)");
98  zap['Y'] = make_field(logy, "logarithmic y-axis");
99  zap['Z'] = make_field(logz, "logarithmic y-axis; after projection");
100  zap['P'] = make_field(project, "projection") = '\0', 'x', 'X', 'y', 'Y';
101  zap['>'] = make_field(xLabel, "x-axis label") = "";
102  zap['^'] = make_field(yLabel, "y-axis label") = "";
103  zap['C'] = make_field(drawLine);
104  zap['F'] = make_field(fillArea);
105  zap['l'] = make_field(lineWidth, "line width") = 2;
106  zap['S'] = make_field(markerSize, "marker size") = 1.0;
107  zap['O'] = make_field(option, "plotting option") = "";
108  zap['G'] = make_field(grid, "grid lines [X][Y]") = JPARSER::initialised();
109  zap['B'] = make_field(batch, "batch processing");
110  zap['T'] = make_field(title, "title") = "KM3NeT preliminary";
111  zap['N'] = make_field(Ndivisions, "axis divisioning (e.g. \"X 505\")") = JPARSER::initialised();
112  zap['g'] = make_field(group, "group colour codes of objects") = 1;
113  zap['t'] = make_field(xTimeFormat, "set time format for x-axis, e.g. \%d\\/\%m\\/\\%y%F1970-01-01 00:00:00") = "";
114  zap['d'] = make_field(debug) = 0;
115 
116  zap(argc, argv);
117  }
118  catch(const exception &error) {
119  FATAL(error.what() << endl);
120  }
121 
122 
123  gROOT->SetBatch(batch);
124 
125  TApplication* tp = new TApplication("user", NULL, NULL);
126  TCanvas* cv = new TCanvas("c1", "c1", canvas.x, canvas.y);
127 
128  JSinglePointer<TStyle> gStyle(new JStyle("gplot", cv->GetWw(), cv->GetWh()));
129 
130  gROOT->SetStyle("gplot");
131  gROOT->ForceStyle();
132 
133 
134  cv->SetFillStyle(4000);
135  cv->SetFillColor(kWhite);
136  cv->Divide(1,1);
137  cv->cd(1);
138 
139 
140  JMarkerAttributes::getInstance().setMarkerSize(markerSize);
141  JLineAttributes ::getInstance().setLineWidth (lineWidth);
142 
143 
144  Double_t xmin = +numeric_limits<double>::max();
145  Double_t xmax = -numeric_limits<double>::max();
146 
147  Double_t ymin = +numeric_limits<double>::max();
148  Double_t ymax = -numeric_limits<double>::max();
149 
150 
151  vector<JRootObject> listOfObjects;
152 
153  const bool px = (project == 'x' || project == 'X'); // projection on x-axis
154  const bool py = (project == 'y' || project == 'Y'); // projection on y-axis
155 
156  logy = (logy || logz);
157 
158  if (px) {
159  swap(Y, Z); // Y becomes range in TH2::ProjectionX() and Z becomes y-axis range
160  }
161 
162  if (py) {
163  swap(X, Z); // X becomes range in TH2::ProjectionY()
164  swap(Y, X); // Y becomes x-axis range and Z becomes y-axis range
165  }
166 
167  TH1* master = NULL;
168 
169  for (vector<JRootObjectID>::const_iterator input = inputFile.begin(); input != inputFile.end(); ++input) {
170 
171  DEBUG("Input: " << *input << endl);
172 
173  TDirectory* dir = getDirectory(*input);
174 
175  if (dir == NULL) {
176  ERROR("File: " << input->getFullFilename() << " not opened." << endl);
177  continue;
178  }
179 
180  const TRegexp regexp(input->getObjectName());
181 
182  TIter iter(dir->GetListOfKeys());
183 
184  for (TKey* key; (key = (TKey*) iter.Next()) != NULL; ) {
185 
186  const TString tag(key->GetName());
187 
188  DEBUG("Key: " << tag << " match = " << tag.Contains(regexp) << endl);
189 
190  // option match
191 
192  if (tag.Contains(regexp)) {
193 
194  if (title == "?") {
195  title = key->GetName();
196  }
197 
198  JRootObject object(key->ReadObj());
199 
200  TAttMarker marker = JMarkerAttributes::getInstance().get(0);
201  TAttLine line = JLineAttributes ::getInstance().get(0);
202 
203  if (group > 1)
204  marker.SetMarkerColor(JMarkerAttributes::getInstance().get(listOfObjects.size()/group).GetMarkerColor());
205  else
206  marker = JMarkerAttributes::getInstance().get(listOfObjects.size());
207 
208  if (drawLine)
209  line = JLineAttributes::getInstance().get(listOfObjects.size());
210  else
211  line.SetLineColor(marker.GetMarkerColor());
212 
213  try {
214 
215  TProfile& h1 = dynamic_cast<TProfile&>(*object);
216 
217  object = h1.ProjectionX();
218  }
219  catch(exception&) {}
220 
221  try {
222 
223  TH2& h2 = dynamic_cast<TH2&>(*object);
224 
225  if (px) {
226 
227  if (Z != JRange_t())
228  object = h2.ProjectionX(MAKE_CSTRING(h2.GetName() << "_px" << LABEL_TERMINATOR << listOfObjects.size()),
229  h2.GetYaxis()->FindBin(Z.getLowerLimit()),
230  h2.GetYaxis()->FindBin(Z.getUpperLimit()) - 1);
231  else
232  object = h2.ProjectionX(MAKE_CSTRING(h2.GetName() << "_px" << LABEL_TERMINATOR << listOfObjects.size()),
233  1,
234  h2.GetYaxis()->GetNbins());
235 
236  } else if (py) {
237 
238  if (Z != JRange_t())
239  object = h2.ProjectionY(MAKE_CSTRING(h2.GetName() << "_py" << LABEL_TERMINATOR << listOfObjects.size()),
240  h2.GetXaxis()->FindBin(Z.getLowerLimit()),
241  h2.GetXaxis()->FindBin(Z.getUpperLimit()) - 1);
242  else
243  object = h2.ProjectionY(MAKE_CSTRING(h2.GetName() << "_py" << LABEL_TERMINATOR << listOfObjects.size()),
244  1,
245  h2.GetXaxis()->GetNbins());
246 
247  } else {
248 
249  ERROR("For 2D histograms, use option option -P for projections or use JPlot2D" << endl);
250 
251  continue;
252  }
253  }
254  catch(exception&) {}
255 
256  try {
257 
258  dynamic_cast<TAttMarker&>(*object) = marker;
259  }
260  catch(exception&) {}
261 
262  try {
263 
264  dynamic_cast<TAttLine&> (*object) = line;
265  }
266  catch(exception&) {}
267 
268  try {
269 
270  TH1& h1 = dynamic_cast<TH1&>(*object);
271 
272  h1.SetStats(stats != -1);
273 
274  xmin = min(xmin, h1.GetXaxis()->GetXmin());
275  xmax = max(xmax, h1.GetXaxis()->GetXmax());
276 
277  ymin = min(ymin, h1.GetMinimum());
278  ymax = max(ymax, h1.GetMaximum());
279 
280  if (drawLine) {
281  for (int i = 1; i <= h1.GetNbinsX(); ++i) {
282  h1.SetBinError(i, 0.0);
283  }
284  }
285  }
286  catch(exception&) {}
287 
288  if (fillArea) {
289 
290  try {
291 
292  TAttFill& fill = dynamic_cast<TAttFill&>(*object);
293 
294  fill.SetFillColor(marker.GetMarkerColor());
295  }
296  catch(exception&) {}
297  }
298 
299  try {
300 
301  TGraph& g1 = dynamic_cast<TGraph&>(*object);
302 
303  for (Int_t i = 0; i != g1.GetN(); ++i) {
304 
305  xmin = min(xmin, g1.GetX()[i]);
306  xmax = max(xmax, g1.GetX()[i]);
307 
308  if (!logy || g1.GetY()[i] > 0.0) {
309  ymin = min(ymin, g1.GetY()[i]);
310  ymax = max(ymax, g1.GetY()[i]);
311  }
312  }
313  }
314  catch(exception&) {}
315 
316  try {
317 
318  TF1& f1 = dynamic_cast<TF1&>(*object);
319 
320  if (xmin != +numeric_limits<double>::max() &&
321  xmax != -numeric_limits<double>::max()) {
322  ymin = min(ymin, f1.GetMinimum(xmin, xmax));
323  ymax = max(ymax, f1.GetMaximum(xmin, xmax));
324  }
325  }
326  catch(exception&) {}
327 
328 
329  try {
330 
331  THStack& hs = dynamic_cast<THStack&>(*object);
332 
333  NOTICE("THStack" << endl);
334 
335  TIterator* iterator = hs.GetHists()->MakeIterator();
336 
337  for (size_t index = 0; TObject* i = iterator->Next(); ++index) {
338 
339  TH1& h1 = dynamic_cast<TH1&>(*i);
340 
341  NOTICE("TH1[" << index << "] " << h1.GetName() << endl);
342 
343  xmin = min(xmin, h1.GetXaxis()->GetXmin());
344  xmax = max(xmax, h1.GetXaxis()->GetXmax());
345 
346  ymin = min(ymin, h1.GetMinimum());
347  ymax = max(ymax, h1.GetMaximum());
348 
349  h1.SetLineWidth(1);
350  h1.SetLineColor(kBlack);
351 
352  if (index != 0) {
353  h1.SetFillColor(JMarkerAttributes::getInstance().get(index).GetMarkerColor());
354  }
355  }
356  }
357  catch(exception&) {}
358 
359  for (TString buffer[] = { object.getLabel(), input->getFilename().c_str(), "" }, *i = buffer; *i != ""; ++i) {
360 
361  *i = (*i)(TRegexp("\\[.*\\]"));
362 
363  DEBUG("Label: <" << *i << ">" << endl);
364 
365  if (i->Length() > 2) {
366  object.setLabel((*i)(1, i->Length() - 2));
367  }
368  }
369 
370  DEBUG("Add object: " << tag << " with label " << object.getLabel() << endl);
371 
372  if (master == NULL) {
373  master = dynamic_cast<TH1*>(object.get());
374  }
375 
376  listOfObjects.push_back(object);
377  }
378  }
379  }
380 
381  if (listOfObjects.empty()) {
382  ERROR("Nothing to draw." << endl);
383  }
384 
385  for (vector<JRootObject>::iterator i = listOfObjects.begin(); i != listOfObjects.end(); ++i) {
386 
387  TH1* h1 = dynamic_cast<TH1*> (i->get());
388  TGraph* g1 = dynamic_cast<TGraph*> (i->get());
389  TAttLine* ls = dynamic_cast<TAttLine*>(i->get());
390 
391  TIterator* iterator = NULL;
392 
393  if (h1 != NULL)
394  iterator = h1->GetListOfFunctions()->MakeIterator();
395  else if (g1 != NULL)
396  iterator = g1->GetListOfFunctions()->MakeIterator();
397  else
398  continue;
399 
400  Double_t x1 = +numeric_limits<Double_t>::max();
401  Double_t x2 = -numeric_limits<Double_t>::max();
402 
403  for (int ns = 0, nc = 1; TF1* f1 = (TF1*) iterator->Next(); ) {
404 
405  Double_t __x1;
406  Double_t __x2;
407 
408  f1->GetRange(__x1, __x2);
409  f1->SetNpx(1000);
410 
411  dynamic_cast<TAttLine&>(*f1) = JLineAttributes::getInstance().get(0);
412 
413  if (listOfObjects.size() == 1) {
414 
415  f1->SetLineStyle(JLineAttributes::getInstance().get(ns).GetLineStyle());
416 
417  if (x1 == __x1 &&
418  x2 == __x2)
419  ++nc; // follow colour style
420  else
421  ++ns; // follow line style
422 
423  f1->SetLineColor(JMarkerAttributes::getInstance().get(nc).GetMarkerColor());
424 
425  } else {
426 
427  // keep colour of base object and accordingly modify line style.
428 
429  f1->SetLineColor(ls->GetLineColor());
430  f1->SetLineStyle(JLineAttributes::getInstance().get(ns++).GetLineStyle());
431  }
432 
433  ymin = min(ymin, f1->GetMinimum(xmin, xmax));
434  ymax = max(ymax, f1->GetMaximum(xmin, xmax));
435 
436  x1 = __x1;
437  x2 = __x2;
438  }
439  }
440 
441 
442  // plot frame
443 
444  if (X != JRange_t()) {
445  xmin = X.getLowerLimit();
446  xmax = X.getUpperLimit();
447  }
448 
449  if (Y != JRange_t()) {
450  ymin = Y.getLowerLimit();
451  ymax = Y.getUpperLimit();
452  } else {
453  setRange(ymin, ymax, logy);
454  }
455 
456  cv->cd(1);
457 
458  if (master == NULL) {
459 
460  if (X != JRange_t()) {
461 
462  master = new TH1D("__H__", NULL, 100, X.getLowerLimit(), X.getUpperLimit());
463 
464  master->SetStats(kFALSE);
465 
466  } else if (xmin < xmax) {
467 
468  master = new TH1D("__H__", NULL, 100, xmin, xmax);
469 
470  master->SetStats(kFALSE);
471 
472  } else {
473 
474  TText* p = new TText(0.5, 0.5, MAKE_CSTRING("No data"));
475 
476  p->SetTextAlign(21);
477  p->SetTextAngle(45);
478  p->Draw();
479  }
480  }
481 
482  if (master != NULL) {
483 
484  if (logx) { gPad->SetLogx(); }
485  if (logy) { gPad->SetLogy(); }
486 
487  master->GetXaxis()->SetRangeUser(xmin, xmax);
488 
489  if (logx > 1) {
490  setLogarithm(master->GetXaxis());
491  }
492 
493  master->SetTitle(title.c_str());
494 
495  master->SetMinimum(ymin);
496  master->SetMaximum(ymax);
497 
498  if (xLabel != "") { master->GetXaxis()->SetTitle(xLabel.c_str()); master->GetXaxis()->CenterTitle(true); }
499  if (yLabel != "") { master->GetYaxis()->SetTitle(yLabel.c_str()); master->GetYaxis()->CenterTitle(true); }
500 
501  master->GetXaxis()->SetMoreLogLabels((logx == 1 && log10(xmax/xmin) < 2) ||
502  (logx > 1 && xmax-xmin < 2));
503 
504  master->GetYaxis()->SetMoreLogLabels( logy && log10(ymax/ymin) < 2);
505  master->GetYaxis()->SetNoExponent ( logy && log10(ymax/ymin) < 2);
506 
507  if (Ndivisions.first != "") {
508  master->SetNdivisions(Ndivisions.second, Ndivisions.first.c_str());
509  }
510 
511  if (xTimeFormat != "") {
512  master->GetXaxis()->SetTimeDisplay(1);
513  master->GetXaxis()->SetTimeFormat(xTimeFormat.c_str());
514  }
515 
516  master->Draw(option.c_str());
517  }
518 
519  if (logx > 1) {
520 
521  for (vector<JRootObject>::iterator i = listOfObjects.begin(); i != listOfObjects.end(); ++i) {
522 
523  TH1* h1 = dynamic_cast<TH1*>(i->get());
524 
525  if (h1 != NULL) {
526 
527  if (logx > 0 && h1 != master) {
528  setLogarithm(h1->GetXaxis());
529  }
530 
531  TIter iter(h1->GetListOfFunctions());
532 
533  const TRegexp key("x[^a-zA-Z]"); // replace parameter x by log10(x)
534  const TString rep("log10(x)"); //
535 
536  for (TF1* f1; (f1 = (TF1*) iter.Next()) != NULL; ) {
537 
538  TString formula(f1->GetExpFormula());
539 
540  for (Ssiz_t pos = 0; (pos = formula.Index(key, pos)) != -1; pos += rep.Length()) {
541  formula.Replace(pos, 1, rep);
542  };
543 
544  TF1 f2(f1->GetName(), formula);
545 
546  f2.SetParameters(f1->GetParameters());
547  f2.SetRange(f1->GetXmin(), f1->GetXmax());
548 
549  static_cast<TAttLine&>(f2) = *f1;
550 
551  *f1 = f2;
552 
553  if (f1->GetXmin() != 0.0 && f1->GetXmax() != 1.0)
554  f1->SetRange(pow(10.0,f1->GetXmin()), pow(10.0,f1->GetXmax()));
555  else
556  f1->SetRange(pow(10.0,xmin), pow(10.0,xmax));
557  }
558  }
559  }
560  }
561 
562  if (grid.count('x') || grid.count('X')) { gPad->SetGridx(); }
563  if (grid.count('y') || grid.count('Y')) { gPad->SetGridy(); }
564 
565  if (stats != -1)
566  gStyle->SetOptStat(stats);
567  else
568  gStyle->SetOptFit(kFALSE);
569 
570  for (vector<JRootObject>::const_iterator i = listOfObjects.begin(); i != listOfObjects.end(); ++i) {
571 
572  DEBUG("Draw " << (*i)->GetName() << ' ' << (*i)->GetTitle() << endl);
573 
574  string buffer(option);
575 
576  //if (!dynamic_cast<THStack*>(i->get())) {
577  // buffer += "SAMES";
578  //}
579 
580  buffer += "SAME";
581 
582  TGraph* p = dynamic_cast<TGraph*>(i->get());
583 
584  if (p != NULL) {
585  if (p->GetN() > 1 && drawLine)
586  buffer += "L"; // drawing cut
587  else
588  buffer += "P"; // drawing point(s)
589  }
590 
591  (*i).Draw(buffer);
592  }
593 
594  gPad->RedrawAxis();
595 
596  if (legend != "") {
597 
598  Ssiz_t height = listOfObjects.size();
599  Ssiz_t width = 1;
600 
601  for (vector<JRootObject>::const_iterator i = listOfObjects.begin(); i != listOfObjects.end(); ++i) {
602  width = max(width, i->getLabel().Length());
603  }
604 
605  TLegend* lg = new JLegend(width, height, legend);
606 
607  for (vector<JRootObject>::const_iterator i = listOfObjects.begin(); i != listOfObjects.end(); ++i) {
608  lg->AddEntry(*i, " " + i->getLabel(), isTAttLine(*i) ? "L" : "LPF");
609  }
610 
611  lg->Draw();
612  }
613 
614  cv->Update();
615 
616  if (outputFile != "") {
617  cv->SaveAs(outputFile.c_str());
618  }
619 
620  if (!batch) {
621  tp->Run();
622  }
623 }
std::iterator
Definition: JSTDTypes.hh:18
TObject
Definition: JRoot.hh:19
JLANG::getInstance
T & getInstance(const T &object)
Get static instance from temporary object.
Definition: JObject.hh:75
g1
Double_t g1(const Double_t x)
Function.
Definition: JQuantiles.cc:25
JPARSER::initialised
Empty structure for specification of parser element that is initialised (i.e.
Definition: JParser.hh:63
std::vector
Definition: JSTDTypes.hh:12
JPARSER::JParser
Utility class to parse command line options.
Definition: JParser.hh:1493
NOTICE
#define NOTICE(A)
Definition: JMessage.hh:64
std::set
Definition: JSTDTypes.hh:13
JGIZMO::setLogarithm
void setLogarithm(TAxis *axis)
Make axis logarithmic (e.g.
Definition: JGizmoToolkit.hh:336
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
MAKE_CSTRING
#define MAKE_CSTRING(A)
Make C-string.
Definition: JPrint.hh:708
ERROR
#define ERROR(A)
Definition: JMessage.hh:66
JPARSER::JCounter
Auxiliary class to handle multiple boolean-like I/O.
Definition: JParser.hh:218
debug
int debug
debug level
Definition: JSirene.cc:59
JGIZMO::LABEL_TERMINATOR
static const char LABEL_TERMINATOR
label terminator
Definition: JRootObject.hh:21
JGIZMO::isTAttLine
bool isTAttLine(const TObject *object)
Get drawing option of TH1.
Definition: JGizmoToolkit.hh:174
JGIZMO::getDirectory
TDirectory * getDirectory(const JRootObjectID &id)
Get TDirectory pointer.
Definition: JGizmoToolkit.hh:121
std::pair
Definition: JSTDTypes.hh:15
JPHYSICS::getLabel
std::string getLabel(const JPDFType_t pdf)
Get PDF label.
Definition: JPDFTypes.hh:60
JGIZMO::setRange
void setRange(double &xmin, double &xmax, const bool logx)
Set axis range.
Definition: JGizmoToolkit.hh:572
make_field
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1954
DEBUG
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
std
Definition: jaanetDictionary.h:36
JAANET::get
T get(const JHead &header)
Get object from header.
Definition: JHeadToolkit.hh:295
FATAL
#define FATAL(A)
Definition: JMessage.hh:67
outputFile
string outputFile
Definition: JDAQTimesliceSelector.cc:37