Reading out fit status

I’m running a bunch of fits on a data set, defaulting to a Gaussian fit. However, sometimes that default fit fails to converge, in which case I would like to run a linear fit. I can’t seem to get the fit status to read out.

status = fit_hist.Fit("gaus","QS") print "Status? ", status if status==1: status = fit_hist.Fit("x++","S") print status

This returns:

How can I get a sensible output out of this.

Try: status.Status()

That produced a result, just not what I was looking for. Running in slightly more verbose mode I get:

FCN=4808 FROM HESSE     STATUS=FAILED         11 CALLS          55 TOTAL
                     EDM=0    STRATEGY= 1  ERROR MATRIX UNCERTAINTY 100.0 per cent
  EXT PARAMETER                APPROXIMATE        STEP         FIRST
  NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE
   1  Constant    -1.52751e+03   6.75459e-01  -6.47143e+01   0.00000e+00
   2  Mean         1.66445e+04   1.41421e+00   1.65371e+04   0.00000e+00
   3  Sigma        3.44089e-01   3.90810e+02  -5.93897e-01   0.00000e+00

But status.Status() is outputting 0 so my backup fit is never called.
So am I looking at the wrong value?

Hi,

a quick look over the code tells met that ‘IsValid()’ should be ‘False’ in case of fit failure. Beyond that, you’re better of asking in the “Stat and Math Tool Support” sub-forum.

Cheers,
Wim

Could you, please, try: status = fit_hist.Fit("gaus","VS") print "Status 1 ? ", status.Status() status = fit_hist.Fit("gaus","VS") print "Status 2 ? ", status.Status() I wonder if you maybe get a non-zero “Status 2”,

Big block of code here. I did IsValid() and Status() both came back True / 0. I also noticed a MinimizerType() function which the page says “Update the fit result with a new minimization status” which I played with a little, but haven’t been able to make different results appear. The MinimizerType() was not used in the block of code I’ve posted here:

[code] **********
** 1683 **SET PRINT 2



** 1684 **SET NOGRAD


PARAMETER DEFINITIONS:
NO. NAME VALUE STEP SIZE LIMITS
1 Constant 9.10623e+01 4.77619e-01 no limits
2 Mean 1.07437e+02 9.36248e+00 no limits
3 Sigma 5.59490e+01 3.03256e+01 0.00000e+00 5.59490e+02


** 1685 **SET ERR 1



** 1686 **SET PRINT 2



** 1687 **SET STR 1


NOW USING STRATEGY 1: TRY TO BALANCE SPEED AGAINST RELIABILITY


** 1688 **MIGRAD 1345 0.01


FIRST CALL TO USER FUNCTION AT NEW START POINT, WITH IFLAG=4.
START MIGRAD MINIMIZATION. STRATEGY 1. CONVERGENCE WHEN EDM .LT. 1.00e-05
FCN=21398.5 FROM MIGRAD STATUS=INITIATE 10 CALLS 11 TOTAL
EDM= unknown STRATEGY= 1 NO ERROR MATRIX
EXT PARAMETER CURRENT GUESS STEP FIRST
NO. NAME VALUE ERROR SIZE DERIVATIVE
1 Constant 9.10623e+01 4.77619e-01 4.77619e-01 6.14609e+02
2 Mean 1.07437e+02 9.36248e+00 9.36248e+00 -2.01411e+01
3 Sigma 5.59490e+01 3.03256e+01 1.87878e-01 6.09461e+04
NO ERROR MATRIX
FCN=4808 FROM MIGRAD STATUS=PROGRESS 32 CALLS 33 TOTAL
EDM=0 STRATEGY= 1 NO ERROR MATRIX
EXT PARAMETER CURRENT GUESS STEP FIRST
NO. NAME VALUE ERROR SIZE DERIVATIVE
1 Constant 2.63479e+01 4.77619e-01 -6.47143e+01 0.00000e+00
2 Mean 1.66445e+04 9.36248e+00 1.65371e+04 0.00000e+00
3 Sigma 3.44089e-01 3.03256e+01 -5.93897e-01 0.00000e+00
MIGRAD MINIMIZATION HAS CONVERGED.
MIGRAD WILL VERIFY CONVERGENCE AND ERROR MATRIX.
START COVARIANCE MATRIX CALCULATION.
MINUIT WARNING IN HESSE
============== Second derivative enters zero, param 1
MINUIT WARNING IN HESSE
============== Second derivative zero for parameter1
MNHESS FAILS AND WILL RETURN DIAGONAL MATRIX.
FCN=4808 FROM MIGRAD STATUS=CONVERGED 43 CALLS 44 TOTAL
EDM=0 STRATEGY= 1 ERROR MATRIX UNCERTAINTY 100.0 per cent
EXT PARAMETER APPROXIMATE STEP FIRST
NO. NAME VALUE ERROR SIZE DERIVATIVE
1 Constant 2.63479e+01 6.75455e-01 -6.47143e+01 0.00000e+00
2 Mean 1.66445e+04 1.41421e+00 1.65371e+04 0.00000e+00
3 Sigma 3.44089e-01 3.90810e+02 -5.93897e-01 0.00000e+00
EXTERNAL ERROR MATRIX. NDIM= 25 NPAR= 3 ERR DEF=1
4.562e-01 0.000e+00 0.000e+00
0.000e+00 2.000e+00 0.000e+00
0.000e+00 0.000e+00 3.848e+02
ERR MATRIX APPROXIMATE
PARAMETER CORRELATION COEFFICIENTS
NO. GLOBAL 1 2 3
1 0.00000 1.000 0.000 0.000
2 0.00000 0.000 1.000 0.000
3 0.00000 0.000 0.000 1.000
ERR MATRIX APPROXIMATE
Info in TMinuitMinimizer::Minimize: Finished to run MIGRAD - status 0


