Jpp - the software that should make you happy
 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 "TH3.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 51 of file JPlot2D.cc.

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