BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
RootResidualFunction.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Fit/Adapter/RootResidualFunction.cpp
6 //! @brief Implements class RootResidualFunction.
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
16 
18  gradient_function_t gradient_fun, size_t npars,
19  size_t ndatasize)
20  : ROOT::Math::FitMethodFunction(static_cast<int>(npars), static_cast<int>(ndatasize))
21  , m_objective_fun(objective_fun)
22  , m_gradient_fun(gradient_fun)
23  , m_npars(npars)
24  , m_datasize(ndatasize)
25 {
26 }
27 
29 {
30  return ROOT::Math::FitMethodFunction::kLeastSquare;
31 }
32 
33 ROOT::Math::IMultiGenFunction* RootResidualFunction::Clone() const
34 {
36 }
37 
38 //! Returns residual value for given data element index. Transform call of ancient
39 //! pointer based function to safer gradient_function_t.
40 //! @param pars: array of fit parameter values from the minimizer
41 //! @param index: index of residual element
42 //! @param gradients: if not zero, then array where we have to put gradients
43 //! @return value of residual for given data element index
44 
45 double RootResidualFunction::DataElement(const double* pars, unsigned int index,
46  double* gradients) const
47 {
48  std::vector<double> par_values;
49  par_values.resize(m_npars, 0.0);
50  std::copy(pars, pars + m_npars, par_values.begin());
51 
52  std::vector<double> par_gradients;
53 
54  if (gradients)
55  par_gradients.resize(m_npars);
56 
57  // retrieving result from user function
58  double result = m_gradient_fun(par_values, index, par_gradients);
59 
60  // packing result back to minimizer's array
61  if (gradients)
62  for (size_t i = 0; i < m_npars; ++i)
63  gradients[i] = par_gradients[i];
64 
65  return result;
66 }
67 
68 double RootResidualFunction::DoEval(const double* pars) const
69 {
70  std::vector<double> par_values;
71  par_values.resize(m_npars, 0.0);
72  std::copy(pars, pars + m_npars, par_values.begin());
73  return m_objective_fun(par_values);
74 }
Declares class RootResidualFunction.
std::function< double(const std::vector< double > &)> scalar_function_t
Definition: Types.h:30
std::function< double(const std::vector< double > &, unsigned int, std::vector< double > &)> gradient_function_t
Definition: Types.h:33
double DoEval(const double *pars) const override
evaluation of chi2
Type_t Type() const override
double DataElement(const double *pars, unsigned int index, double *gradients=0) const override
Evaluation of single data element residual. Will be called by ROOT minimizer.
scalar_function_t m_objective_fun
User function to get value to minimizer.
ROOT::Math::BasicFitMethodFunction< ROOT::Math::IMultiGenFunction >::Type_t Type_t
RootResidualFunction(scalar_function_t objective_fun, gradient_function_t gradient_fun, size_t npars, size_t ndatasize)
Constructs RootResidualFunction.
ROOT::Math::IMultiGenFunction * Clone() const override
gradient_function_t m_gradient_fun
User function to get residual and gradients.
Various mathematical functions.
Definition: Bessel.h:26