_ROOT Version: 6
_Platform: Scientific Linux release 6.9 (Carbon), MacOS X 10.12.6 Compiler: Not Provided
Hello,
I am experiencing some issues when calling GetEntires() with a selection on a TTree object stored as a member of a Python class, namely I am getting zero selected entries, even if a call to the same method from the ROOT interpreter on the same file does give non-zero entries.
The problem is with calling tree.SetDirectory(0). You are probably doing that because you saw a crash due to the file the tree is attached to being destroyed after the __init__ function of the class is finished. What you need is to change f = ... to self.f = ... in your class, and remove the call to self.tree.SetDirectory(0). That will make the file last as long as your class instance, and the tree will then be able to access its own data from the file.
import ROOT
class Bar:
def __init__(self):
self.f = ROOT.TFile.Open("./dstar-d0kpi-pi_ntup_100.root")
self.tree = self.f.Get("dstar")
def selectTree(self):
cutstr = "nTracks >= 10"
nentries_all = self.tree.GetEntries()
nentries_select = self.tree.GetEntries(cutstr)
print("GetEntries(): {}".format(nentries_all))
print("GetEntries(\"{}\"): {}".format(cutstr, nentries_select))
return
if __name__ == "__main__":
bar = Bar()
bar.selectTree()