MINT2
RootPlotter1D.cpp
Go to the documentation of this file.
1 #include "Mint/RootPlotter1D.h"
2 #include "Mint/GlobalFunctions.h"
3 
4 double RootPlotter1D::s_ratioMax = -999.999;
5 double RootPlotter1D::s_ratioMin = -999.999;
6 
8 RootPlotter1D::RootPlotter1D(TH1* histogram, double width, double height) :
9  RootPlotter(histogram, width, height),
10  _ratioMax(s_ratioMax),
11  _ratioMin(s_ratioMin)
12 {
13 
14  _lMargin = 0.14;
15  _rMargin = 0.10;
16  _tMargin = 0.10;
17  _bMargin = 0.14;
18  WELCOME_LOG << "Hello from the Plotter() constructor!";
19 }
20 
21 
23 void RootPlotter1D::setHistogramStyle(TH1* histogram, bool setMinMax){
24 
25  VERBOSE_LOG << histogram << " " << histogram->GetName();
26 
27  histogram->SetTitle("");
28 
29  histogram->GetXaxis()->SetTitleSize(_xAxisTitleSize);
30  histogram->GetYaxis()->SetTitleSize(_yAxisTitleSize);
31 
32  if ( histogram->GetXaxis()->GetLabelSize() != 0.0 )
33  histogram->GetXaxis()->SetLabelSize(_xAxisLabelSize);
34  if ( histogram->GetYaxis()->GetLabelSize() != 0.0 )
35  histogram->GetYaxis()->SetLabelSize(_yAxisLabelSize);
36 
37 
38  histogram->GetXaxis()->SetTickLength(_xAxisTickLength);
39  histogram->GetXaxis()->SetTickLength(_yAxisTickLength);
40 
41  if ( histogram->GetXaxis()->GetLabelOffset() != 999 )
42  histogram->GetXaxis()->SetLabelOffset(_xAxisLabelOffset);
43  if ( histogram->GetYaxis()->GetLabelOffset() != 999 )
44  histogram->GetYaxis()->SetLabelOffset(_yAxisLabelOffset);
45 
46 
47  histogram->GetXaxis()->SetTitleOffset(_xAxisTitleOffset);
48  histogram->GetYaxis()->SetTitleOffset(_yAxisTitleOffset);
49 
50  histogram->GetXaxis()->SetTitle(_xAxisName);
51  histogram->GetYaxis()->SetTitle(_yAxisName);
52 
53  double nBins = (double)histogram->GetNbinsX();
54  double markerSize = 15.0/nBins;
55  if (markerSize > 0.3) markerSize = 0.3;
56  histogram->SetMarkerSize(markerSize);
57 
58  if (setMinMax) histogram->SetMaximum(getGlobalMax());
59  if (setMinMax) histogram->SetMinimum(getGlobalMin());
60 
61  if (_bMargin < 0.01) histogram->GetXaxis()->SetNoExponent();
62 
63  VERBOSE_LOG << "Global Min = " << getGlobalMin();
64  VERBOSE_LOG << "Global Max = " << getGlobalMax();
65 
66 }
67 
68 
71 void RootPlotter1D::addVerticalLine(double xpos, int style, int colour){
72  double ymin = getGlobalMin();
73  double ymax = getGlobalMax();
74  VERBOSE_LOG << "Adding TLine from (" << xpos << ", " << ymin << ") to (" << xpos << ", " << ymax << ")";
75  TLine* line = new TLine(xpos ,ymin ,xpos, ymax );
76  line->SetLineColor(colour);
77  line->SetLineStyle(style);
78  _objToPlot.push_back(line);
79 }
80 
83 void RootPlotter1D::addHorizontalLine(double ypos, int style, int colour){
84  double xlow = getHistogram(0)->GetXaxis()->GetBinLowEdge(1);
85  double xhigh = getHistogram(0)->GetXaxis()->GetBinUpEdge (getHistogram(0)->GetXaxis()->GetNbins());
86  TLine* line = new TLine(xlow ,ypos ,xhigh, ypos );
87  line->SetLineColor(colour);
88  line->SetLineStyle(style);
89  _objToPlot.push_back(line);
90 }
91 
94 void RootPlotter1D::addHorizontalBox(double ypos, double width, int fillColour){
95 
96  double xlow = getHistogram(0)->GetXaxis()->GetBinLowEdge(1);
97  double xhigh = getHistogram(0)->GetXaxis()->GetBinUpEdge (getHistogram(0)->GetXaxis()->GetNbins());
98  TBox* box = new TBox(xlow ,ypos - (width*0.5) ,xhigh, ypos + (width*0.5) );
99  box->SetLineColor(0);
100  box->SetFillColor(fillColour);
101  _objToPlot.push_back(box);
102 }
103 
106 void RootPlotter1D::addVerticalBox(double xmin, double xmax, int fillColour, int fillstyle){
107  double ymin = getGlobalMin();
108  double ymax = getGlobalMax();
109 
110  TBox* box = new TBox(xmin ,ymin ,xmax, ymax );
111  box->SetLineColor(0);
112  box->SetFillColor(fillColour);
113  box->SetFillStyle(fillstyle);
114  _objToPlot.push_back(box);
115 }
116 
119 
120  if ( _forcedMin != -999.999) return _forcedMin;
121 
122  double globalMin = getHistogram(0)->GetBinContent(getHistogram(0)->GetMinimumBin());
123 
124  for (unsigned int h=1; h<_histograms.size(); h++)
125  {
126  double min = getHistogram(h)->GetBinContent(getHistogram(h)->GetMinimumBin());
127  if (min < globalMin) globalMin = min;
128  }
129 
130  if (globalMin > 0) globalMin = globalMin*0.95;
131  else globalMin = globalMin*1.05;
132 
133  return globalMin;
134 }
135 
138 
139  if ( _forcedMax != -999.999) return _forcedMax;
140 
141  double globalMax = getHistogram(0)->GetBinContent(getHistogram(0)->GetMaximumBin());
142  for (unsigned int h=1; h<_histograms.size(); h++)
143  {
144  double max = getHistogram(h)->GetBinContent(getHistogram(h)->GetMaximumBin());
145  if (max > globalMax) globalMax = max;
146  }
147 
148  if (globalMax > 0) globalMax = globalMax*1.05;
149  else globalMax = globalMax*0.95;
150 
151 
152 
153  return globalMax;
154 
155 }
156 
157 
158 
160 
161 }
162 
163 
164 
165 
166 
167 
168 
169 void RootPlotter1D::plotWithRatio(TString plotDirectory, TString plotOptions, TPad* pad){
170 
171  double splitHeight = 0.3;
172  double scaleFactor = (1.0 - splitHeight) / splitHeight;
173 
174  if (pad == 0) pad = _canvas;
175 
176  TString upperPadName = (TString)_canvas->GetName() + "_upper";
177  TString lowerPadName = (TString)_canvas->GetName() + "_lower";
178  TPad* upperPad = new TPad(upperPadName, upperPadName, 0.0, splitHeight, 1.0, 1.0);
179  TPad* lowerPad = new TPad(lowerPadName, lowerPadName, 0.0, 0.0, 1.0, splitHeight);
180 
181  plotRatio("", "",lowerPad, scaleFactor);
182  plot ("", plotOptions,upperPad );
183 
184  pad->cd();
185  upperPad->Draw();
186  lowerPad->Draw();
187  if (plotDirectory != "") pad->Print(plotDirectory + s_imageformat);
188 }
189 
190 
191 void RootPlotter1D::plotRatio(TString plotDirectory, TString plotOptions, TPad* pad, double scaleFactor, double* returnMin, double* returnMax){
192 
193  if (pad == 0) pad = _canvas;
194  pad->cd();
195  TH1D* divHist0 = new TH1D( gDivideTH1D((TH1D*)getHistogram(1), (TH1D*)getHistogram(0), (TString)"division_1" + getHistogram(0)->GetName() ));
196  divHist0->GetXaxis()->SetTitle( getHistogram(0)->GetXaxis()->GetTitle() );
197  divHist0->GetYaxis()->SetTitle( getHistogram(0)->GetYaxis()->GetTitle() );
198 
199  //if there is a scale factor it means that be are adding this underneath another plot.
200  //We therefore adjust the histogram ticks to be the same in both
201 
202  if (scaleFactor != 1.0){
203  divHist0->GetXaxis()->SetTickLength(divHist0->GetXaxis()->GetTickLength()*scaleFactor);
204  divHist0->GetYaxis()->SetNdivisions(502);
205  }
206 
207  RootPlotter1D divPlotter(divHist0);
208  divPlotter.setHistogramOwnership(true);
209 
210  for (unsigned int i = 2; i < _histograms.size(); i++) {
211  TH1D* divHist = new TH1D( gDivideTH1D((TH1D*)getHistogram(i), (TH1D*)getHistogram(0), "division_" + makeString<int>(i) + getHistogram(i)->GetName()) );
212  divPlotter.add(divHist);
213  }
214 
215  //if there is a scale factor it means that be are adding this underneath another plot.
216  //We therefore adjust all the margins and scale the labels to be the same in both sets
217  //of plots
218 
219  if (scaleFactor != 1.0){
220  this-> _bMargin = 0.015;
221  divPlotter._tMargin = 0.07;
222  divPlotter._bMargin = divPlotter._bMargin * scaleFactor;
223  divPlotter._xAxisLabelSize = divPlotter._xAxisLabelSize * scaleFactor;
224  divPlotter._yAxisLabelSize = divPlotter._yAxisLabelSize * scaleFactor;
225  divPlotter._xAxisTitleSize = divPlotter._xAxisTitleSize * scaleFactor;
226  divPlotter._yAxisTitleSize = divPlotter._yAxisTitleSize * scaleFactor;
227  }
228 
229  double min = divPlotter.getRatioMin(&divPlotter);
230  double max = divPlotter.getRatioMax(&divPlotter);
231 
232  if ( fabs(min-1) > fabs(max-1) ) max = 1 + fabs(min-1);
233 
234  divPlotter.setMin(2.0-max);
235  divPlotter.setMax(max);
236 
237  if(returnMin != 0) *returnMin = 2.0-max;
238  if(returnMax != 0) *returnMax = max;
239 
240  divPlotter.addHorizontalLine(1.0, 2);
241  divPlotter.plot("", plotOptions, pad);
242 
243 
244  if (plotDirectory != "") pad->Print(plotDirectory + s_imageformat);
245 }
246 
247 void RootPlotter1D::plotPulls(TString plotDirectory, TString plotOptions , TPad* pad, double scaleFactor){
248 
249  if (pad == 0) pad = _canvas;
250  pad->cd();
251  TH1D* divHist0 = new TH1D( gPullTH1D((TH1D*)getHistogram(1), (TH1D*)getHistogram(0), (TString)"division_1" + getHistogram(0)->GetName() ));
252  divHist0->GetXaxis()->SetTitle( getHistogram(0)->GetXaxis()->GetTitle() );
253  divHist0->GetYaxis()->SetTitle( getHistogram(0)->GetYaxis()->GetTitle() );
254 
255  //if there is a scale factor it means that be are adding this underneath another plot.
256  //We therefore adjust the histogram ticks to be the same in both
257 
258  if (scaleFactor != 1.0){
259  divHist0->GetXaxis()->SetTickLength(divHist0->GetXaxis()->GetTickLength()*scaleFactor);
260  divHist0->GetYaxis()->SetNdivisions(502);
261  }
262 
263  RootPlotter1D divPlotter(divHist0);
264  for (unsigned int i = 2; i < _histograms.size(); i++) {
265  TH1D* divHist = new TH1D( gPullTH1D((TH1D*)getHistogram(i), (TH1D*)getHistogram(0), "division_" + makeString<int>(i) + getHistogram(i)->GetName()) );
266  divPlotter.add(divHist);
267  }
268 
269  //if there is a scale factor it means that be are adding this underneath another plot.
270  //We therefore adjust all the margins and scale the labels to be the same in both sets
271  //of plots
272 
273  if (scaleFactor != 1.0){
274  this-> _bMargin = 0.0001;
275  divPlotter._tMargin = 0.0001;
276  divPlotter._bMargin = divPlotter._bMargin * scaleFactor;
277  divPlotter._xAxisLabelSize = divPlotter._xAxisLabelSize * scaleFactor;
278  divPlotter._yAxisLabelSize = divPlotter._yAxisLabelSize * scaleFactor;
279  divPlotter._xAxisTitleSize = divPlotter._xAxisTitleSize * scaleFactor;
280  divPlotter._yAxisTitleSize = divPlotter._yAxisTitleSize * scaleFactor;
281  divPlotter.setMax(5.0);
282  divPlotter.setMin(-5.0);
283  }
284 
285 
286 
287  divPlotter.addHorizontalBox(0.0, 2.0, 17);
288  divPlotter.addHorizontalLine(0.0, 2);
289  divPlotter.plot("", plotOptions, pad);
290 
291 
292  if (plotDirectory != "") pad->Print(plotDirectory + s_imageformat);
293 
294 }
295 
296 void RootPlotter1D::plotWithPulls(TString plotDirectory, TString plotOptions , TPad* pad){
297  double splitHeight = 0.3;
298  double scaleFactor = (1.0 - splitHeight) / splitHeight;
299 
300  if (pad == 0) pad = _canvas;
301 
302  TString upperPadName = (TString)_canvas->GetName() + "_upper";
303  TString lowerPadName = (TString)_canvas->GetName() + "_lower";
304  TPad* upperPad = new TPad(upperPadName, upperPadName, 0.0, splitHeight, 1.0, 1.0);
305  TPad* lowerPad = new TPad(lowerPadName, lowerPadName, 0.0, 0.0, 1.0, splitHeight);
306 
307  plotPulls("", plotOptions,lowerPad, scaleFactor);
308  plot ("", plotOptions,upperPad);
309 
310  pad->cd();
311  upperPad->Draw();
312  lowerPad->Draw();
313  if (plotDirectory != "") pad->Print(plotDirectory + s_imageformat);
314 }
315 
317 
318  if ( _ratioMin != -999.999) return _ratioMin;
319  if ( s_ratioMin != -999.999) return s_ratioMin;
320 
321  ratioPlotter->setMin(-999.999);
322 
323  double min = ratioPlotter->getGlobalMin() - 1;
324  double absMin = fabs(min);
325 
326  double base = pow(10, floor(log10(absMin)));
327  int unit = ceil(double(absMin)/base);
328  double roundedMin = double(unit)*base;
329 
330  if (min < 0.0) roundedMin = -roundedMin;
331 
332  return 1.0 + roundedMin;
333 
334 }
335 
337 
338  if ( _ratioMax != -999.999) return _ratioMax;
339  if ( s_ratioMax != -999.999) return s_ratioMax;
340 
341  ratioPlotter->setMax(-999.999);
342 
343  double max = ratioPlotter->getGlobalMax() - 1;
344  double absMax = fabs(max);
345 
346  double base = pow(10, floor(log10(absMax)));
347  int unit = ceil(double(absMax)/base);
348  double roundedMax = double(unit)*base;
349 
350  if (max < 0.0) roundedMax = -roundedMax;
351 
352  return 1.0 + roundedMax;
353 
354 }
double _yAxisTickLength
Definition: Plotter.h:53
static TString s_imageformat
Definition: Plotter.h:71
double _tMargin
Definition: Plotter.h:43
virtual void setHistogramStyle(TH1 *histogram, bool setMinMax=1)
virtual double getGlobalMin()
double _rMargin
Definition: Plotter.h:42
std::vector< TObject * > _histograms
Definition: Plotter.h:38
double _forcedMax
Definition: Plotter.h:35
double _yAxisLabelOffset
Definition: Plotter.h:50
double _xAxisLabelOffset
Definition: Plotter.h:49
std::vector< TObject * > _objToPlot
Definition: Plotter.h:37
static double s_ratioMin
Definition: RootPlotter1D.h:53
double _yAxisTitleSize
Definition: Plotter.h:59
TPad * _canvas
Definition: Plotter.h:33
double getRatioMin(RootPlotter1D *ratioPlotter)
void plotPulls(TString plotDirectory, TString plotOptions="", TPad *pad=0, double scaleFactor=1.0)
virtual double getGlobalMax()
double _xAxisLabelSize
Definition: Plotter.h:55
virtual ~RootPlotter1D()
double _xAxisTitleSize
Definition: Plotter.h:58
#define VERBOSE_LOG
double _forcedMin
Definition: Plotter.h:36
void plotWithRatio(TString plotDirectory, TString plotOptions="", TPad *pad=0)
void plotWithPulls(TString plotDirectory, TString plotOptions="", TPad *pad=0)
double _ratioMin
Definition: RootPlotter1D.h:30
void plotRatio(TString plotDirectory, TString plotOptions="", TPad *pad=0, double scaleFactor=1.0, double *returnMin=0, double *returnMax=0)
void setMin(double min)
Definition: Plotter.h:153
double _xAxisTitleOffset
Definition: Plotter.h:46
virtual void plot(TString plotDirectory, TString plotOptions="", TPad *pad=0, double scaleFactor=1.0)
void setMax(double max)
Definition: Plotter.h:155
double _xAxisTickLength
Definition: Plotter.h:52
double _bMargin
Definition: Plotter.h:44
void addVerticalLine(double xpos, int style=1, int colour=1)
void addHorizontalLine(double ypos, int style=1, int colour=1)
TString _yAxisName
Definition: RootPlotter.h:28
TString _xAxisName
Definition: RootPlotter.h:27
void setHistogramOwnership(bool i=1)
Definition: Plotter.h:86
double _lMargin
Definition: Plotter.h:41
void addVerticalBox(double xmin, double xmax, int fillColour, int fillstyle)
double _yAxisTitleOffset
Definition: Plotter.h:47
#define WELCOME_LOG
double getRatioMax(RootPlotter1D *ratioPlotter)
void add(TObject *histogram)
Definition: Plotter.cpp:122
TH1D gDivideTH1D(TH1D const *hist1, TH1D const *hist2, TString name)
TH1 * getHistogram(int i)
Definition: RootPlotter.cpp:16
double _ratioMax
Definition: RootPlotter1D.h:29
void addHorizontalBox(double ypos, double width, int fillColour)
double _yAxisLabelSize
Definition: Plotter.h:56
RootPlotter1D(TH1 *histogram, double width=400, double height=300)
TH1D gPullTH1D(TH1D const *hist1, TH1D const *hist2, TString name)
static double s_ratioMax
Definition: RootPlotter1D.h:50