2014-02-20 20:21:45 +00:00
|
|
|
/*
|
|
|
|
* MinuitMinimizer.cpp, part of LatAnalyze 3
|
|
|
|
*
|
2016-03-23 17:08:25 +00:00
|
|
|
* Copyright (C) 2013 - 2016 Antonin Portelli
|
2014-02-20 20:21:45 +00:00
|
|
|
*
|
|
|
|
* LatAnalyze 3 is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* LatAnalyze 3 is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with LatAnalyze 3. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2014-03-13 18:51:01 +00:00
|
|
|
#include <LatAnalyze/MinuitMinimizer.hpp>
|
|
|
|
#include <LatAnalyze/includes.hpp>
|
2016-03-23 17:08:25 +00:00
|
|
|
#include <Minuit2/Minuit2Minimizer.h>
|
|
|
|
#include <Math/Functor.h>
|
2014-02-20 20:21:45 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace Latan;
|
|
|
|
|
2016-04-01 21:39:49 +01:00
|
|
|
static constexpr double initErr = 0.1;
|
2014-09-18 17:18:51 +01:00
|
|
|
|
2014-02-20 20:21:45 +00:00
|
|
|
/******************************************************************************
|
2016-03-23 17:16:50 +00:00
|
|
|
* MinuitMinimizer implementation *
|
2014-02-20 20:21:45 +00:00
|
|
|
******************************************************************************/
|
2015-11-13 14:30:10 +00:00
|
|
|
// constructors ////////////////////////////////////////////////////////////////
|
|
|
|
MinuitMinimizer::MinuitMinimizer(const Algorithm algorithm)
|
|
|
|
{
|
|
|
|
setAlgorithm(algorithm);
|
|
|
|
}
|
|
|
|
|
2014-09-22 15:19:30 +01:00
|
|
|
// access //////////////////////////////////////////////////////////////////////
|
2015-08-07 15:10:03 +01:00
|
|
|
MinuitMinimizer::Algorithm MinuitMinimizer::getAlgorithm(void) const
|
|
|
|
{
|
|
|
|
return algorithm_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MinuitMinimizer::setAlgorithm(const Algorithm algorithm)
|
|
|
|
{
|
|
|
|
algorithm_ = algorithm;
|
|
|
|
}
|
|
|
|
|
2014-02-20 20:21:45 +00:00
|
|
|
// minimization ////////////////////////////////////////////////////////////////
|
|
|
|
const DVec & MinuitMinimizer::operator()(const DoubleFunction &f)
|
|
|
|
{
|
2016-03-23 17:08:25 +00:00
|
|
|
using namespace ROOT;
|
|
|
|
using namespace Minuit2;
|
|
|
|
|
|
|
|
DVec &x = getState();
|
|
|
|
int printLevel;
|
|
|
|
EMinimizerType minuitAlg;
|
|
|
|
unique_ptr<Math::Minimizer> min;
|
|
|
|
|
|
|
|
// convert Latan parameters to Minuit parameters
|
|
|
|
switch (getVerbosity())
|
|
|
|
{
|
|
|
|
case Verbosity::Silent:
|
|
|
|
printLevel = 0;
|
|
|
|
break;
|
|
|
|
case Verbosity::Normal:
|
|
|
|
printLevel = 2;
|
|
|
|
break;
|
|
|
|
case Verbosity::Debug:
|
|
|
|
printLevel = 3;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch (getAlgorithm())
|
|
|
|
{
|
|
|
|
case Algorithm::Migrad:
|
|
|
|
minuitAlg = kMigrad;
|
|
|
|
break;
|
|
|
|
case Algorithm::Simplex:
|
|
|
|
minuitAlg = kSimplex;
|
|
|
|
break;
|
|
|
|
case Algorithm::Combined:
|
|
|
|
minuitAlg = kCombined;
|
|
|
|
break;
|
|
|
|
}
|
2014-02-20 20:21:45 +00:00
|
|
|
|
2016-03-23 17:08:25 +00:00
|
|
|
// resize minimizer state to match function number of arguments
|
2014-02-20 20:21:45 +00:00
|
|
|
if (f.getNArg() != x.size())
|
|
|
|
{
|
2015-11-13 14:30:10 +00:00
|
|
|
resize(f.getNArg());
|
2014-02-20 20:21:45 +00:00
|
|
|
}
|
|
|
|
|
2016-03-23 17:08:25 +00:00
|
|
|
// create and set minimizer
|
|
|
|
min.reset(new Minuit2Minimizer(minuitAlg));
|
2016-04-05 20:55:11 +01:00
|
|
|
min->SetStrategy(2);
|
2016-03-23 17:08:25 +00:00
|
|
|
min->SetMaxFunctionCalls(getMaxIteration());
|
|
|
|
min->SetTolerance(getPrecision());
|
|
|
|
min->SetPrintLevel(printLevel);
|
|
|
|
|
|
|
|
// set function and variables
|
|
|
|
Math::Functor minuitF(f, x.size());
|
|
|
|
string name;
|
|
|
|
double val, step;
|
2014-02-20 20:21:45 +00:00
|
|
|
|
2016-03-23 17:08:25 +00:00
|
|
|
min->SetFunction(minuitF);
|
2014-02-26 18:36:29 +00:00
|
|
|
for (Index i = 0; i < x.size(); ++i)
|
2014-02-20 20:21:45 +00:00
|
|
|
{
|
2016-03-31 12:12:30 +01:00
|
|
|
name = f.varName().getName(i);
|
2016-03-23 17:08:25 +00:00
|
|
|
val = x(i);
|
|
|
|
step = (fabs(x(i)) != 0.) ? initErr*fabs(x(i)) : 1.;
|
|
|
|
if (hasHighLimit(i) and !hasLowLimit(i))
|
|
|
|
{
|
|
|
|
min->SetUpperLimitedVariable(i, name, val, step, getHighLimit(i));
|
|
|
|
}
|
|
|
|
else if (!hasHighLimit(i) and hasLowLimit(i))
|
2015-01-28 17:22:12 +00:00
|
|
|
{
|
2016-03-23 17:08:25 +00:00
|
|
|
min->SetLowerLimitedVariable(i, name, val, step, getLowLimit(i));
|
2015-01-28 17:22:12 +00:00
|
|
|
}
|
2016-03-23 17:08:25 +00:00
|
|
|
else if (hasHighLimit(i) and hasLowLimit(i))
|
2014-10-14 16:34:27 +01:00
|
|
|
{
|
2016-03-23 17:08:25 +00:00
|
|
|
min->SetLimitedVariable(i, name, val, step, getLowLimit(i),
|
|
|
|
getHighLimit(i));
|
2014-10-14 16:34:27 +01:00
|
|
|
}
|
2016-03-23 17:08:25 +00:00
|
|
|
else
|
2014-10-14 16:34:27 +01:00
|
|
|
{
|
2016-03-23 17:08:25 +00:00
|
|
|
min->SetVariable(i, name, val, step);
|
2014-10-14 16:34:27 +01:00
|
|
|
}
|
2014-02-20 20:21:45 +00:00
|
|
|
}
|
|
|
|
|
2016-03-23 17:08:25 +00:00
|
|
|
// minimize
|
|
|
|
int status;
|
|
|
|
unsigned int n = 0;
|
2014-02-20 20:21:45 +00:00
|
|
|
|
2016-03-23 17:08:25 +00:00
|
|
|
do
|
2015-01-28 17:22:12 +00:00
|
|
|
{
|
2016-03-23 17:08:25 +00:00
|
|
|
if (getVerbosity() >= Verbosity::Normal)
|
|
|
|
{
|
2016-03-31 12:12:30 +01:00
|
|
|
cout << "========== Minuit minimization, pass #" << n + 1;
|
2016-04-01 21:39:49 +01:00
|
|
|
cout << " =========" << endl;
|
2016-03-23 17:08:25 +00:00
|
|
|
}
|
|
|
|
min->Minimize();
|
|
|
|
status = min->Status();
|
2016-04-01 21:39:49 +01:00
|
|
|
n++;
|
|
|
|
} while (status and (n < getMaxPass()));
|
2016-03-23 17:08:25 +00:00
|
|
|
if (getVerbosity() >= Verbosity::Normal)
|
2014-02-20 20:21:45 +00:00
|
|
|
{
|
2016-04-01 21:39:49 +01:00
|
|
|
cout << "=================================================" << endl;
|
2014-02-20 20:21:45 +00:00
|
|
|
}
|
2016-03-23 17:08:25 +00:00
|
|
|
switch (status)
|
2014-02-20 20:21:45 +00:00
|
|
|
{
|
2016-03-23 17:08:25 +00:00
|
|
|
case 1:
|
|
|
|
LATAN_WARNING("invalid minimum: covariance matrix was made positive");
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
LATAN_WARNING("invalid minimum: Hesse analysis is not valid");
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
LATAN_WARNING("invalid minimum: requested precision not reached");
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
LATAN_WARNING("invalid minimum: iteration limit reached");
|
|
|
|
break;
|
2014-02-20 20:21:45 +00:00
|
|
|
}
|
2016-03-23 17:08:25 +00:00
|
|
|
|
|
|
|
// save and return result
|
|
|
|
for (Index i = 0; i < x.size(); ++i)
|
2014-02-20 20:21:45 +00:00
|
|
|
{
|
2016-03-23 17:08:25 +00:00
|
|
|
x(i) = min->X()[i];
|
2014-02-20 20:21:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return x;
|
|
|
|
}
|