Ejemplo n.º 1
0
    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()
Ejemplo n.º 2
0
    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()
Ejemplo n.º 3
0
    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()
Ejemplo n.º 4
0
    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()
Ejemplo n.º 5
0
    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()
Ejemplo n.º 6
0
    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()
Ejemplo n.º 7
0
    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()
Ejemplo n.º 8
0
    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()