# Sum of multiple spectra of different root files

**URL:** <https://root-forum.cern.ch/t/sum-of-multiple-spectra-of-different-root-files/63020>\
**Category:** Newbie\
**Tags:** tree, hist, root\
**Created:** [February 25, 2025, 4:09pm UTC](https://root-forum.cern.ch/t/sum-of-multiple-spectra-of-different-root-files/63020 "2025-02-25T16:09:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![MC\_w](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mc_w/32/38525_2.png) [@MC\_w](https://root-forum.cern.ch/u/MC_w)\
**Post date:** [February 25, 2025, 4:09pm UTC](https://root-forum.cern.ch/t/sum-of-multiple-spectra-of-different-root-files/63020/1 "2025-02-25T16:09:08Z")

</div>

Hi!

I have multiple root files with the structure as in the photo. Inside the outTree I have some Tleaves. In the Ener1, I have the deposited energy of a radioactive source. **I aim to sum all** of the Ener1 outputs to a single histogram. I was not able to find a similar topic, or at least something that worked.

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/6/1/612208088bde0504e4451b8637da5d910af3e5bc.png)

Bear with me, I tried with my friend Chatgpt and I managed to get a of macro that works. The problem is that it works whenever it likes and most of the time it crashes or plots a white c canvas. How do I solve this issue? Again, I aim at summing all the spectra. Any help will be much appreciated, Thanks!

[sumhist.C](https://root-forum.cern.ch/uploads/short-url/hS4r55YAa07Q5Rqe4PMjwrbb6Qd.C) (2.3 KB)

\_ROOT Version:6.26/06  
For Linuxx8664gcc

---

<div class="post-metadata">

**Author:** ![mdessole](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mdessole/32/34021_2.png) [@mdessole](https://root-forum.cern.ch/u/mdessole)\
**Post date:** [February 25, 2025, 4:14pm UTC](https://root-forum.cern.ch/t/sum-of-multiple-spectra-of-different-root-files/63020/2 "2025-02-25T16:14:44Z")

</div>

Hello @MC_w,

could you please detail the error you get at crash time? is it a segfault?

Cheers,  
Monica

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [February 25, 2025, 8:13pm UTC](https://root-forum.cern.ch/t/sum-of-multiple-spectra-of-different-root-files/63020/3 "2025-02-25T20:13:26Z")

</div>

I guess the simplest will be to `hadd` all input files and then to output the `Ener1` to a histogram.

---

<div class="post-metadata">

**Author:** ![MC\_w](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mc_w/32/38525_2.png) [@MC\_w](https://root-forum.cern.ch/u/MC_w)\
**Post date:** [March 3, 2025, 9:21am UTC](https://root-forum.cern.ch/t/sum-of-multiple-spectra-of-different-root-files/63020/4 "2025-03-03T09:21:18Z")

</div>

Hi, sorry for the late reply, I was out of office. Turns out that hadding all the root files was the best solution. Thanks a lot for the help!!!  
Matteo
