void fitSignal(){ // style options gStyle->SetOptStat(0); gStyle->SetOptFit(1111); // prepare histogram to be fitted TH1F *h= new TH1F("h", " Inv. Mass Distribution", 100,0.,3.); TF1 * Bg = new TF1("Bg","expo",0,3); Bg->SetParameters(1,-2.); TF1 * Sig = new TF1("Sig","gaus",1.5,2.5); Sig->SetParameters(1,2.,0.1); h->FillRandom("Bg",30000); h->FillRandom("Sig",10000); //Now fit: pre-set initial value of the parameters for total h->Fit(Bg,"RQ"); h->Fit(Sig,"RQ"); Double_t par[5],dpar[5]; Bg->GetParameters(&par[0]); Sig->GetParameters(&par[2]); //define total and fit histo TF1 *total = new TF1("total","expo(0)+gaus(2)",0,3); total->SetParameters(par); h->Fit(total,"RQ"); h->GetFunction("total")->SetLineColor(3); cout << "Fit Chisquare: "<< h->GetFunction("total")->GetChisquare()/h->GetFunction("total")->GetNDF()<GetFunction("total")->GetParameter(3)<<" +/-"<GetFunction("total")->GetParError(3)<SetMaximum(5.E5); h->GetXaxis()->SetTitle("M_inv (GeV/c^2)"); h->GetYaxis()->SetTitle("Events"); h->SetMarkerStyle(20); TCanvas *cFit = new TCanvas("cFit","Fit Example",200,10,600,400); cFit->SetLogy(); h->Draw("e,p"); Sig->Draw("same"); }