Logo ROOT   6.12/07
Reference Guide
FumiliStandardChi2FCN.cxx
Go to the documentation of this file.
1 // @(#)root/minuit2:$Id$
2 // Authors: M. Winkler, F. James, L. Moneta, A. Zsenei 2003-2005
3 
4 /**********************************************************************
5  * *
6  * Copyright (c) 2005 LCG ROOT Math team, CERN/PH-SFT *
7  * *
8  **********************************************************************/
9 
10 
12 
13 #include <vector>
14 #include <cmath>
15 
16 namespace ROOT {
17 
18  namespace Minuit2 {
19 
20 
21 //#include <iostream>
22 
23 std::vector<double> FumiliStandardChi2FCN::Elements(const std::vector<double>& par) const {
24  // Calculate the f(i) contribution to the Chi2. Chi2 = Sum[f(i)**2]
25 
26  std::vector<double> result;
27  double tmp1 = 0.0;
28  unsigned int fPositionsSize = fPositions.size();
29 
30 
31  for(unsigned int i=0; i < fPositionsSize; i++) {
32 
33  const std::vector<double> & currentPosition = fPositions[i];
34 
35  // The commented line is the object-oriented way to do it
36  // but it is faster to do a single function call...
37  //(*(this->getModelFunction())).SetParameters(par);
38  tmp1 = (*(this->ModelFunction()))(par, currentPosition)- fMeasurements[i];
39 
40  result.push_back(tmp1*fInvErrors[i] );
41 
42  //std::cout << "element " << i << " " << (*(this->getModelFunction()))(par, currentPosition) << " " << fMeasurements[i] << " " << result[i] << std::endl;
43  }
44 
45 
46 
47  return result;
48 
49 }
50 
51 
52 
53 const std::vector<double> & FumiliStandardChi2FCN::GetMeasurement(int index) const {
54  // Return the coordinate (position) values.
55  return fPositions[index];
56 
57 }
58 
59 
61  // Return size
62  return fPositions.size();
63 
64 }
65 
66 
67 
68 void FumiliStandardChi2FCN::EvaluateAll( const std::vector<double> & par) {
69  // Evaluate chi2 value, gradient and hessian all in a single
70  // loop on the measurements
71 
72  int nmeas = GetNumberOfMeasurements();
73  std::vector<double> & grad = Gradient();
74  std::vector<double> & h = Hessian();
75  int npar = par.size();
76  double chi2 = 0;
77  grad.resize(npar);
78  h.resize( static_cast<unsigned int>(0.5 * npar* (npar + 1) ) );
79  // reset Elements
80  grad.assign(npar, 0.0);
81  h.assign(static_cast<unsigned int>(0.5 * npar* (npar + 1) ) , 0.0);
82 
83 
84  const ParametricFunction & modelFunc = *ModelFunction();
85 
86  for (int i = 0; i < nmeas; ++i) {
87 
88  // work for multi-dimensional points
89  const std::vector<double> & currentPosition = fPositions[i];
90  modelFunc.SetParameters( currentPosition );
91  double invError = fInvErrors[i];
92  double fval = modelFunc(par);
93 
94  double element = ( fval - fMeasurements[i] )*invError;
95  chi2 += element*element;
96 
97  // calc derivatives
98 
99  // this method should return a reference
100  std::vector<double> mfg = modelFunc.GetGradient(par);
101 
102  // grad is derivative of chi2 w.r.t to parameters
103  for (int j = 0; j < npar ; ++j) {
104  double dfj = invError * mfg[j];
105  grad[j] += 2.0 * element * dfj;
106 
107  // in second derivative use Fumili approximation neglecting the term containing the
108  // second derivatives of the model function
109  for (int k = j; k < npar; ++ k) {
110  int idx = j + k*(k+1)/2;
111  h[idx] += 2.0 * dfj * invError * mfg[k];
112  }
113 
114  } // end param loop
115 
116  } // end points loop
117 
118  // set Value in base class
119  SetFCNValue( chi2);
120 
121 }
122 
123  } // namespace Minuit2
124 
125 } // namespace ROOT
virtual const std::vector< double > & GetMeasurement(int Index) const
Accessor to the position of the measurement (x coordinate).
virtual void SetParameters(const std::vector< double > &params) const
Sets the parameters of the ParametricFunction.
std::vector< std::vector< double > > fPositions
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
TH1 * h
Definition: legend2.C:5
void SetFCNValue(double value)
const ParametricFunction * ModelFunction() const
Returns the model function used for the data.
Definition: FumiliChi2FCN.h:81
std::vector< double > Elements(const std::vector< double > &par) const
Evaluates the model function for the different measurement points and the Parameter values supplied...
virtual std::vector< double > GetGradient(const std::vector< double > &x) const
Member function returning the Gradient of the function with respect to its variables (but without inc...
virtual int GetNumberOfMeasurements() const
Accessor to the number of measurements used for calculating the chi-square.
std::vector< double > & Hessian()
virtual void EvaluateAll(const std::vector< double > &par)
Evaluate function Value, Gradient and Hessian using Fumili approximation, for values of parameters p ...
Function which has parameters.
virtual const std::vector< double > & Gradient() const
Return cached Value of function Gradient estimated previously using the FumiliFCNBase::EvaluateAll me...