MINT2
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
RootPlotter Class Referenceabstract

#include <RootPlotter.h>

Inheritance diagram for RootPlotter:
Plotter RootPlotter1D RootPlotter2D

Public Member Functions

void setXaxisName (TString name)
 
void setYaxisName (TString name)
 
virtual double getGlobalMax ()
 
virtual double getGlobalMin ()
 
 RootPlotter (TH1 *histogram, double width=300, double height=200)
 
TString & drawOptions (int i)
 
TString & objDrawOptions (int i)
 
void plotStacked (TPad *pad, double scaleFactor)
 
void plotSame (TPad *pad, TString plotOptions, double scaleFactor=1.0)
 
virtual void plot (TString plotDirectory, TString plotOptions="", TPad *pad=0, double scaleFactor=1.0)
 
void addText (TString text, double x, double y, int alignh=1, int alignv=2, double size=0.06, int ndc=true, int color=kBlack)
 
void drawLegend ()
 
virtual ~RootPlotter ()
 
- Public Member Functions inherited from Plotter
int getColor (int i)
 
void setColor (int i, int color)
 
void usePresetColours (bool val=true)
 
void allImageFormats (bool val=true)
 
void setHistogramOwnership (bool i=1)
 
void setObjectOwnership (bool i=1)
 
 Plotter (TString canvasName, double width, double height)
 
 Plotter (const Plotter &other)
 
void add (TObject *histogram)
 
void addDot (double xpos, double ypos, double size, int colour=1, TString shape="circle", double sizeY=0.0)
 
void logX (bool log=1)
 
void logY (bool log=1)
 
void logZ (bool log=1)
 
void addObject (TObject *obj)
 
int getNumObjects ()
 
void setImageFormat (TString format)
 
TPad * getCanvas ()
 
void scaleTextSize (double scale)
 
void scaleAxisTitleSize (double scale)
 
void scaleAxisTitleOffset (double scale)
 
void setXAxisLabelSize (double val)
 
void setYAxisLabelSize (double val)
 
void setXAxisTitleSize (double val)
 
void setYAxisTitleSize (double val)
 
void setXAxisLabelOffset (double val)
 
void setYAxisLabelOffset (double val)
 
void setXAxisTitleOffset (double val)
 
void setYAxisTitleOffset (double val)
 
void setXAxisTickLength (double val)
 
void setYAxisTickLength (double val)
 
void setPropertiesFromTH1 (TH1 *hist)
 
void setMin (double min)
 
void setMax (double max)
 
void setBMargin (double val)
 
void setLMargin (double val)
 
void setRMargin (double val)
 
void setTMargin (double val)
 
virtual ~Plotter ()
 

Protected Member Functions

virtual void setHistogramStyle (TH1 *histogram, bool setMinMax=1)=0
 
TH1 * getHistogram (int i)
 
- Protected Member Functions inherited from Plotter
virtual void setCanvasDefaults (TPad *pad)
 

Protected Attributes

TString _xAxisName
 
TString _yAxisName
 
std::vector< TString > _drawOptions
 
std::vector< TString > _objDrawOptions
 
- Protected Attributes inherited from Plotter
TPad * _canvas
 
TLegend * _legend
 
double _forcedMax
 
double _forcedMin
 
std::vector< TObject * > _objToPlot
 
std::vector< TObject * > _histograms
 
std::vector< int > _colours
 
double _lMargin
 
double _rMargin
 
double _tMargin
 
double _bMargin
 
double _xAxisTitleOffset
 
double _yAxisTitleOffset
 
double _xAxisLabelOffset
 
double _yAxisLabelOffset
 
double _xAxisTickLength
 
double _yAxisTickLength
 
double _xAxisLabelSize
 
double _yAxisLabelSize
 
double _xAxisTitleSize
 
double _yAxisTitleSize
 
bool _histogramOwnership
 
bool _objectOwnership
 
bool _usePresetColours
 
bool _allImageFormats
 

Additional Inherited Members

- Static Public Attributes inherited from Plotter
static TString s_imageformat = ".eps"
 
