Commit 7bfab4e9 authored by Claus Kleinwort's avatar Claus Kleinwort
Browse files

cpp: cleanup and fixes for first Eigen based release

git-svn-id: http://svnsrv.desy.de/public/GeneralBrokenLines/trunk@128 281f6f2b-e318-4fd1-8bce-1a4ba7aab212
parent 5a07f1e8
# - Try to find Eigen3 lib
#
# This module supports requiring a minimum version, e.g. you can do
# find_package(Eigen3 3.1.2)
# to require version 3.1.2 or newer of Eigen3.
#
# Once done this will define
#
# EIGEN3_FOUND - system has eigen lib with correct version
# EIGEN3_INCLUDE_DIR - the eigen include directory
# EIGEN3_VERSION - eigen version
# Copyright (c) 2006, 2007 Montel Laurent, <montel@kde.org>
# Copyright (c) 2008, 2009 Gael Guennebaud, <g.gael@free.fr>
# Copyright (c) 2009 Benoit Jacob <jacob.benoit.1@gmail.com>
# Redistribution and use is allowed according to the terms of the 2-clause BSD license.
if(NOT Eigen3_FIND_VERSION)
if(NOT Eigen3_FIND_VERSION_MAJOR)
set(Eigen3_FIND_VERSION_MAJOR 2)
endif(NOT Eigen3_FIND_VERSION_MAJOR)
if(NOT Eigen3_FIND_VERSION_MINOR)
set(Eigen3_FIND_VERSION_MINOR 91)
endif(NOT Eigen3_FIND_VERSION_MINOR)
if(NOT Eigen3_FIND_VERSION_PATCH)
set(Eigen3_FIND_VERSION_PATCH 0)
endif(NOT Eigen3_FIND_VERSION_PATCH)
set(Eigen3_FIND_VERSION "${Eigen3_FIND_VERSION_MAJOR}.${Eigen3_FIND_VERSION_MINOR}.${Eigen3_FIND_VERSION_PATCH}")
endif(NOT Eigen3_FIND_VERSION)
macro(_eigen3_check_version)
file(READ "${EIGEN3_INCLUDE_DIR}/Eigen/src/Core/util/Macros.h" _eigen3_version_header)
string(REGEX MATCH "define[ \t]+EIGEN_WORLD_VERSION[ \t]+([0-9]+)" _eigen3_world_version_match "${_eigen3_version_header}")
set(EIGEN3_WORLD_VERSION "${CMAKE_MATCH_1}")
string(REGEX MATCH "define[ \t]+EIGEN_MAJOR_VERSION[ \t]+([0-9]+)" _eigen3_major_version_match "${_eigen3_version_header}")
set(EIGEN3_MAJOR_VERSION "${CMAKE_MATCH_1}")
string(REGEX MATCH "define[ \t]+EIGEN_MINOR_VERSION[ \t]+([0-9]+)" _eigen3_minor_version_match "${_eigen3_version_header}")
set(EIGEN3_MINOR_VERSION "${CMAKE_MATCH_1}")
set(EIGEN3_VERSION ${EIGEN3_WORLD_VERSION}.${EIGEN3_MAJOR_VERSION}.${EIGEN3_MINOR_VERSION})
if(${EIGEN3_VERSION} VERSION_LESS ${Eigen3_FIND_VERSION})
set(EIGEN3_VERSION_OK FALSE)
else(${EIGEN3_VERSION} VERSION_LESS ${Eigen3_FIND_VERSION})
set(EIGEN3_VERSION_OK TRUE)
endif(${EIGEN3_VERSION} VERSION_LESS ${Eigen3_FIND_VERSION})
if(NOT EIGEN3_VERSION_OK)
message(STATUS "Eigen3 version ${EIGEN3_VERSION} found in ${EIGEN3_INCLUDE_DIR}, "
"but at least version ${Eigen3_FIND_VERSION} is required")
endif(NOT EIGEN3_VERSION_OK)
endmacro(_eigen3_check_version)
if (EIGEN3_INCLUDE_DIR)
# in cache already
_eigen3_check_version()
set(EIGEN3_FOUND ${EIGEN3_VERSION_OK})
else (EIGEN3_INCLUDE_DIR)
find_path(EIGEN3_INCLUDE_DIR NAMES signature_of_eigen3_matrix_library
PATHS
${CMAKE_INSTALL_PREFIX}/include
${KDE4_INCLUDE_DIR}
${EIGEN3_DIR}/include
PATH_SUFFIXES eigen3 eigen
)
if(EIGEN3_INCLUDE_DIR)
_eigen3_check_version()
endif(EIGEN3_INCLUDE_DIR)
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Eigen3 DEFAULT_MSG EIGEN3_INCLUDE_DIR EIGEN3_VERSION_OK)
mark_as_advanced(EIGEN3_INCLUDE_DIR)
endif(EIGEN3_INCLUDE_DIR)
......@@ -86,8 +86,10 @@ public:
const std::vector<double>* aVector);
void addBlockMatrix(double aWeight, unsigned int nSimple,
unsigned int* anIndex, double* aVector);
Eigen::MatrixXd getBlockMatrix(const std::vector<unsigned int> anIndex) const;
Eigen::MatrixXd getBlockMatrix(unsigned int aSize, unsigned int* anIndex) const;
Eigen::MatrixXd getBlockMatrix(
const std::vector<unsigned int> anIndex) const;
Eigen::MatrixXd getBlockMatrix(unsigned int aSize,
unsigned int* anIndex) const;
void printMatrix() const;
private:
......
......@@ -36,12 +36,13 @@
#include "VMatrix.h"
#include "Eigen/Core"
typedef Eigen::Matrix<double, 5, 5> Matrix5d;
typedef Eigen::Matrix<double, 2, 7> Matrix27d;
//! Namespace for the general broken lines package
namespace gbl {
typedef Eigen::Matrix<double, 5, 5> Matrix5d;
typedef Eigen::Matrix<double, 2, 7> Matrix27d;
enum dataBlockType {
None, InternalMeasurement, InternalKink, ExternalSeed, ExternalMeasurement
};
......@@ -58,8 +59,8 @@ public:
virtual ~GblData();
void addDerivatives(unsigned int iRow,
const std::vector<unsigned int> &labDer, const Matrix5d &matDer,
unsigned int iOff, const Eigen::MatrixXd &derLocal, unsigned int nLocal,
const Eigen::MatrixXd &derTrans);
unsigned int iOff, const Eigen::MatrixXd &derLocal,
unsigned int nLocal, const Eigen::MatrixXd &derTrans);
void addDerivatives(unsigned int iRow,
const std::vector<unsigned int> &labDer, const Matrix27d &matDer,
unsigned int nLocal, const Eigen::MatrixXd &derTrans);
......
......@@ -87,11 +87,13 @@ public:
const TMatrixD &aDerivatives);
#endif
// input via Eigen
void addMeasurement(const Eigen::MatrixXd &aProjection, const Eigen::VectorXd &aResiduals,
void addMeasurement(const Eigen::MatrixXd &aProjection,
const Eigen::VectorXd &aResiduals,
const Eigen::MatrixXd &aPrecision, double minPrecision = 0.);
void addMeasurement(const Eigen::VectorXd &aResiduals, const Eigen::MatrixXd &aPrecision,
double minPrecision = 0.);
void addScatterer(const Eigen::Vector2d &aResiduals, const Eigen::MatrixXd &aPrecision);
void addMeasurement(const Eigen::VectorXd &aResiduals,
const Eigen::MatrixXd &aPrecision, double minPrecision = 0.);
void addScatterer(const Eigen::Vector2d &aResiduals,
const Eigen::MatrixXd &aPrecision);
void addLocals(const Eigen::MatrixXd &aDerivatives);
void addGlobals(const std::vector<int> &aLabels,
const Eigen::MatrixXd &aDerivatives);
......@@ -102,8 +104,8 @@ public:
Vector5d &aPrecision) const;
void getMeasTransformation(Eigen::MatrixXd &aTransformation) const;
bool hasScatterer() const;
void getScatterer(Eigen::Matrix2d &aTransformation, Eigen::Vector2d &aResiduals,
Eigen::Vector2d &aPrecision) const;
void getScatterer(Eigen::Matrix2d &aTransformation,
Eigen::Vector2d &aResiduals, Eigen::Vector2d &aPrecision) const;
void getScatTransformation(Eigen::Matrix2d &aTransformation) const;
unsigned int getNumLocals() const;
const Eigen::MatrixXd& getLocalDerivatives() const;
......@@ -115,8 +117,8 @@ public:
unsigned int getLabel() const;
int getOffset() const;
const Matrix5d& getP2pJacobian() const;
void getDerivatives(int aDirection, Eigen::Matrix2d &matW, Eigen::Matrix2d &matWJ,
Eigen::Vector2d &vecWd) const;
void getDerivatives(int aDirection, Eigen::Matrix2d &matW,
Eigen::Matrix2d &matWJ, Eigen::Vector2d &vecWd) const;
void printPoint(unsigned int level = 0) const;
private:
......
......@@ -97,7 +97,8 @@ public:
TVectorD &aDownWeights);
#endif
unsigned int getLabels(std::vector<unsigned int> &aLabelList) const;
unsigned int getLabels(std::vector<std::vector<unsigned int> > &aLabelList) const;
unsigned int getLabels(
std::vector<std::vector<unsigned int> > &aLabelList) const;
unsigned int fit(double &Chi2, int &Ndf, double &lostWeight,
std::string optionList = "", unsigned int aLabel = 0);
void milleOut(MilleBinary &aMille);
......
......@@ -34,20 +34,20 @@ using namespace Eigen;
namespace gbl {
/// Create bordered band matrix.
BorderedBandMatrix::BorderedBandMatrix() :
BorderedBandMatrix::BorderedBandMatrix() :
numSize(0), numBorder(0), numBand(0), numCol(0) {
}
}
BorderedBandMatrix::~BorderedBandMatrix() {
}
BorderedBandMatrix::~BorderedBandMatrix() {
}
/// Resize bordered band matrix.
/**
/**
* \param nSize [in] Size of matrix
* \param nBorder [in] Size of border (=1 for q/p + additional local parameters)
* \param nBand [in] Band width (usually = 5, for simplified jacobians = 4)
*/
void BorderedBandMatrix::resize(unsigned int nSize, unsigned int nBorder,
void BorderedBandMatrix::resize(unsigned int nSize, unsigned int nBorder,
unsigned int nBand) {
numSize = nSize;
numBorder = nBorder;
......@@ -56,10 +56,10 @@ namespace gbl {
theBorder.resize(numBorder);
theMixed.resize(numBorder, numCol);
theBand.resize((nBand + 1), numCol);
}
}
/// Add symmetric block matrix.
/**
/**
* Add (extended) block matrix defined by 'aVector * aWeight * aVector.T'
* to bordered band matrix:
* BBmatrix(anIndex(i),anIndex(j)) += aVector(i) * aWeight * aVector(j).
......@@ -67,7 +67,7 @@ namespace gbl {
* \param anIndex [in] List of rows/colums to be used
* \param aVector [in] Vector
*/
void BorderedBandMatrix::addBlockMatrix(double aWeight,
void BorderedBandMatrix::addBlockMatrix(double aWeight,
const std::vector<unsigned int>* anIndex,
const std::vector<double>* aVector) {
int nBorder = numBorder;
......@@ -89,10 +89,10 @@ namespace gbl {
}
}
}
}
}
/// Add symmetric block matrix.
/**
/**
* Add (extended) block matrix defined by 'aVector * aWeight * aVector.T'
* to bordered band matrix:
* BBmatrix(anIndex(i),anIndex(j)) += aVector(i) * aWeight * aVector(j).
......@@ -101,7 +101,7 @@ namespace gbl {
* \param anIndex [in] List of rows/colums to be used
* \param aVector [in] Vector
*/
void BorderedBandMatrix::addBlockMatrix(double aWeight, unsigned int aSize,
void BorderedBandMatrix::addBlockMatrix(double aWeight, unsigned int aSize,
unsigned int* anIndex, double* aVector) {
int nBorder = numBorder;
for (unsigned int i = 0; i < aSize; ++i) {
......@@ -121,14 +121,14 @@ namespace gbl {
}
}
}
}
}
/// Retrieve symmetric block matrix.
/**
/**
* Get (compressed) block from bordered band matrix: aMatrix(i,j) = BBmatrix(anIndex(i),anIndex(j)).
* \param anIndex [in] List of rows/colums to be used
*/
MatrixXd BorderedBandMatrix::getBlockMatrix(
MatrixXd BorderedBandMatrix::getBlockMatrix(
const std::vector<unsigned int> anIndex) const {
MatrixXd aMatrix(anIndex.size(), anIndex.size());
......@@ -149,15 +149,15 @@ namespace gbl {
}
}
return aMatrix;
}
}
/// Retrieve symmetric block matrix.
/**
/**
* Get (compressed) block from bordered band matrix: aMatrix(i,j) = BBmatrix(anIndex(i),anIndex(j)).
* \param aSize [in] Matrix size
* \param anIndex [in] Array of rows/colums to be used
*/
MatrixXd BorderedBandMatrix::getBlockMatrix(unsigned int aSize,
MatrixXd BorderedBandMatrix::getBlockMatrix(unsigned int aSize,
unsigned int* anIndex) const {
MatrixXd aMatrix(aSize, aSize);
......@@ -178,10 +178,10 @@ namespace gbl {
}
}
return aMatrix;
}
}
/// Solve linear equation system, partially calculate inverse.
/**
/**
* Solve linear equation A*x=b system with bordered band matrix A,
* calculate bordered band part of inverse of A. Use decomposition
* in border and band part for block matrix algebra:
......@@ -207,7 +207,7 @@ namespace gbl {
* \param [in] aRightHandSide Right hand side (vector) 'b' of A*x=b
* \param [out] aSolution Solution (vector) x of A*x=b
*/
void BorderedBandMatrix::solveAndInvertBorderedBand(
void BorderedBandMatrix::solveAndInvertBorderedBand(
const VVector &aRightHandSide, VVector &aSolution) {
// decompose band
......@@ -216,50 +216,50 @@ namespace gbl {
VMatrix inverseBand = invertBand();
if (numBorder > 0) { // need to use block matrix decomposition to solve
// solve for mixed part
const VMatrix auxMat = solveBand(theMixed);// = Xt
const VMatrix auxMatT = auxMat.transpose();// = X
const VMatrix auxMat = solveBand(theMixed); // = Xt
const VMatrix auxMatT = auxMat.transpose(); // = X
// solve for border part
const VVector auxVec = aRightHandSide.getVec(numBorder)
- auxMat * aRightHandSide.getVec(numCol, numBorder);// = b1 - Xt*b2
VSymMatrix inverseBorder = theBorder - theMixed * auxMatT;
inverseBorder.invert();// = E
const VVector borderSolution = inverseBorder * auxVec;// = x1
inverseBorder.invert(); // = E
const VVector borderSolution = inverseBorder * auxVec; // = x1
// solve for band part
const VVector bandSolution = solveBand(
aRightHandSide.getVec(numCol, numBorder));// = x
aRightHandSide.getVec(numCol, numBorder)); // = x
aSolution.putVec(borderSolution);
aSolution.putVec(bandSolution - auxMatT * borderSolution, numBorder);// = x2
// parts of inverse
theBorder = inverseBorder;// E
theMixed = inverseBorder * auxMat;// E*Xt (-mixed part of inverse) !!!
theBand = inverseBand + bandOfAVAT(auxMatT, inverseBorder);// band(D^-1 + X*E*Xt)
theBorder = inverseBorder; // E
theMixed = inverseBorder * auxMat; // E*Xt (-mixed part of inverse) !!!
theBand = inverseBand + bandOfAVAT(auxMatT, inverseBorder); // band(D^-1 + X*E*Xt)
} else {
aSolution.putVec(solveBand(aRightHandSide));
theBand = inverseBand;
}
}
}
/// Print bordered band matrix.
void BorderedBandMatrix::printMatrix() const {
void BorderedBandMatrix::printMatrix() const {
std::cout << "Border part " << std::endl;
theBorder.print();
std::cout << "Mixed part " << std::endl;
theMixed.print();
std::cout << "Band part " << std::endl;
theBand.print();
}
}
/*============================================================================
/*============================================================================
from Dbandmatrix.F (MillePede-II by V. Blobel, Univ. Hamburg)
============================================================================*/
/// (root free) Cholesky decomposition of band part: C=LDL^T
/**
/**
* Decompose band matrix into diagonal matrix D and lower triangular band matrix
* L (diagonal=1). Overwrite band matrix with D and off-diagonal part of L.
* \exception 2 : matrix is singular.
* \exception 3 : matrix is not positive definite.
*/
void BorderedBandMatrix::decomposeBand() {
void BorderedBandMatrix::decomposeBand() {
int nRow = numBand + 1;
int nCol = numCol;
......@@ -285,16 +285,16 @@ namespace gbl {
theBand(j, i) = rxw;
}
}
}
}
/// Solve for band part.
/**
/**
* Solve C*x=b for band part using decomposition C=LDL^T
* and forward (L*z=b) and backward substitution (L^T*x=D^-1*z).
* \param [in] aRightHandSide Right hand side (vector) 'b' of C*x=b
* \return Solution (vector) 'x' of C*x=b
*/
VVector BorderedBandMatrix::solveBand(const VVector &aRightHandSide) const {
VVector BorderedBandMatrix::solveBand(const VVector &aRightHandSide) const {
int nRow = theBand.getNumRows();
int nCol = theBand.getNumCols();
......@@ -314,16 +314,16 @@ namespace gbl {
aSolution(i) = rxw;
}
return aSolution;
}
}
/// solve band part for mixed part (border rows).
/**
/**
* Solve C*X=B for mixed part using decomposition C=LDL^T
* and forward and backward substitution.
* \param [in] aRightHandSide Right hand side (matrix) 'B' of C*X=B
* \return Solution (matrix) 'X' of C*X=B
*/
VMatrix BorderedBandMatrix::solveBand(const VMatrix &aRightHandSide) const {
VMatrix BorderedBandMatrix::solveBand(const VMatrix &aRightHandSide) const {
int nRow = theBand.getNumRows();
int nCol = theBand.getNumCols();
......@@ -346,13 +346,13 @@ namespace gbl {
}
}
return aSolution;
}
}
/// Invert band part.
/**
/**
* \return Inverted band
*/
VMatrix BorderedBandMatrix::invertBand() {
VMatrix BorderedBandMatrix::invertBand() {
int nRow = numBand + 1;
int nCol = numCol;
......@@ -370,13 +370,13 @@ namespace gbl {
}
}
return inverseBand;
}
}
/// Calculate band part of: 'anArray * aSymArray * anArray.T'.
/**
/**
* \return Band part of product
*/
VMatrix BorderedBandMatrix::bandOfAVAT(const VMatrix &anArray,
VMatrix BorderedBandMatrix::bandOfAVAT(const VMatrix &anArray,
const VSymMatrix &aSymArray) const {
int nBand = numBand;
int nCol = numCol;
......@@ -397,6 +397,6 @@ namespace gbl {
}
}
return aBand;
}
}
}
......@@ -236,7 +236,8 @@ GblTrajectory::GblTrajectory(
*/
GblTrajectory::GblTrajectory(
const std::vector<std::pair<std::vector<GblPoint>, Eigen::MatrixXd> > &aPointsAndTransList,
const Eigen::MatrixXd &extDerivatives, const Eigen::VectorXd &extMeasurements,
const Eigen::MatrixXd &extDerivatives,
const Eigen::VectorXd &extMeasurements,
const Eigen::MatrixXd &extPrecisions) :
numAllPoints(), numPoints(), numOffsets(0), numInnerTrans(
aPointsAndTransList.size()), numParameters(0), numLocals(0), numMeasurements(
......@@ -805,8 +806,8 @@ void GblTrajectory::getFitToKinkJacobian(std::vector<unsigned int> &anIndex,
* \param [out] localCov Covariance for local parameters
* \return error code (non-zero if trajectory not fitted successfully)
*/
unsigned int GblTrajectory::getResults(int aSignedLabel, Eigen::VectorXd &localPar,
Eigen::MatrixXd &localCov) const {
unsigned int GblTrajectory::getResults(int aSignedLabel,
Eigen::VectorXd &localPar, Eigen::MatrixXd &localCov) const {
if (not fitOK)
return 1;
std::pair<std::vector<unsigned int>, MatrixXd> indexAndJacobian =
......@@ -837,8 +838,9 @@ unsigned int GblTrajectory::getResults(int aSignedLabel, Eigen::VectorXd &localP
* \return error code (non-zero if trajectory not fitted successfully)
*/
unsigned int GblTrajectory::getMeasResults(unsigned int aLabel,
unsigned int &numData, Eigen::VectorXd &aResiduals, Eigen::VectorXd &aMeasErrors,
Eigen::VectorXd &aResErrors, Eigen::VectorXd &aDownWeights) {
unsigned int &numData, Eigen::VectorXd &aResiduals,
Eigen::VectorXd &aMeasErrors, Eigen::VectorXd &aResErrors,
Eigen::VectorXd &aDownWeights) {
numData = 0;
if (not fitOK)
return 1;
......@@ -866,8 +868,9 @@ unsigned int GblTrajectory::getMeasResults(unsigned int aLabel,
* \return error code (non-zero if trajectory not fitted successfully)
*/
unsigned int GblTrajectory::getScatResults(unsigned int aLabel,
unsigned int &numData, Eigen::VectorXd &aResiduals, Eigen::VectorXd &aMeasErrors,
Eigen::VectorXd &aResErrors, Eigen::VectorXd &aDownWeights) {
unsigned int &numData, Eigen::VectorXd &aResiduals,
Eigen::VectorXd &aMeasErrors, Eigen::VectorXd &aResErrors,
Eigen::VectorXd &aDownWeights) {
numData = 0;
if (not fitOK)
return 1;
......@@ -988,7 +991,8 @@ unsigned int GblTrajectory::getScatResults(unsigned int aLabel,
* \param [out] aLabelList List of labels (aLabelList[i] = i+1)
* \return error code (non-zero if trajectory not valid (constructed successfully))
*/
unsigned int GblTrajectory::getLabels(std::vector<unsigned int> &aLabelList) const {
unsigned int GblTrajectory::getLabels(
std::vector<unsigned int> &aLabelList) const {
if (not constructOK)
return 1;
......@@ -1492,8 +1496,9 @@ void GblTrajectory::printTrajectory(unsigned int level) const {
void GblTrajectory::printPoints(unsigned int level) const {
std::cout << "GblPoints " << std::endl;
for (unsigned int iTraj = 0; iTraj < numTrajectories; ++iTraj) {
for (std::vector<GblPoint>::const_iterator itPoint = thePoints[iTraj].begin();
itPoint < thePoints[iTraj].end(); ++itPoint) {
for (std::vector<GblPoint>::const_iterator itPoint =
thePoints[iTraj].begin(); itPoint < thePoints[iTraj].end();
++itPoint) {
itPoint->printPoint(level);
}
}
......@@ -1502,7 +1507,8 @@ void GblTrajectory::printPoints(unsigned int level) const {
/// Print GblData blocks for trajectory
void GblTrajectory::printData() const {
std::cout << "GblData blocks " << std::endl;
for (std::vector<GblData>::const_iterator itData = theData.begin(); itData < theData.end(); ++itData) {
for (std::vector<GblData>::const_iterator itData = theData.begin();
itData < theData.end(); ++itData) {
itData->printData();
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment