CombineHarvester
Plotting.h File Reference
#include <map>
#include <string>
#include <iostream>
#include <vector>
#include <cmath>
#include "TStyle.h"
#include "TGraph.h"
#include "TGraph2D.h"
#include "TCanvas.h"
#include "TPad.h"
#include "TLatex.h"
#include "TAxis.h"
#include "TList.h"
#include "TH1F.h"
#include "TLegend.h"
#include "TColor.h"
#include "TROOT.h"
#include "TFrame.h"
#include "TTree.h"
#include "TFile.h"
#include "TLine.h"
#include "TMultiGraph.h"
#include "THStack.h"

Go to the source code of this file.

Functions

Pad and axis histogram creation
std::vector< TPad * > OnePad ()
 Just creates a single pad filling the entire canvas. More...
 
std::vector< TPad * > TwoPadSplit (double split_point, double gap_low, double gap_high)
 Create two pads, split horizontally, on the current canvas split. More...
 
TH1 * CreateAxisHist (TH1 *src, double xmin=0, double xmax=-1)
 Create an empty TH1 from another TH1 for drawing the axes. More...
 
std::vector< TH1 * > CreateAxisHists (unsigned n, TH1 *src, double xmin=0, double xmax=-1)
 Create multiple axis TH1s from another TH1. More...
 
TH1 * CreateAxisHist (TGraph *src, bool at_limits)
 Create an empty TH1 from a TGraph for drawing the axes. More...
 
std::vector< TH1 * > CreateAxisHists (unsigned n, TGraph *src, bool at_limits)
 Create multiple axis TH1s from a TGraph. More...
 
TH1 * GetAxisHist (TPad *pad)
 Finds the TH1 used to draw the axes on a given TPad. More...
 
Object extraction and manipulation
TH1 * MakeRatioHist (TH1 *num, TH1 *den, bool num_err, bool den_err)
 Create a new histogram by dividing one by the other. More...
 
TGraph TGraphFromTree (TTree *tree, TString const &xvar, TString const &yvar, TString const &selection="")
 Create a TGraph from entries in a TTree. More...
 
TGraph2D TGraph2DFromTree (TTree *tree, TString const &xvar, TString const &yvar, TString const &zvar, TString const &selection="")
 Create a TGraph2D from entries in a TTree. More...
 
void ReZeroTGraph (TGraph *gr)
 Shift all the graph y-values upwards such that there are no negative values and the minimum point is at zero. More...
 
Plot decoration (colours, styles, text and legends)
int CreateTransparentColor (int color, float alpha)
 Create a transparent version of a colour. More...
 
void SetupTwoPadSplitAsRatio (std::vector< TPad * > const &pads, TH1 *upper, TH1 *lower, TString y_title, bool y_centered, float y_min, float y_max)
 Set a few style options for a two-pad setup used to show a data-MC comparison and ratio plot. More...
 
void SetupTwoPadSplitAsRatio (std::vector< TPad * > const &pads, TString y_title, bool y_centered, float y_min, float y_max)
 Set a few style options for a two-pad setup used to show a data-MC comparison and ratio plot. More...
 
void StandardAxes (TAxis *xaxis, TAxis *yaxis, TString var, TString units)
 Sets standard x- and y-axis titles with given units. More...
 
void UnitAxes (TAxis *xaxis, TAxis *yaxis, TString var, TString units)
 Sets standard x- and y-axis titles when histograms are drawn divided by bin width. More...
 
TLegend * PositionedLegend (double width, double height, int pos, double offset)
 Create a legend with fixed height, width and positioning. More...
 
TLine * DrawHorizontalLine (TPad *pad, TLine *line, double yval)
 Use an existing TLine to draw a new horizontal line across the current frame. More...
 
void DrawTitle (TPad *pad, TString text, int align)
 Draw text in the top-margin region of a TPad. More...
 
