Root histograms in multi-files

1. Oct 16, 2015

ChrisVer

Suppose I have a collection of 40 directories, each containing 1, 2 ,3 ,4 or 5 .root files with histograms and trees.
What is an optimal way to merge those .root files so I would end up with 40 directories and 40 root files with the histos added together?
One way I thought of was to start typing for the 40 different directories an "hadd" command in the terminal.

...

However I find this kind of tiring and stupid. Do you have any better solution?
My problem with a macro is that I am not sure if it understands the *.root notation (so I cannot think of iterating).
Finally I am not sure if a TChain can, apart from Trees in different root files, merge TH1s (histograms) too.

THanks :)

2. Oct 16, 2015

andrewkirk

You could write a shell script that uses iteration to generate a text file containing a single line of the form:

then just copy the line to the command prompt and execute it

3. Oct 17, 2015

ChrisVer

It helped me to try to do it manually... after the 3rd try, I figured out what "for loop" I had to use in the shell script...That's what I had to do:
Code (Bash):

i=0
for folder in directory_path_of_folders/*; do
hadd -f target_directory/sumfile$((i++)).root$folder/*.root
done

Similar Threads - Root histograms multi Date
How to plot generalized hypergeometric function in ROOT? May 4, 2017
Fortran Getting the cubic root Sep 23, 2016
C/++/# ROOT TLegend fill box Jul 31, 2016
ROOT TMVA::factory weights Aug 4, 2015
ROOT- histogram Rebinning Jun 11, 2015