我正在尝试使用 PyROOT 从 ROOT 文件中读取值,但遇到了这个问题。任何帮助表示赞赏。
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
我的代码基于此链接。我想我正在遵循该示例中给出的所有内容。然而,我没有像预期的那样读出动量值,而是得到一个恒定的零输出。我的树结构如图所示,由 TBrowser 给出。
谢谢!