#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... | |
std::vector< TPad * > OnePad | ( | ) |
Just creates a single pad filling the entire canvas.
Exists just to provide a similar output interface to TwoPadSplit()
Definition at line 459 of file Plotting.h.
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.
split_point | Fraction of the canvas that should be occuplied by the lower pad. |
gap_low | The top margin of the lower pad (with respect to the split point), given as a fraction of the input canvas height |
gap_high | The bottom margin of the upper pad (with respect to the split point), given as a fraction of the input canvas height |
gPad
) will be set to the upper one. Definition at line 468 of file Plotting.h.
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.
src | TH1 input |
xmin | adjust the minimum of the drawn range of the x-axis |
xmax | adjust the maximum of the drawn range of the x-axis (only if xmax > xmin ) |
Definition at line 485 of file Plotting.h.
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.
n | Number of histograms to make |
src | TH1 input |
xmin | adjust the minimum of the drawn range of the x-axis |
xmax | adjust the maximum of the drawn range of the x-axis (only if xmax > xmin ) |
Definition at line 494 of file Plotting.h.
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.
src | TGraph input |
at_limits | By 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 503 of file Plotting.h.
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.
n | Number of histograms to make |
src | TGraph input |
at_limits | By 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.
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.
pad | The TPad to search |
Definition at line 536 of file Plotting.h.
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.
num | Numerator histogram |
den | Denominator histogram |
num_err | Propagate errors on the numerator |
den_err | Propagate errors on the denominator |
Definition at line 567 of file Plotting.h.
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.
tree | Input TTree |
xvar | Branch or expression for the x-values |
yvar | Branch or expression for the y-values |
selection | Optional cut string to apply to each entry |
Definition at line 581 of file Plotting.h.
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.
tree | Input TTree |
xvar | Branch or expression for the x-values |
yvar | Branch or expression for the y-values |
zvar | Branch or expression for the y-values |
selection | Optional cut string to apply to each entry |
Definition at line 588 of file Plotting.h.
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.
int CreateTransparentColor | ( | int | color, |
float | alpha | ||
) |
Create a transparent version of a colour.
color | ROOT colour code |
alpha | Transparency (0 = fully transparent, 1 = opaque) |
Definition at line 614 of file Plotting.h.
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:
pads | Vector of TPad pointers where [0] = upper pad, [1] = lower pad |
upper | Pointer to the upper pad axis TH1 |
lower | Pointer to the lower pad axis TH1 |
y_title | Title for the y-axis of the ratio plot |
y_centered | Centre (vertically) the y-axis title on the ratio plot. |
y_min | Set the y-axis minimum in the ratio plot |
y_max | Set the y-axis maximum in the ratio plot |
Definition at line 632 of file Plotting.h.
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:
pads | Vector of TPad pointers where [0] = upper pad, [1] = lower pad |
y_title | Title for the y-axis of the ratio plot |
y_centered | Centre (vertically) the y-axis title on the ratio plot. |
y_min | Set the y-axis minimum in the ratio plot |
y_max | Set the y-axis maximum in the ratio plot |
Definition at line 623 of file Plotting.h.
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>
.
xaxis | Target x-axis |
yaxis | Target y-axis |
var | String of the variable name |
units | String of the units, e.g. GeV . Leave empty if the variable is dimensionless |
Definition at line 651 of file Plotting.h.
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>)
.
xaxis | Target x-axis |
yaxis | Target y-axis |
var | String of the variable name |
units | String of the units, e.g. GeV |
Definition at line 664 of file Plotting.h.
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:
width | Legend width as a fraction of the pad |
height | Legend height as a fraction of the pad |
pos | Position index (see above) |
offset | Position offset from edges of frame |
Definition at line 669 of file Plotting.h.
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
pad | TPad where the line should be drawn |
line | Existing TLine already customised with the desired colour, width and style |
yval | The y-value (given in the axis units) where the line should be drawn |
Definition at line 705 of file Plotting.h.
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:
Note also that the text size will be set automatically to fill the top margin areas.
pad | Input Tpad |
text | Text to draw |
align | Alignment option (see above) |
Definition at line 713 of file Plotting.h.
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)
pad | The target TPad |
cmsText | Usually just "CMS" |
extraText | The subtitle, e.g. "Preliminary" |
iPosX | Sets the position and alignment of the text (see above) |
relPosX | Sets the x-position of the text relative to the frame |
relPosY | Sets the y-position of the text relative to the frame |
relExtraDY | Sets the position of the extraText relative to the cmsText |
Definition at line 744 of file Plotting.h.
void DrawCMSLogo | ( | TPad * | pad, |
TString | cmsText, | ||
TString | extraText, | ||
int | iPosX | ||
) |
Call DrawCMSLogo with some sensible defaults for the positioning parameters.
pad | The target TPad |
cmsText | Usually just "CMS" |
extraText | The subtitle, e.g. "Preliminary" |
iPosX | Sets the position and alignment of the text |
Definition at line 837 of file Plotting.h.
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.
pad | TPad to examine |
x_min | Minimum x-axis value to start the scan |
x_max | Maximum x-axis alue to conclude the scan |
Definition at line 844 of file Plotting.h.
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.
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.
pad | TPad to adjust |
fix_y | The position on the y-axis that will be fixed |
fraction | The 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.
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.
pad | TPad to modify |
box | Pointer to the reference TBox (e.g. could be a TLegend) |
frac | Desired 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.
void FixOverlay | ( | ) |
Just re-draws the axes on the current TPad.
Definition at line 945 of file Plotting.h.