Sum of multiple spectra of different root files

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.

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 (2.3 KB)

_ROOT Version:6.26/06
For Linuxx8664gcc

Hello @MC_w,

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

Cheers,
Monica

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