I would like to know how I could add a new branch to one of my TTree in a ROOT file using Python.
with root_open('MC_output.root', mode='a') as myfile:
new_column = numpy.array(gb_weights , dtype=[('weight', 'f8')])
root_numpy.array2tree(new_column , tree=myfile.TrainTree)
new_column_test = numpy.array(gb_weights_test , dtype=[('weight', 'f8')])
root_numpy.array2tree(new_column_test, tree=myfile.TestTree)
myfile.write()
myfile.close()
In this example, TrainTree and TestTree already exist in the ROOT file. I just want to add to them a new branch 'weight'. The problem I have here is that it will duplicate the trees, so in my file I have 2 TrainTree and 2 TestTree.
Do I have to use a temporary file to solve this issue? Or is there a better, simpler way to do it?
Thanks for your help!
Actually I could found out the solution by myself:
I was not using the correct method from numpy, I should have used the array2root instead of the array2tree.
new_column = numpy.array(gb_weights , dtype=[('weight', 'f8')])
root_numpy.array2root(new_column, 'MC_output.root' , 'TrainTree')
new_column_test = numpy.array(gb_weights_test , dtype=[('weight', 'f8')])
root_numpy.array2root(new_column_test, 'MC_output.root', 'TestTree')
Is the correct way to do it. Notice that in this case the file is not opened, and so its contain is not temporarily loaded.