Merging tree friends

Hello all,
I am encountering a persistent problem when merging files containing trees with friends:
I have several files, each containing many TTrees. One of them, named “tree”, is a friend of all the others and is used to plot variables from different trees against each other. So far, everything works fine. Now, since there are many small files, I would like to merge them into one big file to speed things up. The way I do it is:

… loop over files to create one chain for every tree in the files
TFile *ff = new TFile(“tmp/test.root”, “recreate”);
for (int i=0; i<trees.size(); i++) {
TF[i] = chains[i]->CloneTree(); // here I also tried "CopyTree"
TString tname = trees[i].c_str();
for (int i=0; i<trees.size(); i++) {

The resulting file contains all the data from the original files, as can be verified by plotting the variables in each individual tree. However, using the “tree” tree to plot variables will only yield as many events as are contained in the first file. Here’s what I tried to solve the problem, all to no avail:

  • Removing friends from tree and adding them again, both before and after merge: Won’t work, because kRemoveFriend & fFriendLockStatus is TRUE
  • Making the trees friend to another tree: Same result, even if I don’t write the original “tree” tree to the new file.
  • Adding friends again to tree, creating new tree from scratch and making him friends with everyone, some more exotic things that I don’t remember but mostly resulted in segfaults.

The individual files contain unique identifiers “RunID” and “EventID”. I tried setting event limits in Draw() and noticed that in a combination of two files, every other event is plotted (but all coming from the first file in the chain), and so on.
Has anyone had the same problem before? Or a clue to what might solve it?


Did you also clone the ‘master’ tree? Does masterTree->GetEntries() returns the correct number of entries?

[quote]The individual files contain unique identifiers “RunID” and “EventID”.[/quote]Humm … I think then the problem is simply that you need to rebuild the index on the resulting tree. In ROOT 5.16/00 and above this should be automatically done.


Yes, that did it! Thank you very much!!