TTree::Project within pyROOT

I am trying to use TTree::Project within pyROOT, and am having some issues.

My code is as follows:

import ROOT

numBins = 350
minE = 0.0
maxE = 3500.0

# Define the histograms
possible_bjets = ROOT.TH1D("possible_bjets", "Possible b-jets;Jet Energy (GeV);Number of jets", numBins, minE, maxE)
tagged_bjets = ROOT.TH1D("tagged_bjets", "Tagged b-jets;Jet Energy (GeV);Number of jets", numBins, minE, maxE)

possible_mistags = ROOT.TH1D("possible_mistags", "Possible Mistags;Jet Energy (GeV);Number of jets", numBins, minE, maxE)
mistagged_jets = ROOT.TH1D("mistagged_jets", "Mistagged Jets;Jet Energy (GeV);Number of jets", numBins, minE, maxE)

# Define the cuts
etaCut = ROOT.TCut("abs(jeteta) < 2.3")
decayCut = ROOT.TCut("BdecTrans > 25.7 && BdecTrans < 122.5")
bCut = ROOT.TCut("(abs(Bid) < 600 && abs(Bid) > 500) || (abs(Bid) < 6000 && abs(Bid) > 5000)")
notB = ROOT.TCut("!(%s)" % bCut)
hitCut = ROOT.TCut("max( (jethit2_cone1 - jethit1_cone1) / jethit1_cone1," \
                       " max( (jethit3_cone1 - jethit2_cone1) / jethit2_cone1," \
                       " (jethit4_cone1 - jethit3_cone1) / jethit3_cone1 ) ) >= 1");

evaluationFile = ROOT.TFile.Open("deep-learning-evaluation.root")

print "Number of possible b-jets: " + str(evaluationFile.Jets.GetEntries(str(etaCut + decayCut + bCut)))
print "Number of possible light-jets: " + str(evaluationFile.Jets.GetEntries(str(etaCut + notB)))

evaluationFile.Jets.Project("possible_bjets", "jetEnergy", str(etaCut + decayCut + bCut))
evaluationFile.Jets.Project("possible_mistags", "jetEnergy", str(etaCut + notB))

evaluationFile.Jets.Project("tagged_bjets", "jetEnergy", str(etaCut + decayCut + bCut + hitCut))
evaluationFile.Jets.Project("mistagged_jets", "jetEnergy", str(etaCut + notB + hitCut))

print "Number of entries for possible_bjets: " + str(possible_bjets.GetEffectiveEntries())
print "Number of entries for tagged_bjets: " + str(tagged_bjets.GetEffectiveEntries())
print "Number of entries for possible_mistags: " + str(possible_mistags.GetEffectiveEntries())
print "Number of entries for mistagged_jets: " + str(mistagged_jets.GetEffectiveEntries())

taggingEfficiency = ROOT.TEfficiency(tagged_bjets, possible_bjets)
mistaggingRate = ROOT.TEfficiency(mistagged_jets, possible_mistags)

canvas = ROOT.TCanvas("canvas", "Efficiency/Mistagging Canvas")
taggingEfficiency.SetLineColor(2)
taggingEfficiency.Draw("e1p")

mistaggingRate.SetLineColor(3)
mistaggingRate.Draw("SAME")

legend = ROOT.TLegend(0.7, 0.7, 0.9, 0.9)
legend.AddEntry(taggingEfficiency, "Efficiency of tagging #b-jets")
legend.AddEntry(mistaggingRate, "Mistagging Rate")
legend.Draw()

canvas.Show()

raw_input("Press enter to exit...")

However, my output goes something like:

Number of possible b-jets: 14223
Number of possible light-jets: 949104
Number of entries for possible_bjets: 0.0
Number of entries for tagged_bjets: 0.0
Number of entries for possible_mistags: 0.0
Number of entries for mistagged_jets: 0.0
Press enter to exit…

Which suggests to me that I am doing something wrong within TTree::Project, however, I am using it how I would use it within CINT or ACLiC and so I am not sure why it is misbehaving!

I am now not so sure that the issue is with TTree::Project, as whilst the number of entries was listed as zero, when filling the TH1Ds manually the graph still did not display.

Does anyone have any ideas? Maybe @couet @Danilo?

Does it work if you use it in CINT/Cling or AClic ?

(I have now double checked), it appears it doesn’t work in CINT or ACLiC either… Any ideas?

The issue is that Project does not find the histogram in the ROOT current directory.
Try something like

gROOT.cd() # make sure they are in gROOT
possible_bjets = ROOT.TH1D("possible_bjets", "Possible b-jets;Jet Energy (GeV);Number of jets", numBins, minE, maxE)
...
evaluationFile = ROOT.TFile.Open("deep-learning-evaluation.root") # This change the current directory
gROOT.cd() # make sure the histo's directory is the current directory.
...
evaluationFile.Jets.Project("possible_bjets", "jetEnergy", str(etaCut + decayCut + bCut))

Cheers,
Philippe.

Thanks, that fixed my issue! However, the efficiency graph still is not drawn; but no errors are presented?

This blocks ROOT from actual refreshing/displaying.

Cheers,
Philippe.

Without it though, the canvas opens and closes instantly?

You need to find a way to ‘peg’ the python reference to the canvas to avoid its deletion.

I am unsure how to do this (I’m unfamilar with python). My understanding was that the garbage collector would do this automatically without giving me a choice? I can run gc.disable at the beginning of my script, but I don’t think this will help?

yes python automatically exit when you do:

$ python graph.py

graph.py is one of the example in $ROOTSYS/tutorials/pyroot

to behave like ROOT interactive with a prompt. You can do:

$ python
Python 2.7.10 (v2.7.10:15c95b7d81dc, May 23 2015, 09:33:12)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import ROOT
>>> execfile("graph.py")
 i 0 0.000000 1.986693
 i 1 0.100000 2.955202
 i 2 0.200000 3.894183
 i 3 0.300000 4.794255
 i 4 0.400000 5.646425
 i 5 0.500000 6.442177
 i 6 0.600000 7.173561
 i 7 0.700000 7.833269
 i 8 0.800000 8.414710
 i 9 0.900000 8.912074
 i 10 1.000000 9.320391
 i 11 1.100000 9.635582
 i 12 1.200000 9.854497
 i 13 1.300000 9.974950
 i 14 1.400000 9.995736
 i 15 1.500000 9.916648
 i 16 1.600000 9.738476
 i 17 1.700000 9.463001
 i 18 1.800000 9.092974
 i 19 1.900000 8.632094
>>>

The TCanvas stays open .

It does, but still no graph has been drawn! :frowning:

can you try:

taggingEfficiency.SetLineColor(2)
taggingEfficiency.Draw("Ae1p")

mistaggingRate.SetLineColor(3)
mistaggingRate.Draw("L")

I still get a blank graph; the Legend is displayed, but no graph.

Are the axis displayed ?
Do you get some error message ?

Also try:

taggingEfficiency.SetMarkerColor(2);
taggingEfficiency.SetMarkerStyle(20);

No, the axes are not displayed either, but I get no error message!

Have you try the marker attributes changes I suggested ?

Yes, I used those and it didn’t make a difference!

Thanks for your persistence and help so far!

To help you further I would need a script I can run. The one you posted refers to some ROOT file I do not have.

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