# Merge horizontally TTrees with TBranch as key

**URL:** <https://root-forum.cern.ch/t/merge-horizontally-ttrees-with-tbranch-as-key/62279>\
**Category:** ROOT\
**Tags:** tree\
**Created:** [November 20, 2024, 9:49am UTC](https://root-forum.cern.ch/t/merge-horizontally-ttrees-with-tbranch-as-key/62279 "2024-11-20T09:49:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sfrances](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/ea666f/32.png) [@sfrances](https://root-forum.cern.ch/u/sfrances)\
**Post date:** [November 20, 2024, 9:49am UTC](https://root-forum.cern.ch/t/merge-horizontally-ttrees-with-tbranch-as-key/62279/1 "2024-11-20T09:49:10Z")

</div>

Let’s say I have two TTrees, with same names and branches. They are produced starting from a different source (e.g. two data streams) but events can overlap, meaning that I can find the same event in both. Total number of entries per tree is of course different.

There is a branch called eventID that is uniquely identifying the events in both ttees. The user can guarantee that the branches of the two trees are identical for the same eventID but cannot guarantee that entries are sorted by eventID.

Is there any existing method for merging the two ttrees using the eventID as a key? Or even just a sort of AddFriend mechanism with this handle? Preserving the info of the “origin” of the event would be a bonus but I suspect the task is already challenging as it is.

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [November 20, 2024, 1:15pm UTC](https://root-forum.cern.ch/t/merge-horizontally-ttrees-with-tbranch-as-key/62279/2 "2024-11-20T13:15:16Z")

</div>

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](https://root.cern.ch/doc/master/classTTree.html#a3f6b5bb591ff7a5bd0b06eea6c12b998) based on that column to loop on identical events:

```auto
// 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](https://root-forum.cern.ch/t/sort-a-rdataframe/50739))

Cheers,  
Danilo

---

<div class="post-metadata">

**Author:** ![sfrances](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/ea666f/32.png) [@sfrances](https://root-forum.cern.ch/u/sfrances)\
**Post date:** [November 20, 2024, 8:51pm UTC](https://root-forum.cern.ch/t/merge-horizontally-ttrees-with-tbranch-as-key/62279/3 "2024-11-20T20:51:23Z")

</div>

Hi Danilo,

Thank you for the idea.  
Indeed this might be the only reasonable solution.

Ciao,  
S

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [November 21, 2024, 6:09am UTC](https://root-forum.cern.ch/t/merge-horizontally-ttrees-with-tbranch-as-key/62279/4 "2024-11-21T06:09:00Z")

</div>

Hi Simone,

Let us know how that goes. I am sure other users faced this sophisticated situation.

Cheers,  
D

---

<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:** [December 5, 2024, 6:09am UTC](https://root-forum.cern.ch/t/merge-horizontally-ttrees-with-tbranch-as-key/62279/5 "2024-12-05T06:09:33Z")

</div>

Closed on December 5, 2024, 6:09am UTC

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