How to use TChain to merge multiple TTrees into a ROOTfile


I am still stuck with merging issues…

I have two input files, which contain only set of TTree-s, each of which in turn might contain TBranchElement and so forth.

The problem is that I have merge those TTree-s and HADD (under ROOT 5.12) is somehow not managing to merge them properly (each of my input files is around 90MB, but i doubt the size is the problem) and so i have to either write manually a TTree merger or use some existing one (is there?).

Firstly i tried to use TChain-s in a fillowing manner:

TChain chain1(“tree_one”);
TChain chain2(“tree_two”);

and then when i do


with a subsequent TChain::Merge, it puts the contents of of tree_two into tree_one and doesnt create a separate tree_two on the same directory level as tree_one.

I also tried adding into one TChain multiple TTree-s using TChain::AddFile where one explicitly writes which TTree of which file should go into the TChain but that didnt work also, the result was the same as above…

And as i am a beginner in this TChain, TTree affairs, i am a bit lost as to how to proceed…

Does anyone have a sample root macro that would read TTree(s) from many ROOT files and merge them into one ROOT file? Or else how can i use TChain to merge many TTree-s into one ROOT file?

Thanks a lot in advance,

I very easy use TChain in your case, if in example you have two ROOT files: myfile1.root and myfile2.root, that contain the TTree mytree, with the same structure, you can use TChain::Add(), ex.:

TChain mychain("mychain");


If mytree is not in a directory and is and the only TTree in the file you can omit the tree name too.

I didnt mean to add same TTree from different ROOT files…

I rather meant to add different TTree-s, having different names and structures, to one ROOT file.



As I keep repeating to you, the solution to your problem is $ROOTSYS/bin/hadd.
If this does not work, post 2 small files showing the problem somewhere in a public area.