Jpp test-rotations-old
the software that should make you happy
Loading...
Searching...
No Matches
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 "TRootCanvas.h"
13#include "TKey.h"
14#include "TStyle.h"
15#include "TAttMarker.h"
16#include "TAttLine.h"
17#include "TH2.h"
18#include "TH3.h"
19#include "TH2D.h"
20#include "TGraph.h"
21#include "TGraphErrors.h"
22#include "TGraph2D.h"
23#include "TGraph2DErrors.h"
24#include "TEllipse.h"
25#include "TLine.h"
26#include "TF2.h"
27#include "TString.h"
28#include "TRegexp.h"
29#include "TText.h"
30
31#include "JTools/JRange.hh"
32#include "JLang/JEquals.hh"
33#include "JROOT/JStyle.hh"
34#include "JROOT/JCanvas.hh"
37#include "JGizmo/JRootObject.hh"
39
40#include "Jeep/JProperties.hh"
41#include "Jeep/JParser.hh"
42#include "Jeep/JMessage.hh"
43
44namespace {
45
46 /**
47 * Set x of object to logarithmic.
48 *
49 * \param object pointer to object
50 * \return true if set; else false
51 */
52 template<class T>
53 inline bool setLogX(TObject* object)
54 {
55 using namespace JPP;
56
57 T* p = dynamic_cast<T*>(object);
58
59 if (p != NULL) {
60
62
63 return true;
64 }
65
66 return false;
67 }
68
69 /**
70 * Set y of object to logarithmic.
71 *
72 * \param object pointer to object
73 * \return true if set; else false
74 */
75 template<class T>
76 inline bool setLogY(TObject* object)
77 {
78 using namespace JPP;
79
80 T* p = dynamic_cast<T*>(object);
81
82 if (p != NULL) {
83
85
86 return true;
87 }
88
89 return false;
90 }
91
92 const std::string MASTER = "__H__"; //!< Name of prototype
93
94 const char* const JName_t = "?"; //!< Draw histogram name as title of plot
95 const char* const JTitle_t = "%"; //!< Draw histogram title as title of plot
96
97
98 /**
99 * Auxiliary data structure for master from TH2 or TGraph.
100 */
101 struct JMaster :
102 public JLANG::JEquals<JMaster, TObject*>
103 {
104 JMaster() :
105 h2(NULL),
106 g2(NULL)
107 {}
108
109 bool equals(const JMaster& master) const
110 {
111 return (this->h2 == master.h2 &&
112 this->g2 == master.g2);
113 }
114
115 bool equals(const TObject* p) const
116 {
117 return (this->h2 == dynamic_cast<const TH2*>(p) &&
118 this->g2 == dynamic_cast<const TGraph2D*>(p));
119 }
120
121 JMaster* operator->()
122 {
123 return this;
124 }
125
126 void operator=(TObject* p)
127 {
128 h2 = dynamic_cast<TH2*> (p);
129 g2 = dynamic_cast<TGraph2D*>(p);
130 }
131
132 const char* GetName() const
133 {
134 return (h2 != NULL ? h2->GetName() :
135 g2 != NULL ? g2->GetName() :
136 NULL);
137 }
138
139 void SetTitle(const char* title)
140 {
141 if (h2 != NULL) { h2->SetTitle(title); }
142 if (g2 != NULL) { g2->SetTitle(title); }
143 }
144
145 void SetStats(const Bool_t stats)
146 {
147 if (h2 != NULL) { h2->SetStats(stats); }
148 }
149
150 void SetMaximum(Double_t maximum)
151 {
152 if (h2 != NULL) { h2->SetMaximum(maximum); }
153 if (g2 != NULL) { g2->SetMaximum(maximum); }
154 }
155
156 void SetMinimum(Double_t minimum)
157 {
158 if (h2 != NULL) { h2->SetMinimum(minimum); }
159 if (g2 != NULL) { g2->SetMinimum(minimum); }
160 }
161
162 void SetNdivisions(Int_t n, Option_t *axis)
163 {
164 if (h2 != NULL) { h2->SetNdivisions(n, axis); }
165 }
166
167 TAxis* GetXaxis()
168 {
169 return (h2 != NULL ? h2->GetXaxis() :
170 g2 != NULL ? g2->GetXaxis() :
171 NULL);
172 }
173
174 TAxis* GetYaxis()
175 {
176 return (h2 != NULL ? h2->GetYaxis() :
177 g2 != NULL ? g2->GetYaxis() :
178 NULL);
179 }
180
181 TAxis* GetZaxis()
182 {
183 return (h2 != NULL ? h2->GetZaxis() :
184 g2 != NULL ? g2->GetZaxis() :
185 NULL);
186 }
187
188 void Draw(Option_t *option="")
189 {
190 if (h2 != NULL) { h2->Draw(option); }
191 if (g2 != NULL) { g2->Draw(option); }
192 }
193
194 void setLogarithmicX()
195 {
196 if (h2 != NULL) { JGIZMO::setLogarithmicX(h2); }
197 if (g2 != NULL) { JGIZMO::setLogarithmicX(g2); }
198 }
199
200 void setLogarithmicY()
201 {
202 if (h2 != NULL) { JGIZMO::setLogarithmicY(h2); }
203 if (g2 != NULL) { JGIZMO::setLogarithmicY(g2); }
204 }
205
206 private:
207 TH2* h2;
208 TGraph2D* g2;
209 };
210
211 inline void setLogarithmicX(JMaster& master) { master.setLogarithmicX(); }
212 inline void setLogarithmicY(JMaster& master) { master.setLogarithmicY(); }
213}
214
215
216/**
217 * \file
218 * General purpose plot program for 2D ROOT objects.
219 * The option <tt>-f</tt> corresponds to <tt><file name>:<object name></tt>.
220 * \author mdejong
221 */
222int main(int argc, char **argv)
223{
224 using namespace std;
225 using namespace JPP;
226
227 typedef JRange<double> JRange_t;
228
229 vector<JRootObjectID> inputFile;
230 string outputFile;
231 JCanvas canvas;
232 JStyle::JParameters parameters;
233 int stats;
234 JRange_t X;
235 JRange_t Y;
236 JRange_t Z;
237 JCounter logx;
238 JCounter logy;
239 bool logz;
240 string project;
241 string xLabel;
242 string yLabel;
243 string zLabel;
244 double markerSize;
245 string option;
246 set<char> grid;
247 bool batch;
248 string title;
249 map<string, int> Ndivisions;
250 int palette;
251 int debug;
252
253 try {
254
255 JProperties properties = parameters.getProperties();
256
257 JParser<> zap("General purpose plot program for 2D ROOT objects.");
258
259 zap['f'] = make_field(inputFile, "<input file>:<object name>");
260 zap['o'] = make_field(outputFile, "graphics output") = "";
261 zap['w'] = make_field(canvas, "size of canvas <nx>x<ny> [pixels]") = JCanvas(500, 500);
262 zap['@'] = make_field(properties) = JPARSER::initialised();
263 zap['s'] = make_field(stats) = -1;
264 zap['x'] = make_field(X, "x-abscissa range") = JRange_t::DEFAULT_RANGE();
265 zap['y'] = make_field(Y, "y-abscissa range") = JRange_t::DEFAULT_RANGE();
266 zap['z'] = make_field(Z, "ordinate range") = JRange_t::DEFAULT_RANGE();
267 zap['X'] = make_field(logx, "logarithmic x-axis (-XX log10 axis)");
268 zap['Y'] = make_field(logy, "logarithmic y-axis (-YY log10 axis)");
269 zap['Z'] = make_field(logz, "logarithmic z-axis");
270 zap['P'] = make_field(project, "projection") = "", "xy", "yx", "xz", "zx", "yz", "zy";
271 zap['>'] = make_field(xLabel, "x-axis label") = "";
272 zap['<'] = make_field(yLabel, "y-axis label") = "";
273 zap['^'] = make_field(zLabel, "z-axis label") = "";
274 zap['S'] = make_field(markerSize, "marker size") = 1.0;
275 zap['O'] = make_field(option, "plotting option") = "";
276 zap['G'] = make_field(grid, "grid lines [X][Y]") = JPARSER::initialised();
277 zap['B'] = make_field(batch, "batch processing");
278 zap['T'] = make_field(title, "graphics title ("
279 << "\"" << JName_t << "\" -> ROOT name; "
280 << "\"" << JTitle_t << "\" -> ROOT title)") = "KM3NeT preliminary";
281 zap['N'] = make_field(Ndivisions, "axis divisioning (e.g. \"X 505\")") = JPARSER::initialised();
282 zap['p'] = make_field(palette, "palette") = -1;
283 zap['d'] = make_field(debug) = 0;
284
285 zap(argc, argv);
286 }
287 catch(const exception &error) {
288 FATAL(error.what() << endl);
289 }
290
291
292 gROOT->SetBatch(batch);
293
294 TApplication* tp = new TApplication("user", NULL, NULL);
295 TCanvas* cv = new TCanvas("c1", "c1", canvas.x, canvas.y);
296
297 if (!batch) {
298 ((TRootCanvas *) cv->GetCanvasImp())->Connect("CloseWindow()", "TApplication", tp, "Terminate()");
299 }
300
301 unique_ptr<TStyle> gStyle(new JStyle("gplot", cv->GetWw(), cv->GetWh(), parameters));
302
303 if (palette != -1) {
304 gStyle->SetPalette(palette);
305 }
306
307 gROOT->SetStyle("gplot");
308 gROOT->ForceStyle();
309
310
311 cv->SetFillStyle(4000);
312 cv->SetFillColor(kWhite);
313 cv->Divide(1,1);
314 cv->cd(1);
315
316 JMarkerAttributes::getInstance().setMarkerSize(markerSize);
317
318 Double_t xmin = numeric_limits<double>::max();
319 Double_t xmax = numeric_limits<double>::lowest();
320 Double_t ymin = numeric_limits<double>::max();
321 Double_t ymax = numeric_limits<double>::lowest();
322 Double_t zmin = numeric_limits<double>::max();
323 Double_t zmax = numeric_limits<double>::lowest();
324
325 vector<JRootObject> listOfObjects;
326
327 //TH2* master = NULL;
328 JMaster master;
329
330 for (vector<JRootObjectID>::const_iterator input = inputFile.begin(); input != inputFile.end(); ++input) {
331
332 DEBUG("Input: " << *input << endl);
333
334 TDirectory* dir = getDirectory(*input);
335
336 if (dir == NULL) {
337 ERROR("File: " << input->getFullFilename() << " not opened." << endl);
338 continue;
339 }
340
341 const TRegexp regexp(input->getObjectName());
342
343 TIter iter(dir->GetListOfKeys());
344
345 for (TKey* key; (key = (TKey*) iter.Next()) != NULL; ) {
346
347 const TString tag(key->GetName());
348
349 DEBUG("Key: " << tag << " match = " << tag.Contains(regexp) << endl);
350
351 // option match
352
353 if (tag.Contains(regexp) && isTObject(key)) {
354
355 if (title == JName_t) {
356 title = key->GetName();
357 } else if (title == JTitle_t) {
358 title = key->GetTitle();
359 }
360
361 JRootObject object(key->ReadObj());
362
363 try {
364
365 TH3& h3 = dynamic_cast<TH3&>(*object);
366
367 object = h3.Project3D(project.c_str());
368 }
369 catch(exception&) {}
370
371 try {
372
373 TH2& h2 = dynamic_cast<TH2&>(*object);
374
375 h2.SetStats(stats != -1);
376
377 xmin = min(xmin, h2.GetXaxis()->GetXmin());
378 xmax = max(xmax, h2.GetXaxis()->GetXmax());
379 ymin = min(ymin, h2.GetYaxis()->GetXmin());
380 ymax = max(ymax, h2.GetYaxis()->GetXmax());
381 zmin = min(zmin, logz ? h2.GetMinimum(0.0) : h2.GetMinimum());
382 zmax = max(zmax, h2.GetMaximum());
383 }
384 catch(exception&) {}
385
386 try {
387
388 TGraph& g1 = dynamic_cast<TGraph&>(*object);
389
390 for (Int_t i = 0; i != g1.GetN(); ++i) {
391 xmin = min(xmin, g1.GetX()[i] - numeric_limits<float>::epsilon());
392 xmax = max(xmax, g1.GetX()[i] + numeric_limits<float>::epsilon());
393 ymin = min(ymin, g1.GetY()[i] - numeric_limits<float>::epsilon());
394 ymax = max(ymax, g1.GetY()[i] + numeric_limits<float>::epsilon());
395 }
396
397 int ng = 0;
398
399 for (vector<JRootObject>::iterator i = listOfObjects.begin(); i != listOfObjects.end(); ++i) {
400 if (dynamic_cast<TGraph*>(i->get()) != NULL) {
401 ++ng;
402 }
403 }
404
405 static_cast<TAttMarker&>(g1) = JMarkerAttributes::getInstance().get(ng);
406 }
407 catch(exception&) {}
408
409 try {
410
411 TGraph2D& g2 = dynamic_cast<TGraph2D&>(*object);
412
413 for (Int_t i = 0; i != g2.GetN(); ++i) {
414 if (!logz || g2.GetZ()[i] > 0.0) {
415 xmin = min(xmin, g2.GetX()[i]);
416 xmax = max(xmax, g2.GetX()[i]);
417 ymin = min(ymin, g2.GetY()[i]);
418 ymax = max(ymax, g2.GetY()[i]);
419 zmin = min(zmin, g2.GetZ()[i]);
420 zmax = max(zmax, g2.GetZ()[i]);
421 }
422 }
423
424 if (Z.is_valid()) {
425 g2.SetMinimum(Z.getLowerLimit());
426 g2.SetMaximum(Z.getUpperLimit());
427 }
428 }
429 catch(exception&) {}
430
431 try {
432
433 TF2& f2 = dynamic_cast<TF2&>(*object);
434
435 f2.SetLineColor(kRed);
436 f2.SetTitle((title + ";" + xLabel + ";" + yLabel + ";" + zLabel).c_str());
437
438 double __xmin;
439 double __xmax;
440 double __ymin;
441 double __ymax;
442
443 f2.GetRange(__xmin, __ymin, __xmax, __ymax);
444
445 xmin = min(xmin, __xmin);
446 xmax = max(xmax, __xmax);
447 ymin = min(ymin, __ymin);
448 ymax = max(ymax, __ymax);
449 zmin = min(zmin, f2.GetMinimum());
450 zmax = max(zmax, f2.GetMaximum());
451 }
452 catch(exception&) {}
453
454 // label
455
456 for (TString buffer[] = { object.getLabel(), input->getFilename().c_str(), "" }, *i = buffer; *i != ""; ++i) {
457
458 *i = (*i)(TRegexp("\\[.*\\]"));
459
460 if ((*i).Length() > 2) {
461 object.setLabel((*i)(1, (*i).Length() - 2));
462 }
463 }
464
465 if (dynamic_cast<TH2*> (object.get()) != NULL ||
466 dynamic_cast<TGraph*> (object.get()) != NULL ||
467 dynamic_cast<TGraph2D*>(object.get()) != NULL ||
468 dynamic_cast<TEllipse*>(object.get()) != NULL ||
469 dynamic_cast<TLine*> (object.get()) != NULL ||
470 dynamic_cast<TText*> (object.get()) != NULL ||
471 dynamic_cast<TF2*> (object.get()) != NULL) {
472
473 DEBUG("Add object: " << tag << " with label <" << object.getLabel() << ">" << endl);
474
475 if (master == NULL) {
476 //master = dynamic_cast<TH2*>(object.get());
477 master = object.get();
478 }
479
480 listOfObjects.push_back(object);
481
482 } else {
483 ERROR("For other objects than 2D histograms, use JPlot1D" << endl);
484 }
485 }
486 }
487 }
488
489 if (listOfObjects.empty()) {
490 ERROR("Nothing to draw." << endl);
491 }
492
493 // plot frame
494
495 cv->cd(1);
496
497 if (option.find("COLZ") != string::npos ||
498 option.find("colz") != string::npos) {
499 gPad->SetRightMargin(0.20);
500 }
501
502 if (X.is_valid()) {
503 xmin = X.getLowerLimit();
504 xmax = X.getUpperLimit();
505 }
506
507 if (Y.is_valid()) {
508 ymin = Y.getLowerLimit();
509 ymax = Y.getUpperLimit();
510 }
511
512 if (Z.is_valid()) {
513 zmin = Z.getLowerLimit();
514 zmax = Z.getUpperLimit();
515 } else if (zmax > zmin) {
516 setRange(zmin, zmax, logz);
517 }
518
519 if (!listOfObjects.empty()) {
520
521 if (master == NULL) {
522
523 master = new TH2D(MASTER.c_str(), NULL,
524 100, xmin, xmax,
525 100, ymin, ymax);
526
527 master->SetStats(kFALSE);
528 }
529 }
530
531 if (master == NULL) {
532
533 TText* p = new TText(0.5, 0.5, "No data");
534
535 p->SetTextAlign(21);
536 p->SetTextAngle(45);
537 p->Draw();
538
539 } else {
540
541 if (logx) { gPad->SetLogx(); }
542 if (logy) { gPad->SetLogy(); }
543 if (logz) { gPad->SetLogz(); }
544
545 master->SetTitle(title.c_str());
546
547 master->GetXaxis()->SetRangeUser(xmin, xmax);
548 master->GetYaxis()->SetRangeUser(ymin, ymax);
549
550 if (logx > 1) { setLogarithmicX(master); }
551 if (logy > 1) { setLogarithmicY(master); }
552
553 if (logx > 2) { master->GetXaxis()->SetNoExponent(); }
554 if (logy > 2) { master->GetYaxis()->SetNoExponent(); }
555
556 master->SetMinimum(zmin);
557 master->SetMaximum(zmax);
558
559 if (xLabel != "") { master->GetXaxis()->SetTitle(xLabel.c_str()); master->GetXaxis()->CenterTitle(true); }
560 if (yLabel != "") { master->GetYaxis()->SetTitle(yLabel.c_str()); master->GetYaxis()->CenterTitle(true); }
561 if (zLabel != "") { master->GetZaxis()->SetTitle(zLabel.c_str()); master->GetZaxis()->CenterTitle(true); }
562
563 master->GetXaxis()->SetMoreLogLabels((logx == 1 && log10(xmax/xmin) < 2) ||
564 (logx > 1 && xmax-xmin < 2));
565 master->GetYaxis()->SetMoreLogLabels((logy == 1 && log10(ymax/ymin) < 2) ||
566 (logy > 1 && ymax-ymin < 2));
567
568 for (map<string, int>::const_iterator i = Ndivisions.begin(); i != Ndivisions.end(); ++i) {
569 master->SetNdivisions(i->second, i->first.c_str());
570 }
571
572 DEBUG("Draw " << master->GetName() << ' ' << option << endl);
573
574 master->Draw(option.c_str());
575 }
576
577 if (logx > 1 || logy > 1) {
578 for (vector<JRootObject>::iterator i = listOfObjects.begin(); i != listOfObjects.end(); ++i) {
579 if (dynamic_cast<TH2*>(i->get()) != master) {
580 if (logx > 1) {
581 if (setLogX<TH2> (i->get())) {}
582 else if (setLogX<TF2> (i->get())) {}
583 else if (setLogX<TGraph2DErrors>(i->get())) {}
584 else if (setLogX<TGraph2D> (i->get())) {}
585 else if (setLogX<TGraphErrors> (i->get())) {}
586 else if (setLogX<TGraph> (i->get())) {}
587 else if (setLogX<TLine> (i->get())) {}
588 else if (setLogX<TEllipse> (i->get())) {}
589 }
590 if (logy > 1) {
591 if (setLogY<TH2> (i->get())) {}
592 else if (setLogY<TF2> (i->get())) {}
593 else if (setLogY<TGraph2DErrors>(i->get())) {}
594 else if (setLogY<TGraph2D> (i->get())) {}
595 else if (setLogY<TGraphErrors> (i->get())) {}
596 else if (setLogY<TGraph> (i->get())) {}
597 else if (setLogY<TLine> (i->get())) {}
598 else if (setLogY<TEllipse> (i->get())) {}
599 }
600 }
601 }
602 }
603
604 if (grid.count('x') || grid.count('X')) { gPad->SetGridx(); }
605 if (grid.count('y') || grid.count('Y')) { gPad->SetGridy(); }
606
607 if (stats != -1)
608 gStyle->SetOptStat(stats);
609 else
610 gStyle->SetOptFit(kFALSE);
611
612
613 for (vector<JRootObject>::iterator i = listOfObjects.begin(); i != listOfObjects.end(); ++i) {
614
615 if (i->get() != master) {
616
617 DEBUG("Draw " << (*i)->GetName() << ' ' << (*i)->GetTitle() << endl);
618
619 string buffer(option);
620
621 buffer += "SAME";
622
623 try {
624
625 TH2& h2 = dynamic_cast<TH2&>(*(*i));
626
627 h2.SetMinimum(zmin);
628 h2.SetMaximum(zmax);
629 }
630 catch(exception&) {}
631
632 try {
633
634 TGraph& g1 = dynamic_cast<TGraph&>(*(*i));
635
636 buffer = "P";
637 }
638 catch(exception&) {}
639
640 try {
641
642 TGraph2D& g2 = dynamic_cast<TGraph2D&>(*(*i));
643
644 g2.SetMinimum(zmin);
645 g2.SetMaximum(zmax);
646 }
647 catch(exception&) {}
648
649 try {
650
651 TF2& f2 = dynamic_cast<TF2&>(*(*i));
652
653 f2.SetNpx(1000);
654 f2.SetNpy(1000);
655 /*
656 f2.SetRange(xmin, ymin, zmin,
657 xmax, ymax, zmax);
658 */
659 }
660 catch(exception&) {}
661
662 (*i)->Draw(buffer.c_str());
663 }
664 }
665
666 //gPad->RedrawAxis();
667
668 cv->Update();
669
670 if (outputFile != "") {
671 cv->SaveAs(outputFile.c_str());
672 }
673
674 if (!batch) {
675 tp->Run();
676 }
677
678 return (master != NULL ? 0 : 1);
679}
string outputFile
General purpose messaging.
#define DEBUG(A)
Message macros.
Definition JMessage.hh:62
#define ERROR(A)
Definition JMessage.hh:66
#define FATAL(A)
Definition JMessage.hh:67
int debug
debug level
Definition JSirene.cc:72
Utility class to parse command line options.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition JParser.hh:2142
int main(int argc, char **argv)
Definition JPlot2D.cc:222
Utility class to parse parameter values.
Double_t g1(const Double_t x)
Function.
Definition JQuantiles.cc:25
Auxiliary class to define a range between two values.
Utility class to parse parameter values.
Auxiliary data structure for TObject with a user defined label.
Auxiliary class to handle multiple boolean-like I/O.
Definition JParser.hh:423
Utility class to parse command line options.
Definition JParser.hh:1698
Data structure for size of TCanvas.
Definition JCanvas.hh:26
int y
number of pixels in Y
Definition JCanvas.hh:99
int x
number of pixels in X
Definition JCanvas.hh:98
Wrapper class around ROOT TStyle.
Definition JStyle.hh:24
Range of values.
Definition JRange.hh:42
void setLogarithmicX(TList *list)
Make x-axis of objects in list logarithmic (e.g. after using log10()).
void setLogarithmicY(TList *list)
Make y-axis of objects in list logarithmic (e.g. after using log10()).
bool equals(const JFirst_t &first, const JSecond_t &second, const double precision=std::numeric_limits< double >::min())
Check equality.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Template definition of auxiliary base class for comparison of data structures.
Definition JEquals.hh:84
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition JParser.hh:68
JProperties getProperties()
Get properties of this class.
Definition JStyle.hh:44