Copying entries from a TTree

I want to merge the same three branches (call them “A”, “B”, and “C”, if you like) from a large number of ROOT files, containing identically formatted TTree’s.

Currently, I iterate over each file and store that file’s TTree in a TChain. Then, I iterate over every entry in the TChain, and store the wanted branches of that entry in a new TTree, which is itself stored in a new ROOT file. Unfortunately, this is incredibly slow, due to the fact I’m working with a large number of files and, in each file, a large number of entries.

In the end, I want a ROOT file with a TTree that contains only these three branches, with all entries from each of these files. Further, if possible, I’d also like to take the sum of two of these branches, and store the result in a fourth branch, if possible.

I have been looking at the TTree documentation, and at first the CopyTree/CloneTree methods looked of use however, upon further inspection, it seems that may not be the case. The Merge method of the TChain class also looks potentially useful, but how might I restrict this to certain branches?

How can this be done better (i.e. faster)?

Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


TChain ch(...);
for(filename : files)
TFile *output = TFile::Open(newfilename, "NEW");
TTree *newtree = ch.CloneTree(-1,"fast");
delete output;

Add the new branch with the sum of the 3 branches will be much slower. (The above does not require any uncompressing of the data).

1 Like

Thanks, this works great! I completely forgot about SetBranchStatus()