Logo ROOT   6.14/05
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 
13 from ROOT import TCanvas, TColor, TGaxis, TH1F, TPad
14 from ROOT import kBlack, kBlue, kRed
15 
16 
17 def 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 
30 def 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 
38 def 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 
50  # Adjust y-axis settings
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 
60  # Adjust x-axis settings
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 
71 def createCanvasPads():
72  c = TCanvas("c", "canvas", 800, 800)
73  # Upper histogram plot is pad1
74  pad1 = TPad("pad1", "pad1", 0, 0.3, 1, 1.0)
75  pad1.SetBottomMargin(0) # joins upper and lower plot
76  pad1.SetGridx()
77  pad1.Draw()
78  # Lower ratio plot is pad2
79  c.cd() # returns to main canvas before defining pad2
80  pad2 = TPad("pad2", "pad2", 0, 0.05, 1, 0.3)
81  pad2.SetTopMargin(0) # joins upper and lower plot
82  pad2.SetBottomMargin(0.2)
83  pad2.SetGridx()
84  pad2.Draw()
85 
86  return c, pad1, pad2
87 
88 
89 def ratioplot():
90  # create required parts
91  h1 = createH1()
92  h2 = createH2()
93  h3 = createRatio(h1, h2)
94  c, pad1, pad2 = createCanvasPads()
95 
96  # draw everything
97  pad1.cd()
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()
106  pad2.cd()
107  h3.Draw("ep")
108 
109 # To hold window open when running from command line
110 # text = raw_input()
111 
112 
113 if __name__ == "__main__":
114  ratioplot()
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:567
The most important graphics class in the ROOT system.
Definition: TPad.h:29
The axis painter class.
Definition: TGaxis.h:24
The Canvas class.
Definition: TCanvas.h:31