** 1689 **HESSE 1345


START COVARIANCE MATRIX CALCULATION.
MINUIT WARNING IN HESSE
============== Second derivative zero for parameter1
MNHESS FAILS AND WILL RETURN DIAGONAL MATRIX.
FCN=4808 FROM HESSE STATUS=FAILED 11 CALLS 55 TOTAL
EDM=0 STRATEGY= 1 ERROR MATRIX UNCERTAINTY 100.0 per cent
EXT PARAMETER APPROXIMATE INTERNAL INTERNAL
NO. NAME VALUE ERROR STEP SIZE VALUE
1 Constant -1.52751e+03 6.75455e-01 -6.47143e+01 2.63479e+01
2 Mean 1.66445e+04 1.41421e+00 1.65371e+04 1.66445e+04
3 Sigma 3.44089e-01 3.90810e+02 -5.93897e-01 -1.52119e+00
EXTERNAL ERROR MATRIX. NDIM= 25 NPAR= 3 ERR DEF=1
4.562e-01 0.000e+00 0.000e+00
0.000e+00 2.000e+00 0.000e+00
0.000e+00 0.000e+00 3.848e+02
ERR MATRIX APPROXIMATE
PARAMETER CORRELATION COEFFICIENTS
NO. GLOBAL 1 2 3
1 0.00000 1.000 0.000 0.000
2 0.00000 0.000 1.000 0.000
3 0.00000 0.000 0.000 1.000
ERR MATRIX APPROXIMATE
Info in TMinuitMinimizer::Minimize: Finished to run HESSE - status 0
EXTERNAL ERROR MATRIX. NDIM= 3 NPAR= 3 ERR DEF=1
4.562e-01 0.000e+00 0.000e+00
0.000e+00 2.000e+00 0.000e+00
0.000e+00 0.000e+00 3.848e+02
FCN=4808 FROM HESSE STATUS=FAILED 11 CALLS 55 TOTAL
EDM=0 STRATEGY= 1 ERROR MATRIX UNCERTAINTY 100.0 per cent
EXT PARAMETER PARABOLIC MINOS ERRORS
NO. NAME VALUE ERROR NEGATIVE POSITIVE
1 Constant -1.52751e+03 6.75455e-01
2 Mean 1.66445e+04 1.41421e+00
3 Sigma 3.44089e-01 3.90810e+02
Status1? True


** 1690 **SET PRINT 2



** 1691 **SET NOGRAD


PARAMETER DEFINITIONS:
NO. NAME VALUE STEP SIZE LIMITS
1 Constant 9.10623e+01 6.75455e-01 no limits
2 Mean 1.07437e+02 1.41421e+00 no limits
3 Sigma 5.59490e+01 3.90810e+02 0.00000e+00 5.59490e+02
MINUIT WARNING IN PARAMETR
============== VARIABLE3 BROUGHT BACK INSIDE LIMITS.


