Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
TRatioPlot.h
Go to the documentation of this file.
1// @(#)root/gpad:$Id$
2// Author: Paul Gessinger 25/08/2016
3
4/*************************************************************************
5 * Copyright (C) 1995-2024, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12#ifndef ROOT_TRatioPlot
13#define ROOT_TRatioPlot
14
15
16//////////////////////////////////////////////////////////////////////////
17// //
18// ROOT_TRatioPlot //
19// //
20// A collection of histograms //
21// //
22//////////////////////////////////////////////////////////////////////////
23
24#include "TObject.h"
25
26#include "TString.h"
27
28#include "TFitResultPtr.h"
29
30class TColorNumber;
31class TH1;
32class TPad;
33class TVirtualPad;
34class TGraph;
36class TGraphErrors;
37class TAxis;
38class TGaxis;
39class TLine;
40class TFitResult;
41class THStack;
42class TBrowser;
43
44class TRatioPlot : public TObject {
45
46private:
47 TRatioPlot &operator=(const TRatioPlot &) = delete;
48 TRatioPlot(const TRatioPlot &) = delete;
49
51 kDivideHist = 1, ///< Use `TH1::Divide` to create the ratio.
52 kDivideGraph = 2, ///< Use `TGraphAsymmErrors::Divide` to create the ratio.
53 kDifference = 3, ///< Calculate the difference between the histograms.
54 kFitResidual = 4, ///< Calculate the fit residual between the histogram and a fit stored within it.
55 kDifferenceSign = 5 ///< Calculate the difference divided by the error.
56 };
57
58 enum ErrorMode {
59 kErrorSymmetric = 1, ///< Use the regular `TH1::GetBinError` as the error
60 kErrorAsymmetric = 2, ///< Use `TH1::GetBinErrorUp` and `TH1::GetBinErrorLow` for the error, depending on y values.
61 kErrorFunc = 3 ///< Use the square root of the function value as the error.
62 };
63
65 kHideUp = 1, ///< Hide the first label of the upper y axis when there is low space.
66 kHideLow = 2, ///< Hide the last label of the lower y axis when there is low space.
67 kNoHide = 3, ///< Do not hide labels when there is low space.
68 kForceHideUp = 4, ///< Always hide the first label of the upper y axis
69 kForceHideLow = 5 ///< Always hide the last label of the lower y axis
70 };
71
72
73protected:
74
75 TVirtualPad *fParentPad = nullptr; ///< Stores the pad the ratio plot was created in
76 TPad *fUpperPad = nullptr; ///< The pad which contains the upper plot part
77 TPad *fLowerPad = nullptr; ///< The pad which contains the calculated lower plot part
78 TPad *fTopPad = nullptr; ///< The Pad that drawn on top on the others to have consistent coordinates
79
80 TH1 *fH1 = nullptr; ///< Stores the primary histogram
81 TH1 *fH2 = nullptr; ///< Stores the secondary histogram, if there is one
82 TObject *fHistDrawProxy = nullptr; ///< The object which is actually drawn, this might be TH1 or THStack
83 Bool_t fHistDrawProxyStack = kFALSE; ///< If stack was assigned as proxy
84
85 Int_t fMode = 0; ///< Stores which calculation is supposed to be performed as specified by user option
86 Int_t fErrorMode = ErrorMode::kErrorSymmetric; ///< Stores the error mode, sym, asym or func
87 TString fOption; ///< Stores the option which is given in the constructor as a string
88 TString fH1DrawOpt; ///< Stores draw option for h1 given in constructor
89 TString fH2DrawOpt; ///< Stores draw option for h2 given in constructor
90 TString fGraphDrawOpt; ///< Stores draw option for the lower plot graph given in constructor
91 TString fFitDrawOpt; ///< Stores draw option for the fit function in the fit residual case
92
93 Float_t fSplitFraction = 0.3; ///< Stores the fraction at which the upper and lower pads meet
94
95 TGraph *fRatioGraph = nullptr; ///< Stores the lower plot's graph
96 TGraphErrors *fConfidenceInterval1 = nullptr; ///< Stores the graph for the 1 sigma band
97 TGraphErrors *fConfidenceInterval2 = nullptr; ///< Stores the graph for the 2 sigma band
98 Color_t fCi1Color = kYellow; ///< Stores the color for the 1 sigma band
99 Color_t fCi2Color = kGreen; ///< Stores the color for the 2 sigma band
100
101 Bool_t fShowConfidenceIntervals = kTRUE; ///< Stores whether to show the confidence interval bands. From Draw option
102
103 Double_t fCl1 = 0.6827; ///< Stores the confidence level for the inner confidence interval band
104 Double_t fCl2 = 0.9545; ///< Stores the confidence level for the outer confidence interval band
105
106 Double_t fC1 = 1.; ///< Stores the scale factor for h1 (or THStack sum)
107 Double_t fC2 = 1.; ///< Stores the scale factor for h2
108
109 TFitResult *fFitResult = nullptr; ///< Stores the explicit fit result given in the fit residual case. Can be 0
110
111 TAxis *fSharedXAxis = nullptr; ///< X axis that stores the range for both plots
112 TGaxis *fUpperGXaxis = nullptr; ///< Upper graphical x axis
113 TGaxis *fLowerGXaxis = nullptr; ///< Lower graphical x axis
114 TGaxis *fUpperGYaxis = nullptr; ///< Upper graphical y axis
115 TGaxis *fLowerGYaxis = nullptr; ///< Lower graphical y axis
116 TGaxis *fUpperGXaxisMirror = nullptr; ///< Upper mirror of the x axis
117 TGaxis *fLowerGXaxisMirror = nullptr; ///< Lower mirror of the x axis
118 TGaxis *fUpperGYaxisMirror = nullptr; ///< Upper mirror of the y axis
119 TGaxis *fLowerGYaxisMirror = nullptr; ///< Lower mirror of the y axis
120
121 TAxis *fUpYaxis = nullptr; ///< Clone of the upper y axis
122 TAxis *fLowYaxis = nullptr; ///< Clone of the lower y axis
123
124 std::vector<TLine*> fGridlines; ///< Keeps TLine objects for the gridlines
125 std::vector<double> fGridlinePositions; ///< Stores the y positions for the gridlines
126 Bool_t fShowGridlines = kTRUE; ///< Stores whether to show the gridlines at all
127 Int_t fHideLabelMode = HideLabelMode::kHideLow; ///< Stores which label to hide if the margin is to narrow, if at all
128
129 // store margins to be able do determine
130 // what has changed when user drags
131 Float_t fUpTopMargin = 0.1; ///< Stores the top margin of the upper pad
132 Float_t fUpBottomMargin = 0.05; ///< Stores the bottom margin of the upper pad
133 Float_t fLowTopMargin = 0.05; ///< Stores the top margin of the lower pad
134 Float_t fLowBottomMargin = 0.3; ///< Stores the bottom margin of the lower pad
135
136 Float_t fLeftMargin = 0.1; ///< Stores the common left margin of both pads
137 Float_t fRightMargin = 0.1; ///< Stores the common right margin of both pads
138
140
141 Bool_t fIsUpdating = kFALSE; ///<! Keeps track of whether its currently updating to reject other calls until done
142
143 virtual void SyncAxesRanges();
144 virtual void SetupPads();
145 virtual void ConnectPadsSignals();
146 virtual void CreateVisualAxes();
147 virtual void UpdateVisualAxes();
153
154 void ImportAxisAttributes(TGaxis* gaxis, TAxis* axis);
155
156 virtual void Init(TH1* h1, TH1* h2, Option_t *option = "");
157
158public:
159
161 ~TRatioPlot() override;
162 TRatioPlot(TH1* h1, TH1* h2, Option_t *option = "pois");
163
164 TRatioPlot(THStack* st, TH1* h2, Option_t *option = "pois");
165
166 TRatioPlot(TH1* h1, THStack* st, Option_t *option = "pois");
167
168 TRatioPlot(TH1* h1, Option_t *option = "", TFitResult *fitres = nullptr);
169
174
176
177 void Draw(Option_t *chopt = "") override;
178
179 void Paint(Option_t *opt = "") override;
180
181 // Slots for signal receiving
182 void UnZoomed();
185
186 // Getters
187 TAxis *GetXaxis() const { return fSharedXAxis; }
188 TAxis *GetUpYaxis() const { return fUpYaxis; }
189 TAxis *GetLowYaxis() const { return fLowYaxis; }
190
191 virtual TObject *GetUpperRefObject() const;
194
195 virtual TGraph *GetLowerRefGraph() const;
198
199 ////////////////////////////////////////////////////////////////////////////////
200 /// Get the output of the calculation in the form of a graph. The type of
201 /// the return value depends on the input option that was given in the constructor.
202
204
205 ////////////////////////////////////////////////////////////////////////////////
206 /// Returns the graph for the 1 sigma confidence interval in the fit residual case
207
209
210 ////////////////////////////////////////////////////////////////////////////////
211 /// Returns the graph for the 2 sigma confidence interval in the fit residual case
212
214
215 TPad *GetUpperPad() const { return fUpperPad; }
216 TPad *GetLowerPad() const { return fLowerPad; }
217
218 // Setters
219
220 ////////////////////////////////////////////////////////////////////////////////
221 /// Explicitly specify the fit result that is to be used for fit residual calculation.
222 /// If it is not provided, the last fit registered in the global fitter is used.
223 /// The fit result can also be specified in the constructor.
224 ///
225 /// \param fitres The fit result coming from the fit function call
226
227 void SetFitResult(TFitResultPtr fitres) { fFitResult = fitres.Get(); }
228
229 // Setters for margins
234 void SetLeftMargin(Float_t margin);
236
241
242 virtual void SetGridlines(Double_t *gridlines, Int_t numGridlines);
243 virtual void SetGridlines(std::vector<double> gridlines);
244
247
248 void SetC1(Double_t c1) { fC1 = c1; }
249 void SetC2(Double_t c2) { fC2 = c2; }
250
251 ClassDefOverride(TRatioPlot, 2) //A ratio of histograms
252};
253
254#endif
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
bool Bool_t
Boolean (0=false, 1=true) (bool).
Definition RtypesCore.h:77
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
double Double_t
Double 8 bytes.
Definition RtypesCore.h:73
short Color_t
Color number (short).
Definition RtypesCore.h:99
float Float_t
Float 4 bytes (float).
Definition RtypesCore.h:71
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
const char Option_t
Option string (const char).
Definition RtypesCore.h:80
@ kGreen
Definition Rtypes.h:67
@ kYellow
Definition Rtypes.h:67
#define ClassDefOverride(name, id)
Definition Rtypes.h:348
Class to manage histogram axis.
Definition TAxis.h:32
Using a TBrowser one can browse all ROOT objects.
Definition TBrowser.h:37
Provides an indirection to the TFitResult class and with a semantics identical to a TFitResult pointe...
TFitResult * Get() const
Return contained pointer.
Extends the ROOT::Fit::Result class with a TNamed inheritance providing easy possibility for I/O.
Definition TFitResult.h:34
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:109
Use the TLine constructor to create a simple line.
Definition TLine.h:22
TObject()
TObject constructor.
Definition TObject.h:259
The most important graphics class in the ROOT system.
Definition TPad.h:28
void SubPadResized()
@ kForceHideUp
Always hide the first label of the upper y axis.
Definition TRatioPlot.h:68
@ kHideUp
Hide the first label of the upper y axis when there is low space.
Definition TRatioPlot.h:65
@ kNoHide
Do not hide labels when there is low space.
Definition TRatioPlot.h:67
@ kHideLow
Hide the last label of the lower y axis when there is low space.
Definition TRatioPlot.h:66
@ kForceHideLow
Always hide the last label of the lower y axis.
Definition TRatioPlot.h:69
TGraphErrors * fConfidenceInterval2
Stores the graph for the 2 sigma band.
Definition TRatioPlot.h:97
TAxis * GetLowerRefXaxis() const
TGraphErrors * GetConfidenceInterval2() const
Returns the graph for the 2 sigma confidence interval in the fit residual case.
Definition TRatioPlot.h:213
void SetConfidenceIntervalColors(TColorNumber ci1, TColorNumber ci2)
TRatioPlot(TH1 *h1, THStack *st, Option_t *option="pois")
TAxis * GetLowYaxis() const
Definition TRatioPlot.h:189
virtual void SetGridlines(Double_t *gridlines, Int_t numGridlines)
Int_t fErrorMode
Stores the error mode, sym, asym or func.
Definition TRatioPlot.h:86
TAxis * GetLowerRefYaxis() const
TGaxis * fUpperGXaxisMirror
Upper mirror of the x axis.
Definition TRatioPlot.h:116
TRatioPlot & operator=(const TRatioPlot &)=delete
TGaxis * fLowerGXaxisMirror
Lower mirror of the x axis.
Definition TRatioPlot.h:117
virtual Bool_t SyncPadMargins()
TPad * GetLowerPad() const
Definition TRatioPlot.h:216
TAxis * GetUpperRefYaxis() const
TRatioPlot(TH1 *h1, TH1 *h2, Option_t *option="pois")
TAxis * GetUpYaxis() const
Definition TRatioPlot.h:188
Float_t fLowBottomMargin
Stores the bottom margin of the lower pad.
Definition TRatioPlot.h:134
void CreateGridlines()
Int_t BuildLowerPlot()
void SetUpBottomMargin(Float_t margin)
Float_t fUpBottomMargin
Stores the bottom margin of the upper pad.
Definition TRatioPlot.h:132
TH1 * fH1
Stores the primary histogram.
Definition TRatioPlot.h:80
void SetH2DrawOpt(Option_t *opt)
virtual void CreateVisualAxes()
TFitResult * fFitResult
Stores the explicit fit result given in the fit residual case. Can be 0.
Definition TRatioPlot.h:109
virtual void Init(TH1 *h1, TH1 *h2, Option_t *option="")
Color_t fCi1Color
Stores the color for the 1 sigma band.
Definition TRatioPlot.h:98
virtual void SetupPads()
TGaxis * fUpperGYaxis
Upper graphical y axis.
Definition TRatioPlot.h:114
Double_t fC2
Stores the scale factor for h2.
Definition TRatioPlot.h:107
void UpdateGridlines()
virtual void ConnectPadsSignals()
void SetLowBottomMargin(Float_t margin)
void SetConfidenceIntervalColors(Color_t ci1=kYellow, Color_t ci2=kGreen)
Double_t fC1
Stores the scale factor for h1 (or THStack sum).
Definition TRatioPlot.h:106
Float_t fLowTopMargin
Stores the top margin of the lower pad.
Definition TRatioPlot.h:133
TString fH2DrawOpt
Stores draw option for h2 given in constructor.
Definition TRatioPlot.h:89
void SetUpTopMargin(Float_t margin)
TGaxis * fLowerGXaxis
Lower graphical x axis.
Definition TRatioPlot.h:113
void SetC2(Double_t c2)
Definition TRatioPlot.h:249
Bool_t fIsUpdating
! Keeps track of whether its currently updating to reject other calls until done
Definition TRatioPlot.h:141
virtual void SyncAxesRanges()
void SetGraphDrawOpt(Option_t *opt)
Double_t fCl1
Stores the confidence level for the inner confidence interval band.
Definition TRatioPlot.h:103
@ kErrorAsymmetric
Use TH1::GetBinErrorUp and TH1::GetBinErrorLow for the error, depending on y values.
Definition TRatioPlot.h:60
@ kErrorFunc
Use the square root of the function value as the error.
Definition TRatioPlot.h:61
@ kErrorSymmetric
Use the regular TH1::GetBinError as the error.
Definition TRatioPlot.h:59
Float_t GetSeparationMargin() const
void ImportAxisAttributes(TGaxis *gaxis, TAxis *axis)
void SetFitDrawOpt(Option_t *opt)
std::vector< TLine * > fGridlines
Keeps TLine objects for the gridlines.
Definition TRatioPlot.h:124
void SetPadMargins()
TPad * fLowerPad
The pad which contains the calculated lower plot part.
Definition TRatioPlot.h:77
TAxis * fSharedXAxis
X axis that stores the range for both plots.
Definition TRatioPlot.h:111
void SetFitResult(TFitResultPtr fitres)
Explicitly specify the fit result that is to be used for fit residual calculation.
Definition TRatioPlot.h:227
TString fFitDrawOpt
Stores draw option for the fit function in the fit residual case.
Definition TRatioPlot.h:91
TGaxis * fUpperGYaxisMirror
Upper mirror of the y axis.
Definition TRatioPlot.h:118
TGraphErrors * GetConfidenceInterval1() const
Returns the graph for the 1 sigma confidence interval in the fit residual case.
Definition TRatioPlot.h:208
TAxis * fUpYaxis
Clone of the upper y axis.
Definition TRatioPlot.h:121
TVirtualPad * fParentPad
Stores the pad the ratio plot was created in.
Definition TRatioPlot.h:75
Float_t fUpTopMargin
Stores the top margin of the upper pad.
Definition TRatioPlot.h:131
TGraph * GetCalculationOutputGraph() const
Get the output of the calculation in the form of a graph.
Definition TRatioPlot.h:203
void SetH1DrawOpt(Option_t *opt)
TGraph * fRatioGraph
Stores the lower plot's graph.
Definition TRatioPlot.h:95
TRatioPlot(const TRatioPlot &)=delete
Int_t fMode
Stores which calculation is supposed to be performed as specified by user option.
Definition TRatioPlot.h:85
void SetSplitFraction(Float_t sf)
TObject * fHistDrawProxy
The object which is actually drawn, this might be TH1 or THStack.
Definition TRatioPlot.h:82
virtual TObject * GetUpperRefObject() const
Float_t fRightMargin
Stores the common right margin of both pads.
Definition TRatioPlot.h:137
virtual void UpdateVisualAxes()
TGaxis * fLowerGYaxisMirror
Lower mirror of the y axis.
Definition TRatioPlot.h:119
Int_t fHideLabelMode
Stores which label to hide if the margin is to narrow, if at all.
Definition TRatioPlot.h:127
TRatioPlot(THStack *st, TH1 *h2, Option_t *option="pois")
void SetC1(Double_t c1)
Definition TRatioPlot.h:248
TPad * fTopPad
The Pad that drawn on top on the others to have consistent coordinates.
Definition TRatioPlot.h:78
void SetInsetWidth(Double_t width)
void RangeAxisChanged()
@ kDifference
Calculate the difference between the histograms.
Definition TRatioPlot.h:53
@ kDivideHist
Use TH1::Divide to create the ratio.
Definition TRatioPlot.h:51
@ kFitResidual
Calculate the fit residual between the histogram and a fit stored within it.
Definition TRatioPlot.h:54
@ kDifferenceSign
Calculate the difference divided by the error.
Definition TRatioPlot.h:55
@ kDivideGraph
Use TGraphAsymmErrors::Divide to create the ratio.
Definition TRatioPlot.h:52
void UnZoomed()
TH1 * fH2
Stores the secondary histogram, if there is one.
Definition TRatioPlot.h:81
Double_t fCl2
Stores the confidence level for the outer confidence interval band.
Definition TRatioPlot.h:104
TAxis * fLowYaxis
Clone of the lower y axis.
Definition TRatioPlot.h:122
TString fOption
Stores the option which is given in the constructor as a string.
Definition TRatioPlot.h:87
TPad * fUpperPad
The pad which contains the upper plot part.
Definition TRatioPlot.h:76
Bool_t fShowConfidenceIntervals
Stores whether to show the confidence interval bands. From Draw option.
Definition TRatioPlot.h:101
virtual void SetGridlines(std::vector< double > gridlines)
void SetLowTopMargin(Float_t margin)
void SetSeparationMargin(Float_t)
Bool_t fHistDrawProxyStack
If stack was assigned as proxy.
Definition TRatioPlot.h:83
Float_t fInsetWidth
Definition TRatioPlot.h:139
TAxis * GetXaxis() const
Definition TRatioPlot.h:187
void SetLeftMargin(Float_t margin)
Float_t fLeftMargin
Stores the common left margin of both pads.
Definition TRatioPlot.h:136
TAxis * GetUpperRefXaxis() const
Float_t fSplitFraction
Stores the fraction at which the upper and lower pads meet.
Definition TRatioPlot.h:93
void Paint(Option_t *opt="") override
This method must be overridden if a class wants to paint itself.
TGraphErrors * fConfidenceInterval1
Stores the graph for the 1 sigma band.
Definition TRatioPlot.h:96
TRatioPlot(TH1 *h1, Option_t *option="", TFitResult *fitres=nullptr)
TGaxis * fUpperGXaxis
Upper graphical x axis.
Definition TRatioPlot.h:112
virtual TGraph * GetLowerRefGraph() const
std::vector< double > fGridlinePositions
Stores the y positions for the gridlines.
Definition TRatioPlot.h:125
TString fH1DrawOpt
Stores draw option for h1 given in constructor.
Definition TRatioPlot.h:88
Bool_t fShowGridlines
Stores whether to show the gridlines at all.
Definition TRatioPlot.h:126
void Draw(Option_t *chopt="") override
Default Draw method for all objects.
TString fGraphDrawOpt
Stores draw option for the lower plot graph given in constructor.
Definition TRatioPlot.h:90
~TRatioPlot() override
Color_t fCi2Color
Stores the color for the 2 sigma band.
Definition TRatioPlot.h:99
void SetRightMargin(Float_t margin)
TGaxis * fLowerGYaxis
Lower graphical y axis.
Definition TRatioPlot.h:115
void SetConfidenceLevels(Double_t cl1, Double_t cl2)
TPad * GetUpperPad() const
Definition TRatioPlot.h:215
Basic string class.
Definition TString.h:138
TVirtualPad is an abstract base class for the Pad and Canvas classes.
Definition TVirtualPad.h:51
return c1
Definition legend1.C:41
TH1F * h1
Definition legend1.C:5
return c2
Definition legend2.C:14