1 #ifndef PhisycsTools_Utilities_RootMinuit_h 2 #define PhisycsTools_Utilities_RootMinuit_h 12 #include "Math/SMatrix.h" 13 #include <boost/shared_ptr.hpp> 20 template<
class Function>
30 <<
"RootMinuit: can't add parameter " << name
31 <<
" after minuit initialization\n";
39 parMap_.push_back(std::make_pair(name, par));
70 template<
unsigned int N>
71 void getErrorMatrix(ROOT::Math::SMatrix<
double,
N,
N, ROOT::Math::MatRepSym<double, N> > & err) {
75 <<
"RootMinuit: can't call getErrorMatrix passing an SMatrix of dimension " <<
N 77 double *
e =
new double[
N*
N];
79 for(
size_t i = 0;
i <
N; ++
i) {
80 for(
size_t j = 0; j <=
i; ++j) {
81 err(
i, j) = e[
i + N*j];
110 <<
"RootMinuit: error in setting parameter " << i
111 <<
" value = " << par.
val <<
" error = " << par.
err 112 <<
" range = [" << par.
min <<
", " << par.
max <<
"]\n";
118 for(; i !=
end; ++
i) {
119 size_t index = i->second;
120 minuit_->GetParameter(index, val, err);
130 return minuit_->GetNumFreePars();
138 minuit_->mnexcm(
"MINIMIZE", arglist, 2, ierflag);
139 if ( ierflag != 0 )
std::cerr <<
"ERROR in minimize!!" << std::endl;
152 minuit_->mnexcm(
"MIGRAD", arglist, 2, ierflag);
153 if ( ierflag != 0 )
std::cerr <<
"ERROR in migrad!!" << std::endl;
170 for(; i !=
end; ++
i) {
171 cout << i->first <<
" = " << *
pars_[i->second]
185 std::vector<boost::shared_ptr<double> >
pars_;
186 static std::vector<boost::shared_ptr<double> > *
fPars_;
189 static void fcn_(
int &,
double *,
double &
f,
double *par,
int) {
190 size_t size = fPars_->size();
191 for(
size_t i = 0;
i <
size; ++
i)
192 *((*fPars_)[
i]) = par[
i];
196 typename std::map<std::string, size_t>::const_iterator
p = parIndices_.find(name);
197 if(p == parIndices_.end())
199 <<
"RootMinuit: can't find parameter " << name <<
"\n";
203 if(initialized_)
return;
204 minuit_.reset(
new TMinuit(parMap_.size()));
209 minuit_->mnexcm(
"SET PRINT", arglist, 1, ierflg);
212 <<
"RootMinuit: error in calling SET PRINT\n";
215 minuit_->mnexcm(
"SET ERR", arglist, 1, ierflg);
218 <<
"RootMinuit: error in calling SET ERR\n";
221 typename parameterVector_t::const_iterator
p = parMap_.begin(),
end = parMap_.end();
222 for(; p !=
end; ++
p, ++
i) {
225 minuit_->mnparm(i, name, par.
val, par.
err, par.
min, par.
max, ierflg);
228 <<
"RootMinuit: error in setting parameter " << i
229 <<
" value = " << par.
val <<
" error = " << par.
err 230 <<
" range = [" << par.
min <<
", " << par.
max <<
"]\n";
233 for(i = 0, p = parMap_.begin(); p !=
end; ++
p, ++
i)
235 minuit_->FixParameter(i);
237 minuit_->SetFCN(
fcn_);
241 template<
class Function>
244 template<
class Function>
double getParameterError(const std::string &name)
const std::string & name() const
void printParameters(std::ostream &cout=std::cout)
void getErrorMatrix(ROOT::Math::SMatrix< double, N, N, ROOT::Math::MatRepSym< double, N > > &err)
void printFitResults(std::ostream &cout=std::cout)
std::vector< boost::shared_ptr< double > > pars_
void setParameter(const std::string &name, double val)
std::map< std::string, size_t > parIndices_
double getParameterError(const std::string &name, double &val)
size_t parameterIndex(const std::string &name) const
void fixParameter(const std::string &name)
static std::vector< boost::shared_ptr< double > > * fPars_
void addParameter(const std::string &name, boost::shared_ptr< double > val, double err, double min, double max)
static void print(double amin, unsigned int numberOfFreeParameters, const Function &f)
RootMinuit(const Function &f, bool verbose=false)
std::vector< std::pair< std::string, parameter_t > > parameterVector_t
int numberOfFreeParameters()
double getParameter(const std::string &name)
std::unique_ptr< TMinuit > minuit_
double getParameter(const std::string &name, double &err)
void addParameter(const funct::Parameter &par, double err, double min, double max)
static void fcn_(int &, double *, double &f, double *par, int)
parameterVector_t parMap_
static double evaluate(const Function &f)
void releaseParameter(const std::string &name)