static TString s_imageformat2 = ""
 
static TString s_legend_position = "RightTop"
 
static int s_plotterCount = 0
 
static double s_forcedMax = -999.999
 
static double s_forcedMin = -999.999
 

Detailed Description

HyperPlot, Author: Sam Harnew, sam.h.nosp@m.arne.nosp@m.w@gma.nosp@m.il.c.nosp@m.om , Date: Dec 2015

Class to make plotting root histograms a little easier.

Definition at line 23 of file RootPlotter.h.

Constructor & Destructor Documentation

◆ RootPlotter()

RootPlotter::RootPlotter ( TH1 *  histogram,
double  width = 300,
double  height = 200 
)

Construct a RootPlotter with one histogram on a canvas with specified width and height

Definition at line 4 of file RootPlotter.cpp.

4  :
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 }
std::vector< TObject * > _histograms
Definition: Plotter.h:38
TString _yAxisName
Definition: RootPlotter.h:28
TString _xAxisName
Definition: RootPlotter.h:27
#define WELCOME_LOG
Plotter(TString canvasName, double width, double height)
Definition: Plotter.cpp:20

◆ ~RootPlotter()

RootPlotter::~RootPlotter ( )
virtual

Destructor.

Definition at line 210 of file RootPlotter.cpp.

210  {
211 
212 }

Member Function Documentation

◆ addText()

void RootPlotter::addText ( TString  text,
double  x,
double  y,
int  alignh = 1,
int  alignv = 2,
double  size = 0.06,
int  ndc = true,
int  color = kBlack 
)

Add a text string at a point (x,y) on the histogram

Definition at line 33 of file RootPlotter.cpp.

33  {
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 }
void addObject(TObject *obj)
Definition: Plotter.cpp:168

◆ drawLegend()

void RootPlotter::drawLegend ( )

Add a legend to the Canvas - not really tested

Definition at line 49 of file RootPlotter.cpp.

49  {
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 }
std::vector< TObject * > _histograms
Definition: Plotter.h:38
#define VERBOSE_LOG
TLegend * _legend
Definition: Plotter.h:34
TH1 * getHistogram(int i)
Definition: RootPlotter.cpp:16
static TString s_legend_position
Definition: Plotter.h:73

◆ drawOptions()

TString & RootPlotter::drawOptions ( int  i)

Get a reference to the draw options of histogram i - used for getting and setting

Definition at line 21 of file RootPlotter.cpp.

21  {
22  while ((int)_drawOptions.size() <= i) _drawOptions.push_back("");
23  return _drawOptions.at(i);
24 }
std::vector< TString > _drawOptions
Definition: RootPlotter.h:30

◆ getGlobalMax()

virtual double RootPlotter::getGlobalMax ( )
inlinevirtual

Get the minimum of all histograms added (or the forced min if set)

Reimplemented in RootPlotter1D, and RootPlotter2D.

Definition at line 44 of file RootPlotter.h.

◆ getGlobalMin()

virtual double RootPlotter::getGlobalMin ( )
inlinevirtual

Get the maximum of all histograms added (or the forced max if set)

Reimplemented in RootPlotter1D, and RootPlotter2D.

Definition at line 45 of file RootPlotter.h.

◆ getHistogram()

TH1 * RootPlotter::getHistogram ( int  i)
protected

Get one of the histograms that has been added to the plotter

Definition at line 16 of file RootPlotter.cpp.

16  {
17  return (TH1*) _histograms.at(i);
18 }
std::vector< TObject * > _histograms
Definition: Plotter.h:38

◆ objDrawOptions()

TString & RootPlotter::objDrawOptions ( int  i)

Get a reference to the draw options of object i - used for getting and setting

Definition at line 27 of file RootPlotter.cpp.

27  {
28  while ((int)_objDrawOptions.size() <= i) _objDrawOptions.push_back("");
29  return _objDrawOptions.at(i);
30 }
std::vector< TString > _objDrawOptions
Definition: RootPlotter.h:31

◆ plot()

void RootPlotter::plot ( TString  plotDirectory,
TString  plotOptions = "",
TPad *  pad = 0,
double  scaleFactor = 1.0 
)
virtual

