Belle II Software  release-08-01-10
NewFitterGSL Class Reference

A kinematic fitter using the Newton-Raphson method to solve the equations. More...

#include <NewFitterGSL.h>

Inheritance diagram for NewFitterGSL:
Collaboration diagram for NewFitterGSL:

Public Types

enum  {
  NPARMAX = 50 ,
  NCONMAX = 10 ,
  NUNMMAX = 10
}
 
enum  { NITMAX = 100 }
 

Public Member Functions

 NewFitterGSL ()
 Constructor.
 
virtual ~NewFitterGSL ()
 Virtual destructor.
 
virtual double fit () override
 The fit method, returns the fit probability.
 
virtual int getError () const override
 Get the error code of the last fit: 0=OK, 1=failed.
 
virtual double getProbability () const override
 Get the fit probability of the last fit.
 
virtual double getChi2 () const override
 Get the chi**2 of the last fit.
 
virtual int getDoF () const override
 Get the number of degrees of freedom of the last fit.
 
virtual int getIterations () const override
 Get the number of iterations of the last fit.
 
virtual int getNcon () const
 Get the number of hard constraints of the last fit.
 
virtual int getNsoft () const
 Get the number of soft constraints of the last fit.
 
virtual int getNpar () const
 Get the number of all parameters of the last fit.
 
virtual int getNunm () const
 Get the number of unmeasured parameters of the last fit.
 
virtual bool initialize () override
 Initialize the fitter.
 
virtual void setDebug (int debuglevel)
 Set the Debug Level.
 
virtual int determineLambdas (gsl_vector *vecxnew, const gsl_matrix *MatM, const gsl_vector *vecx, gsl_matrix *MatW, gsl_vector *vecw, double eps=0)
 Determine best lambda values. More...
 
virtual void calc2ndOrderCorr (gsl_vector *vecdxhat, const gsl_vector *vecxnew, const gsl_matrix *MatM, gsl_matrix *MatW, gsl_vector *vecw, double eps=0)
 Calculate 2nd order correction step. More...
 
virtual gsl_matrix_view calcZ (int &rankA, gsl_matrix *MatW1, gsl_matrix *MatW2, gsl_vector *vecw1, gsl_vector *vecw2, gsl_permutation *permW, double eps=0)
 Calculate null space of constraints; return value is a matrix view of MatW1, with column vectors spanning null(A) More...
 
virtual gsl_matrix_view calcReducedHessian (int &rankH, gsl_matrix *MatW1, const gsl_vector *vecx, gsl_matrix *MatW2, gsl_matrix *MatW3, gsl_vector *vecw1, gsl_vector *vecw2, gsl_permutation *permW, double eps=0)
 Calculate reduced Hessian; return value is a matrix view of MatW1 giving the reduced Hessian. More...
 
virtual gsl_vector_view calcReducedHessianEigenvalues (int &rankH, gsl_matrix *MatW1, const gsl_vector *vecx, gsl_matrix *MatW2, gsl_matrix *MatW3, gsl_vector *vecw1, gsl_vector *vecw2, gsl_permutation *permW, gsl_eigen_symm_workspace *eigenws, double eps=0)
 
virtual double calcChi2 ()
 Calculate the chi2.
 
void fillx (gsl_vector *vecx)
 
void fillperr (gsl_vector *vece)
 
void assembleM (gsl_matrix *MatM, const gsl_vector *vecx, bool errorpropagation=false)
 
void assembleG (gsl_matrix *MatM, const gsl_vector *vecx)
 
void scaleM (gsl_matrix *MatMscal, const gsl_matrix *MatM, const gsl_vector *vece)
 
void assembley (gsl_vector *vecy, const gsl_vector *vecx)
 
void scaley (gsl_vector *vecyscal, const gsl_vector *vecy, const gsl_vector *vece)
 
int assembleChi2Der (gsl_vector *vecy)
 
void addConstraints (gsl_vector *vecy)
 
void assembleConstDer (gsl_matrix *MatM)
 
