Filling the branches of a tree in PyROOT


I wrote a simple script in python where I tried to fill a tree branch with an array that has two entries, but only the first entry in filled into the tree branch. The code is the following:

import ROOT as root
import numpy as np
from array import array

file = root.TFile.Open("tree_tester.root", "RECREATE")

tree = root.TTree("tree","tree")

myvar = array('i')


tree.Branch('myvar', myvar, 'myvar/I')


So, how can I create and fill a tree branch with all the array elements?

Hi i solved this with:

myvar = array('d', [0])
tree.Branch('myvar', myvar, 'myvar/D')

for i in range(10):
    myvar[0] = float(i**2)

But I have another question, if a wanted to fill the tree entry with an array with like two entries, how could i do it?


You can proceed in a similar way, you create an array of size two and you define it as a branch.

You can find examples here:

If you look for the PyROOT box, it will tell you how you need to call Branch to achieve what you want.

1 Like