Reading multiple trees from file and storing them in TList

I would like to loop over several files, store a tree from each file in a TList, and merge the trees in a TList into a single tree.

I’ve tried to follow the suggestions here
root.cern.ch/phpBB2/viewtopic.ph … 9c7a5ba777
The only way I could get my code to work was to use

gROOT.cd()
treeClone = tree.CloneTree()

which is very slow since it’s loading thee trees into RAM. Is there a better way to do this? In the code below you can see the methods which don’t work in addition to the one that does.

from ROOT import TTree, TFile, gROOT, TList

fileNameList = ["f1.root", "f2.root", "f3,root"]

# List to store trees
treeList = TList()

# Loop over files and add them to the list
############# Does not work ########
#for f in fileNameList:
#    file = TFile(f)
#    tree = file.Get("TopTree")
#    treeList.Add(tree)
#####################################

############ Does not work ###########
#for f in fileNameList:
#    file = TFile(f)
#    tree = file.Get("TopTree")
#    treeClone = tree.CloneTree(0)
#    treeClone.SetDirectory(0)
#    treeList.Add(treeClone)
########################################

################ Does work ###########
for f in fileNameList:
    file = TFile(f)
    tree = file.Get("TopTree")
    gROOT.cd()
    treeClone = tree.CloneTree()
    treeList.Add(treeClone)
########################################

# Merge trees
treeMerged = TTree.MergeTrees(treeList)

Hi,

Is there a reason to not simply chain the trees using TChain?

Cheers,
Charles

[quote=“cplager”]Hi,

Is there a reason to not simply chain the trees using TChain?

Cheers,
Charles[/quote]

I need a single tree as input to a multivariate analysis program. I want to combine trees from all signal files into a single tree and trees from all background files into a single tree.

Thanks,

Pat

[quote=“pdryan”][quote=“cplager”]Hi,

Is there a reason to not simply chain the trees using TChain?

Cheers,
Charles[/quote]

I need a single tree as input to a multivariate analysis program. I want to combine trees from all signal files into a single tree and trees from all background files into a single tree.

Thanks,

Pat[/quote]

To me that suggests that you want two chains, one for all of the input files and another for all of the background files, right?

Cheers, Charles

[quote]I need a single tree as input to a multivariate analysis program. I want to combine trees from all signal files into a single tree and trees from all background files into a single tree.[/quote]To the multivariate analysis a TChain object should look like a single TTree object.

[quote]which is very slow since it’s loading thee trees into RAM. Is there a better way to do this? In the code below you can see the methods which don’t work in addition to the one that does. [/quote]Use:

Cheers,
Philippe.

Thanks for the help! The following causes the program to crash (see output below).

Here is the info on my python version:
Python 2.5 (r25:51908, Oct 18 2007, 16:19:30)
[GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-59)] on linux2

I am using root version 5.24

[pryan@jever TreeManipulation]$ python tmp.py

 *** Break *** segmentation violation
Using host libthread_db library "/lib/tls/libthread_db.so.1".
Attaching to program: /proc/10919/exe, process 10919
[Thread debugging using libthread_db enabled]
[New Thread -1208121664 (LWP 10919)]
[New Thread -1212408928 (LWP 10928)]
0x006717a2 in _dl_sysinfo_int80 () from /lib/ld-linux.so.2
Thread 2 (Thread -1212408928 (LWP 10928)):
#0  0x006717a2 in _dl_sysinfo_int80 () from /lib/ld-linux.so.2
#1  0x008fdc1f in sem_wait@GLIBC_2.0 () from /lib/tls/libpthread.so.0
#2  0x00c18e13 in PyThread_acquire_lock (lock=0x9e1f728, waitflag=1) at Python/thread_pthread.h:334
#3  0x00bea9b0 in PyEval_AcquireThread (tstate=0xa4e22f8) at Python/ceval.c:251
#4  0x00c1e969 in t_bootstrap (boot_raw=0xa463a00) at Modules/threadmodule.c:422
#5  0x008f93cc in start_thread () from /lib/tls/libpthread.so.0
#6  0x0075af0e in clone () from /lib/tls/libc.so.6