** 1692 **SET ERR 1



** 1693 **SET PRINT 2



** 1694 **SET STR 1


NOW USING STRATEGY 1: TRY TO BALANCE SPEED AGAINST RELIABILITY


** 1695 **MIGRAD 1345 0.01


FIRST CALL TO USER FUNCTION AT NEW START POINT, WITH IFLAG=4.
START MIGRAD MINIMIZATION. STRATEGY 1. CONVERGENCE WHEN EDM .LT. 1.00e-05
FCN=21398.5 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 Constant 9.10623e+01 6.75455e-01 6.75455e-01 6.14609e+02
2 Mean 1.07437e+02 1.41421e+00 1.41421e+00 -2.01233e+01
3 Sigma 5.59490e+01 3.90810e+02 1.10431e+00 6.09307e+04
NO ERROR MATRIX
FCN=4808 FROM MIGRAD STATUS=PROGRESS 34 CALLS 35 TOTAL
EDM=0 STRATEGY= 1 NO ERROR MATRIX
EXT PARAMETER CURRENT GUESS STEP FIRST
NO. NAME VALUE ERROR SIZE DERIVATIVE
1 Constant 2.63479e+01 6.75455e-01 -6.47143e+01 0.00000e+00
2 Mean 9.46690e+03 1.41421e+00 9.35946e+03 0.00000e+00
3 Sigma 3.41498e-01 3.90810e+02 -5.94085e-01 0.00000e+00
MIGRAD MINIMIZATION HAS CONVERGED.
MIGRAD WILL VERIFY CONVERGENCE AND ERROR MATRIX.
START COVARIANCE MATRIX CALCULATION.
MINUIT WARNING IN HESSE
============== Second derivative enters zero, param 1
MINUIT WARNING IN HESSE
============== Second derivative zero for parameter1
MNHESS FAILS AND WILL RETURN DIAGONAL MATRIX.
FCN=4808 FROM MIGRAD STATUS=CONVERGED 45 CALLS 46 TOTAL
EDM=0 STRATEGY= 1 ERROR MATRIX UNCERTAINTY 100.0 per cent
EXT PARAMETER APPROXIMATE STEP FIRST
NO. NAME VALUE ERROR SIZE DERIVATIVE
1 Constant 2.63479e+01 9.55237e-01 -6.47143e+01 0.00000e+00
2 Mean 9.46690e+03 1.41421e+00 9.35946e+03 0.00000e+00
3 Sigma 3.41498e-01 3.90837e+02 -5.94085e-01 0.00000e+00
EXTERNAL ERROR MATRIX. NDIM= 25 NPAR= 3 ERR DEF=1
9.125e-01 0.000e+00 0.000e+00
0.000e+00 2.000e+00 0.000e+00
0.000e+00 0.000e+00 3.819e+02
ERR MATRIX APPROXIMATE
PARAMETER CORRELATION COEFFICIENTS
NO. GLOBAL 1 2 3
1 0.00000 1.000 0.000 0.000
2 0.00000 0.000 1.000 0.000
3 0.00000 0.000 0.000 1.000
ERR MATRIX APPROXIMATE
Info in TMinuitMinimizer::Minimize: Finished to run MIGRAD - status 0


** 1696 **HESSE 1345


