GeneralBrokenLines  Rev: 2.2.0
GblPoint.cpp
Go to the documentation of this file.
1 /*
2  * GblPoint.cpp
3  *
4  * Created on: Aug 18, 2011
5  * Author: kleinwrt
6  */
7 
30 #include "GblPoint.h"
31 using namespace Eigen;
32 
34 namespace gbl {
35 
37 
41 GblPoint::GblPoint(const Matrix5d &aJacobian) :
42  theLabel(0), theOffset(0), p2pJacobian(aJacobian), measDim(0), measPrecMin(
43  0.), transFlag(false), measTransformation(), scatFlag(false), localDerivatives(), globalLabels(), globalDerivatives() {
44 }
45 
46 #ifdef GBL_EIGEN_SUPPORT_ROOT
48 
52 GblPoint::GblPoint(const TMatrixD &aJacobian) :
53 theLabel(0), theOffset(0), measDim(0), measPrecMin(0.), transFlag(false),
54 measTransformation(), scatFlag(false), localDerivatives(), globalLabels(),
55 globalDerivatives() {
56 
57  for (unsigned int i = 0; i < 5; ++i) {
58  for (unsigned int j = 0; j < 5; ++j) {
59  p2pJacobian(i, j) = aJacobian(i, j);
60  }
61  }
62 }
63 #endif
64 
66 }
67 
68 #ifdef GBL_EIGEN_SUPPORT_ROOT
70 
78 void GblPoint::addMeasurement(const TMatrixD &aProjection,
79  const TVectorD &aResiduals, const TVectorD &aPrecision,
80  double minPrecision) {
81  measDim = aResiduals.GetNrows();
82  measPrecMin = minPrecision;
83  unsigned int iOff = 5 - measDim;
84  for (unsigned int i = 0; i < measDim; ++i) {
85  measResiduals(iOff + i) = aResiduals[i];
86  measPrecision(iOff + i) = aPrecision[i];
87  for (unsigned int j = 0; j < measDim; ++j) {
88  measProjection(iOff + i, iOff + j) = aProjection(i, j);
89  }
90  }
91 }
92 
94 
103 void GblPoint::addMeasurement(const TMatrixD &aProjection,
104  const TVectorD &aResiduals, const TMatrixDSym &aPrecision,
105  double minPrecision) {
106  measDim = aResiduals.GetNrows();
107  measPrecMin = minPrecision;
108  TMatrixDSymEigen measEigen(aPrecision);
109  TMatrixD tmpTransformation(measDim, measDim);
110  tmpTransformation = measEigen.GetEigenVectors();
111  tmpTransformation.T();
112  transFlag = true;
113  TVectorD transResiduals = tmpTransformation * aResiduals;
114  TVectorD transPrecision = measEigen.GetEigenValues();
115  TMatrixD transProjection = tmpTransformation * aProjection;
117  unsigned int iOff = 5 - measDim;
118  for (unsigned int i = 0; i < measDim; ++i) {
119  measResiduals(iOff + i) = transResiduals[i];
120  measPrecision(iOff + i) = transPrecision[i];
121  for (unsigned int j = 0; j < measDim; ++j) {
122  measTransformation(i, j) = tmpTransformation(i, j);
123  measProjection(iOff + i, iOff + j) = transProjection(i, j);
124  }
125  }
126 }
127 
129 
136 void GblPoint::addMeasurement(const TVectorD &aResiduals,
137  const TVectorD &aPrecision, double minPrecision) {
138  measDim = aResiduals.GetNrows();
139  measPrecMin = minPrecision;
140  unsigned int iOff = 5 - measDim;
141  for (unsigned int i = 0; i < measDim; ++i) {
142  measResiduals(iOff + i) = aResiduals[i];
143  measPrecision(iOff + i) = aPrecision[i];
144  }
145  measProjection.setIdentity();
146 }
147 
149 
157 void GblPoint::addMeasurement(const TVectorD &aResiduals,
158  const TMatrixDSym &aPrecision, double minPrecision) {
159  measDim = aResiduals.GetNrows();
160  measPrecMin = minPrecision;
161  TMatrixDSymEigen measEigen(aPrecision);
162  TMatrixD tmpTransformation(measDim, measDim);
163  tmpTransformation = measEigen.GetEigenVectors();
164  tmpTransformation.T();
165  transFlag = true;
166  TVectorD transResiduals = tmpTransformation * aResiduals;
167  TVectorD transPrecision = measEigen.GetEigenValues();
169  unsigned int iOff = 5 - measDim;
170  for (unsigned int i = 0; i < measDim; ++i) {
171  measResiduals(iOff + i) = transResiduals[i];
172  measPrecision(iOff + i) = transPrecision[i];
173  for (unsigned int j = 0; j < measDim; ++j) {
174  measTransformation(i, j) = tmpTransformation(i, j);
175  measProjection(iOff + i, iOff + j) = measTransformation(i, j);
176  }
177  }
178 }
179 #endif
180 
182 
186 unsigned int GblPoint::hasMeasurement() const {
187  return measDim;
188 }
189 
191 
194 double GblPoint::getMeasPrecMin() const {
195  return measPrecMin;
196 }
197 
199 
204 void GblPoint::getMeasurement(Matrix5d &aProjection, Vector5d &aResiduals,
205  Vector5d &aPrecision) const {
206  aProjection.bottomRightCorner(measDim, measDim) =
207  measProjection.bottomRightCorner(measDim, measDim);
208  aResiduals.tail(measDim) = measResiduals.tail(measDim);
209  aPrecision.tail(measDim) = measPrecision.tail(measDim);
210 }
211 
213 
216 void GblPoint::getMeasTransformation(MatrixXd &aTransformation) const {
217  aTransformation.resize(measDim, measDim);
218  if (transFlag) {
219  aTransformation = measTransformation;
220  } else {
221  aTransformation.setIdentity();
222  }
223 }
224 
225 #ifdef GBL_EIGEN_SUPPORT_ROOT
227 
234 void GblPoint::addScatterer(const TVectorD &aResiduals,
235  const TVectorD &aPrecision) {
236  scatFlag = true;
237  scatResiduals(0) = aResiduals[0];
238  scatResiduals(1) = aResiduals[1];
239  scatPrecision(0) = aPrecision[0];
240  scatPrecision(1) = aPrecision[1];
241  scatTransformation.setIdentity();
242 }
243 
245 
260 void GblPoint::addScatterer(const TVectorD &aResiduals,
261  const TMatrixDSym &aPrecision) {
262  scatFlag = true;
263  TMatrixDSymEigen scatEigen(aPrecision);
264  TMatrixD aTransformation = scatEigen.GetEigenVectors();
265  aTransformation.T();
266  TVectorD transResiduals = aTransformation * aResiduals;
267  TVectorD transPrecision = scatEigen.GetEigenValues();
268  scatTransformation.resize(2, 2);
269  for (unsigned int i = 0; i < 2; ++i) {
270  scatResiduals(i) = transResiduals[i];
271  scatPrecision(i) = transPrecision[i];
272  for (unsigned int j = 0; j < 2; ++j) {
273  scatTransformation(i, j) = aTransformation(i, j);
274  }
275  }
276 }
277 #endif
278 
281  return scatFlag;
282 }
283 
285 
290 void GblPoint::getScatterer(Matrix2d &aTransformation, Vector2d &aResiduals,
291  Vector2d &aPrecision) const {
292  aTransformation = scatTransformation;
293  aResiduals = scatResiduals;
294  aPrecision = scatPrecision;
295 }
296 
298 
301 void GblPoint::getScatTransformation(Matrix2d &aTransformation) const {
302  if (scatFlag) {
303  aTransformation = scatTransformation;
304  } else {
305  aTransformation.setIdentity();
306  }
307 }
308 
309 #ifdef GBL_EIGEN_SUPPORT_ROOT
311 
315 void GblPoint::addLocals(const TMatrixD &aDerivatives) {
316  if (measDim) {
317  unsigned int numDer = aDerivatives.GetNcols();
318  localDerivatives.resize(measDim, numDer);
319  // convert from ROOT
320  MatrixXd tmpDerivatives(measDim, numDer);
321  for (unsigned int i = 0; i < measDim; ++i) {
322  for (unsigned int j = 0; j < numDer; ++j)
323  tmpDerivatives(i, j) = aDerivatives(i, j);
324  }
325  if (transFlag) {
326  localDerivatives = measTransformation * tmpDerivatives;
327  } else {
328  localDerivatives = tmpDerivatives;
329  }
330  }
331 }
332 #endif
333 
335 unsigned int GblPoint::getNumLocals() const {
336  return localDerivatives.cols();
337 }
338 
340 const MatrixXd& GblPoint::getLocalDerivatives() const {
341  return localDerivatives;
342 }
343 
344 #ifdef GBL_EIGEN_SUPPORT_ROOT
346 
351 void GblPoint::addGlobals(const std::vector<int> &aLabels,
352  const TMatrixD &aDerivatives) {
353  if (measDim) {
354  globalLabels = aLabels;
355  unsigned int numDer = aDerivatives.GetNcols();
356  globalDerivatives.resize(measDim, numDer);
357  // convert from ROOT
358  MatrixXd tmpDerivatives(measDim, numDer);
359  for (unsigned int i = 0; i < measDim; ++i) {
360  for (unsigned int j = 0; j < numDer; ++j)
361  tmpDerivatives(i, j) = aDerivatives(i, j);
362  }
363  if (transFlag) {
364  globalDerivatives = measTransformation * tmpDerivatives;
365  } else {
366  globalDerivatives = tmpDerivatives;
367  }
368 
369  }
370 }
371 #endif
372 
374 unsigned int GblPoint::getNumGlobals() const {
375  return globalDerivatives.cols();
376 }
377 
379 
382 void GblPoint::getGlobalLabels(std::vector<int> &aLabels) const {
383  aLabels = globalLabels;
384 }
385 
387 
390 void GblPoint::getGlobalDerivatives(MatrixXd &aDerivatives) const {
391  aDerivatives = globalDerivatives;
392 }
393 
395 
401  std::vector<int> &aLabels, std::vector<double> &aDerivatives) const {
402  aLabels.resize(globalDerivatives.cols());
403  aDerivatives.resize(globalDerivatives.cols());
404  for (unsigned int i = 0; i < globalDerivatives.cols(); ++i) {
405  aLabels[i] = globalLabels[i];
406  aDerivatives[i] = globalDerivatives(aRow, i);
407  }
408 }
409 
411 
414 void GblPoint::setLabel(unsigned int aLabel) {
415  theLabel = aLabel;
416 }
417 
419 unsigned int GblPoint::getLabel() const {
420  return theLabel;
421 }
422 
424 
427 void GblPoint::setOffset(int anOffset) {
428  theOffset = anOffset;
429 }
430 
432 int GblPoint::getOffset() const {
433  return theOffset;
434 }
435 
438  return p2pJacobian;
439 }
440 
442 
446  // to optimize: need only two last rows of inverse
447  // prevJacobian = aJac.inverse();
448  // block matrix algebra
449  Matrix23d CA = aJac.block<2, 3>(3, 0) * aJac.block<3, 3>(0, 0).inverse(); // C*A^-1
450  Matrix2d DCAB = aJac.block<2, 2>(3, 3) - CA * aJac.block<3, 2>(0, 3); // D - C*A^-1 *B
451  Matrix2d DCABInv = DCAB.inverse();
452  prevJacobian.block<2, 2>(3, 3) = DCABInv;
453  prevJacobian.block<2, 3>(3, 0) = -DCABInv * CA;
454 }
455 
457 
461  nextJacobian = aJac;
462 }
463 
465 
474 void GblPoint::getDerivatives(int aDirection, Matrix2d &matW, Matrix2d &matWJ,
475  Vector2d &vecWd) const {
476 
477  Matrix2d matJ;
478  Vector2d vecd;
479  if (aDirection < 1) {
480  matJ = prevJacobian.block<2, 2>(3, 3);
481  matW = -prevJacobian.block<2, 2>(3, 1);
482  vecd = prevJacobian.block<2, 1>(3, 0);
483  } else {
484  matJ = nextJacobian.block<2, 2>(3, 3);
485  matW = nextJacobian.block<2, 2>(3, 1);
486  vecd = nextJacobian.block<2, 1>(3, 0);
487  }
488 
489  if (!matW.determinant()) {
490  std::cout << " GblPoint::getDerivatives failed to invert matrix "
491  << std::endl;
492  std::cout
493  << " Possible reason for singular matrix: multiple GblPoints at same arc-length"
494  << std::endl;
495  throw std::overflow_error("Singular matrix inversion exception");
496  }
497  matW = matW.inverse().eval();
498  matWJ = matW * matJ;
499  vecWd = matW * vecd;
500 }
501 
503 
506 void GblPoint::printPoint(unsigned int level) const {
507  std::cout << " GblPoint";
508  if (theLabel) {
509  std::cout << ", label " << theLabel;
510  if (theOffset >= 0) {
511  std::cout << ", offset " << theOffset;
512  }
513  }
514  if (measDim) {
515  std::cout << ", " << measDim << " measurements";
516  }
517  if (scatFlag) {
518  std::cout << ", scatterer";
519  }
520  if (transFlag) {
521  std::cout << ", diagonalized";
522  }
523  if (localDerivatives.cols()) {
524  std::cout << ", " << localDerivatives.cols() << " local derivatives";
525  }
526  if (globalDerivatives.cols()) {
527  std::cout << ", " << globalDerivatives.cols() << " global derivatives";
528  }
529  std::cout << std::endl;
530  if (level > 0) {
531  IOFormat CleanFmt(4, 0, ", ", "\n", "[", "]");
532  if (measDim) {
533  std::cout << " Measurement" << std::endl;
534  std::cout << " Projection: " << std::endl
535  << measProjection.format(CleanFmt) << std::endl;
536  std::cout << " Residuals: "
537  << measResiduals.transpose().format(CleanFmt) << std::endl;
538  std::cout << " Precision (min.: " << measPrecMin << "): "
539  << measPrecision.transpose().format(CleanFmt) << std::endl;
540  }
541  if (scatFlag) {
542  std::cout << " Scatterer" << std::endl;
543  std::cout << " Residuals: "
544  << scatResiduals.transpose().format(CleanFmt) << std::endl;
545  std::cout << " Precision: "
546  << scatPrecision.transpose().format(CleanFmt) << std::endl;
547  }
548  if (localDerivatives.cols()) {
549  std::cout << " Local Derivatives:" << std::endl
550  << localDerivatives.format(CleanFmt) << std::endl;
551  }
552  if (globalDerivatives.cols()) {
553  std::cout << " Global Labels:";
554  for (unsigned int i = 0; i < globalLabels.size(); ++i) {
555  std::cout << " " << globalLabels[i];
556  }
557  std::cout << std::endl;
558  std::cout << " Global Derivatives:"
559  << globalDerivatives.format(CleanFmt) << std::endl;
560  }
561  std::cout << " Jacobian " << std::endl;
562  std::cout << " Point-to-point " << std::endl
563  << p2pJacobian.format(CleanFmt) << std::endl;
564  if (theLabel) {
565  std::cout << " To previous offset " << std::endl
566  << prevJacobian.format(CleanFmt) << std::endl;
567  std::cout << " To next offset " << std::endl
568  << nextJacobian.format(CleanFmt) << std::endl;
569  }
570  }
571 }
572 
573 }
Matrix5d nextJacobian
Jacobian to next scatterer (or last measurement)
Definition: GblPoint.h:269
void getScatterer(Eigen::Matrix2d &aTransformation, Eigen::Vector2d &aResiduals, Eigen::Vector2d &aPrecision) const
Retrieve scatterer of a point.
Definition: GblPoint.cpp:290
std::vector< int > globalLabels
Labels of global (MP-II) derivatives.
Definition: GblPoint.h:284
void getDerivatives(int aDirection, Eigen::Matrix2d &matW, Eigen::Matrix2d &matWJ, Eigen::Vector2d &vecWd) const
Retrieve derivatives of local track model.
Definition: GblPoint.cpp:474
void addMeasurement(const Eigen::MatrixBase< Projection > &aProjection, const Eigen::MatrixBase< Residuals > &aResiduals, const Eigen::MatrixBase< Precision > &aPrecision, double minPrecision=0.)
Add a measurement to a point.
Definition: GblPoint.h:290
const Eigen::MatrixXd & getLocalDerivatives() const
Retrieve local derivatives from a point.
Definition: GblPoint.cpp:340
Eigen::MatrixXd globalDerivatives
Derivatives of measurement vs additional global (MP-II) parameters.
Definition: GblPoint.h:285
void getScatTransformation(Eigen::Matrix2d &aTransformation) const
Get scatterer transformation (from diagonalization).
Definition: GblPoint.cpp:301
Eigen::Matrix2d scatTransformation
Transformation of diagonalization (of scat. precision matrix)
Definition: GblPoint.h:280
void printPoint(unsigned int level=0) const
Print GblPoint.
Definition: GblPoint.cpp:506
bool scatFlag
Scatterer present?
Definition: GblPoint.h:279
double getMeasPrecMin() const
get precision cutoff.
Definition: GblPoint.cpp:194
void addPrevJacobian(const Matrix5d &aJac)
Define jacobian to previous scatterer (by GBLTrajectory constructor)
Definition: GblPoint.cpp:445
GblPoint(const Matrix5d &aJacobian)
Create a point.
Definition: GblPoint.cpp:41
double measPrecMin
Minimal measurement precision (for usage)
Definition: GblPoint.h:271
Vector5d measResiduals
Measurement residuals.
Definition: GblPoint.h:274
unsigned int hasMeasurement() const
Check for measurement at a point.
Definition: GblPoint.cpp:186
unsigned int measDim
Dimension of measurement (1-5), 0 indicates absence of measurement.
Definition: GblPoint.h:270
bool transFlag
Transformation exists?
Definition: GblPoint.h:276
void getMeasurement(Matrix5d &aProjection, Vector5d &aResiduals, Vector5d &aPrecision) const
Retrieve measurement of a point.
Definition: GblPoint.cpp:204
bool hasScatterer() const
Check for scatterer at a point.
Definition: GblPoint.cpp:280
void addScatterer(const Eigen::Vector2d &aResiduals, const Eigen::MatrixBase< Precision > &aPrecision)
Add a (thin) scatterer to a point.
Definition: GblPoint.h:366
void getGlobalLabelsAndDerivatives(unsigned int aRow, std::vector< int > &aLabels, std::vector< double > &aDerivatives) const
Retrieve global derivatives from a point for a single row.
Definition: GblPoint.cpp:400
unsigned int theLabel
Label identifying point.
Definition: GblPoint.h:265
unsigned int getNumLocals() const
Retrieve number of local derivatives from a point.
Definition: GblPoint.cpp:335
int theOffset
Offset number at point if not negative (else interpolation needed)
Definition: GblPoint.h:266
void getGlobalDerivatives(Eigen::MatrixXd &aDerivatives) const
Retrieve global derivatives from a point.
Definition: GblPoint.cpp:390
void setLabel(unsigned int aLabel)
Define label of point (by GBLTrajectory constructor)
Definition: GblPoint.cpp:414
void addLocals(const Eigen::MatrixBase< Derivative > &aDerivatives)
Add local derivatives to a point.
Definition: GblPoint.h:391
void addGlobals(const std::vector< int > &aLabels, const Eigen::MatrixBase< Derivative > &aDerivatives)
Add global derivatives to a point.
Definition: GblPoint.h:403
Matrix5d measProjection
Projection from measurement to local system.
Definition: GblPoint.h:272
void getGlobalLabels(std::vector< int > &aLabels) const
Retrieve global derivatives labels from a point.
Definition: GblPoint.cpp:382
void addNextJacobian(const Matrix5d &aJac)
Define jacobian to next scatterer (by GBLTrajectory constructor)
Definition: GblPoint.cpp:460
unsigned int getLabel() const
Retrieve label of point.
Definition: GblPoint.cpp:419
void getMeasTransformation(Eigen::MatrixXd &aTransformation) const
Get measurement transformation (from diagonalization).
Definition: GblPoint.cpp:216
Eigen::Vector2d scatPrecision
Scattering precision (diagonal of inverse covariance matrix)
Definition: GblPoint.h:282
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor, 5, 5 > measTransformation
Transformation of diagonalization (of meas. precision matrix)
Definition: GblPoint.h:278
Vector5d measPrecision
Measurement precision (diagonal of inverse covariance matrix)
Definition: GblPoint.h:275
virtual ~GblPoint()
Definition: GblPoint.cpp:65
Matrix5d prevJacobian
Jacobian to previous scatterer (or first measurement)
Definition: GblPoint.h:268
Eigen::MatrixXd localDerivatives
Derivatives of measurement vs additional local (fit) parameters.
Definition: GblPoint.h:283
const Matrix5d & getP2pJacobian() const
Retrieve point-to-(previous)point jacobian.
Definition: GblPoint.cpp:437
void setOffset(int anOffset)
Define offset for point (by GBLTrajectory constructor)
Definition: GblPoint.cpp:427
Eigen::Vector2d scatResiduals
Scattering residuals (initial kinks if iterating)
Definition: GblPoint.h:281
unsigned int getNumGlobals() const
Retrieve number of global derivatives from a point.
Definition: GblPoint.cpp:374
Matrix5d p2pJacobian
Point-to-point jacobian from previous point.
Definition: GblPoint.h:267
int getOffset() const
Retrieve offset for point.
Definition: GblPoint.cpp:432
Namespace for the general broken lines package.
Eigen::Matrix< double, 2, 3 > Matrix23d
Definition: GblPoint.h:51
Eigen::Matrix< double, 5, 1 > Vector5d
Definition: GblPoint.h:50
Eigen::Matrix< double, 5, 5 > Matrix5d
Definition: GblData.h:46