# 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:** 1\
**Showing post:** 3

<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

---

_[View the full topic](https://root-forum.cern.ch/t/horizontally-merging-two-ttrees/56397)._
