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