# How to fit polynomail and BreitWigner function?

Hi,
I try to use polynomial and breit-wigner function to fit a curve where background follows polynomial and the peak follows breit-wigner. But when I try to fit I only get a horizontal line which is much higher than data’s curve. I don’t know what’s wrong with my code. Here’s the code about fitting and the attachment is the data I use.
Thanks,
Zhaozhou

``` RooRealVar mass_peak("m0","m0",60,120); RooRealVar width("width","width",0,100); RooRealVar a0("a0","",0,1000); RooRealVar x("x","",40,140); x.setBins(1000); RooPolyVar pol5("pol5","",x,RooArgList(a0));//construct a polynomial RooRealVar coe_bw("coe_bw","",1); RooRealVar coe_pol("coe_pol","",1); RooBreitWigner bw("bw","Breit-Wigner function fit",x,mass_peak,width);//construct a Breit-Wigner function RooDataHist *mass_data=new RooDataHist("Zmass","Z boson's mass",x,invmass_mmp);//convert data to roodatahist RooPlot *plot_frame=x.frame(RooFit::Title("signal invariant mass of muon+ muon- and photon"),RooFit::Bins(1000)); RooAddPdf bwandpol("bwandpol","",RooArgList(bw,pol5),RooArgList(coe_bw,coe_pol));//construct function include background and peak bwandpol.fitTo(*mass_data);//fit the curve ```
MCinvmass2E7.root (4.77 KB)

Hi ZhaoZhou,

please find below a working macro.
The point is to use a RooPolynomial for the background and the fact that when you add two pdfs, you just need a single coefficient. See the online doc for RooAddPdf and RooPolynomial for more details.

Cheers,
Danilo

``````void macro(){
TFile f("MCinvmass2E7.root");
TH1F* invmass_mmp;
f.GetObject("mass_mmp",invmass_mmp);
RooRealVar mass_peak("m0","m0",90, 60,120);
RooRealVar width("width","width",10, 0,100);
RooRealVar a0("a0","",0,1000);
RooRealVar x("x","",40,140);
x.setBins(1000);
RooPolynomial pol5("pol5","",x,RooArgList(a0));//construct a polynomial
RooRealVar coe_bw("coe_bw","",1,0,1);
RooRealVar coe_pol("coe_pol","",.0,0,1);
RooBreitWigner bw("bw","Breit-Wigner function fit",x,mass_peak,width);//construct a Breit-Wigner function
RooDataHist *mass_data=new RooDataHist("Zmass","Z boson's mass",x,invmass_mmp);//convert data to roodatahist
RooPlot *plot_frame=x.frame(RooFit::Title("signal invariant mass of muon+ muon- and photon"),RooFit::Bins(1000));
RooAddPdf bwandpol("bwandpol","",bw,pol5,coe_bw);//construct function include background and peak
bwandpol.fitTo(*mass_data);//fit the curve
mass_data->plotOn(plot_frame);
bwandpol.plotOn(plot_frame);
plot_frame->Draw();
}``````

[quote=“dpiparo”]Hi ZhaoZhou,

please find below a working macro.
The point is to use a RooPolynomial for the background and the fact that when you add two pdfs, you just need a single coefficient. See the online doc for RooAddPdf and RooPolynomial for more details.

Cheers,
Danilo

``` void macro(){ TFile f("MCinvmass2E7.root"); TH1F* invmass_mmp; f.GetObject("mass_mmp",invmass_mmp); RooRealVar mass_peak("m0","m0",90, 60,120); RooRealVar width("width","width",10, 0,100); RooRealVar a0("a0","",0,1000); RooRealVar x("x","",40,140); x.setBins(1000); RooPolynomial pol5("pol5","",x,RooArgList(a0));//construct a polynomial RooRealVar coe_bw("coe_bw","",1,0,1); RooRealVar coe_pol("coe_pol","",.0,0,1); RooBreitWigner bw("bw","Breit-Wigner function fit",x,mass_peak,width);//construct a Breit-Wigner function RooDataHist *mass_data=new RooDataHist("Zmass","Z boson's mass",x,invmass_mmp);//convert data to roodatahist RooPlot *plot_frame=x.frame(RooFit::Title("signal invariant mass of muon+ muon- and photon"),RooFit::Bins(1000)); RooAddPdf bwandpol("bwandpol","",bw,pol5,coe_bw);//construct function include background and peak bwandpol.fitTo(*mass_data);//fit the curve mass_data->plotOn(plot_frame); bwandpol.plotOn(plot_frame); plot_frame->Draw(); } ```[/quote]
Hi Danilo,
Thanks for your answer and it works well in this situation, and I find that the problem is that I should use RooPolynomial instead of RooPolyVar and use only one coefficient. But i still don’t understand when I just use one coefficient what the function that is created by RooAddPdf looks like (I mean it’s like “function1+coefficientfunciton2" or "coefficientfunction1 + (1- coefficient)*function2”?).
Thank you again,
Zhaozhou

Hi Zhaozhou,

“coefficient*function1 + (1- coefficient)*function2” . See the generalised case with N components in the documentation of the RooAddPdf class: root.cern.ch/doc/master/classRo … bb62b407c8

Cheers,
D