void DrawCMSLogo (TPad *pad, TString cmsText, TString extraText, int iPosX, float relPosX, float relPosY, float relExtraDY)
 Draw the CMS logo and subtitle in the new style. More...
 
void DrawCMSLogo (TPad *pad, TString cmsText, TString extraText, int iPosX)
 Call DrawCMSLogo with some sensible defaults for the positioning parameters. More...
 
Axis adjustment and overlap checks
double GetPadYMax (TPad *pad, double x_min, double x_max)
 Find the maximum value of all drawn objects in a given x-axis range. More...
 
double GetPadYMax (TPad *pad)
 Call GetPadYMax(TPad*, double, double) with the full range of the given TPad. More...
 
void FixTopRange (TPad *pad, double fix_y, double fraction)
 Adjusts the y-axis maximum on the pad such that the specified y-value is positioned a fixed fraction from this new maximum. More...
 
void FixBoxPadding (TPad *pad, TBox *box, double frac)
 Modify the pad y-axis range to ensure there is at least a given gap between a particular TBox and the highest drawn element in the x-axis range of this box. More...
 
void FixOverlay ()
 Just re-draws the axes on the current TPad. More...
 

Function Documentation

◆ OnePad()

std::vector< TPad * > OnePad ( )

Just creates a single pad filling the entire canvas.

Exists just to provide a similar output interface to TwoPadSplit()

Returns
A vector containing a single pointer to the newly created TPad

Definition at line 459 of file Plotting.h.

◆ TwoPadSplit()

std::vector< TPad * > TwoPadSplit ( double  split_point,
double  gap_low,
double  gap_high 
)

Create two pads, split horizontally, on the current canvas split.

Note that both pads will actually cover the full area of the canvas. We create the appearance of two separated pads by adjusting the top and bottom margins, and then making the background of each pad transparent.

Parameters
split_pointFraction of the canvas that should be occuplied by the lower pad.
gap_lowThe top margin of the lower pad (with respect to the split point), given as a fraction of the input canvas height
gap_highThe bottom margin of the upper pad (with respect to the split point), given as a fraction of the input canvas height
Returns
A vector of pointers to the new pads: [0] = upper, [1] = lower. The current pad (gPad) will be set to the upper one.

Definition at line 468 of file Plotting.h.

◆ CreateAxisHist() [1/2]

TH1 * CreateAxisHist ( TH1 *  src,
double  xmin = 0,
double  xmax = -1 
)

Create an empty TH1 from another TH1 for drawing the axes.

A copy of the input TH1 is made, and all bin contents and errors are removed.

Parameters
srcTH1 input
xminadjust the minimum of the drawn range of the x-axis
xmaxadjust the maximum of the drawn range of the x-axis (only if xmax > xmin)
Returns
An empty TH1 with axes built from the input TH1

Definition at line 485 of file Plotting.h.

◆ CreateAxisHists() [1/2]

std::vector< TH1 * > CreateAxisHists ( unsigned  n,
TH1 *  src,
double  xmin = 0,
double  xmax = -1 
)

Create multiple axis TH1s from another TH1.

Just calls CreateAxisHist(TH1*, double, double) multiple times.

Parameters
nNumber of histograms to make
srcTH1 input
xminadjust the minimum of the drawn range of the x-axis
xmaxadjust the maximum of the drawn range of the x-axis (only if xmax > xmin)

Definition at line 494 of file Plotting.h.

◆ CreateAxisHist() [2/2]

TH1 * CreateAxisHist ( TGraph *  src,
bool  at_limits 
)

Create an empty TH1 from a TGraph for drawing the axes.

This function just draws the input TGraph on a temporary TCanvas and extracts the internal TH1 that ROOT creates to do this. In this process the x- and y-axis ranges are computing automatically.

Parameters
srcTGraph input
at_limitsBy default the x-axis range will go beyond the min and max x-values found in the TGraph. Set this option to true to fix the range to these min/max values.
Returns
An empty TH1 with axes built from the input TGraph

Definition at line 503 of file Plotting.h.

