Roofit does work with constant background

Dear everyone,

it is the first time that I am using roofit and thus it should be an error from my side.
I am trying to fit a peak sitting on a almost constant background.

What am I doing:
I read the data from an array (24 data points) and create a RooDataSet with x and y coordinates.
Then I try to fit the peak with a Gaussian and a constant line (pol 0).
However, the fit does not converge (even not when using the initial values gained by a binned standard root fit).
However, as soon as I remove the constant background from the data arrays (i.e. subtracting 4620 from the array y_help[i]), the fit does converge.

Another problem is, that the last bin in the plot seems to be empty even though the RooDataSet contains events for this bin. Maybe I am doing also something wrong in displaying the data.

I would be much obliged for any help and like to thank you in advance.

Here is my code:

//Data for the fit
double x_help[24]={3.866,3.8665,3.867,3.8675,3.868,3.8685,3.869,3.8695,3.87,3.8705,3.871,3.8715,3.872,3.8725,3.873,3.8735,3.874,3.8745,3.875,3.8755,3.876,3.8765,3.877,3.8775};
double y_help[24]={4625,4625,4620,4620,4625,4625,4640,4675,4750,4870,5025,5135,5150,5055,4920,4795,4720,4675,4655,4650,4640,4635,4635,4630};

int nbins=24;

double xmin=3.866;
double xmax=3.8775;

//Creating Roofit Variables
RooRealVar x("x","x",xmin,xmax);
RooRealVar y("y","y",0,10000);

RooDataSet data("data","dataset with x",
		RooArgSet(x, y),
		StoreError(RooArgSet(y))
		);

//Read the data from an array
 for(int bins=0;bins<nbins;bins++)
   {

     x.setVal(x_help[bins]);
     y.setVal(y_help[bins]);

     for(int a=0;a<y_help[bins];a++) data.add(RooArgSet(x,y)); //Add Data to tree!
     //for(int a=0;a<yval[bins];a++) data_h.add(RooArgSet(x,y)); //Add Data to histo     
   }

//Construct functions for fit
RooRealVar mean("mean","mean of gaussian",3.872,xmin,xmax) ;
RooRealVar sigma("sigma","width of gaussian",0.001,0.0,0.1) ;

// Build gaussian p.d.f in terms of x,mean and sigma 
RooGaussian gauss("gauss","gaussian PDF",x,mean,sigma);

//Define bg function
RooPolynomial pol0("pol0","pol0",x,RooArgList());

RooRealVar gauss_yield("gauss_yield","yield of gaussian",2500,0,5000); // startvalue, range low, range up
RooRealVar pol0_yield("pol0_yield","yield of polynom",100000,0,500000); // startvalue, range low, range up

RooAddPdf model("model","pol0+gauss",RooArgList(gauss,pol0),RooArgList(gauss_yield,pol0_yield));
model.fitTo(data);//Fit

//Draw the result
TCanvas *my_canvas_0 = new TCanvas("my_canvas_0", "",0);
RooPlot* xframe = x.frame(Bins(24),Title("Gaussian p.d.f. with data")) ;
data.plotOn(xframe) ;
model.plotOn(xframe);
xframe->Draw();

RooFit assumes that both the PDFs you are adding are normalized. In order for the sum of the two PDFs to be normalized, the sum of their coefficients must be 1. Thus, when adding N pdfs, you only need to provide n-1 coefficients.

When declaring gauss_yield, it should be a value between 0 and 1.
Change:

to:

double start_yield = 0.5; // set this to an appropriate value
RooRealVar gauss_yield("gauss_yield","yield of gaussian",start_yield,0,1);

Change:

To: RooAddPdf model("model","pol0+gauss",RooArgList(gauss,pol0),RooArgList(gauss_yield));

Dear Brian Amadio,

thank you very much for your answer.
However, the fit is still not converging - but at least a yield can be seen. The fitted width and yield is far
too small and also the hight of the constant background is not correct.
I also tried to use a Chebychev Polynom with one paparamter as backgroudn function, but still the same problem. The fit does not converge correctly.

I appreciate any help.
Thank you very much in advance.


/Data for the fit
double x_help[24]={3.866,3.8665,3.867,3.8675,3.868,3.8685,3.869,3.8695,3.87,3.8705,3.871,3.8715,3.872,3.8725,3.873,3.8735,3.874,3.8745,3.875,3.8755,3.876,3.8765,3.877,3.8775};
double y_help[24]={4625,4625,4620,4620,4625,4625,4640,4675,4750,4870,5025,5135,5150,5055,4920,4795,4720,4675,4655,4650,4640,4635,4635,4630};

int nbins=24;

double xmin=3.866;
double xmax=3.8775;

//Creating Roofit Variables
RooRealVar x("x","x",xmin,xmax);

RooDataSet data("data","dataset with x", RooArgSet(x)  );

//Read the data from an array
 for(int bins=0;bins<nbins;bins++)
   {
     x.setVal(x_help[bins]);
     for(int a=0;a<y_help[bins];a++) data.add(RooArgSet(x)); //Add Data to tree!
   }

//Construct functions for fit
RooRealVar mean("mean","mean of gaussian",3.8718,xmin,xmax) ;
RooRealVar sigma("sigma","width of gaussian",0.002,0.0,1.0) ;

// Build gaussian p.d.f in terms of x,mean and sigma
RooGaussian gauss("gauss","gaussian PDF",x,mean,sigma);

//Define bg function
RooPolynomial pol0("pol0","pol0",x,RooArgList());

double start_yield = 0.035; // set this to an appropriate value
RooRealVar gauss_yield("gauss_yield","yield of gaussian",start_yield,0.0,1); // startvalue, range low, range up

RooAddPdf model("model","pol0+gauss",RooArgList(gauss,pol0),RooArgList(gauss_yield));
model.fitTo(data);//Fit

//Draw the result
TCanvas *my_canvas_0 = new TCanvas("my_canvas_0", "",0);
RooPlot* xframe = x.frame(Bins(24),Title("Gaussian p.d.f. with data")) ;
data.plotOn(xframe) ;
model.plotOn(xframe);
xframe->Draw();