int calcNewtonDx (gsl_vector *vecdx, gsl_vector *vecdxscal, gsl_vector *vecx, const gsl_vector *vece, gsl_matrix *MatM, gsl_matrix *MatMscal, gsl_vector *vecy, gsl_vector *vecyscal, gsl_matrix *MatW, gsl_matrix *MatW2, gsl_permutation *permW, gsl_vector *vecw)
 
int calcLimitedDx (double &alpha, double &mu, gsl_vector *vecxnew, int imode, gsl_vector *vecx, gsl_vector *vecdxhat, const gsl_vector *vecdx, const gsl_vector *vecdxscal, const gsl_vector *vece, const gsl_matrix *MatM, const gsl_matrix *MatMscal, gsl_matrix *MatW, gsl_vector *vecw)
 
int doLineSearch (double &alpha, gsl_vector *vecxnew, int imode, double phi0, double dphi0, double eta, double zeta, double mu, const gsl_vector *vecx, const gsl_vector *vecdx, const gsl_vector *vece, gsl_vector *vecw)
 
double calcMu (const gsl_vector *vecx, const gsl_vector *vece, const gsl_vector *vecdx, const gsl_vector *vecdxscal, const gsl_vector *xnew, const gsl_matrix *MatM, const gsl_matrix *MatMscal, gsl_vector *vecw)
 
double meritFunction (double mu, const gsl_vector *vecx, const gsl_vector *vece)
 
double meritFunctionDeriv (double mu, const gsl_vector *vecx, const gsl_vector *vece, const gsl_vector *vecdx, gsl_vector *vecw)
 
bool updateParams (gsl_vector *vecx)
 
int invertM ()
 
int calcCovMatrix (gsl_matrix *MatW, gsl_permutation *permW, gsl_vector *vecx)
 
double calcpTLp (const gsl_vector *vecdx, const gsl_matrix *MatM, gsl_vector *vecw)
 
int solveSystem (gsl_vector *vecdxscal, double &detW, const gsl_vector *vecyscal, const gsl_matrix *MatMscal, gsl_matrix *MatW, gsl_matrix *MatW2, gsl_vector *vecw, double epsLU, double epsSV)
 solve system of equations Mscal*dxscal = yscal
 
int solveSystemLU (gsl_vector *vecdxscal, double &detW, const gsl_vector *vecyscal, const gsl_matrix *MatMscal, gsl_matrix *MatW, gsl_vector *vecw, double eps)
 solve system of equations Mscal*dxscal = yscal using LU decomposition
 
int solveSystemSVD (gsl_vector *vecdxscal, const gsl_vector *vecyscal, const gsl_matrix *MatMscal, gsl_matrix *MatW, gsl_matrix *MatW2, gsl_vector *vecw, double eps)
 solve system of equations Mscal*dxscal = yscal using SVD decomposition
 
virtual void addFitObject (BaseFitObject *fitobject_)
 
virtual void addFitObject (BaseFitObject &fitobject_)
 
virtual void addConstraint (BaseConstraint *constraint_)
 
virtual void addConstraint (BaseConstraint &constraint_)
 
virtual void addHardConstraint (BaseHardConstraint *constraint_)
 
virtual void addHardConstraint (BaseHardConstraint &constraint_)
 
virtual void addSoftConstraint (BaseSoftConstraint *constraint_)
 
virtual void addSoftConstraint (BaseSoftConstraint &constraint_)
 
virtual std::vector< BaseFitObject * > * getFitObjects ()
 
virtual std::vector< BaseHardConstraint * > * getConstraints ()
 
virtual std::vector< BaseSoftConstraint * > * getSoftConstraints ()
 
virtual void reset ()
 
virtual BaseTracergetTracer ()
 
virtual const BaseTracergetTracer () const
 
virtual void setTracer (BaseTracer *newTracer)
 
virtual void setTracer (BaseTracer &newTracer)
 
virtual const double * getGlobalCovarianceMatrix (int &idim) const
 
virtual double * getGlobalCovarianceMatrix (int &idim)
 

Static Public Member Functions

static void ini_gsl_permutation (gsl_permutation *&p, unsigned int size)
 
static void ini_gsl_vector (gsl_vector *&v, int unsigned size)
 
