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

**URL:** <https://root-forum.cern.ch/t/roofit-model-with-user-defined-functions-nan-in-specific-x-values/64502>\
**Category:** Newbie\
**Tags:** roofit\
**Created:** [December 2, 2025, 11:56am UTC](https://root-forum.cern.ch/t/roofit-model-with-user-defined-functions-nan-in-specific-x-values/64502 "2025-12-02T11:56:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![\_pinkhaze](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/_pinkhaze/32/39561_2.png) [@\_pinkhaze](https://root-forum.cern.ch/u/_pinkhaze)\
**Post date:** [December 2, 2025, 11:56am UTC](https://root-forum.cern.ch/t/roofit-model-with-user-defined-functions-nan-in-specific-x-values/64502/1 "2025-12-02T11:56:00Z")

</div>

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.

```cpp
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

* * *

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [December 2, 2025, 2:35pm UTC](https://root-forum.cern.ch/t/roofit-model-with-user-defined-functions-nan-in-specific-x-values/64502/2 "2025-12-02T14:35:27Z")

</div>

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
