Suitable Algorithm for Retrying Fits?

Hi, I have a bit of an open-ended question. I am trying to fit a graph with a function, but the function comes from a functor that has a fixed parameter in the constructor. Other than that parameter, the function also has parameters that can be set through TF1::SetParameter. I would like to write a simple loop over plausible values of the fixed parameter, where at each loop iteration I perform a fit over the other parameters, then at the end I just use the best one.

To illustrate (in pseudo-python):

fixed_paramvalues = [1,2,3]
best_chi2 = None
for fp in fixed_paramvalues:
  myfunctor = ROOT.myfunctor(fp)
  f = ROOT.TF1("f",myfunctor,xmin,xmax,myfunctor.n_pars)
  ftr_p = mygraph.Fit(f,"S")
  chi2 = ftr_p.Get().Chi2()
  if best_chi2 is None or chi2 < best_chi2:
    best_chi2 = chi2
    best_f = f
# do stuff with best_f

Now after that loop I would like to plot mygraph with the best_f overlaid on top, but there seems to be a lot of implicit stuff happening in mygraph.Fit.

Can the experts give some general advice in a situation like this? Like should I be using the “N” option for fitting? Is my naive python loop breaking something fundamental on the ROOT side?


Your simple script should work fine, if not please let me know. You might want to use the option “Q” to avoid printing lots of output and maybe also option “N” (as you suggested) to not draw and store the fitted function.

Best Regards