Unable to create new Branch in PyRoot


Hello,

I am rather new to Root and have been so far trying to use it through the PyRoot interface.
My goal with this particular piece of code is to take the values from an existing TTree branch and modify them slightly, and then save this modified array as a new Branch.

My issue arises when I try to create the new Branch. I saw elsewhere that it is best to clone the original Tree before adding a branch so this is what I’m doing, but nevertheless I get an error when it comes to adding a new Branch. The error is as follows: “TypeError: can not resolve method template call for ‘Branch’”. Here is the section of code in question.

f = TFile('myfile.root')
myTree = f.Get('bigtree')

nentries = myTree.GetEntries()

evtCounter = 0
for entry in myTree:
    if evtCounter > 100000:
        continue

#read branches
nOfH = getattr(entry, "ahc_nHits")
biftime = getattr(entry,"bif_Time")

#create new variable to fill
bt=[]
for i in range(0,nentries):
    myTree.GetEntry(i)
    p = biftime[0]
    #stupid way to have it apply to all hits 
    for j in range(0,nOfH):
        bt.append(p)

#clone existing tree to add new branch
newTree = myTree.CloneTree(0)
newBranch = newTree.Branch("bt", bt, 'bt/F')

for i in range(nentries):
    newBranch.Fill()

It is specifically the line newBranch = newTree.Branch("bt", bt, 'bt/F') that it’s having an issue with. I’ve based my code off of previous examples on this forum, so I am really not sure what the issue is. Any ideas what I’m doing wrong here?

ROOT Version: 6.08/06
Platform: Mac OS Catalina
Compiler: PyRoot (Python 2.7.10)


Hi,
sorry for the high latency.

in newTree.Branch("bt", bt, 'bt/F'), bt is a Python list. I don’t think PyROOT knows how to deal with Python lists passed as template arguments of C++ functions.

If you want to create a TTree branch that contains multiple floats, you can use a ROOT.std.vector (a Python proxy to a C++ vector, that Branch knows how to deal with):

f = TFile('myfile.root')
myTree = f.Get('bigtree')

nentries = myTree.GetEntries()

evtCounter = 0
for entry in myTree:
    if evtCounter > 100000:
        continue

#read branches
nOfH = getattr(entry, "ahc_nHits")
biftime = getattr(entry,"bif_Time")

#create new variable to fill
bt=[]
for i in range(0,nentries):
    myTree.GetEntry(i)
    p = biftime[0]
    #stupid way to have it apply to all hits 
    for j in range(0,nOfH):
        bt.append(p)

#clone existing tree to add new branch
newTree = myTree.CloneTree(0)
newBranch = newTree.Branch("bt", bt, 'bt/F')

for i in range(nentries):
    newBranch.Fill()
1 Like