Horizontally merging two TTrees


Let’s say I have two root files, both containing a TTree with the same number of events but with a different set of columns. Both contain a common column that uniquely identifies each event. I want to merge the two TTrees horizontally so that I have one TTree with columns from both TTrees. Can I do this?

If so, is it also possible if the events in one of the TTrees is a subset of the other (as opposed to having a 1-1 correspondence as above)?

Thanks in advance!

I guess your question is similar to this one.

Hi @harshjaluka ,

for a simple horizonal join in which you process every entry of the two trees in lockstep (entry 1 of tree A with entry 1 of tree B, then entry 2 of tree A with entry 2 of tree B), it’s enough to use mainTree->AddFriend(auxTree) and loop over mainTree. Docs are here and you can also search the forum or the ROOT tutorials for example usages. Friend trees are also described in the manual: Trees - ROOT .

For more complex joins over a common column value (e.g. EventNumber), TTree offers “indexed friend trees”. Here’s an example of how to build that relation: Merging two TTrees with different variables but 1 common key . Docs are here. For every entry in the main tree, the entry in the friend tree with the same value for the index column will be loaded, even if it’s out of order and even if the relation is many to one.


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