PyRoot: read TClonesArray from TChain

Hi,

I have trouble reading a TClonesArray with TChain. See the following code. I created two files containing a TTree of TClonesArray. I then read them back. If I use TFile.Get() to get the TTree form one file, it works. However, if I use a TChain, it crashes.

*** Break *** segmentation violation
(no debugging symbols found)
Using host libthread_db library “/lib/tls/libthread_db.so.1”.
Attaching to program: /proc/1242/exe, process 1242
[Thread debugging using libthread_db enabled]

Traceback (most recent call last):
File “./testclonesarray.py”, line 61, in ?
read2((“testca1.root”,“testca2.root”))
File “./testclonesarray.py”, line 49, in read2
for ent in t:
File “/opt/exp_software/superb/cern/root/5.22/lib/ROOT.py”, line 212, in TTree__iter_
while self.GetEntry(i):
SystemError: problem in C++; program state has been reset

*** Break *** segmentation violation

Help is appreciated. I am new to PyRoot.

#!/usr/bin/env python

from ROOT import *

##
def write(fn):
    f= TFile(fn,"RECREATE")
    t= TTree("T","my ttree")

    rnd= TRandom3()
    par= TClonesArray("TParticle",100)
    t.Branch("Particles","TClonesArray",AddressOf(par), 128000,0)
    
    for i in xrange(10):
        for j in xrange(int(rnd.Gaus(5,2))):
            e= rnd.Gaus(1,0.1)
            p= TParticle(22,0,0,0,0,0,0,0,e,e,0,0,0,0)
            par[j]=p

        t.Fill()
        par.Clear()
        
    t.Write()
    f.Close()
####
def read1(fn):
    print "Read ",fn
    f= TFile(fn)
    t= f.Get("T")

    par= TClonesArray("TParticle",100)
    t.SetBranchAddress("Particles",par)
    
    for ent in t:
        print t.Particles.GetEntries()
        
####
def read2(fns):
    print "Read ",fns
    t= TChain("T")
    for f in fns:
        t.Add(f)

    t.GetListOfFiles().Print()

    par= TClonesArray("TParticle",100)
    t.SetBranchAddress("Particles",par)
    
    for ent in t:
        print t.Particles.GetEntries()
####

if __name__ == "__main__":
    write("testca1.root")
    write("testca2.root") 

    print "Test read using TFile.Get"
    read1("testca1.root")

    print "Test read using TChain"
    read2(("testca1.root","testca2.root"))

Chih-hsiang

Hi,

Can you try with:t.SetBranchAddress("Particles",AddressOf(par))?

Cheers,
Philippe

[quote=“pcanal”]Hi,

Can you try with:t.SetBranchAddress("Particles",AddressOf(par))?

Cheers,
Philippe[/quote]

Oh, that’s it! Thank you very much.