MINT2
RootPlotter.cpp
Go to the documentation of this file.
1 #include "Mint/RootPlotter.h"
2 
4 RootPlotter::RootPlotter(TH1* histogram, double width, double height) :
5  Plotter("canvas_" + (TString)histogram->GetName(), width, height),
6  _xAxisName(histogram->GetXaxis()->GetTitle()),
7  _yAxisName(histogram->GetYaxis()->GetTitle())
8 {
9  _histograms.push_back(histogram);
10 
11  WELCOME_LOG << "Hello from the Plotter() constructor!";
12 }
13 
14 
17  return (TH1*) _histograms.at(i);
18 }
19 
21 TString& RootPlotter::drawOptions(int i){
22  while ((int)_drawOptions.size() <= i) _drawOptions.push_back("");
23  return _drawOptions.at(i);
24 }
25 
28  while ((int)_objDrawOptions.size() <= i) _objDrawOptions.push_back("");
29  return _objDrawOptions.at(i);
30 }
31 
33 void RootPlotter::addText(TString text, double x, double y, int alignh, int alignv, double size, int ndc, int color){
34 
35  //1left 2center 3right
36 
37  TLatex* textL = new TLatex(x, y, text);
38 
39  if (ndc == true) textL->SetNDC();
40  textL->SetTextAlign(alignh*10 + alignv);
41  textL->SetTextSize(size);
42  textL->SetTextColor(color);
43 
44  this->addObject(textL);
45 
46 }
47 
50 
51 
52  if (_legend != 0) {delete _legend; _legend = 0;}
53 
54  if (s_legend_position == "TopRight" ) _legend = new TLegend(0.55,0.6,0.85,0.78);
55  else if (s_legend_position == "BottomRight") _legend = new TLegend(0.55,0.2,0.85,0.38);
56  else if (s_legend_position == "TopLeft" ) _legend = new TLegend(0.15,0.6,0.5,0.78);
57  else if (s_legend_position == "BottomLeft" ) _legend = new TLegend(0.6,0.6,0.85,0.8);
58 
59  VERBOSE_LOG << "Legend position is " << s_legend_position;
60 
61  if (_legend != 0){
62  _legend->SetFillColor(0);
63 
64  for (unsigned int i = 1; i < _histograms.size(); i++){
65  _legend->AddEntry(getHistogram(i),getHistogram(i)->GetTitle(),"l");
66  }
67 
68  _legend->Draw();
69  }
70 
71 }
72 
74 void RootPlotter::plotStacked(TPad* pad, double scaleFactor){
75 
76 
77  if (scaleFactor != 1.0){
78  _tMargin = 0.0001;
79  _bMargin = _bMargin * scaleFactor;
80  _xAxisLabelSize = _xAxisLabelSize * scaleFactor;
81  _yAxisLabelSize = _yAxisLabelSize * scaleFactor;
82  _xAxisTitleSize = _xAxisTitleSize * scaleFactor;
83  _yAxisTitleSize = _yAxisTitleSize * scaleFactor;
84  }
85  this->setCanvasDefaults(pad);
86  gStyle->SetHistTopMargin(0.0);
87 
88  THStack* histogramStack = new THStack("stackedHistograms","stackedHistograms");
89 
90 
91  for (unsigned int i = 0; i < _histograms.size(); i++) {
92  this->setHistogramStyle( getHistogram(i) , 0);
93  getHistogram(i)->SetFillColor(getColor(i));
94  histogramStack->Add(getHistogram(i));
95  }
96 
97  _canvas->cd();
98  histogramStack->Draw("hist");
99  pad->Draw();
100 
101  histogramStack->GetYaxis()->SetNdivisions( getHistogram(0)->GetYaxis()->GetNdivisions () );
102  histogramStack->GetYaxis()->CenterTitle ( getHistogram(0)->GetYaxis()->GetCenterTitle() );
103  histogramStack->GetXaxis()->SetNdivisions( getHistogram(0)->GetXaxis()->GetNdivisions () );
104  histogramStack->GetXaxis()->CenterTitle ( getHistogram(0)->GetXaxis()->GetCenterTitle() );
105 
106  //The histogram is not drawn until the pad is drawn on a canvas - we must therefore
107  // draw the pad before the GetHistogram() returns something. Might run into problems
108  // if we start using pads within pads
109 
110  this->setHistogramStyle( histogramStack->GetHistogram(), 0 );
111 
112  if (scaleFactor != 1.0){
113  //histogramStack->GetHistogram()->GetXaxis()->SetTickLength(histogramStack->GetHistogram()->GetXaxis()->GetTickLength()*scaleFactor);
114  histogramStack->GetHistogram()->GetYaxis()->SetNdivisions(502);
115  }
116 
117  pad->cd();
118  histogramStack->DrawClone("hist");
119 
120  delete histogramStack;
121 }
122 
123 
125 void RootPlotter::plotSame(TPad* pad, TString plotOptions, double scaleFactor){
126 
127  if (pad == 0) pad = _canvas;
128  this->setCanvasDefaults(pad);
129  pad->cd();
130 
131  if (scaleFactor != 1.0){
132  //std::cout << "Scaling histogram in pad " << pad->GetName() << " by " << scaleFactor << std::endl;
133  _yAxisLabelSize = _yAxisLabelSize * scaleFactor;
134  _yAxisTitleSize = _yAxisTitleSize * scaleFactor;
135  _xAxisLabelOffset = _xAxisLabelOffset * scaleFactor;
136  _xAxisTickLength = _xAxisTickLength * scaleFactor;
137  _yAxisTitleOffset = _yAxisTitleOffset * (1.0/scaleFactor);
138  //getHistogram(0)->GetXaxis()->SetTickLength( getHistogram(0)->GetXaxis()->GetTickLength()*scaleFactor );
139  //getHistogram(0)->GetYaxis()->SetTickLength( getHistogram(0)->GetYaxis()->GetTickLength()*scaleFactor );
140  //getHistogram(0)->GetYaxis()->SetNdivisions(502);
141  }
142 
143 
144 
145  VERBOSE_LOG << "Setting histogram style";
146  this->setHistogramStyle( getHistogram(0) );
147  if (_usePresetColours) getHistogram(0)->SetLineColor(getColor(0));
148  if (_usePresetColours) getHistogram(0)->SetMarkerColor(getColor(0));
149  VERBOSE_LOG << "Draw axis";
150 
151  getHistogram(0)->DrawCopy(plotOptions + "AXIS");
152  this->setHistogramStyle( getHistogram(0) );
153  getHistogram(0)->DrawCopy(plotOptions + "AXIS");
154 
155 
156  for (unsigned int i = 0; i < _histograms.size(); i++){
157  VERBOSE_LOG << "Set style" << i;
158  this->setHistogramStyle( getHistogram(i) );
159  if (_usePresetColours)getHistogram(i)->SetLineColor(getColor(i));
160  //getHistogram(i)->SetFillColor(getColor(i));
161  if (_usePresetColours)getHistogram(i)->SetMarkerColor(getColor(i));
162  VERBOSE_LOG << "Draw" << i;
163  getHistogram(i)->DrawCopy(plotOptions + drawOptions(i) + "SAME");
164  }
165 
166 
167 
168  for (unsigned int i = 0; i < _objToPlot.size(); i++){
169  if ( TString(_objToPlot.at(i)->ClassName()).Contains("TGraph") ){
170  if (_usePresetColours) ((TGraph*)_objToPlot.at(i))->SetLineColor (getColor(i));
171  if (_usePresetColours) ((TGraph*)_objToPlot.at(i))->SetMarkerColor(getColor(i));
172  }
173  _objToPlot.at(i)->DrawClone(plotOptions + "SAME" + objDrawOptions(i) );
174  //INFO_LOG << "Drawing object" << i << " with option " << objDrawOptions(i);
175  }
176  VERBOSE_LOG << "Draw axis";
177  getHistogram(0)->DrawCopy(plotOptions + "SAME AXIS");
178 
179  drawLegend();
180 
181 }
182 
184 void RootPlotter::plot(TString plotDirectory, TString plotOptions, TPad* pad, double scaleFactor){
185 
186  if (pad == 0) pad = _canvas;
187 
188  this->setCanvasDefaults(pad);
189  pad->cd();
190  //set the histogram options on the first histogram
191 
192  if (plotOptions == "STACKED") plotStacked(pad, scaleFactor);
193  else plotSame (pad, plotOptions, scaleFactor);
194 
195  pad->Update();
196  if (plotDirectory != "") {
197  pad->Print(plotDirectory + s_imageformat);
198  if (s_imageformat2 != "") pad->Print(plotDirectory + s_imageformat2);
199  if (_allImageFormats == true){
200  pad->Print(plotDirectory + ".pdf");
201  pad->Print(plotDirectory + ".eps");
202  pad->Print(plotDirectory + ".png");
203  pad->Print(plotDirectory + ".C" );
204  }
205  }
206 }
207 
208 
211 
212 }
static TString s_imageformat
Definition: Plotter.h:71
double _tMargin
Definition: Plotter.h:43
void addObject(TObject *obj)
Definition: Plotter.cpp:168
virtual void setCanvasDefaults(TPad *pad)
Definition: Plotter.cpp:84
std::vector< TObject * > _histograms
Definition: Plotter.h:38
double _xAxisLabelOffset
Definition: Plotter.h:49
std::vector< TObject * > _objToPlot
Definition: Plotter.h:37
void drawLegend()
Definition: RootPlotter.cpp:49
double _yAxisTitleSize
Definition: Plotter.h:59
TPad * _canvas
Definition: Plotter.h:33
double _xAxisLabelSize
Definition: Plotter.h:55
double _xAxisTitleSize
Definition: Plotter.h:58
virtual ~RootPlotter()
#define VERBOSE_LOG
static TString s_imageformat2
Definition: Plotter.h:72
void plotSame(TPad *pad, TString plotOptions, double scaleFactor=1.0)
TLegend * _legend
Definition: Plotter.h:34
TString & objDrawOptions(int i)
Definition: RootPlotter.cpp:27
RootPlotter(TH1 *histogram, double width=300, double height=200)
Definition: RootPlotter.cpp:4
virtual void plot(TString plotDirectory, TString plotOptions="", TPad *pad=0, double scaleFactor=1.0)
double _xAxisTickLength
Definition: Plotter.h:52
double _bMargin
Definition: Plotter.h:44
TString & drawOptions(int i)
Definition: RootPlotter.cpp:21
bool _usePresetColours
Definition: Plotter.h:64
void plotStacked(TPad *pad, double scaleFactor)
Definition: RootPlotter.cpp:74
virtual void setHistogramStyle(TH1 *histogram, bool setMinMax=1)=0
double _yAxisTitleOffset
Definition: Plotter.h:47
bool _allImageFormats
Definition: Plotter.h:65
#define WELCOME_LOG
void addText(TString text, double x, double y, int alignh=1, int alignv=2, double size=0.06, int ndc=true, int color=kBlack)
Definition: RootPlotter.cpp:33
TH1 * getHistogram(int i)
Definition: RootPlotter.cpp:16
std::vector< TString > _drawOptions
Definition: RootPlotter.h:30
std::vector< TString > _objDrawOptions
Definition: RootPlotter.h:31
double _yAxisLabelSize
Definition: Plotter.h:56
static TString s_legend_position
Definition: Plotter.h:73
int getColor(int i)
Definition: Plotter.cpp:109