| 16 |
#include "Fit/PoissonLikelihoodFCN.h" |
#include "Fit/PoissonLikelihoodFCN.h" |
| 17 |
#include "Fit/LogLikelihoodFCN.h" |
#include "Fit/LogLikelihoodFCN.h" |
| 18 |
#include "Math/Minimizer.h" |
#include "Math/Minimizer.h" |
| 19 |
|
#include "Math/MinimizerOptions.h" |
| 20 |
|
#include "Fit/MinimizerControlParams.h" |
| 21 |
#include "Fit/BinData.h" |
#include "Fit/BinData.h" |
| 22 |
#include "Fit/UnBinData.h" |
#include "Fit/UnBinData.h" |
| 23 |
#include "Math/Error.h" |
#include "Math/Error.h" |
| 43 |
Fitter::~Fitter() |
Fitter::~Fitter() |
| 44 |
{ |
{ |
| 45 |
// Destructor implementation. |
// Destructor implementation. |
| 46 |
|
// since function pointer is normally own by FitResult. delete only if fit result is empty |
| 47 |
if (fFunc != 0) delete fFunc; |
if (fFunc && fResult.FittedFunction() == 0) delete fFunc; |
| 48 |
} |
} |
| 49 |
|
|
| 50 |
Fitter::Fitter(const Fitter &) |
Fitter::Fitter(const Fitter & rhs) |
| 51 |
{ |
{ |
| 52 |
// Implementation of copy constructor. |
// Implementation of copy constructor. |
| 53 |
|
// copy FitResult, FitCOnfig and clone fit function |
| 54 |
|
(*this) = rhs; |
| 55 |
} |
} |
| 56 |
|
|
| 57 |
Fitter & Fitter::operator = (const Fitter &rhs) |
Fitter & Fitter::operator = (const Fitter &rhs) |
| 58 |
{ |
{ |
| 59 |
// Implementation of assignment operator. |
// Implementation of assignment operator. |
| 60 |
if (this == &rhs) return *this; // time saving self-test |
if (this == &rhs) return *this; // time saving self-test |
| 61 |
|
fUseGradient = rhs.fUseGradient; |
| 62 |
|
fResult = rhs.fResult; |
| 63 |
|
fConfig = rhs.fConfig; |
| 64 |
|
// function is copied and managed by FitResult (maybe should use an auto_ptr) |
| 65 |
|
fFunc = fResult.ModelFunction(); |
| 66 |
|
if (rhs.fFunc != 0 && fResult.ModelFunction() == 0) { // case no fit has been done yet - then clone |
| 67 |
|
if (fFunc) delete fFunc; |
| 68 |
|
fFunc = dynamic_cast<IModelFunction *>( (rhs.fFunc)->Clone() ); |
| 69 |
|
assert(fFunc != 0); |
| 70 |
|
} |
| 71 |
return *this; |
return *this; |
| 72 |
} |
} |
| 73 |
|
|
| 81 |
fFunc = dynamic_cast<IModelFunction *> ( func.Clone() ); |
fFunc = dynamic_cast<IModelFunction *> ( func.Clone() ); |
| 82 |
|
|
| 83 |
// creates the parameter settings |
// creates the parameter settings |
| 84 |
fConfig.SetParamsSettings(*fFunc); |
fConfig.CreateParamsSettings(*fFunc); |
| 85 |
} |
} |
| 86 |
|
|
| 87 |
|
|
| 94 |
fFunc = new ROOT::Math::MultiDimParamFunctionAdapter(func); |
fFunc = new ROOT::Math::MultiDimParamFunctionAdapter(func); |
| 95 |
|
|
| 96 |
// creates the parameter settings |
// creates the parameter settings |
| 97 |
fConfig.SetParamsSettings(*fFunc); |
fConfig.CreateParamsSettings(*fFunc); |
| 98 |
} |
} |
| 99 |
|
|
| 100 |
void Fitter::SetFunction(const IGradModelFunction & func) |
void Fitter::SetFunction(const IGradModelFunction & func) |
| 105 |
fFunc = dynamic_cast<IModelFunction *> ( func.Clone() ); |
fFunc = dynamic_cast<IModelFunction *> ( func.Clone() ); |
| 106 |
|
|
| 107 |
// creates the parameter settings |
// creates the parameter settings |
| 108 |
fConfig.SetParamsSettings(*fFunc); |
fConfig.CreateParamsSettings(*fFunc); |
| 109 |
} |
} |
| 110 |
|
|
| 111 |
|
|
| 117 |
fFunc = new ROOT::Math::MultiDimParamGradFunctionAdapter(func); |
fFunc = new ROOT::Math::MultiDimParamGradFunctionAdapter(func); |
| 118 |
|
|
| 119 |
// creates the parameter settings |
// creates the parameter settings |
| 120 |
fConfig.SetParamsSettings(*fFunc); |
fConfig.CreateParamsSettings(*fFunc); |
| 121 |
} |
} |
| 122 |
|
|
| 123 |
|
|
| 125 |
// fit a user provided FCN function |
// fit a user provided FCN function |
| 126 |
// create fit parameter settings |
// create fit parameter settings |
| 127 |
unsigned int npar = fcn.NDim(); |
unsigned int npar = fcn.NDim(); |
| 128 |
if (params != 0 || fConfig.ParamsSettings().size() != npar) fConfig.SetParamsSettings(npar, params); |
if (params != 0 ) |
| 129 |
|
fConfig.SetParamsSettings(npar, params); |
| 130 |
|
else { |
| 131 |
|
if ( fConfig.ParamsSettings().size() != npar) { |
| 132 |
|
MATH_ERROR_MSG("Fitter::FitFCN","wrong fit parameter settings"); |
| 133 |
|
return false; |
| 134 |
|
} |
| 135 |
|
} |
| 136 |
// create Minimizer |
// create Minimizer |
| 137 |
std::auto_ptr<ROOT::Math::Minimizer> minimizer = std::auto_ptr<ROOT::Math::Minimizer> ( fConfig.CreateMinimizer() ); |
std::auto_ptr<ROOT::Math::Minimizer> minimizer = std::auto_ptr<ROOT::Math::Minimizer> ( fConfig.CreateMinimizer() ); |
| 138 |
if (minimizer.get() == 0) return false; |
if (minimizer.get() == 0) return false; |
| 143 |
bool Fitter::FitFCN(const BaseGradFunc & fcn, const double * params, unsigned int dataSize) { |
bool Fitter::FitFCN(const BaseGradFunc & fcn, const double * params, unsigned int dataSize) { |
| 144 |
// fit a user provided FCN gradient function |
// fit a user provided FCN gradient function |
| 145 |
unsigned int npar = fcn.NDim(); |
unsigned int npar = fcn.NDim(); |
| 146 |
if (params != 0 || fConfig.ParamsSettings().size() != npar) fConfig.SetParamsSettings(npar, params); |
if (params != 0 ) |
| 147 |
|
fConfig.SetParamsSettings(npar, params); |
| 148 |
|
else { |
| 149 |
|
if ( fConfig.ParamsSettings().size() != npar) { |
| 150 |
|
MATH_ERROR_MSG("Fitter::FitFCN","wrong fit parameter settings"); |
| 151 |
|
return false; |
| 152 |
|
} |
| 153 |
|
} |
| 154 |
// create Minimizer (need to be done afterwards) |
// create Minimizer (need to be done afterwards) |
| 155 |
std::auto_ptr<ROOT::Math::Minimizer> minimizer = std::auto_ptr<ROOT::Math::Minimizer> ( fConfig.CreateMinimizer() ); |
std::auto_ptr<ROOT::Math::Minimizer> minimizer = std::auto_ptr<ROOT::Math::Minimizer> ( fConfig.CreateMinimizer() ); |
| 156 |
if (minimizer.get() == 0) return false; |
if (minimizer.get() == 0) return false; |
| 202 |
if (minimizer.get() == 0) return false; |
if (minimizer.get() == 0) return false; |
| 203 |
if (fFunc == 0) return false; |
if (fFunc == 0) return false; |
| 204 |
|
|
| 205 |
// logl fit (error is 0.5) |
// logl fit (error should be 0.5) set if different than default values (of 1) |
| 206 |
|
if (fConfig.MinimizerOptions().ErrorDef() == ROOT::Math::MinimizerOptions::DefaultErrorDef() ) { |
| 207 |
|
fConfig.MinimizerOptions().SetErrorDef(0.5); |
| 208 |
minimizer->SetErrorUp(0.5); |
minimizer->SetErrorUp(0.5); |
| 209 |
|
} |
| 210 |
|
|
| 211 |
// create a chi2 function to be used for the equivalent chi-square |
// create a chi2 function to be used for the equivalent chi-square |
| 212 |
Chi2FCN<BaseFunc> chi2(data,*fFunc); |
Chi2FCN<BaseFunc> chi2(data,*fFunc); |
| 245 |
std::cout << "Fitter ParamSettings " << Config().ParamsSettings()[ipar].IsBound() << " lower limit " << Config().ParamsSettings()[ipar].LowerLimit() << " upper limit " << Config().ParamsSettings()[ipar].UpperLimit() << std::endl; |
std::cout << "Fitter ParamSettings " << Config().ParamsSettings()[ipar].IsBound() << " lower limit " << Config().ParamsSettings()[ipar].LowerLimit() << " upper limit " << Config().ParamsSettings()[ipar].UpperLimit() << std::endl; |
| 246 |
#endif |
#endif |
| 247 |
|
|
| 248 |
// logl fit (error is 0.5) |
// logl fit (error should be 0.5) set if different than default values (of 1) |
| 249 |
|
if (fConfig.MinimizerOptions().ErrorDef() == ROOT::Math::MinimizerOptions::DefaultErrorDef() ) { |
| 250 |
|
fConfig.MinimizerOptions().SetErrorDef(0.5); |
| 251 |
minimizer->SetErrorUp(0.5); |
minimizer->SetErrorUp(0.5); |
| 252 |
|
} |
| 253 |
|
|
| 254 |
if (!fUseGradient) { |
if (!fUseGradient) { |
| 255 |
// do minimzation without using the gradient |
// do minimzation without using the gradient |
| 315 |
// } |
// } |
| 316 |
// return false; |
// return false; |
| 317 |
|
|
| 318 |
// if requested parabolic error ansure correct analysis by the minimizer |
// if requested parabolic error do correct error analysis by the minimizer (call HESSE) |
| 319 |
if (fConfig.MinimizerOptions().ParabErrors()) minimizer.SetValidError(true); |
if (fConfig.ParabErrors()) minimizer.SetValidError(true); |
| 320 |
|
|
| 321 |
|
|
| 322 |
bool ret = minimizer.Minimize(); |
bool ret = minimizer.Minimize(); |
| 326 |
#endif |
#endif |
| 327 |
|
|
| 328 |
unsigned int ncalls = ObjFuncTrait<ObjFunc>::NCalls(objFunc); |
unsigned int ncalls = ObjFuncTrait<ObjFunc>::NCalls(objFunc); |
| 329 |
fResult = FitResult(minimizer,fConfig, *fFunc, ret, dataSize, chi2func, fConfig.MinimizerOptions().MinosErrors(), ncalls ); |
fResult = FitResult(minimizer,fConfig, *fFunc, ret, dataSize, chi2func, fConfig.MinosErrors(), ncalls ); |
| 330 |
if (fConfig.NormalizeErrors() ) fResult.NormalizeErrors(); |
if (fConfig.NormalizeErrors() ) fResult.NormalizeErrors(); |
| 331 |
return ret; |
return ret; |
| 332 |
} |
} |