Logo ROOT  
Reference Guide
MnUserParameterState.cxx
Go to the documentation of this file.
1// @(#)root/minuit2:$Id$
2// Authors: M. Winkler, F. James, L. Moneta, A. Zsenei 2003-2005
3
4/**********************************************************************
5 * *
6 * Copyright (c) 2005 LCG ROOT Math team, CERN/PH-SFT *
7 * *
8 **********************************************************************/
9
13#include "Minuit2/MnPrint.h"
14
15namespace ROOT {
16
17namespace Minuit2 {
18
19//
20// construct from user parameters (befor minimization)
21//
22MnUserParameterState::MnUserParameterState(const std::vector<double> &par, const std::vector<double> &err)
23 : fValid(true), fCovarianceValid(false), fGCCValid(false), fCovStatus(-1), fFVal(0.), fEDM(0.), fNFcn(0),
24 fParameters(MnUserParameters(par, err)), fCovariance(MnUserCovariance()), fGlobalCC(MnGlobalCorrelationCoeff()),
25 fIntParameters(par), fIntCovariance(MnUserCovariance())
26{
27}
28
30 : fValid(true), fCovarianceValid(false), fGCCValid(false), fCovStatus(-1), fFVal(0.), fEDM(0.), fNFcn(0),
31 fParameters(par), fCovariance(MnUserCovariance()), fGlobalCC(MnGlobalCorrelationCoeff()),
32 fIntParameters(std::vector<double>()), fIntCovariance(MnUserCovariance())
33{
34 // construct from user parameters (befor minimization)
35
36 for (std::vector<MinuitParameter>::const_iterator ipar = MinuitParameters().begin();
37 ipar != MinuitParameters().end(); ++ipar) {
38 if ((*ipar).IsConst() || (*ipar).IsFixed())
39 continue;
40 if ((*ipar).HasLimits())
41 fIntParameters.push_back(Ext2int((*ipar).Number(), (*ipar).Value()));
42 else
43 fIntParameters.push_back((*ipar).Value());
44 }
45}
46
47//
48// construct from user parameters + errors (befor minimization)
49//
50MnUserParameterState::MnUserParameterState(const std::vector<double> &par, const std::vector<double> &cov,
51 unsigned int nrow)
52 : fValid(true), fCovarianceValid(true), fGCCValid(false), fCovStatus(-1), fFVal(0.), fEDM(0.), fNFcn(0),
53 fParameters(MnUserParameters()), fCovariance(MnUserCovariance(cov, nrow)), fGlobalCC(MnGlobalCorrelationCoeff()),
54 fIntParameters(par), fIntCovariance(MnUserCovariance(cov, nrow))
55{
56 // construct from user parameters + errors (before minimization) using std::vector for parameter error and // an
57 // std::vector of size n*(n+1)/2 for the covariance matrix and n (rank of cov matrix)
58
59 std::vector<double> err;
60 err.reserve(par.size());
61 for (unsigned int i = 0; i < par.size(); i++) {
62 assert(fCovariance(i, i) > 0.);
63 err.push_back(std::sqrt(fCovariance(i, i)));
64 }
65 fParameters = MnUserParameters(par, err);
66 assert(fCovariance.Nrow() == VariableParameters());
67}
68
69MnUserParameterState::MnUserParameterState(const std::vector<double> &par, const MnUserCovariance &cov)
70 : fValid(true), fCovarianceValid(true), fGCCValid(false), fCovStatus(-1), fFVal(0.), fEDM(0.), fNFcn(0),
71 fParameters(MnUserParameters()), fCovariance(cov), fGlobalCC(MnGlobalCorrelationCoeff()), fIntParameters(par),
72 fIntCovariance(cov)
73{
74 // construct from user parameters + errors (before minimization) using std::vector (params) and MnUserCovariance
75 // class
76
77 std::vector<double> err;
78 err.reserve(par.size());
79 for (unsigned int i = 0; i < par.size(); i++) {
80 assert(fCovariance(i, i) > 0.);
81 err.push_back(std::sqrt(fCovariance(i, i)));
82 }
83 fParameters = MnUserParameters(par, err);
84 assert(fCovariance.Nrow() == VariableParameters());
85}
86
88 : fValid(true), fCovarianceValid(true), fGCCValid(false), fCovStatus(-1), fFVal(0.), fEDM(0.), fNFcn(0),
89 fParameters(par), fCovariance(cov), fGlobalCC(MnGlobalCorrelationCoeff()), fIntParameters(std::vector<double>()),
90 fIntCovariance(cov)
91{
92 // construct from user parameters + errors (befor minimization) using
93 // MnUserParameters and MnUserCovariance objects
94
96 for (std::vector<MinuitParameter>::const_iterator ipar = MinuitParameters().begin();
97 ipar != MinuitParameters().end(); ++ipar) {
98 if ((*ipar).IsConst() || (*ipar).IsFixed())
99 continue;
100 if ((*ipar).HasLimits())
101 fIntParameters.push_back(Ext2int((*ipar).Number(), (*ipar).Value()));
102 else
103 fIntParameters.push_back((*ipar).Value());
104 }
105 assert(fCovariance.Nrow() == VariableParameters());
106 //
107 // need to Fix that in case of limited parameters
108 // fIntCovariance = MnUserCovariance();
109 //
110}
111
112//
113//
115 : fValid(st.IsValid()), fCovarianceValid(false), fGCCValid(false), fCovStatus(-1), fFVal(st.Fval()), fEDM(st.Edm()),
116 fNFcn(st.NFcn()), fParameters(MnUserParameters()), fCovariance(MnUserCovariance()),
117 fGlobalCC(MnGlobalCorrelationCoeff()), fIntParameters(std::vector<double>()), fIntCovariance(MnUserCovariance())
118{
119 //
120 // construct from internal parameters (after minimization)
121 //
122 // std::cout << "build a MnUSerParameterState after minimization.." << std::endl;
123
124 for (std::vector<MinuitParameter>::const_iterator ipar = trafo.Parameters().begin();
125 ipar != trafo.Parameters().end(); ++ipar) {
126 if ((*ipar).IsConst()) {
127 Add((*ipar).GetName(), (*ipar).Value());
128 } else if ((*ipar).IsFixed()) {
129 Add((*ipar).GetName(), (*ipar).Value(), (*ipar).Error());
130 if ((*ipar).HasLimits()) {
131 if ((*ipar).HasLowerLimit() && (*ipar).HasUpperLimit())
132 SetLimits((*ipar).GetName(), (*ipar).LowerLimit(), (*ipar).UpperLimit());
133 else if ((*ipar).HasLowerLimit() && !(*ipar).HasUpperLimit())
134 SetLowerLimit((*ipar).GetName(), (*ipar).LowerLimit());
135 else
136 SetUpperLimit((*ipar).GetName(), (*ipar).UpperLimit());
137 }
138 Fix((*ipar).GetName());
139 } else if ((*ipar).HasLimits()) {
140 unsigned int i = trafo.IntOfExt((*ipar).Number());
141 double err =
142 st.Error().IsValid() ? std::sqrt(2. * up * st.Error().InvHessian()(i, i)) : st.Parameters().Dirin()(i);
143 Add((*ipar).GetName(), trafo.Int2ext(i, st.Vec()(i)), trafo.Int2extError(i, st.Vec()(i), err));
144 if ((*ipar).HasLowerLimit() && (*ipar).HasUpperLimit())
145 SetLimits((*ipar).GetName(), (*ipar).LowerLimit(), (*ipar).UpperLimit());
146 else if ((*ipar).HasLowerLimit() && !(*ipar).HasUpperLimit())
147 SetLowerLimit((*ipar).GetName(), (*ipar).LowerLimit());
148 else
149 SetUpperLimit((*ipar).GetName(), (*ipar).UpperLimit());
150 } else {
151 unsigned int i = trafo.IntOfExt((*ipar).Number());
152 double err =
153 st.Error().IsValid() ? std::sqrt(2. * up * st.Error().InvHessian()(i, i)) : st.Parameters().Dirin()(i);
154 Add((*ipar).GetName(), st.Vec()(i), err);
155 }
156 }
157
158 // need to be set afterwards because becore the ::Add method set fCovarianceValid to false
160
161 fCovStatus = -1; // when not available
162 // if (st.Error().HesseFailed() || st.Error().InvertFailed() ) fCovStatus = -1;
163 // when available
164 if (st.Error().IsAvailable())
165 fCovStatus = 0;
166
167 if (fCovarianceValid) {
168 fCovariance = trafo.Int2extCovariance(st.Vec(), st.Error().InvHessian());
170 MnUserCovariance(std::vector<double>(st.Error().InvHessian().Data(),
171 st.Error().InvHessian().Data() + st.Error().InvHessian().size()),
172 st.Error().InvHessian().Nrow());
173 fCovariance.Scale(2. * up);
176
177 assert(fCovariance.Nrow() == VariableParameters());
178
179 fCovStatus = 1; // when is valid
180 }
181 if (st.Error().IsMadePosDef())
182 fCovStatus = 2;
183 if (st.Error().IsAccurate())
184 fCovStatus = 3;
185}
186
188{
189 // invert covariance matrix and return Hessian
190 // need to copy in a MnSymMatrix
191 MnPrint print("MnUserParameterState::Hessian");
192
194 std::copy(fCovariance.Data().begin(), fCovariance.Data().end(), mat.Data());
195 int ifail = Invert(mat);
196 if (ifail != 0) {
197 print.Warn("Inversion failed; return diagonal matrix");
198
200 for (unsigned int i = 0; i < fCovariance.Nrow(); i++) {
201 tmp(i, i) = 1. / fCovariance(i, i);
202 }
203 return tmp;
204 }
205
206 MnUserCovariance hessian(mat.Data(), fCovariance.Nrow());
207 return hessian;
208}
209
210// facade: forward interface of MnUserParameters and MnUserTransformation
211// via MnUserParameterState
212
213const std::vector<MinuitParameter> &MnUserParameterState::MinuitParameters() const
214{
215 // access to parameters (row-wise)
216 return fParameters.Parameters();
217}
218
219std::vector<double> MnUserParameterState::Params() const
220{
221 // access to parameters in column-wise representation
222 return fParameters.Params();
223}
224std::vector<double> MnUserParameterState::Errors() const
225{
226 // access to errors in column-wise representation
227 return fParameters.Errors();
228}
229
231{
232 // access to single Parameter i
233 return fParameters.Parameter(i);
234}
235
236void MnUserParameterState::Add(const std::string &name, double val, double err)
237{
238 MnPrint print("MnUserParameterState::Add");
239
240 // add free Parameter
241 if (fParameters.Add(name, val, err)) {
242 fIntParameters.push_back(val);
243 fCovarianceValid = false;
244 fGCCValid = false;
245 fValid = true;
246 } else {
247 // redefine an existing parameter
248 int i = Index(name);
249 SetValue(i, val);
250 if (Parameter(i).IsConst()) {
251 print.Warn("Cannot modify status of constant parameter", name);
252 return;
253 }
254 SetError(i, err);
255 // release if it was fixed
256 if (Parameter(i).IsFixed())
257 Release(i);
258 }
259}
260
261void MnUserParameterState::Add(const std::string &name, double val, double err, double low, double up)
262{
263 MnPrint print("MnUserParameterState::Add");
264
265 // add limited Parameter
266 if (fParameters.Add(name, val, err, low, up)) {
267 fCovarianceValid = false;
268 fIntParameters.push_back(Ext2int(Index(name), val));
269 fGCCValid = false;
270 fValid = true;
271 } else { // Parameter already exist - just set values
272 int i = Index(name);
273 SetValue(i, val);
274 if (Parameter(i).IsConst()) {
275 print.Warn("Cannot modify status of constant parameter", name);
276 return;
277 }
278 SetError(i, err);
279 SetLimits(i, low, up);
280 // release if it was fixed
281 if (Parameter(i).IsFixed())
282 Release(i);
283 }
284}
285
286void MnUserParameterState::Add(const std::string &name, double val)
287{
288 // add const Parameter
289 if (fParameters.Add(name, val))
290 fValid = true;
291 else
292 SetValue(name, val);
293}
294
295// interaction via external number of Parameter
296
297void MnUserParameterState::Fix(unsigned int e)
298{
299 // fix parameter e (external index)
300 if (!Parameter(e).IsFixed() && !Parameter(e).IsConst()) {
301 unsigned int i = IntOfExt(e);
302 if (fCovarianceValid) {
305 }
306 fIntParameters.erase(fIntParameters.begin() + i, fIntParameters.begin() + i + 1);
307 }
309 fGCCValid = false;
310}
311
313{
314 // release parameter e (external index)
315 // no-op if parameter is const or if it is not fixed
316 if (Parameter(e).IsConst() || !Parameter(e).IsFixed())
317 return;
319 fCovarianceValid = false;
320 fGCCValid = false;
321 unsigned int i = IntOfExt(e);
322 if (Parameter(e).HasLimits())
323 fIntParameters.insert(fIntParameters.begin() + i, Ext2int(e, Parameter(e).Value()));
324 else
325 fIntParameters.insert(fIntParameters.begin() + i, Parameter(e).Value());
326}
327
328void MnUserParameterState::SetValue(unsigned int e, double val)
329{
330 // set value for parameter e ( external index )
331 fParameters.SetValue(e, val);
332 if (!Parameter(e).IsFixed() && !Parameter(e).IsConst()) {
333 unsigned int i = IntOfExt(e);
334 if (Parameter(e).HasLimits())
335 fIntParameters[i] = Ext2int(e, val);
336 else
337 fIntParameters[i] = val;
338 }
339}
340
341void MnUserParameterState::SetError(unsigned int e, double val)
342{
343 // set error for parameter e (external index)
344 fParameters.SetError(e, val);
345}
346
347void MnUserParameterState::SetLimits(unsigned int e, double low, double up)
348{
349 // set limits for parameter e (external index)
350 fParameters.SetLimits(e, low, up);
351 fCovarianceValid = false;
352 fGCCValid = false;
353 if (!Parameter(e).IsFixed() && !Parameter(e).IsConst()) {
354 unsigned int i = IntOfExt(e);
355 if (low < fIntParameters[i] && fIntParameters[i] < up)
357 else if (low >= fIntParameters[i])
358 fIntParameters[i] = Ext2int(e, low + 0.1 * Parameter(e).Error());
359 else
360 fIntParameters[i] = Ext2int(e, up - 0.1 * Parameter(e).Error());
361 }
362}
363
364void MnUserParameterState::SetUpperLimit(unsigned int e, double up)
365{
366 // set upper limit for parameter e (external index)
368 fCovarianceValid = false;
369 fGCCValid = false;
370 if (!Parameter(e).IsFixed() && !Parameter(e).IsConst()) {
371 unsigned int i = IntOfExt(e);
372 if (fIntParameters[i] < up)
374 else
375 fIntParameters[i] = Ext2int(e, up - 0.1 * Parameter(e).Error());
376 }
377}
378
379void MnUserParameterState::SetLowerLimit(unsigned int e, double low)
380{
381 // set lower limit for parameter e (external index)
383 fCovarianceValid = false;
384 fGCCValid = false;
385 if (!Parameter(e).IsFixed() && !Parameter(e).IsConst()) {
386 unsigned int i = IntOfExt(e);
387 if (low < fIntParameters[i])
389 else
390 fIntParameters[i] = Ext2int(e, low + 0.1 * Parameter(e).Error());
391 }
392}
393
395{
396 // remove limit for parameter e (external index)
398 fCovarianceValid = false;
399 fGCCValid = false;
400 if (!Parameter(e).IsFixed() && !Parameter(e).IsConst())
402}
403
404double MnUserParameterState::Value(unsigned int i) const
405{
406 // get value for parameter e (external index)
407 return fParameters.Value(i);
408}
409double MnUserParameterState::Error(unsigned int i) const
410{
411 // get error for parameter e (external index)
412 return fParameters.Error(i);
413}
414
415// interaction via name of Parameter
416
417void MnUserParameterState::Fix(const std::string &name)
418{
419 Fix(Index(name));
420}
421
422void MnUserParameterState::Release(const std::string &name)
423{
425}
426
427void MnUserParameterState::SetValue(const std::string &name, double val)
428{
429 SetValue(Index(name), val);
430}
431
432void MnUserParameterState::SetError(const std::string &name, double val)
433{
434 SetError(Index(name), val);
435}
436
437void MnUserParameterState::SetLimits(const std::string &name, double low, double up)
438{
439 SetLimits(Index(name), low, up);
440}
441
442void MnUserParameterState::SetUpperLimit(const std::string &name, double up)
443{
445}
446
447void MnUserParameterState::SetLowerLimit(const std::string &name, double low)
448{
449 SetLowerLimit(Index(name), low);
450}
451
453{
455}
456
457double MnUserParameterState::Value(const std::string &name) const
458{
459 return Value(Index(name));
460}
461
462double MnUserParameterState::Error(const std::string &name) const
463{
464 return Error(Index(name));
465}
466
467unsigned int MnUserParameterState::Index(const std::string &name) const
468{
469 // convert name into external number of Parameter
470 return fParameters.Index(name);
471}
472
473const char *MnUserParameterState::Name(unsigned int i) const
474{
475 // convert external number into name of Parameter (API returing a const char *)
476 return fParameters.Name(i);
477}
478const std::string &MnUserParameterState::GetName(unsigned int i) const
479{
480 // convert external number into name of Parameter (new interface returning a string)
481 return fParameters.GetName(i);
482}
483
484// transformation internal <-> external (forward to transformation class)
485
486double MnUserParameterState::Int2ext(unsigned int i, double val) const
487{
488 // internal to external value
489 return fParameters.Trafo().Int2ext(i, val);
490}
491double MnUserParameterState::Ext2int(unsigned int e, double val) const
492{
493 // external to internal value
494 return fParameters.Trafo().Ext2int(e, val);
495}
496unsigned int MnUserParameterState::IntOfExt(unsigned int ext) const
497{
498 // return internal index for external index ext
499 return fParameters.Trafo().IntOfExt(ext);
500}
501unsigned int MnUserParameterState::ExtOfInt(unsigned int internal) const
502{
503 // return external index for internal index internal
504 return fParameters.Trafo().ExtOfInt(internal);
505}
507{
508 // return number of variable parameters
510}
512{
513 // return global parameter precision
514 return fParameters.Precision();
515}
516
518{
519 // set global parameter precision
521}
522
523} // namespace Minuit2
524
525} // namespace ROOT
double
Definition: Converters.cxx:939
#define e(i)
Definition: RSha256.hxx:103
char name[80]
Definition: TGX11.cxx:110
Class describing a symmetric matrix of size n.
Definition: LASymMatrix.h:45
const double * Data() const
Definition: LASymMatrix.h:268
unsigned int Nrow() const
Definition: LASymMatrix.h:274
unsigned int size() const
Definition: LASymMatrix.h:272
const MnAlgebraicSymMatrix & InvHessian() const
Definition: MinimumError.h:54
const MnAlgebraicVector & Dirin() const
MinimumState keeps the information (position, Gradient, 2nd deriv, etc) after one minimization step (...
Definition: MinimumState.h:27
const MinimumError & Error() const
Definition: MinimumState.h:62
const MnAlgebraicVector & Vec() const
Definition: MinimumState.h:59
const MinimumParameters & Parameters() const
Definition: MinimumState.h:58
class for the individual Minuit Parameter with Name and number; contains the input numbers for the mi...
class to reduce the covariance matrix when a parameter is fixed by removing the corresponding row and...
class for global correlation coefficient
Sets the relative floating point (double) arithmetic precision.
void Warn(const Ts &... args)
Definition: MnPrint.h:126
Class containing the covariance matrix data represented as a vector of size n*(n+1)/2 Used to hide in...
const std::vector< double > & Data() const
const MnMachinePrecision & Precision() const
void SetLimits(unsigned int, double, double)
unsigned int Index(const std::string &) const
const std::string & GetName(unsigned int) const
double Int2ext(unsigned int, double) const
const MinuitParameter & Parameter(unsigned int i) const
double Ext2int(unsigned int, double) const
void Add(const std::string &name, double val, double err)
unsigned int ExtOfInt(unsigned int) const
const char * Name(unsigned int) const
MnUserParameterState()
default constructor (invalid state)
const std::vector< ROOT::Minuit2::MinuitParameter > & MinuitParameters() const
facade: forward interface of MnUserParameters and MnUserTransformation
unsigned int IntOfExt(unsigned int) const
void SetUpperLimit(unsigned int, double)
std::vector< double > Errors() const
void SetLowerLimit(unsigned int, double)
std::vector< double > Params() const
API class for the user interaction with the parameters; serves as input to the minimizer as well as o...
double Error(unsigned int) const
std::vector< double > Params() const
access to parameters and errors in column-wise representation
const char * Name(unsigned int) const
const MinuitParameter & Parameter(unsigned int) const
access to single Parameter
unsigned int Index(const std::string &) const
double Value(unsigned int) const
const MnMachinePrecision & Precision() const
void Fix(unsigned int)
interaction via external number of Parameter
void SetLowerLimit(unsigned int, double)
void SetError(unsigned int, double)
void SetValue(unsigned int, double)
const std::vector< ROOT::Minuit2::MinuitParameter > & Parameters() const
access to parameters (row-wise)
const MnUserTransformation & Trafo() const
std::vector< double > Errors() const
const std::string & GetName(unsigned int) const
void SetUpperLimit(unsigned int, double)
bool Add(const std::string &, double, double)
Add free Parameter Name, Value, Error.
void SetLimits(unsigned int, double, double)
class dealing with the transformation between user specified parameters (external) and internal param...
double Ext2int(unsigned int, double) const
MnUserCovariance Int2extCovariance(const MnAlgebraicVector &, const MnAlgebraicSymMatrix &) const
unsigned int ExtOfInt(unsigned int internal) const
const std::vector< MinuitParameter > & Parameters() const
unsigned int IntOfExt(unsigned int) const
double Int2ext(unsigned int, double) const
double Int2extError(unsigned int, double, double) const
VecExpr< UnaryOp< Sqrt< T >, VecExpr< A, T, D >, T >, T, D > sqrt(const VecExpr< A, T, D > &rhs)
int Invert(LASymMatrix &)
Definition: LaInverse.cxx:21
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...