Home | News | Documentation | Download

Reading a branch of arrays in PyROOT

Dear experts,

I’m trying to read a TBranch that was created with something like the following lines:

L = ROOT.TTree('name','name')
v=array('i',[0])
L.Branch('fibre',v, 'fibre/I')
for i in range(48):
     v[0] = i
     L.Fill()

#The branch looks like this:
************************
*    Row   *     fibre *
************************
*        0 *         0 *
*        1 *         1 *
*        2 *         2 *
*        3 *         3 *
*        4 *         4 *
*        5 *         5 *
*        6 *         6 *
*        7 *         7 *
*        8 *         8 *
*        9 *         9 *
*       10 *        10 *
..................................

How can I read the elements on this branch? So far, I have tried something like:

ntuple = ROOT.TChain('L')
nEntries = ntuple.GetEntries()
for i in range (nEntries):
     x = ntuple.fibre[0]

And I get this TypeError: ‘int’ object is not subscriptable

Thanks for any help.
Chilufya

The code you provided cannot be run and seems a bit to small to understand what’s going on. Can you provide something more complete ? Then @pcanal can help you.

ntuple = ROOT.TChain('L')
nEntries = ntuple.GetEntries()
for i in range (nEntries):
     x = ntuple.fibre

Thanks a lot and apologies for the delayed response.

I already did something similar to what you have suggested. I did this:

import ROOT
import numpy as np
ntuple = ROOT.TChain(‘L’)
for event in ntuple:
x = np.asarray([ntuple.fibre])[0]