BornAgain  1.18.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 scattering at grazing incidence
4 //
5 //! @file Fit/RootAdapter/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), m_gradient_fun(gradient_fun), m_npars(npars),
22  m_datasize(ndatasize)
23 {
24 }
25 
27 {
28  return ROOT::Math::FitMethodFunction::kLeastSquare;
29 }
30 
31 ROOT::Math::IMultiGenFunction* RootResidualFunction::Clone() const
32 {
34 }
35 
36 //! Returns residual value for given data element index. Transform call of ancient
37 //! pointer based function to safer gradient_function_t.
38 //! @param pars: array of fit parameter values from the minimizer
39 //! @param index: index of residual element
40 //! @param gradients: if not zero, then array where we have to put gradients
41 //! @return value of residual for given data element index
42 
43 double RootResidualFunction::DataElement(const double* pars, unsigned int index,
44  double* gradients) const
45 {
46  std::vector<double> par_values;
47  par_values.resize(m_npars, 0.0);
48  std::copy(pars, pars + m_npars, par_values.begin());
49 
50  std::vector<double> par_gradients;
51 
52  if (gradients)
53  par_gradients.resize(m_npars);
54 
55  // retrieving result from user function
56  double result = m_gradient_fun(par_values, index, par_gradients);
57 
58  // packing result back to minimizer's array
59  if (gradients)
60  for (size_t i = 0; i < m_npars; ++i)
61  gradients[i] = par_gradients[i];
62 
63  return result;
64 }
65 
66 double RootResidualFunction::DoEval(const double* pars) const
67 {
68  std::vector<double> par_values;
69  par_values.resize(m_npars, 0.0);
70  std::copy(pars, pars + m_npars, par_values.begin());
71  return m_objective_fun(par_values);
72 }
std::function< double(const std::vector< double > &)> scalar_function_t
Definition: KernelTypes.h:28
std::function< double(const std::vector< double > &, unsigned int, std::vector< double > &)> gradient_function_t
Definition: KernelTypes.h:31
Declares class RootResidualFunction.
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.