Facing Problem while fitting

You need to estimate initial function’s parameters looking at the spectrum, e.g. (estimated_sigma = estimated_fwhm / 2.35): estimated_area = 2.51 * estimated_height * estimated_sigma = 1.06 * estimated_height * estimated_fwhm

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:

 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+"
 error: function definition is not allowed here
 {
 ^
<<< cling interactive line includer >>>:1:1: error: expected '}'

std::cout << h->Integral(h->FindFixBin(400.), h->FindFixBin(435.)) << std::endl;

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.

${ROOTSYS}/tutorials/spectrum

Let’s say i choose the code:

   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?

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

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

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

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

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. :wink:

:slightly_smiling_face:
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;
}

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

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