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() {
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() {
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() {
174 for (
unsigned int iTraj = 0; iTraj < aPointsAndTransList.size(); ++iTraj) {
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(
204 for (
unsigned int iTraj = 0; iTraj < aPointsAndTransList.size(); ++iTraj) {
237 unsigned int aLabel = 0;
239 std::cout <<
" GblTrajectory construction failed: too few GblPoints "
247 std::vector<GblPoint>::iterator itPoint;
249 itPoint <
thePoints[iTraj].end(); ++itPoint) {
252 itPoint->setLabel(++aLabel);
259 }
catch (std::overflow_error &
e) {
260 std::cout <<
" GblTrajectory construction failed: " << e.what()
282 std::vector<GblPoint>::iterator itPoint;
284 itPoint <
thePoints[iTraj].end() - 1; ++itPoint) {
285 if (itPoint->hasScatterer()) {
304 unsigned int numStep = 0;
305 std::vector<GblPoint>::iterator itPoint;
307 itPoint <
thePoints[iTraj].end(); ++itPoint) {
309 scatJacobian = itPoint->getP2pJacobian();
311 scatJacobian = itPoint->getP2pJacobian() * scatJacobian;
314 itPoint->addPrevJacobian(scatJacobian);
315 if (itPoint->getOffset() >= 0) {
318 previousPoint = &(*itPoint);
323 itPoint >
thePoints[iTraj].begin(); --itPoint) {
324 if (itPoint->getOffset() >= 0) {
328 itPoint->addNextJacobian(scatJacobian);
329 scatJacobian = scatJacobian * itPoint->getP2pJacobian();
344 int aSignedLabel)
const {
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);
357 unsigned int aLabel =
abs(aSignedLabel);
358 unsigned int firstLabel = 1;
359 unsigned int lastLabel = 0;
360 unsigned int aTrajectory = 0;
365 if (aLabel <= lastLabel)
367 if (iTraj < numTrajectories - 1)
372 if (aSignedLabel > 0) {
374 if (aLabel >= lastLabel) {
380 if (aLabel <= firstLabel) {
386 std::vector<unsigned int> labDer(5);
391 for (
unsigned int i = 0;
i < nLocals; ++
i) {
392 aJacobian(
i + 5,
i) = 1.0;
393 anIndex.push_back(
i + 1);
396 unsigned int iCol = nLocals;
397 for (
unsigned int i = 0;
i < 5; ++
i) {
399 anIndex.push_back(labDer[
i]);
400 for (
unsigned int j = 0;
j < 5; ++
j) {
401 aJacobian(
j, iCol) = matDer(
j, i);
406 return std::make_pair(anIndex, aJacobian);
421 unsigned int nJacobian)
const {
431 SMatrix22 prevW, prevWJ, nextW, nextWJ, matN;
437 matN = sumWJ.Inverse(ierr);
441 const SVector2 prevNd(matN * prevWd);
442 const SVector2 nextNd(matN * nextWd);
444 unsigned int iOff = nDim * (-nOffset - 1) + nLocals + nCurv + 1;
448 aJacobian.Place_in_col(-prevNd - nextNd, 3, 0);
449 anIndex[0] = nLocals + 1;
451 aJacobian.Place_at(prevNW, 3, 1);
452 aJacobian.Place_at(nextNW, 3, 3);
453 for (
unsigned int i = 0;
i < nDim; ++
i) {
455 anIndex[3 + theDimension[
i]] = iOff + nDim +
i;
461 const SMatrix22 prevWPN(nextWJ * prevNW);
462 const SMatrix22 nextWPN(prevWJ * nextNW);
463 const SVector2 prevWNd(nextWJ * prevNd);
464 const SVector2 nextWNd(prevWJ * nextNd);
466 aJacobian(0, 0) = 1.0;
467 aJacobian.Place_in_col(prevWNd - nextWNd, 1, 0);
469 aJacobian.Place_at(-prevWPN, 1, 1);
470 aJacobian.Place_at(nextWPN, 1, 3);
476 unsigned int iOff1 = nDim * nOffset + nCurv + nLocals + 1;
477 unsigned int index1 = 3 - 2 * nJacobian;
478 unsigned int iOff2 = iOff1 + nDim * (nJacobian * 2 - 1);
479 unsigned int index2 = 1 + 2 * nJacobian;
481 aJacobian(3, index1) = 1.0;
482 aJacobian(4, index1 + 1) = 1.0;
483 for (
unsigned int i = 0;
i < nDim; ++
i) {
492 double sign = (nJacobian > 0) ? 1. : -1.;
494 aJacobian(0, 0) = 1.0;
495 aJacobian.Place_in_col(-sign * vecWd, 1, 0);
496 anIndex[0] = nLocals + 1;
498 aJacobian.Place_at(-sign * matWJ, 1, index1);
499 aJacobian.Place_at(sign * matW, 1, index2);
500 for (
unsigned int i = 0;
i < nDim; ++
i) {
528 const SVector2 sumWd(prevWd + nextWd);
530 unsigned int iOff = (nOffset - 1) * nDim + nCurv + nLocals + 1;
534 aJacobian.Place_in_col(-sumWd, 0, 0);
535 anIndex[0] = nLocals + 1;
537 aJacobian.Place_at(prevW, 0, 1);
538 aJacobian.Place_at(-sumWJ, 0, 3);
539 aJacobian.Place_at(nextW, 0, 5);
540 for (
unsigned int i = 0;
i < nDim; ++
i) {
542 anIndex[3 + theDimension[
i]] = iOff + nDim +
i;
543 anIndex[5 + theDimension[
i]] = iOff + nDim * 2 +
i;
558 TMatrixDSym &localCov)
const {
561 std::pair<std::vector<unsigned int>, TMatrixD> indexAndJacobian =
563 unsigned int nParBrl = indexAndJacobian.first.size();
564 TVectorD aVec(nParBrl);
565 for (
unsigned int i = 0;
i < nParBrl; ++
i) {
566 aVec[
i] =
theVector(indexAndJacobian.first[
i] - 1);
569 localPar = indexAndJacobian.second * aVec;
570 localCov = aMat.Similarity(indexAndJacobian.second);
588 unsigned int &numData, TVectorD &aResiduals, TVectorD &aMeasErrors,
589 TVectorD &aResErrors, TVectorD &aDownWeights) {
596 for (
unsigned int i = 0;
i < numData; ++
i) {
598 aResErrors[i], aDownWeights[i]);
617 unsigned int &numData, TVectorD &aResiduals, TVectorD &aMeasErrors,
618 TVectorD &aResErrors, TVectorD &aDownWeights) {
625 for (
unsigned int i = 0;
i < numData; ++
i) {
627 aResErrors[i], aDownWeights[i]);
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;
650 std::vector<std::vector<unsigned int> > &aLabelList) {
651 unsigned int aLabel = 0;
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;
673 double &aMeasError,
double &aResError,
double &aDownWeight) {
676 std::vector<unsigned int>* indLocal;
677 std::vector<double>* derLocal;
678 theData[aData].getResidual(aResidual, aMeasVar, aDownWeight, indLocal,
680 unsigned int nParBrl = (*indLocal).size();
681 TVectorD aVec(nParBrl);
682 for (
unsigned int j = 0;
j < nParBrl; ++
j) {
683 aVec[
j] = (*derLocal)[
j];
686 double aFitVar = aMat.Similarity(aVec);
687 aMeasError =
sqrt(aMeasVar);
688 aResError = (aFitVar < aMeasVar ?
sqrt(aMeasVar - aFitVar) : 0.);
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;
721 unsigned int nData = 0;
722 std::vector<TMatrixD> innerTransDer;
723 std::vector<std::vector<unsigned int> > innerTransLab;
731 std::vector<unsigned int> firstLabels(5);
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);
745 innerTransLab.push_back(firstLabels);
751 std::vector<GblPoint>::iterator itPoint;
754 itPoint <
thePoints[iTraj].end(); ++itPoint) {
756 unsigned int nLabel = itPoint->getLabel();
757 unsigned int measDim = itPoint->hasMeasurement();
759 const TMatrixD localDer = itPoint->getLocalDerivatives();
760 const std::vector<int> globalLab = itPoint->getGlobalLabels();
761 const TMatrixD globalDer = itPoint->getGlobalDerivatives();
763 itPoint->getMeasurement(matP, aMeas, aPrec);
764 unsigned int iOff = 5 - measDim;
765 std::vector<unsigned int> labDer(5);
767 unsigned int nJacobian =
768 (itPoint <
thePoints[iTraj].end() - 1) ? 1 : 0;
772 matPDer = matP * matDer;
781 TMatrixD proDer(measDim, 5);
783 unsigned int ifirst = 0;
784 unsigned int ilabel = 0;
786 if (labDer[ilabel] > 0) {
787 while (innerTransLab[iTraj][ifirst]
788 != labDer[ilabel] and ifirst < 5) {
792 labDer[ilabel] -= 2 * nDim * (iTraj + 1);
796 for (
unsigned int k = iOff;
k < 5; ++
k) {
797 proDer(
k - iOff, ifirst) = matPDer(
k,
805 transDer = proDer * innerTransDer[iTraj];
807 for (
unsigned int i = iOff;
i < 5; ++
i) {
809 GblData aData(nLabel, aMeas(
i), aPrec(
i));
811 globalLab, globalDer,
numLocals, transDer);
829 itPoint <
thePoints[iTraj].end() - 1; ++itPoint) {
831 unsigned int nLabel = itPoint->getLabel();
832 if (itPoint->hasScatterer()) {
833 itPoint->getScatterer(matT, aMeas, aPrec);
835 std::vector<unsigned int> labDer(7);
838 matTDer = matT * matDer;
841 TMatrixD proDer(nDim, 5);
843 unsigned int ifirst = 0;
844 unsigned int ilabel = 0;
846 if (labDer[ilabel] > 0) {
847 while (innerTransLab[iTraj][ifirst]
848 != labDer[ilabel] and ifirst < 5) {
852 labDer[ilabel] -= 2 * nDim * (iTraj + 1);
856 for (
unsigned int k = 0;
k < nDim; ++
k) {
857 proDer(
k, ifirst) = matTDer(
k, ilabel);
864 transDer = proDer * innerTransDer[iTraj];
866 for (
unsigned int i = 0;
i < nDim; ++
i) {
868 if (aPrec(iDim) > 0.) {
869 GblData aData(nLabel, aMeas(iDim), aPrec(iDim));
884 std::pair<std::vector<unsigned int>, TMatrixD> indexAndJacobian =
886 std::vector<unsigned int> externalSeedIndex = indexAndJacobian.first;
887 std::vector<double> externalSeedDerivatives(externalSeedIndex.size());
889 const TVectorD valEigen = externalSeedEigen.GetEigenValues();
890 TMatrixD vecEigen = externalSeedEigen.GetEigenVectors();
891 vecEigen = vecEigen.T() * indexAndJacobian.second;
893 if (valEigen(
i) > 0.) {
895 externalSeedDerivatives[
j] = vecEigen(
i,
j);
910 for (
unsigned int iExt = 0; iExt < nExt; ++iExt) {
911 for (
unsigned int iCol = 0; iCol <
numCurvature; ++iCol) {
912 index[iCol] = iCol + 1;
927 std::vector<GblData>::iterator itData;
928 for (itData =
theData.begin(); itData <
theData.end(); ++itData) {
939 std::vector<GblData>::iterator itData;
940 for (itData =
theData.begin(); itData <
theData.end(); ++itData) {
941 aLoss += (1. - itData->setDownWeighting(aMethod));
958 const double normChi2[4] = { 1.0, 0.8737, 0.9326, 0.8228 };
967 unsigned int aMethod = 0;
971 unsigned int ierr = 0;
977 for (
unsigned int i = 0;
i < optionList.size(); ++
i)
979 size_t aPosition = methodList.find(optionList[
i]);
980 if (aPosition != std::string::npos) {
981 aMethod = aPosition / 2 + 1;
990 for (
unsigned int i = 0;
i <
theData.size(); ++
i) {
993 Chi2 /= normChi2[aMethod];
997 std::cout <<
" GblTrajectory::fit exception " << e << std::endl;
1007 std::vector<unsigned int>* indLocal;
1008 std::vector<double>* derLocal;
1009 std::vector<int>* labGlobal;
1010 std::vector<double>* derGlobal;
1016 std::vector<GblData>::iterator itData;
1017 for (itData =
theData.begin(); itData !=
theData.end(); ++itData) {
1018 itData->getAllData(aValue, aErr, indLocal, derLocal, labGlobal,
1020 aMille.
addData(aValue, aErr, *indLocal, *derLocal, *labGlobal,
1033 <<
" subtrajectories" << std::endl;
1035 std::cout <<
"Simple GblTrajectory" << std::endl;
1038 std::cout <<
" 2D-trajectory" << std::endl;
1048 std::cout <<
" Number of ext. measurements : "
1056 std::cout <<
" Constructed OK " << std::endl;
1059 std::cout <<
" Fitted OK " << std::endl;
1063 std::cout <<
" Inner transformations" << std::endl;
1069 std::cout <<
" External measurements" << std::endl;
1070 std::cout <<
" Measurements:" << std::endl;
1072 std::cout <<
" Precisions:" << std::endl;
1074 std::cout <<
" Derivatives:" << std::endl;
1078 std::cout <<
" External seed:" << std::endl;
1082 std::cout <<
" Fit results" << std::endl;
1083 std::cout <<
" Parameters:" << std::endl;
1085 std::cout <<
" Covariance matrix (bordered band part):"
1099 std::vector<GblPoint>::iterator itPoint;
1101 itPoint <
thePoints[iTraj].end(); ++itPoint) {
1102 itPoint->printPoint(level);
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();
void printMatrix() const
Print bordered band matrix.
std::vector< unsigned int > scatDataIndex
mapping points to data blocks from scatterers
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.
void construct()
Construct trajectory from list of points.
Data (block) for independent scalar measurement.
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
unsigned int numParameters
Number of fit parameters.
void writeRecord()
Write record to file.
bool constructOK
Trajectory has been successfully constructed (ready for fit/output)
std::vector< unsigned int > measDataIndex
mapping points to data blocks from measurements
bool fitOK
Trajectory has been successfully fitted (results are valid)
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
std::vector< unsigned int > theDimension
List of active dimensions (0=u1, 1=u2) in fit.
TMatrixDSym externalSeed
Precision (inverse covariance matrix) of external seed.
void print() const
Print vector.
ROOT::Math::SVector< double, 2 > SVector2
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.
ROOT::Math::SMatrix< double, 5, 5 > SMatrix55
unsigned int numOffsets
Number of (points with) offsets on trajectory.
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)
unsigned int numInnerTrans
Number of inner transformations to external parameters.
void calcJacobians()
Calculate Jacobians to previous/next scatterer from point to point ones.
ROOT::Math::SVector< double, 5 > SVector5
unsigned int numMeasurements
Total number of measurements.
ROOT::Math::SMatrix< double, 2 > SMatrix22
void resize(const unsigned int nRows)
Resize vector.
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)
void printData()
Print GblData blocks for trajectory.
double downWeight(unsigned int aMethod)
Down-weight all points.
std::vector< unsigned int > numPoints
Number of points on (sub)trajectory.
std::vector< TMatrixD > innerTransformations
Transformations at innermost points of.
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.
void resize(unsigned int nSize, unsigned int nBorder=1, unsigned int nBand=5)
Resize bordered band matrix.
TMatrixD externalDerivatives
std::vector< std::vector< GblPoint > > thePoints
(list of) List of points on trajectory
BorderedBandMatrix theMatrix
(Bordered band) matrix of linear equation system
VVector theVector
Vector of linear equation system.
unsigned int numTrajectories
Number of trajectories (in composed trajectory)
unsigned int getScatResults(unsigned int aLabel, unsigned int &numRes, TVectorD &aResiduals, TVectorD &aMeasErrors, TVectorD &aResErrors, TVectorD &aDownWeights)
Get (kink) residuals at point from scatterer.
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.
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.
ROOT::Math::SMatrix< double, 2, 5 > SMatrix25
TMatrixDSym getBlockMatrix(const std::vector< unsigned int > anIndex) const
Retrieve symmetric block matrix.
Millepede-II (binary) record.
void addNextJacobian(const SMatrix55 &aJac)
Define jacobian to next scatterer (by GBLTrajectory constructor)
unsigned int getNumPoints() const
Retrieve number of point from trajectory.
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.
unsigned int numAllPoints
Number of all points on trajectory.
void printPoints(unsigned int level=0)
Print GblPoints on trajectory.
void solveAndInvertBorderedBand(const VVector &aRightHandSide, VVector &aSolution)
Solve linear equation system, partially calculate inverse.
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
tuple size
Write out results.
unsigned int numCurvature
Number of curvature parameters (0 or 1) or external parameters.
const SMatrix55 & getP2pJacobian() const
Retrieve point-to-(previous)point jacobian.