Skip to content
Snippets Groups Projects
Commit df894b34 authored by Enric Tejedor Saavedra's avatar Enric Tejedor Saavedra
Browse files

[Exp PyROOT] Test that TChains are pythonically iterable

parent 61f358b8
No related branches found
No related tags found
No related merge requests found
...@@ -11,6 +11,9 @@ class TTreeIterable(unittest.TestCase): ...@@ -11,6 +11,9 @@ class TTreeIterable(unittest.TestCase):
For example, this allows to do: For example, this allows to do:
`for event in mytree:` `for event in mytree:`
`...` `...`
Since this pythonization is common to TTree and its subclasses, TChain is
also tested here.
""" """
filename = 'treeiterable.root' filename = 'treeiterable.root'
...@@ -31,49 +34,56 @@ class TTreeIterable(unittest.TestCase): ...@@ -31,49 +34,56 @@ class TTreeIterable(unittest.TestCase):
'RECREATE') 'RECREATE')
# Helper # Helper
def get_file_and_tree(self): def get_file_objects(self):
f = ROOT.TFile(self.filename) f = ROOT.TFile(self.filename)
t = f.Get(self.treename) t = f.Get(self.treename)
# Prevent double deletion of the tree (Python and C++ TFile) # Prevent double deletion of the tree (Python and C++ TFile)
SetOwnership(t, False) SetOwnership(t, False)
return f,t c = ROOT.TChain(self.treename)
c.Add(self.filename)
c.Add(self.filename)
return f,t,c
# Tests # Tests
def test_basic_type_branch(self): def test_basic_type_branch(self):
f,t = self.get_file_and_tree() f,t,c = self.get_file_objects()
n = array('f', [ 0. ]) for ds in t,c:
t.SetBranchAddress('floatb', n) n = array('f', [ 0. ])
ds.SetBranchAddress('floatb', n)
i = 0 i = 0
for entry in t: for entry in ds:
self.assertEqual(n[0], i+self.more) self.assertEqual(n[0], i+self.more)
i += 1 i = (i + 1) % self.nentries
def test_array_branch(self): def test_array_branch(self):
f,t = self.get_file_and_tree() f,t,c = self.get_file_objects()
a = array('d', self.arraysize*[ 0. ]) for ds in t,c:
t.SetBranchAddress('arrayb', a) a = array('d', self.arraysize*[ 0. ])
ds.SetBranchAddress('arrayb', a)
i = 0 i = 0
for entry in t: for entry in ds:
for j in range(self.arraysize): for j in range(self.arraysize):
self.assertEqual(a[j], i+j) self.assertEqual(a[j], i+j)
i += 1 i = (i + 1) % self.nentries
def test_struct_branch(self): def test_struct_branch(self):
f,t = self.get_file_and_tree() f,t,c = self.get_file_objects()
ms = ROOT.MyStruct() for ds in t,c:
t.SetBranchAddress('structleaflistb', ms) ms = ROOT.MyStruct()
ds.SetBranchAddress('structleaflistb', ms)
i = 0 i = 0
for entry in t: for entry in ds:
self.assertEqual(ms.myint1, i+self.more) self.assertEqual(ms.myint1, i+self.more)
self.assertEqual(ms.myint2, i*self.more) self.assertEqual(ms.myint2, i*self.more)
i += 1 i = (i + 1) % self.nentries
if __name__ == '__main__': if __name__ == '__main__':
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment