# How To Correctly Use ROOT::Fit::Chi2FCN?

**URL:** <https://root-forum.cern.ch/t/how-to-correctly-use-root-fit-chi2fcn/16493>\
**Category:** ROOT\
**Created:** [August 7, 2013, 8:11am UTC](https://root-forum.cern.ch/t/how-to-correctly-use-root-fit-chi2fcn/16493 "2013-08-07T08:11:52Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![mweber15](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/e19b73/32.png) [@mweber15](https://root-forum.cern.ch/u/mweber15)\
**Post date:** [August 7, 2013, 8:11am UTC](https://root-forum.cern.ch/t/how-to-correctly-use-root-fit-chi2fcn/16493/1 "2013-08-07T08:11:52Z")

</div>

Dear all,

in order to perform a multi-parametric chi2 fit of my binned model function f(x\_i,p) to a binned data y\_i histogram I would like to apply the fit method “`GSLMultiFit`” (The reason is that using other standard minimizer, like `Minuit`, gives poor convergence in my case when starting slightly away from the expected minimum p\_min). The `GSLMultiFit` method can be initialized, [for example](http://root.cern.ch/drupal/content/numerical-minimization), via  
`ROOT::Math::Minimizer* min = ROOT::Math::Factory::CreateMinimizer("GSLMultiFit", "");`  
I understand that this minimizing routine does not work by handing over a common user-derived fit function of type `ROOT::Math::IBaseFunctionMultiDim` but instead requires a function of class type [`ROOT::Fit::Chi2FCN`](http://root.cern.ch/root/html/ROOT __Fit__ Chi2FCN_-p0Math__IBaseFunctionMultiDim_.html). This class provides a public method named  
`virtual double DataElement(const double* x, unsigned int i, double* g)`  
which can be used to specify – to my understanding – the i-th term (residual) of the chi2 sum (or LogL function, when performing a LogL fit).

Now comes my problem: In my case, the model function f(x\_i,p) is not given by any analytical expression. The histogram f(x\_i), where x\_i denotes the i-th model bin, is computed by a rather complex signal MC simulation (detector response function) and, of course, depends on the fit parameters p. Therefore, I cannot afford to have the model function f be called whenever a single residual i is computed (data and fit histograms have 50 bins, so there are 50 summands in the chi2 sum). Instead, what I would like to do in every minimization step, is compute the entire model histogram f(x\_i,p) just once for given p and then sum over all 50 residuals to compute only once the obtained chi2 (afterwards repeat until a minimum chi2 is reached).

Is there anyone having experience with this kind of problem? Is there a way to have the entire model histogram f(x\_i) be simulated first and afterwards accessed within “`DataElement(..)`” to provide the i-th residual (f(x\_i)-y\_i)^2/y\_i ? How does the private member function “`DoEval()`” play a role in this class (when is it called during the actual minimzation process)? Can it be used to achieve what I need (mind that `DoEval() const` is a “`const`” function thus it cannot modify class members)? I know there is one example code provided [this post](https://root-forum.cern.ch/t/locate-like-command/65/1) but I don’t see how it would help solving my particular problem.

Thanks for your support,  
cheers

Marc
