35 #include "Minuit2/FunctionMinimum.h"
36 #include "Minuit2/MnMigrad.h"
37 #include "Minuit2/MnMinos.h"
50 template <
typename ReturnT>
51 class MinimizerFunctionBase1 :
public ROOT::Minuit2::FCNBase {
53 explicit MinimizerFunctionBase1(Function1<ReturnT>
const &
function,
57 MinimizerFunctionBase1(MinimizerFunctionBase1
const &) =
default;
58 MinimizerFunctionBase1(MinimizerFunctionBase1 &&) =
default;
59 MinimizerFunctionBase1 &
operator=(MinimizerFunctionBase1
const &) =
default;
60 MinimizerFunctionBase1 &
operator=(MinimizerFunctionBase1 &&) =
default;
61 ~MinimizerFunctionBase1()
override =
default;
63 double Up()
const override {
return _errorDef; }
70 inline void setErrorDef(
double def) {_errorDef=def;}
83 template <
typename ReturnT>
84 class MinimizerFunctionBase2 :
public ROOT::Minuit2::FCNBase {
86 explicit MinimizerFunctionBase2(Function2<ReturnT>
const &
function,
91 MinimizerFunctionBase2(MinimizerFunctionBase2
const &) =
default;
92 MinimizerFunctionBase2(MinimizerFunctionBase2 &&) =
default;
93 MinimizerFunctionBase2 &
operator=(MinimizerFunctionBase2
const &) =
default;
94 MinimizerFunctionBase2 &
operator=(MinimizerFunctionBase2 &&) =
default;
95 ~MinimizerFunctionBase2()
override =
default;
97 double Up()
const override {
return _errorDef; }
105 inline void setErrorDef(
double def) {_errorDef=def;}
118 template <
typename ReturnT>
119 MinimizerFunctionBase1<ReturnT>::MinimizerFunctionBase1(Function1<ReturnT>
const &
function,
124 : _functionPtr(function.
clone()),
125 _measurementList(measurementList),
126 _varianceList(varianceList),
127 _xPositionList(xPositionList),
128 _errorDef(errorDef) {}
130 template <
typename ReturnT>
131 MinimizerFunctionBase2<ReturnT>::MinimizerFunctionBase2(Function2<ReturnT>
const &
function,
137 : _functionPtr(function.
clone()),
138 _measurementList(measurementList),
139 _varianceList(varianceList),
140 _xPositionList(xPositionList),
141 _yPositionList(yPositionList),
142 _errorDef(errorDef) {}
145 template <
typename ReturnT>
148 this->_functionPtr->setParameters(par);
151 for (
unsigned int i = 0; i < this->_measurementList.
size(); i++) {
152 double resid = (*(this->_functionPtr))(this->_xPositionList[i]) - this->_measurementList[i];
153 chi2 += resid * resid / this->_varianceList[i];
159 template <
typename ReturnT>
162 this->_functionPtr->setParameters(par);
165 for (
unsigned int i = 0; i < this->_measurementList.
size(); i++) {
166 double resid = (*(this->_functionPtr))(this->_xPositionList[i], this->_yPositionList[i]) -
167 this->_measurementList[i];
168 chi2 += resid * resid / this->_varianceList[i];
175 template <
typename ReturnT>
180 unsigned int const nParameters =
function.getNParameters();
181 if (initialParameterList.
size() != nParameters) {
184 if (stepSizeList.
size() != nParameters) {
187 unsigned int const nMeasurements = measurementList.
size();
188 if (varianceList.
size() != nMeasurements) {
191 if (xPositionList.
size() != nMeasurements) {
195 MinimizerFunctionBase1<ReturnT> minimizerFunc(
function, measurementList, varianceList, xPositionList,
198 ROOT::Minuit2::MnUserParameters fitPar;
200 for (
unsigned int i = 0; i < nParameters; ++i) {
202 fitPar.Add(paramNames[i].c_str(), initialParameterList[i], stepSizeList[i]);
205 ROOT::Minuit2::MnMigrad migrad(minimizerFunc, fitPar);
206 ROOT::Minuit2::FunctionMinimum
min = migrad();
207 ROOT::Minuit2::MnMinos minos(minimizerFunc,
min);
213 LOGL_WARN(
"lsst.afw.math.minimize",
"Fit failed to converge");
216 for (
unsigned int i = 0; i < nParameters; ++i) {
221 double e =
min.UserState().Error(paramNames[i].c_str());
229 template <
typename ReturnT>
234 unsigned int const nParameters =
function.getNParameters();
235 if (initialParameterList.
size() != nParameters) {
238 if (stepSizeList.
size() != nParameters) {
241 unsigned int const nMeasurements = measurementList.
size();
242 if (varianceList.
size() != nMeasurements) {
245 if (xPositionList.
size() != nMeasurements) {
248 if (yPositionList.
size() != nMeasurements) {
252 MinimizerFunctionBase2<ReturnT> minimizerFunc(
function, measurementList, varianceList, xPositionList,
253 yPositionList, errorDef);
255 ROOT::Minuit2::MnUserParameters fitPar;
257 for (
unsigned int i = 0; i < nParameters; ++i) {
259 fitPar.Add(paramNames[i].c_str(), initialParameterList[i], stepSizeList[i]);
262 ROOT::Minuit2::MnMigrad migrad(minimizerFunc, fitPar);
263 ROOT::Minuit2::FunctionMinimum
min = migrad();
264 ROOT::Minuit2::MnMinos minos(minimizerFunc,
min);
270 LOGL_WARN(
"lsst.afw.math.minimize",
"Fit failed to converge");
273 for (
unsigned int i = 0; i < nParameters; ++i) {
278 double e =
min.UserState().Error(paramNames[i].c_str());
289 #define minimizeFuncs(ReturnT) \
290 template FitResults minimize(Function1<ReturnT> const &, std::vector<double> const &, \
291 std::vector<double> const &, std::vector<double> const &, \
292 std::vector<double> const &, std::vector<double> const &, double); \
293 NL template FitResults minimize(Function2<ReturnT> const &, std::vector<double> const &, \
294 std::vector<double> const &, std::vector<double> const &, \
295 std::vector<double> const &, std::vector<double> const &, \
296 std::vector<double> const &, double);
298 minimizeFuncs(
float) minimizeFuncs(
double)
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
LSST DM logging module built on log4cxx.
#define LOGL_WARN(logger, message...)
Log a warn-level message using a varargs/printf style interface.
A Function taking one argument.
A Function taking two arguments.
Reports invalid arguments.
FilterProperty & operator=(FilterProperty const &)=default
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)
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
A base class for image defects.
Results from minimizing a function.
std::vector< double > parameterList
fit parameters
bool isValid
true if the fit converged; false otherwise
double chiSq
chi squared; may be nan or infinite, but only if isValid false
std::vector< std::pair< double, double > > parameterErrorList
negative,positive (1 sigma?) error for each parameter