TLegend is not drawn

Hi,
I am trying to draw a pair of histograms and a legend and save the canvas to a file in a python script. The script is below:

[code]#! /usr/bin/env python

rootFileName = ‘Results/qcdEleId2/AnalysisOutput.root’

classifications = [‘goldenBarrel’,‘bigBremBarrel’,‘narrowBarrel’,‘showeringBarrel’,‘goldenEndcap’,‘bigBremEndcap’,‘narrowEndcap’,‘showeringEndcap’,‘crack’]

from ROOT import *

file = TFile(rootFileName)

#scale and format histogram
def prepHisto(histo,axisMin,axisMax):
histo.Scale(1.0/histo.GetEntries())
histo.GetXaxis().SetRangeUser(axisMin,axisMax)
histo.SetMarkerSize(0.4)
#add histograms for each class together
def addHistos(isolation,variable):
histos = []
for classification in classifications:
name = ‘Histograms/e_’+isolation+’’+classification+’’+variable
histo = file.Get(name)
histos.append(histo)
h = histos[0].Clone(isolation+variable)
for i in range(1,len(histos)):
h.Add(histos[i])
return h
#draw isolated and non isolated histograms with legend and save to file
def drawHisto(name,axisMin=-1,axisMax=-1):
c = TCanvas(name,name,600,600)
c.cd()
leg = TLegend(0.5,0.6,0.7,0.8)
hIso = addHistos(‘isolated’,name)
hNonIso = addHistos(‘nonIsolated’,name)
hIso.SetMarkerColor(kRed)
hNonIso.SetMarkerColor(kBlue)
hIso.SetLineColor(kRed)
hNonIso.SetLineColor(kBlue)
if (axisMin == -1) and (axisMax == -1):
axisMin = hIso.GetBinLowEdge(1)
axisMax = hIso.GetBinLowEdge(hIso.GetNbinsX()+1)
prepHisto(hIso,axisMin,axisMax)
prepHisto(hNonIso,axisMin,axisMax)
leg.AddEntry(hIso,‘Isolated’)
leg.AddEntry(hNonIso,‘Non-Isolated’)
leg.Draw()
hNonIso.Draw()
hIso.Draw(‘SAME’)
c.SaveAs(name+".png")

drawHisto(‘EoP’,0,10)[/code]

The histograms are drawn as expected but the legend is nowhere to be seen. I have had a look through the forums and tried doing SetOwnership(leg,0) inside drawHistos but still no luck. Anyone know what I am doing wrong?

Thanks
Nick

Nick,

just draw the legend after the histograms, instead of before: hNonIso.Draw() hIso.Draw('SAME') leg.Draw()
or at least, with that change it works for me …

HTH,
Wim

Yep. That works.

Thanks