Legend not drawn from function in PyRoot

Hi,

I am having trouble drawing legend, legend does not get drawn if I put the Draw statement in a function, though it does draw if the Draw statement is outside.

A simple example is

from ROfrom ROOT import *
file=TFile("SingleTopNtuple/SingleTop.ntuple.TDR_012.root")
tree=file.Get("UserData")

histo=TH1F("histo","", 100, -5,5)
tree.Draw("elec_eta>>histo", "")
legend=TLegend(0.6,0.6,0.8,0.8)
legend.AddEntry(histo, histo.GetName(), "l")
legend.Draw()

This does what I expect it to do. Draw histogram and legend. Now the following

from ROfrom ROOT import *
file=TFile("SingleTopNtuple/SingleTop.ntuple.TDR_012.root")
tree=file.Get("UserData")

def fun():
  histo=TH1F("histo","", 100, -5,5)
  tree.Draw("elec_eta>>histo", "")
  legend=TLegend(0.6,0.6,0.8,0.8)
  legend.AddEntry(histo, histo.GetName(), "l")
  legend.Draw()

fun()

does not draw the legend, only the histogram.
A workaround is

from ROfrom ROOT import *
file=TFile("SingleTopNtuple/SingleTop.ntuple.TDR_012.root")
tree=file.Get("UserData")

def fun():
  histo=TH1F("histo","", 100, -5,5)
  tree.Draw("elec_eta>>histo", "")
  legend=TLegend(0.6,0.6,0.8,0.8)
  legend.AddEntry(histo, histo.GetName(), "l")
  return legend

leg=fun()
leg.Draw()

this works the same as the first example.

Why does the second not work?

Thank you for your help.

Cheers
Akira

Please post PyROOT related problems to the PyRoot Forum

Rene

Hi,

I’m sorry, I didn’t know there existed one. I have copied this one and the other one (Draw options ignored after TH1::Sumw2 (PyRoot) ) to PyRoot Forum. Please feel free to delete them. Sorry for the trouble.

Cheers
Akira