# Filling a function to a histogram

**URL:** <https://root-forum.cern.ch/t/filling-a-function-to-a-histogram/45901>\
**Category:** Newbie\
**Tags:** hist\
**Created:** [July 19, 2021, 8:04am UTC](https://root-forum.cern.ch/t/filling-a-function-to-a-histogram/45901 "2021-07-19T08:04:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aman\_Upadhyay](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/aman_upadhyay/32/23958_2.png) [@Aman\_Upadhyay](https://root-forum.cern.ch/u/Aman_Upadhyay)\
**Post date:** [July 19, 2021, 8:04am UTC](https://root-forum.cern.ch/t/filling-a-function-to-a-histogram/45901/1 "2021-07-19T08:04:05Z")

</div>

Hello Rooters,

I was trying to calculate the significance value for every value using a signal and background histogram. I need to save this significance value and the corresponding bin value for later use. I was trying to use TH1D for storing significance values. I need to save the significance value for the first bin cut in the first bin. First I am not sure how to do that. Second I feel using a histogram to store a function is not the most natural way here, there must be something better. I have attached the code below. Thanks for your time!

* * *

```auto
int nent = h1_s->GetNbinsX();
		for(int e=0; e<nent; e++){
			double cut_value = h1_s->GetBinCenter(e);
			s = h1_s->Integral(e,nent);
			b = h1_b->Integral(e,nent);
			significance = s/sqrt(s+b);
			h_declengthNorm->Fill(significance);

```

h1\_s and h1\_b are signal and background histograms respectively. h\_declengthNorm is a histogram with the same number of bins, bin min, and bin max as h1\_s and h1\_b, this is where I intend to save the significance function.

* * *

ROOT Version:6.24/02  
Platform:(NAME=“HefftorLinux”,ID\_LIKE=“arch”,VERSION\_ID=v2021-02-05)  
Compiler: gcc 11.1.0

---

<div class="post-metadata">

**Author:** ![jalopezg](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jalopezg/32/25844_2.png) [@jalopezg](https://root-forum.cern.ch/u/jalopezg)\
**Post date:** [July 19, 2021, 11:07am UTC](https://root-forum.cern.ch/t/filling-a-function-to-a-histogram/45901/2 "2021-07-19T11:07:57Z")

</div>

Hi @Aman_Upadhyay; I am sure either @couet or @moneta can help you with this.

Cheers,  
J.

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [July 19, 2021, 1:28pm UTC](https://root-forum.cern.ch/t/filling-a-function-to-a-histogram/45901/3 "2021-07-19T13:28:35Z")

</div>

histo-\>[Fill](https://root.cern/doc/master/classTH1.html#filling-histograms)(x) does not do what you think it does; maybe you want SetBinContent instead (search for it on that same page). In any case, I think saving the bin centre and the significance (as 2 variables) in a [TTree](https://root.cern/doc/master/classTTree.html) is probably a better choice than a histogram, to let you use the values later for anything else.

---

<div class="post-metadata">

**Author:** ![Aman\_Upadhyay](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/aman_upadhyay/32/23958_2.png) [@Aman\_Upadhyay](https://root-forum.cern.ch/u/Aman_Upadhyay)\
**Post date:** [July 19, 2021, 2:53pm UTC](https://root-forum.cern.ch/t/filling-a-function-to-a-histogram/45901/4 "2021-07-19T14:53:24Z")

</div>

Yes, I know histo-\>Fill(x) will not work in this case 😅, that’s why the question. I needed SetBinContent, thanks for that! And I see that a TTree will be much better. Thanks a lot.
