Hi,
I would like to fit a histogram in ROOT with a Gaussian (signal) and a Polynomial (background) and get the number of Signal Background events.
I was able to fit the histogram with one (background + Signal) fit function and deduce the total number of events.
However I would like to fit them separately. I tried to follow this example :https://root.cern.ch/root/html/tutorials/fit/combinedFit.C.html but my fits are not working.
Could anyone please let me know what I’m doing wrong ?
This is the piece of code :
Double_t background(Double_t *x, Double_t *par) {
return par[0] + par[1]*x[0] + par[2]*x[0]*x[0];
}
Double_t GAUSS(Double_t *x, Double_t *par){
return par[0]*exp(-0.5*(((x[0]-par[1])/par[2])*((x[0]-par[1])/par[2])));
}
Double_t fitFunction(Double_t *x, Double_t *par) {
return background(x,par) + GAUSS(x,&par[3]);
}
void TEST(){
n.Draw("Mph>>histo(100,3.04,3.19)");
histo->SetLineWidth(2);
histo->SetMarkerStyle(20);
TF1 *fitFcn = new TF1("fitFcn",fitFunction,3.04,3.17,6);
fitFcn->SetParameters(1,1,1,1,3.095,0.004);
histo->SetMinimum(0);
histo->Fit("fitFcn","0");
fitFcn->SetParameter(4,0.004);
fitFcn->SetParameter(5,3.095);
histo->Fit("fitFcn","V+","ep");
TF1 *backFcn = new TF1("backFcn",background,3.04,3.17,3);
backFcn->SetLineColor(kRed);
TF1 *signalFcn = new TF1("signalFcn",GAUSS,3.04,3.17,3);
signalFcn->SetLineColor(kBlue);
signalFcn->SetNpx(500);
Double_t par[6];
fitFcn->GetParameters(par);
backFcn->SetParameters(par);
backFcn->Draw("same");
signalFcn->SetParameters(&par[3]);
signalFcn->Draw("same");
double w=histo->GetBinWidth(1);
int integral = backFcn->Integral(3.04,3.19)/w;
int integral2=signalFcn->Integral(3.04,3.19)/w;
cout<<"Integral 1::::::"<<integral1<<endl;
cout<<"Integral 2::::::"<<integral2<<endl;
TLegend *legend=new TLegend(0.6,0.65,0.88,0.85);
legend->SetTextFont(72);
legend->SetTextSize(0.04);
legend->AddEntry(histo,"Data","lpe");
legend->AddEntry(backFcn,"Background fit","l");
legend->AddEntry(signalFcn,"Signal fit","l");
legend->AddEntry(fitFcn,"Global Fit","l");
legend->Draw();
}