Merge histograms/trees from several root files with hadd

Dear root experts,

I would like to do hadd of several root files.

Each of these root files it has a top directory folder with a folder name
“run_XXX”, where “XXX” is an unique run number.
An example of one of this root file “in_101.root”, the top directory folder name is “run_101”.

If I use hadd in the straight forward way like
“hadd out.root in_101.root in_102.root in_103.root”, then in out.root it will contain
several directory folders : “run_101”, “run_102”, “run_103”.

What I want instead is to merge all the histograms and trees in run_xxx of all the root files together.

Is there a possible way of doing this?

Thank you.

Regards,
Song-Ming

Welcome to the ROOT forum.

I tried to reproduce your problem the following way:

I made two files hs1.root and hs2.root both having the directory hs1 in it. Then I used hadd on them and the hso.root resulting file has only one folder hs1.

% rootls -l hs1.root
TProfile        Nov 22 10:37 2021 hprof;1  "Profile of pz versus px" 
TH1F            Nov 22 10:37 2021 hpx;1    "This is the px distribution" 
TH2F            Nov 22 10:37 2021 hpxpy;1  "py vs px" 
TDirectoryFile  Sep 04 12:54 2023 hs1;1    "hs1" 
TNtuple         Nov 22 10:37 2021 ntuple;1 "Demo ntuple" 

% rootls -l hs2.root    
TProfile        Nov 22 10:37 2021 hprof;1  "Profile of pz versus px" 
TH1F            Nov 22 10:37 2021 hpx;1    "This is the px distribution" 
TH2F            Nov 22 10:37 2021 hpxpy;1  "py vs px" 
TDirectoryFile  Sep 04 12:54 2023 hs1;1    "hs1" 
TNtuple         Nov 22 10:37 2021 ntuple;1 "Demo ntuple" 

% hadd hso.root hs1.root hs2.root
hadd Target file: hso.root
hadd compression setting for all output: 1
hadd Source file 1: hs1.root
hadd Source file 2: hs2.root
hadd Target path: hso.root:/
hadd Target path: hso.root:/hs1

% rootls -l hso.root             
TProfile        Sep 04 12:55 2023 hprof;1  "Profile of pz versus px" 
TH1F            Sep 04 12:55 2023 hpx;1    "This is the px distribution" 
TH2F            Sep 04 12:55 2023 hpxpy;1  "py vs px" 
TDirectoryFile  Sep 04 12:55 2023 hs1;1    "hs1" 
TNtuple         Sep 04 12:55 2023 ntuple;1 "Demo ntuple" 
% 

Hi,

Thank you for doing this test.
I am sorry in my first message I was not clear.
In the example I gave in “in_101.root” it has “run_101” as the top directory folder.
For “in_102.root” it has “run_102” top directory folder, and in “in_103.root” it has “run_103” top directory folder.

So when I do a hadd of these three root files, the output merged root file will contain separate “run_101”, “run_102” and “run_103” directory folders, instead of just one “run” folder that contains histograms/trees that are merged from the the histograms and trees in the “run_101”, “run_102” and “run_103” directory folders.

Regards,
Song-Ming

Ah ok, I understand. This is normal you have three directories with different names in the three initial ROOT file. You end up with that exact same directories in the resulting file. Nothing we can do.

Ok, thank you very much.
Regards,
Song-Ming

May be something like:

cp in_101.root in_101_copy.root; rootmv in_101_copy.root:run_101 in_101_copy.root:run
cp in_102.root in_102_copy.root; rootmv in_102_copy.root:run_102 in_102_copy.root:run
cp in_103.root in_103_copy.root; rootmv in_103_copy.root:run_103 in_103_copy.root:run
hadd out.root in_101_copy.root in_102_copy.root in_103_copy.root

Hi,

Thank you for making this suggestion.
I tested and it works!

Regards,
Song-Ming

1 Like

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