Merging root files

Hello!

Many root files are produced after running over Grid.
It is necessary to make one root file from them.
I could not find some topic on that either in the ROOT Users Guide or
in the ROOT tutorials.
Please someone give me some information on merging root files.

Best Regards,
Sangryul Ro

use $ROOTSYS/bin/hadd

$ hadd
Usage: C:\cygwin\home\brun\root\bin\hadd.exe [-f] [-T] targetfile source1 source2 [source3 ...]
This program will add histograms from a list of root files and write them
to a target root file. The target file is newly created and must not
exist, or if -f ("force") is given, must not be one of the source files.
Supply at least two source files for this to make sense... ;-)
If the first argument is -T, Trees are not merged

Rene

1 Like

To avoid the merge all together, you could use a TChain.
To merge the list look at TTree::Merge, TChain::Merge and TTree::MergeTrees.
Also it is possible that the ‘hadd’ executable is just what you need.

Philippe.

Thanks for all the information.
On afs, the ROOTSYS variables are automatically defined.
And I found that it is
/afs/cern.ch/cms/external/lcg/external/root/
3.10.02/slc3_ia32_gcc323/root.
So when I issued the command,
/afs/cern.ch/cms/external/lcg/external/root/3.10.02/slc3_ia32_gcc323/root/bin/hadd.exe ntpl01.root ntuples_2.root ntuples_5.root,
the error messages shown below are displayed.
I am trying hard to find what’ wrong.

Cheers, Sangryul

######################
Syntax Error: /afs/cern.ch/cms/external/lcg/external/root/3.10.02/slc3_ia32_gcc323/root/afs/cern.ch/cms/external/lcg/external/root/3.10.02/slc3_ia32_gcc323/root/afs/cern.ch/cms/external/lcg/external/root/3.10.02/slc3_ia32_gcc323/root/afs/cern.ch/cms/external/lcg/external/root/3.10.02/slc3_ia32_gcc323/root/bin/hadd.exe ntpl01.root ntuples_2.root ntuples_5.root FILE:(tmpfile) LINE:1
*** Interpreter error recovered ***

The command, hadd, is working now.
But it is only for root files containing histograms.
When it contains root tree, it sent the error message as follows.

#############################################
[lxplus016] ~/sw/ORCA_8_7_3/src/Workspace > hadd ntpl02.root myUserTree_01.root myUserTree_02.root
Target file: ntpl02.root
Source file 1: myUserTree_01.root
Warning in TClass::TClass: no dictionary for class MyEvent is available
Source file 2: myUserTree_02.root
Unknown object type, name: rootTree title: events
#############################################

In a ROOT session, I can load the dynamic library.
But here it’s not and that seems causing the warning message above.
I am investigationg the details particulary noting the last line.
But if someone have an idea, that will be appreciated.

Cheers, Sangryul Ro

Hi,

In ROOT session you can also use $ROOTSYS/tutorials/io/hadd.C to do the same work as the hadd executable once you loaded your library.

Philippe

Yes. That works.
It seems that hadd.C must be used to add root files containing root trees and requiring the dynamic libraries loaded.

Cheers, Sangryul Ro

Hi, I have a similar problem. It would be very helpful if someone can help.
Inside a parent directory, I have 100 folders with different names (randomly generated names). Each of these folders has a folder called root_files having one root file: A.root. so the directory is: parent_dir/folder_1/root_files. Inside which, I have 1 histogram: A.root. So 100 A.root files in total. Here, A.root is a histogram saved in .root format.

I want to add all the 100 A.root from parent_dir/folder_name/root_files. Could you please suggest how can I do it using hadd?
As I the folder name is randomly generated, it would be nice if you can suggest a command where hadd will find. “A.root” from subdirectories and merge all of them into one output. root. To make it more simple: here’s what I am trying to do:

  1. find. -name '*A.root' #when I am inside /results
  2. hadd output.root {all the A.root files}

I dont know how to perfectly merge these 2 commands. It would be very helpful if someone can suggest a suitable command which would do these 2 tasks. Thanks a lot for any suggestion :slight_smile:

Best,
Cein.

hadd output.root $(find /path/to/parent_dir -name "*A.root")

1 Like

Thanks a lot :slight_smile: