Jpp  18.6.0-rc.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/JProperties.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 104 of file JPlot2D.cc.

105 {
106  using namespace std;
107  using namespace JPP;
108 
109  typedef JRange<double> JRange_t;
110 
111  vector<JRootObjectID> inputFile;
112  string outputFile;
113  JCanvas canvas;
114  JStyle::JParameters parameters;
115  int stats;
116  JRange_t X;
117  JRange_t Y;
118  JRange_t Z;
119  JCounter logx;
120  JCounter logy;
121  bool logz;
122  string project;
123  string xLabel;
124  string yLabel;
125  string zLabel;
126  double markerSize;
127  string option;
128  set<char> grid;
129  bool batch;
130  string title;
131  map<string, int> Ndivisions;
132  int palette;
133  int debug;
134 
135  try {
136 
137  JProperties properties = parameters.getProperties();
138 
139  JParser<> zap("General purpose plot program for 2D ROOT objects.");
140 
141  zap['f'] = make_field(inputFile, "<input file>:<object name>");
142  zap['o'] = make_field(outputFile, "graphics output") = "";
143  zap['w'] = make_field(canvas, "size of canvas <nx>x<ny> [pixels]") = JCanvas(500, 500);
144  zap['@'] = make_field(properties) = JPARSER::initialised();
145  zap['s'] = make_field(stats) = -1;
146  zap['x'] = make_field(X, "x-abscissa range") = JRange_t::DEFAULT_RANGE();
147  zap['y'] = make_field(Y, "y-abscissa range") = JRange_t::DEFAULT_RANGE();
148  zap['z'] = make_field(Z, "ordinate range") = JRange_t::DEFAULT_RANGE();
149  zap['X'] = make_field(logx, "logarithmic x-axis (-XX log10 axis)");
150  zap['Y'] = make_field(logy, "logarithmic y-axis (-YY log10 axis)");
151  zap['Z'] = make_field(logz, "logarithmic z-axis");
152  zap['P'] = make_field(project, "projection") = "", "xy", "yx", "xz", "zx", "yz", "zy";
153  zap['>'] = make_field(xLabel, "x-axis label") = "";
154  zap['<'] = make_field(yLabel, "y-axis label") = "";
155  zap['^'] = make_field(zLabel, "z-axis label") = "";
156  zap['S'] = make_field(markerSize, "marker size") = 1.0;
157  zap['O'] = make_field(option, "plotting option") = "";
158  zap['G'] = make_field(grid, "grid lines [X][Y]") = JPARSER::initialised();
159  zap['B'] = make_field(batch, "batch processing");
160  zap['T'] = make_field(title, "graphics title ("
161  << "\"" << JName_t << "\" -> ROOT name; "
162  << "\"" << JTitle_t << "\" -> ROOT title)") = "KM3NeT preliminary";
163  zap['N'] = make_field(Ndivisions, "axis divisioning (e.g. \"X 505\")") = JPARSER::initialised();
164  zap['p'] = make_field(palette, "palette") = -1;
165  zap['d'] = make_field(debug) = 0;
166 
167  zap(argc, argv);
168  }
169  catch(const exception &error) {
170  FATAL(error.what() << endl);
171  }
172 
173 
174  gROOT->SetBatch(batch);
175 
176  TApplication* tp = new TApplication("user", NULL, NULL);
177  TCanvas* cv = new TCanvas("c1", "c1", canvas.x, canvas.y);
178 
179  unique_ptr<TStyle> gStyle(new JStyle("gplot", cv->GetWw(), cv->GetWh(), parameters));
180 
181  if (palette != -1) {
182  gStyle->SetPalette(palette);
183  }
184 
185  gROOT->SetStyle("gplot");
186  gROOT->ForceStyle();
187 
188 
189  cv->SetFillStyle(4000);
190  cv->SetFillColor(kWhite);
191  cv->Divide(1,1);
192  cv->cd(1);
193 
194  JMarkerAttributes::getInstance().setMarkerSize(markerSize);
195 
196  Double_t xmin = numeric_limits<double>::max();
197  Double_t xmax = numeric_limits<double>::lowest();
198  Double_t ymin = numeric_limits<double>::max();
199  Double_t ymax = numeric_limits<double>::lowest();
200  Double_t zmin = numeric_limits<double>::max();
201  Double_t zmax = numeric_limits<double>::lowest();
202 
203  vector<JRootObject> listOfObjects;
204 
205  TH2* master = NULL;
206 
207  for (vector<JRootObjectID>::const_iterator input = inputFile.begin(); input != inputFile.end(); ++input) {
208 
209  DEBUG("Input: " << *input << endl);
210 
211  TDirectory* dir = getDirectory(*input);
212 
213  if (dir == NULL) {
214  ERROR("File: " << input->getFullFilename() << " not opened." << endl);
215  continue;
216  }
217 
218  const TRegexp regexp(input->getObjectName());
219 
220  TIter iter(dir->GetListOfKeys());
221 
222  for (TKey* key; (key = (TKey*) iter.Next()) != NULL; ) {
223 
224  const TString tag(key->GetName());
225 
226  DEBUG("Key: " << tag << " match = " << tag.Contains(regexp) << endl);
227 
228  // option match
229 
230  if (tag.Contains(regexp) && isTObject(key)) {
231 
232  if (title == JName_t) {
233  title = key->GetName();
234  } else if (title == JTitle_t) {
235  title = key->GetTitle();
236  }
237 
238  JRootObject object(key->ReadObj());
239 
240  try {
241 
242  TH3& h3 = dynamic_cast<TH3&>(*object);
243 
244  object = h3.Project3D(project.c_str());
245  }
246  catch(exception&) {}
247 
248  try {
249 
250  TH2& h2 = dynamic_cast<TH2&>(*object);
251 
252  h2.SetStats(stats != -1);
253 
254  xmin = min(xmin, h2.GetXaxis()->GetXmin());
255  xmax = max(xmax, h2.GetXaxis()->GetXmax());
256  ymin = min(ymin, h2.GetYaxis()->GetXmin());
257  ymax = max(ymax, h2.GetYaxis()->GetXmax());
258  zmin = min(zmin, logz ? h2.GetMinimum(0.0) : h2.GetMinimum());
259  zmax = max(zmax, h2.GetMaximum());
260  }
261  catch(exception&) {}
262 
263  try {
264 
265  TGraph& g1 = dynamic_cast<TGraph&>(*object);
266 
267  for (Int_t i = 0; i != g1.GetN(); ++i) {
268  xmin = min(xmin, g1.GetX()[i] - numeric_limits<float>::epsilon());
269  xmax = max(xmax, g1.GetX()[i] + numeric_limits<float>::epsilon());
270  ymin = min(ymin, g1.GetY()[i] - numeric_limits<float>::epsilon());
271  ymax = max(ymax, g1.GetY()[i] + numeric_limits<float>::epsilon());
272  }
273 
274  int ng = 0;
275 
276  for (vector<JRootObject>::iterator i = listOfObjects.begin(); i != listOfObjects.end(); ++i) {
277  if (dynamic_cast<TGraph*>(i->get()) != NULL) {
278  ++ng;
279  }
280  }
281 
282  static_cast<TAttMarker&>(g1) = JMarkerAttributes::getInstance().get(ng);
283  }
284  catch(exception&) {}
285 
286  try {
287 
288  TGraph2D& g2 = dynamic_cast<TGraph2D&>(*object);
289 
290  for (Int_t i = 0; i != g2.GetN(); ++i) {
291  if (!logz || g2.GetZ()[i] > 0.0) {
292  xmin = min(xmin, g2.GetX()[i]);
293  xmax = max(xmax, g2.GetX()[i]);
294  ymin = min(ymin, g2.GetY()[i]);
295  ymax = max(ymax, g2.GetY()[i]);
296  zmin = min(zmin, g2.GetZ()[i]);
297  zmax = max(zmax, g2.GetZ()[i]);
298  }
299  }
300 
301  if (Z.is_valid()) {
302  g2.SetMinimum(Z.getLowerLimit());
303  g2.SetMaximum(Z.getUpperLimit());
304  }
305  }
306  catch(exception&) {}
307 
308  try {
309 
310  TF2& f2 = dynamic_cast<TF2&>(*object);
311 
312  f2.SetLineColor(kRed);
313  f2.SetTitle((title + ";" + xLabel + ";" + yLabel + ";" + zLabel).c_str());
314 
315  double __xmin;
316  double __xmax;
317  double __ymin;
318  double __ymax;
319 
320  f2.GetRange(__xmin, __ymin, __xmax, __ymax);
321 
322  xmin = min(xmin, __xmin);
323  xmax = max(xmax, __xmax);
324  ymin = min(ymin, __ymin);
325  ymax = max(ymax, __ymax);
326  zmin = min(zmin, f2.GetMinimum());
327  zmax = max(zmax, f2.GetMaximum());
328  }
329  catch(exception&) {}
330 
331  // label
332 
333  for (TString buffer[] = { object.getLabel(), input->getFilename().c_str(), "" }, *i = buffer; *i != ""; ++i) {
334 
335  *i = (*i)(TRegexp("\\[.*\\]"));
336 
337  if ((*i).Length() > 2) {
338  object.setLabel((*i)(1, (*i).Length() - 2));
339  }
340  }
341 
342  if (dynamic_cast<TH2*> (object.get()) != NULL ||
343  dynamic_cast<TGraph*> (object.get()) != NULL ||
344  dynamic_cast<TGraph2D*>(object.get()) != NULL ||
345  dynamic_cast<TEllipse*>(object.get()) != NULL ||
346  dynamic_cast<TLine*> (object.get()) != NULL ||
347  dynamic_cast<TText*> (object.get()) != NULL ||
348  dynamic_cast<TF2*> (object.get()) != NULL) {
349 
350  DEBUG("Add object: " << tag << " with label <" << object.getLabel() << ">" << endl);
351 
352  if (master == NULL) {
353  master = dynamic_cast<TH2*>(object.get());
354  }
355 
356  listOfObjects.push_back(object);
357 
358  } else {
359  ERROR("For other objects than 2D histograms, use JPlot1D" << endl);
360  }
361  }
362  }
363  }
364 
365  if (listOfObjects.empty()) {
366  ERROR("Nothing to draw." << endl);
367  }
368 
369  // plot frame
370 
371  cv->cd(1);
372 
373  if (option.find("COLZ") != string::npos ||
374  option.find("colz") != string::npos) {
375  gPad->SetRightMargin(0.20);
376  }
377 
378  if (X.is_valid()) {
379  xmin = X.getLowerLimit();
380  xmax = X.getUpperLimit();
381  }
382 
383  if (Y.is_valid()) {
384  ymin = Y.getLowerLimit();
385  ymax = Y.getUpperLimit();
386  }
387 
388  if (Z.is_valid()) {
389  zmin = Z.getLowerLimit();
390  zmax = Z.getUpperLimit();
391  } else if (zmax > zmin) {
392  setRange(zmin, zmax, logz);
393  }
394 
395  if (!listOfObjects.empty()) {
396 
397  if (master == NULL) {
398 
399  master = new TH2D(MASTER.c_str(), NULL,
400  100, xmin, xmax,
401  100, ymin, ymax);
402 
403  master->SetStats(kFALSE);
404  }
405  }
406 
407  if (master == NULL) {
408 
409  TText* p = new TText(0.5, 0.5, "No data");
410 
411  p->SetTextAlign(21);
412  p->SetTextAngle(45);
413  p->Draw();
414 
415  } else {
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 (map<string, int>::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(option.c_str());
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 }
const double xmax
Definition: JQuadrature.cc:24
Utility class to parse command line options.
Definition: JParser.hh:1711
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
Utility class to parse parameter values.
Definition: JProperties.hh:497
*fatal Wrong number of arguments esac JCookie sh typeset Z DETECTOR typeset Z SOURCE_RUN typeset Z TARGET_RUN set_variable PARAMETERS_FILE $WORKDIR parameters
Definition: diff-Tuna.sh:38
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:84
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:2158
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:435
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