[SOLVED] Problem with fit inside a loop

Hello, I’m trying to fit many TGraphErrors inside a loop. I get strange results, in the sense that some fit fail giving me nan as fitted parameters, and this happens in a completely random fashion: a fit may fail once, but if I start the loop again from the beginning that fit could success, and another one who previously was carried out succesfully may fail.
I tried to call Fit with V option, and this is where things go wrong:

[code]


** 28 **SET ERR 1


PARAMETER DEFINITIONS:
NO. NAME VALUE STEP SIZE LIMITS
1 p0 3.00000e+00 9.00000e-01 no limits
2 p1 7.50000e+01 2.25000e+01 no limits
3 p2 1.60000e+02 4.80000e+01 no limits


** 29 **SET PRINT 2



** 30 **MIGRAD 5000 5.525e-06


FIRST CALL TO USER FUNCTION AT NEW START POINT, WITH IFLAG=4.
START MIGRAD MINIMIZATION. STRATEGY 1. CONVERGENCE WHEN EDM .LT. 5.53e-09
FCN=2626.19 FROM MIGRAD STATUS=INITIATE 12 CALLS 13 TOTAL
EDM= unknown STRATEGY= 1 NO ERROR MATRIX
EXT PARAMETER CURRENT GUESS STEP FIRST
NO. NAME VALUE ERROR SIZE DERIVATIVE
1 p0 3.00000e+00 9.00000e-01 9.00000e-01 1.14047e+03
2 p1 7.50000e+01 2.25000e+01 2.25000e+01 -1.17534e+02
3 p2 1.60000e+02 4.80000e+01 4.80000e+01 -2.73817e+01
NO ERROR MATRIX
FCN=568.96 FROM MIGRAD STATUS=PROGRESS 20 CALLS 21 TOTAL
EDM=1730.91 STRATEGY= 1 NO ERROR MATRIX
EXT PARAMETER CURRENT GUESS STEP FIRST
NO. NAME VALUE ERROR SIZE DERIVATIVE
1 p0 2.85474e+00 9.00000e-01 -1.45255e-01 2.03371e+03
2 p1 8.88307e+01 2.25000e+01 1.38307e+01 1.59401e+01
3 p2 2.44846e+02 4.80000e+01 8.48462e+01 8.83290e-01
MIGRAD MINIMIZATION HAS CONVERGED.
MIGRAD WILL VERIFY CONVERGENCE AND ERROR MATRIX.
START COVARIANCE MATRIX CALCULATION.
EIGENVALUES OF SECOND-DERIVATIVE MATRIX:
1.5178e-01 8.4599e-01 2.0022e+00
COVARIANCE MATRIX CALCULATED SUCCESSFULLY
FCN=98.3239 FROM MIGRAD STATUS=CONVERGED 53 CALLS 54 TOTAL
EDM=2.07115e-20 STRATEGY= 1 ERROR MATRIX ACCURATE
EXT PARAMETER STEP FIRST
NO. NAME VALUE ERROR SIZE DERIVATIVE
1 p0 2.33275e+00 2.50499e-02 9.66311e-05 9.92675e-09
2 p1 9.78149e+01 1.17330e+00 2.93720e-03 1.93530e-10
3 p2 2.18916e+02 5.27438e+00 1.50723e-02 7.07134e-12
EXTERNAL ERROR MATRIX. NDIM= 25 NPAR= 3 ERR DEF=1
6.275e-04 -1.747e-02 5.237e-02
-1.747e-02 1.377e+00 -4.970e+00
5.237e-02 -4.970e+00 2.782e+01
PARAMETER CORRELATION COEFFICIENTS
NO. GLOBAL 1 2 3
1 0.60960 1.000 -0.594 0.396
2 0.85753 -0.594 1.000 -0.803
3 0.80942 0.396 -0.803 1.000
FCN=98.3239 FROM MIGRAD STATUS=CONVERGED 53 CALLS 54 TOTAL
EDM=2.07115e-20 STRATEGY= 1 ERROR MATRIX ACCURATE
EXT PARAMETER STEP FIRST
NO. NAME VALUE ERROR SIZE DERIVATIVE
1 p0 2.33275e+00 2.50499e-02 9.66311e-05 9.92675e-09
2 p1 9.78149e+01 1.17330e+00 2.93720e-03 1.93530e-10
3 p2 2.18916e+02 5.27438e+00 1.50723e-02 7.07134e-12
p0: 2.33275
p1: 97.8149
p2: 218.916
Efficiency: 90%


** 31 **SET ERR 1


PARAMETER DEFINITIONS:
NO. NAME VALUE STEP SIZE LIMITS
1 p0 8.00000e+00 2.40000e+00 no limits
2 p1 1.30000e+02 3.90000e+01 no limits
3 p2 5.00000e+02 1.50000e+02 no limits


** 32 **SET PRINT 2



** 33 **MIGRAD 5000 3.458e-05


FIRST CALL TO USER FUNCTION AT NEW START POINT, WITH IFLAG=4.
START MIGRAD MINIMIZATION. STRATEGY 1. CONVERGENCE WHEN EDM .LT. 3.46e-08
FCN=-nan FROM MIGRAD STATUS=INITIATE 109 CALLS 110 TOTAL
EDM= unknown STRATEGY= 1 NO ERROR MATRIX
EXT PARAMETER CURRENT GUESS STEP FIRST
NO. NAME VALUE ERROR SIZE DERIVATIVE
1 p0 8.00000e+00 2.40000e+00 0.00000e+00 -nan
2 p1 1.30000e+02 3.90000e+01 0.00000e+00 -nan
3 p2 5.00000e+02 1.50000e+02 0.00000e+00 -nan
NO ERROR MATRIX [/code]
Migrad call number 30 correctly initializes the fit, and everything goes fine; but next call, number 33, it seems to incorrectly initialize the fit. FCN is set to nan, but I do exactly the same operations as in the previous case, since I do this inside a loop. And as I said, this happens randomly at some loop stage.
Do someone have hints about what’s happening? I can post some details (code, dumps) if it would be useful. By the way, I’m using root 5.20.
Thanks to everyone.

Hi ,
the nan might be caused by your fitting function. You should check well the function and eventually you need to post your code so we can reproduce the problem

Best Regards

Lorenzo

Thanks, Lorenzo. I think I found the bug in my code: I was fitting projections of a 2D histo, and I computed these projections inside a loop, without deleting the projection at the end of the loop. This maybe caused some confusion because I ended with many projections with the same name.
Now the nan doesn’t appear anymore (at least, at first inspection).