static void ini_gsl_matrix (gsl_matrix *&m, int unsigned size1, unsigned int size2)
 
static void debug_print (const gsl_matrix *m, const char *name)
 
static void debug_print (const gsl_vector *v, const char *name)
 
static void add (gsl_vector *vecz, const gsl_vector *vecx, double a, const gsl_vector *vecy)
 
static bool isfinite (const gsl_vector *vec)
 
static bool isfinite (const gsl_matrix *mat)
 
static void MoorePenroseInverse (gsl_matrix *Ainv, gsl_matrix *A, gsl_matrix *W, gsl_vector *w, double eps=0)
 Compute the Moore-Penrose pseudo-inverse A+ of A, using SVD. More...
 

Public Attributes

int npar
 total number of parameters
 
int ncon
 total number of hard constraints
 
int nsoft
 total number of soft constraints
 
int nunm
 total number of unmeasured parameters
 
int ierr
 Error status.
 
int nit
 Number of iterations.
 
double fitprob
 fit probability
 
double chi2
 final chi2
 
unsigned int idim
 
gsl_vector * x
 
gsl_vector * xold
 
gsl_vector * xnew
 
gsl_vector * dx
 
gsl_vector * dxscal
 
gsl_vector * y
 
gsl_vector * yscal
 
gsl_vector * perr
 
gsl_vector * v1
 
gsl_vector * v2
 
gsl_matrix * M
 
gsl_matrix * Mscal
 
gsl_matrix * W
 
gsl_matrix * W2
 
gsl_matrix * W3
 
gsl_matrix * M1
 
gsl_matrix * M2
 
gsl_matrix * M3
 
gsl_matrix * M4
 
gsl_matrix * M5
 
gsl_matrix * CC
 
gsl_matrix * CC1
 
gsl_matrix * CCinv
 
gsl_permutation * permW
 
gsl_eigen_symm_workspace * eigenws
 
unsigned int eigenwsdim
 
double chi2best
 
double chi2new
 
double chi2old
 
double fvalbest
 
double scale
 
double scalebest
 
double stepsize
 
double stepbest
 
double scalevals [NITMAX]
 
double fvals [NITMAX]
 
int imerit
 
bool try2ndOrderCorr
 
int debug
 
std::map< std::string, double > traceValues
 

Protected Types

typedef std::vector< BaseFitObject * > FitObjectContainer
 
typedef std::vector< BaseHardConstraint * > ConstraintContainer
 
typedef std::vector< BaseSoftConstraint * > SoftConstraintContainer
 
typedef FitObjectContainer::iterator FitObjectIterator
 
typedef ConstraintContainer::iterator ConstraintIterator
 
typedef SoftConstraintContainer::iterator SoftConstraintIterator
 

Protected Attributes

FitObjectContainer fitobjects
 
ConstraintContainer constraints
 
SoftConstraintContainer softconstraints
 
int covDim
 dimension of global covariance matrix
 
double * cov
 global covariance matrix of last fit problem
 
bool covValid
 Flag whether global covariance is valid.
 
BaseTracertracer
 

Detailed Description

A kinematic fitter using the Newton-Raphson method to solve the equations.

This class implements a kinematic fitter using the Newton-Raphson method to solve the system of equations arising from the Lagrange multiplier method

Author: Benno List Last update:

Date
2011/05/03 13:16:41

by:

Author
blist

Changelog:

  • 15.11.2010 First version

Definition at line 51 of file NewFitterGSL.h.

Member Function Documentation

◆ calc2ndOrderCorr()

void calc2ndOrderCorr ( gsl_vector *  vecdxhat,
const gsl_vector *  vecxnew,
const gsl_matrix *  MatM,
gsl_matrix *  MatW,
gsl_vector *  vecw,
double  eps = 0 
)
virtual

Calculate 2nd order correction step.

Parameters
vecdxhatthe correction step
vecxnewthe current state vector (parameters must be set to vecxnew!)
MatMThe matrix of the last Newton step
MatWwork matrix
vecwwork vector
epsSingular values < eps*(max(abs(s_i))) are set to 0

Definition at line 1633 of file NewFitterGSL.cc.

