Logo ROOT  
Reference Guide
FumiliFCNAdapter.h
Go to the documentation of this file.
1// @(#)root/minuit2:$Id$
2// Author: L. Moneta 10/2006
3
4/**********************************************************************
5 * *
6 * Copyright (c) 2006 ROOT Foundation, CERN/PH-SFT *
7 * *
8 **********************************************************************/
9
10#ifndef ROOT_Minuit2_FumiliFCNAdapter
11#define ROOT_Minuit2_FumiliFCNAdapter
12
14
16
17#include "Minuit2/MnPrint.h"
18
19// #ifndef ROOT_Math_Util
20// #include "Math/Util.h"
21// #endif
22
23#include <cmath>
24#include <cassert>
25#include <vector>
26
27namespace ROOT {
28
29namespace Minuit2 {
30
31/**
32
33
34template wrapped class for adapting to FumiliFCNBase signature
35
36@author Lorenzo Moneta
37
38@ingroup Minuit
39
40*/
41
42template <class Function>
44
45public:
46 // typedef ROOT::Math::FitMethodFunction Function;
47 typedef typename Function::Type_t Type_t;
48
49 FumiliFCNAdapter(const Function &f, unsigned int ndim, double up = 1.) : FumiliFCNBase(ndim), fFunc(f), fUp(up) {}
50
51 ~FumiliFCNAdapter() override {}
52
53 double operator()(const std::vector<double> &v) const override { return fFunc.operator()(&v[0]); }
54 double operator()(const double *v) const { return fFunc.operator()(v); }
55 double Up() const override { return fUp; }
56
57 void SetErrorDef(double up) override { fUp = up; }
58
59 // virtual std::vector<double> Gradient(const std::vector<double>&) const;
60
61 // forward interface
62 // virtual double operator()(int npar, double* params,int iflag = 4) const;
63
64 /**
65 evaluate gradient hessian and function value needed by fumili
66 */
67 void EvaluateAll(const std::vector<double> &v) override;
68
69private:
70 // data member
71
73 double fUp;
74};
75
76template <class Function>
77void FumiliFCNAdapter<Function>::EvaluateAll(const std::vector<double> &v)
78{
79 MnPrint print("FumiliFCNAdaptor");
80
81 // typedef FumiliFCNAdapter::Function Function;
82
83 // evaluate all elements
84 unsigned int npar = Dimension();
85 if (npar != v.size())
86 print.Error("npar", npar, "v.size()", v.size());
87 assert(npar == v.size());
88 // must distinguish case of likelihood or LS
89
90 std::vector<double> &grad = Gradient();
91 std::vector<double> &hess = Hessian();
92 // reset
93 assert(grad.size() == npar);
94 grad.assign(npar, 0.0);
95 hess.assign(hess.size(), 0.0);
96
97 unsigned int ndata = fFunc.NPoints();
98
99 std::vector<double> gf(npar);
100
101 // loop on the data points
102
103 // assume for now least-square
104 if (fFunc.Type() == Function::kLeastSquare) {
105
106 for (unsigned int i = 0; i < ndata; ++i) {
107 // calculate data element and gradient
108 double fval = fFunc.DataElement(&v.front(), i, &gf[0]);
109
110 for (unsigned int j = 0; j < npar; ++j) {
111 grad[j] += 2. * fval * gf[j];
112 for (unsigned int k = j; k < npar; ++k) {
113 int idx = j + k * (k + 1) / 2;
114 hess[idx] += 2.0 * gf[j] * gf[k];
115 }
116 }
117 }
118 } else if (fFunc.Type() == Function::kLogLikelihood) {
119
120 for (unsigned int i = 0; i < ndata; ++i) {
121
122 // calculate data element and gradient
123 fFunc.DataElement(&v.front(), i, &gf[0]);
124
125 for (unsigned int j = 0; j < npar; ++j) {
126 double gfj = gf[j];
127 grad[j] -= gfj;
128 for (unsigned int k = j; k < npar; ++k) {
129 int idx = j + k * (k + 1) / 2;
130 hess[idx] += gfj * gf[k];
131 }
132 }
133 }
134 } else {
135 print.Error("Type of fit method is not supported, it must be chi2 or log-likelihood");
136 }
137}
138
139} // end namespace Minuit2
140
141} // end namespace ROOT
142
143#endif // ROOT_Minuit2_FCNAdapter
#define f(i)
Definition: RSha256.hxx:104
Double_t(* Function)(Double_t)
Definition: Functor.C:4
template wrapped class for adapting to FumiliFCNBase signature
double Up() const override
Error definition of the function.
double operator()(const double *v) const
void SetErrorDef(double up) override
add interface to set dynamically a new error definition Re-implement this function if needed.
void EvaluateAll(const std::vector< double > &v) override
evaluate gradient hessian and function value needed by fumili
double operator()(const std::vector< double > &v) const override
The meaning of the vector of parameters is of course defined by the user, who uses the values of thos...
FumiliFCNAdapter(const Function &f, unsigned int ndim, double up=1.)
Extension of the FCNBase for the Fumili method.
Definition: FumiliFCNBase.h:46
void Error(const Ts &... args)
Definition: MnPrint.h:120
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.