◆ CreateAxisHists() [2/2]

std::vector< TH1 * > CreateAxisHists ( unsigned  n,
TGraph *  src,
bool  at_limits 
)

Create multiple axis TH1s from a TGraph.

Just calls CreateAxisHist(TGraph*, bool) multiple times.

Parameters
nNumber of histograms to make
srcTGraph input
at_limitsBy default the x-axis range will go beyond the min and max x-values found in the TGraph. Set this option to true to fix the range to these min/max values.

Definition at line 528 of file Plotting.h.

◆ GetAxisHist()

TH1 * GetAxisHist ( TPad *  pad)

Finds the TH1 used to draw the axes on a given TPad.

Should be able to find a TH1 regardless of whether a TH1, THStack or TGraph was used to define the axes.

Parameters
padThe TPad to search
Returns
A pointer to the axis TH1 if it exists, a NULL pointer otherwise

Definition at line 536 of file Plotting.h.

◆ MakeRatioHist()

TH1 * MakeRatioHist ( TH1 *  num,
TH1 *  den,
bool  num_err,
bool  den_err 
)

Create a new histogram by dividing one by the other.

The errors on the two inputs are optionally propagated. Note that these are just combined in quadrature, thus assuming the inputs are uncorrelated. This does not hold for efficiency calculations.

Parameters
numNumerator histogram
denDenominator histogram
num_errPropagate errors on the numerator
den_errPropagate errors on the denominator
Returns
Pointer to the newly created TH1

Definition at line 567 of file Plotting.h.

◆ TGraphFromTree()

TGraph TGraphFromTree ( TTree *  tree,
TString const &  xvar,
TString const &  yvar,
TString const &  selection = "" 
)

Create a TGraph from entries in a TTree.

Note that the graph entries will be filled in whichever order they are found in the TTree. It may be desirable to call TGraph::Sort on the resulting object.

Parameters
treeInput TTree
xvarBranch or expression for the x-values
yvarBranch or expression for the y-values
selectionOptional cut string to apply to each entry

Definition at line 581 of file Plotting.h.

◆ TGraph2DFromTree()

TGraph2D TGraph2DFromTree ( TTree *  tree,
TString const &  xvar,
TString const &  yvar,
TString const &  zvar,
TString const &  selection = "" 
)

Create a TGraph2D from entries in a TTree.

Note that the graph entries will be filled in whichever order they are found in the TTree. It may be desirable to call TGraph::Sort on the resulting object.

Parameters
treeInput TTree
xvarBranch or expression for the x-values
yvarBranch or expression for the y-values
zvarBranch or expression for the y-values
selectionOptional cut string to apply to each entry

Definition at line 588 of file Plotting.h.

◆ ReZeroTGraph()

void ReZeroTGraph ( TGraph *  gr)

Shift all the graph y-values upwards such that there are no negative values and the minimum point is at zero.

Useful for fixing graphs of log-likelihood scans extracted from combine where the true minimum was not found correctly in the initial fit.

Definition at line 595 of file Plotting.h.

◆ CreateTransparentColor()

int CreateTransparentColor ( int  color,
float  alpha 
)

Create a transparent version of a colour.

Parameters
colorROOT colour code
alphaTransparency (0 = fully transparent, 1 = opaque)
Returns
The code for the new TColor

Definition at line 614 of file Plotting.h.

◆ SetupTwoPadSplitAsRatio() [1/2]

void SetupTwoPadSplitAsRatio ( std::vector< TPad * > const &  pads,
TH1 *  upper,
TH1 *  lower,
TString  y_title,
bool  y_centered,
float  y_min,
float  y_max 
)

Set a few style options for a two-pad setup used to show a data-MC comparison and ratio plot.

Use this version of the function if the axis histograms haven't been drawn yet. This function will:

  • remove the x-axis title and labels on the main plot
  • fix the axis tick-lengths to be consistent between both plots
  • enable x- and y-axis ticks on the ratio plot
  • Set the range and title options for the ratio plot y-axis
