00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef ROOT_Math_GSLNLSMinimizer
00028 #define ROOT_Math_GSLNLSMinimizer
00029
00030
00031
00032 #ifndef ROOT_Math_Minimizer
00033 #include "Math/Minimizer.h"
00034 #endif
00035
00036
00037 #ifndef ROOT_Math_IFunctionfwd
00038 #include "Math/IFunctionfwd.h"
00039 #endif
00040
00041 #ifndef ROOT_Math_IParamFunctionfwd
00042 #include "Math/IParamFunctionfwd.h"
00043 #endif
00044
00045 #ifndef ROOT_Math_FitMethodFunction
00046 #include "Math/FitMethodFunction.h"
00047 #endif
00048
00049 #ifndef ROOT_Math_MinimizerVariable
00050 #include "Math/MinimizerVariable.h"
00051 #endif
00052
00053
00054 #include <vector>
00055 #include <map>
00056 #include <string>
00057
00058 namespace ROOT {
00059
00060 namespace Math {
00061
00062 class GSLMultiFit;
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077 class LSResidualFunc : public IMultiGradFunction {
00078 public:
00079
00080
00081 LSResidualFunc() : fIndex(0), fChi2(0)
00082 {}
00083
00084
00085 LSResidualFunc(const ROOT::Math::FitMethodFunction & func, unsigned int i) :
00086 fIndex(i),
00087 fChi2(&func),
00088 fX2(std::vector<double>(func.NDim() ) )
00089 {}
00090
00091
00092
00093 LSResidualFunc(const LSResidualFunc & rhs) :
00094 IMultiGenFunction(),
00095 IMultiGradFunction()
00096 {
00097 operator=(rhs);
00098 }
00099
00100
00101 LSResidualFunc & operator= (const LSResidualFunc & rhs)
00102 {
00103 fIndex = rhs.fIndex;
00104 fChi2 = rhs.fChi2;
00105 fX2 = rhs.fX2;
00106 return *this;
00107 }
00108
00109 IMultiGenFunction * Clone() const {
00110 return new LSResidualFunc(*fChi2,fIndex);
00111 }
00112
00113 unsigned int NDim() const { return fChi2->NDim(); }
00114
00115 void Gradient( const double * x, double * g) const {
00116 double f0 = 0;
00117 FdF(x,f0,g);
00118 }
00119
00120 void FdF (const double * x, double & f, double * g) const {
00121 unsigned int n = NDim();
00122 std::copy(x,x+n,fX2.begin());
00123 const double kEps = 1.0E-4;
00124 f = DoEval(x);
00125 for (unsigned int i = 0; i < n; ++i) {
00126 fX2[i] += kEps;
00127 g[i] = ( DoEval(&fX2.front()) - f )/kEps;
00128 fX2[i] = x[i];
00129 }
00130 }
00131
00132
00133 private:
00134
00135 double DoEval (const double * x) const {
00136 return fChi2->DataElement(x, fIndex);
00137 }
00138
00139 double DoDerivative(const double * x, unsigned int icoord) const {
00140
00141 std::copy(x,x+NDim(),fX2.begin());
00142 const double kEps = 1.0E-4;
00143 fX2[icoord] += kEps;
00144 return ( DoEval(&fX2.front()) - DoEval(x) )/kEps;
00145 }
00146
00147 unsigned int fIndex;
00148 const ROOT::Math::FitMethodFunction * fChi2;
00149 mutable std::vector<double> fX2;
00150 };
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162 class GSLNLSMinimizer : public ROOT::Math::Minimizer {
00163
00164 public:
00165
00166
00167
00168
00169 GSLNLSMinimizer (int type = 0);
00170
00171
00172
00173
00174 ~GSLNLSMinimizer ();
00175
00176 private:
00177
00178
00179
00180
00181
00182 GSLNLSMinimizer(const GSLNLSMinimizer &) : ROOT::Math::Minimizer() {}
00183
00184
00185
00186
00187 GSLNLSMinimizer & operator = (const GSLNLSMinimizer & rhs) {
00188 if (this == &rhs) return *this;
00189 return *this;
00190 }
00191
00192 public:
00193
00194
00195 virtual void SetFunction(const ROOT::Math::IMultiGenFunction & func);
00196
00197
00198 virtual void SetFunction(const ROOT::Math::IMultiGradFunction & func);
00199
00200
00201 virtual bool SetVariable(unsigned int ivar, const std::string & name, double val, double step);
00202
00203
00204 virtual bool SetLowerLimitedVariable(unsigned int ivar , const std::string & name , double val , double step , double lower );
00205
00206 virtual bool SetUpperLimitedVariable(unsigned int ivar , const std::string & name , double val , double step , double upper );
00207
00208 virtual bool SetLimitedVariable(unsigned int ivar , const std::string & name , double val , double step , double lower , double upper );
00209
00210 virtual bool SetFixedVariable(unsigned int ivar , const std::string & name , double val );
00211
00212
00213 virtual bool SetVariableValue(unsigned int ivar, double val );
00214
00215 virtual bool SetVariableValues(const double * x);
00216
00217
00218 virtual bool Minimize();
00219
00220
00221 virtual double MinValue() const { return fMinVal; }
00222
00223
00224 virtual double Edm() const { return fEdm; }
00225
00226
00227 virtual const double * X() const { return &fValues.front(); }
00228
00229
00230 virtual const double * MinGradient() const;
00231
00232
00233 virtual unsigned int NCalls() const { return (fObjFunc) ? fObjFunc->NCalls() : 0; }
00234
00235
00236
00237 virtual unsigned int NDim() const { return fDim; }
00238
00239
00240
00241 virtual unsigned int NFree() const { return fNFree; }
00242
00243
00244 virtual bool ProvidesError() const { return true; }
00245
00246
00247 virtual const double * Errors() const { return (fErrors.size() > 0) ? &fErrors.front() : 0; }
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258 virtual double CovMatrix(unsigned int , unsigned int ) const;
00259
00260
00261 virtual int CovMatrixStatus() const;
00262
00263 protected:
00264
00265 private:
00266
00267
00268 unsigned int fDim;
00269 unsigned int fNFree;
00270 unsigned int fSize;
00271
00272
00273 ROOT::Math::GSLMultiFit * fGSLMultiFit;
00274 const ROOT::Math::FitMethodFunction * fObjFunc;
00275
00276 double fMinVal;
00277 double fEdm;
00278 double fLSTolerance;
00279 std::vector<double> fValues;
00280 std::vector<double> fErrors;
00281 std::vector<double> fCovMatrix;
00282 std::vector<double> fSteps;
00283 std::vector<std::string> fNames;
00284 std::vector<LSResidualFunc> fResiduals;
00285
00286 std::vector<ROOT::Math::EMinimVariableType> fVarTypes;
00287 std::map< unsigned int, std::pair<double, double> > fBounds;
00288
00289
00290 };
00291
00292 }
00293
00294 }
00295
00296
00297 #endif