Jpp
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 
39 /**
40  * \file
41  * General purpose plot program for 2D ROOT objects.
42  * The option <tt>-f</tt> corresponds to <tt><file name>:<object name></tt>.
43  * \author mdejong
44  */
45 int main(int argc, char **argv)
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.Contains(regexp) << endl);
165 
166  // option match
167 
168  if (tag.Contains(regexp)) {
169 
170  if (title == "?") {
171  title = key->GetName();
172  }
173 
174  JRootObject object(key->ReadObj());
175 
176  try {
177 
178  dynamic_cast<TAttLine&>(*object) = lineAttributes[listOfObjects.size() % lineAttributes.size()];
179  }
180  catch(exception&) {}
181 
182  try {
183 
184  TH2& h2 = dynamic_cast<TH2&>(*object);
185 
186  h2.SetStats(stats != -1);
187 
188  xmin = min(xmin, h2.GetXaxis()->GetXmin());
189  xmax = max(xmax, h2.GetXaxis()->GetXmax());
190  ymin = min(ymin, h2.GetYaxis()->GetXmin());
191  ymax = max(ymax, h2.GetYaxis()->GetXmax());
192  zmin = min(zmin, h2.GetMinimum());
193  zmax = max(zmax, h2.GetMaximum());
194  }
195  catch(exception&) {}
196 
197  try {
198 
199  TGraph& g1 = dynamic_cast<TGraph&>(*object);
200 
201  for (Int_t i = 0; i != g1.GetN(); ++i) {
202 
203  xmin = min(xmin, g1.GetX()[i]);
204  xmax = max(xmax, g1.GetX()[i]);
205  ymin = min(ymin, g1.GetY()[i]);
206  ymax = max(ymax, g1.GetY()[i]);
207  }
208 
209  int ng = 0;
210 
211  for (vector<JRootObject>::iterator i = listOfObjects.begin(); i != listOfObjects.end(); ++i) {
212  if (dynamic_cast<TGraph*>(i->get()) != NULL) {
213  ++ng;
214  }
215  }
216 
217  static_cast<TAttMarker&>(g1) = JMarkerAttributes::getInstance().get(ng);
218  }
219  catch(exception&) {}
220 
221  try {
222 
223  TGraph2D& g2 = dynamic_cast<TGraph2D&>(*object);
224 
225  for (Int_t i = 0; i != g2.GetN(); ++i) {
226 
227  if (!logy || g2.GetZ()[i] > 0.0) {
228  xmin = min(xmin, g2.GetX()[i]);
229  xmax = max(xmax, g2.GetX()[i]);
230  ymin = min(ymin, g2.GetY()[i]);
231  ymax = max(ymax, g2.GetY()[i]);
232  zmin = min(zmin, g2.GetZ()[i]);
233  zmax = max(zmax, g2.GetZ()[i]);
234  }
235  }
236 
237  if (Z != JRange_t()) {
238  g2.SetMinimum(Z.getLowerLimit());
239  g2.SetMaximum(Z.getUpperLimit());
240  }
241 
242  setRange(ymin, ymax, logy == 1);
243  }
244  catch(exception&) {}
245 
246  try {
247 
248  TF2& f2 = dynamic_cast<TF2&>(*object);
249 
250  f2.SetLineColor(kRed);
251  f2.SetTitle((title + ";" + xLabel + ";" + yLabel + ";" + zLabel).c_str());
252 
253  if (xmin != +numeric_limits<double>::max() &&
254  xmax != -numeric_limits<double>::max() &&
255  ymin != +numeric_limits<double>::max() &&
256  ymax != -numeric_limits<double>::max()) {
257  zmin = min(zmin, f2.GetMinimum(xmin, xmax, ymin, ymax));
258  zmax = max(zmax, f2.GetMaximum(xmin, xmax, ymin, ymax));
259  }
260  }
261  catch(exception&) {}
262 
263  for (TString buffer[] = { object.getLabel(), input->getFilename().c_str(), "" }, *i = buffer; *i != ""; ++i) {
264 
265  *i = (*i)(TRegexp("\\[.*\\]"));
266 
267  if ((*i).Length() > 2) {
268  object.setLabel((*i)(1, (*i).Length() - 2));
269  }
270  }
271 
272  DEBUG("Add object: " << tag << " with label " << object.getLabel() << endl);
273 
274  if (dynamic_cast<TH2*> (object.get()) != NULL ||
275  dynamic_cast<TGraph*> (object.get()) != NULL ||
276  dynamic_cast<TGraph2D*>(object.get()) != NULL ||
277  dynamic_cast<TF2*> (object.get()) != NULL) {
278 
279  if (master == NULL) {
280  master = dynamic_cast<TH2*>(object.get());
281  }
282 
283  listOfObjects.push_back(object);
284 
285  } else {
286  ERROR("For other objects than 2D histograms, use JPlot1D" << endl);
287  }
288  }
289  }
290  }
291 
292  if (listOfObjects.empty()) {
293  ERROR("Nothing to draw." << endl);
294  }
295 
296  // plot frame
297 
298  cv->cd(1);
299 
300  if (option.find("COLZ") != string::npos ||
301  option.find("colz") != string::npos) {
302  gPad->SetRightMargin(0.20);
303  }
304 
305  if (X != JRange_t()) {
306  xmin = X.getLowerLimit();
307  xmax = X.getUpperLimit();
308  }
309 
310  if (Y != JRange_t()) {
311  ymin = Y.getLowerLimit();
312  ymax = Y.getUpperLimit();
313  }
314 
315  if (Z != JRange_t()) {
316  zmin = Z.getLowerLimit();
317  zmax = Z.getUpperLimit();
318  } else {
319  setRange(zmin, zmax, logz);
320  }
321 
322  if (master == NULL) {
323 
324  if (X != JRange_t() &&
325  Y != JRange_t()) {
326 
327  master = new TH2D("__H__", NULL,
328  100, X.getLowerLimit(), X.getUpperLimit(),
329  100, Y.getLowerLimit(), Y.getUpperLimit());
330 
331  master->SetStats(kFALSE);
332 
333  } else if (xmin < xmax && ymin < ymax) {
334 
335  master = new TH2D("__H__", NULL,
336  100, xmin, xmax,
337  100, ymin, ymax);
338 
339  master->SetStats(kFALSE);
340 
341  } else {
342 
343  TText* p = new TText(0.5, 0.5, MAKE_CSTRING("No data"));
344 
345  p->SetTextAlign(21);
346  p->SetTextAngle(45);
347  p->Draw();
348  }
349  }
350 
351  if (master != NULL) {
352 
353  if (logx) { gPad->SetLogx(); }
354  if (logy) { gPad->SetLogy(); }
355  if (logz) { gPad->SetLogz(); }
356 
357  master->GetXaxis()->SetRangeUser(xmin, xmax);
358  master->GetYaxis()->SetRangeUser(ymin, ymax);
359 
360  if (logx > 1) { setLogarithm(master->GetXaxis()); }
361  if (logy > 1) { setLogarithm(master->GetYaxis()); }
362 
363  master->SetTitle(title.c_str());
364 
365  master->SetMinimum(zmin);
366  master->SetMaximum(zmax);
367 
368  if (xLabel != "") { master->GetXaxis()->SetTitle(xLabel.c_str()); master->GetXaxis()->CenterTitle(true); }
369  if (yLabel != "") { master->GetYaxis()->SetTitle(yLabel.c_str()); master->GetYaxis()->CenterTitle(true); }
370  if (zLabel != "") { master->GetZaxis()->SetTitle(zLabel.c_str()); master->GetZaxis()->CenterTitle(true); }
371 
372  master->GetXaxis()->SetMoreLogLabels((logx == 1 && log10(xmax/xmin) < 2) ||
373  (logx > 1 && xmax-xmin < 2));
374  master->GetYaxis()->SetMoreLogLabels((logy == 1 && log10(ymax/ymin) < 2) ||
375  (logy > 1 && ymax-ymin < 2));
376 
377  if (Ndivisions.first != "") {
378  master->SetNdivisions(Ndivisions.second, Ndivisions.first.c_str());
379  }
380 
381  master->Draw(option.c_str());
382  }
383 
384  if (logx > 1 || logy > 1) {
385 
386  for (vector<JRootObject>::iterator i = listOfObjects.begin(); i != listOfObjects.end(); ++i) {
387 
388  TH2* h2 = dynamic_cast<TH2*>(i->get());
389 
390  if (h2 != NULL && h2 != master) {
391  if (logx > 1) { setLogarithm(h2->GetXaxis()); }
392  if (logy > 1) { setLogarithm(h2->GetYaxis()); }
393  }
394  }
395  }
396 
397  if (stats != -1)
398  gStyle->SetOptStat(stats);
399  else
400  gStyle->SetOptFit(kFALSE);
401 
402  for (vector<JRootObject>::iterator i = listOfObjects.begin(); i != listOfObjects.end(); ++i) {
403 
404  DEBUG("Draw " << (*i)->GetName() << ' ' << (*i)->GetTitle() << endl);
405 
406  string buffer(option);
407 
408  buffer += "SAME";
409 
410  try {
411 
412  TH2& h2 = dynamic_cast<TH2&>(*(*i));
413 
414  h2.SetMinimum(zmin);
415  h2.SetMaximum(zmax);
416  }
417  catch(exception&) {}
418 
419  try {
420 
421  TGraph& g1 = dynamic_cast<TGraph&>(*(*i));
422 
423  buffer = "SAMEP";
424  }
425  catch(exception&) {}
426 
427  try {
428 
429  TGraph2D& g2 = dynamic_cast<TGraph2D&>(*(*i));
430 
431  g2.SetMinimum(zmin);
432  g2.SetMaximum(zmax);
433  }
434  catch(exception&) {}
435 
436  try {
437 
438  TF2& f2 = dynamic_cast<TF2&>(*(*i));
439 
440  f2.SetRange(xmin, ymin, zmin,
441  xmax, ymax, zmax);
442  }
443  catch(exception&) {}
444 
445  (*i)->Draw(buffer.c_str());
446  }
447 
448  //gPad->RedrawAxis();
449 
450  cv->Update();
451 
452  if (outputFile != "") {
453  cv->SaveAs(outputFile.c_str());
454  }
455 
456  if (!batch) {
457  tp->Run();
458  }
459 }
JCanvas.hh
JRootObject.hh
main
int main(int argc, char **argv)
Definition: JPlot2D.cc:45
JMessage.hh
JLANG::getInstance
T & getInstance(const T &object)
Get static instance from temporary object.
Definition: JObject.hh:73
JMarkerAttributes.hh
g1
Double_t g1(const Double_t x)
Function.
Definition: JQuantiles.cc:25
JSinglePointer.hh
JPARSER::initialised
Empty structure for specification of parser element that is initialised (i.e.
Definition: JParser.hh:63
std::vector
Definition: JSTDTypes.hh:12
JPARSER::JParser
Utility class to parse command line options.
Definition: JParser.hh:1493
JGizmoToolkit.hh
JGIZMO::setLogarithm
void setLogarithm(TAxis *axis)
Make axis logarithmic (e.g.
Definition: JGizmoToolkit.hh:336
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
MAKE_CSTRING
#define MAKE_CSTRING(A)
Make C-string.
Definition: JPrint.hh:708
ERROR
#define ERROR(A)
Definition: JMessage.hh:66
JPARSER::JCounter
Auxiliary class to handle multiple boolean-like I/O.
Definition: JParser.hh:218
JRange.hh
JStyle.hh
debug
int debug
debug level
Definition: JSirene.cc:59
JRootObjectID.hh
JGIZMO::getDirectory
TDirectory * getDirectory(const JRootObjectID &id)
Get TDirectory pointer.
Definition: JGizmoToolkit.hh:121
std::pair
Definition: JSTDTypes.hh:15
JPHYSICS::getLabel
std::string getLabel(const JPDFType_t pdf)
Get PDF label.
Definition: JPDFTypes.hh:60
JParser.hh
JGIZMO::setRange
void setRange(double &xmin, double &xmax, const bool logx)
Set axis range.
Definition: JGizmoToolkit.hh:572
make_field
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1954
DEBUG
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
std
Definition: jaanetDictionary.h:36
JLineAttributes.hh
JAANET::get
T get(const JHead &header)
Get object from header.
Definition: JHeadToolkit.hh:295
FATAL
#define FATAL(A)
Definition: JMessage.hh:67
outputFile
string outputFile
Definition: JDAQTimesliceSelector.cc:37