Parameters
padsVector of TPad pointers where [0] = upper pad, [1] = lower pad
upperPointer to the upper pad axis TH1
lowerPointer to the lower pad axis TH1
y_titleTitle for the y-axis of the ratio plot
y_centeredCentre (vertically) the y-axis title on the ratio plot.
y_minSet the y-axis minimum in the ratio plot
y_maxSet the y-axis maximum in the ratio plot

Definition at line 632 of file Plotting.h.

◆ SetupTwoPadSplitAsRatio() [2/2]

void SetupTwoPadSplitAsRatio ( std::vector< TPad * > const &  pads,
TString  y_title,
bool  y_centered,
float  y_min,
float  y_max 
)

Set a few style options for a two-pad setup used to show a data-MC comparison and ratio plot.

This function should be called after the axis histograms have been created in both pads (e.g. after some TH1, TGraph etc. has been drawn). It will:

  • remove the x-axis title and labels on the main plot
  • fix the axis tick-lengths to be consistent between both plots
  • enable x- and y-axis ticks on the ratio plot
  • Set the range and title options for the ratio plot y-axis
Parameters
padsVector of TPad pointers where [0] = upper pad, [1] = lower pad
y_titleTitle for the y-axis of the ratio plot
y_centeredCentre (vertically) the y-axis title on the ratio plot.
y_minSet the y-axis minimum in the ratio plot
y_maxSet the y-axis maximum in the ratio plot

Definition at line 623 of file Plotting.h.

◆ StandardAxes()

void StandardAxes ( TAxis *  xaxis,
TAxis *  yaxis,
TString  var,
TString  units 
)

Sets standard x- and y-axis titles with given units.

Will create an x-axis title of the form <variable> (<units>) and a y-axis title of the form Events / <bin_width> <units>.

Parameters
xaxisTarget x-axis
yaxisTarget y-axis
varString of the variable name
unitsString of the units, e.g. GeV. Leave empty if the variable is dimensionless

Definition at line 651 of file Plotting.h.

◆ UnitAxes()

void UnitAxes ( TAxis *  xaxis,
TAxis *  yaxis,
TString  var,
TString  units 
)

Sets standard x- and y-axis titles when histograms are drawn divided by bin width.

Will create an x-axis title of the form <variable> (<units>) and a y-axis title of the form dN/d<var> (1/<units>).

Parameters
xaxisTarget x-axis
yaxisTarget y-axis
varString of the variable name
unitsString of the units, e.g. GeV

Definition at line 664 of file Plotting.h.

◆ PositionedLegend()

TLegend * PositionedLegend ( double  width,
double  height,
int  pos,
double  offset 
)

Create a legend with fixed height, width and positioning.

Legend will be created in one three fixed positions:

  1. Top-left corner
  2. Top centered
  3. Top-right corner
  4. Bottom-left corner
  5. Bottom centered
  6. Bottom-right corner
Parameters
widthLegend width as a fraction of the pad
heightLegend height as a fraction of the pad
posPosition index (see above)
offsetPosition offset from edges of frame
Returns
Pointer to newly created TLegend

Definition at line 669 of file Plotting.h.

◆ DrawHorizontalLine()

TLine * DrawHorizontalLine ( TPad *  pad,
TLine *  line,
double  yval 
)

Use an existing TLine to draw a new horizontal line across the current frame.

Will find the TH1 used to draw the axes on the pad, get the min/max x-values and then draw a TLine at the specified y-value

Parameters
padTPad where the line should be drawn
lineExisting TLine already customised with the desired colour, width and style
yvalThe y-value (given in the axis units) where the line should be drawn
Returns
A pointer to the newly created TLine

Definition at line 705 of file Plotting.h.

◆ DrawTitle()

void DrawTitle ( TPad *  pad,
TString  text,
int  align 
)

Draw text in the top-margin region of a TPad.

