pyROOT: cant draw TGraph in ratioplot

Hi!

I am drawing two TGraphs (which work), then I want to draw the ratio between the curves in another pad. I get the ratio by evaluating (graph.Eval(i)) the curves at a number of points and then taking the ratio and storing that into another TGraph. The ratio-TGraph however I cant draw.
Here is the code (pyROOT):

def graph4(graph_vec):

c1 = r.TCanvas(“c1”,“Graphs”,700,700)

c1.cd()

p1 = r.TPad(“p1”,“p1”,0.,0.35,1.,1.)
p1.Draw()
p1.SetBottomMargin(0.001)
p1.cd()
p1.SetGrid()

p1.SetLogy()

gr1 = graph_vec[0]
gr2 = graph_vec[1]

gr1.SetLineColor(4)
gr1.SetMarkerColor(1)
gr1.SetMarkerStyle(8)
gr1.SetMarkerSize(0.8)
gr1.SetTitle("")

gr1.SetTitle("")

gr1.GetXaxis().SetTitle(“Eff_HS”)
gr1.GetXaxis().CenterTitle()

gr1.GetYaxis().SetTitle(“EFF_PU”)
gr1.GetYaxis().CenterTitle()
gr1.GetYaxis().SetRangeUser(0,1)
gr1.GetYaxis().SetTitleOffset(1.5)
gr1.GetXaxis().SetTickSize(0.)

gr1.Draw(“AP”)

gr2.SetMarkerColor(r.kRed)
gr2.SetMarkerStyle(33)
gr2.SetMarkerSize(0.8)
gr2.Draw(“P”)

#TLegend *leg = new TLegend(0.15,0.75,0.5,0.85)

#leg.AddEntry(gr1,“QGSP_BIC_HP”,“lp”)
#leg.AddEntry(gr2,“QGSP_BERT_HP”,“lp”)
#leg.Draw()

c1.cd()
p2 = r.TPad(“p2”,“p3”,0.,0.05,1.,0.3)#xmin,ymin,xmax,ymax
p2.Draw()

p2.SetTopMargin(0.001)
p2.SetBottomMargin(0.3)

p2.SetGrid()

##Ratio ##
p2.cd()
x = []
rat_vec=[]
for i in np.arange(0, 1, 0.1):
#print ('i = ', i )
if (graph_vec[1].Eval(i)) == 0:
rat_vec.append(1)
#continue
else:
rat_vec.append((graph_vec[0].Eval(i))/(graph_vec[1].Eval(i)))
x.append(i)

gr_ratio = r.TGraph( len(x), np.asarray(x), np.asarray(rat_vec) )
#gr_ratio.GetXaxis().SetLabelSize(0.075)
#gr_ratio.GetYaxis().SetLabelSize(0.075)
gr_ratio.SetLineColor(4)
gr_ratio.SetMarkerColor(1)
gr_ratio.SetMarkerStyle(8)
gr_ratio.SetMarkerSize(0.8)
gr_ratio.SetTitle("")

gr_ratio.SetTitle("")

gr_ratio.GetXaxis().SetTitle(“Eff_HS”)
gr_ratio.GetXaxis().CenterTitle()
gr_ratio.GetYaxis().SetRangeUser(0,1)
gr_ratio.GetYaxis().SetTitleOffset(1.5)
gr_ratio.GetXaxis().SetTickSize(0.)

gr_ratio.Draw(“AL”)
gr1.Draw(“AL”)

return c1, p2 , p1

Then in main…
def main ( …)
.
.
canv_rat1, pad_rat1, pad_rat2 = graph4(graph_vec)
canv_rat1.SaveAs (args.output_dir + ‘/’ + outputname + ‘_graph4.png’)

Some further info. If I draw one of the inital graphs it shows up in the lower pad. If I try and draw the ratio in a separate canvas it does not show.

Any thoughts about what is going wrong?

Thanks a lot!

All best,

Tom

Hello,
I think we need @couet to have a first look here.

One thing:

The “A” in the second “AL” makes the first graph (gr_ratio) disappear from the pad/canvas. Try using only “L” for gr1.

1 Like

Hi,

thanks! However it did not seem to help. It does as well not work if I comment gr1.Draw() → #gr1.Draw().

Any thoughts?

In the code you posted I see only on canvas creation. Where is this “separate canvas” you are talking about ? can you post a running (small) example reproducing the problem ?

Hi, I actually solved the issue now. The problem was that I needed to define the TGraph outside of the function I made.

Thanks anyways!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.