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