Exemple #1
0
def create_tree(fname, nentries=1000):
    """Create a file with a tree
    >>> create_tree ( 'file.root' ,  1000 ) 
    """

    import ROOT, random
    import ostap.io.root_file

    from array import array
    var0 = array('i', [0])
    var1 = array('d', [0])
    var2 = array('d', [0])
    var3 = array('d', [0])

    NV = 200
    evars = [array('d', [0]) for i in range(NV)]

    NM = 100
    mvars = [array('d', 4 * [0]) for i in range(NM)]

    from ostap.core.core import ROOTCWD

    with ROOTCWD(), ROOT.TFile.Open(fname, 'new') as root_file:

        root_file.cd()
        tree = ROOT.TTree('S', 'tree')
        tree.SetDirectory(root_file)
        tree.Branch('evt', var0, 'evt/I')
        tree.Branch('mass', var1, 'mass/D')
        tree.Branch('pt', var2, 'pt/D')
        tree.Branch('eta', var3, 'eta/D')

        for i in range(1, NV):
            tree.Branch("sv%d" % i, evars[i], 'sv%d/D' % i)

        for i in range(1, NM):
            tree.Branch("vv%d" % i, mvars[i], 'vv%d[4]/D' % i)

        for i in range(nentries):

            m = random.gauss(3.1, 0.015)
            pt = random.uniform(0, 8)
            eta = random.uniform(2, 4)

            var0[0] = i
            var1[0] = m
            var2[0] = pt
            var3[0] = eta

            for j in range(NV):
                evars[j] = j
            for j in range(NM):
                for k in range(4):
                    mvars[j][k] = 4 * j + k

            tree.Fill()

        root_file.Write()
def create_tree(fname, nentries=1000):
    """Create a file with a tree
    >>> create_tree ( 'file.root' ,  1000 ) 
    """

    import ROOT, random
    import ostap.io.root_file

    from array import array
    var1 = array('d', [0])
    var2 = array('d', [0])
    var3 = array('d', [0])

    from ostap.core.core import ROOTCWD

    with ROOTCWD(), ROOT.TFile.Open(fname, 'new') as root_file:
        root_file.cd()
        tree = ROOT.TTree('S', 'tree')
        tree.SetDirectory(root_file)
        tree.Branch('mass', var1, 'mass/D')
        tree.Branch('pt', var2, 'pt/D')
        tree.Branch('eta', var3, 'eta/D')

        for i in range(nentries):

            m = random.gauss(3.1, 0.015)
            pt = random.uniform(0, 10)
            eta = random.uniform(2, 5)

            var1[0] = m
            var2[0] = pt
            var3[0] = eta

            tree.Fill()

        root_file.Write()