Forming 1 TTree from Branches of Two TTrees


Currently, I have a file which contains two uniquely named TTrees each containing unique TBranches. My goal is to take all of these branches from the two TTrees and put them into a single TTree in a new output file. The branches are TClonesArrays which hold custom class objects with sub-branches corresponding to each of the class’s member variables.

I have tried to grab the two trees and get the branch addresses and then passed the address to the new branch in the new tree, but when I fill, the branch-sub-branch structure is not constructed in the new TTree correctly and only zeros are filled.

I started working on this in pyROOT but I then moved to using a traditional .C macro so suggestions for either syntax are greatly appreciated!


Instead of creating another tree, you could try to use: TTree::AddFriend and/or: TChain::AddFriend
Try: grep -r AddFriend ${ROOTSYS}/t[eu]*

Hi @Wile_E_Coyote,

Does this work in the context of writing out a combined tree to a file? I know this is useful when working with the data in the trees, but I am combining them and writing them to an output file without working with the contained data.

Josh H

Hi Josh,

this code successfully “overlays” two trees. It uses TDataFrame and it would be good to run it with the latest ROOT.

// Prepare two fake dataset to then "superimpose"
int i = 0;
auto createDataset = [&i](const char *treeName, const char *fileName, const char *branchName){
   ROOT::Experimental::TDataFrame d(16);
   d.Define(branchName, [&i](){return i++;}).Snapshot<int>(treeName, fileName, {branchName});


// -------- This is the interesting part...
// Now we superimpose those
TFile f0("f0.root");
TTree *t0;

TFile f1("f1.root");
TTree *t1;

ROOT::Experimental::TDataFrame d(*t0);


This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.