Home | News | Documentation | Download

Convert fitter to string

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 fitr=hh->Fit(&ball,"SL","");

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

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

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

What do you expect to get in that string ? TH1::Fit returns a
TFitResultPtr I don’t that can be casted to a `string.

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

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"

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

Yes, that would be just as useful

Hi,

You can do it as following:

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

1 Like

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

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;
    }
}

See If status=converged break - #6 by moneta

1 Like