RooFit: model with user defined functions, NaN in specific x values

Dear Root experts

I am trying to fit some peaks + background in a TH1 using a function I wrote in RooFit. I was able to fit my TH1 with this same function using the Fit method of ROOT, but when doing it with RooFit, my code crashes after the parameters converge. I think the main problem is that in certain x values, the fitted value becomes NaN, however, how can I go around this and still fit the desired range? I did not have this problem using a TF1 and the Fit method.

RooRealVar x("x","Channels",73000,75950);

RooDataHist dh("dh", "dh", RooArgList(x), hEnergy); // hEnergy is the TH1 I want to fit
  
RooRealVar tau1("tau1", "tau1", 90, 10, 500);
RooRealVar sigma1("sigma1", "sigma1", 50, 10, 500);
RooRealVar mu1("mu1", "mu1", 75000, 74900, 75210);
RooRealVar y1("y1", "y1", 0.02, 0., 1.);

RooRealVar tau2("tau2", "tau2", 90, 10, 300);
RooRealVar sigma2("sigma2", "sigma2", 50, 10, 300);
RooRealVar mu2("mu2", "mu2", 75600, 75500, 75710);
RooRealVar y2("y2", "y2", 0.06, 0., 1.);

TString sForm =  "1 / (2*@1) * exp((@0-@2) / @1 + @3*@3 / (2*@1*@1)) * erfc(((@0-@2) / @3 + @3 / @1) / sqrt(2))"; //fitting function for peaks

RooGenericPdf funcPdf1("pdf1", sForm, RooArgList(x, tau1, mu1, sigma1)); //peak 1
RooGenericPdf funcPdf2("pdf2", sForm, RooArgList(x, tau2, mu2, sigma2)); //peak 2

RooRealVar taubkg("taubkg", "taubkg", -0.01, -1.0, 0.0);
RooExponential bkg("bkg", "bkg", x, taubkg); //exponential function for background
RooRealVar ybkg("ybkg","ybkg",1e-5,0,1.);

RooAddPdf model("model", "model", RooArgList(funcPdf1, funcPdf2, bkg), RooArgList(y1, y2, ybkg));

RooFitResult* result = model.fitTo(dh, PrintLevel(1), SumW2Error(kFALSE), Minimizer("Minuit2", "migrad"), Extended(kFALSE)); 

result->Print("v");


ROOT Version: 6.34.04
Platform: Linux


If you share your histogram hEnergy as a .root file, we might be able to help you further

Also, consider retrying with ROOT 6.38 to see if the crash disappears