# Sum of TF1 in a loop

**URL:** <https://root-forum.cern.ch/t/sum-of-tf1-in-a-loop/39276>\
**Category:** Newbie\
**Created:** [May 6, 2020, 1:37am UTC](https://root-forum.cern.ch/t/sum-of-tf1-in-a-loop/39276 "2020-05-06T01:37:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Newbie](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@Newbie](https://root-forum.cern.ch/u/Newbie)\
**Post date:** [May 6, 2020, 1:37am UTC](https://root-forum.cern.ch/t/sum-of-tf1-in-a-loop/39276/1 "2020-05-06T01:37:48Z")

</div>

Dear,

In a loop of events, I want to calculate a Gaussian for each event, using the corresponding mean and width of the event, but also summing all the gaussians after each event. This is what I tried (naively), which doesn’t work:

> Gaus\_i = ROOT.TF1(“Gaus\_i”,“gaus”,100, 200)  
> Gaus\_tot = ROOT.TF1(“Gaus\_tot”,“gaus”,100, 200)  
> i = 0
> 
> for iev in chain:  
> i+= 1  
> Gaus\_i.Clear()  
> Gaus\_i.SetParameters(i, iev.mean, iev.width)  
> Gaus\_tot = ROOT.TF1(“Gaus\_tot”,“Gaus\_tot + Gaus\_i”,100, 200)

Is there any solution to this problem? Btw is the use of TF1 here the best way, or should I use something else?

Many thanks!

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [May 6, 2020, 9:08am UTC](https://root-forum.cern.ch/t/sum-of-tf1-in-a-loop/39276/2 "2020-05-06T09:08:21Z")

</div>

Hi,

It is not clear to me exactly what you wnat to do it. It looks like you want to build a parametrize function as sum of many gaussian, each one for each single event. Is this really what you need ?  
If you just want just to evaluate a Gaussian function on one event using a TF1 is not the optimal way to do it. You should use for example the `TMath::Gaus` function and do something like

```auto
gaus_tot = 0.
for iev in chain: 
   i+= 1
   gaus_tot += ROOT.TMath.Gaus(x, iev.mean, iev.width, true) ## use true if you want a normalized gaussian

```

but it is not clear to me on what you are evaluating on, what is x ?

Lorenzo

---

<div class="post-metadata">

**Author:** ![Newbie](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@Newbie](https://root-forum.cern.ch/u/Newbie)\
**Post date:** [May 6, 2020, 1:44pm UTC](https://root-forum.cern.ch/t/sum-of-tf1-in-a-loop/39276/3 "2020-05-06T13:44:53Z")

</div>

Hi Lorenzo,

My apologies for the vague description, but I want to construct a PDF that is the sum of gaussians with as mean the value of the parameter I’m considering and the resolution as the standard deviation. Then I want to compare this resulting distribution to a certain histogram.

is the x here the amplitude of the Gaussian? Would it make sense if I equate it to the weight of the event?

Many thanks for you help!

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [May 6, 2020, 1:49pm UTC](https://root-forum.cern.ch/t/sum-of-tf1-in-a-loop/39276/4 "2020-05-06T13:49:56Z")

</div>

Hi,  
Apologies, but still I don’t fully understand what you want to constrict. Do you want to build a PDF as sum of gaussian kernels, something like this

> **[Kernel density estimation](https://en.wikipedia.org/wiki/Kernel_density_estimation)**
>
> In statistics, kernel density estimation (KDE) is a non-parametric way to estimate the probability density function of a random variable. Kernel density estimation is a fundamental data smoothing problem where inferences about the population are made, based on a finite data sample. In some fields such as signal processing and econometrics it is also termed the Parzen–Rosenblatt window method, after Emanuel Parzen and Murray Rosenblatt, who are usually credited with independently creating it in ...

For this we have already a class in ROOT doing that  
[https://root.cern.ch/doc/master/classTKDE.html](https://root.cern.ch/doc/master/classTKDE.html)

Lorenzo

---

<div class="post-metadata">

**Author:** ![Newbie](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@Newbie](https://root-forum.cern.ch/u/Newbie)\
**Post date:** [May 6, 2020, 3:17pm UTC](https://root-forum.cern.ch/t/sum-of-tf1-in-a-loop/39276/5 "2020-05-06T15:17:22Z")

</div>

Hi Lorenzo,

The piece of code you wrote is exactly what I want to calculate, I want to know that specifi distribution which is the sum of the gaussians, however I don’t know how to plot the resulting distribution, TMath returns just a number right? Because I want to compare it to a histogram

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [May 6, 2020, 8:06pm UTC](https://root-forum.cern.ch/t/sum-of-tf1-in-a-loop/39276/6 "2020-05-06T20:06:34Z")

</div>

Hi,  
You might want to create a function from that and plot. Maybe something like this

```auto
def my_function(x, par) : 
  gaus_tot = 0.
  for iev in chain: 
      i+= 1
      gaus_tot += ROOT.TMath.Gaus(x[0], iev.mean, iev.width, true) 
      return gaus_tot

f1 = ROOT.TF1('f1',my_function, 100, 200 , 0)
f1.Draw()
```

---

<div class="post-metadata">

**Author:** ![Newbie](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@Newbie](https://root-forum.cern.ch/u/Newbie)\
**Post date:** [May 8, 2020, 2:28pm UTC](https://root-forum.cern.ch/t/sum-of-tf1-in-a-loop/39276/7 "2020-05-08T14:28:42Z")

</div>

Hi Lorenzo,

This solution looks promising (and efficient), do you need to pass a variable here? i.e. “par” in the definition  
I didn’t manage to run it

In the meantime I figured out a way by basically converting TF1’s to TH1’s then add the histograms:

> for iev in chain1:  
> Gaus\_i.Clear()  
> Gaus\_i = ROOT.TF1(“Gaus\_i”,“gaus”,100, 200)  
> Gaus\_i.SetParameters(1., iev.mean, iev.sigma)  
> Hist\_i = Gaus\_i.GetHistogram()  
> Hist\_tot.Add(Hist\_i, iev.weight)

And this works, however on the edges of the histogram, peaks are appearing, which is due to accumulating events that doesn’t fall out the range. Any idea how to fix that?

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [May 10, 2020, 5:16pm UTC](https://root-forum.cern.ch/t/sum-of-tf1-in-a-loop/39276/8 "2020-05-10T17:16:23Z")

</div>

Hi,

Sorry my code above was just an example. Attached here is a fully running code

For your solution, I would need a script to run, please provide me in case

Lorenzo

[rootForum\_39276.py](https://root-forum.cern.ch/uploads/short-url/kIGeYBqwcQcruaFy13a7xjrK8dp.py) (576 Bytes)
