#include "TH1.h" #include "TMath.h" #include "TF1.h" #include "TLegend.h" #include "TCanvas.h" // Breit-Wigner peak function Double_t bw(Double_t *x, Double_t *par) { return (1/(2*TMath::Pi()))*par[0]*par[2]/((x[0]-par[1])*(x[0]-par[1])+(par[2]*par[2]/4)); } // Background function Double_t back(Double_t *x, Double_t *par) { return par[0]*1/(par[1]*pow(x[0],3) + par[2]*pow(x[0],4) + par[3]*pow(x[0],5)) + par[4]; } // Sum of background and peak function Double_t fitFunction(Double_t *x, Double_t *par) { return bw(x,par) + back(x,&par[3]); } void Fit() { TFile *file = new TFile("teste02.root"); TH1D *MuonMassInv = file->Get("MuonMassInv"); gStyle->SetOptFit(); TF1 *fitsimple = new TF1("fitsimple",fitFunction,10,200,8); fitsimple->SetNpx(500); fitsimple->SetLineWidth(4); fitsimple->SetLineColor(kMagenta); fitsimple->SetParName(0,"Constant"); fitsimple->SetParameter(0,15000); fitsimple->SetParName(1,"Mean"); fitsimple->SetParameter(1,91.38); fitsimple->SetParName(2,"Sigma"); fitsimple->SetParameter(2, 3); fitsimple->SetParameter(3, 1e9); fitsimple->SetParameter(4, 2); fitsimple->SetParameter(5, 2.5e-2); fitsimple->SetParameter(6, 9e-3); fitsimple->SetParameter(7, -20); MuonMassInv->Fit("fitsimple","","",10.,200.); TF1 *backFcn = new TF1("backFcn",back,10,200,5); backFcn->SetLineColor(kRed); TF1 *signalFcn = new TF1("signalFcn",bw,10,200,3); signalFcn->SetLineColor(kBlue); Double_t par[8]; // writes the fit results into the par array fitsimple->GetParameters(par); backFcn->SetParameters(&par[3]); signalFcn->SetParameters(&par[0]); //Subtract background TH1D *hisSignal = new TH1D(*MuonMassInv); hisSignal->Sumw2(); hisSignal->Add(backFcn,-1); //Plot Spectra and functions gPad->SetLogy(); MuonMassInv->Draw("e"); // hisSignal->Draw("SAME"); //signalFcn->Draw("SAME"); // backFcn->Draw("SAME"); /* // draw the legend 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(fitsimple,"Global Fit","l"); legend->Draw(); */ // MuonMassInv->Draw(); } // TF1 *back = new TF1("back","1e9/(2*pow(x,3) + 2.5e-2*pow(x,4) - 9e-5*pow(x,5)) - 21.5 ",10,200);