CMS 3D CMS Logo

GblPoint.cc
Go to the documentation of this file.
1 /*
2  * GblPoint.cpp
3  *
4  * Created on: Aug 18, 2011
5  * Author: kleinwrt
6  */
7 
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),
43  measPrecMin(0.), transFlag(false), measTransformation(), scatFlag(false),
44  localDerivatives(), globalLabels(), globalDerivatives()
45  {
46  }
47 
48 #ifdef GBL_EIGEN_SUPPORT_ROOT
49 
54  GblPoint::GblPoint(const TMatrixD &aJacobian) :
55  theLabel(0), theOffset(0), measDim(0), measPrecMin(0.), transFlag(false),
58  {
59 
60  for (unsigned int i = 0; i < 5; ++i) {
61  for (unsigned int j = 0; j < 5; ++j) {
62  p2pJacobian(i, j) = aJacobian(i, j);
63  }
64  }
65  }
66 #endif
67 
69  }
70 
71 #ifdef GBL_EIGEN_SUPPORT_ROOT
72 
81  void GblPoint::addMeasurement(const TMatrixD &aProjection,
82  const TVectorD &aResiduals, const TVectorD &aPrecision,
83  double minPrecision) {
84  measDim = aResiduals.GetNrows();
85  measPrecMin = minPrecision;
86  unsigned int iOff = 5 - measDim;
87  for (unsigned int i = 0; i < measDim; ++i) {
88  measResiduals(iOff + i) = aResiduals[i];
89  measPrecision(iOff + i) = aPrecision[i];
90  for (unsigned int j = 0; j < measDim; ++j) {
91  measProjection(iOff + i, iOff + j) = aProjection(i, j);
92  }
93  }
94  }
95 
97 
106  void GblPoint::addMeasurement(const TMatrixD &aProjection,
107  const TVectorD &aResiduals, const TMatrixDSym &aPrecision,
108  double minPrecision) {
109  measDim = aResiduals.GetNrows();
110  measPrecMin = minPrecision;
111  TMatrixDSymEigen measEigen(aPrecision);
112  TMatrixD tmpTransformation(measDim, measDim);
113  tmpTransformation = measEigen.GetEigenVectors();
114  tmpTransformation.T();
115  transFlag = true;
116  TVectorD transResiduals = tmpTransformation * aResiduals;
117  TVectorD transPrecision = measEigen.GetEigenValues();
118  TMatrixD transProjection = tmpTransformation * aProjection;
120  unsigned int iOff = 5 - measDim;
121  for (unsigned int i = 0; i < measDim; ++i) {
122  measResiduals(iOff + i) = transResiduals[i];
123  measPrecision(iOff + i) = transPrecision[i];
124  for (unsigned int j = 0; j < measDim; ++j) {
125  measTransformation(i, j) = tmpTransformation(i, j);
126  measProjection(iOff + i, iOff + j) = transProjection(i, j);
127  }
128  }
129  }
130 
132 
139  void GblPoint::addMeasurement(const TVectorD &aResiduals,
140  const TVectorD &aPrecision, double minPrecision) {
141  measDim = aResiduals.GetNrows();
142  measPrecMin = minPrecision;
143  unsigned int iOff = 5 - measDim;
144  for (unsigned int i = 0; i < measDim; ++i) {
145  measResiduals(iOff + i) = aResiduals[i];
146  measPrecision(iOff + i) = aPrecision[i];
147  }
148  measProjection.setIdentity();
149  }
150 
152 
160  void GblPoint::addMeasurement(const TVectorD &aResiduals,
161  const TMatrixDSym &aPrecision, double minPrecision) {
162  measDim = aResiduals.GetNrows();
163  measPrecMin = minPrecision;
164  TMatrixDSymEigen measEigen(aPrecision);
165  TMatrixD tmpTransformation(measDim, measDim);
166  tmpTransformation = measEigen.GetEigenVectors();
167  tmpTransformation.T();
168  transFlag = true;
169  TVectorD transResiduals = tmpTransformation * aResiduals;
170  TVectorD transPrecision = measEigen.GetEigenValues();
172  unsigned int iOff = 5 - measDim;
173  for (unsigned int i = 0; i < measDim; ++i) {
174  measResiduals(iOff + i) = transResiduals[i];
175  measPrecision(iOff + i) = transPrecision[i];
176  for (unsigned int j = 0; j < measDim; ++j) {
177  measTransformation(i, j) = tmpTransformation(i, j);
178  measProjection(iOff + i, iOff + j) = measTransformation(i, j);
179  }
180  }
181  }
182 #endif
183 
185 
189  unsigned int GblPoint::hasMeasurement() const {
190  return measDim;
191  }
192 
194 
197  double GblPoint::getMeasPrecMin() const {
198  return measPrecMin;
199  }
200 
202 
207  void GblPoint::getMeasurement(Matrix5d &aProjection, Vector5d &aResiduals,
208  Vector5d &aPrecision) const {
209  aProjection.bottomRightCorner(measDim, measDim) =
210  measProjection.bottomRightCorner(measDim, measDim);
211  aResiduals.tail(measDim) = measResiduals.tail(measDim);
212  aPrecision.tail(measDim) = measPrecision.tail(measDim);
213  }
214 
216 
219  void GblPoint::getMeasTransformation(MatrixXd &aTransformation) const {
220  aTransformation.resize(measDim, measDim);
221  if (transFlag) {
222  aTransformation = measTransformation;
223  } else {
224  aTransformation.setIdentity();
225  }
226  }
227 
228 #ifdef GBL_EIGEN_SUPPORT_ROOT
229 
237  void GblPoint::addScatterer(const TVectorD &aResiduals,
238  const TVectorD &aPrecision) {
239  scatFlag = true;
240  scatResiduals(0) = aResiduals[0];
241  scatResiduals(1) = aResiduals[1];
242  scatPrecision(0) = aPrecision[0];
243  scatPrecision(1) = aPrecision[1];
244  scatTransformation.setIdentity();
245  }
246 
248 
263  void GblPoint::addScatterer(const TVectorD &aResiduals,
264  const TMatrixDSym &aPrecision) {
265  scatFlag = true;
266  TMatrixDSymEigen scatEigen(aPrecision);
267  TMatrixD aTransformation = scatEigen.GetEigenVectors();
268  aTransformation.T();
269  TVectorD transResiduals = aTransformation * aResiduals;
270  TVectorD transPrecision = scatEigen.GetEigenValues();
271  scatTransformation.resize(2, 2);
272  for (unsigned int i = 0; i < 2; ++i) {
273  scatResiduals(i) = transResiduals[i];
274  scatPrecision(i) = transPrecision[i];
275  for (unsigned int j = 0; j < 2; ++j) {
276  scatTransformation(i, j) = aTransformation(i, j);
277  }
278  }
279  }
280 #endif
281 
283 
298  void GblPoint::addScatterer(const Vector2d &aResiduals,
299  const Vector2d& aPrecision) {
300  scatFlag = true;
301  scatResiduals = aResiduals;
302  scatPrecision = aPrecision;
303  scatTransformation.setIdentity();
304  }
305 
307  bool GblPoint::hasScatterer() const {
308  return scatFlag;
309  }
310 
312 
317  void GblPoint::getScatterer(Matrix2d &aTransformation, Vector2d &aResiduals,
318  Vector2d &aPrecision) const {
319  aTransformation = scatTransformation;
320  aResiduals = scatResiduals;
321  aPrecision = scatPrecision;
322  }
323 
325 
328  void GblPoint::getScatTransformation(Matrix2d &aTransformation) const {
329  if (scatFlag) {
330  aTransformation = scatTransformation;
331  } else {
332  aTransformation.setIdentity();
333  }
334  }
335 
336 #ifdef GBL_EIGEN_SUPPORT_ROOT
337 
342  void GblPoint::addLocals(const TMatrixD &aDerivatives) {
343  if (measDim) {
344  unsigned int numDer = aDerivatives.GetNcols();
345  localDerivatives.resize(measDim, numDer);
346  // convert from ROOT
347  MatrixXd tmpDerivatives(measDim, numDer);
348  for (unsigned int i = 0; i < measDim; ++i) {
349  for (unsigned int j = 0; j < numDer; ++j)
350  tmpDerivatives(i, j) = aDerivatives(i, j);
351  }
352  if (transFlag) {
353  localDerivatives = measTransformation * tmpDerivatives;
354  } else {
355  localDerivatives = tmpDerivatives;
356  }
357  }
358  }
359 #endif
360 
362  unsigned int GblPoint::getNumLocals() const {
363  return localDerivatives.cols();
364  }
365 
367  const MatrixXd& GblPoint::getLocalDerivatives() const {
368  return localDerivatives;
369  }
370 
371 #ifdef GBL_EIGEN_SUPPORT_ROOT
372 
378  void GblPoint::addGlobals(const std::vector<int> &aLabels,
379  const TMatrixD &aDerivatives) {
380  if (measDim) {
381  globalLabels = aLabels;
382  unsigned int numDer = aDerivatives.GetNcols();
383  globalDerivatives.resize(measDim, numDer);
384  // convert from ROOT
385  MatrixXd tmpDerivatives(measDim, numDer);
386  for (unsigned int i = 0; i < measDim; ++i) {
387  for (unsigned int j = 0; j < numDer; ++j)
388  tmpDerivatives(i, j) = aDerivatives(i, j);
389  }
390  if (transFlag) {
391  globalDerivatives = measTransformation * tmpDerivatives;
392  } else {
393  globalDerivatives = tmpDerivatives;
394  }
395 
396  }
397  }
398 #endif
399 
401  unsigned int GblPoint::getNumGlobals() const {
402  return globalDerivatives.cols();
403  }
404 
406 
409  void GblPoint::getGlobalLabels(std::vector<int> &aLabels) const {
410  aLabels = globalLabels;
411  }
412 
414 
417  void GblPoint::getGlobalDerivatives(MatrixXd &aDerivatives) const {
418  aDerivatives = globalDerivatives;
419  }
420 
422 
428  std::vector<int> &aLabels, std::vector<double> &aDerivatives) const {
429  aLabels.resize(globalDerivatives.cols());
430  aDerivatives.resize(globalDerivatives.cols());
431  for (unsigned int i = 0; i < globalDerivatives.cols(); ++i) {
432  aLabels[i] = globalLabels[i];
433  aDerivatives[i] = globalDerivatives(aRow, i);
434  }
435  }
436 
438 
441  void GblPoint::setLabel(unsigned int aLabel) {
442  theLabel = aLabel;
443  }
444 
446  unsigned int GblPoint::getLabel() const {
447  return theLabel;
448  }
449 
451 
454  void GblPoint::setOffset(int anOffset) {
455  theOffset = anOffset;
456  }
457 
459  int GblPoint::getOffset() const {
460  return theOffset;
461  }
462 
465  return p2pJacobian;
466  }
467 
469 
473  // to optimize: need only two last rows of inverse
474  // prevJacobian = aJac.inverse();
475  // block matrix algebra
476  Matrix23d CA = aJac.block<2, 3>(3, 0) * aJac.block<3, 3>(0, 0).inverse(); // C*A^-1
477  Matrix2d DCAB = aJac.block<2, 2>(3, 3) - CA * aJac.block<3, 2>(0, 3); // D - C*A^-1 *B
478  Matrix2d DCABInv = DCAB.inverse();
479  prevJacobian.block<2, 2>(3, 3) = DCABInv;
480  prevJacobian.block<2, 3>(3, 0) = -DCABInv * CA;
481  }
482 
484 
488  nextJacobian = aJac;
489  }
490 
492 
501  void GblPoint::getDerivatives(int aDirection, Matrix2d &matW, Matrix2d &matWJ,
502  Vector2d &vecWd) const {
503 
504  Matrix2d matJ;
505  Vector2d vecd;
506  if (aDirection < 1) {
507  matJ = prevJacobian.block<2, 2>(3, 3);
508  matW = -prevJacobian.block<2, 2>(3, 1);
509  vecd = prevJacobian.block<2, 1>(3, 0);
510  } else {
511  matJ = nextJacobian.block<2, 2>(3, 3);
512  matW = nextJacobian.block<2, 2>(3, 1);
513  vecd = nextJacobian.block<2, 1>(3, 0);
514  }
515 
516  if (!matW.determinant()) {
517  std::cout << " GblPoint::getDerivatives failed to invert matrix "
518  << std::endl;
519  std::cout
520  << " Possible reason for singular matrix: multiple GblPoints at same arc-length"
521  << std::endl;
522  throw std::overflow_error("Singular matrix inversion exception");
523  }
524  matW = matW.inverse().eval();
525  matWJ = matW * matJ;
526  vecWd = matW * vecd;
527  }
528 
530 
533  void GblPoint::printPoint(unsigned int level) const {
534  std::cout << " GblPoint";
535  if (theLabel) {
536  std::cout << ", label " << theLabel;
537  if (theOffset >= 0) {
538  std::cout << ", offset " << theOffset;
539  }
540  }
541  if (measDim) {
542  std::cout << ", " << measDim << " measurements";
543  }
544  if (scatFlag) {
545  std::cout << ", scatterer";
546  }
547  if (transFlag) {
548  std::cout << ", diagonalized";
549  }
550  if (localDerivatives.cols()) {
551  std::cout << ", " << localDerivatives.cols() << " local derivatives";
552  }
553  if (globalDerivatives.cols()) {
554  std::cout << ", " << globalDerivatives.cols() << " global derivatives";
555  }
556  std::cout << std::endl;
557  if (level > 0) {
558  IOFormat CleanFmt(4, 0, ", ", "\n", "[", "]");
559  if (measDim) {
560  std::cout << " Measurement" << std::endl;
561  std::cout << " Projection: " << std::endl
562  << measProjection.format(CleanFmt) << std::endl;
563  std::cout << " Residuals: "
564  << measResiduals.transpose().format(CleanFmt) << std::endl;
565  std::cout << " Precision (min.: " << measPrecMin << "): "
566  << measPrecision.transpose().format(CleanFmt) << std::endl;
567  }
568  if (scatFlag) {
569  std::cout << " Scatterer" << std::endl;
570  std::cout << " Residuals: "
571  << scatResiduals.transpose().format(CleanFmt) << std::endl;
572  std::cout << " Precision: "
573  << scatPrecision.transpose().format(CleanFmt) << std::endl;
574  }
575  if (localDerivatives.cols()) {
576  std::cout << " Local Derivatives:" << std::endl
577  << localDerivatives.format(CleanFmt) << std::endl;
578  }
579  if (globalDerivatives.cols()) {
580  std::cout << " Global Labels:";
581  for (unsigned int i = 0; i < globalLabels.size(); ++i) {
582  std::cout << " " << globalLabels[i];
583  }
584  std::cout << std::endl;
585  std::cout << " Global Derivatives:"
586  << globalDerivatives.format(CleanFmt) << std::endl;
587  }
588  std::cout << " Jacobian " << std::endl;
589  std::cout << " Point-to-point " << std::endl
590  << p2pJacobian.format(CleanFmt) << std::endl;
591  if (theLabel) {
592  std::cout << " To previous offset " << std::endl
593  << prevJacobian.format(CleanFmt) << std::endl;
594  std::cout << " To next offset " << std::endl
595  << nextJacobian.format(CleanFmt) << std::endl;
596  }
597  }
598  }
599 
600 }
const Eigen::MatrixXd & getLocalDerivatives() const
Retrieve local derivatives from a point.
Definition: GblPoint.cc:367
void addNextJacobian(const Matrix5d &aJac)
Define jacobian to next scatterer (by GBLTrajectory constructor)
Definition: GblPoint.cc:487
void getGlobalDerivatives(Eigen::MatrixXd &aDerivatives) const
Retrieve global derivatives from a point.
Definition: GblPoint.cc:417
Vector5d measResiduals
Measurement residuals.
Definition: GblPoint.h:166
void setLabel(unsigned int aLabel)
Define label of point (by GBLTrajectory constructor)
Definition: GblPoint.cc:441
bool transFlag
Transformation exists?
Definition: GblPoint.h:168
bool scatFlag
Scatterer present?
Definition: GblPoint.h:171
void setOffset(int anOffset)
Define offset for point (by GBLTrajectory constructor)
Definition: GblPoint.cc:454
int theOffset
Offset number at point if not negative (else interpolation needed)
Definition: GblPoint.h:159
GblPoint(const Matrix5d &aJacobian)
Create a point.
Definition: GblPoint.cc:41
Eigen::Vector2d scatPrecision
Scattering precision (diagonal of inverse covariance matrix)
Definition: GblPoint.h:174
bool hasScatterer() const
Check for scatterer at a point.
Definition: GblPoint.cc:307
unsigned int getLabel() const
Retrieve label of point.
Definition: GblPoint.cc:446
double measPrecMin
Minimal measurement precision (for usage)
Definition: GblPoint.h:164
void addGlobals(const std::vector< int > &aLabels, const Eigen::MatrixBase< Derivative > &aDerivatives)
Add global derivatives to a point.
Definition: GblPoint.h:342
void addLocals(const Eigen::MatrixBase< Derivative > &aDerivatives)
Add local derivatives to a point.
Definition: GblPoint.h:323
double getMeasPrecMin() const
get precision cutoff.
Definition: GblPoint.cc:197
unsigned int measDim
Dimension of measurement (1-5), 0 indicates absence of measurement.
Definition: GblPoint.h:163
void getMeasurement(Matrix5d &aProjection, Vector5d &aResiduals, Vector5d &aPrecision) const
Retrieve measurement of a point.
Definition: GblPoint.cc:207
int getOffset() const
Retrieve offset for point.
Definition: GblPoint.cc:459
unsigned int hasMeasurement() const
Check for measurement at a point.
Definition: GblPoint.cc:189
Eigen::MatrixXd globalDerivatives
Derivatives of measurement vs additional global (MP-II) parameters.
Definition: GblPoint.h:177
Eigen::MatrixXd localDerivatives
Derivatives of measurement vs additional local (fit) parameters.
Definition: GblPoint.h:175
void getGlobalLabels(std::vector< int > &aLabels) const
Retrieve global derivatives labels from a point.
Definition: GblPoint.cc:409
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.cc:427
void printPoint(unsigned int level=0) const
Print GblPoint.
Definition: GblPoint.cc:533
std::vector< int > globalLabels
Labels of global (MP-II) derivatives.
Definition: GblPoint.h:176
void addScatterer(const Eigen::Vector2d &aResiduals, const Eigen::MatrixBase< Precision > &aPrecision)
Add a (thin) scatterer to a point.
Definition: GblPoint.h:305
Namespace for the general broken lines package.
unsigned int getNumLocals() const
Retrieve number of local derivatives from a point.
Definition: GblPoint.cc:362
Matrix5d nextJacobian
Jacobian to next scatterer (or last measurement)
Definition: GblPoint.h:162
void getMeasTransformation(Eigen::MatrixXd &aTransformation) const
Get measurement transformation (from diagonalization).
Definition: GblPoint.cc:219
Eigen::Matrix2d scatTransformation
Transformation of diagonalization (of scat. precision matrix)
Definition: GblPoint.h:172
Eigen::Matrix< double, 5, 1 > Vector5d
Definition: GblPoint.h:47
Eigen::Matrix< double, 2, 3 > Matrix23d
Definition: GblPoint.h:48
Vector5d measPrecision
Measurement precision (diagonal of inverse covariance matrix)
Definition: GblPoint.h:167
void getDerivatives(int aDirection, Eigen::Matrix2d &matW, Eigen::Matrix2d &matWJ, Eigen::Vector2d &vecWd) const
Retrieve derivatives of local track model.
Definition: GblPoint.cc:501
Eigen::Vector2d scatResiduals
Scattering residuals (initial kinks if iterating)
Definition: GblPoint.h:173
Matrix5d measProjection
Projection from measurement to local system.
Definition: GblPoint.h:165
void getScatTransformation(Eigen::Matrix2d &aTransformation) const
Get scatterer transformation (from diagonalization).
Definition: GblPoint.cc:328
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:194
Matrix5d prevJacobian
Jacobian to previous scatterer (or first measurement)
Definition: GblPoint.h:161
unsigned int theLabel
Label identifying point.
Definition: GblPoint.h:158
void addPrevJacobian(const Matrix5d &aJac)
Define jacobian to previous scatterer (by GBLTrajectory constructor)
Definition: GblPoint.cc:472
void getScatterer(Eigen::Matrix2d &aTransformation, Eigen::Vector2d &aResiduals, Eigen::Vector2d &aPrecision) const
Retrieve scatterer of a point.
Definition: GblPoint.cc:317
virtual ~GblPoint()
Definition: GblPoint.cc:68
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor, 5, 5 > measTransformation
Transformation of diagonalization (of meas. precision matrix)
Definition: GblPoint.h:170
const Matrix5d & getP2pJacobian() const
Retrieve point-to-(previous)point jacobian.
Definition: GblPoint.cc:464
unsigned int getNumGlobals() const
Retrieve number of global derivatives from a point.
Definition: GblPoint.cc:401
Matrix5d p2pJacobian
Point-to-point jacobian from previous point.
Definition: GblPoint.h:160
Eigen::Matrix< double, 5, 5 > Matrix5d
Definition: GblData.h:43