How to merge TDirectory objects into one


Here is what I’d like to do:
I have 2 ROOT files, each of them contains a TDirectory with many histograms in it. I’d like to open these 2 ROOT files, merge both TDirectory objects into one and save the resulting TDirectory into a new ROOT file.

I actually solved this by opening each TDirectory and looping over all the histograms to copy them one by one into a new ROOT file. I was wondering if there was a fancier solution like the “Merge()” function for the TTree class.
Thanks a lot


Do the 2 directory have the same name in both file?


Hi Philippe,

Yes they do.
It looks like that:
TDirectory Dir_1=(TDirectory)File_1->Get(“MapDir”);
TDirectory Dir_2=(TDirectory)File_2->Get(“MapDir”);


In this case you can either use the executable ‘hadd’ or the class TFileMerger to add the 2 files together.