TTree in PyROOT?

Dear all,

I’m using the following code to create a TTree in a *.root file.

[code]import ROOT
from ROOT import *
import numpy

f = ROOT.TFile(“datatree.root”,“RECREATE”)

m = numpy.array([0.,0.,0.,0.,0.])
for part in [‘detector_A’,‘detector_B’,‘detector_C’,‘detector_D’]:
tree = ROOT.TTree(part,"%s PARTITION"%part)
for mod in range(1,65):
branch = tree.Branch(part+"_m%02i"%mod,m,“channel/D:time/D:constant/D:constant_db/D:deviation/D”)
tree.Write()
del tree
f.Close()
[/code]

Now, I should be able to fill the tree by doing the following.

f = ROOT.TFile("datatree.root","UPDATE") tree = f.Get("detector_A") m = numpy.array([2.,0.,2.,0.,2.]) branch = tree.Branch("detector_A_m10",m,"channel/D:time/D:constant/D:constant_db/D:deviation/D") branch.Fill() tree.Write() f.Write() f.Close()

However, it seems my “data” [2,0,2,0,2] is not stored in the tree, when I inspect it with the TBrowser. Why does it not work?

Any help is most welcomed,
Marco

Still not sure why the other code “fails”, but this piece of code seems to work (for future reference):

[code]import ROOT
import numpy as n
import numpy as m

f = ROOT.TFile(“treetest.root”, “recreate”)
t = ROOT.TTree(“detector_A”, “detector_A”)
n = n.zeros(5, dtype=float)
u = m.zeros(5, dtype=float)
t.Branch(‘detector_A_m30’, n, “channel/D:time/D:constant/D:constant_db/D:deviation/D”)
t.Branch(‘detector_A_m31’, u, “channel/D:time/D:constant/D:constant_db/D:deviation/D”)
channel = 1
time = 213232.
constant = 2.
constant_db = 3.
deviation = constant/constant_db - 1.
t.Fill()
f.Write()
f.Close()

f = ROOT.TFile(“treetest.root”, “update”)
t = ROOT.TTree(“detector_A”, “detector_A”)
t.Branch(‘TILECAL_LBA_m30’, n, “channel/D:time/D:constant/D:constant_db/D:deviation/D”)
t.Branch(‘TILECAL_LBA_m31’, u, “channel/D:time/D:constant/D:constant_db/D:deviation/D”)
channel = 1
time = 213232.
constant = 2.
constant_db = 3.
deviation = constant/constant_db - 1.
for i in xrange(100000):
n[0] = channelROOT.gRandom.Uniform()
n[1] = time
ROOT.gRandom.Uniform()
n[2] = constantROOT.gRandom.Uniform()
n[3] = constant_db
ROOT.gRandom.Uniform()
n[4] = deviationROOT.gRandom.Uniform()
u[0] = channel
ROOT.gRandom.Uniform()
u[1] = timeROOT.gRandom.Uniform()
u[2] = constant
ROOT.gRandom.Uniform()
u[3] = constant_dbROOT.gRandom.Uniform()
u[4] = deviation
ROOT.gRandom.Uniform()
t.Fill()
f.Write()
f.Close()[/code]

1 Like