Jpp  18.2.1
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 <set>
#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 "TGraphErrors.h"
#include "TGraph2D.h"
#include "TGraph2DErrors.h"
#include "TEllipse.h"
#include "TLine.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 103 of file JPlot2D.cc.

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