Logo ROOT  
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
12from ROOT import TCanvas, TColor, TGaxis, TH1F, TPad
13from ROOT import kBlack, kBlue, kRed
14
15
16def 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)
21 h1.FillRandom("gaus")
22 h1.GetYaxis().SetTitleSize(20)
23 h1.GetYaxis().SetTitleFont(43)
24 h1.GetYaxis().SetTitleOffset(1.55)
26 return h1
27
28
29def createH2():
30 h2 = TH1F("h2", "h2", 100, -5, 5)
31 h2.FillRandom("gaus")
32 h2.SetLineColor(kRed)
34 return h2
35
36
37def createRatio(h1, h2):
38 h3 = h1.Clone("h3")
39 h3.SetLineColor(kBlack)
41 h3.SetTitle("")
42 h3.SetMinimum(0.8)
43 h3.SetMaximum(1.35)
44 # Set up plot for markers and errors
45 h3.Sumw2()
47 h3.Divide(h2)
48
49 # Adjust y-axis settings
50 y = h3.GetYaxis()
51 y.SetTitle("ratio h1/h2 ")
58
59 # Adjust x-axis settings
60 x = h3.GetXaxis()
66
67 return h3
68
69
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
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
83 pad2.Draw()
84
85 return c, pad1, pad2
86
87
88def 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, "")
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
112if __name__ == "__main__":
113 ratioplot()
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
The Canvas class.
Definition TCanvas.h:23
The axis painter class.
Definition TGaxis.h:26
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:877
The most important graphics class in the ROOT system.
Definition TPad.h:28