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

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

#include <string>
#include <iostream>
#include <vector>
#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 "TH2.h"
#include "TH2D.h"
#include "TGraph.h"
#include "TGraph2D.h"
#include "TGraph2DErrors.h"
#include "TF2.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/JRootObjectID.hh"
#include "JGizmo/JRootObject.hh"
#include "JGizmo/JGizmoToolkit.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 2D ROOT objects.

The option -f corresponds to <file name>:<object name>.

Author
mdejong

Definition in file JPlot2D.cc.

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 45 of file JPlot2D.cc.

46 {
47  using namespace std;
48  using namespace JPP;
49 
50  typedef JRange<double> JRange_t;
51 
52  vector<JRootObjectID> inputFile;
53  string outputFile;
54  JCanvas canvas;
55  int stats;
56  JRange_t X;
57  JRange_t Y;
58  JRange_t Z;
59  JCounter logx;
60  JCounter logy;
61  bool logz;
62  string xLabel;
63  string yLabel;
64  string zLabel;
65  int lineWidth;
66  string option;
67  bool batch;
68  string title;
69  pair<string, int> Ndivisions;
70  int palette;
71  int debug;
72 
73  try {
74 
75  JParser<> zap("General purpose plot program for 2D ROOT objects.");
76 
77  zap['f'] = make_field(inputFile, "<input file>:<object name>");
78  zap['o'] = make_field(outputFile, "graphics output") = "";
79  zap['w'] = make_field(canvas, "size of canvas <nx>x<ny> [pixels]") = JCanvas(500, 500);
80  zap['s'] = make_field(stats) = -1;
81  zap['x'] = make_field(X, "x-abscissa range") = JRange_t();
82  zap['y'] = make_field(Y, "y-abscissa range") = JRange_t();
83  zap['z'] = make_field(Z, "ordinate range") = JRange_t();
84  zap['X'] = make_field(logx, "logarithmic x-axis (-XX log10 axis)");
85  zap['Y'] = make_field(logy, "logarithmic y-axis (-YY log10 axis)");
86  zap['Z'] = make_field(logz, "logarithmic z-axis");
87  zap['>'] = make_field(xLabel, "x-axis label") = "";
88  zap['<'] = make_field(yLabel, "y-axis label") = "";
89  zap['^'] = make_field(zLabel, "z-axis label") = "";
90  zap['l'] = make_field(lineWidth, "line width") = 2;
91  zap['O'] = make_field(option, "plotting option") = "";
92  zap['B'] = make_field(batch, "batch processing");
93  zap['T'] = make_field(title, "title") = "KM3NeT preliminary";
94  zap['N'] = make_field(Ndivisions, "axis divisioning (e.g. \"X 505\")") = JPARSER::initialised();
95  zap['p'] = make_field(palette, "palette") = 53;
96  zap['d'] = make_field(debug) = 0;
97 
98  zap(argc, argv);
99  }
100  catch(const exception &error) {
101  FATAL(error.what() << endl);
102  }
103 
104 
105  gROOT->SetBatch(batch);
106 
107  TApplication* tp = new TApplication("user", NULL, NULL);
108  TCanvas* cv = new TCanvas("c1", "c1", canvas.x, canvas.y);
109 
110  JSinglePointer<TStyle> gStyle(new JStyle("gplot", cv->GetWw(), cv->GetWh()));
111 
112  gStyle->SetPalette(palette);
113 
114  gROOT->SetStyle("gplot");
115  gROOT->ForceStyle();
116 
117 
118  cv->SetFillStyle(4000);
119  cv->SetFillColor(kWhite);
120  cv->Divide(1,1);
121  cv->cd(1);
122 
123  vector<TAttLine> lineAttributes;
124 
125  lineAttributes.push_back(TAttLine(kBlack, 1, lineWidth));
126  lineAttributes.push_back(TAttLine(kRed, 1, lineWidth));
127  lineAttributes.push_back(TAttLine(kBlue, 1, lineWidth));
128  lineAttributes.push_back(TAttLine(kGreen, 1, lineWidth));
129  lineAttributes.push_back(TAttLine(kMagenta, 1, lineWidth));
130  lineAttributes.push_back(TAttLine(kYellow, 1, lineWidth));
131  lineAttributes.push_back(TAttLine(kCyan, 1, lineWidth));
132 
133 
134  Double_t xmin = +numeric_limits<double>::max();
135  Double_t xmax = -numeric_limits<double>::max();
136  Double_t ymin = +numeric_limits<double>::max();
137  Double_t ymax = -numeric_limits<double>::max();
138  Double_t zmin = +numeric_limits<double>::max();
139  Double_t zmax = -numeric_limits<double>::max();
140 
141  vector<JRootObject> listOfObjects;
142 
143  TH2* master = NULL;
144 
145  for (vector<JRootObjectID>::const_iterator input = inputFile.begin(); input != inputFile.end(); ++input) {
146 
147  DEBUG("Input: " << *input << endl);
148 
149  TDirectory* dir = getDirectory(*input);
150 
151  if (dir == NULL) {
152  ERROR("File: " << input->getFullFilename() << " not opened." << endl);
153  continue;
154  }
155 
156  const TRegexp regexp(input->getObjectName());
157 
158  TIter iter(dir->GetListOfKeys());
159 
160  for (TKey* key; (key = (TKey*) iter.Next()) != NULL; ) {
161 
162  const TString tag(key->GetName());
163 
164  DEBUG("Key: " << tag << " match = " << tag.Index(regexp) << " (-1 /= OK)" << endl);
165 
166  // option match
167 
168  if (tag.Index(regexp) != -1) {
169 
170  JRootObject object(key->ReadObj());
171 
172  try {
173 
174  dynamic_cast<TAttLine&>(*object) = lineAttributes[listOfObjects.size() % lineAttributes.size()];
175  }
176  catch(exception&) {}
177 
178  try {
179 
180  TH2& h2 = dynamic_cast<TH2&>(*object);
181 
182  h2.SetStats(stats != -1);
183 
184  xmin = min(xmin, h2.GetXaxis()->GetXmin());
185  xmax = max(xmax, h2.GetXaxis()->GetXmax());
186  ymin = min(ymin, h2.GetYaxis()->GetXmin());
187  ymax = max(ymax, h2.GetYaxis()->GetXmax());
188  zmin = min(zmin, h2.GetMinimum());
189  zmax = max(zmax, h2.GetMaximum());
190  }
191  catch(exception&) {}
192 
193  try {
194 
195  TGraph& g1 = dynamic_cast<TGraph&>(*object);
196 
197  for (Int_t i = 0; i != g1.GetN(); ++i) {
198 
199  xmin = min(xmin, g1.GetX()[i]);
200  xmax = max(xmax, g1.GetX()[i]);
201  ymin = min(ymin, g1.GetY()[i]);
202  ymax = max(ymax, g1.GetY()[i]);
203  }
204 
205  int ng = 0;
206 
207  for (vector<JRootObject>::iterator i = listOfObjects.begin(); i != listOfObjects.end(); ++i) {
208  if (dynamic_cast<TGraph*>(i->get()) != NULL) {
209  ++ng;
210  }
211  }
212 
213  static_cast<TAttMarker&>(g1) = JMarkerAttributes::getInstance().get(ng);
214  }
215  catch(exception&) {}
216 
217  try {
218 
219  TGraph2D& g2 = dynamic_cast<TGraph2D&>(*object);
220 
221  for (Int_t i = 0; i != g2.GetN(); ++i) {
222 
223  if (!logy || g2.GetZ()[i] > 0.0) {
224  xmin = min(xmin, g2.GetX()[i]);
225  xmax = max(xmax, g2.GetX()[i]);
226  ymin = min(ymin, g2.GetY()[i]);
227  ymax = max(ymax, g2.GetY()[i]);
228  zmin = min(zmin, g2.GetZ()[i]);
229  zmax = max(zmax, g2.GetZ()[i]);
230  }
231  }
232 
233  if (Z != JRange_t()) {
234  g2.SetMinimum(Z.getLowerLimit());
235  g2.SetMaximum(Z.getUpperLimit());
236  }
237 
238  setRange(xmin, xmax, logx == 1);
239  setRange(ymin, ymax, logy == 1);
240  }
241  catch(exception&) {}
242 
243  try {
244 
245  TF2& f2 = dynamic_cast<TF2&>(*object);
246 
247  f2.SetLineColor(kRed);
248  f2.SetTitle((title + ";" + xLabel + ";" + yLabel + ";" + zLabel).c_str());
249  }
250  catch(exception&) {}
251 
252  for (TString buffer[] = { object.getLabel(), input->getFilename().c_str(), "" }, *i = buffer; *i != ""; ++i) {
253 
254  *i = (*i)(TRegexp("\\[.*\\]"));
255 
256  if ((*i).Length() > 2) {
257  object.setLabel((*i)(1, (*i).Length() - 2));
258  }
259  }
260 
261  DEBUG("Add object: " << tag << " with label " << object.getLabel() << endl);
262 
263  if (dynamic_cast<TH2*> (object.get()) != NULL ||
264  dynamic_cast<TGraph*> (object.get()) != NULL ||
265  dynamic_cast<TGraph2D*>(object.get()) != NULL ||
266  dynamic_cast<TF2*> (object.get()) != NULL) {
267 
268  if (master == NULL) {
269  master = dynamic_cast<TH2*>(object.get());
270  }
271 
272  listOfObjects.push_back(object);
273 
274  } else {
275  ERROR("For other objects than 2D histograms, use JPlot1D" << endl);
276  }
277  }
278  }
279  }
280 
281  if (listOfObjects.empty()) {
282  ERROR("Nothing to draw." << endl);
283  }
284 
285  // plot frame
286 
287  cv->cd(1);
288 
289  if (option.find("COLZ") != string::npos ||
290  option.find("colz") != string::npos) {
291  gPad->SetRightMargin(0.20);
292  }
293 
294  if (X != JRange_t()) {
295  xmin = X.getLowerLimit();
296  xmax = X.getUpperLimit();
297  }
298 
299  if (Y != JRange_t()) {
300  ymin = Y.getLowerLimit();
301  ymax = Y.getUpperLimit();
302  }
303 
304  if (Z != JRange_t()) {
305  zmin = Z.getLowerLimit();
306  zmax = Z.getUpperLimit();
307  } else {
308  setRange(zmin, zmax, logz);
309  }
310 
311  if (master == NULL) {
312 
313  if (X != JRange_t() &&
314  Y != JRange_t()) {
315 
316  master = new TH2D("__H__", NULL,
317  100, X.getLowerLimit(), X.getUpperLimit(),
318  100, Y.getLowerLimit(), Y.getUpperLimit());
319 
320  master->SetStats(kFALSE);
321 
322  } else if (xmin < xmax && ymin < ymax) {
323 
324  master = new TH2D("__H__", NULL,
325  100, xmin, xmax,
326  100, ymin, ymax);
327 
328  master->SetStats(kFALSE);
329 
330  } else {
331 
332  TText* p = new TText(0.5, 0.5, MAKE_CSTRING("No data"));
333 
334  p->SetTextAlign(21);
335  p->SetTextAngle(45);
336  p->Draw();
337  }
338  }
339 
340  if (master != NULL) {
341 
342  if (logx) { gPad->SetLogx(); }
343  if (logy) { gPad->SetLogy(); }
344  if (logz) { gPad->SetLogz(); }
345 
346  master->GetXaxis()->SetRangeUser(xmin, xmax);
347  master->GetYaxis()->SetRangeUser(ymin, ymax);
348 
349  if (logx > 1) { setLogarithm(master->GetXaxis()); }
350  if (logy > 1) { setLogarithm(master->GetYaxis()); }
351 
352  master->SetTitle(title.c_str());
353 
354  master->SetMinimum(zmin);
355  master->SetMaximum(zmax);
356 
357  if (xLabel != "") { master->GetXaxis()->SetTitle(xLabel.c_str()); master->GetXaxis()->CenterTitle(true); }
358  if (yLabel != "") { master->GetYaxis()->SetTitle(yLabel.c_str()); master->GetYaxis()->CenterTitle(true); }
359  if (zLabel != "") { master->GetZaxis()->SetTitle(zLabel.c_str()); master->GetZaxis()->CenterTitle(true); }
360 
361  master->GetXaxis()->SetMoreLogLabels((logx == 1 && log10(xmax/xmin) < 2) ||
362  (logx > 1 && xmax-xmin < 2));
363  master->GetYaxis()->SetMoreLogLabels((logy == 1 && log10(ymax/ymin) < 2) ||
364  (logy > 1 && ymax-ymin < 2));
365 
366  if (Ndivisions.first != "") {
367  master->SetNdivisions(Ndivisions.second, Ndivisions.first.c_str());
368  }
369 
370  master->Draw(option.c_str());
371  }
372 
373  if (logx > 1 || logy > 1) {
374 
375  for (vector<JRootObject>::iterator i = listOfObjects.begin(); i != listOfObjects.end(); ++i) {
376 
377  TH2* h2 = dynamic_cast<TH2*>(i->get());
378 
379  if (h2 != NULL && h2 != master) {
380  if (logx > 1) { setLogarithm(h2->GetXaxis()); }
381  if (logy > 1) { setLogarithm(h2->GetYaxis()); }
382  }
383  }
384  }
385 
386  if (stats != -1)
387  gStyle->SetOptStat(stats);
388  else
389  gStyle->SetOptFit(kFALSE);
390 
391  for (vector<JRootObject>::iterator i = listOfObjects.begin(); i != listOfObjects.end(); ++i) {
392 
393  DEBUG("Draw " << (*i)->GetName() << ' ' << (*i)->GetTitle() << endl);
394 
395  string buffer(option);
396 
397  buffer += "SAME";
398 
399  try {
400 
401  TH2& h2 = dynamic_cast<TH2&>(*(*i));
402 
403  h2.SetMinimum(zmin);
404  h2.SetMaximum(zmax);
405  }
406  catch(exception&) {}
407 
408  try {
409 
410  TGraph& g1 = dynamic_cast<TGraph&>(*(*i));
411 
412  buffer = "SAMEP";
413  }
414  catch(exception&) {}
415 
416  try {
417 
418  TGraph2D& g2 = dynamic_cast<TGraph2D&>(*(*i));
419 
420  g2.SetMinimum(zmin);
421  g2.SetMaximum(zmax);
422  }
423  catch(exception&) {}
424 
425  try {
426 
427  TF2& f2 = dynamic_cast<TF2&>(*(*i));
428 
429  f2.SetRange(xmin, ymin, zmin,
430  xmax, ymax, zmax);
431  }
432  catch(exception&) {}
433 
434  (*i)->Draw(buffer.c_str());
435  }
436 
437  //gPad->RedrawAxis();
438 
439  cv->Update();
440 
441  if (outputFile != "") {
442  cv->SaveAs(outputFile.c_str());
443  }
444 
445  if (!batch) {
446  tp->Run();
447  }
448 }
Utility class to parse command line options.
Definition: JParser.hh:1410
#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.
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 ERROR(A)
Definition: JMessage.hh:64
int debug
debug level
Definition: JSirene.cc:59
#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