Merging 2 trees contents event-by-event

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.

Welcome to the ROOT forum

Have you looked at the AddFriend functionality ?

1 Like

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?

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.

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

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

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 in RDF

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?

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

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

will generate libevent/