# Convert fitter to string

**URL:** <https://root-forum.cern.ch/t/convert-fitter-to-string/43137>\
**Category:** Newbie\
**Created:** [January 17, 2021, 11:18pm UTC](https://root-forum.cern.ch/t/convert-fitter-to-string/43137 "2021-01-17T23:18:55Z")\
**Posts on this page:** 8\
**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:** [January 17, 2021, 11:18pm UTC](https://root-forum.cern.ch/t/convert-fitter-to-string/43137/1 "2021-01-17T23:18:56Z")

</div>

Hi,  
I am wondering if there is a way to convert the results of a fit to a C++ string, like convert the following line:

```auto
auto fitr=hh->Fit(&ball,"SL","");

```

where hh is a TH1 pointer, and ball is a TF1 object. Something like

```auto
string x = auto fitr=hh->Fit(&ball,"SL","");

```

Is it possible, and if so how?  
\_ROOT Version: 6.23/01

---

<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:** [January 18, 2021, 6:40am UTC](https://root-forum.cern.ch/t/convert-fitter-to-string/43137/2 "2021-01-18T06:40:01Z")

</div>

What do you expect to get in that `string` ? `TH1::Fit` returns a  
[TFitResultPtr](https://root.cern/doc/master/classTFitResultPtr.html) I don’t that can be casted to a `string.

---

<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 18, 2021, 7:02pm UTC](https://root-forum.cern.ch/t/convert-fitter-to-string/43137/3 "2021-01-18T19:02:57Z")

</div>

I would expect to get a string containing the text of the fit that is printed to the screen when Fit is called.  
Something like

```auto
string x = "FCN=1005.87 FROM MIGRAD STATUS=CONVERGED 278 CALLS 279 TOTAL
                     EDM=1.94984e-07 STRATEGY= 1 ERROR MATRIX UNCERTAINTY   
                     4.7 per cent
  EXT PARAMETER STEP FIRST   
  NO. NAME VALUE ERROR SIZE DERIVATIVE 
   1 p0 4.55485e+04 2.93626e+01 4.22959e-02 -1.96629e-04
   2 p1 1.28116e+01 6.95203e-03 1.14863e-06 5.26017e-02
   3 p2 4.50409e-01 1.28498e-04 -1.82278e-07 2.09336e+01
   4 p3 1.76940e+00 4.94606e-06 7.74180e-10 5.44901e+01
   5 p4 2.84730e-01 2.28220e-05 3.31556e-08 3.63427e+02
   6 p5 2.85412e+04 4.22895e+03 -8.20212e+00 2.68426e-07
   7 p6 6.35570e-02 5.24217e-03 2.16981e-03 2.52700e-03
   8 p7 3.40204e-02 2.48912e-03 -8.67748e-06 3.19052e-01
                               ERR DEF= 0.5"

```

---

<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:** [January 19, 2021, 9:53am UTC](https://root-forum.cern.ch/t/convert-fitter-to-string/43137/4 "2021-01-19T09:53:27Z")

</div>

That will not work.  
May be @moneta knows how to put this printout in a file (from a macro).

---

<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 21, 2021, 4:05am UTC](https://root-forum.cern.ch/t/convert-fitter-to-string/43137/5 "2021-01-21T04:05:32Z")

</div>

Yes, that would be just as useful

---

<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 25, 2021, 3:58pm UTC](https://root-forum.cern.ch/t/convert-fitter-to-string/43137/6 "2021-01-25T15:58:09Z")

</div>

Hi,

You can do it as following:

```auto
auto fitr=hh->Fit(&ball,"SL","");
auto fitResult = static_cast<ROOT::Fit::FitResult *>(fitr.Get());
std::ostringstream resultStream;
fitResult->Print(resultStream);
std::string resultString = resultStream.str();

```

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:** [January 27, 2021, 12:17am UTC](https://root-forum.cern.ch/t/convert-fitter-to-string/43137/7 "2021-01-27T00:17:36Z")

</div>

Thanks. This is nice, but it makes the Print() method output into a string, I was initially looking for a way to put the output of a call to Fit into a string, so I could search it for “CONVERGED”. Well, it turns out that there is an easier way, which I display below, where hh is a TH1 pointer

```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:34am UTC](https://root-forum.cern.ch/t/convert-fitter-to-string/43137/8 "2021-01-27T11:34:33Z")

</div>

See [If status=converged break - #6 by moneta](https://root-forum.cern.ch/t/if-status-converged-break/41782/6)
