1

我喜欢使用 PyROOT 阅读 TTree 中分支的内容。我在这里寻找解决我的问题的可能方法:Reading a TTree in root using PyRoot。但是,答案对我没有帮助,因为 PyROOT 可能不知道分支的结构。请查看底部的输出片段,并提出解决方案。

谢谢,萨迪亚

>>> import ROOT
>>> f = ROOT.TFile("../Ntuple/v0406_default_1012p2_101X_dataRun2_Express_v7_Fill6573_Ntuple.root")
>>> f.ls()
    TFile**        ../Ntuples/v0406_default_1012p2_101X_dataRun2_Express_v7_Fill6573_Ntuple.root
    TFile*        ../Ntuples/v0406_default_1012p2_101X_dataRun2_Express_v7_Fill6573_Ntuple.root
    KEY: TTree    trajTree;1    Trajectory measurements in the Pixel detector.
    KEY: TTree    eventTree;1    The event.
    KEY: TTree    clustTree;1    Pixel clusters.
    KEY: TTree    trajROCEfficiencyTree;1    ROC and module efficiencies.
    >>> t = f.Get("trajTree")
    >>> t.Print()
    ******************************************************************************
    *Tree    :trajTree  : Trajectory measurements in the Pixel detector.         *
    *Entries : 42180482 : Total =     31858466801 bytes  File  Size = 8076610485 *
    *        :          : Tree compression factor =   3.94                              *
    ******************************************************************************
    *............................................................................*
    *Br    5 :clust_pix : pix[size][2]/F                                         *
    *Entries : 42180482 : Total  Size= 1597865089 bytes  File Size  =  569202378 *
    *Baskets :    12058 : Basket Size=    2175488 bytes  Compression=   2.81     *
    *............................................................................*
    *Br    7 :traj      : validhit/I:missing:lx/F:ly:lz:glx:gly:glz:clust_near/I:*
    *         | hit_near:pass_effcuts:alpha/F:beta:norm_charge:d_tr:dx_tr:dy_tr: *
    *         | d_cl:dx_cl:dy_cl:dx_hit:dy_hit:onedge/I:lx_err/F:ly_err/F        *
    *Entries :42180482 : Total  Size= 4220749516 bytes  File Size  = 2508894561 *
    *Baskets :    28411 : Basket Size=    2275840 bytes  Compression=   1.68     *
    *............................................................................*

>>> t.clust_pix
<read-write buffer ptr 0x7fba04428200, size 514 at 0x115e5ecf0>

>>> t.traj
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'TTree' object has no attribute 'traj'

>>> t.traj.beta
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'TTree' object has no attribute 'traj'
4

1 回答 1

1

在同事的帮助下自我回答:我定义了分支的结构,然后设置了它的地址。

结构数据文件为structDef.py

from ROOT import gROOT, AddressOf
gROOT.ProcessLine(
"struct Traj {\
  Int_t           validhit;\
  Float_t         missing;\
  Float_t         lx;\
  Float_t         ly;\
  Float_t         lz;\
  Float_t         glx;\
  Float_t         gly;\
  Float_t         glz;\
  Int_t           clust_near;\
  Float_t         hit_near;\
  Float_t         pass_effcuts;\
  Float_t         alpha;\
  Float_t         beta;\
  Float_t         norm_charge;\
  Float_t         d_tr;\
  Float_t         dx_tr;\
  Float_t         dy_tr;\
  Float_t         d_cl;\
  Float_t         dx_cl;\
  Float_t         dy_cl;\
  Float_t         dx_hit;\
  Float_t         dy_hit;\
  Int_t           onedge;\
  Float_t         lx_err;\
  Float_t         ly_err;\
  };"
); 

然后在我的主代码中,我设置了分支地址。

#!/usr/bin/env python
from ROOT import TFile, TTree
import structDef
from ROOT import Traj
traj = Traj()

f = TFile.Open('../Ntuples/v0406_default_1012p2_101X_dataRun2_Express_v7_Fill6573_Ntuple.root')
t1 = f.Get("trajTree")
t1.SetBranchAddress("traj", structDef.AddressOf(traj, 'validhit'))

for iev in xrange(t1.GetEntries()):
    t1.GetEntry(iev)
    print traj.norm_charge 

如果有人有更好的解决方案,那么我非常感谢您的帮助,因为我确实看到了警告,尽管它对我有用。

input_line_20:2:9: error: redefinition of 'Traj'
 struct Traj {      Int_t           validhit;      Float_t         missing;      Float_t         lx;      Float_t         ly;      Float_t         lz;      Float_t         glx;      Float_...
    ^
input_line_19:2:9: note: previous definition is here
 struct Traj {      Int_t           validhit;      Float_t         missing;      Float_t         lx;      Float_t         ly;      Float_t         lz;      Float_t         glx;      Float_...
    ^
17.0971317291

我喜欢 python,但是这个附加层使我的宏变得复杂。此外,我喜欢提示如何在 python 中有效地循环遍历树中的所有条目,就像在 C++ 中一样。可能更多的是ROOT与PyROOT的问题。目前,如果某些东西是用 C++ 编写的,我的宏需要双倍的时间。

于 2018-05-15T07:10:32.617 回答