Jpp  16.0.0
the software that should make you happy
 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 <set>
5 #include <cmath>
6 
7 #include "TROOT.h"
8 #include "TFile.h"
9 #include "TClass.h"
10 #include "TApplication.h"
11 #include "TCanvas.h"
12 #include "TKey.h"
13 #include "TStyle.h"
14 #include "TAttMarker.h"
15 #include "TAttLine.h"
16 #include "TH2.h"
17 #include "TH3.h"
18 #include "TH2D.h"
19 #include "TGraph.h"
20 #include "TGraphErrors.h"
21 #include "TGraph2D.h"
22 #include "TGraph2DErrors.h"
23 #include "TEllipse.h"
24 #include "TLine.h"
25 #include "TF2.h"
26 #include "TString.h"
27 #include "TRegexp.h"
28 #include "TText.h"
29 
30 #include "JTools/JRange.hh"
31 #include "JLang/JSinglePointer.hh"
32 #include "JROOT/JStyle.hh"
33 #include "JROOT/JCanvas.hh"
35 #include "JGizmo/JRootObjectID.hh"
36 #include "JGizmo/JRootObject.hh"
37 #include "JGizmo/JGizmoToolkit.hh"
38 
39 #include "Jeep/JParser.hh"
40 #include "Jeep/JMessage.hh"
41 
42 namespace {
43 
44  /**
45  * Set x of object to logarithmic.
46  *
47  * \param object pointer to object
48  * \return true if set; else false
49  */
50  template<class T>
51  inline bool setLogX(TObject* object)
52  {
53  using namespace JPP;
54 
55  T* p = dynamic_cast<T*>(object);
56 
57  if (p != NULL) {
58 
59  setLogarithmicX(p);
60 
61  return true;
62  }
63 
64  return false;
65  }
66 
67  /**
68  * Set y of object to logarithmic.
69  *
70  * \param object pointer to object
71  * \return true if set; else false
72  */
73  template<class T>
74  inline bool setLogY(TObject* object)
75  {
76  using namespace JPP;
77 
78  T* p = dynamic_cast<T*>(object);
79 
80  if (p != NULL) {
81 
82  setLogarithmicY(p);
83 
84  return true;
85  }
86 
87  return false;
88  }
89 
90  const std::string MASTER = "__H__"; //!< Name of prototype
91 
92  const char* const JName_t = "?"; //!< Draw histogram name as title of plot
93  const char* const JTitle_t = "%"; //!< Draw histogram title as title of plot
94 }
95 
96 
97 /**
98  * \file
99  * General purpose plot program for 2D ROOT objects.
100  * The option <tt>-f</tt> corresponds to <tt><file name>:<object name></tt>.
101  * \author mdejong
102  */
103 int main(int argc, char **argv)
104 {
105  using namespace std;
106  using namespace JPP;
107 
108  typedef JRange<double> JRange_t;
109  typedef pair<string, int> division_type;
110 
111  vector<JRootObjectID> inputFile;
112  string outputFile;
113  JCanvas canvas;
114  int stats;
115  JRange_t X;
116  JRange_t Y;
117  JRange_t Z;
118  JCounter logx;
119  JCounter logy;
120  bool logz;
121  string project;
122  string xLabel;
123  string yLabel;
124  string zLabel;
125  double markerSize;
126  string option;
127  set<char> grid;
128  bool batch;
129  string title;
130  vector<division_type> Ndivisions;
131  int palette;
132  int debug;
133 
134  try {
135 
136  JParser<> zap("General purpose plot program for 2D ROOT objects.");
137 
138  zap['f'] = make_field(inputFile, "<input file>:<object name>");
139  zap['o'] = make_field(outputFile, "graphics output") = "";
140  zap['w'] = make_field(canvas, "size of canvas <nx>x<ny> [pixels]") = JCanvas(500, 500);
141  zap['s'] = make_field(stats) = -1;
142  zap['x'] = make_field(X, "x-abscissa range") = JRange_t();
143  zap['y'] = make_field(Y, "y-abscissa range") = JRange_t();
144  zap['z'] = make_field(Z, "ordinate range") = JRange_t();
145  zap['X'] = make_field(logx, "logarithmic x-axis (-XX log10 axis)");
146  zap['Y'] = make_field(logy, "logarithmic y-axis (-YY log10 axis)");
147  zap['Z'] = make_field(logz, "logarithmic z-axis");
148  zap['P'] = make_field(project, "projection") = "", "xy", "yx", "xz", "zx", "yz", "zy";
149  zap['>'] = make_field(xLabel, "x-axis label") = "";
150  zap['<'] = make_field(yLabel, "y-axis label") = "";
151  zap['^'] = make_field(zLabel, "z-axis label") = "";
152  zap['S'] = make_field(markerSize, "marker size") = 1.0;
153  zap['O'] = make_field(option, "plotting option") = "";
154  zap['G'] = make_field(grid, "grid lines [X][Y]") = JPARSER::initialised();
155  zap['B'] = make_field(batch, "batch processing");
156  zap['T'] = make_field(title, "title") = "KM3NeT preliminary";
157  zap['N'] = make_field(Ndivisions, "axis divisioning (e.g. \"X 505\")") = JPARSER::initialised();
158  zap['p'] = make_field(palette, "palette") = -1;
159  zap['d'] = make_field(debug) = 0;
160 
161  zap(argc, argv);
162  }
163  catch(const exception &error) {
164  FATAL(error.what() << endl);
165  }
166 
167 
168  gROOT->SetBatch(batch);
169 
170  TApplication* tp = new TApplication("user", NULL, NULL);
171  TCanvas* cv = new TCanvas("c1", "c1", canvas.x, canvas.y);
172 
173  JSinglePointer<TStyle> gStyle(new JStyle("gplot", cv->GetWw(), cv->GetWh()));
174 
175  if (palette != -1) {
176  gStyle->SetPalette(palette);
177  }
178 
179  gROOT->SetStyle("gplot");
180  gROOT->ForceStyle();
181 
182 
183  cv->SetFillStyle(4000);
184  cv->SetFillColor(kWhite);
185  cv->Divide(1,1);
186  cv->cd(1);
187 
188  JMarkerAttributes::getInstance().setMarkerSize(markerSize);
189 
190  Double_t xmin = numeric_limits<double>::max();
191  Double_t xmax = numeric_limits<double>::lowest();
192  Double_t ymin = numeric_limits<double>::max();
193  Double_t ymax = numeric_limits<double>::lowest();
194  Double_t zmin = numeric_limits<double>::max();
195  Double_t zmax = numeric_limits<double>::lowest();
196 
197  vector<JRootObject> listOfObjects;
198 
199  TH2* master = NULL;
200 
201  for (vector<JRootObjectID>::const_iterator input = inputFile.begin(); input != inputFile.end(); ++input) {
202 
203  DEBUG("Input: " << *input << endl);
204 
205  TDirectory* dir = getDirectory(*input);
206 
207  if (dir == NULL) {
208  ERROR("File: " << input->getFullFilename() << " not opened." << endl);
209  continue;
210  }
211 
212  const TRegexp regexp(input->getObjectName());
213 
214  TIter iter(dir->GetListOfKeys());
215 
216  for (TKey* key; (key = (TKey*) iter.Next()) != NULL; ) {
217 
218  const TString tag(key->GetName());
219 
220  DEBUG("Key: " << tag << " match = " << tag.Contains(regexp) << endl);
221 
222  // option match
223 
224  if (tag.Contains(regexp) && isTObject(key)) {
225 
226  if (title == JName_t) {
227  title = key->GetName();
228  } else if (title == JTitle_t) {
229  title = key->GetTitle();
230  }
231 
232  JRootObject object(key->ReadObj());
233 
234  try {
235 
236  TH3& h3 = dynamic_cast<TH3&>(*object);
237 
238  object = h3.Project3D(project.c_str());
239  }
240  catch(exception&) {}
241 
242  try {
243 
244  TH2& h2 = dynamic_cast<TH2&>(*object);
245 
246  h2.SetStats(stats != -1);
247 
248  xmin = min(xmin, h2.GetXaxis()->GetXmin());
249  xmax = max(xmax, h2.GetXaxis()->GetXmax());
250  ymin = min(ymin, h2.GetYaxis()->GetXmin());
251  ymax = max(ymax, h2.GetYaxis()->GetXmax());
252  zmin = min(zmin, logz ? h2.GetMinimum(0.0) : h2.GetMinimum());
253  zmax = max(zmax, h2.GetMaximum());
254  }
255  catch(exception&) {}
256 
257  try {
258 
259  TGraph& g1 = dynamic_cast<TGraph&>(*object);
260 
261  for (Int_t i = 0; i != g1.GetN(); ++i) {
262  xmin = min(xmin, g1.GetX()[i] - numeric_limits<float>::epsilon());
263  xmax = max(xmax, g1.GetX()[i] + numeric_limits<float>::epsilon());
264  ymin = min(ymin, g1.GetY()[i] - numeric_limits<float>::epsilon());
265  ymax = max(ymax, g1.GetY()[i] + numeric_limits<float>::epsilon());
266  }
267 
268  int ng = 0;
269 
270  for (vector<JRootObject>::iterator i = listOfObjects.begin(); i != listOfObjects.end(); ++i) {
271  if (dynamic_cast<TGraph*>(i->get()) != NULL) {
272  ++ng;
273  }
274  }
275 
276  static_cast<TAttMarker&>(g1) = JMarkerAttributes::getInstance().get(ng);
277  }
278  catch(exception&) {}
279 
280  try {
281 
282  TGraph2D& g2 = dynamic_cast<TGraph2D&>(*object);
283 
284  for (Int_t i = 0; i != g2.GetN(); ++i) {
285  if (!logz || g2.GetZ()[i] > 0.0) {
286  xmin = min(xmin, g2.GetX()[i]);
287  xmax = max(xmax, g2.GetX()[i]);
288  ymin = min(ymin, g2.GetY()[i]);
289  ymax = max(ymax, g2.GetY()[i]);
290  zmin = min(zmin, g2.GetZ()[i]);
291  zmax = max(zmax, g2.GetZ()[i]);
292  }
293  }
294 
295  if (Z != JRange_t()) {
296  g2.SetMinimum(Z.getLowerLimit());
297  g2.SetMaximum(Z.getUpperLimit());
298  }
299  }
300  catch(exception&) {}
301 
302  try {
303 
304  TF2& f2 = dynamic_cast<TF2&>(*object);
305 
306  f2.SetLineColor(kRed);
307  f2.SetTitle((title + ";" + xLabel + ";" + yLabel + ";" + zLabel).c_str());
308 
309  double __xmin;
310  double __xmax;
311  double __ymin;
312  double __ymax;
313 
314  f2.GetRange(__xmin, __ymin, __xmax, __ymax);
315 
316  xmin = min(xmin, __xmin);
317  xmax = max(xmax, __xmax);
318  ymin = min(ymin, __ymin);
319  ymax = max(ymax, __ymax);
320  zmin = min(zmin, f2.GetMinimum());
321  zmax = max(zmax, f2.GetMaximum());
322  }
323  catch(exception&) {}
324 
325  // label
326 
327  for (TString buffer[] = { object.getLabel(), input->getFilename().c_str(), "" }, *i = buffer; *i != ""; ++i) {
328 
329  *i = (*i)(TRegexp("\\[.*\\]"));
330 
331  if ((*i).Length() > 2) {
332  object.setLabel((*i)(1, (*i).Length() - 2));
333  }
334  }
335 
336  if (dynamic_cast<TH2*> (object.get()) != NULL ||
337  dynamic_cast<TGraph*> (object.get()) != NULL ||
338  dynamic_cast<TGraph2D*>(object.get()) != NULL ||
339  dynamic_cast<TEllipse*>(object.get()) != NULL ||
340  dynamic_cast<TLine*> (object.get()) != NULL ||
341  dynamic_cast<TF2*> (object.get()) != NULL) {
342 
343  DEBUG("Add object: " << tag << " with label <" << object.getLabel() << ">" << endl);
344 
345  if (master == NULL) {
346  master = dynamic_cast<TH2*>(object.get());
347  }
348 
349  listOfObjects.push_back(object);
350 
351  } else {
352  ERROR("For other objects than 2D histograms, use JPlot1D" << endl);
353  }
354  }
355  }
356  }
357 
358  if (listOfObjects.empty()) {
359  ERROR("Nothing to draw." << endl);
360  }
361 
362  // plot frame
363 
364  cv->cd(1);
365 
366  if (option.find("COLZ") != string::npos ||
367  option.find("colz") != string::npos) {
368  gPad->SetRightMargin(0.20);
369  }
370 
371  if (X != JRange_t()) {
372  xmin = X.getLowerLimit();
373  xmax = X.getUpperLimit();
374  }
375 
376  if (Y != JRange_t()) {
377  ymin = Y.getLowerLimit();
378  ymax = Y.getUpperLimit();
379  }
380 
381  if (Z != JRange_t()) {
382  zmin = Z.getLowerLimit();
383  zmax = Z.getUpperLimit();
384  } else {
385  setRange(zmin, zmax, logz);
386  }
387 
388  if (!listOfObjects.empty()) {
389 
390  if (master == NULL) {
391 
392  master = new TH2D(MASTER.c_str(), NULL,
393  100, xmin, xmax,
394  100, ymin, ymax);
395 
396  master->SetStats(kFALSE);
397  }
398  }
399 
400  if (master == NULL) {
401 
402  TText* p = new TText(0.5, 0.5, MAKE_CSTRING("No data"));
403 
404  p->SetTextAlign(21);
405  p->SetTextAngle(45);
406  p->Draw();
407 
408  } else {
409 
410  if (logx) { gPad->SetLogx(); }
411  if (logy) { gPad->SetLogy(); }
412  if (logz) { gPad->SetLogz(); }
413 
414  master->GetXaxis()->SetRangeUser(xmin, xmax);
415  master->GetYaxis()->SetRangeUser(ymin, ymax);
416 
417  if (logx > 1) { setLogarithmicX(master); }
418  if (logy > 1) { setLogarithmicY(master); }
419 
420  master->SetTitle(title.c_str());
421 
422  master->SetMinimum(zmin);
423  master->SetMaximum(zmax);
424 
425  if (xLabel != "") { master->GetXaxis()->SetTitle(xLabel.c_str()); master->GetXaxis()->CenterTitle(true); }
426  if (yLabel != "") { master->GetYaxis()->SetTitle(yLabel.c_str()); master->GetYaxis()->CenterTitle(true); }
427  if (zLabel != "") { master->GetZaxis()->SetTitle(zLabel.c_str()); master->GetZaxis()->CenterTitle(true); }
428 
429  master->GetXaxis()->SetMoreLogLabels((logx == 1 && log10(xmax/xmin) < 2) ||
430  (logx > 1 && xmax-xmin < 2));
431  master->GetYaxis()->SetMoreLogLabels((logy == 1 && log10(ymax/ymin) < 2) ||
432  (logy > 1 && ymax-ymin < 2));
433 
434  for (vector<division_type>::const_iterator i = Ndivisions.begin(); i != Ndivisions.end(); ++i) {
435  master->SetNdivisions(i->second, i->first.c_str());
436  }
437 
438  DEBUG("Draw " << master->GetName() << ' ' << option << endl);
439 
440  master->Draw(option.c_str());
441  }
442 
443  if (logx > 1 || logy > 1) {
444  for (vector<JRootObject>::iterator i = listOfObjects.begin(); i != listOfObjects.end(); ++i) {
445  if (dynamic_cast<TH2*>(i->get()) != master) {
446  if (logx > 1) {
447  if (setLogX<TH2> (i->get())) {}
448  else if (setLogX<TF2> (i->get())) {}
449  else if (setLogX<TGraph2DErrors>(i->get())) {}
450  else if (setLogX<TGraph2D> (i->get())) {}
451  else if (setLogX<TGraphErrors> (i->get())) {}
452  else if (setLogX<TGraph> (i->get())) {}
453  else if (setLogX<TLine> (i->get())) {}
454  else if (setLogX<TEllipse> (i->get())) {}
455  }
456  if (logy > 1) {
457  if (setLogY<TH2> (i->get())) {}
458  else if (setLogY<TF2> (i->get())) {}
459  else if (setLogY<TGraph2DErrors>(i->get())) {}
460  else if (setLogY<TGraph2D> (i->get())) {}
461  else if (setLogY<TGraphErrors> (i->get())) {}
462  else if (setLogY<TGraph> (i->get())) {}
463  else if (setLogY<TLine> (i->get())) {}
464  else if (setLogY<TEllipse> (i->get())) {}
465  }
466  }
467  }
468  }
469 
470  if (grid.count('x') || grid.count('X')) { gPad->SetGridx(); }
471  if (grid.count('y') || grid.count('Y')) { gPad->SetGridy(); }
472 
473  if (stats != -1)
474  gStyle->SetOptStat(stats);
475  else
476  gStyle->SetOptFit(kFALSE);
477 
478  for (vector<JRootObject>::iterator i = listOfObjects.begin(); i != listOfObjects.end(); ++i) {
479 
480  DEBUG("Draw " << (*i)->GetName() << ' ' << (*i)->GetTitle() << endl);
481 
482  string buffer(option);
483 
484  buffer += "SAME";
485 
486  try {
487 
488  TH2& h2 = dynamic_cast<TH2&>(*(*i));
489 
490  h2.SetMinimum(zmin);
491  h2.SetMaximum(zmax);
492  }
493  catch(exception&) {}
494 
495  try {
496 
497  TGraph& g1 = dynamic_cast<TGraph&>(*(*i));
498 
499  buffer = "P";
500  }
501  catch(exception&) {}
502 
503  try {
504 
505  TGraph2D& g2 = dynamic_cast<TGraph2D&>(*(*i));
506 
507  g2.SetMinimum(zmin);
508  g2.SetMaximum(zmax);
509  }
510  catch(exception&) {}
511 
512  try {
513 
514  TF2& f2 = dynamic_cast<TF2&>(*(*i));
515 
516  f2.SetNpx(1000);
517  f2.SetNpy(1000);
518  /*
519  f2.SetRange(xmin, ymin, zmin,
520  xmax, ymax, zmax);
521  */
522  }
523  catch(exception&) {}
524 
525  (*i)->Draw(buffer.c_str());
526  }
527 
528  gPad->RedrawAxis();
529 
530  cv->Update();
531 
532  if (outputFile != "") {
533  cv->SaveAs(outputFile.c_str());
534  }
535 
536  if (!batch) {
537  tp->Run();
538  }
539 
540  return (master != NULL ? 0 : 1);
541 }
Utility class to parse command line options.
Definition: JParser.hh:1500
then echo Test string reversed by master(hit< return > to continue)." $DIR/JProcess -c "$DIR/JEcho" -rC fi if (( 1 ))
int main(int argc, char *argv[])
Definition: Main.cc:15
std::string getLabel(const JLocation &location)
Get module label for monitoring and other applications.
Definition: JLocation.hh:246
Definition: JRoot.hh:19
#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
void setLogarithmicX(TList *list)
Make x-axis of objects in list logarithmic (e.g. after using log10()).
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
set_variable E_E log10(E_{fit}/E_{#mu})"
do set_variable OUTPUT_DIRECTORY $WORKDIR T
#define ERROR(A)
Definition: JMessage.hh:66
then break fi done getCenter read X Y Z let X
int debug
debug level
Definition: JSirene.cc:63
General purpose messaging.
void setLogarithmicY(TList *list)
Make y-axis of objects in list logarithmic (e.g. after using log10()).
#define FATAL(A)
Definition: JMessage.hh:67
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.
then usage $script< input_file >< detector_file > fi set_variable OUTPUT_DIR set_variable SELECTOR JDAQTimesliceL1 set_variable DEBUG case set_variable DEBUG
Auxiliary class to handle multiple boolean-like I/O.
Definition: JParser.hh:221
bool isTObject(const TKey *key)
Check if given key corresponds to a TObject.
TDirectory * getDirectory(const JRootObjectID &id)
Get TDirectory pointer.
do set_variable MODULE getModule a $WORKDIR detector_a datx L $STRING JEditDetector a $WORKDIR detector_a datx M $MODULE setz o $WORKDIR detector_a datx JEditDetector a $WORKDIR detector_b datx M $MODULE setz o $WORKDIR detector_b datx done echo Output stored at $WORKDIR detector_a datx and $WORKDIR tripod_a txt JDrawDetector2D a $WORKDIR detector_a datx a $WORKDIR detector_b datx L BL o detector $FORMAT $BATCH JDrawDetector2D T $WORKDIR tripod_a txt T $WORKDIR tripod_b txt L BL o tripod $FORMAT $BATCH JCompareDetector a $WORKDIR detector_a datx b $WORKDIR detector_b datx o $WORKDIR abc root &dev null for KEY in X Y Z
Double_t g1(const Double_t x)
Function.
Definition: JQuantiles.cc:25