ROOT
v6-36
Reference Guide
Loading...
Searching...
No Matches
ratioplot.py
Go to the documentation of this file.
1
## \file
2
## \ingroup tutorial_hist_legacy
3
## Display two histograms and their ratio.
4
##
5
## This program illustrates how to plot two histograms and their
6
## ratio on the same canvas. Original macro by Olivier Couet.
7
##
8
## \macro_code
9
##
10
## \author Michael Moran
11
12
from
ROOT
import
TCanvas, TColor, TGaxis, TH1F, TPad
13
from
ROOT
import
kBlack, kBlue, kRed
14
15
16
def
createH1
():
17
h1 =
TH1F
(
"h1"
, (
"Two gaussian plots and their ratio; x title; h1 and h2"
18
" histograms"
), 100, -5, 5)
19
h1.SetLineColor
(kBlue+1)
20
h1.SetLineWidth
(2)
21
h1.FillRandom
(
"gaus"
)
22
h1.GetYaxis
().SetTitleSize(20)
23
h1.GetYaxis
().SetTitleFont(43)
24
h1.GetYaxis
().SetTitleOffset(1.55)
25
h1.SetStats
(0)
26
return
h1
27
28
29
def
createH2
():
30
h2 =
TH1F
(
"h2"
,
"h2"
, 100, -5, 5)
31
h2.FillRandom
(
"gaus"
)
32
h2.SetLineColor
(kRed)
33
h2.SetLineWidth
(2)
34
return
h2
35
36
37
def
createRatio
(h1, h2):
38
h3 =
h1.Clone
(
"h3"
)
39
h3.SetLineColor
(kBlack)
40
h3.SetMarkerStyle
(21)
41
h3.SetTitle
(
""
)
42
h3.SetMinimum
(0.8)
43
h3.SetMaximum
(1.35)
44
# Set up plot for markers and errors
45
h3.Sumw2
()
46
h3.SetStats
(0)
47
h3.Divide
(h2)
48
49
# Adjust y-axis settings
50
y =
h3.GetYaxis
()
51
y.SetTitle
(
"ratio h1/h2 "
)
52
y.SetNdivisions
(505)
53
y.SetTitleSize
(20)
54
y.SetTitleFont
(43)
55
y.SetTitleOffset
(1.55)
56
y.SetLabelFont
(43)
57
y.SetLabelSize
(15)
58
59
# Adjust x-axis settings
60
x =
h3.GetXaxis
()
61
x.SetTitleSize
(20)
62
x.SetTitleFont
(43)
63
x.SetTitleOffset
(4.0)
64
x.SetLabelFont
(43)
65
x.SetLabelSize
(15)
66
67
return
h3
68
69
70
def
createCanvasPads
():
71
c =
TCanvas
(
"c"
,
"canvas"
, 800, 800)
72
# Upper histogram plot is pad1
73
pad1 =
TPad
(
"pad1"
,
"pad1"
, 0, 0.3, 1, 1.0)
74
pad1.SetBottomMargin
(0)
# joins upper and lower plot
75
pad1.SetGridx
()
76
pad1.Draw
()
77
# Lower ratio plot is pad2
78
c.cd
()
# returns to main canvas before defining pad2
79
pad2 =
TPad
(
"pad2"
,
"pad2"
, 0, 0.05, 1, 0.3)
80
pad2.SetTopMargin
(0)
# joins upper and lower plot
81
pad2.SetBottomMargin
(0.2)
82
pad2.SetGridx
()
83
pad2.Draw
()
84
85
return
c, pad1, pad2
86
87
88
def
ratioplot():
89
# create required parts
90
h1 =
createH1
()
91
h2 =
createH2
()
92
h3 =
createRatio
(h1, h2)
93
c, pad1, pad2 =
createCanvasPads
()
94
95
# draw everything
96
pad1.cd
()
97
h1.Draw
()
98
h2.Draw
(
"same"
)
99
# to avoid clipping the bottom zero, redraw a small axis
100
h1.GetYaxis
().SetLabelSize(0.0)
101
axis =
TGaxis
(-5, 20, -5, 220, 20, 220, 510,
""
)
102
axis.SetLabelFont
(43)
103
axis.SetLabelSize
(15)
104
axis.Draw
()
105
pad2.cd
()
106
h3.Draw
(
"ep"
)
107
108
# To hold window open when running from command line
109
# text = raw_input()
110
111
112
if
__name__ ==
"__main__"
:
113
ratioplot()
TRangeDynCast
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Definition
TCollection.h:358
ROOT::Detail::TRangeCast
Definition
TCollection.h:311
TCanvas
The Canvas class.
Definition
TCanvas.h:23
TGaxis
The axis painter class.
Definition
TGaxis.h:26
TH1F
1-D histogram with a float per channel (see TH1 documentation)
Definition
TH1.h:877
TPad
The most important graphics class in the ROOT system.
Definition
TPad.h:28
ratioplot
Definition
ratioplot.py:1
tutorials
legacy
hist
ratioplot.py
ROOT v6-36 - Reference Guide Generated on Thu Aug 21 2025 04:30:47 (GVA Time) using Doxygen 1.10.0