# Problem in estimation of sigma and bin range while fitting gaussian peak

**URL:** <https://root-forum.cern.ch/t/problem-in-estimation-of-sigma-and-bin-range-while-fitting-gaussian-peak/39086>\
**Category:** Newbie\
**Tags:** root\
**Created:** [April 25, 2020, 2:02pm UTC](https://root-forum.cern.ch/t/problem-in-estimation-of-sigma-and-bin-range-while-fitting-gaussian-peak/39086 "2020-04-25T14:02:30Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![arorastacy](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/arorastacy/32/21238_2.png) [@arorastacy](https://root-forum.cern.ch/u/arorastacy)\
**Post date:** [April 25, 2020, 2:02pm UTC](https://root-forum.cern.ch/t/problem-in-estimation-of-sigma-and-bin-range-while-fitting-gaussian-peak/39086/1 "2020-04-25T14:02:30Z")

</div>

Hi, @couet @Wile_E_Coyote @moneta  
I have uploaded all the necessary files. In the C file I have use “?” because i am not sure to some of the estimated values.  
And I am trying to fit the peak which has a mean~1126.  
[d9.pdf](https://root-forum.cern.ch/uploads/short-url/zvvBCjzVPZQ8LUgLF4SI6tTovED.pdf) (20.5 KB) [d9\_1.C](https://root-forum.cern.ch/uploads/short-url/bwmpV8orvJ52B6e75ihMdEl41KE.C) (991 Bytes) [d9\_1.txt](https://root-forum.cern.ch/uploads/short-url/8XxpjuyWTkPJTa0ttNOqGzu711N.txt) (16.0 KB)

ROOT Version: 6.14  
_Platform:_ Linux  
_Compiler:_ Not Provided

* * *

---

<div class="post-metadata">

**Author:** ![arorastacy](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/arorastacy/32/21238_2.png) [@arorastacy](https://root-forum.cern.ch/u/arorastacy)\
**Post date:** [April 25, 2020, 3:39pm UTC](https://root-forum.cern.ch/t/problem-in-estimation-of-sigma-and-bin-range-while-fitting-gaussian-peak/39086/2 "2020-04-25T15:39:42Z")

</div>

I am asking this question because if i change the bin range then it will effect sigma, area and error in area and in sigma.  
So, in order to get the area correctly i used bin range from (mean-3_sigma to mean+3_sigma)  
but for that i require sigma that i am not sure at the first place.

---

<div class="post-metadata">

**Author:** ![arorastacy](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/arorastacy/32/21238_2.png) [@arorastacy](https://root-forum.cern.ch/u/arorastacy)\
**Post date:** [April 25, 2020, 6:38pm UTC](https://root-forum.cern.ch/t/problem-in-estimation-of-sigma-and-bin-range-while-fitting-gaussian-peak/39086/3 "2020-04-25T18:38:26Z")

</div>

I am having a problem in fitting the gaussian peak having the range of 1102 to 1150.  
Could you please help me?  
Have a nice day!

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [April 25, 2020, 7:17pm UTC](https://root-forum.cern.ch/t/problem-in-estimation-of-sigma-and-bin-range-while-fitting-gaussian-peak/39086/5 "2020-04-25T19:17:29Z")

</div>

[d9\_1.cxx](https://root-forum.cern.ch/uploads/short-url/z0QaYVkAhLgFTGQMPDNZJVvFiWQ.cxx) (1.9 KB)

BTW. Note that:

```cpp
  h->Fit(f, "IL+", "", xmin, xmax); // e.g. "IL+" or "I+" or "L+" or "+"

```

is fully equivalent to:

```cpp
  f->SetRange(xmin, xmax);
  h->Fit(f, "ILR+"); // e.g. "ILR+" or "IR+" or "LR+" or "R+"

```

---

<div class="post-metadata">

**Author:** ![arorastacy](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/arorastacy/32/21238_2.png) [@arorastacy](https://root-forum.cern.ch/u/arorastacy)\
**Post date:** [April 25, 2020, 7:23pm UTC](https://root-forum.cern.ch/t/problem-in-estimation-of-sigma-and-bin-range-while-fitting-gaussian-peak/39086/6 "2020-04-25T19:23:22Z")

</div>

Thanks @Wile_E_Coyote

 ![d9_1](https://root-forum.cern.ch/uploads/default/original/3X/9/6/9691f66351123f84239031b943e39bf855d6374d.png)  
I’m getting this using your code.  
Now, the thing that i am concern about is the height of fitted peaks. Why it is going higher than its original value?  
Could you please also explain me how you have selected the bin ranges for both the peaks?

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [April 25, 2020, 7:25pm UTC](https://root-forum.cern.ch/t/problem-in-estimation-of-sigma-and-bin-range-while-fitting-gaussian-peak/39086/7 "2020-04-25T19:25:20Z")

</div>

The histogram’s “bin width” is quite big (8 channels) when compared to peaks’ sigmas (around 4 to 5 channels).

---

<div class="post-metadata">

**Author:** ![arorastacy](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/arorastacy/32/21238_2.png) [@arorastacy](https://root-forum.cern.ch/u/arorastacy)\
**Post date:** [April 25, 2020, 7:27pm UTC](https://root-forum.cern.ch/t/problem-in-estimation-of-sigma-and-bin-range-while-fitting-gaussian-peak/39086/8 "2020-04-25T19:27:09Z")

</div>

@Wile_E_Coyote  
Sorry, But I am not getting it. Please elaborate

> [@Wile\_E\_Coyote](#):
>
> “bin width” is big (8 channels)

Also, please tell me about the heights of the fitted peaks.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [April 25, 2020, 8:15pm UTC](https://root-forum.cern.ch/t/problem-in-estimation-of-sigma-and-bin-range-while-fitting-gaussian-peak/39086/9 "2020-04-25T20:15:42Z")

</div>

Try the following example:

```cpp
{
  gStyle->SetOptStat("nemrI");
  gStyle->SetOptFit(111);
  TH1F *h1 = new TH1F("h1", "h1", 100, -5., 5.); // bin width = 0.1
#if 0 /* 0 or 1 */
  ((TF1*)gROOT->GetFunction("gaus"))->SetParameter(2, 2.); // 2 = sigma
#elif 0 /* 0 or 1 */
  ((TF1*)gROOT->GetFunction("gaus"))->SetParameter(2, 0.5); // 2 = sigma
#endif /* 0 or 1 */
  h1->FillRandom("gaus", 10000); // default mean = 0., default sigma = 1.
  TH1F *h2 = (TH1F*)h1->Clone("h2"); h2->SetTitle("h2");
  TH1F *h3 = (TH1F*)h1->Clone("h3"); h3->SetTitle("h3");
  TH1F *h4 = (TH1F*)h1->Clone("h4"); h4->SetTitle("h4");
  Int_t ngroup = 25; // e.g. 10, 20, 25
  h2->Rebin(ngroup); // bin width = 0.1 * ngroup
  h4->Rebin(ngroup); // bin width = 0.1 * ngroup
  Double_t xl = h1->GetBinCenter(1); // the first bin
  Double_t xr = h1->GetBinCenter(h1->GetNbinsX()); // the last bin
#if 0 /* 0 or 1 */
  xl = -2.; xr = 2.; // set "narrow" fit range
#endif /* 0 or 1 */
  TCanvas *c = new TCanvas("c", "c");
  c->Divide(2, 2);
  c->cd(1);
  h1->SetTitle("L"); h1->Fit("gausn", "L", "", xl, xr); // "L" or ""
  c->cd(2);
  h2->SetTitle("L"); h2->Fit("gausn", "L", "", xl, xr); // "L" or ""
  c->cd(3);
  h3->SetTitle("IL"); h3->Fit("gausn", "IL", "", xl, xr); // "IL" or "I"
  c->cd(4);
  h4->SetTitle("IL"); h4->Fit("gausn", "IL", "", xl, xr); // "IL" or "I"
  c->cd(0);
}

```

---

<div class="post-metadata">

**Author:** ![arorastacy](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/arorastacy/32/21238_2.png) [@arorastacy](https://root-forum.cern.ch/u/arorastacy)\
**Post date:** [April 25, 2020, 8:46pm UTC](https://root-forum.cern.ch/t/problem-in-estimation-of-sigma-and-bin-range-while-fitting-gaussian-peak/39086/11 "2020-04-25T20:46:01Z")

</div>

@Wile_E_Coyote  
8-channel width means that the raw data is having bin range from 0 to 8192 but there are only 1023 entries in the histogram. Do you mean this or anything else?  
Still unable to figure out how you set the bin ranges of peak around 1126 and 1710?  
Also, when you set range of peak around 1710 i.e.(1680 to 1755) it does not give a mean of 1710.

> [@Wile\_E\_Coyote](#):
>
> peaks’ sigmas (around 4 to 5 channels).

Please elaborate.

---

<div class="post-metadata">

**Author:** ![arorastacy](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/arorastacy/32/21238_2.png) [@arorastacy](https://root-forum.cern.ch/u/arorastacy)\
**Post date:** [April 26, 2020, 9:07am UTC](https://root-forum.cern.ch/t/problem-in-estimation-of-sigma-and-bin-range-while-fitting-gaussian-peak/39086/12 "2020-04-26T09:07:35Z")

</div>

@Wile_E_Coyote  
Also could you please help in fitting the other peaks.  
Could you please help me in setting the range of other peaks?  
because i don’t know how you taken the bin range.

---

<div class="post-metadata">

**Author:** ![arorastacy](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/arorastacy/32/21238_2.png) [@arorastacy](https://root-forum.cern.ch/u/arorastacy)\
**Post date:** [April 26, 2020, 11:56am UTC](https://root-forum.cern.ch/t/problem-in-estimation-of-sigma-and-bin-range-while-fitting-gaussian-peak/39086/13 "2020-04-26T11:56:28Z")

</div>

@Wile_E_Coyote  
One more question is that do I need to take bin range using (mean - 3_sigma) to (mean + 3_sigma)??  
Does this gives me 99.9 % area under the curve??  
Knowing this is so important because changing bins can easily affect the area by some good margin.

---

<div class="post-metadata">

**Author:** ![arorastacy](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/arorastacy/32/21238_2.png) [@arorastacy](https://root-forum.cern.ch/u/arorastacy)\
**Post date:** [April 26, 2020, 1:29pm UTC](https://root-forum.cern.ch/t/problem-in-estimation-of-sigma-and-bin-range-while-fitting-gaussian-peak/39086/15 "2020-04-26T13:29:25Z")

</div>

@Wile_E_Coyote  
Does the sigma that we get in the root terminal (while fitting the “gaus” function) does not have any connection with bin range or does it have?

---

<div class="post-metadata">

**Author:** ![arorastacy](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/arorastacy/32/21238_2.png) [@arorastacy](https://root-forum.cern.ch/u/arorastacy)\
**Post date:** [April 26, 2020, 1:50pm UTC](https://root-forum.cern.ch/t/problem-in-estimation-of-sigma-and-bin-range-while-fitting-gaussian-peak/39086/16 "2020-04-26T13:50:24Z")

</div>

Still unable to figure out when you set the bin ranges of peak around 1710 i.e.(1680 to 1755) it does not give a mean of 1710. ?  
Then, how you would have decide that range??  
Even, if I use (1650, 1785) I got almost the same area but still the mean is not 1710 if you see analytically.

Also, tell me about this “ILR”.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [April 26, 2020, 1:58pm UTC](https://root-forum.cern.ch/t/problem-in-estimation-of-sigma-and-bin-range-while-fitting-gaussian-peak/39086/17 "2020-04-26T13:58:06Z")

</div>

It’s the “data analyst’s” responsibility to choose an appropriate fit range so that the peak and the background can be described well enough.

Histogram fit options are described e.g. in: [TH1::Fit](https://root.cern/doc/master/classTH1.html#a7e7d34c91d5ebab4fc9bba3ca47dabdd)

---

<div class="post-metadata">

**Author:** ![arorastacy](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/arorastacy/32/21238_2.png) [@arorastacy](https://root-forum.cern.ch/u/arorastacy)\
**Post date:** [April 26, 2020, 2:05pm UTC](https://root-forum.cern.ch/t/problem-in-estimation-of-sigma-and-bin-range-while-fitting-gaussian-peak/39086/18 "2020-04-26T14:05:57Z")

</div>

@Wile_E_Coyote  
Does the value of the area that I am getting involves background subtraction or not??

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [April 26, 2020, 2:25pm UTC](https://root-forum.cern.ch/t/problem-in-estimation-of-sigma-and-bin-range-while-fitting-gaussian-peak/39086/19 "2020-04-26T14:25:31Z")

</div>

Write a simple dedicated simulation (e.g. several big peaks plus some non-egligible background) and play with it, e.g.:

```cpp
{
  TH1F *h = new TH1F("h", "h", 1000, 0., 10000.); // bin width = 10.
#if 1 /* 0 or 1 */
  ((TF1*)gROOT->GetFunction("pol1"))->SetParameters(10000., -1.);
  h->FillRandom("pol1", 100000); // linear background
#else /* 0 or 1 */
  h->FillRandom("pol0", 100000); // constant background
#endif /* 0 or 1 */
  TF1 *f = (TF1*)gROOT->GetFunction("gaus");
  f->SetParameters(1., 1000., 2.5); h->FillRandom("gaus", 1000);
  f->SetParameters(1., 2000., 5.0); h->FillRandom("gaus", 1000);
  f->SetParameters(1., 3000., 7.5); h->FillRandom("gaus", 1000);
  f->SetParameters(1., 4000., 10.0); h->FillRandom("gaus", 1000);
  f->SetParameters(1., 5000., 15.0); h->FillRandom("gaus", 1000);
  f->SetParameters(1., 6000., 20.0); h->FillRandom("gaus", 1000);
  f->SetParameters(1., 7000., 25.0); h->FillRandom("gaus", 1000);
  f->SetParameters(1., 8000., 30.0); h->FillRandom("gaus", 1000);
  f->SetParameters(1., 9000., 40.0); h->FillRandom("gaus", 1000);
  h->Draw(""); // e.g. "" or "HIST"
}

```

---

<div class="post-metadata">

**Author:** ![arorastacy](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/arorastacy/32/21238_2.png) [@arorastacy](https://root-forum.cern.ch/u/arorastacy)\
**Post date:** [April 26, 2020, 3:26pm UTC](https://root-forum.cern.ch/t/problem-in-estimation-of-sigma-and-bin-range-while-fitting-gaussian-peak/39086/21 "2020-04-26T15:26:45Z")

</div>

The two curves that I am getting are as follows:

 ![fC](https://root-forum.cern.ch/uploads/default/original/3X/7/3/73c51e3bdc07b01294e8353e3abf017abf763659.png) ![fL](https://root-forum.cern.ch/uploads/default/original/3X/7/4/74bd013dbfc7dd2149e8d96a6d5b6d89fe0ab97c.png)  
First one is with constant background and second one is with linear background.  
Now, if i choose any two peaks to fit in the histogram.  
then how does their areas gets changed with the different (linear and constant backgrounds)?.  
Could you please help me in doing that?

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [April 26, 2020, 3:30pm UTC](https://root-forum.cern.ch/t/problem-in-estimation-of-sigma-and-bin-range-while-fitting-gaussian-peak/39086/22 "2020-04-26T15:30:32Z")

</div>

[Learning by doing](https://en.wikipedia.org/wiki/Learning_by_doing) 🤔  
[Trial and error](https://en.wikipedia.org/wiki/Trial_and_error) 😉

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [April 27, 2020, 8:14am UTC](https://root-forum.cern.ch/t/problem-in-estimation-of-sigma-and-bin-range-while-fitting-gaussian-peak/39086/24 "2020-04-27T08:14:29Z")

</div>

Hi @arorastacy - don’t misinterpret Wile\_E’s helpfulness as a substitution for your own physics expertise. This is the ROOT forum, we’re happy to help with ROOT! Your questions are in the area of supervision: we shouldn’t be answering “Please tell me everything is fine or not?” - that’s for your supervisor and physics group to answer. Please don’t hesitate to ask any technical, specific question with using ROOT.

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [May 12, 2020, 4:52pm UTC](https://root-forum.cern.ch/t/problem-in-estimation-of-sigma-and-bin-range-while-fitting-gaussian-peak/39086/27 "2020-05-12T16:52:23Z")

</div>

Closed on May 12, 2020, 4:52pm UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
