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…
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.
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");