Copy branches from tree to tree

I have four files (Ci.root, i = 1,2,3,4) each with a single tree called Ci. These trees each contain only a single branch (called ampl) of std:: objects. The way that these files are produced, they have identical numbers of entries (indeed, the same-numbered entries in each tree are from the same physical event).

I would like a short program that can combine the ampl branches from the four trees into different branches of a single tree, in a single file. This could be by adding branches to one of the original Ci trees, or making an entirely new file with a new tree.

I am having trouble writing such a program myself, and I’m thinking that there must be a simple way to accomplish this. For example once I tried writing my own program to add histograms from different files, but then I learned of the “hadd” program provided by ROOT. Is there an equivalent program for trees and branches? Or perhaps someone has already done this and can provide code?

Thank you,
Jean-François

Hi,

In alternative to developing such a program, if my understanding is correct you should be able to use the TTree friend technology to have a common view of the 4 TTrees. Have a look at root.cern.ch/root/html/TTree.htm … :AddFriend.
Something like this should work:

   // Open the first file and load the first TTree
   TFile f("C1.root");
   TTree *t = (TTree *) f->Get("C1");
   // Add the others as friends
   t->AddFriend("C2", "C2.root");
   t->AddFriend("C3", "C3.root");
   t->AddFriend("C4", "C4.root");

From now on you can access the wanted ‘ampl’ by prefixing the right ‘Ci.’, i.e. ‘C1.ampl’ or ‘C3.ampl’ as if they were different branches of the same TTree.

G. Ganis