Hi ROOTers,
I am having trouble fitting my data with fits. I have a spectrum of multiple peaks. I am creating separate canvas for each peak and fitting them using gaus+pol1 fitting. Whenever I fit using gaus, everything works well but whenever I add a pol1 to the fit, my fit fails. I tried defining gaus and pol1 functions separately but still for some reason it fails. Here is the code -
Double_t min, max, sigma;
TH1F *myhist[npeaksFound];
char *histname = new char[npeaksFound];
TCanvas *ch=new TCanvas(histname,"ch");
ch->Print("Peaks_disk118.pdf[");
for (int i=0;i<npeaksFound;i++) {
sprintf(histname,"histo%d",i);
// sigma = 5/2.5 ; // using the first peak, fwhm visually is 5 so std dev = fwhm/2.5 this only considers half the peak
min = xpeaks[i] - 5; // just taking fwhm
max = xpeaks[i] + 5;
cout << "peak , xmin, xmax = " << xpeaks[i] << " " << min << " " << max << endl;
myhist[i] = (TH1F*)hTVsC_backsub->Clone();
TCanvas *ch=new TCanvas(histname,"ch");
myhist[i]->GetXaxis()->SetRangeUser(min,max);
Double_t binmaxY = myhist[i]->GetMaximumBin();
Double_t binmaxX = myhist[i]->GetBinCenter(binmaxY);
TF1 *gaus = new TF1("gaus", "[0]* exp(-0.5*((x-[1])/[2])^2)");
TF1 *pol1 = new TF1("pol1", "[3] + [4] * x ");
gaus->SetParameter(1,xpeaks[i]); // mean
gaus->SetParameter(2,(max-min)/2.); // std dev ...defines the sigma
pol1->SetParameter(4,0.1);// slope
pol1->SetParameter(3,1.); // intercept
//TF1* Tfit =new TF1("Tfit","gaus"); // add pol1
//myhist[i]->Fit("gaus");
myhist[i]->Fit("pol1");
// myhist[i]->Fit("Tfit");
/*TF1* Tfit =new TF1("Tfit","gaus+pol1"); // add pol1
myhist[i]->Fit("Tfit");*/
ch->cd();
myhist[i]->Draw(""); // I have removed Draw("histo") to see the fit lines; Ask Dr. Meisel why this happens;
ch->Print("Peaks_disk118.pdf");
Double_t p1 = gaus->GetParameter(1); //mean
Double_t p2 = gaus->GetParameter(2); //std dev
cout << " Mean p1 : " << p1 << "\t sigma p2: " << p2 << endl;
Double_t lower_limit = p1 - 3*p2;
Double_t upper_limit = p1 + 3*p2;
Double_t counts = myhist[i]->Integral(upper_limit, lower_limit);
Double_t rms = myhist[i]->GetRMS();
Double_t total_counts = sqrt(counts);
Double_t uncertainty = rms/total_counts; // use getRMS command here..this is wrong
cout << lower_limit << "=============================" << endl;
cout << upper_limit << "=============================" << endl;
cout << rms << "=============================" << endl;
cout << total_counts << "=============================" << endl;
cout << uncertainty << "=============================" << endl;
Double_t Mean = myhist[i]->GetMean();
Can anyone help with this? Thank you for your time and help!
ROOT Version: 6.18/04
Platform: C++
Compiler: Visual Studio Code