Merging two Trees

Hi all!

I am facing the following problem:
I have two root files with one root tree each.
I would like to merge this two trees in one,
but respecting ofcourse some event number or so.
So I realized that AddFriend is not appropriate,
since there is no check if the event number in one
tree is the same in the other.

I found TTree::MakeIndex which will solve
my problem, but know I would not like to explicitely write
SetBranhAddress(bla bla) (ok maybe could be done
with MakeClass this one) for all branches of trees
and then Branch(bla bla) for the new tree, not only
because the brunch number is big but also because I
do not want to depend on what exactly branches each tree
has, as far as they satisfy the event number should be merged.

See for example a simple script which ofcourse does not work,
(probably I am mixing up CopyTree and CloneTree meanings)
But I think it will explain what I would like to do.

Thanks in advance!
Merge2Trees.C (1.13 KB)


Neither CopyTree nor CloneTree handle Friends the way your code seem to expect. Namely they do NOT copy the friend nor do they fold it in the new tree.

The good news is that you do not even need to ‘merge’ you tree physical. The AddFriends does a logical merge and you can then use the first tree as if it had the combination of both set of branches.


Hi again!
Thank you for the promt reply!

Indeed CopyTree or CloneTree would not work like this,
the script was, let say, what I would like to do,
but I still do not see with the AddFriends how the event number
will be respected. i.e. I see in the documentation that the two
trees will be friends and then the nth entry of the first tree is
allowed to be correlated with (i.e. have access to) the nth entry
of the other tree.
But this is exactly what I do not want! Because this has a great
danger of mixing up my events!
For example with the AddFriend:
-----1st Tree>============>2nd Tree

The first is ok but then is wrong!

Is there a way of using AddFriend and avoiding the above, or
any other suggestion?



If the friend tree has an index (BuildIndex as you did), it will be use to find the correct entry in the friend tree.