Hadd to an already existing file

Hi all,
I was wondering if there’s a way to use hadd on a file already existing. My idea is to launch a scrip with a for loop inside and for every step the loop runs some code that produces a .root file. I would like to use hadd on each of these files keeping always the same target file (I would avoid to use a single hadd at the end of the loop for all the files contemporaneously).

Hi @gabriele,
have you tried the option -a?

$ man hadd
       -a     Append to the output


Hi Vincenzo,

I tried, but this doesn’t work, it says that it is not a supported option:

user@machine:~/path/to/dir/ hadd target.root file1.root
hadd Target file: target.root
hadd Source file 1: file1.root
hadd Target path: target.root:/
user@machine:~/path/to/dir/hadd -a target.root file2.root
Error: option -a is not a supported option.
hadd Target file: target.root
Error in <TFile::TFile>: file target.root already exists
Error in <TFileMerger::OutputFile>: cannot open the MERGER output file target.root
hadd error opening target file (does target.root exist?).
Pass "-f" argument to force re-creation of output file.

I also tried the -f, but it overwrites the target file.



Hi @gabriele,

which ROOT version is this? I could imagine that the append option didn’t exist in older versions.

Is appending/hadding necessary? What’s inside the file?

  • Histograms: Makes sense to hadd/append.
  • Trees: You don’t need to append. Just make a chain of the single trees when you read them.

Hi @StephanH,

ROOT version in use is 5.18.00 (unfortunately I can’t change it because it is on a server) and the produced root files contain only 1D (TH1) and 2D (TH2) histograms.



Hmm, the first ROOT version I ever touched was ROOT 5.28. So I really cannot help with 5.18. :frowning:

The “-a” option of ROOT was added in v6.06/00. With v5.18, you will need to use temporary files and make those partial merged input of the next ones. Note that you can (and should) pass more than 2 files are input to hadd (eg. You could hadd them 200 at a time or more).