Jpp  16.0.0-rc.2
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  for (TString buffer[] = { object.getLabel(), input->getFilename().c_str(), "" }, *i = buffer; *i != ""; ++i) {
326 
327  *i = (*i)(TRegexp("\\[.*\\]"));
328 
329  if ((*i).Length() > 2) {
330  object.setLabel((*i)(1, (*i).Length() - 2));
331  }
332  }
333 
334  if (dynamic_cast<TH2*> (object.get()) != NULL ||
335  dynamic_cast<TGraph*> (object.get()) != NULL ||
336  dynamic_cast<TGraph2D*>(object.get()) != NULL ||
337  dynamic_cast<TEllipse*>(object.get()) != NULL ||
338  dynamic_cast<TLine*> (object.get()) != NULL ||
339  dynamic_cast<TF2*> (object.get()) != NULL) {
340 
341  DEBUG("Add object: " << tag << " with label " << object.getLabel() << endl);
342 
343  if (master == NULL) {
344  master = dynamic_cast<TH2*>(object.get());
345  }
346 
347  listOfObjects.push_back(object);
348 
349  } else {
350  ERROR("For other objects than 2D histograms, use JPlot1D" << endl);
351  }
352  }
353  }
354  }
355 
356  if (listOfObjects.empty()) {
357  ERROR("Nothing to draw." << endl);
358  }
359 
360  // plot frame
361 
362  cv->cd(1);
363 
364  if (option.find("COLZ") != string::npos ||
365  option.find("colz") != string::npos) {
366  gPad->SetRightMargin(0.20);
367  }
368 
369  if (X != JRange_t()) {
370  xmin = X.getLowerLimit();
371  xmax = X.getUpperLimit();
372  }
373 
374  if (Y != JRange_t()) {
375  ymin = Y.getLowerLimit();
376  ymax = Y.getUpperLimit();
377  }
378 
379  if (Z != JRange_t()) {
380  zmin = Z.getLowerLimit();
381  zmax = Z.getUpperLimit();
382  } else {
383  setRange(zmin, zmax, logz);
384  }
385 
386  if (master == NULL) {
387 
388  if (X != JRange_t() &&
389  Y != JRange_t()) {
390 
391  master = new TH2D(MASTER.c_str(), NULL,
392  100, X.getLowerLimit(), X.getUpperLimit(),
393  100, Y.getLowerLimit(), Y.getUpperLimit());
394 
395  master->SetStats(kFALSE);
396 
397  } else if (xmin < xmax && ymin < ymax) {
398 
399  master = new TH2D(MASTER.c_str(), NULL,
400  100, xmin, xmax,
401  100, ymin, ymax);
402 
403  master->SetStats(kFALSE);
404 
405  } else {
406 
407  TText* p = new TText(0.5, 0.5, MAKE_CSTRING("No data"));
408 
409  p->SetTextAlign(21);
410  p->SetTextAngle(45);
411  p->Draw();
412  }
413  }
414 
415  if (master != NULL) {
416 
417  if (logx) { gPad->SetLogx(); }
418  if (logy) { gPad->SetLogy(); }
419  if (logz) { gPad->SetLogz(); }
420 
421  master->GetXaxis()->SetRangeUser(xmin, xmax);
422  master->GetYaxis()->SetRangeUser(ymin, ymax);
423 
424  if (logx > 1) { setLogarithmicX(master); }
425  if (logy > 1) { setLogarithmicY(master); }
426 
427  master->SetTitle(title.c_str());
428 
429  master->SetMinimum(zmin);
430  master->SetMaximum(zmax);
431 
432  if (xLabel != "") { master->GetXaxis()->SetTitle(xLabel.c_str()); master->GetXaxis()->CenterTitle(true); }
433  if (yLabel != "") { master->GetYaxis()->SetTitle(yLabel.c_str()); master->GetYaxis()->CenterTitle(true); }
434  if (zLabel != "") { master->GetZaxis()->SetTitle(zLabel.c_str()); master->GetZaxis()->CenterTitle(true); }
435 
436  master->GetXaxis()->SetMoreLogLabels((logx == 1 && log10(xmax/xmin) < 2) ||
437  (logx > 1 && xmax-xmin < 2));
438  master->GetYaxis()->SetMoreLogLabels((logy == 1 && log10(ymax/ymin) < 2) ||
439  (logy > 1 && ymax-ymin < 2));
440 
441  for (vector<division_type>::const_iterator i = Ndivisions.begin(); i != Ndivisions.end(); ++i) {
442  master->SetNdivisions(i->second, i->first.c_str());
443  }
444 
445  DEBUG("Draw " << master->GetName() << ' ' << option << endl);
446 
447  master->Draw(MAKE_CSTRING(option << (MASTER == master->GetName() ? "AXIS" : "")));
448  }
449 
450  if (logx > 1 || logy > 1) {
451  for (vector<JRootObject>::iterator i = listOfObjects.begin(); i != listOfObjects.end(); ++i) {
452  if (dynamic_cast<TH2*>(i->get()) != master) {
453  if (logx > 1) {
454  if (setLogX<TH2> (i->get())) {}
455  else if (setLogX<TF2> (i->get())) {}
456  else if (setLogX<TGraph2DErrors>(i->get())) {}
457  else if (setLogX<TGraph2D> (i->get())) {}
458  else if (setLogX<TGraphErrors> (i->get())) {}
459  else if (setLogX<TGraph> (i->get())) {}
460  else if (setLogX<TLine> (i->get())) {}
461  else if (setLogX<TEllipse> (i->get())) {}
462  }
463  if (logy > 1) {
464  if (setLogY<TH2> (i->get())) {}
465  else if (setLogY<TF2> (i->get())) {}
466  else if (setLogY<TGraph2DErrors>(i->get())) {}
467  else if (setLogY<TGraph2D> (i->get())) {}
468  else if (setLogY<TGraphErrors> (i->get())) {}
469  else if (setLogY<TGraph> (i->get())) {}
470  else if (setLogY<TLine> (i->get())) {}
471  else if (setLogY<TEllipse> (i->get())) {}
472  }
473  }
474  }
475  }
476 
477  if (grid.count('x') || grid.count('X')) { gPad->SetGridx(); }
478  if (grid.count('y') || grid.count('Y')) { gPad->SetGridy(); }
479 
480  if (stats != -1)
481  gStyle->SetOptStat(stats);
482  else
483  gStyle->SetOptFit(kFALSE);
484 
485  for (vector<JRootObject>::iterator i = listOfObjects.begin(); i != listOfObjects.end(); ++i) {
486 
487  DEBUG("Draw " << (*i)->GetName() << ' ' << (*i)->GetTitle() << endl);
488 
489  string buffer(option);
490 
491  buffer += "SAME";
492 
493  try {
494 
495  TH2& h2 = dynamic_cast<TH2&>(*(*i));
496 
497  h2.SetMinimum(zmin);
498  h2.SetMaximum(zmax);
499  }
500  catch(exception&) {}
501 
502  try {
503 
504  TGraph& g1 = dynamic_cast<TGraph&>(*(*i));
505 
506  buffer = "P";
507  }
508  catch(exception&) {}
509 
510  try {
511 
512  TGraph2D& g2 = dynamic_cast<TGraph2D&>(*(*i));
513 
514  g2.SetMinimum(zmin);
515  g2.SetMaximum(zmax);
516  }
517  catch(exception&) {}
518 
519  try {
520 
521  TF2& f2 = dynamic_cast<TF2&>(*(*i));
522 
523  f2.SetNpx(1000);
524  f2.SetNpy(1000);
525  /*
526  f2.SetRange(xmin, ymin, zmin,
527  xmax, ymax, zmax);
528  */
529  }
530  catch(exception&) {}
531 
532  (*i)->Draw(buffer.c_str());
533  }
534 
535  gPad->RedrawAxis();
536 
537  cv->Update();
538 
539  if (outputFile != "") {
540  cv->SaveAs(outputFile.c_str());
541  }
542 
543  if (!batch) {
544  tp->Run();
545  }
546 
547  return (master != NULL ? 0 : 1);
548 }
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