# Horizontally merging two TTrees

**URL:** <https://root-forum.cern.ch/t/horizontally-merging-two-ttrees/56397>\
**Category:** ROOT\
**Tags:** tree, rdataframe, root\
**Created:** [September 17, 2023, 9:22pm UTC](https://root-forum.cern.ch/t/horizontally-merging-two-ttrees/56397 "2023-09-17T21:22:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![harshjaluka](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@harshjaluka](https://root-forum.cern.ch/u/harshjaluka)\
**Post date:** [September 17, 2023, 9:22pm UTC](https://root-forum.cern.ch/t/horizontally-merging-two-ttrees/56397/1 "2023-09-17T21:22:39Z")

</div>

Hi!

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!

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [September 18, 2023, 6:09am UTC](https://root-forum.cern.ch/t/horizontally-merging-two-ttrees/56397/2 "2023-09-18T06:09:04Z")

</div>

I guess your question is similar to [this one](https://root-forum.cern.ch/t/merging-2-trees-contents-event-by-event/56215/7).

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [September 18, 2023, 5:19pm UTC](https://root-forum.cern.ch/t/horizontally-merging-two-ttrees/56397/3 "2023-09-18T17:19:59Z")

</div>

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](https://root.cern/doc/master/classTTree.html#a011d362261b694ee7dd780bad21f030b) and you can also search the forum or the ROOT tutorials for example usages. Friend trees are also described in the manual: [Trees - ROOT](https://root.cern/manual/trees/#widening-a-ttree-through-friends) .

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](https://root-forum.cern.ch/t/merging-two-ttrees-with-different-variables-but-1-common-key/55861) . Docs are [here](https://root.cern/doc/master/classTTree.html#a3f6b5bb591ff7a5bd0b06eea6c12b998). 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.

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [October 2, 2023, 5:20pm UTC](https://root-forum.cern.ch/t/horizontally-merging-two-ttrees/56397/4 "2023-10-02T17:20:33Z")

</div>

Closed on October 2, 2023, 5:20pm UTC

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