Thread 1 (Thread -1208121664 (LWP 10919)):
#0  0x006717a2 in _dl_sysinfo_int80 () from /lib/ld-linux.so.2
#1  0x0071a4db in __waitpid_nocancel () from /lib/tls/libc.so.6
#2  0x006c3779 in do_system () from /lib/tls/libc.so.6
#3  0x006c3af1 in system () from /lib/tls/libc.so.6
#4  0x008ff98d in system () from /lib/tls/libpthread.so.0
#5  0x010b94bb in TUnixSystem::Exec () from /rooms/home/cern/root/pro/lib/libCore.so
#6  0x010bf153 in TUnixSystem::StackTrace () from /rooms/home/cern/root/pro/lib/libCore.so
#7  0x010bbb4e in TUnixSystem::DispatchSignals () from /rooms/home/cern/root/pro/lib/libCore.so
#8  0x010bbbdc in SigHandler () from /rooms/home/cern/root/pro/lib/libCore.so
#9  0x010bae59 in sighandler () from /rooms/home/cern/root/pro/lib/libCore.so
#10 <signal handler called>
#11 0x07db15cf in TTreeCloner::CopyProcessIds () from /rooms/home/cern/root/pro/lib/libTree.so
#12 0x07db1be9 in TTreeCloner::Exec () from /rooms/home/cern/root/pro/lib/libTree.so
#13 0x07db98d8 in TTree::CloneTree () from /rooms/home/cern/root/pro/lib/libTree.so
#14 0x07de4712 in G__G__Tree_109_0_31 () from /rooms/home/cern/root/pro/lib/libTree.so
#15 0x058538c5 in Cint::G__CallFunc::Execute () from /rooms/home/cern/root/pro/lib/libCint.so
#16 0x001557dc in PyROOT::TRootObjectExecutor::Execute () from /rooms/home/cern/root/pro/lib/libPyROOT.so
#17 0x0015b044 in PyROOT::TMethodHolder<PyROOT::TScopeAdapter, PyROOT::TMemberAdapter>::CallSafe () from /rooms/home/cern/root/pro/lib/libPyROOT.so
#18 0x0015b260 in PyROOT::TMethodHolder<PyROOT::TScopeAdapter, PyROOT::TMemberAdapter>::Execute () from /rooms/home/cern/root/pro/lib/libPyROOT.so
#19 0x0015f6dd in PyROOT::TMethodHolder<PyROOT::TScopeAdapter, PyROOT::TMemberAdapter>::operator() () from /rooms/home/cern/root/pro/lib/libPyROOT.so
#20 0x00164705 in PyROOT::(anonymous namespace)::mp_call () from /rooms/home/cern/root/pro/lib/libPyROOT.so
#21 0x00b7c37c in PyObject_Call (func=0x7bb820, arg=0xb7f7720c, kw=0x0) at Objects/abstract.c:1860
#22 0x00bef4f8 in do_call (func=0xb7bd3c4c, pp_stack=0xbfea7a5c, na=2, nk=0) at Python/ceval.c:3777
#23 0x00beef40 in call_function (pp_stack=0xbfea7a5c, oparg=0) at Python/ceval.c:3589
#24 0x00bed2b7 in PyEval_EvalFrameEx (f=0x9c67374, throwflag=0) at Python/ceval.c:2269
#25 0x00bedca2 in PyEval_EvalCodeEx (co=0xb7f5e9b0, globals=0x0, locals=0xb7fb1a44, args=0x0, argcount=0, kws=0x0, kwcount=0, defs=0x0, defcount=0, closure=0x0) at Python/ceval.c:2833
#26 0x00beadaf in PyEval_EvalCode (co=0xb7f5e9b0, globals=0xb7fb1a44, locals=0xb7fb1a44) at Python/ceval.c:494
#27 0x00c114c6 in run_mod (mod=0x9c935c8, filename=0xbff52b50 "tmp.py", globals=0xb7fb1a44, locals=0xb7fb1a44, flags=0xbfea7c5c, arena=0x9c78ea0) at Python/pythonrun.c:1264
#28 0x00c11450 in PyRun_FileExFlags (fp=0x9c55008, filename=0xbff52b50 "tmp.py", start=257, globals=0xb7fb1a44, locals=0xb7fb1a44, closeit=1, flags=0xbfea7c5c) at Python/pythonrun.c:1250
#29 0x00c10aed in PyRun_SimpleFileExFlags (fp=0x9c55008, filename=0xbff52b50 "tmp.py", closeit=1, flags=0xbfea7c5c) at Python/pythonrun.c:870
#30 0x00c105bd in PyRun_AnyFileExFlags (fp=0x9c55008, filename=0xbff52b50 "tmp.py", closeit=1, flags=0xbfea7c5c) at Python/pythonrun.c:689
#31 0x00c1c37a in Py_Main (argc=1, argv=0xbfea7d84) at Modules/main.c:496
#32 0x0804869e in main (argc=2, argv=0xbfea7d84) at Modules/python.c:23
Traceback (most recent call last):
  File "tmp.py", line 36, in <module>
    treeClone = tree.CloneTree(-1, "fast")
SystemError: problem in C++; program state has been reset

Hi,

Do you also have the problem if you use C++ (i.e. either CINT or ACliC or your compiled code)?
Do you also have the problem if you do NOT use a 2nd thread?

Cheers,
Philippe.