Merge branches of same tree of two root files

Dear Expert,

I have two root files (File1.root, and File2.root). These files have the tree of the same name (t) but different branches like below:
File1.root → t → b1, b2, b3…
File2.root → t → c1, c2, c3 …

How can I merge these branches (b1, b2, b3…, c1, c2, c3…) in a single root file of tree “t” like below?
File.root → t → b1, b2, b3…, c1, c2, c3…

Can you please help me to do this?

Regards
Chanchal


Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


Hi,

hadd final.root File1.root File2.root
is what you are looking for?

You can also use it with wildcards,
e.g. if you have 1000 files File1.root, …, File1000.root, you could do:
hadd final.root File*.root to combine all them.

Here is some documentation I managed to find: ROOT: main/src/hadd.cxx File Reference

cheers

Hii @FoxWise,

hadd is not working fine. I used it earlier also. See below:

Its shows one warning and File.root only gives the branches of File1.root (first file) not of both files.

For your reference, here are the files:
File1.root (1.5 MB)
File2.root (104.7 KB)

Can you please help me to sort it out?

Regards
Chanchal

Hm,

No idea, was it always impossible to merge two trees if one of the branches wasn’t present in either of the trees and I just didn’t notice?

Maybe ROOT experts can help you here.

Hi @chanchal , @FoxWise ,

as far as I know hadd is only for vertical concatenation of trees (i.e. concatenating the entries of two trees with the same schema).

The TTree friend mechanism can be used to do logical horizontal concatenations, and you can use RDataFrame (in particular, Snapshot) to then save the larger dataset to a new file.

Example code (not tested, but it should give you the idea):

auto *t1 = file1.Get<TTree>("t1"); 
auto *t2 = file2.Get<TTree>("t2");
t1->AddFriend(t2);
ROOT::RDataFrame df(*t1);
df.Snapshot("newtreee" , "newfile.root");

Cheers,
Enrico

1 Like

Thanks, a lot @eguiraud, and @FoxWise.

it’s working.

1 Like

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