Please fill also the fields below. Note that root -b -q
will tell you this info, and starting from 6.28/06 upwards, you can call .forum bug
from the ROOT prompt to pre-populate a topic.
ROOT Version: 6.24/08
Platform: Linux espyroot 3.10.0-1160.62.1.el7.x86_64
Compiler: c++
Hello everyone, I have a problem when using the TEfficiency class. Specifically, this is my script:
#This is a pyroot scripts that reads the "mc_410000.ttbar_lep.exactly2lep.filtered.root" file we used above,
#applies a selection requiring at least one jet in the event with pT>50000 MeV
#and requires that the leading lepton (i.e. the one with the highest pt) passes TightID requirements (i.e. its variable lep_isTightID is 1).
#Produce a plot of the leading lepton pt, one of the leading jet pt, and use a 2D plot to study if these two variables are correlated.
#Write in output a tree containing just the leading lepton pt and type, and the jet pt and trueflavor for all the jets in the event.
#Also, the script must calculate efficiencies vs pt for the isTightID requirement, separetely for electrons and muons.
from ROOT import TEfficiency, TCanvas, TFile, TTree, TH1F, TH2F
from array import array
NMAXEVENTS=1e4
ttbarFile = TFile.Open("mc_410000.ttbar_lep.exactly2lep.filtered.root","READ")
ttbarTree = ttbarFile.Get("mini")
entries=ttbarTree.GetEntries()
print("from file %s read tree %s with entries %d" %(ttbarFile.GetName(),ttbarTree.GetName(),entries))
outfile = TFile.Open("output_pyroot1.root","RECREATE")
outtree = TTree("microTree","microTree")
outtree.SetDirectory(outfile)
mcWeight = array('f', [0.])
leading_lep_pt = array('f', [0.])
leading_lep_type = array('f', [0.])
jet_pt = array('f', [0.])
jet_trueflav = array('f', [0.])
outtree.Branch("mcWeight",mcWeight,"mcWeight/F")
outtree.Branch("leading_lep_pt",leading_lep_pt,"leading_lep_pt/F")
outtree.Branch("leading_lep_type",leading_lep_type,"leading_lep_type/F")
outtree.Branch("jet_pt",jet_pt,"jet_pt/F")
outtree.Branch("jet_trueflav",jet_trueflav,"jet_trueflav/F")
histos = {}
histos["leading_lep_pt"] = TH1F("leading_lep_pt","Leading lepton p_{T} ; lep p_{T} [GeV] ; entries", 40 , 0, 200)
histos["leading_jet_pt"] = TH1F("leading_jet_pt","Leading jet p_{T} ; jet p_{T} [GeV] ; entries", 40 , 0, 200)
histos["jet_vs_lep_pt"] = TH2F("jet_vs_lep_pt","jet p_{T} vs lepton p_{T} ; jet p_{T} [GeV] ; lep p_{T} ;entries", 20 , 0, 200, 20, 0, 200)
muon_eff_vs_pt = TEfficiency("muon_eff_vs_pt","muon efficiency vs p_{T}; p_{T} [GeV]; #epsilon_{#mu}",40,50,200)
elec_eff_vs_pt = TEfficiency("elec_eff_vs_pt","electron efficiency vs p_{T}; p_{T} [GeV] ; #epsilon_{e}",40,50,200)
for key, value in histos.items():
value.Sumw2()
entry = 0
for e in ttbarTree:
entry+=1
if((entry%10000)==0):
print("reading entry %d out of %d" % (entry,entries))
if(NMAXEVENTS>=0 and entry>NMAXEVENTS):
break
for i in range(0,len(e.jet_pt)):
mcWeight[0]=e.mcWeight
leading_lep_pt[0]=e.lep_pt[0]
leading_lep_type[0]=e.lep_type[0]
jet_pt[0]=e.jet_pt[i]
jet_trueflav[0]=e.jet_trueflav[i]
outtree.Fill()
w=e.mcWeight
if(e.jet_n==0):
continue
if(e.jet_pt[0]<50000):
continue
if(e.lep_isTightID[0]==0):
continue
if(e.lep_type[0]==13 or e.lep_type[0]==-13):
muon_eff_vs_pt.Fill(e.lep_isTightID[0],e.lep_pt[0])
#print(f"{e.lep_isTightID[0]} \t {e.lep_pt[0]}")
elif(e.lep_type[0]==11 or e.lep_type[0]==-11):
elec_eff_vs_pt.Fill(e.lep_isTightID[0],e.lep_pt[0])
histos["leading_lep_pt"].Fill(e.lep_pt[0]/1000.,w)
histos["leading_jet_pt"].Fill(e.jet_pt[0]/1000.,w)
histos["jet_vs_lep_pt"].Fill(e.jet_pt[0]/1000.,e.lep_pt[0]/1000.,w)
##end of loop on entries
for key, value in histos.items():
print("writing %s histogram to file %s" % (value.GetName(),outfile.GetName()))
outfile.WriteTObject(value)
c1 = TCanvas()
histos["leading_lep_pt"].Draw()
c1.Draw()
c2 = TCanvas()
histos["leading_jet_pt"].Draw()
c2.Draw()
c3 = TCanvas()
histos["jet_vs_lep_pt"].Draw("colz")
c3.Draw()
c4=TCanvas()
muon_eff_vs_pt.Draw("AP")
c4.Draw()
c4.SaveAs("muon_eff_vs_pt.png")
c5=TCanvas()
elec_eff_vs_pt.Draw("AP")
c5.Draw()
c5.SaveAs("elec_eff_vs_pt.png")
#print("closing input file %s" % ttbarFile.GetName())
#ttbarFile.Close()
#outfile.Write()
#print("output signal written to file %s" % outfile.GetName())
#outfile.Close()
where at the beginning there is a description of what the script should run. Then there some variables read from the tree, such as lep_isTightID[0], which is the first component of a boolean vector.
The problem consists of the following: the script run with no errors (apparently), however the TEfficiency plots are totally blank, even without the histogram title (if I added “muon_eff_vs_pt = … #epsilon_{#mu}”,40,50,200**,40,50,200**)" then the histogram title would appear). I don’t know how to solve this issue and finally have something filled in the histogram.
Thank you in advance to anyone who helps me. For any question, you can freely ask me.
Daniele