Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 39 additions & 48 deletions bindings/pyroot/pythonizations/test/ttree_iterable.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class TTreeIterable(unittest.TestCase):
"""
Test for the pythonization that makes TTree instances iterable in Python.
Test for the pythonization that makes TTree instances iterable in Python.
For example, this allows to do:
`for event in mytree:`
`...`
Expand All @@ -15,28 +15,19 @@ class TTreeIterable(unittest.TestCase):
and TNtupleD are also tested here.
"""

filename = 'treeiterable.root'
treename = 'mytree'
tuplename = 'mytuple'
nentries = 10
filename = "treeiterable.root"
treename = "mytree"
tuplename = "mytuple"
nentries = 10
arraysize = 10
more = 10
more = 10

# Setup
@classmethod
def setUpClass(cls):
ROOT.gInterpreter.Declare('#include "TreeHelper.h"')
ROOT.CreateTTree(cls.filename,
cls.treename,
cls.nentries,
cls.arraysize,
cls.more,
'RECREATE')
ROOT.CreateTNtuple(cls.filename,
cls.tuplename,
cls.nentries,
cls.more,
'UPDATE')
ROOT.gInterpreter.Declare('#include "TreeHelper.h"')
ROOT.CreateTTree(cls.filename, cls.treename, cls.nentries, cls.arraysize, cls.more, "RECREATE")
ROOT.CreateTNtuple(cls.filename, cls.tuplename, cls.nentries, cls.more, "UPDATE")

# Helpers
def get_tree_and_chain(self):
Expand All @@ -49,77 +40,77 @@ def get_tree_and_chain(self):
c.Add(self.filename)
c.Add(self.filename)

return f,t,c
return f, t, c

def get_ntuples(self):
f = ROOT.TFile(self.filename)

nt = f.Get(self.tuplename)
ROOT.SetOwnership(nt, False)

ntd = f.Get(self.tuplename + 'D')
ntd = f.Get(self.tuplename + "D")
ROOT.SetOwnership(ntd, False)

return f,nt,ntd
return f, nt, ntd

# Tests
def test_basic_type_branch(self):
f,t,c = self.get_tree_and_chain()
f, t, c = self.get_tree_and_chain()

for ds in t,c:
n = array('f', [ 0. ])
ds.SetBranchAddress('floatb', n)
for ds in t, c:
n = array("f", [0.0])
ds.SetBranchAddress("floatb", n)

i = 0
for entry in ds:
self.assertEqual(n[0], i+self.more)
for _ in ds:
self.assertEqual(n[0], i + self.more)
i = (i + 1) % self.nentries

def test_array_branch(self):
f,t,c = self.get_tree_and_chain()
f, t, c = self.get_tree_and_chain()

for ds in t,c:
a = array('d', self.arraysize*[ 0. ])
ds.SetBranchAddress('arrayb', a)
for ds in t, c:
a = array("d", self.arraysize * [0.0])
ds.SetBranchAddress("arrayb", a)

i = 0
for entry in ds:
for _ in ds:
for j in range(self.arraysize):
self.assertEqual(a[j], i+j)
self.assertEqual(a[j], i + j)
i = (i + 1) % self.nentries

def test_struct_branch(self):
f,t,c = self.get_tree_and_chain()
f, t, c = self.get_tree_and_chain()

for ds in t,c:
for ds in t, c:
ms = ROOT.MyStruct()
ds.SetBranchAddress('structleaflistb', ms)
ds.SetBranchAddress("structleaflistb", ms)

i = 0
for entry in ds:
self.assertEqual(ms.myint1, i+self.more)
self.assertEqual(ms.myint2, i*self.more)
for _ in ds:
self.assertEqual(ms.myint1, i + self.more)
self.assertEqual(ms.myint2, i * self.more)
i = (i + 1) % self.nentries

def test_ntuples(self):
f,nt,ntd = self.get_ntuples()
f, nt, ntd = self.get_ntuples()

colnames = ['x','y','z']
cols = [ array('f', [ 0. ]) for _ in colnames ]
colsd = [ array('d', [ 0. ]) for _ in colnames ]
colnames = ["x", "y", "z"]
cols = [array("f", [0.0]) for _ in colnames]
colsd = [array("d", [0.0]) for _ in colnames]
ncols = len(cols)

for ds,cs in [(nt,cols),(ntd,colsd)]:
for ds, cs in [(nt, cols), (ntd, colsd)]:
for i in range(ncols):
ds.SetBranchAddress(colnames[i], cs[i])

numentry = 0
for entry in ds:

for _ in ds:
for i in range(ncols):
self.assertEqual(cs[i][0], numentry + i*self.more)
self.assertEqual(cs[i][0], numentry + i * self.more)
numentry += 1


if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()
Loading