# Facing Problem while fitting

**URL:** <https://root-forum.cern.ch/t/facing-problem-while-fitting/38305>\
**Category:** ROOT\
**Tags:** hist\
**Created:** [March 7, 2020, 6:25am UTC](https://root-forum.cern.ch/t/facing-problem-while-fitting/38305 "2020-03-07T06:25:29Z")\
**Posts on this page:** 13\
**Page:** 2

<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:** [March 8, 2020, 8:17am UTC](https://root-forum.cern.ch/t/facing-problem-while-fitting/38305/22 "2020-03-08T08:17:15Z")

</div>

You need to estimate initial function’s parameters looking at the spectrum, e.g. ([estimated\_sigma = estimated\_fwhm / 2.35](https://en.wikipedia.org/wiki/Full_width_at_half_maximum)): [estimated\_area = 2.51 \* estimated\_height \* estimated\_sigma = 1.06 \* estimated\_height \* estimated\_fwhm](https://en.wikipedia.org/wiki/Gaussian_function)

---

<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:** [March 8, 2020, 9:57am UTC](https://root-forum.cern.ch/t/facing-problem-while-fitting/38305/23 "2020-03-08T09:57:12Z")

</div>

Analytically i understood how am i getting values of the area under the peaks.

But when i try to retrieve its value from the original code i got an error:

```cpp
 Double_t TH1::Integral(Int_t binx1 = 400., Int_t binx2 = 435., Option_t *option) const
 {
    double err = 0;
    return DoIntegral(binx1,binx2,0,-1,0,-1,err,option);
 }

  TGraph *g = new TGraph("eu_gu_1.dat");
  // g->Sort(); // just a precaution
  Int_t n = g->GetN();
  Double_t *x = new Double_t[(n + 1)];
  x[0] = (3.0 * g->GetX()[0] - g->GetX()[1]) / 2.0;
  x[n] = (3.0 * g->GetX()[(n - 1)] - g->GetX()[(n - 2)]) / 2.0;
  for (Int_t i = 1; i < n; i++)
    { x[i] = (g->GetX()[(i - 1)] + g->GetX()[i]) / 2.0; }
  TH1D *h = new TH1D("h", "spectrum;x-value;#counts", n, x);
  h->FillN(n, g->GetX(), g->GetY());
  h->Sumw2(kFALSE); // restore proper errors
  delete [] x; // no longer needed
  delete g; // no longer needed
  TF1 *f1 = new TF1("f1", "gausn(0) + pol1(3)", 400., 435.); // linear background
  f1->SetParameters(14000., 415., 2., 0., 0.);
  h->Fit("f1", "BR+"); // e.g. "BR+" or "LBR+"
  TF1 *f2 = new TF1("f2", "gausn(0) + pol1(3)", 1160., 1200.);
  f2->SetParameters(15000., 1178., 2., 0., 0.);
  h->Fit("f2", "BR+"); // e.g. "BR+" or "LBR+"
  TF1 *f3 = new TF1("f3", "gausn", 4795., 4835.); // clean peak, no background
  f3->SetParameters(4400., 4815., 3.);
  h->Fit("f3", "BR+"); // e.g. "BR+" or "LBR+"

```

```nohighlight
 error: function definition is not allowed here
 {
 ^
<<< cling interactive line includer >>>:1:1: error: expected '}'

```

---

<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:** [March 8, 2020, 10:01am UTC](https://root-forum.cern.ch/t/facing-problem-while-fitting/38305/24 "2020-03-08T10:01:53Z")

</div>

[`std::cout << h->Integral(h->FindFixBin(400.), h->FindFixBin(435.)) << std::endl;`](https://root.cern.ch/doc/master/classTH1.html)

---

<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:** [March 8, 2020, 10:07am UTC](https://root-forum.cern.ch/t/facing-problem-while-fitting/38305/25 "2020-03-08T10:07:56Z")

</div>

So if i have to find it out for a number of peaks then each time i have to change values of the range. Is it possible to get the area of all the peaks simultaneously.

---

<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:** [March 8, 2020, 10:14am UTC](https://root-forum.cern.ch/t/facing-problem-while-fitting/38305/26 "2020-03-08T10:14:49Z")

</div>

[${ROOTSYS}/tutorials/spectrum](https://root.cern.ch/doc/master/group __tutorial__ spectrum.html)

---

<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:** [March 8, 2020, 10:39am UTC](https://root-forum.cern.ch/t/facing-problem-while-fitting/38305/27 "2020-03-08T10:39:30Z")

</div>

Let’s say i choose the code:

```cpp
   Double_t fpeaks(Double_t *x, Double_t *par) {
   Double_t result = par[0] + par[1]*x[0];  
   for (Int_t p=0;p<npeaks;p++) {
      Double_t norm = par[3*p+2]; // "height" or "area"
      Double_t mean = par[3*p+3];
      Double_t sigma = par[3*p+4];
#if defined( __PEAKS_C_FIT_AREAS__ )
      norm /= sigma * (TMath::Sqrt(TMath::TwoPi())); // "area"
#endif /* defined( __PEAKS_C_FIT_AREAS__ ) */
      result += norm*TMath::Gaus(x[0],mean,sigma);
   }
   return result;
}

```

In line 2, I am unable to understand what does this signifies? par[0] = ? par[1] = ?  
How are the values of p[0] & p[1] taken?

---

<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:** [March 8, 2020, 11:06am UTC](https://root-forum.cern.ch/t/facing-problem-while-fitting/38305/28 "2020-03-08T11:06:32Z")

</div>

Well, it’s a good question for your supervisor.

---

<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:** [March 8, 2020, 11:10am UTC](https://root-forum.cern.ch/t/facing-problem-while-fitting/38305/29 "2020-03-08T11:10:44Z")

</div>

Do I need to change **par[0] + par[1] _x[0]_** into gaus function? Am i thinking in right direction?

---

<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:** [March 8, 2020, 3:27pm UTC](https://root-forum.cern.ch/t/facing-problem-while-fitting/38305/30 "2020-03-08T15:27:37Z")

</div>

I calculated the area under all the peaks by both the methods

1. by sigma_2.5_H  
2.integral fix bin method  
The two values are not exactly similar and are given below:  
------------------------ Integration ------sigma_2.5_H  
1st peak (400-435) – 17412 ------- 14346  
2nd peak (1160-1200) 17388 ------- 15724  
3rd peak (4795-4835) - 4598 --------- 4423 (comparably close)

---

<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:** [March 8, 2020, 4:06pm UTC](https://root-forum.cern.ch/t/facing-problem-while-fitting/38305/31 "2020-03-08T16:06:58Z")

</div>

Try with “LBR+” fit options (you’ll get better “agreement” for the last peak).

Then show it to your supervisor, who should easily be able to explain the nonnegligible differences between the fitted peak area and the corresponding histogram integral. 😉

---

<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:** [March 8, 2020, 4:48pm UTC](https://root-forum.cern.ch/t/facing-problem-while-fitting/38305/32 "2020-03-08T16:48:49Z")

</div>

🙂  
Thanks for helping my throughout my problem but please tell me about how should I combine my code with calculating the no. of peaks codes.  
Double\_t fpeaks(Double\_t _x, Double\_t par) {  
Double\_t result = par[0] + par[1] x[0];  
for (Int\_t p=0;p\<npeaks;p++) {  
Double\_t norm = par[3 p+2]; // “height” or “area”  
Double\_t mean = par[3 p+3];  
Double\_t sigma = par[3_ p+4];  
#if defined( **PEAKS\_C\_FIT\_AREAS** )  
norm /= sigma \* (TMath::Sqrt(TMath::TwoPi())); // “area”  
#endif /\* defined( **PEAKS\_C\_FIT\_AREAS** ) _/  
result += norm_ TMath::Gaus(x[0],mean,sigma);  
}  
return result;  
}

---

<div class="post-metadata">

**Author:** ![vaidgourav2995](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/vaidgourav2995/32/21107_2.png) [@vaidgourav2995](https://root-forum.cern.ch/u/vaidgourav2995)\
**Post date:** [March 8, 2020, 7:31pm UTC](https://root-forum.cern.ch/t/facing-problem-while-fitting/38305/33 "2020-03-08T19:31:50Z")

</div>

Sir, Please explain this to me also. I am also unable to solve this.

---

<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:** [March 22, 2020, 7:41pm UTC](https://root-forum.cern.ch/t/facing-problem-while-fitting/38305/34 "2020-03-22T19:41:06Z")

</div>

Closed on March 22, 2020, 7:41pm UTC

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

[Previous page](https://root-forum.cern.ch/t/facing-problem-while-fitting/38305.md?page=1)
