# If status=converged break

**URL:** <https://root-forum.cern.ch/t/if-status-converged-break/41782>\
**Category:** Newbie\
**Created:** [October 9, 2020, 1:47am UTC](https://root-forum.cern.ch/t/if-status-converged-break/41782 "2020-10-09T01:47:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Edward\_Finkelstein](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/edward_finkelstein/32/20070_2.png) [@Edward\_Finkelstein](https://root-forum.cern.ch/u/Edward_Finkelstein)\
**Post date:** [October 9, 2020, 1:47am UTC](https://root-forum.cern.ch/t/if-status-converged-break/41782/1 "2020-10-09T01:47:34Z")

</div>

Hello,  
Is there a way to break a for loop if the fitter status=converged, and if so, how is it coded? I have some code below, where hh is a TH1 object and ball is a TF1. How would I break the loop when status=converged?

```auto
for (int i=0; i<100; i++)
    {
    hh->Fit(&ball,"SL","");
    }

```

* * *

_ROOT Version:_ 6.23/01  
_Platform:_ Not Provided  
_Compiler:_ Not Provided

* * *

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [October 9, 2020, 7:15am UTC](https://root-forum.cern.ch/t/if-status-converged-break/41782/2 "2020-10-09T07:15:44Z")

</div>

The question is: _“is there a way to know if a fit converged or not ?”_. I am sure @moneta knows.

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [October 9, 2020, 7:42am UTC](https://root-forum.cern.ch/t/if-status-converged-break/41782/3 "2020-10-09T07:42:14Z")

</div>

Hi,

you can check the status returned in FitResult, for example:

```auto
for (int i=0; i<100; i++) {
    auto result = hh->Fit(&ball,"SL","");
    if (result == 0) break;
 }

```

Lorenzo

---

<div class="post-metadata">

**Author:** ![Edward\_Finkelstein](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/edward_finkelstein/32/20070_2.png) [@Edward\_Finkelstein](https://root-forum.cern.ch/u/Edward_Finkelstein)\
**Post date:** [October 13, 2020, 10:25pm UTC](https://root-forum.cern.ch/t/if-status-converged-break/41782/4 "2020-10-13T22:25:23Z")

</div>

Thank you moneta!

---

<div class="post-metadata">

**Author:** ![Edward\_Finkelstein](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/edward_finkelstein/32/20070_2.png) [@Edward\_Finkelstein](https://root-forum.cern.ch/u/Edward_Finkelstein)\
**Post date:** [January 27, 2021, 12:49am UTC](https://root-forum.cern.ch/t/if-status-converged-break/41782/5 "2021-01-27T00:49:15Z")

</div>

In hindsight to this post, the following was more what I was looking for:

```auto
int i = 1;
while(true)
{
    cout << i << endl;
    auto fitr = hh->Fit(&ball,"SL","");
    fitr->Print();
    i++;
    if(strncmp(gMinuit->fCstatu.Data(),"CONVERGED",9)==0)
    {
        break;
    }
}

```

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [January 27, 2021, 11:33am UTC](https://root-forum.cern.ch/t/if-status-converged-break/41782/6 "2021-01-27T11:33:29Z")

</div>

Note that the above is valid only when using TMinuit and not Minuit2.  
A more general solution is to use the return valued of `FitResult::Status()` or better what is returned by `FitResult::IsValid()`. The above lines should be then:

```auto
for (int i=0; i<100; i++) {
    auto result = hh->Fit(&ball,"SL","");
    if (result->IsValid() ) break;
 }

```

Note that In case of TMinuit when `gMinuit->fCstatu="CONVERGED"` , the return status is always zero and the FitResult is flagged as valid.  
If the fit failed instead the status is equal to 4 when using TMinuit.  
In case of Minuit2 you have different status values depending on the reason of fit failures.  
See [`Minuit2Minimizer::Minimize()`](https://root.cern/doc/master/classROOT_1_1Minuit2_1_1Minuit2Minimizer.html#a8f72504b2afaabf666601a44e50a79d6).  
In this case when the return status is 0 or 1, the fit can be considered a valid fit, while for status\>1 the fit is invalid.

Lorenzo
