10 #ifndef ROOT_Minuit2_FumiliFCNAdapter
11 #define ROOT_Minuit2_FumiliFCNAdapter
40 template<
class Function>
41 class FumiliFCNAdapter :
public FumiliFCNBase {
46 typedef typename Function::Type_t Type_t;
48 FumiliFCNAdapter(
const Function & f,
unsigned int ndim,
double up = 1.) :
49 FumiliFCNBase( ndim ),
54 ~FumiliFCNAdapter() {}
57 double operator()(
const std::vector<double>& v)
const {
58 return fFunc.operator()(&v[0]);
60 double operator()(
const double * v)
const {
61 return fFunc.operator()(v);
63 double Up()
const {
return fUp;}
65 void SetErrorDef(
double up) { fUp = up; }
76 void EvaluateAll(
const std::vector<double> & v);
83 const Function & fFunc;
88 template<
class Function>
89 void FumiliFCNAdapter<Function>::EvaluateAll(
const std::vector<double> & v) {
94 unsigned int npar = Dimension();
95 if (npar != v.size() ) std::cout <<
"npar = " << npar <<
" " << v.size() << std::endl;
96 assert(npar == v.size());
99 std::vector<double> & grad = Gradient();
100 std::vector<double> & hess = Hessian();
102 assert(grad.size() == npar);
103 grad.assign( npar, 0.0);
104 hess.assign( hess.size(), 0.0);
107 unsigned int ndata = fFunc.NPoints();
109 std::vector<double> gf(npar);
115 if (fFunc.Type() == Function::kLeastSquare) {
117 for (
unsigned int i = 0; i < ndata; ++i) {
119 double fval = fFunc.DataElement(&v.front(), i, &gf[0]);
124 for (
unsigned int j = 0; j < npar; ++j) {
125 grad[j] += 2. * fval * gf[j];
126 for (
unsigned int k = j; k < npar; ++ k) {
127 int idx = j + k*(k+1)/2;
128 hess[idx] += 2.0 * gf[j] * gf[k];
133 else if (fFunc.Type() == Function::kLogLikelihood) {
136 for (
unsigned int i = 0; i < ndata; ++i) {
140 double fval = fFunc.DataElement(&v.front(), i, &gf[0]);
144 for (
unsigned int j = 0; j < npar; ++j) {
147 for (
unsigned int k = j; k < npar; ++ k) {
148 int idx = j + k*(k+1)/2;
149 hess[idx] += gfj * gf[k] ;
155 MN_ERROR_MSG(
"FumiliFCNAdapter: type of fit method is not supported, it must be chi2 or log-likelihood");
167 #endif //ROOT_Minuit2_FCNAdapter