START COVARIANCE MATRIX CALCULATION.
MINUIT WARNING IN HESSE
============== Second derivative zero for parameter1
MNHESS FAILS AND WILL RETURN DIAGONAL MATRIX.
FCN=4808 FROM HESSE STATUS=FAILED 11 CALLS 57 TOTAL
EDM=0 STRATEGY= 1 ERROR MATRIX UNCERTAINTY 100.0 per cent
EXT PARAMETER APPROXIMATE INTERNAL INTERNAL
NO. NAME VALUE ERROR STEP SIZE VALUE
1 Constant -1.52751e+03 9.55237e-01 -6.47143e+01 2.63479e+01
2 Mean 9.46690e+03 1.41421e+00 9.35946e+03 9.46690e+03
3 Sigma 3.41498e-01 3.90837e+02 -5.94085e-01 -1.52138e+00
EXTERNAL ERROR MATRIX. NDIM= 25 NPAR= 3 ERR DEF=1
9.125e-01 0.000e+00 0.000e+00
0.000e+00 2.000e+00 0.000e+00
0.000e+00 0.000e+00 3.819e+02
ERR MATRIX APPROXIMATE
PARAMETER CORRELATION COEFFICIENTS
NO. GLOBAL 1 2 3
1 0.00000 1.000 0.000 0.000
2 0.00000 0.000 1.000 0.000
3 0.00000 0.000 0.000 1.000
ERR MATRIX APPROXIMATE
Info in TMinuitMinimizer::Minimize: Finished to run HESSE - status 0
EXTERNAL ERROR MATRIX. NDIM= 3 NPAR= 3 ERR DEF=1
9.125e-01 0.000e+00 0.000e+00
0.000e+00 2.000e+00 0.000e+00
0.000e+00 0.000e+00 3.819e+02
FCN=4808 FROM HESSE STATUS=FAILED 11 CALLS 57 TOTAL
EDM=0 STRATEGY= 1 ERROR MATRIX UNCERTAINTY 100.0 per cent
EXT PARAMETER PARABOLIC MINOS ERRORS
NO. NAME VALUE ERROR NEGATIVE POSITIVE
1 Constant -1.52751e+03 9.55237e-01
2 Mean 9.46690e+03 1.41421e+00
3 Sigma 3.41498e-01 3.90837e+02
Status2? True
[/code]

It seems to me that status.Status() should return an integer value, not a logical one -> see the Minimize method, for example.
BTW. “logical true” usually means a “non-zero value” (a “zero value” is a “logical false”).

[quote=“Wile E. Coyote”]It seems to me that status.Status() should return an integer value, not a logical one -> see the Minimize method, for example.
BTW. “logical true” usually means a “non-zero value”.[/quote]

Yes, Status() will always return 0 no matter the outcome of the fit. Which is odd, but I think 0 is intended to mean everything is fine, in this case.

In the output you show I can see:

Status1? True

Status2? True
so in both cases you get a NON-ZERO Status().

Switching from IsValid() to Status and stepping out of verbose mode

FCN=4808 FROM HESSE STATUS=FAILED 11 CALLS 55 TOTAL EDM=0 STRATEGY= 1 ERROR MATRIX UNCERTAINTY 100.0 per cent EXT PARAMETER APPROXIMATE STEP FIRST NO. NAME VALUE ERROR SIZE DERIVATIVE 1 Constant -1.52751e+03 6.75455e-01 -6.47143e+01 0.00000e+00 2 Mean 1.66445e+04 1.41421e+00 1.65371e+04 0.00000e+00 3 Sigma 3.44089e-01 3.90810e+02 -5.93897e-01 0.00000e+00 Status1? 0 FCN=4808 FROM HESSE STATUS=FAILED 11 CALLS 57 TOTAL EDM=0 STRATEGY= 1 ERROR MATRIX UNCERTAINTY 100.0 per cent EXT PARAMETER APPROXIMATE STEP FIRST NO. NAME VALUE ERROR SIZE DERIVATIVE 1 Constant -1.52751e+03 9.55237e-01 -6.47143e+01 0.00000e+00 2 Mean 9.46690e+03 1.41421e+00 9.35946e+03 0.00000e+00 3 Sigma 3.41498e-01 3.90837e+02 -5.94085e-01 0.00000e+00 Status2? 0

From root.cern.ch/root/html/TH1.html#TH1:Fit@1

Even though this is an old post, I could not find a good solution to this question on any forum.
So I am posting a solution I found years ago for other people in the same situation as me:
I was calling the Fit() method of the TGraphErrors/TH1/TH2… to perform the fit and I could not get the status as the output (verbose) and the status/isvalid did not match (always 0).
The key was to get the status from the global minuit instance performing the fit:

bool TestFitSuccess(bool verbose = false)
{
    std::string minuitstatus = std::string(gMinuit->fCstatu);
    if(minuitstatus.compare("CONVERGED ") != 0 && minuitstatus.compare("OK        ") != 0) //the spaces are important
    {
        if(verbose)
            std::cout << "  Minimization did not converge! (status_\"" << minuitstatus << "\")" << std::endl;
       return false;
    }
    else
        return true;
}