I am using pyroot to create a canvas and divide it into two TPads. On the top pad I plot multiple histograms, stored in histlist
. Then in the bottom pad, I plot the ratio of each histogram to the first histogram in histlist
. The code below will plot each histogram successfully, but it will only show the last histogram in the second TPad. That is, the first pad has four histograms in different colors, exactly as I want. But the second pad only shows one histogram, the last one plotted in the loop, whereas it should display 3. It seems that I am handling everything the same in each TPad. Any idea what I am doing wrong?
import ROOT
from ROOT import *
import numpy
c1 = ROOT.TCanvas()
c1.cd()
p12 = ROOT.TPad("pad1", "tall",0,0.165,1,1)
p22 = ROOT.TPad("pad2", "short",0,0.0,1.0,0.23)
p22.SetBottomMargin(0.35)
p12.Draw()
p22.Draw()
legend = TLegend(0.75,0.75,0.95,0.95)
colors = [1,2,8,4]
ii=0
for (hh,ll) in zip(histlist,labels):
p12.cd()
ROOT.gPad.SetLeftMargin(0.15)
hh.Scale(1/hh.Integral())
hh.SetStats(0)
hh.GetXaxis().SetTitle("")
hh.GetYaxis().SetTitle("Events")
hh.GetYaxis().SetTitleOffset(0.8)
hh.GetYaxis().SetTitleSize(0.05);
hh.GetXaxis().SetLabelSize(0)
hh.SetLineWidth(2)
hh.SetLineColor(colors[ii%len(colors)])
hh.SetDirectory(gROOT)
if ii==0:
hh.Draw("hist")
legend.AddEntry(hh, ll)
ii+= 1
continue
else:
hh.Draw("histsame")
legend.AddEntry(hh, ll)
ii += 1
legend.Draw("same")
ix = 0
for hx in histlist:
if (ix==0):
ix += 1
continue
p22.cd()
ROOT.gPad.SetLeftMargin(0.15)
myhist = hx.Clone()
myhist.Divide(histlist[0])
myhist.SetTitle("")
myhist.GetYaxis().SetTitle("Ratio")
myhist.GetXaxis().SetTitle("Diphoton Mass (GeV)")
myhist.SetLineColor(colors[ix%len(colors)])
myhist.GetYaxis().SetTitleOffset(0.2);
myhist.GetYaxis().SetTitleSize(0.18);
myhist.GetYaxis().SetNdivisions(4);
myhist.GetXaxis().SetTitleOffset(0.84);
myhist.GetXaxis().SetTitleSize(0.18);
myhist.GetXaxis().SetLabelSize(0.15);
myhist.GetYaxis().SetLabelSize(0.1);
myhist.SetStats(0)
myhist.SetDirectory(gROOT)
if(ix==1):
myhist.Draw("E0")
else:
myhist.Draw("E0SAME")
ix += 1
l1 = ROOT.TLine(0,1,15,1)
l1.SetLineColor(ROOT.kBlack)
l1.Draw("same")
c1.Print("plot.png")