Problem with TTree::GetEntries() with a selection string - PyROOT


_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.

You can find a simple PyROOT macro foo.py and the test ROOT file to reproduce my problem at: https://cernbox.cern.ch/index.php/s/GR5kwJZK7EU4QQR

Strangely enough, you can observe that doing the same thing on a standard ROOT file (see class Foo() ) works as expected.

Any ideas of what could be going wrong with my file?

Thank you!!

Marco

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()

Running the code above, I get:

GetEntries(): 39458
GetEntries("nTracks >= 10"): 29642

Thanks!

Too easy, how I couldn’t think about that…

Out of curiosity tho, why did the class Foo() implementation work with SetDirectory(0)?

Not sure, probably because it was a small file with only one cluster to be read, which was read when the tree was created.

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