Specified text will be latex-rendered in one of three positions:

  1. Left aligned with left side of frame
  2. Centre aligned along top of frame
  3. Right aligned with right side of fram

Note also that the text size will be set automatically to fill the top margin areas.

Parameters
padInput Tpad
textText to draw
alignAlignment option (see above)

Definition at line 713 of file Plotting.h.

◆ DrawCMSLogo() [1/2]

void DrawCMSLogo ( TPad *  pad,
TString  cmsText,
TString  extraText,
int  iPosX,
float  relPosX,
float  relPosY,
float  relExtraDY 
)

Draw the CMS logo and subtitle in the new style.

Adapted from the example here.

Position setting:

iPos=0  : out of frame
iPos=11 : top-left, left-aligned
iPos=33 : top-right, right-aligned
iPos=22 : center, centered

mode generally :

iPos = 10*(alignement 1/2/3) + position (1/2/3 = left/center/right)
Parameters
padThe target TPad
cmsTextUsually just "CMS"
extraTextThe subtitle, e.g. "Preliminary"
iPosXSets the position and alignment of the text (see above)
relPosXSets the x-position of the text relative to the frame
relPosYSets the y-position of the text relative to the frame
relExtraDYSets the position of the extraText relative to the cmsText

Definition at line 744 of file Plotting.h.

◆ DrawCMSLogo() [2/2]

void DrawCMSLogo ( TPad *  pad,
TString  cmsText,
TString  extraText,
int  iPosX 
)

Call DrawCMSLogo with some sensible defaults for the positioning parameters.

Parameters
padThe target TPad
cmsTextUsually just "CMS"
extraTextThe subtitle, e.g. "Preliminary"
iPosXSets the position and alignment of the text

Definition at line 837 of file Plotting.h.

◆ GetPadYMax() [1/2]

double GetPadYMax ( TPad *  pad,
double  x_min,
double  x_max 
)

Find the maximum value of all drawn objects in a given x-axis range.

Currently only objects derived from TH1 and TGraph are examined.

Parameters
padTPad to examine
x_minMinimum x-axis value to start the scan
x_maxMaximum x-axis alue to conclude the scan
Returns
Value on y-axis where the maximum is found

Definition at line 844 of file Plotting.h.

◆ GetPadYMax() [2/2]

double GetPadYMax ( TPad *  pad)

Call GetPadYMax(TPad*, double, double) with the full range of the given TPad.

Definition at line 877 of file Plotting.h.

◆ FixTopRange()

void FixTopRange ( TPad *  pad,
double  fix_y,
double  fraction 
)

Adjusts the y-axis maximum on the pad such that the specified y-value is positioned a fixed fraction from this new maximum.

Should work for both linear and log-scale y-axes. This function is most useful in conjunction with GetPadYMax() to supply the y-axis value to fix, thus ensuring that a fixed fraction at the top of the frame is free of any histogram or graph.

Parameters
padTPad to adjust
fix_yThe position on the y-axis that will be fixed
fractionThe fraction of the y-axis that fix_y should be positioned (measured from the top of the frame)

Definition at line 885 of file Plotting.h.

◆ FixBoxPadding()

void FixBoxPadding ( TPad *  pad,
TBox *  box,
double  frac 
)

Modify the pad y-axis range to ensure there is at least a given gap between a particular TBox and the highest drawn element in the x-axis range of this box.

Most useful after drawing a legend somewhere near the top of the frame, which may have overlapped with some of the histograms or graphs that had already been drawn. This function will check for the overlap, then adjust the y-axis range to ensure there is a gap. Note that if a sufficient (or larger) gap already exists, the y-axis range will not be adujusted.

Parameters
padTPad to modify
boxPointer to the reference TBox (e.g. could be a TLegend)
fracDesired fraction of the frame height between the bottom of the TBox and the top of the drawn objects.

Definition at line 902 of file Plotting.h.

◆ FixOverlay()

void FixOverlay ( )

Just re-draws the axes on the current TPad.

Definition at line 945 of file Plotting.h.