CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
GblTrajectory.cc
Go to the documentation of this file.
1 /*
2  * GblTrajectory.cpp
3  *
4  * Created on: Aug 18, 2011
5  * Author: kleinwrt
6  * Revision: 113
7  */
8 
104 
106 namespace gbl {
107 
109 
117 GblTrajectory::GblTrajectory(const std::vector<GblPoint> &aPointList,
118  bool flagCurv, bool flagU1dir, bool flagU2dir) :
119  numAllPoints(aPointList.size()), numPoints(), numOffsets(0), numInnerTrans(
120  0), numCurvature(flagCurv ? 1 : 0), numParameters(0), numLocals(
121  0), numMeasurements(0), externalPoint(0), theDimension(0), thePoints(), theData(), measDataIndex(), scatDataIndex(), externalSeed(), innerTransformations(), externalDerivatives(), externalMeasurements(), externalPrecisions() {
122 
123  if (flagU1dir)
124  theDimension.push_back(0);
125  if (flagU2dir)
126  theDimension.push_back(1);
127  // simple (single) trajectory
128  thePoints.push_back(aPointList);
129  numPoints.push_back(numAllPoints);
130  construct(); // construct trajectory
131 }
132 
134 
145 GblTrajectory::GblTrajectory(const std::vector<GblPoint> &aPointList,
146  unsigned int aLabel, const TMatrixDSym &aSeed, bool flagCurv,
147  bool flagU1dir, bool flagU2dir) :
148  numAllPoints(aPointList.size()), numPoints(), numOffsets(0), numInnerTrans(
149  0), numCurvature(flagCurv ? 1 : 0), numParameters(0), numLocals(
150  0), numMeasurements(0), externalPoint(aLabel), theDimension(0), thePoints(), theData(), measDataIndex(), scatDataIndex(), externalSeed(
151  aSeed), innerTransformations(), externalDerivatives(), externalMeasurements(), externalPrecisions() {
152 
153  if (flagU1dir)
154  theDimension.push_back(0);
155  if (flagU2dir)
156  theDimension.push_back(1);
157  // simple (single) trajectory
158  thePoints.push_back(aPointList);
159  numPoints.push_back(numAllPoints);
160  construct(); // construct trajectory
161 }
162 
164 
169  const std::vector<std::pair<std::vector<GblPoint>, TMatrixD> > &aPointsAndTransList) :
170  numAllPoints(), numPoints(), numOffsets(0), numInnerTrans(
171  aPointsAndTransList.size()), numParameters(0), numLocals(0), numMeasurements(
172  0), externalPoint(0), theDimension(0), thePoints(), theData(), measDataIndex(), scatDataIndex(), externalSeed(), innerTransformations(), externalDerivatives(), externalMeasurements(), externalPrecisions() {
173 
174  for (unsigned int iTraj = 0; iTraj < aPointsAndTransList.size(); ++iTraj) {
175  thePoints.push_back(aPointsAndTransList[iTraj].first);
176  numPoints.push_back(thePoints.back().size());
177  numAllPoints += numPoints.back();
178  innerTransformations.push_back(aPointsAndTransList[iTraj].second);
179  }
180  theDimension.push_back(0);
181  theDimension.push_back(1);
182  numCurvature = innerTransformations[0].GetNcols();
183  construct(); // construct (composed) trajectory
184 }
185 
187 
195  const std::vector<std::pair<std::vector<GblPoint>, TMatrixD> > &aPointsAndTransList,
196  const TMatrixD &extDerivatives, const TVectorD &extMeasurements,
197  const TVectorD &extPrecisions) :
198  numAllPoints(), numPoints(), numOffsets(0), numInnerTrans(
199  aPointsAndTransList.size()), numParameters(0), numLocals(0), numMeasurements(
200  0), externalPoint(0), theDimension(0), thePoints(), theData(), measDataIndex(), scatDataIndex(), externalSeed(), innerTransformations(), externalDerivatives(
201  extDerivatives), externalMeasurements(extMeasurements), externalPrecisions(
202  extPrecisions) {
203 
204  for (unsigned int iTraj = 0; iTraj < aPointsAndTransList.size(); ++iTraj) {
205  thePoints.push_back(aPointsAndTransList[iTraj].first);
206  numPoints.push_back(thePoints.back().size());
207  numAllPoints += numPoints.back();
208  innerTransformations.push_back(aPointsAndTransList[iTraj].second);
209  }
210  theDimension.push_back(0);
211  theDimension.push_back(1);
212  numCurvature = innerTransformations[0].GetNcols();
213  construct(); // construct (composed) trajectory
214 }
215 
217 }
218 
221  return constructOK;
222 }
223 
225 unsigned int GblTrajectory::getNumPoints() const {
226  return numAllPoints;
227 }
228 
230 
234 
235  constructOK = false;
236  fitOK = false;
237  unsigned int aLabel = 0;
238  if (numAllPoints < 2) {
239  std::cout << " GblTrajectory construction failed: too few GblPoints "
240  << std::endl;
241  return;
242  }
243  // loop over trajectories
244  numTrajectories = thePoints.size();
245  //std::cout << " numTrajectories: " << numTrajectories << ", " << innerTransformations.size() << std::endl;
246  for (unsigned int iTraj = 0; iTraj < numTrajectories; ++iTraj) {
247  std::vector<GblPoint>::iterator itPoint;
248  for (itPoint = thePoints[iTraj].begin();
249  itPoint < thePoints[iTraj].end(); ++itPoint) {
250  numLocals = std::max(numLocals, itPoint->getNumLocals());
251  numMeasurements += itPoint->hasMeasurement();
252  itPoint->setLabel(++aLabel);
253  }
254  }
255  defineOffsets();
256  calcJacobians();
257  try {
258  prepare();
259  } catch (std::overflow_error &e) {
260  std::cout << " GblTrajectory construction failed: " << e.what()
261  << std::endl;
262  return;
263  }
264  constructOK = true;
265  // number of fit parameters
268 }
269 
271 
276 
277  // loop over trajectories
278  for (unsigned int iTraj = 0; iTraj < numTrajectories; ++iTraj) {
279  // first point is offset
280  thePoints[iTraj].front().setOffset(numOffsets++);
281  // intermediate scatterers are offsets
282  std::vector<GblPoint>::iterator itPoint;
283  for (itPoint = thePoints[iTraj].begin() + 1;
284  itPoint < thePoints[iTraj].end() - 1; ++itPoint) {
285  if (itPoint->hasScatterer()) {
286  itPoint->setOffset(numOffsets++);
287  } else {
288  itPoint->setOffset(-numOffsets);
289  }
290  }
291  // last point is offset
292  thePoints[iTraj].back().setOffset(numOffsets++);
293  }
294 }
295 
298 
299  SMatrix55 scatJacobian;
300  // loop over trajectories
301  for (unsigned int iTraj = 0; iTraj < numTrajectories; ++iTraj) {
302  // forward propagation (all)
303  GblPoint* previousPoint = &thePoints[iTraj].front();
304  unsigned int numStep = 0;
305  std::vector<GblPoint>::iterator itPoint;
306  for (itPoint = thePoints[iTraj].begin() + 1;
307  itPoint < thePoints[iTraj].end(); ++itPoint) {
308  if (numStep == 0) {
309  scatJacobian = itPoint->getP2pJacobian();
310  } else {
311  scatJacobian = itPoint->getP2pJacobian() * scatJacobian;
312  }
313  numStep++;
314  itPoint->addPrevJacobian(scatJacobian); // iPoint -> previous scatterer
315  if (itPoint->getOffset() >= 0) {
316  previousPoint->addNextJacobian(scatJacobian); // lastPoint -> next scatterer
317  numStep = 0;
318  previousPoint = &(*itPoint);
319  }
320  }
321  // backward propagation (without scatterers)
322  for (itPoint = thePoints[iTraj].end() - 1;
323  itPoint > thePoints[iTraj].begin(); --itPoint) {
324  if (itPoint->getOffset() >= 0) {
325  scatJacobian = itPoint->getP2pJacobian();
326  continue; // skip offsets
327  }
328  itPoint->addNextJacobian(scatJacobian); // iPoint -> next scatterer
329  scatJacobian = scatJacobian * itPoint->getP2pJacobian();
330  }
331  }
332 }
333 
335 
343 std::pair<std::vector<unsigned int>, TMatrixD> GblTrajectory::getJacobian(
344  int aSignedLabel) const {
345 
346  unsigned int nDim = theDimension.size();
347  unsigned int nCurv = numCurvature;
348  unsigned int nLocals = numLocals;
349  unsigned int nBorder = nCurv + nLocals;
350  unsigned int nParBRL = nBorder + 2 * nDim;
351  unsigned int nParLoc = nLocals + 5;
352  std::vector<unsigned int> anIndex;
353  anIndex.reserve(nParBRL);
354  TMatrixD aJacobian(nParLoc, nParBRL);
355  aJacobian.Zero();
356 
357  unsigned int aLabel = abs(aSignedLabel);
358  unsigned int firstLabel = 1;
359  unsigned int lastLabel = 0;
360  unsigned int aTrajectory = 0;
361  // loop over trajectories
362  for (unsigned int iTraj = 0; iTraj < numTrajectories; ++iTraj) {
363  aTrajectory = iTraj;
364  lastLabel += numPoints[iTraj];
365  if (aLabel <= lastLabel)
366  break;
367  if (iTraj < numTrajectories - 1)
368  firstLabel += numPoints[iTraj];
369  }
370  int nJacobian; // 0: prev, 1: next
371  // check consistency of (index, direction)
372  if (aSignedLabel > 0) {
373  nJacobian = 1;
374  if (aLabel >= lastLabel) {
375  aLabel = lastLabel;
376  nJacobian = 0;
377  }
378  } else {
379  nJacobian = 0;
380  if (aLabel <= firstLabel) {
381  aLabel = firstLabel;
382  nJacobian = 1;
383  }
384  }
385  const GblPoint aPoint = thePoints[aTrajectory][aLabel - firstLabel];
386  std::vector<unsigned int> labDer(5);
387  SMatrix55 matDer;
388  getFitToLocalJacobian(labDer, matDer, aPoint, 5, nJacobian);
389 
390  // from local parameters
391  for (unsigned int i = 0; i < nLocals; ++i) {
392  aJacobian(i + 5, i) = 1.0;
393  anIndex.push_back(i + 1);
394  }
395  // from trajectory parameters
396  unsigned int iCol = nLocals;
397  for (unsigned int i = 0; i < 5; ++i) {
398  if (labDer[i] > 0) {
399  anIndex.push_back(labDer[i]);
400  for (unsigned int j = 0; j < 5; ++j) {
401  aJacobian(j, iCol) = matDer(j, i);
402  }
403  ++iCol;
404  }
405  }
406  return std::make_pair(anIndex, aJacobian);
407 }
408 
410 
419 void GblTrajectory::getFitToLocalJacobian(std::vector<unsigned int> &anIndex,
420  SMatrix55 &aJacobian, const GblPoint &aPoint, unsigned int measDim,
421  unsigned int nJacobian) const {
422 
423  unsigned int nDim = theDimension.size();
424  unsigned int nCurv = numCurvature;
425  unsigned int nLocals = numLocals;
426 
427  int nOffset = aPoint.getOffset();
428 
429  if (nOffset < 0) // need interpolation
430  {
431  SMatrix22 prevW, prevWJ, nextW, nextWJ, matN;
432  SVector2 prevWd, nextWd;
433  int ierr;
434  aPoint.getDerivatives(0, prevW, prevWJ, prevWd); // W-, W- * J-, W- * d-
435  aPoint.getDerivatives(1, nextW, nextWJ, nextWd); // W-, W- * J-, W- * d-
436  const SMatrix22 sumWJ(prevWJ + nextWJ);
437  matN = sumWJ.Inverse(ierr); // N = (W- * J- + W+ * J+)^-1
438  // derivatives for u_int
439  const SMatrix22 prevNW(matN * prevW); // N * W-
440  const SMatrix22 nextNW(matN * nextW); // N * W+
441  const SVector2 prevNd(matN * prevWd); // N * W- * d-
442  const SVector2 nextNd(matN * nextWd); // N * W+ * d+
443 
444  unsigned int iOff = nDim * (-nOffset - 1) + nLocals + nCurv + 1; // first offset ('i' in u_i)
445 
446  // local offset
447  if (nCurv > 0) {
448  aJacobian.Place_in_col(-prevNd - nextNd, 3, 0); // from curvature
449  anIndex[0] = nLocals + 1;
450  }
451  aJacobian.Place_at(prevNW, 3, 1); // from 1st Offset
452  aJacobian.Place_at(nextNW, 3, 3); // from 2nd Offset
453  for (unsigned int i = 0; i < nDim; ++i) {
454  anIndex[1 + theDimension[i]] = iOff + i;
455  anIndex[3 + theDimension[i]] = iOff + nDim + i;
456  }
457 
458  // local slope and curvature
459  if (measDim > 2) {
460  // derivatives for u'_int
461  const SMatrix22 prevWPN(nextWJ * prevNW); // W+ * J+ * N * W-
462  const SMatrix22 nextWPN(prevWJ * nextNW); // W- * J- * N * W+
463  const SVector2 prevWNd(nextWJ * prevNd); // W+ * J+ * N * W- * d-
464  const SVector2 nextWNd(prevWJ * nextNd); // W- * J- * N * W+ * d+
465  if (nCurv > 0) {
466  aJacobian(0, 0) = 1.0;
467  aJacobian.Place_in_col(prevWNd - nextWNd, 1, 0); // from curvature
468  }
469  aJacobian.Place_at(-prevWPN, 1, 1); // from 1st Offset
470  aJacobian.Place_at(nextWPN, 1, 3); // from 2nd Offset
471  }
472  } else { // at point
473  // anIndex must be sorted
474  // forward : iOff2 = iOff1 + nDim, index1 = 1, index2 = 3
475  // backward: iOff2 = iOff1 - nDim, index1 = 3, index2 = 1
476  unsigned int iOff1 = nDim * nOffset + nCurv + nLocals + 1; // first offset ('i' in u_i)
477  unsigned int index1 = 3 - 2 * nJacobian; // index of first offset
478  unsigned int iOff2 = iOff1 + nDim * (nJacobian * 2 - 1); // second offset ('i' in u_i)
479  unsigned int index2 = 1 + 2 * nJacobian; // index of second offset
480  // local offset
481  aJacobian(3, index1) = 1.0; // from 1st Offset
482  aJacobian(4, index1 + 1) = 1.0;
483  for (unsigned int i = 0; i < nDim; ++i) {
484  anIndex[index1 + theDimension[i]] = iOff1 + i;
485  }
486 
487  // local slope and curvature
488  if (measDim > 2) {
489  SMatrix22 matW, matWJ;
490  SVector2 vecWd;
491  aPoint.getDerivatives(nJacobian, matW, matWJ, vecWd); // W, W * J, W * d
492  double sign = (nJacobian > 0) ? 1. : -1.;
493  if (nCurv > 0) {
494  aJacobian(0, 0) = 1.0;
495  aJacobian.Place_in_col(-sign * vecWd, 1, 0); // from curvature
496  anIndex[0] = nLocals + 1;
497  }
498  aJacobian.Place_at(-sign * matWJ, 1, index1); // from 1st Offset
499  aJacobian.Place_at(sign * matW, 1, index2); // from 2nd Offset
500  for (unsigned int i = 0; i < nDim; ++i) {
501  anIndex[index2 + theDimension[i]] = iOff2 + i;
502  }
503  }
504  }
505 }
506 
508 
514 void GblTrajectory::getFitToKinkJacobian(std::vector<unsigned int> &anIndex,
515  SMatrix27 &aJacobian, const GblPoint &aPoint) const {
516 
517  unsigned int nDim = theDimension.size();
518  unsigned int nCurv = numCurvature;
519  unsigned int nLocals = numLocals;
520 
521  int nOffset = aPoint.getOffset();
522 
523  SMatrix22 prevW, prevWJ, nextW, nextWJ;
524  SVector2 prevWd, nextWd;
525  aPoint.getDerivatives(0, prevW, prevWJ, prevWd); // W-, W- * J-, W- * d-
526  aPoint.getDerivatives(1, nextW, nextWJ, nextWd); // W-, W- * J-, W- * d-
527  const SMatrix22 sumWJ(prevWJ + nextWJ); // W- * J- + W+ * J+
528  const SVector2 sumWd(prevWd + nextWd); // W+ * d+ + W- * d-
529 
530  unsigned int iOff = (nOffset - 1) * nDim + nCurv + nLocals + 1; // first offset ('i' in u_i)
531 
532  // local offset
533  if (nCurv > 0) {
534  aJacobian.Place_in_col(-sumWd, 0, 0); // from curvature
535  anIndex[0] = nLocals + 1;
536  }
537  aJacobian.Place_at(prevW, 0, 1); // from 1st Offset
538  aJacobian.Place_at(-sumWJ, 0, 3); // from 2nd Offset
539  aJacobian.Place_at(nextW, 0, 5); // from 1st Offset
540  for (unsigned int i = 0; i < nDim; ++i) {
541  anIndex[1 + theDimension[i]] = iOff + i;
542  anIndex[3 + theDimension[i]] = iOff + nDim + i;
543  anIndex[5 + theDimension[i]] = iOff + nDim * 2 + i;
544  }
545 }
546 
548 
557 unsigned int GblTrajectory::getResults(int aSignedLabel, TVectorD &localPar,
558  TMatrixDSym &localCov) const {
559  if (not fitOK)
560  return 1;
561  std::pair<std::vector<unsigned int>, TMatrixD> indexAndJacobian =
562  getJacobian(aSignedLabel);
563  unsigned int nParBrl = indexAndJacobian.first.size();
564  TVectorD aVec(nParBrl); // compressed vector
565  for (unsigned int i = 0; i < nParBrl; ++i) {
566  aVec[i] = theVector(indexAndJacobian.first[i] - 1);
567  }
568  TMatrixDSym aMat = theMatrix.getBlockMatrix(indexAndJacobian.first); // compressed matrix
569  localPar = indexAndJacobian.second * aVec;
570  localCov = aMat.Similarity(indexAndJacobian.second);
571  return 0;
572 }
573 
575 
587 unsigned int GblTrajectory::getMeasResults(unsigned int aLabel,
588  unsigned int &numData, TVectorD &aResiduals, TVectorD &aMeasErrors,
589  TVectorD &aResErrors, TVectorD &aDownWeights) {
590  numData = 0;
591  if (not fitOK)
592  return 1;
593 
594  unsigned int firstData = measDataIndex[aLabel - 1]; // first data block with measurement
595  numData = measDataIndex[aLabel] - firstData; // number of data blocks
596  for (unsigned int i = 0; i < numData; ++i) {
597  getResAndErr(firstData + i, aResiduals[i], aMeasErrors[i],
598  aResErrors[i], aDownWeights[i]);
599  }
600  return 0;
601 }
602 
604 
616 unsigned int GblTrajectory::getScatResults(unsigned int aLabel,
617  unsigned int &numData, TVectorD &aResiduals, TVectorD &aMeasErrors,
618  TVectorD &aResErrors, TVectorD &aDownWeights) {
619  numData = 0;
620  if (not fitOK)
621  return 1;
622 
623  unsigned int firstData = scatDataIndex[aLabel - 1]; // first data block with scatterer
624  numData = scatDataIndex[aLabel] - firstData; // number of data blocks
625  for (unsigned int i = 0; i < numData; ++i) {
626  getResAndErr(firstData + i, aResiduals[i], aMeasErrors[i],
627  aResErrors[i], aDownWeights[i]);
628  }
629  return 0;
630 }
631 
633 
636 void GblTrajectory::getLabels(std::vector<unsigned int> &aLabelList) {
637  unsigned int aLabel = 0;
638  unsigned int nPoint = thePoints[0].size();
639  aLabelList.resize(nPoint);
640  for (unsigned i = 0; i < nPoint; ++i) {
641  aLabelList[i] = ++aLabel;
642  }
643 }
644 
646 
650  std::vector<std::vector<unsigned int> > &aLabelList) {
651  unsigned int aLabel = 0;
652  aLabelList.resize(numTrajectories);
653  for (unsigned int iTraj = 0; iTraj < numTrajectories; ++iTraj) {
654  unsigned int nPoint = thePoints[iTraj].size();
655  aLabelList[iTraj].resize(nPoint);
656  for (unsigned i = 0; i < nPoint; ++i) {
657  aLabelList[iTraj][i] = ++aLabel;
658  }
659  }
660 }
661 
663 
672 void GblTrajectory::getResAndErr(unsigned int aData, double &aResidual,
673  double &aMeasError, double &aResError, double &aDownWeight) {
674 
675  double aMeasVar;
676  std::vector<unsigned int>* indLocal;
677  std::vector<double>* derLocal;
678  theData[aData].getResidual(aResidual, aMeasVar, aDownWeight, indLocal,
679  derLocal);
680  unsigned int nParBrl = (*indLocal).size();
681  TVectorD aVec(nParBrl); // compressed vector of derivatives
682  for (unsigned int j = 0; j < nParBrl; ++j) {
683  aVec[j] = (*derLocal)[j];
684  }
685  TMatrixDSym aMat = theMatrix.getBlockMatrix(*indLocal); // compressed (covariance) matrix
686  double aFitVar = aMat.Similarity(aVec); // variance from track fit
687  aMeasError = sqrt(aMeasVar); // error of measurement
688  aResError = (aFitVar < aMeasVar ? sqrt(aMeasVar - aFitVar) : 0.); // error of residual
689 }
690 
693  unsigned int nBorder = numCurvature + numLocals;
695  theMatrix.resize(numParameters, nBorder);
696  double aValue, aWeight;
697  std::vector<unsigned int>* indLocal;
698  std::vector<double>* derLocal;
699  std::vector<GblData>::iterator itData;
700  for (itData = theData.begin(); itData < theData.end(); ++itData) {
701  itData->getLocalData(aValue, aWeight, indLocal, derLocal);
702  for (unsigned int j = 0; j < indLocal->size(); ++j) {
703  theVector((*indLocal)[j] - 1) += (*derLocal)[j] * aWeight * aValue;
704  }
705  theMatrix.addBlockMatrix(aWeight, indLocal, derLocal);
706  }
707 }
708 
710 
714  unsigned int nDim = theDimension.size();
715  // upper limit
716  unsigned int maxData = numMeasurements + nDim * (numOffsets - 2)
717  + externalSeed.GetNrows();
718  theData.reserve(maxData);
719  measDataIndex.resize(numAllPoints + 3); // include external seed and measurements
720  scatDataIndex.resize(numAllPoints + 1);
721  unsigned int nData = 0;
722  std::vector<TMatrixD> innerTransDer;
723  std::vector<std::vector<unsigned int> > innerTransLab;
724  // composed trajectory ?
725  if (numInnerTrans > 0) {
726  //std::cout << "composed trajectory" << std::endl;
727  for (unsigned int iTraj = 0; iTraj < numTrajectories; ++iTraj) {
728  // innermost point
729  GblPoint* innerPoint = &thePoints[iTraj].front();
730  // transformation fit to local track parameters
731  std::vector<unsigned int> firstLabels(5);
732  SMatrix55 matFitToLocal, matLocalToFit;
733  getFitToLocalJacobian(firstLabels, matFitToLocal, *innerPoint, 5);
734  // transformation local track to fit parameters
735  int ierr;
736  matLocalToFit = matFitToLocal.Inverse(ierr);
737  TMatrixD localToFit(5, 5);
738  for (unsigned int i = 0; i < 5; ++i) {
739  for (unsigned int j = 0; j < 5; ++j) {
740  localToFit(i, j) = matLocalToFit(i, j);
741  }
742  }
743  // transformation external to fit parameters at inner (first) point
744  innerTransDer.push_back(localToFit * innerTransformations[iTraj]);
745  innerTransLab.push_back(firstLabels);
746  }
747  }
748  // measurements
749  SMatrix55 matP;
750  // loop over trajectories
751  std::vector<GblPoint>::iterator itPoint;
752  for (unsigned int iTraj = 0; iTraj < numTrajectories; ++iTraj) {
753  for (itPoint = thePoints[iTraj].begin();
754  itPoint < thePoints[iTraj].end(); ++itPoint) {
755  SVector5 aMeas, aPrec;
756  unsigned int nLabel = itPoint->getLabel();
757  unsigned int measDim = itPoint->hasMeasurement();
758  if (measDim) {
759  const TMatrixD localDer = itPoint->getLocalDerivatives();
760  const std::vector<int> globalLab = itPoint->getGlobalLabels();
761  const TMatrixD globalDer = itPoint->getGlobalDerivatives();
762  TMatrixD transDer;
763  itPoint->getMeasurement(matP, aMeas, aPrec);
764  unsigned int iOff = 5 - measDim; // first active component
765  std::vector<unsigned int> labDer(5);
766  SMatrix55 matDer, matPDer;
767  unsigned int nJacobian =
768  (itPoint < thePoints[iTraj].end() - 1) ? 1 : 0; // last point needs backward propagation
769  getFitToLocalJacobian(labDer, matDer, *itPoint, measDim,
770  nJacobian);
771  if (measDim > 2) {
772  matPDer = matP * matDer;
773  } else { // 'shortcut' for position measurements
774  matPDer.Place_at(
775  matP.Sub<SMatrix22>(3, 3)
776  * matDer.Sub<SMatrix25>(3, 0), 3, 0);
777  }
778 
779  if (numInnerTrans > 0) {
780  // transform for external parameters
781  TMatrixD proDer(measDim, 5);
782  // match parameters
783  unsigned int ifirst = 0;
784  unsigned int ilabel = 0;
785  while (ilabel < 5) {
786  if (labDer[ilabel] > 0) {
787  while (innerTransLab[iTraj][ifirst]
788  != labDer[ilabel] and ifirst < 5) {
789  ++ifirst;
790  }
791  if (ifirst >= 5) {
792  labDer[ilabel] -= 2 * nDim * (iTraj + 1); // adjust label
793  } else {
794  // match
795  labDer[ilabel] = 0; // mark as related to external parameters
796  for (unsigned int k = iOff; k < 5; ++k) {
797  proDer(k - iOff, ifirst) = matPDer(k,
798  ilabel);
799  }
800  }
801  }
802  ++ilabel;
803  }
804  transDer.ResizeTo(measDim, numCurvature);
805  transDer = proDer * innerTransDer[iTraj];
806  }
807  for (unsigned int i = iOff; i < 5; ++i) {
808  if (aPrec(i) > 0.) {
809  GblData aData(nLabel, aMeas(i), aPrec(i));
810  aData.addDerivatives(i, labDer, matPDer, iOff, localDer,
811  globalLab, globalDer, numLocals, transDer);
812  theData.push_back(aData);
813  nData++;
814  }
815  }
816 
817  }
818  measDataIndex[nLabel] = nData;
819  }
820  }
821 
822  // pseudo measurements from kinks
823  SMatrix22 matT;
824  scatDataIndex[0] = nData;
825  scatDataIndex[1] = nData;
826  // loop over trajectories
827  for (unsigned int iTraj = 0; iTraj < numTrajectories; ++iTraj) {
828  for (itPoint = thePoints[iTraj].begin() + 1;
829  itPoint < thePoints[iTraj].end() - 1; ++itPoint) {
830  SVector2 aMeas, aPrec;
831  unsigned int nLabel = itPoint->getLabel();
832  if (itPoint->hasScatterer()) {
833  itPoint->getScatterer(matT, aMeas, aPrec);
834  TMatrixD transDer;
835  std::vector<unsigned int> labDer(7);
836  SMatrix27 matDer, matTDer;
837  getFitToKinkJacobian(labDer, matDer, *itPoint);
838  matTDer = matT * matDer;
839  if (numInnerTrans > 0) {
840  // transform for external parameters
841  TMatrixD proDer(nDim, 5);
842  // match parameters
843  unsigned int ifirst = 0;
844  unsigned int ilabel = 0;
845  while (ilabel < 7) {
846  if (labDer[ilabel] > 0) {
847  while (innerTransLab[iTraj][ifirst]
848  != labDer[ilabel] and ifirst < 5) {
849  ++ifirst;
850  }
851  if (ifirst >= 5) {
852  labDer[ilabel] -= 2 * nDim * (iTraj + 1); // adjust label
853  } else {
854  // match
855  labDer[ilabel] = 0; // mark as related to external parameters
856  for (unsigned int k = 0; k < nDim; ++k) {
857  proDer(k, ifirst) = matTDer(k, ilabel);
858  }
859  }
860  }
861  ++ilabel;
862  }
863  transDer.ResizeTo(nDim, numCurvature);
864  transDer = proDer * innerTransDer[iTraj];
865  }
866  for (unsigned int i = 0; i < nDim; ++i) {
867  unsigned int iDim = theDimension[i];
868  if (aPrec(iDim) > 0.) {
869  GblData aData(nLabel, aMeas(iDim), aPrec(iDim));
870  aData.addDerivatives(iDim, labDer, matTDer, numLocals,
871  transDer);
872  theData.push_back(aData);
873  nData++;
874  }
875  }
876  }
877  scatDataIndex[nLabel] = nData;
878  }
879  scatDataIndex[thePoints[iTraj].back().getLabel()] = nData;
880  }
881 
882  // external seed
883  if (externalPoint > 0) {
884  std::pair<std::vector<unsigned int>, TMatrixD> indexAndJacobian =
886  std::vector<unsigned int> externalSeedIndex = indexAndJacobian.first;
887  std::vector<double> externalSeedDerivatives(externalSeedIndex.size());
888  const TMatrixDSymEigen externalSeedEigen(externalSeed);
889  const TVectorD valEigen = externalSeedEigen.GetEigenValues();
890  TMatrixD vecEigen = externalSeedEigen.GetEigenVectors();
891  vecEigen = vecEigen.T() * indexAndJacobian.second;
892  for (int i = 0; i < externalSeed.GetNrows(); ++i) {
893  if (valEigen(i) > 0.) {
894  for (int j = 0; j < externalSeed.GetNcols(); ++j) {
895  externalSeedDerivatives[j] = vecEigen(i, j);
896  }
897  GblData aData(externalPoint, 0., valEigen(i));
898  aData.addDerivatives(externalSeedIndex, externalSeedDerivatives);
899  theData.push_back(aData);
900  nData++;
901  }
902  }
903  }
904  measDataIndex[numAllPoints + 1] = nData;
905  // external measurements
906  unsigned int nExt = externalMeasurements.GetNrows();
907  if (nExt > 0) {
908  std::vector<unsigned int> index(numCurvature);
909  std::vector<double> derivatives(numCurvature);
910  for (unsigned int iExt = 0; iExt < nExt; ++iExt) {
911  for (unsigned int iCol = 0; iCol < numCurvature; ++iCol) {
912  index[iCol] = iCol + 1;
913  derivatives[iCol] = externalDerivatives(iExt, iCol);
914  }
915  GblData aData(1U, externalMeasurements(iExt),
916  externalPrecisions(iExt));
917  aData.addDerivatives(index, derivatives);
918  theData.push_back(aData);
919  nData++;
920  }
921  }
922  measDataIndex[numAllPoints + 2] = nData;
923 }
924 
927  std::vector<GblData>::iterator itData;
928  for (itData = theData.begin(); itData < theData.end(); ++itData) {
929  itData->setPrediction(theVector);
930  }
931 }
932 
934 
937 double GblTrajectory::downWeight(unsigned int aMethod) {
938  double aLoss = 0.;
939  std::vector<GblData>::iterator itData;
940  for (itData = theData.begin(); itData < theData.end(); ++itData) {
941  aLoss += (1. - itData->setDownWeighting(aMethod));
942  }
943  return aLoss;
944 }
945 
947 
956 unsigned int GblTrajectory::fit(double &Chi2, int &Ndf, double &lostWeight,
957  std::string optionList) {
958  const double normChi2[4] = { 1.0, 0.8737, 0.9326, 0.8228 };
959  const std::string methodList = "TtHhCc";
960 
961  Chi2 = 0.;
962  Ndf = -1;
963  lostWeight = 0.;
964  if (not constructOK)
965  return 10;
966 
967  unsigned int aMethod = 0;
968 
970  lostWeight = 0.;
971  unsigned int ierr = 0;
972  try {
973 
975  predict();
976 
977  for (unsigned int i = 0; i < optionList.size(); ++i) // down weighting iterations
978  {
979  size_t aPosition = methodList.find(optionList[i]);
980  if (aPosition != std::string::npos) {
981  aMethod = aPosition / 2 + 1;
982  lostWeight = downWeight(aMethod);
985  predict();
986  }
987  }
988  Ndf = theData.size() - numParameters;
989  Chi2 = 0.;
990  for (unsigned int i = 0; i < theData.size(); ++i) {
991  Chi2 += theData[i].getChi2();
992  }
993  Chi2 /= normChi2[aMethod];
994  fitOK = true;
995 
996  } catch (int e) {
997  std::cout << " GblTrajectory::fit exception " << e << std::endl;
998  ierr = e;
999  }
1000  return ierr;
1001 }
1002 
1005  double aValue;
1006  double aErr;
1007  std::vector<unsigned int>* indLocal;
1008  std::vector<double>* derLocal;
1009  std::vector<int>* labGlobal;
1010  std::vector<double>* derGlobal;
1011 
1012  if (not constructOK)
1013  return;
1014 
1015 // data: measurements, kinks and external seed
1016  std::vector<GblData>::iterator itData;
1017  for (itData = theData.begin(); itData != theData.end(); ++itData) {
1018  itData->getAllData(aValue, aErr, indLocal, derLocal, labGlobal,
1019  derGlobal);
1020  aMille.addData(aValue, aErr, *indLocal, *derLocal, *labGlobal,
1021  *derGlobal);
1022  }
1023  aMille.writeRecord();
1024 }
1025 
1027 
1031  if (numInnerTrans) {
1032  std::cout << "Composed GblTrajectory, " << numInnerTrans
1033  << " subtrajectories" << std::endl;
1034  } else {
1035  std::cout << "Simple GblTrajectory" << std::endl;
1036  }
1037  if (theDimension.size() < 2) {
1038  std::cout << " 2D-trajectory" << std::endl;
1039  }
1040  std::cout << " Number of GblPoints : " << numAllPoints
1041  << std::endl;
1042  std::cout << " Number of points with offsets: " << numOffsets << std::endl;
1043  std::cout << " Number of fit parameters : " << numParameters
1044  << std::endl;
1045  std::cout << " Number of measurements : " << numMeasurements
1046  << std::endl;
1047  if (externalMeasurements.GetNrows()) {
1048  std::cout << " Number of ext. measurements : "
1049  << externalMeasurements.GetNrows() << std::endl;
1050  }
1051  if (externalPoint) {
1052  std::cout << " Label of point with ext. seed: " << externalPoint
1053  << std::endl;
1054  }
1055  if (constructOK) {
1056  std::cout << " Constructed OK " << std::endl;
1057  }
1058  if (fitOK) {
1059  std::cout << " Fitted OK " << std::endl;
1060  }
1061  if (level > 0) {
1062  if (numInnerTrans) {
1063  std::cout << " Inner transformations" << std::endl;
1064  for (unsigned int i = 0; i < numInnerTrans; ++i) {
1065  innerTransformations[i].Print();
1066  }
1067  }
1068  if (externalMeasurements.GetNrows()) {
1069  std::cout << " External measurements" << std::endl;
1070  std::cout << " Measurements:" << std::endl;
1071  externalMeasurements.Print();
1072  std::cout << " Precisions:" << std::endl;
1073  externalPrecisions.Print();
1074  std::cout << " Derivatives:" << std::endl;
1075  externalDerivatives.Print();
1076  }
1077  if (externalPoint) {
1078  std::cout << " External seed:" << std::endl;
1079  externalSeed.Print();
1080  }
1081  if (fitOK) {
1082  std::cout << " Fit results" << std::endl;
1083  std::cout << " Parameters:" << std::endl;
1084  theVector.print();
1085  std::cout << " Covariance matrix (bordered band part):"
1086  << std::endl;
1088  }
1089  }
1090 }
1091 
1093 
1096 void GblTrajectory::printPoints(unsigned int level) {
1097  std::cout << "GblPoints " << std::endl;
1098  for (unsigned int iTraj = 0; iTraj < numTrajectories; ++iTraj) {
1099  std::vector<GblPoint>::iterator itPoint;
1100  for (itPoint = thePoints[iTraj].begin();
1101  itPoint < thePoints[iTraj].end(); ++itPoint) {
1102  itPoint->printPoint(level);
1103  }
1104  }
1105 }
1106 
1109  std::cout << "GblData blocks " << std::endl;
1110  std::vector<GblData>::iterator itData;
1111  for (itData = theData.begin(); itData < theData.end(); ++itData) {
1112  itData->printData();
1113  }
1114 }
1115 
1116 }
void printMatrix() const
Print bordered band matrix.
std::vector< unsigned int > scatDataIndex
mapping points to data blocks from scatterers
Definition: GblTrajectory.h:76
void addBlockMatrix(double aWeight, const std::vector< unsigned int > *anIndex, const std::vector< double > *aVector)
Add symmetric block matrix.
void getDerivatives(int aDirection, SMatrix22 &matW, SMatrix22 &matWJ, SVector2 &vecWd) const
Retrieve derivatives of local track model.
Definition: GblPoint.cc:393
int i
Definition: DBlmapReader.cc:9
void construct()
Construct trajectory from list of points.
Data (block) for independent scalar measurement.
Definition: GblData.h:33
std::pair< std::vector< unsigned int >, TMatrixD > getJacobian(int aSignedLabel) const
Get jacobian for transformation from fit to track parameters at point.
void buildLinearEquationSystem()
Build linear equation system from data (blocks).
void printTrajectory(unsigned int level=0)
Print GblTrajectory.
TVectorD externalPrecisions
Definition: GblTrajectory.h:82
unsigned int numParameters
Number of fit parameters.
Definition: GblTrajectory.h:66
void writeRecord()
Write record to file.
Definition: MilleBinary.cc:89
bool constructOK
Trajectory has been successfully constructed (ready for fit/output)
Definition: GblTrajectory.h:70
std::vector< unsigned int > measDataIndex
mapping points to data blocks from measurements
Definition: GblTrajectory.h:75
bool fitOK
Trajectory has been successfully fitted (results are valid)
Definition: GblTrajectory.h:71
void getFitToLocalJacobian(std::vector< unsigned int > &anIndex, SMatrix55 &aJacobian, const GblPoint &aPoint, unsigned int measDim, unsigned int nJacobian=1) const
Get (part of) jacobian for transformation from (trajectory) fit to track parameters at point...
ROOT::Math::SMatrix< double, 2, 7 > SMatrix27
Definition: GblData.h:22
double sign(double x)
std::vector< unsigned int > theDimension
List of active dimensions (0=u1, 1=u2) in fit.
Definition: GblTrajectory.h:72
TMatrixDSym externalSeed
Precision (inverse covariance matrix) of external seed.
Definition: GblTrajectory.h:77
void print() const
Print vector.
Definition: VMatrix.cc:276
ROOT::Math::SVector< double, 2 > SVector2
Definition: GblPoint.h:29
void predict()
Calculate predictions for all points.
void getFitToKinkJacobian(std::vector< unsigned int > &anIndex, SMatrix27 &aJacobian, const GblPoint &aPoint) const
Get jacobian for transformation from (trajectory) fit to kink parameters at point.
int getOffset() const
Retrieve offset for point.
Definition: GblPoint.cc:349
ROOT::Math::SMatrix< double, 5, 5 > SMatrix55
Definition: GblData.h:23
unsigned int numOffsets
Number of (points with) offsets on trajectory.
Definition: GblTrajectory.h:63
bool isValid() const
Retrieve validity of trajectory.
U second(std::pair< T, U > const &p)
void getLabels(std::vector< unsigned int > &aLabelList)
Get (list of) labels of points on (simple) trajectory.
unsigned int externalPoint
Label of external point (or 0)
Definition: GblTrajectory.h:69
unsigned int numInnerTrans
Number of inner transformations to external parameters.
Definition: GblTrajectory.h:64
void calcJacobians()
Calculate Jacobians to previous/next scatterer from point to point ones.
ROOT::Math::SVector< double, 5 > SVector5
Definition: GblPoint.h:30
unsigned int numMeasurements
Total number of measurements.
Definition: GblTrajectory.h:68
ROOT::Math::SMatrix< double, 2 > SMatrix22
Definition: GblPoint.h:22
T sqrt(T t)
Definition: SSEVec.h:18
void resize(const unsigned int nRows)
Resize vector.
Definition: VMatrix.cc:240
void defineOffsets()
Define offsets from list of points.
void prepare()
Prepare fit for simple or composed trajectory.
unsigned int getMeasResults(unsigned int aLabel, unsigned int &numRes, TVectorD &aResiduals, TVectorD &aMeasErrors, TVectorD &aResErrors, TVectorD &aDownWeights)
Get residuals at point from measurement.
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
int j
Definition: DBlmapReader.cc:9
void printData()
Print GblData blocks for trajectory.
double downWeight(unsigned int aMethod)
Down-weight all points.
#define end
Definition: vmac.h:37
std::vector< unsigned int > numPoints
Number of points on (sub)trajectory.
Definition: GblTrajectory.h:61
std::vector< TMatrixD > innerTransformations
Transformations at innermost points of.
Definition: GblTrajectory.h:78
unsigned int fit(double &Chi2, int &Ndf, double &lostWeight, std::string optionList="")
Perform fit of trajectory.
void milleOut(MilleBinary &aMille)
Write valid trajectory to Millepede-II binary file.
std::vector< GblData > theData
List of data blocks.
Definition: GblTrajectory.h:74
void resize(unsigned int nSize, unsigned int nBorder=1, unsigned int nBand=5)
Resize bordered band matrix.
TMatrixD externalDerivatives
Definition: GblTrajectory.h:80
std::vector< std::vector< GblPoint > > thePoints
(list of) List of points on trajectory
Definition: GblTrajectory.h:73
BorderedBandMatrix theMatrix
(Bordered band) matrix of linear equation system
Definition: GblTrajectory.h:84
VVector theVector
Vector of linear equation system.
Definition: GblTrajectory.h:83
unsigned int numTrajectories
Number of trajectories (in composed trajectory)
Definition: GblTrajectory.h:62
unsigned int getScatResults(unsigned int aLabel, unsigned int &numRes, TVectorD &aResiduals, TVectorD &aMeasErrors, TVectorD &aResErrors, TVectorD &aDownWeights)
Get (kink) residuals at point from scatterer.
#define begin
Definition: vmac.h:30
void addData(double aMeas, double aPrec, const std::vector< unsigned int > &indLocal, const std::vector< double > &derLocal, const std::vector< int > &labGlobal, const std::vector< double > &derGlobal)
Add data block to (end of) record.
Definition: MilleBinary.cc:48
GblTrajectory(const std::vector< GblPoint > &aPointList, bool flagCurv=true, bool flagU1dir=true, bool flagU2dir=true)
Create new (simple) trajectory from list of points.
unsigned int numLocals
Total number of (additional) local parameters.
Definition: GblTrajectory.h:67
virtual ~GblTrajectory()
ROOT::Math::SMatrix< double, 2, 5 > SMatrix25
Definition: GblData.h:21
TMatrixDSym getBlockMatrix(const std::vector< unsigned int > anIndex) const
Retrieve symmetric block matrix.
Millepede-II (binary) record.
Definition: MilleBinary.h:46
void addNextJacobian(const SMatrix55 &aJac)
Define jacobian to next scatterer (by GBLTrajectory constructor)
Definition: GblPoint.cc:379
unsigned int getNumPoints() const
Retrieve number of point from trajectory.
tuple cout
Definition: gather_cfg.py:145
void addDerivatives(unsigned int iRow, const std::vector< unsigned int > &labDer, const SMatrix55 &matDer, unsigned int iOff, const TMatrixD &derLocal, const std::vector< int > &labGlobal, const TMatrixD &derGlobal, unsigned int nLocal, const TMatrixD &derTrans)
Add derivatives from measurement.
Definition: GblData.cc:41
tuple level
Definition: testEve_cfg.py:34
unsigned int numAllPoints
Number of all points on trajectory.
Definition: GblTrajectory.h:60
void printPoints(unsigned int level=0)
Print GblPoints on trajectory.
void solveAndInvertBorderedBand(const VVector &aRightHandSide, VVector &aSolution)
Solve linear equation system, partially calculate inverse.
Definition: Chi2.h:17
void getResAndErr(unsigned int aData, double &aResidual, double &aMeadsError, double &aResError, double &aDownWeight)
Get residual and errors from data block.
unsigned int getResults(int aSignedLabel, TVectorD &localPar, TMatrixDSym &localCov) const
Get fit results at point.
TVectorD externalMeasurements
Definition: GblTrajectory.h:81
tuple size
Write out results.
unsigned int numCurvature
Number of curvature parameters (0 or 1) or external parameters.
Definition: GblTrajectory.h:65
const SMatrix55 & getP2pJacobian() const
Retrieve point-to-(previous)point jacobian.
Definition: GblPoint.cc:354
Point on trajectory.
Definition: GblPoint.h:46