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 "JROOT/JStyle.hh"
#include "JROOT/JCanvas.hh"
#include "JROOT/JMarkerAttributes.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 50 of file JPlot2D.cc.

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