1639  {
1640  assert(vecdxhat);
1641  assert(vecdxhat->size == idim);
1642  assert(vecxnew);
1643  assert(vecxnew->size == idim);
1644  assert(MatM);
1645  assert(MatM->size1 == idim && MatM->size2 == idim);
1646  assert(MatW);
1647  assert(MatW->size1 == idim && MatW->size2 == idim);
1648  assert(vecw);
1649  assert(vecw->size == idim);
1650  assert(idim == static_cast<unsigned int>(npar + ncon));
1651 
1652 
1653  // Calculate 2nd order correction
1654  // see Nocedal&Wright (15.36)
1655  gsl_matrix_const_view AT(gsl_matrix_const_submatrix(MatM, 0, npar, npar, ncon));
1656  gsl_matrix_view AAT(gsl_matrix_submatrix(MatW, npar, npar, ncon, ncon));
1657  gsl_matrix_view ATcopy(gsl_matrix_submatrix(MatW, 0, npar, npar, ncon));
1658  gsl_matrix_view& V = AAT;
1659 
1660  gsl_vector_set_zero(vecdxhat);
1661  addConstraints(vecdxhat);
1662  gsl_vector_view c(gsl_vector_subvector(vecdxhat, npar, ncon));
1663  gsl_vector_view phat = (gsl_vector_subvector(vecdxhat, 0, npar));
1664 
1665  gsl_vector_set_zero(vecw);
1666  gsl_vector_view AATinvc(gsl_vector_subvector(vecw, npar, ncon));
1667  gsl_vector_view& s = AATinvc;
1668 
1669 
1670  // AAT = 1*A*A^T + 0*AAT
1671  gsl_blas_dgemm(CblasTrans, CblasNoTrans, 1, &AT.matrix, &AT.matrix, 0, &AAT.matrix);
1672 
1673  // solve AAT * AATinvc = c using the Cholsky factorization method
1674  gsl_error_handler_t* old_handler = gsl_set_error_handler_off();
1675  int cholesky_result = gsl_linalg_cholesky_decomp(&AAT.matrix);
1676  gsl_set_error_handler(old_handler);
1677  if (cholesky_result) {
1678  B2INFO("NewFitterGSL::calc2ndOrderCorr: resorting to SVD");
1679  // AAT is not positive definite, i.e. A does not have full column rank
1680  // => use the SVD of AT to solve A lambdanew = gradf
1681 
1682  // ATcopy = AT
1683  gsl_matrix_memcpy(&ATcopy.matrix, &AT.matrix);
1684 
1685  // SVD decomposition of Acopy
1686  gsl_linalg_SV_decomp_jacobi(&ATcopy.matrix, &V.matrix, &s.vector);
1687  // set small values to zero
1688  double mins = eps * std::fabs(gsl_vector_get(&s.vector, 0));
1689  for (int i = 0; i < ncon; ++i) {
1690  if (std::fabs(gsl_vector_get(&s.vector, i)) <= mins)
1691  gsl_vector_set(&s.vector, i, 0);
1692  }
1693  gsl_linalg_SV_solve(&ATcopy.matrix, &V.matrix, &s.vector, &c.vector, &AATinvc.vector);
1694  } else {
1695  gsl_linalg_cholesky_solve(&AAT.matrix, &c.vector, &AATinvc.vector);
1696  }
1697 
1698  // phat = -1*A^T*AATinvc+ 0*phat
1699  gsl_blas_dgemv(CblasNoTrans, -1, &AT.matrix, &AATinvc.vector, 0, &phat.vector);
1700  gsl_vector_set_zero(&c.vector);
1701 
1702  }
int ncon
total number of hard constraints
Definition: NewFitterGSL.h:255
int npar
total number of parameters
Definition: NewFitterGSL.h:254

◆ calcLimitedDx()

