def test07_write_builtin(self): """Test writing of builtins""" from cppyy import gbl # bootstraps, only needed for tests from cppyy.gbl import TFile, TTree from cppyy.gbl.std import vector f = TFile(self.fname, "RECREATE") mytree = TTree(self.tname, self.title) mytree._python_owns = False import array mytree.ba = array.array('c', [chr(0)]) mytree.ia = array.array('i', [0]) mytree.da = array.array('d', [0.]) mytree.Branch("my_bool", mytree.ba, "my_bool/O") mytree.Branch("my_int", mytree.ia, "my_int/I") mytree.Branch("my_int2", mytree.ia, "my_int2/I") mytree.Branch("my_double", mytree.da, "my_double/D") for i in range(self.N): # make sure value is different from default (0) mytree.ba[0] = i % 2 and chr(0) or chr(1) mytree.ia[0] = i + 1 mytree.da[0] = (i + 1) / 2. mytree.Fill() f.Write() f.Close()
def test05_read_some_data_object(self): """Test reading of a complex data object""" from cppyy import gbl from cppyy.gbl import TFile f = TFile(self.fname) mytree = f.Get(self.tname) j = 1 for event in mytree: i = 0 assert len(event.data.get_floats()) == j * self.M for entry in event.data.get_floats(): assert i == int(entry) i += 1 k = 1 assert len(event.data.get_tuples()) == j for mytuple in event.data.get_tuples(): i = 0 assert len(mytuple) == k * self.M for entry in mytuple: assert i == int(entry) i += 1 k += 1 j += 1 assert j - 1 == self.N # f.Close()
def test01_write_stdvector(self): """Test writing of a single branched TTree with an std::vector<double>""" from cppyy import gbl # bootstraps, only needed for tests from cppyy.gbl import TFile, TTree from cppyy.gbl.std import vector f = TFile(self.fname, "RECREATE") mytree = TTree(self.tname, self.title) mytree._python_owns = False v = vector("double")() raises(TypeError, TTree.Branch, None, "mydata", v.__class__.__name__, v) raises(TypeError, TTree.Branch, v, "mydata", v.__class__.__name__, v) mytree.Branch("mydata", v.__class__.__name__, v) for i in range(self.N): for j in range(self.M): v.push_back(i * self.M + j) mytree.Fill() v.clear() f.Write() f.Close()
def test09_user_read_builtin(self): """Test user-directed reading of builtins""" from cppyy import gbl from cppyy.gbl import TFile f = TFile(self.fname) mytree = f.Get(self.tname) # note, this is an old, annoted tree from test08 for i in range(3, mytree.GetEntriesFast()): mytree.GetEntry(i) assert mytree.my_int == i + 1 assert mytree.my_int2 == i + 1 f.Close()
def test06_branch_activation(self): """Test of automatic branch activation""" from cppyy import gbl from cppyy.gbl import TFile, TTree from cppyy.gbl.std import vector L = 5 # writing f = TFile(self.fname, "RECREATE") mytree = TTree(self.tname, self.title) mytree._python_owns = False for i in range(L): v = vector("double")() mytree.Branch("mydata_%d" % i, v.__class__.__name__, v) mytree.__dict__["v_%d" % i] = v for i in range(self.N): for k in range(L): v = mytree.__dict__["v_%d" % k] for j in range(self.M): mytree.__dict__["v_%d" % k].push_back(i * self.M + j * L + k) mytree.Fill() for k in range(L): v = mytree.__dict__["v_%d" % k] v.clear() f.Write() f.Close() del mytree, f import gc gc.collect() # reading f = TFile(self.fname) mytree = f.Get(self.tname) # force (initial) disabling of all branches mytree.SetBranchStatus("*", 0) i = 0 for event in mytree: for k in range(L): j = 0 data = getattr(mytree, "mydata_%d" % k) assert len(data) == self.M for entry in data: assert entry == i * self.M + j * L + k j += 1 assert j == self.M i += 1 assert i == self.N f.Close()
def test03_read_stdvector(self): """Test reading of a single branched TTree with an std::vector<double>""" from cppyy import gbl from cppyy.gbl import TFile f = TFile(self.fname) mytree = f.Get(self.tname) i = 0 for event in mytree: assert len(event.mydata) == self.M for entry in event.mydata: assert i == int(entry) i += 1 assert i == self.N * self.M f.Close()
def test08_read_builtin(self): """Test reading of builtins""" from cppyy import gbl from cppyy.gbl import TFile f = TFile(self.fname) mytree = f.Get(self.tname) raises(AttributeError, getattr, mytree, "does_not_exist") i = 1 for event in mytree: assert event.my_bool == (i - 1) % 2 and 0 or 1 assert event.my_int == i assert event.my_double == i / 2. i += 1 assert (i - 1) == self.N f.Close()
def test04_write_some_data_object(self): """Test writing of a complex data object""" from cppyy import gbl from cppyy.gbl import TFile, TTree, IO from cppyy.gbl.IO import SomeDataObject f = TFile(self.fname, "RECREATE") mytree = TTree(self.tname, self.title) d = SomeDataObject() b = mytree.Branch("data", d) mytree._python_owns = False assert b for i in range(self.N): for j in range(self.M): d.add_float(i * self.M + j) d.add_tuple(d.get_floats()) mytree.Fill() f.Write() f.Close()