# Merging 2 trees contents event-by-event

**URL:** <https://root-forum.cern.ch/t/merging-2-trees-contents-event-by-event/56215>\
**Category:** ROOT\
**Created:** [August 30, 2023, 6:49am UTC](https://root-forum.cern.ch/t/merging-2-trees-contents-event-by-event/56215 "2023-08-30T06:49:25Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![mmaneyro](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mmaneyro/32/34214_2.png) [@mmaneyro](https://root-forum.cern.ch/u/mmaneyro)\
**Post date:** [August 30, 2023, 6:49am UTC](https://root-forum.cern.ch/t/merging-2-trees-contents-event-by-event/56215/1 "2023-08-30T06:49:25Z")

</div>

Hello  
I would like to merge event-by-event two Trees containing simple branches (say pT, eta, phi) with 2 particles per event. Namely, starting from Trees 1 and 2 with 1000 events each, with 2-particles per event, I would like to get a new merged output Tree1+2 with 1000 new events with 2+2=4 particles per event (each new event being the simple sum of the branches of event 1,… 1000 and 1,…, 1000 of Tree and 2, respectively). The standard ROOT tree merging tools just produce an output tree with 2000 events with 2-particles per event, whereas what I need is 1000 events with 4-particles per event. I don’t know if it’s clear… It seems like an obvious operation to me, but I couldn’t find a simple solution to get the resulting merged Tree file.

Thanks in advance for your feedback.

---

<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:** [August 30, 2023, 7:03am UTC](https://root-forum.cern.ch/t/merging-2-trees-contents-event-by-event/56215/2 "2023-08-30T07:03:57Z")

</div>

Welcome to the ROOT forum

Have you looked at the [AddFriend](https://root.cern/doc/v628/tree3_8C.html) functionality ?

---

<div class="post-metadata">

**Author:** ![mmaneyro](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mmaneyro/32/34214_2.png) [@mmaneyro](https://root-forum.cern.ch/u/mmaneyro)\
**Post date:** [September 6, 2023, 2:13pm UTC](https://root-forum.cern.ch/t/merging-2-trees-contents-event-by-event/56215/3 "2023-09-06T14:13:11Z")

</div>

Thank you for your suggestion. This allows me to access the branches from the friend tree from the original tree. What I would like to get is a tree where each branch contains the corresponding data coming from both trees.  
How should I proceed with the friend trees to get this result? Or should I try a different approach altogether?

---

<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 11, 2023, 8:08am UTC](https://root-forum.cern.ch/t/merging-2-trees-contents-event-by-event/56215/4 "2023-09-11T08:08:09Z")

</div>

In principle if the tree T1 has the branches A B C and the tree T2 the branches D E F making them ‘friends’ is like having a tree T3 with branches A B C D E F.

---

<div class="post-metadata">

**Author:** ![mmaneyro](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mmaneyro/32/34214_2.png) [@mmaneyro](https://root-forum.cern.ch/u/mmaneyro)\
**Post date:** [September 12, 2023, 1:59pm UTC](https://root-forum.cern.ch/t/merging-2-trees-contents-event-by-event/56215/5 "2023-09-12T13:59:56Z")

</div>

Exacly, what I’m looking for is T3 with branches A’ B’ C’, where A’=A+D and so on.

---

<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 12, 2023, 2:26pm UTC](https://root-forum.cern.ch/t/merging-2-trees-contents-event-by-event/56215/6 "2023-09-12T14:26:04Z")

</div>

So in principle, the “friend” mechanism is that. May be @pcanal can confirm/help.

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [September 12, 2023, 4:28pm UTC](https://root-forum.cern.ch/t/merging-2-trees-contents-event-by-event/56215/7 "2023-09-12T16:28:49Z")

</div>

> [@mmaneyro](#):
>
> Exacly, what I’m looking for is T3 with branches A’ B’ C’, where A’=A+D and so on.

If the data inside the branch needs to be ‘merged’ in any way, you will need to create a new `TTree` based on the original `TTree` and its `Friend` using a tool that let you express the type of merging operation you mean (I.e. addition of value or concatenation of collections, etc.).

For this I recommend you investigate the use of [`SnapShot`](https://root.cern.ch/doc/master/classROOT_1_1RDF_1_1RInterface.html#ac5903d3acec8c52f13cbd405371f7fb7) in [`RDF`](https://root.cern.ch/doc/master/classROOT_1_1RDataFrame.html)

---

<div class="post-metadata">

**Author:** ![mmaneyro](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mmaneyro/32/34214_2.png) [@mmaneyro](https://root-forum.cern.ch/u/mmaneyro)\
**Post date:** [September 20, 2023, 8:17pm UTC](https://root-forum.cern.ch/t/merging-2-trees-contents-event-by-event/56215/8 "2023-09-20T20:17:06Z")

</div>

Besides performing an addition of some of the values, which is doable with RDF, some of the data needs to be concatenated entry by entry (or event by event). In other words, starting off with two branches with momenta for 4 different particles per entry, I would like to get one branch with momenta for 8 particles by entry. Is that merge operation definable for RDF or a tree?

All of my attempts so far have allowed me to either get a file with double the number of entries (half coming from each of the files), or twice the amount of branches by using friend trees. Is concatenating two entries even possible?

An issue I ran into looking into snapshots is that my trees contain TClonesArray type branches. Therefore snapshot gives the following error :  
Error in TTree::Bronch: TClonesArray with no dictionary defined in branch: Event  
I tried using  
df.Snapshot\<TClonesArray, TClonesArray, TClonesArray\>(“name” , “name.root”,{“Event”, “Rwgt”, “Particle”});  
but this didn’t seem to make a difference. I read on some other posts that TClonesArray is not well supported by snapshot, is this still true and are there any workarounds?

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [September 21, 2023, 1:17am UTC](https://root-forum.cern.ch/t/merging-2-trees-contents-event-by-event/56215/9 "2023-09-21T01:17:21Z")

</div>

> [@mmaneyro](#):
>
> Error in TTree::Bronch: TClonesArray with no dictionary defined in branch: Event

You can get past this by either loading a library containing the original code or by generating one via:

```auto
file->MakeProject("libevent", "*", "RECREATE+");

```

will generate `libevent/libevent.so`

---

<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 5, 2023, 1:17am UTC](https://root-forum.cern.ch/t/merging-2-trees-contents-event-by-event/56215/10 "2023-10-05T01:17:24Z")

</div>

Closed on October 5, 2023, 1:17am UTC

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