ROOT check for fit convergence

Hey there,

I would like to fit TH1D histograms with TF1 functions, but I don’t quite know, what parameters to use as starting values in order to achieve convergence. My approach is to somehow randomize the parameter starting values and try fitting multiple times. I don’t know though, how to find out if one of the functions I fit converged or not?

For example:

   function -> SetParameter(0, rand() % 40 * 0.5 + 0.1);
   function -> SetParameter(1, rand() % 5000 + 1);
   function -> SetParameter(2, rand() % 500 * 0.01 + 0.1s);
   // (...)
   histogram -> Fit("functionName", "M");
   function -> HasAccuratelyConverged(); // <--- Looking for this kind of functionality

Is there a straightforward approach for this problem? Should I capture the message printed by the fitter algorithm, and search for the word “CONVERGED”?

Adam Hunyadi

Hi Adam,

you’ll want to do something like

TFitResultPtr fp = histogram->Fit("functionName", "M");

You’ll then have an object that represents a pointer to TFitResult, which is in turn a ROOT::Fit::FitResult. The latter object exposes the return codes of the underlying minimiser with the status() method, and will most likely fill all your needs when you’re trying to see how your fit went.

One other remark: taking the remainder of an integer division of a return value of rand() is a bad idea in many implementations, as this may yield non-random bits, especially on systems where rand() is implemented with a linear congruential generator (LCG), which is what many (most?) C standard libraries do. If you have to use rand, it’s much better to use something like this:

function->SetParameter(0, double(rand()) / double(RAND_MAX) * 40. * .5 + 0.1);

It’s even better to use one of the ROOT-supplied random number generators - for everyday work, I recommend TRandom3.



1 Like

Search for “fit result” and “fit status” in the TH1::Fit method description.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.