Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Functions
JPlot1D.cc File Reference

General purpose plot program for 1D ROOT objects. More...

#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

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.Index(regexp) << " (-1 /= OK)" << endl);
189 
190  // option match
191 
192  if (tag.Index(regexp) != -1) {
193 
194  JRootObject object(key->ReadObj());
195 
196  TAttMarker marker = JMarkerAttributes::getInstance().get(0);
197  TAttLine line = JLineAttributes ::getInstance().get(0);
198 
199  if (group > 1)
200  marker.SetMarkerColor(JMarkerAttributes::getInstance().get(listOfObjects.size()/group).GetMarkerColor());
201  else
202  marker = JMarkerAttributes::getInstance().get(listOfObjects.size());;
203 
204  if (drawLine)
205  line = JLineAttributes::getInstance().get(listOfObjects.size());
206  else
207  line.SetLineColor(marker.GetMarkerColor());
208 
209  try {
210 
211  TProfile& h1 = dynamic_cast<TProfile&>(*object);
212 
213  object = h1.ProjectionX();
214  }
215  catch(exception&) {}
216 
217  try {
218 
219  TH2& h2 = dynamic_cast<TH2&>(*object);
220 
221  if (px) {
222 
223  if (Z != JRange_t())
224  object = h2.ProjectionX(MAKE_CSTRING(h2.GetName() << "_px" << LABEL_TERMINATOR << listOfObjects.size()),
225  h2.GetYaxis()->FindBin(Z.getLowerLimit()),
226  h2.GetYaxis()->FindBin(Z.getUpperLimit()) - 1);
227  else
228  object = h2.ProjectionX(MAKE_CSTRING(h2.GetName() << "_px" << LABEL_TERMINATOR << listOfObjects.size()),
229  1,
230  h2.GetYaxis()->GetNbins());
231 
232  } else if (py) {
233 
234  if (Z != JRange_t())
235  object = h2.ProjectionY(MAKE_CSTRING(h2.GetName() << "_py" << LABEL_TERMINATOR << listOfObjects.size()),
236  h2.GetXaxis()->FindBin(Z.getLowerLimit()),
237  h2.GetXaxis()->FindBin(Z.getUpperLimit()) - 1);
238  else
239  object = h2.ProjectionY(MAKE_CSTRING(h2.GetName() << "_py" << LABEL_TERMINATOR << listOfObjects.size()),
240  1,
241  h2.GetXaxis()->GetNbins());
242 
243  } else {
244 
245  ERROR("For 2D histograms, use option option -P for projections or use JPlot2D" << endl);
246 
247  continue;
248  }
249  }
250  catch(exception&) {}
251 
252  try {
253 
254  dynamic_cast<TAttMarker&>(*object) = marker;
255  }
256  catch(exception&) {}
257 
258  try {
259 
260  dynamic_cast<TAttLine&> (*object) = line;
261  }
262  catch(exception&) {}
263 
264  try {
265 
266  TH1& h1 = dynamic_cast<TH1&>(*object);
267 
268  h1.SetStats(stats != -1);
269 
270  xmin = min(xmin, h1.GetXaxis()->GetXmin());
271  xmax = max(xmax, h1.GetXaxis()->GetXmax());
272 
273  ymin = min(ymin, h1.GetMinimum());
274  ymax = max(ymax, h1.GetMaximum());
275 
276  if (drawLine) {
277  for (int i = 1; i <= h1.GetNbinsX(); ++i) {
278  h1.SetBinError(i, 0.0);
279  }
280  }
281 
282  TIterator* iterator = h1.GetListOfFunctions()->MakeIterator();
283 
284  for (int n = 0; TF1* f1 = (TF1*) iterator->Next(); ) {
285 
286  f1->SetNpx(1000);
287 
288  dynamic_cast<TAttLine&>(*f1) = JLineAttributes::getInstance().get(0);
289 
290  if (inputFile.size() == 1)
291  f1->SetLineColor(kRed);
292  else
293  f1->SetLineColor(h1.GetLineColor());
294 
295  f1->SetLineStyle(JLineAttributes::getInstance().get(n++).GetLineStyle());
296  }
297  }
298  catch(exception&) {}
299 
300  if (fillArea) {
301 
302  try {
303 
304  TAttFill& fill = dynamic_cast<TAttFill&>(*object);
305 
306  fill.SetFillColor(marker.GetMarkerColor());
307  }
308  catch(exception&) {}
309  }
310 
311  try {
312 
313  TGraph& g1 = dynamic_cast<TGraph&>(*object);
314 
315  for (Int_t i = 0; i != g1.GetN(); ++i) {
316 
317  xmin = min(xmin, g1.GetX()[i]);
318  xmax = max(xmax, g1.GetX()[i]);
319 
320  if (!logy || g1.GetY()[i] > 0.0) {
321  ymin = min(ymin, g1.GetY()[i]);
322  ymax = max(ymax, g1.GetY()[i]);
323  }
324  }
325 
326  setRange(xmin, xmax, logx == 1);
327  }
328  catch(exception&) {}
329 
330  try {
331 
332  THStack& hs = dynamic_cast<THStack&>(*object);
333 
334  NOTICE("THStack" << endl);
335 
336  TIterator* iterator = hs.GetHists()->MakeIterator();
337 
338  for (size_t index = 0; TObject* i = iterator->Next(); ++index) {
339 
340  TH1& h1 = dynamic_cast<TH1&>(*i);
341 
342  NOTICE("TH1[" << index << "] " << h1.GetName() << endl);
343 
344  xmin = min(xmin, h1.GetXaxis()->GetXmin());
345  xmax = max(xmax, h1.GetXaxis()->GetXmax());
346 
347  ymin = min(ymin, h1.GetMinimum());
348  ymax = max(ymax, h1.GetMaximum());
349 
350  h1.SetLineWidth(1);
351  h1.SetLineColor(kBlack);
352 
353  if (index != 0) {
354  h1.SetFillColor(JMarkerAttributes::getInstance().get(index).GetMarkerColor());
355  }
356  }
357  }
358  catch(exception&) {}
359 
360  for (TString buffer[] = { object.getLabel(), input->getFilename().c_str(), "" }, *i = buffer; *i != ""; ++i) {
361 
362  *i = (*i)(TRegexp("\\[.*\\]"));
363 
364  DEBUG("Label: <" << *i << ">" << endl);
365 
366  if (i->Length() > 2) {
367  object.setLabel((*i)(1, i->Length() - 2));
368  }
369  }
370 
371  DEBUG("Add object: " << tag << " with label " << object.getLabel() << endl);
372 
373  if (master == NULL) {
374  master = dynamic_cast<TH1*>(object.get());
375  }
376 
377  listOfObjects.push_back(object);
378  }
379  }
380  }
381 
382  if (listOfObjects.empty()) {
383  ERROR("Nothing to draw." << endl);
384  }
385 
386  // plot frame
387 
388  if (X != JRange_t()) {
389  xmin = X.getLowerLimit();
390  xmax = X.getUpperLimit();
391  }
392 
393  if (Y != JRange_t()) {
394  ymin = Y.getLowerLimit();
395  ymax = Y.getUpperLimit();
396  } else {
397  setRange(ymin, ymax, logy);
398  }
399 
400  cv->cd(1);
401 
402  if (master == NULL) {
403 
404  if (X != JRange_t()) {
405 
406  master = new TH1D("__H__", NULL, 100, X.getLowerLimit(), X.getUpperLimit());
407 
408  master->SetStats(kFALSE);
409 
410  } else if (xmin < xmax) {
411 
412  master = new TH1D("__H__", NULL, 100, xmin, xmax);
413 
414  master->SetStats(kFALSE);
415 
416  } else {
417 
418  TText* p = new TText(0.5, 0.5, MAKE_CSTRING("No data"));
419 
420  p->SetTextAlign(21);
421  p->SetTextAngle(45);
422  p->Draw();
423  }
424  }
425 
426  if (master != NULL) {
427 
428  if (logx) { gPad->SetLogx(); }
429  if (logy) { gPad->SetLogy(); }
430 
431  master->GetXaxis()->SetRangeUser(xmin, xmax);
432 
433  if (logx > 1) {
434  setLogarithm(master->GetXaxis());
435  }
436 
437  master->SetTitle(title.c_str());
438 
439  master->SetMinimum(ymin);
440  master->SetMaximum(ymax);
441 
442  if (xLabel != "") { master->GetXaxis()->SetTitle(xLabel.c_str()); master->GetXaxis()->CenterTitle(true); }
443  if (yLabel != "") { master->GetYaxis()->SetTitle(yLabel.c_str()); master->GetYaxis()->CenterTitle(true); }
444 
445  master->GetXaxis()->SetMoreLogLabels((logx == 1 && log10(xmax/xmin) < 2) ||
446  (logx > 1 && xmax-xmin < 2));
447 
448  master->GetYaxis()->SetMoreLogLabels( logy && log10(ymax/ymin) < 2);
449  master->GetYaxis()->SetNoExponent ( logy && log10(ymax/ymin) < 2);
450 
451  if (Ndivisions.first != "") {
452  master->SetNdivisions(Ndivisions.second, Ndivisions.first.c_str());
453  }
454 
455  if (xTimeFormat != "") {
456  master->GetXaxis()->SetTimeDisplay(1);
457  master->GetXaxis()->SetTimeFormat(xTimeFormat.c_str());
458  }
459 
460  master->Draw(option.c_str());
461  }
462 
463  if (logx > 1) {
464 
465  for (vector<JRootObject>::iterator i = listOfObjects.begin(); i != listOfObjects.end(); ++i) {
466 
467  TH1* h1 = dynamic_cast<TH1*>(i->get());
468 
469  if (h1 != NULL) {
470 
471  if (logx > 0 && h1 != master) {
472  setLogarithm(h1->GetXaxis());
473  }
474 
475  TIter iter(h1->GetListOfFunctions());
476 
477  const TRegexp key("x[^a-zA-Z]"); // replace parameter x by log10(x)
478  const TString rep("log10(x)"); //
479 
480  for (TF1* f1; (f1 = (TF1*) iter.Next()) != NULL; ) {
481 
482  TString formula(f1->GetExpFormula());
483 
484  for (Ssiz_t pos = 0; (pos = formula.Index(key, pos)) != -1; pos += rep.Length()) {
485  formula.Replace(pos, 1, rep);
486  };
487 
488  TF1 f2(f1->GetName(), formula);
489 
490  f2.SetParameters(f1->GetParameters());
491  f2.SetRange(f1->GetXmin(), f1->GetXmax());
492 
493  static_cast<TAttLine&>(f2) = *f1;
494 
495  *f1 = f2;
496 
497  if (f1->GetXmin() != 0.0 && f1->GetXmax() != 1.0)
498  f1->SetRange(pow(10.0,f1->GetXmin()), pow(10.0,f1->GetXmax()));
499  else
500  f1->SetRange(pow(10.0,xmin), pow(10.0,xmax));
501  }
502  }
503  }
504  }
505 
506  if (grid.count('x') || grid.count('X')) { gPad->SetGridx(); }
507  if (grid.count('y') || grid.count('Y')) { gPad->SetGridy(); }
508 
509  if (stats != -1)
510  gStyle->SetOptStat(stats);
511  else
512  gStyle->SetOptFit(kFALSE);
513 
514  for (vector<JRootObject>::const_iterator i = listOfObjects.begin(); i != listOfObjects.end(); ++i) {
515 
516  DEBUG("Draw " << (*i)->GetName() << ' ' << (*i)->GetTitle() << endl);
517 
518  string buffer(option);
519 
520  //if (!dynamic_cast<THStack*>(i->get())) {
521  // buffer += "SAMES";
522  //}
523 
524  buffer += "SAME";
525 
526  TGraph* p = dynamic_cast<TGraph*>(i->get());
527 
528  if (p != NULL) {
529  if (p->GetN() > 1 && drawLine)
530  buffer += "L"; // drawing cut
531  else
532  buffer += "P"; // drawing point(s)
533  }
534 
535  (*i).Draw(buffer);
536  }
537 
538  gPad->RedrawAxis();
539 
540  if (legend != "") {
541 
542  Ssiz_t height = listOfObjects.size();
543  Ssiz_t width = 1;
544 
545  for (vector<JRootObject>::const_iterator i = listOfObjects.begin(); i != listOfObjects.end(); ++i) {
546  width = max(width, i->getLabel().Length());
547  }
548 
549  TLegend* lg = new JLegend(width, height, legend);
550 
551  for (vector<JRootObject>::const_iterator i = listOfObjects.begin(); i != listOfObjects.end(); ++i) {
552  lg->AddEntry(*i, " " + i->getLabel(), isTAttLine(*i) ? "L" : "LPF");
553  }
554 
555  lg->Draw();
556  }
557 
558  cv->Update();
559 
560  if (outputFile != "") {
561  cv->SaveAs(outputFile.c_str());
562  }
563 
564  if (!batch) {
565  tp->Run();
566  }
567 }
Utility class to parse command line options.
Definition: JParser.hh:1410
Definition: JRoot.hh:19
#define MAKE_CSTRING(A)
Make C-string.
Definition: JPrint.hh:611
Empty structure for specification of parser element that is initialised (i.e.
Definition: JParser.hh:64
string outputFile
void setLogarithm(TAxis *axis)
Make axis logarithmic (e.g.
bool isTAttLine(const TObject *object)
Get drawing option of TH1.
T & getInstance(const T &object)
Get static instance from temporary object.
Definition: JObject.hh:73
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1836
#define NOTICE(A)
Definition: JMessage.hh:62
#define ERROR(A)
Definition: JMessage.hh:64
int debug
debug level
Definition: JSirene.cc:59
static const char LABEL_TERMINATOR
label terminator
Definition: JRootObject.hh:21
#define FATAL(A)
Definition: JMessage.hh:65
std::string getLabel(const JPDFType_t pdf)
Get PDF label.
Definition: JPDFTypes.hh:59
void setRange(double &xmin, double &xmax, const bool logx)
Set axis range.
Auxiliary class to handle multiple boolean-like I/O.
Definition: JParser.hh:218
TDirectory * getDirectory(const JRootObjectID &id)
Get TDirectory pointer.
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:60
Double_t g1(const Double_t x)
Function.
Definition: JQuantiles.cc:25