int calcLimitedDx ( double &  alpha,
double &  mu,
gsl_vector *  vecxnew,
int  imode,
gsl_vector *  vecx,
gsl_vector *  vecdxhat,
const gsl_vector *  vecdx,
const gsl_vector *  vecdxscal,
const gsl_vector *  vece,
const gsl_matrix *  MatM,
const gsl_matrix *  MatMscal,
gsl_matrix *  MatW,
gsl_vector *  vecw 
)
Parameters
alphaOutput value alpha
muValue of mu for merit function
vecxnewNew vector x
imodemode: 0=Armijo, 1=Wolfe, 2=Goldstein
vecxCurrent vector x
vecdxhatthe 2nd order correction step, if any
vecdxUpdate vector dx
vecdxscalResult: Update vector dx, scaled
veceError vector e
MatMMatrix M
MatMscalMatrix M, scaled
MatWWork matrix
vecwWork vector w1

Definition at line 913 of file NewFitterGSL.cc.

◆ calcMu()

double calcMu ( const gsl_vector *  vecx,
const gsl_vector *  vece,
const gsl_vector *  vecdx,
const gsl_vector *  vecdxscal,
const gsl_vector *  xnew,
const gsl_matrix *  MatM,
const gsl_matrix *  MatMscal,
gsl_vector *  vecw 
)
Parameters
vecxCurrent vector x
veceCurrent errors x
vecdxCurrent step dx
vecdxscalCurrent step dx, scaled
xnewNew vector x
MatMCurrent matrix M
MatMscalCurrent scaled matrix M
vecwWork vector w

Definition at line 1161 of file NewFitterGSL.cc.

◆ calcNewtonDx()

int calcNewtonDx ( gsl_vector *  vecdx,
gsl_vector *  vecdxscal,
gsl_vector *  vecx,
const gsl_vector *  vece,
gsl_matrix *  MatM,
gsl_matrix *  MatMscal,
gsl_vector *  vecy,
gsl_vector *  vecyscal,
gsl_matrix *  MatW,
gsl_matrix *  MatW2,
gsl_permutation *  permW,
gsl_vector *  vecw 
)
Parameters
vecdxResult: Update vector dx
vecdxscalResult: Update vector dx, scaled
vecxCurrent vector x
veceCurrent ``error'' set of x
MatMMatrix M
MatMscalMatrix M, scaled
vecyVector y
vecyscalVector y, scaled,
MatWWork matrix
MatW2Work matrix
permWWork permutation vector
vecwWork vector

Definition at line 802 of file NewFitterGSL.cc.

◆ calcpTLp()

double calcpTLp ( const gsl_vector *  vecdx,
const gsl_matrix *  MatM,
gsl_vector *  vecw 
)
Parameters
vecdxCurrent step dx
MatMCurrent matrix M
vecwWork vector w

Definition at line 1613 of file NewFitterGSL.cc.

◆ calcReducedHessian()

gsl_matrix_view calcReducedHessian ( int &  rankH,
gsl_matrix *  MatW1,
const gsl_vector *  vecx,
gsl_matrix *  MatW2,
gsl_matrix *  MatW3,
gsl_vector *  vecw1,
gsl_vector *  vecw2,
gsl_permutation *  permW,
double  eps = 0 
)
virtual

Calculate reduced Hessian; return value is a matrix view of MatW1 giving the reduced Hessian.

Parameters
rankHdimension of H
MatW1work matrix, contains H at the end
vecxvector with current x values
MatW2work matrix, contains Z at the end
MatW3work matrix
vecw1work vector
vecw2work vector
permWWork permutation vector
epsSingular values < eps*(max(abs(s_i))) are set to 0

Definition at line 1870 of file NewFitterGSL.cc.

◆ calcReducedHessianEigenvalues()

gsl_vector_view calcReducedHessianEigenvalues ( int &  rankH,
gsl_matrix *  MatW1,
const gsl_vector *  vecx,
gsl_matrix *  MatW2,
gsl_matrix *  MatW3,
gsl_vector *  vecw1,
gsl_vector *  vecw2,
gsl_permutation *  permW,
gsl_eigen_symm_workspace *  eigenws,
double  eps = 0 
)
virtual
Parameters
rankHdimension of H
MatW1work matrix, contains H at the end
vecxvector with current x values
MatW2work matrix, contains Z at the end
MatW3work matrix
vecw1work vector
vecw2work vector
permWWork permutation vector
eigenwsWork space for eigenvalue calculation
epsSingular values < eps*(max(abs(s_i))) are set to 0

