#include "TMinuitMinimizer.h"
#include "Math/IFunction.h"
#include "TMinuit.h"
#include <iostream>
#include <cassert>
#include <algorithm>
#include <functional>
ROOT::Math::IMultiGenFunction * TMinuitMinimizer::fgFunc = 0;
TMinuit * TMinuitMinimizer::fgMinuit = 0;
ClassImp(TMinuitMinimizer)
TMinuitMinimizer::TMinuitMinimizer(ROOT::Minuit::EMinimizerType type ) :
fDim(0),
fStrategy(1),
fType(type),
fMinuit(fgMinuit)
{
}
TMinuitMinimizer::TMinuitMinimizer(const char * type ) :
fDim(0),
fStrategy(1),
fMinuit(fgMinuit)
{
std::string algoname(type);
std::transform(algoname.begin(), algoname.end(), algoname.begin(), (int(*)(int)) tolower );
ROOT::Minuit::EMinimizerType algoType = ROOT::Minuit::kMigrad;
if (algoname == "simplex") algoType = ROOT::Minuit::kSimplex;
if (algoname == "minimize" ) algoType = ROOT::Minuit::kCombined;
if (algoname == "migrad_imp" ) algoType = ROOT::Minuit::kMigradImproved;
fType = algoType;
}
TMinuitMinimizer::~TMinuitMinimizer()
{
if (fMinuit) delete fMinuit;
}
TMinuitMinimizer::TMinuitMinimizer(const TMinuitMinimizer &) :
Minimizer()
{
}
TMinuitMinimizer & TMinuitMinimizer::operator = (const TMinuitMinimizer &rhs)
{
if (this == &rhs) return *this;
return *this;
}
void TMinuitMinimizer::SetFunction(const ROOT::Math::IMultiGenFunction & func) {
fDim = func.NDim();
#ifdef USE_STATIC_TMINUIT
if (fgMinuit == 0) fgMinuit = new TMinuit(fDim);
fMinuit = fgMinuit;
#else
if (fMinuit) {
delete fMinuit;
}
fMinuit = new TMinuit(fDim);
#endif
fDim = func.NDim();
fgFunc = const_cast<ROOT::Math::IMultiGenFunction *>(&func);
fMinuit->SetFCN(&TMinuitMinimizer::Fcn);
double arglist[1];
arglist[0] = PrintLevel() - 1;
int ierr= 0;
fMinuit->mnexcm("SET PRINT",arglist,1,ierr);
}
void TMinuitMinimizer::SetFunction(const ROOT::Math::IMultiGradFunction & func) {
fDim = func.NDim();
#ifdef USE_STATIC_TMINUIT
if (fgMinuit == 0) fgMinuit = new TMinuit(fDim);
fMinuit = fgMinuit;
#else
if (fMinuit) delete fMinuit;
fMinuit = new TMinuit(fDim);
#endif
fDim = func.NDim();
fgFunc = const_cast<ROOT::Math::IMultiGradFunction *>(&func);
fMinuit->SetFCN(&TMinuitMinimizer::FcnGrad);
double arglist[1];
arglist[0] = PrintLevel() - 1;
int ierr= 0;
fMinuit->mnexcm("SET PRINT",arglist,1,ierr);
fMinuit->mnexcm("SET GRAD",arglist,0,ierr);
}
void TMinuitMinimizer::Fcn( int &, double * , double & f, double * x , int ) {
f = fgFunc->operator()(x);
}
void TMinuitMinimizer::FcnGrad( int &, double * g, double & f, double * x , int iflag ) {
ROOT::Math::IMultiGradFunction * gFunc = dynamic_cast<ROOT::Math::IMultiGradFunction *> ( fgFunc);
assert(gFunc != 0);
f = gFunc->operator()(x);
if (iflag == 2) gFunc->Gradient(x,g);
}
bool TMinuitMinimizer::SetVariable(unsigned int ivar, const std::string & name, double val, double step) {
if (fMinuit == 0) {
std::cerr << "TMinuitMinimizer: ERROR : invalid TMinuit pointer. Set function first " << std::endl;
}
fMinuit->DefineParameter(ivar , name.c_str(), val, step, 0., 0. );
return true;
}
bool TMinuitMinimizer::SetLimitedVariable(unsigned int ivar, const std::string & name, double val, double step, double lower, double upper) {
if (fMinuit == 0) {
std::cerr << "TMinuitMinimizer: ERROR : invalid TMinuit pointer. Set function first " << std::endl;
}
fMinuit->DefineParameter(ivar, name.c_str(), val, step, lower, upper );
return true;
}
#ifdef LATER
bool Minuit2Minimizer::SetLowerLimitedVariable(unsigned int ivar , const std::string & name , double val , double step , double lower ) {
double s = val-lower;
double upper = s*1.0E15;
if (s != 0) upper = 1.0E15;
return SetLimitedVariable(ivar, name, val, step, lower,upper);
}
#endif
bool TMinuitMinimizer::SetFixedVariable(unsigned int ivar, const std::string & name, double val) {
if (fMinuit == 0) {
std::cerr << "TMinuitMinimizer: ERROR : invalid TMinuit pointer. Set function first " << std::endl;
}
fMinuit->DefineParameter(ivar, name.c_str(), val, 0., val, val );
fMinuit->FixParameter(ivar);
return true;
}
bool TMinuitMinimizer::Minimize() {
assert(fMinuit != 0 );
double arglist[10];
int ierr = 0;
arglist[0] = ErrorUp();
fMinuit->mnexcm("SET Err",arglist,1,ierr);
int printlevel = PrintLevel();
arglist[0] = printlevel - 1;
fMinuit->mnexcm("SET PRINT",arglist,1,ierr);
if (printlevel == 0) fMinuit->mnexcm("SET NOW",arglist,0,ierr);
arglist[0] = MaxFunctionCalls();
arglist[1] = Tolerance();
int nargs = 2;
switch (fType){
case ROOT::Minuit::kMigrad:
fMinuit->mnexcm("MIGRAD",arglist,nargs,ierr);
break;
case ROOT::Minuit::kCombined:
fMinuit->mnexcm("MINIMIZE",arglist,nargs,ierr);
break;
case ROOT::Minuit::kSimplex:
fMinuit->mnexcm("SIMPLEX",arglist,nargs,ierr);
break;
default:
fMinuit->mnexcm("MIGRAD",arglist,nargs,ierr);
}
if (ierr == 0 && fType == ROOT::Minuit::kMigradImproved)
fMinuit->mnexcm("IMPROVE",arglist,1,ierr);
if (ierr == 0 && IsValidError() ) {
fMinuit->mnexcm("HESSE",arglist,1,ierr);
}
int ntot;
int istat;
int nfree;
double errdef = 0;
fMinuit->mnstat(fMinVal,fEdm,errdef,nfree,ntot,istat);
assert (static_cast<unsigned int>(ntot) == fDim);
assert( nfree == fMinuit->GetNumFreePars() );
fNFree = nfree;
assert (errdef == ErrorUp());
fParams.resize( fDim);
fErrors.resize( fDim);
for (unsigned int i = 0; i < fDim; ++i) {
fMinuit->GetParameter( i, fParams[i], fErrors[i]);
}
if (ierr == 0) {
fCovar.resize(fDim*fDim);
fMinuit->mnemat(&fCovar.front(), fDim);
fMinosRun = false;
return true;
}
else {
return false;
}
}
bool TMinuitMinimizer::GetMinosError(unsigned int i, double & errLow, double & errUp) {
if (!fMinosRun) {
double arglist[2];
int ierr = 0;
arglist[0] = ErrorUp();
fMinuit->mnexcm("SET Err",arglist,1,ierr);
std::cout << "print level " << PrintLevel() << std::endl;
arglist[0] = PrintLevel()-1;
fMinuit->mnexcm("SET PRINT",arglist,1,ierr);
arglist[0] = MaxFunctionCalls();
arglist[1] = Tolerance();
int nargs = 2;
fMinuit->mnexcm("MINOS",arglist,nargs,ierr);
if (ierr != 0 ) return false;
fMinosRun = true;
}
double errParab = 0;
double gcor = 0;
fMinuit->mnerrs(i,errUp,errLow, errParab, gcor);
return true;
}
Last change: Wed Jun 25 08:49:37 2008
Last generated: 2008-06-25 08:49
This page has been automatically generated. If you have any comments or suggestions about the page layout send a mail to ROOT support, or contact the developers with any questions or problems regarding ROOT.