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,
123 : _functionPtr(function.
clone()),
124 _measurementList(measurementList),
125 _varianceList(varianceList),
126 _xPositionList(xPositionList),
127 _errorDef(errorDef) {}
129 template <
typename ReturnT>
130 MinimizerFunctionBase2<ReturnT>::MinimizerFunctionBase2(Function2<ReturnT>
const &
function,
136 : _functionPtr(function.
clone()),
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);
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);
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)