Merge horizontally TTrees with TBranch as key

Hi,

Thanks for the post, this is an interesting and not so easy problem, for which a standard and performant solution might not exist (and the reason is deeply rooted in the nature of the operation performed, not really in ROOT’s columnar IO).

Is eventID an integer value? In that case one could approach the problem building an index based on that column to loop on identical events:

// Set up the 2 trees and the way to read them
tree1->BuildIndex("eventID");
tree2->BuildIndex("eventID");
for (auto id : eventIDCollection) {
   tree1->GetEntryWithIndex(id);
   tree2->GetEntryWithIndex(id);
   // Custom operations for the merging and writing the 3rd TTree
}

Alternatively, if everything fits in memory, you can resort to RDataFrame (see this post Sort a RDataFrame)

Cheers,
Danilo