# TH1F combine spectra

**URL:** <https://root-forum.cern.ch/t/th1f-combine-spectra/34432>\
**Category:** Newbie\
**Created:** [June 5, 2019, 9:07am UTC](https://root-forum.cern.ch/t/th1f-combine-spectra/34432 "2019-06-05T09:07:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jbrummer](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@jbrummer](https://root-forum.cern.ch/u/jbrummer)\
**Post date:** [June 5, 2019, 9:07am UTC](https://root-forum.cern.ch/t/th1f-combine-spectra/34432/1 "2019-06-05T09:07:23Z")

</div>

Hello

I have a question about a piece of code. I have various 1D histograms and want to combine. Now I have been able to do this successfully but I want to know why I cannot include the code in the same script.

e.g.

```auto
#include <runsPR254.h>
#include <TROOT.h>
#include <TH1F.h>

TH1F *hCombo, *hExCurrent;

void hEx_add()
{
	for(int i=0 ; i<109 ; i++)
	{
		gROOT->ProcessLine(Form(".x Ex/Ex-runs%d.C",runs[i]));
		hExCurrent = (TH1F*)gROOT->FindObjectAny("Ex");
		if(i==0) hCombo = (TH1F*)hExCurrent->Clone("hExCombo");
		else if(i>0) hCombo->Add(hExCurrent);
	}
	hCombo->Draw();
}

```

Now if this code is executed in the same script as the one that actually generates the 1D histograms, it simply copies the first histogram every loop iteration. I have to run this script separately. Does anyone know why? Or is this a bug. I think it might be.

(ROOT v5-34-00-patches, Ubuntu 18.04)

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [June 5, 2019, 10:32am UTC](https://root-forum.cern.ch/t/th1f-combine-spectra/34432/2 "2019-06-05T10:32:44Z")

</div>

> [@jbrummer](#):
>
> Now if this code is executed in the same script as the one that actually generates the 1D histograms

Can you show this “same script” ?  
We need to have the full picture to understand your problem.
