2

I am trying to read out values from a ROOT file using PyROOT and is stuck with this issue. Any help is appreciated.

import ROOT
rootFile = "file.root"

f = ROOT.TFile(rootFile,'read')
tree = f.Get('FCS_ParametrizationInput')
leaves = tree.GetListOfLeaves()

# define dynamically a python class containing root Leaves objects
class PyListOfLeaves(dict) :
    pass

# create an istance
pyl = PyListOfLeaves()

for i in range(0,leaves.GetEntries() ) :
    leaf = leaves.At(i)
    name = leaf.GetName()
    # add dynamically attribute to my class 
    pyl.__setattr__(name,leaf)

    if name == 'TruthPz':
        break


nev = tree.GetEntries()
for iev in range(0,nev) :
    tree.GetEntry(iev)
    # get values from the tree using Python class pyl which contains leaves
    # objects 
    px = pyl.TruthPx.GetValue()
    py = pyl.TruthPy.GetValue()
    pz = pyl.TruthPz.GetValue()

    print(px)
    if iev == 10:
        break

I based my code on this link. I think I am following everything as is given in that example. However, instead of it reading out momentum values as it is supposed to, I am getting a constant output of zeros. My tree structure is given as in the figure as shown by TBrowser.

enter image description here

Thanks!

Asen Christov
  • 848
  • 6
  • 21
Ananda
  • 2,925
  • 5
  • 22
  • 45
  • Your code works for me. Did you try a simple `tree.Draw("TruthPz")` to convince yourself the input file doesn't contain p_z = 0 entries for the first ten entries in the tree? Is `TruthPz` actually a floating point branch (or other "number") as opposed to an array branch? I don't recall how these look in the browser, but for verbosity could you post the output of `tree.Print('TruthP*')`? – pseyfert Aug 24 '18 at 12:52

0 Answers0