Fitting with two gaussian curves

I am fitting my data using two Gaussian curves. After fitting, the mean value that I am getting for the combined curves is for example 10.24 and it is visible from the graph that the peak (i.e. mean) is before the value of 10. So the mean shouldn’t be more than 10.
My only interpretation is that because of fitting of two Gaussian curves, I am obtaining an incorrect mean value.
Can anyone please tell me what should I do ?

-Diksha

Post here your data and your macro which fits it.

Here is my macro:
fit_2_gaus.C (1.5 KB)

and here is the graph I am obtaining:

My root file’s size is large, so I am unable to upload it here. :sweat:

Just save and post the “h” histogram:

// right after T1->Project("h", ...
TFile *f = TFile::Open("h.root", "RECREATE");
h->Write();
delete f;

BTW. Note that the displayed “Constant”, “Mean” and “Sigma” correspond to the first function that you fit (i.e. “g1” / “m1”).

h.root (3.7 KB)

But I am using the “+” command in the end for the fitting which I think then should give all the three parameters for the combined curve. Is it not ?

Try:

{
#if 1 /* 0 or 1 */
  TFile *f = TFile::Open("h.root");
  if ((!f) || f->IsZombie()) { delete f; return; } // just a precaution
  TH1F *h; f->GetObject("h", h);
  if (!h) { delete f; return; } // just a precaution
#else /* 0 or 1 */
  TH1F *h= new TH1F("h", "trkmm gaussian fit", 50, 4, 16);
  ((TF1*)gROOT->GetFunction("gaus"))->SetParameters(1., 8., 1.);
  h->FillRandom("gaus");
  ((TF1*)gROOT->GetFunction("gaus"))->SetParameters(1., 10., 1.);
  h->FillRandom("gaus");
#endif /* 0 or 1 */
  
  TF1 *g1 = new TF1 ("m1", "gaus", 4, 9);
  g1->SetLineColor(kRed);
  TF1 *g2 = new TF1 ("m2", "gaus", 8, 14);
  g2->SetLineColor(kGreen);
  TF1 *f1 = new TF1("double_gaus", "gaus(0) + gaus(3)", 4, 16);
  f1->SetParNames("Constant 1", "Mean 1", "Sigma 1",
                  "Constant 2", "Mean 2", "Sigma 2");
  f1->SetLineColor(kBlue);
  
  gStyle->SetOptFit(1);
  
  h->Fit(g1, "R");
  h->Fit(g2, "R+");
  
  Double_t par[6];
  g1->GetParameters(&par[0]);
  g2->GetParameters(&par[3]);
  f1->SetParameters(par);
  
  h->Fit(f1, "R+");
  // h->Fit(f1, "+", "e1", 4, 16);
  h->Draw("e1");
  
  std::cout << std::endl; g1->Print();
  std::cout << std::endl; g2->Print();
  std::cout << std::endl; f1->Print();
}

BTW. Note that the “f1” / “double_gaus” has 6 parameters so there is NO single “mean” and “sigma”. However, right below “Entries”, you do have the “Mean” and the “RMS” (which is actually the “Sigma”) values printed for the whole histogram (which is probably what you want).

Try also:

{
#if 1 /* 0 or 1 */
  TFile *f = TFile::Open("h.root");
  if ((!f) || f->IsZombie()) { delete f; return; } // just a precaution
  TH1F *h; f->GetObject("h", h);
  if (!h) { delete f; return; } // just a precaution
#else /* 0 or 1 */
  TH1F *h= new TH1F("h", "trkmm gaussian fit", 50, 4, 16);
  ((TF1*)gROOT->GetFunction("gaus"))->SetParameters(1., 8., 1.);
  h->FillRandom("gaus");
  ((TF1*)gROOT->GetFunction("gaus"))->SetParameters(1., 10., 1.);
  h->FillRandom("gaus");
#endif /* 0 or 1 */
  
  TF1 *g1 = new TF1 ("m1", "gaus", 4, 9);
  g1->SetLineColor(kRed);
  TF1 *g2 = new TF1 ("m2", "gaus", 8, 14);
  g2->SetLineColor(kGreen);
  TF1 *f1 = new TF1("double_gaus", "gaus(0) + gaus(3)", 4, 16);
  f1->SetParNames("Constant 1", "Mean 1", "Sigma 1",
                  "Constant 2", "Mean 2", "Sigma 2");
  f1->SetLineColor(kBlue);
  
  gStyle->SetOptFit(1);
  
  h->Fit(g1, "R");
  h->Fit(g2, "R");
  
  Double_t par[6];
  g1->GetParameters(&par[0]);
  g2->GetParameters(&par[3]);
  f1->SetParameters(par);
  
  h->Fit(f1, "R");
  // h->Fit(f1, "+", "e1", 4, 16);
  h->Draw("e1");
  
  g1->Draw("SAME");
  g2->Draw("SAME");
  f1->Draw("SAME");
  
  std::cout << std::endl; g1->Print();
  std::cout << std::endl; g2->Print();
  std::cout << std::endl; f1->Print();
}

But the mean and the RMS value just below the entries are the values for histogram (i.e. data) but I want the value of Mean and sigma for the fitted curve. So as I have 6 parameters, that means I have 2 values of mean and 2 values of sigma. How can I take a combined value ? Can I take the average of the two values?

and what are these mean and sigma values in this graph i.e. Mean: 10.24 and sigma: 2.037 ?

Try:

  std::cout << g1->Mean(f1->GetXmin(), f1->GetXmax()) << std::endl;
  std::cout << TMath::Sqrt(g1->Variance(f1->GetXmin(), f1->GetXmax())) << std::endl;
  std::cout << g2->Mean(f1->GetXmin(), f1->GetXmax()) << std::endl;
  std::cout << TMath::Sqrt(g2->Variance(f1->GetXmin(), f1->GetXmax())) << std::endl;
  std::cout << f1->Mean(f1->GetXmin(), f1->GetXmax()) << std::endl;
  std::cout << TMath::Sqrt(f1->Variance(f1->GetXmin(), f1->GetXmax())) << std::endl;

Actually, you should probably use something like “mean ± 5 sigma” in calculations:

  Double_t xmin = f1->GetXmin();
  Double_t xmax = f1->GetXmax();
  Double_t mean = f1->Mean(xmin, xmax);
  Double_t sigma = TMath::Sqrt(f1->Variance(xmin, xmax));
  xmin = mean - 5.0 * sigma;
  xmax = mean + 5.0 * sigma;
  mean = f1->Mean(xmin, xmax);
  sigma = TMath::Sqrt(f1->Variance(xmin, xmax));
  std::cout << "Mean = " << mean << std::endl;
  std::cout << "Sigma = " << sigma << std::endl;

It worked. Thanks a ton! :slight_smile:

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