Call this to save the canvas to a specific directory.

Implements Plotter.

Definition at line 184 of file RootPlotter.cpp.

184  {
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 }
static TString s_imageformat
Definition: Plotter.h:71
virtual void setCanvasDefaults(TPad *pad)
Definition: Plotter.cpp:84
TPad * _canvas
Definition: Plotter.h:33
static TString s_imageformat2
Definition: Plotter.h:72
void plotSame(TPad *pad, TString plotOptions, double scaleFactor=1.0)
void plotStacked(TPad *pad, double scaleFactor)
Definition: RootPlotter.cpp:74
bool _allImageFormats
Definition: Plotter.h:65

◆ plotSame()

void RootPlotter::plotSame ( TPad *  pad,
TString  plotOptions,
double  scaleFactor = 1.0 
)

plot with the option SAME

Definition at line 125 of file RootPlotter.cpp.

125  {
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 }
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
#define VERBOSE_LOG
TString & objDrawOptions(int i)
Definition: RootPlotter.cpp:27
double _xAxisTickLength
Definition: Plotter.h:52
TString & drawOptions(int i)
Definition: RootPlotter.cpp:21
bool _usePresetColours
Definition: Plotter.h:64
virtual void setHistogramStyle(TH1 *histogram, bool setMinMax=1)=0
double _yAxisTitleOffset
Definition: Plotter.h:47
TH1 * getHistogram(int i)
Definition: RootPlotter.cpp:16
double _yAxisLabelSize
Definition: Plotter.h:56
int getColor(int i)
Definition: Plotter.cpp:109

◆ plotStacked()

void RootPlotter::plotStacked ( TPad *  pad,
double  scaleFactor 
)

plot stacked

Definition at line 74 of file RootPlotter.cpp.

74  {
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 }
double _tMargin
Definition: Plotter.h:43
virtual void setCanvasDefaults(TPad *pad)
Definition: Plotter.cpp:84
std::vector< TObject * > _histograms
Definition: Plotter.h:38
double _yAxisTitleSize
Definition: Plotter.h:59
TPad * _canvas
Definition: Plotter.h:33
double _xAxisLabelSize
Definition: Plotter.h:55
double _xAxisTitleSize
Definition: Plotter.h:58
double _bMargin
Definition: Plotter.h:44
virtual void setHistogramStyle(TH1 *histogram, bool setMinMax=1)=0
TH1 * getHistogram(int i)
Definition: RootPlotter.cpp:16
double _yAxisLabelSize
Definition: Plotter.h:56
int getColor(int i)
Definition: Plotter.cpp:109

◆ setHistogramStyle()

virtual void RootPlotter::setHistogramStyle ( TH1 *  histogram,
bool  setMinMax = 1 
)
protectedpure virtual

purely virtual function that sets the correct histogram style (different for TH1 and TH2)

Implemented in RootPlotter1D, and RootPlotter2D.

◆ setXaxisName()

void RootPlotter::setXaxisName ( TString  name)
inline

Set the x-axis title name

Definition at line 41 of file RootPlotter.h.

◆ setYaxisName()

void RootPlotter::setYaxisName ( TString  name)
inline

Set the y-axis title name

Definition at line 42 of file RootPlotter.h.

Member Data Documentation

◆ _drawOptions

std::vector<TString> RootPlotter::_drawOptions
protected

options passed to Root on how to draw each histogram added

Definition at line 30 of file RootPlotter.h.

◆ _objDrawOptions

std::vector<TString> RootPlotter::_objDrawOptions
protected

options passed to Root on how to draw each object added

Definition at line 31 of file RootPlotter.h.

◆ _xAxisName

TString RootPlotter::_xAxisName
protected

xAxis name - by default taken from the first histogram added

Definition at line 27 of file RootPlotter.h.

◆ _yAxisName

TString RootPlotter::_yAxisName
protected

yAxis name - by default taken from the first histogram added

Definition at line 28 of file RootPlotter.h.


The documentation for this class was generated from the following files: