CMS 3D CMS Logo

RootMinuit.h
Go to the documentation of this file.
1 #ifndef PhisycsTools_Utilities_RootMinuit_h
2 #define PhisycsTools_Utilities_RootMinuit_h
3 
11 #include "TMinuit.h"
12 #include "Math/SMatrix.h"
13 #include <boost/shared_ptr.hpp>
14 #include <vector>
15 #include <string>
16 #include <memory>
17 
18 namespace fit {
19 
20  template<class Function>
21  class RootMinuit {
22  public:
23  RootMinuit(const Function& f, bool verbose = false) :
25  f_ = f;
26  }
27  void addParameter(const std::string & name, boost::shared_ptr<double> val, double err, double min, double max) {
28  if(initialized_)
30  << "RootMinuit: can't add parameter " << name
31  << " after minuit initialization\n";
32  pars_.push_back(val);
33  parameter_t par;
34  par.val = *val;
35  par.err = err;
36  par.min = min;
37  par.max = max;
38  par.fixed = false;
39  parMap_.push_back(std::make_pair(name, par));
40  size_t s = parIndices_.size();
41  parIndices_[name] = s;
42  }
43  void addParameter(const funct::Parameter & par, double err, double min, double max) {
44  return addParameter(par.name(), par, err, min, max);
45  }
46  double getParameter(const std::string & name, double & err) {
47  double val;
48  init();
49  minuit_->GetParameter(parameterIndex(name), val, err);
50  return val;
51  }
52  double getParameter(const std::string & name) {
53  double val, err;
54  init();
55  minuit_->GetParameter(parameterIndex(name), val, err);
56  return val;
57  }
58  double getParameterError(const std::string & name, double & val) {
59  double err;
60  init();
61  minuit_->GetParameter(parameterIndex(name), val, err);
62  return err;
63  }
65  double val, err;
66  init();
67  minuit_->GetParameter(parameterIndex(name), val, err);
68  return err;
69  }
70  template<unsigned int N>
71  void getErrorMatrix(ROOT::Math::SMatrix<double, N, N, ROOT::Math::MatRepSym<double, N> > & err) {
72  init();
73  if(N != numberOfParameters())
75  << "RootMinuit: can't call getErrorMatrix passing an SMatrix of dimension " << N
76  << " while the number of parameters is " << numberOfParameters() << "\n";
77  double * e = new double[N*N];
78  minuit_->mnemat(e, numberOfParameters());
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];
82  }
83  }
84  delete [] e;
85  setParameters();
86  }
87  void fixParameter(const std::string & name) {
88  size_t i = parameterIndex(name);
89  parMap_[i].second.fixed = true;
90  if(initialized_) {
91  minuit_->FixParameter(i);
92  }
93  }
95  size_t i = parameterIndex(name);
96  parMap_[i].second.fixed = false;
97  if(initialized_) {
98  minuit_->Release(i);
99  }
100  }
101  void setParameter(const std::string & name, double val) {
102  size_t i = parameterIndex(name);
103  parameter_t & par = parMap_[i].second;
104  par.val = val;
105  if(initialized_) {
106  int ierflg = 0;
107  minuit_->mnparm(i, name, par.val, par.err, par.min, par.max, ierflg);
108  if(ierflg != 0)
110  << "RootMinuit: error in setting parameter " << i
111  << " value = " << par.val << " error = " << par.err
112  << " range = [" << par.min << ", " << par.max << "]\n";
113  }
114  }
115  void setParameters() {
116  std::map<std::string, size_t>::const_iterator i = parIndices_.begin(), end = parIndices_.end();
117  double val, err;
118  for(; i != end; ++i) {
119  size_t index = i->second;
120  minuit_->GetParameter(index, val, err);
121  *pars_[index] = val;
122  }
123  }
125  init();
126  return minuit_->GetNumPars();
127  }
129  init();
130  return minuit_->GetNumFreePars();
131  }
132  double minimize() {
133  init();
134  double arglist[10];
135  arglist[0] = 5000;
136  arglist[1] = 0.1;
137  int ierflag;
138  minuit_->mnexcm("MINIMIZE", arglist, 2, ierflag);
139  if ( ierflag != 0 ) std::cerr << "ERROR in minimize!!" << std::endl;
140  if(verbose_) minuit_->mnmatu(1); //Prints the covariance matrix
141  double m = minValue();
142  if(verbose_) minuit_->mnprin(3, m);
143  setParameters();
144  return m;
145  }
146  double migrad() {
147  init();
148  double arglist[10];
149  arglist[0] = 5000;
150  arglist[1] = 0.1;
151  int ierflag;
152  minuit_->mnexcm("MIGRAD", arglist, 2, ierflag);
153  if ( ierflag != 0 ) std::cerr << "ERROR in migrad!!" << std::endl;
154  if(verbose_) minuit_->mnmatu(1); //Prints the covariance matrix
155  double m = minValue();
156  if(verbose_) minuit_->mnprin(3, m);
157  setParameters();
158  return m;
159  }
160  double minValue() {
161  init();
162  int ierflag;
163  double edm, errdef;
164  int nvpar, nparx;
165  minuit_->mnstat(minValue_, edm, errdef, nvpar, nparx, ierflag);
166  return minValue_;
167  }
168  void printParameters(std::ostream& cout = std::cout) {
169  std::map<std::string, size_t>::const_iterator i = parIndices_.begin(), end = parIndices_.end();
170  for(; i != end; ++i) {
171  cout << i->first << " = " << *pars_[i->second]
172  << " +/- " << getParameterError(i->first) << std::endl;
173  }
174  }
175  void printFitResults(std::ostream& cout = std::cout) {
178  }
179  private:
181  std::map<std::string, size_t> parIndices_;
183  double minValue_;
184  std::unique_ptr<TMinuit> minuit_;
185  std::vector<boost::shared_ptr<double> > pars_;
186  static std::vector<boost::shared_ptr<double> > *fPars_;
187  bool verbose_;
188  static Function f_;
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];
194  }
195  size_t parameterIndex(const std::string &name) const {
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";
200  return p->second;
201  }
202  void init() {
203  if(initialized_) return;
204  minuit_.reset(new TMinuit(parMap_.size()));
205  double arglist[10];
206  int ierflg = 0;
207  if (! verbose_) {
208  arglist[0] = -1;
209  minuit_->mnexcm("SET PRINT", arglist, 1, ierflg);
210  if (ierflg != 0)
212  << "RootMinuit: error in calling SET PRINT\n";
213  }
214  arglist[0] = 1;
215  minuit_->mnexcm("SET ERR", arglist, 1, ierflg);
216  if (ierflg != 0)
218  << "RootMinuit: error in calling SET ERR\n";
219 
220  size_t i = 0;
221  typename parameterVector_t::const_iterator p = parMap_.begin(), end = parMap_.end();
222  for(; p != end; ++p, ++i) {
223  const std::string & name = p->first;
224  const parameter_t & par = p->second;
225  minuit_->mnparm(i, name, par.val, par.err, par.min, par.max, ierflg);
226  if(ierflg != 0)
228  << "RootMinuit: error in setting parameter " << i
229  << " value = " << par.val << " error = " << par.err
230  << " range = [" << par.min << ", " << par.max << "]\n";
231  }
232  initialized_ = true;
233  for(i = 0, p = parMap_.begin(); p != end; ++p, ++i)
234  if(p->second.fixed)
235  minuit_->FixParameter(i);
236  fPars_= & pars_;
237  minuit_->SetFCN(fcn_);
238  }
239  };
240 
241  template<class Function>
243 
244  template<class Function>
245  std::vector<boost::shared_ptr<double> > * RootMinuit<Function>::fPars_ = 0;
246 }
247 
248 #endif
size
Write out results.
double minValue_
Definition: RootMinuit.h:183
double getParameterError(const std::string &name)
Definition: RootMinuit.h:64
const std::string & name() const
Definition: Parameter.h:13
void printParameters(std::ostream &cout=std::cout)
Definition: RootMinuit.h:168
void getErrorMatrix(ROOT::Math::SMatrix< double, N, N, ROOT::Math::MatRepSym< double, N > > &err)
Definition: RootMinuit.h:71
void printFitResults(std::ostream &cout=std::cout)
Definition: RootMinuit.h:175
std::vector< boost::shared_ptr< double > > pars_
Definition: RootMinuit.h:185
void setParameter(const std::string &name, double val)
Definition: RootMinuit.h:101
std::map< std::string, size_t > parIndices_
Definition: RootMinuit.h:181
void setParameters()
Definition: RootMinuit.h:115
double migrad()
Definition: RootMinuit.h:146
double getParameterError(const std::string &name, double &val)
Definition: RootMinuit.h:58
size_t parameterIndex(const std::string &name) const
Definition: RootMinuit.h:195
void fixParameter(const std::string &name)
Definition: RootMinuit.h:87
static std::vector< boost::shared_ptr< double > > * fPars_
Definition: RootMinuit.h:186
void addParameter(const std::string &name, boost::shared_ptr< double > val, double err, double min, double max)
Definition: RootMinuit.h:27
static void print(double amin, unsigned int numberOfFreeParameters, const Function &f)
RootMinuit(const Function &f, bool verbose=false)
Definition: RootMinuit.h:23
std::vector< std::pair< std::string, parameter_t > > parameterVector_t
Definition: ParameterMap.h:14
double minimize()
Definition: RootMinuit.h:132
int numberOfFreeParameters()
Definition: RootMinuit.h:128
double getParameter(const std::string &name)
Definition: RootMinuit.h:52
double f[11][100]
#define end
Definition: vmac.h:39
T min(T a, T b)
Definition: MathUtil.h:58
#define N
Definition: blowfish.cc:9
static Function f_
Definition: RootMinuit.h:188
int numberOfParameters()
Definition: RootMinuit.h:124
std::unique_ptr< TMinuit > minuit_
Definition: RootMinuit.h:184
double getParameter(const std::string &name, double &err)
Definition: RootMinuit.h:46
HLT enums.
void addParameter(const funct::Parameter &par, double err, double min, double max)
Definition: RootMinuit.h:43
static void fcn_(int &, double *, double &f, double *par, int)
Definition: RootMinuit.h:189
parameterVector_t parMap_
Definition: RootMinuit.h:180
static double evaluate(const Function &f)
void releaseParameter(const std::string &name)
Definition: RootMinuit.h:94
double minValue()
Definition: RootMinuit.h:160