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,
public daf::base::Citizen {
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,
public daf::base::Citizen {
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,
123 : daf::
base::Citizen(typeid(this)),
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 : daf::
base::Citizen(typeid(this)),
139 _measurementList(measurementList),
140 _varianceList(varianceList),
141 _xPositionList(xPositionList),
142 _yPositionList(yPositionList),
143 _errorDef(errorDef) {}
146 template <
typename ReturnT>
149 this->_functionPtr->setParameters(par);
152 for (
unsigned int i = 0; i < this->_measurementList.
size(); i++) {
153 double resid = (*(this->_functionPtr))(this->_xPositionList[i]) - this->_measurementList[i];
154 chi2 += resid * resid / this->_varianceList[i];
160 template <
typename ReturnT>
163 this->_functionPtr->setParameters(par);
166 for (
unsigned int i = 0; i < this->_measurementList.
size(); i++) {
167 double resid = (*(this->_functionPtr))(this->_xPositionList[i], this->_yPositionList[i]) -
168 this->_measurementList[i];
169 chi2 += resid * resid / this->_varianceList[i];
176 template <
typename ReturnT>
181 unsigned int const nParameters =
function.getNParameters();
182 if (initialParameterList.
size() != nParameters) {
185 if (stepSizeList.
size() != nParameters) {
188 unsigned int const nMeasurements = measurementList.
size();
189 if (varianceList.
size() != nMeasurements) {
192 if (xPositionList.
size() != nMeasurements) {
196 MinimizerFunctionBase1<ReturnT> minimizerFunc(
function, measurementList, varianceList, xPositionList,
199 ROOT::Minuit2::MnUserParameters fitPar;
201 for (
unsigned int i = 0; i < nParameters; ++i) {
203 fitPar.Add(paramNames[i].c_str(), initialParameterList[i], stepSizeList[i]);
206 ROOT::Minuit2::MnMigrad migrad(minimizerFunc, fitPar);
207 ROOT::Minuit2::FunctionMinimum
min = migrad();
208 ROOT::Minuit2::MnMinos minos(minimizerFunc, min);
211 fitResults.
chiSq = min.Fval();
214 LOGL_WARN(
"afw.math.minimize",
"Fit failed to converge");
217 for (
unsigned int i = 0; i < nParameters; ++i) {
222 double e = min.UserState().Error(paramNames[i].c_str());
230 template <
typename ReturnT>
235 unsigned int const nParameters =
function.getNParameters();
236 if (initialParameterList.
size() != nParameters) {
239 if (stepSizeList.
size() != nParameters) {
242 unsigned int const nMeasurements = measurementList.
size();
243 if (varianceList.
size() != nMeasurements) {
246 if (xPositionList.
size() != nMeasurements) {
249 if (yPositionList.
size() != nMeasurements) {
253 MinimizerFunctionBase2<ReturnT> minimizerFunc(
function, measurementList, varianceList, xPositionList,
254 yPositionList, errorDef);
256 ROOT::Minuit2::MnUserParameters fitPar;
258 for (
unsigned int i = 0; i < nParameters; ++i) {
260 fitPar.Add(paramNames[i].c_str(), initialParameterList[i], stepSizeList[i]);
263 ROOT::Minuit2::MnMigrad migrad(minimizerFunc, fitPar);
264 ROOT::Minuit2::FunctionMinimum
min = migrad();
265 ROOT::Minuit2::MnMinos minos(minimizerFunc, min);
268 fitResults.
chiSq = min.Fval();
271 LOGL_WARN(
"afw.math.minimize",
"Fit failed to converge");
274 for (
unsigned int i = 0; i < nParameters; ++i) {
279 double e = min.UserState().Error(paramNames[i].c_str());
290 #define minimizeFuncs(ReturnT) \ 291 template FitResults minimize(Function1<ReturnT> const &, std::vector<double> const &, \ 292 std::vector<double> const &, std::vector<double> const &, \ 293 std::vector<double> const &, std::vector<double> const &, double); \ 294 NL template FitResults minimize(Function2<ReturnT> const &, std::vector<double> const &, \ 295 std::vector<double> const &, std::vector<double> const &, \ 296 std::vector<double> const &, std::vector<double> const &, \ 297 std::vector<double> const &, double); 299 minimizeFuncs(
float) minimizeFuncs(
double)
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
def format(config, name=None, writeSourceLine=True, prefix="", verbose=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.