Fitting to user defined function that incorporates TRandom

Hello! I’m hoping someone more familiar with ROOT’s TMinuit fitting mechanism may be able to help with a problem I’ve encountered. I am trying to use TMinuit’s default Chi^2 minimization fit to a TGraph using a user defined TF1. The TF1 I want to use incorporates some uncertainties by selecting a value from a gaussian constructed through ROOT’s TRandom2 Gaus(double mean, double sigma). I have found that TMinuit is able to successfully find a fit if I instead make this random number fixed. Trying to use the random number generator within the TF1 construction, however, causes the fit to fail with the complaint:
Abnormal Termination of minimization.
Invalid FitResult (status = 4)

Reading up a bit, I gather that status = 4 indicates that MIGRAD did not converge. I have tried the recommended solutions to this problem such as decreasing precision (even to 1) or changing the minimization strategy (ROOT::Math::MinimizerOptions::SetDefaultStrategy(0) ) or increasing the maximum number of iterations all with no effect.

Attached is the code. Note that in the TF1 constructor method “double OscSmear” the random number I want to use is labeled “Ej,” and it can easily be replaced by the fixed “Eo” in the calculation of “P_current” with the result that the fit will then converge. I’m using ROOT version 5.34/02 if that makes any difference.

Thanks so much for any advice or help you can offer!
fit_test_f.cpp (2.22 KB)

The problem is that each call to “OscSmear” (with exactly the same “L_E[0]”, “par[0]” and “par[1]”) will return a different value. This fools the minimizing procedure (try with sig = 1e-6 or smaller in order to make these return value differences “acceptable”).