# Hadd to an already existing file

**URL:** <https://root-forum.cern.ch/t/hadd-to-an-already-existing-file/40716>\
**Category:** Newbie\
**Created:** [July 29, 2020, 1:28pm UTC](https://root-forum.cern.ch/t/hadd-to-an-already-existing-file/40716 "2020-07-29T13:28:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gabriele](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/g/76d3ee/32.png) [@gabriele](https://root-forum.cern.ch/u/gabriele)\
**Post date:** [July 29, 2020, 1:28pm UTC](https://root-forum.cern.ch/t/hadd-to-an-already-existing-file/40716/1 "2020-07-29T13:28:48Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![vpadulan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/vpadulan/32/25315_2.png) [@vpadulan](https://root-forum.cern.ch/u/vpadulan)\
**Post date:** [July 29, 2020, 1:48pm UTC](https://root-forum.cern.ch/t/hadd-to-an-already-existing-file/40716/2 "2020-07-29T13:48:53Z")

</div>

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

```auto
$ man hadd
[...]
OPTIONS
       -a Append to the output

```

Cheers,  
Vincenzo

---

<div class="post-metadata">

**Author:** ![gabriele](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/g/76d3ee/32.png) [@gabriele](https://root-forum.cern.ch/u/gabriele)\
**Post date:** [July 29, 2020, 1:57pm UTC](https://root-forum.cern.ch/t/hadd-to-an-already-existing-file/40716/3 "2020-07-29T13:57:03Z")

</div>

Hi Vincenzo,

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

```auto
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.

Thanks,

gabriele

---

<div class="post-metadata">

**Author:** ![StephanH](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@StephanH](https://root-forum.cern.ch/u/StephanH)\
**Post date:** [July 29, 2020, 2:05pm UTC](https://root-forum.cern.ch/t/hadd-to-an-already-existing-file/40716/4 "2020-07-29T14:05:21Z")

</div>

Hi @gabriele,

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

Also:  
Is appending/`hadd`ing 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.

---

<div class="post-metadata">

**Author:** ![gabriele](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/g/76d3ee/32.png) [@gabriele](https://root-forum.cern.ch/u/gabriele)\
**Post date:** [July 29, 2020, 2:27pm UTC](https://root-forum.cern.ch/t/hadd-to-an-already-existing-file/40716/5 "2020-07-29T14:27:42Z")

</div>

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.

Cheers,

gabriele

---

<div class="post-metadata">

**Author:** ![StephanH](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@StephanH](https://root-forum.cern.ch/u/StephanH)\
**Post date:** [July 29, 2020, 2:30pm UTC](https://root-forum.cern.ch/t/hadd-to-an-already-existing-file/40716/6 "2020-07-29T14:30:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [July 29, 2020, 2:30pm UTC](https://root-forum.cern.ch/t/hadd-to-an-already-existing-file/40716/7 "2020-07-29T14:30:59Z")

</div>

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).