Definition at line 1914 of file NewFitterGSL.cc.

◆ calcZ()

gsl_matrix_view calcZ ( int &  rankA,
gsl_matrix *  MatW1,
gsl_matrix *  MatW2,
gsl_vector *  vecw1,
gsl_vector *  vecw2,
gsl_permutation *  permW,
double  eps = 0 
)
virtual

Calculate null space of constraints; return value is a matrix view of MatW1, with column vectors spanning null(A)

Parameters
rankArank of A (= number of lin. indep. constraints)
MatW1work matrix, contains Z at the end
MatW2work matrix
vecw1work vector
vecw2work vector
permWWork permutation vector
epsSingular values < eps*(max(abs(s_i))) are set to 0

Definition at line 1834 of file NewFitterGSL.cc.

◆ determineLambdas()

int determineLambdas ( gsl_vector *  vecxnew,
const gsl_matrix *  MatM,
const gsl_vector *  vecx,
gsl_matrix *  MatW,
gsl_vector *  vecw,
double  eps = 0 
)
virtual

Determine best lambda values.

Parameters
vecxnewvector with new lambda values
MatMmatrix with constraint derivatives
vecxvector with current x values
MatWwork matrix
vecwwork vector
epsSingular values < eps*(max(abs(s_i))) are set to 0

Definition at line 1482 of file NewFitterGSL.cc.

◆ doLineSearch()

int doLineSearch ( double &  alpha,
gsl_vector *  vecxnew,
int  imode,
double  phi0,
double  dphi0,
double  eta,
double  zeta,
double  mu,
const gsl_vector *  vecx,
const gsl_vector *  vecdx,
const gsl_vector *  vece,
gsl_vector *  vecw 
)
Parameters
alphaOutput value alpha
vecxnewNew vector x
imodemode: 0=Armijo, 1=Wolfe, 2=Goldstein
phi0Merit function for alpha=0
dphi0Directional derivative of merit function for alpha=0
etaConstant for Armijo's rule
zetaConstant for Wolfe's or Goldstein's rule
muValue of mu for merit function
vecxCurrent vector x
vecdxUpdate vector dx
veceError vector e
vecwWork vector w

Definition at line 1047 of file NewFitterGSL.cc.

◆ getGlobalCovarianceMatrix() [1/2]

double * getGlobalCovarianceMatrix ( int &  idim)
virtualinherited
Parameters
idim1st dimension of global covariance matrix

Definition at line 158 of file BaseFitter.cc.

◆ getGlobalCovarianceMatrix() [2/2]

const double * getGlobalCovarianceMatrix ( int &  idim) const
virtualinherited
Parameters
idim1st dimension of global covariance matrix

Definition at line 148 of file BaseFitter.cc.

◆ meritFunction()

double meritFunction ( double  mu,
const gsl_vector *  vecx,
const gsl_vector *  vece 
)
Parameters
muValue of mu
vecxCurrent vector x
veceCurrent errors x

Definition at line 1249 of file NewFitterGSL.cc.

◆ meritFunctionDeriv()

double meritFunctionDeriv ( double  mu,
const gsl_vector *  vecx,
const gsl_vector *  vece,
const gsl_vector *  vecdx,
gsl_vector *  vecw 
)
Parameters
muValue of mu
vecxCurrent vector x
veceCurrent errors x
vecdxCurrent update vector dx
vecwwork vector

Definition at line 1284 of file NewFitterGSL.cc.

◆ MoorePenroseInverse()

void MoorePenroseInverse ( gsl_matrix *  Ainv,
gsl_matrix *  A,
gsl_matrix *  W,
gsl_vector *  w,
double  eps = 0 
)
static

Compute the Moore-Penrose pseudo-inverse A+ of A, using SVD.

Parameters
AinvResult: m x n matrix A+
AInput: n x m matrix A, n >= m (is destroyed!)
WWork matrix, at least m x m
wWork vector w, at least m
epsSingular values < eps*(max(abs(s_i))) are set to 0

Definition at line 1569 of file NewFitterGSL.cc.


The documentation for this class was generated from the following files: