33 #include "Minuit2/FunctionMinimum.h" 34 #include "Minuit2/MnMigrad.h" 35 #include "Minuit2/MnMinos.h" 36 #include "Minuit2/MnPrint.h" 49 template <
typename ReturnT>
50 class MinimizerFunctionBase1 :
public ROOT::Minuit2::FCNBase {
52 explicit MinimizerFunctionBase1(Function1<ReturnT>
const &
function,
56 MinimizerFunctionBase1(MinimizerFunctionBase1
const &) =
default;
57 MinimizerFunctionBase1(MinimizerFunctionBase1 &&) =
default;
58 MinimizerFunctionBase1 &operator=(MinimizerFunctionBase1
const &) =
default;
59 MinimizerFunctionBase1 &operator=(MinimizerFunctionBase1 &&) =
default;
60 ~MinimizerFunctionBase1()
override =
default;
62 double Up()
const override {
return _errorDef; }
69 inline void setErrorDef(
double def) {_errorDef=def;}
82 template <
typename ReturnT>
83 class MinimizerFunctionBase2 :
public ROOT::Minuit2::FCNBase {
85 explicit MinimizerFunctionBase2(Function2<ReturnT>
const &
function,
90 MinimizerFunctionBase2(MinimizerFunctionBase2
const &) =
default;
91 MinimizerFunctionBase2(MinimizerFunctionBase2 &&) =
default;
92 MinimizerFunctionBase2 &operator=(MinimizerFunctionBase2
const &) =
default;
93 MinimizerFunctionBase2 &operator=(MinimizerFunctionBase2 &&) =
default;
94 ~MinimizerFunctionBase2()
override =
default;
96 double Up()
const override {
return _errorDef; }
104 inline void setErrorDef(
double def) {_errorDef=def;}
117 template <
typename ReturnT>
118 MinimizerFunctionBase1<ReturnT>::MinimizerFunctionBase1(Function1<ReturnT>
const &
function,
124 _measurementList(measurementList),
125 _varianceList(varianceList),
126 _xPositionList(xPositionList),
127 _errorDef(errorDef) {}
129 template <
typename ReturnT>
130 MinimizerFunctionBase2<ReturnT>::MinimizerFunctionBase2(Function2<ReturnT>
const &
function,
137 _measurementList(measurementList),
138 _varianceList(varianceList),
139 _xPositionList(xPositionList),
140 _yPositionList(yPositionList),
141 _errorDef(errorDef) {}
144 template <
typename ReturnT>
147 this->_functionPtr->setParameters(par);
150 for (
unsigned int i = 0; i < this->_measurementList.
size(); i++) {
151 double resid = (*(this->_functionPtr))(this->_xPositionList[i]) - this->_measurementList[i];
152 chi2 += resid * resid / this->_varianceList[i];
158 template <
typename ReturnT>
161 this->_functionPtr->setParameters(par);
164 for (
unsigned int i = 0; i < this->_measurementList.
size(); i++) {
165 double resid = (*(this->_functionPtr))(this->_xPositionList[i], this->_yPositionList[i]) -
166 this->_measurementList[i];
167 chi2 += resid * resid / this->_varianceList[i];
174 template <
typename ReturnT>
179 unsigned int const nParameters =
function.getNParameters();
180 if (initialParameterList.
size() != nParameters) {
183 if (stepSizeList.
size() != nParameters) {
186 unsigned int const nMeasurements = measurementList.
size();
187 if (varianceList.
size() != nMeasurements) {
190 if (xPositionList.
size() != nMeasurements) {
194 MinimizerFunctionBase1<ReturnT> minimizerFunc(
function, measurementList, varianceList, xPositionList,
197 ROOT::Minuit2::MnUserParameters fitPar;
199 for (
unsigned int i = 0; i < nParameters; ++i) {
201 fitPar.Add(paramNames[i].c_str(), initialParameterList[i], stepSizeList[i]);
204 ROOT::Minuit2::MnMigrad migrad(minimizerFunc, fitPar);
205 ROOT::Minuit2::FunctionMinimum
min = migrad();
206 ROOT::Minuit2::MnMinos minos(minimizerFunc, min);
209 fitResults.
chiSq = min.Fval();
212 LOGL_WARN(
"afw.math.minimize",
"Fit failed to converge");
215 for (
unsigned int i = 0; i < nParameters; ++i) {
220 double e = min.UserState().Error(paramNames[i].c_str());
228 template <
typename ReturnT>
233 unsigned int const nParameters =
function.getNParameters();
234 if (initialParameterList.
size() != nParameters) {
237 if (stepSizeList.
size() != nParameters) {
240 unsigned int const nMeasurements = measurementList.
size();
241 if (varianceList.
size() != nMeasurements) {
244 if (xPositionList.
size() != nMeasurements) {
247 if (yPositionList.
size() != nMeasurements) {
251 MinimizerFunctionBase2<ReturnT> minimizerFunc(
function, measurementList, varianceList, xPositionList,
252 yPositionList, errorDef);
254 ROOT::Minuit2::MnUserParameters fitPar;
256 for (
unsigned int i = 0; i < nParameters; ++i) {
258 fitPar.Add(paramNames[i].c_str(), initialParameterList[i], stepSizeList[i]);
261 ROOT::Minuit2::MnMigrad migrad(minimizerFunc, fitPar);
262 ROOT::Minuit2::FunctionMinimum
min = migrad();
263 ROOT::Minuit2::MnMinos minos(minimizerFunc, min);
266 fitResults.
chiSq = min.Fval();
269 LOGL_WARN(
"afw.math.minimize",
"Fit failed to converge");
272 for (
unsigned int i = 0; i < nParameters; ++i) {
277 double e = min.UserState().Error(paramNames[i].c_str());
288 #define minimizeFuncs(ReturnT) \ 289 template FitResults minimize(Function1<ReturnT> const &, std::vector<double> const &, \ 290 std::vector<double> const &, std::vector<double> const &, \ 291 std::vector<double> const &, std::vector<double> const &, double); \ 292 NL template FitResults minimize(Function2<ReturnT> const &, std::vector<double> const &, \ 293 std::vector<double> const &, std::vector<double> const &, \ 294 std::vector<double> const &, std::vector<double> const &, \ 295 std::vector<double> const &, double); 297 minimizeFuncs(
float) minimizeFuncs(
double)
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
std::vector< double > parameterList
fit parameters
std::vector< std::pair< double, double > > parameterErrorList
negative,positive (1 sigma?) error for each parameter
FitResults minimize(lsst::afw::math::Function1< ReturnT > const &function, std::vector< double > const &initialParameterList, std::vector< double > const &stepSizeList, std::vector< double > const &measurementList, std::vector< double > const &varianceList, std::vector< double > const &xPositionList, double errorDef)
Find the minimum of a function(x)
A Function taking two arguments.
LSST DM logging module built on log4cxx.
A base class for image defects.
Results from minimizing a function.
double chiSq
chi squared; may be nan or infinite, but only if isValid false
A Function taking one argument.
#define LOGL_WARN(logger, message...)
Log a warn-level message using a varargs/printf style interface.
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
bool isValid
true if the fit converged; false otherwise
Reports invalid arguments.