148 bool flagCurv,
bool flagU1dir,
bool flagU2dir) :
149 numAllPoints(aPointList.size()), numPoints(), numOffsets(0), numInnerTrans(
150 0), numCurvature(flagCurv ? 1 : 0), numParameters(0), numLocals(
151 0), numMeasurements(0), externalPoint(0), theDimension(0), thePoints(), theData(), measDataIndex(), scatDataIndex(), externalSeed(), innerTransformations(), externalDerivatives(), externalMeasurements(), externalPrecisions() {
176 unsigned int aLabel,
const TMatrixDSym &aSeed,
bool flagCurv,
177 bool flagU1dir,
bool flagU2dir) :
178 numAllPoints(aPointList.size()), numPoints(), numOffsets(0), numInnerTrans(
179 0), numCurvature(flagCurv ? 1 : 0), numParameters(0), numLocals(
180 0), numMeasurements(0), externalPoint(aLabel), theDimension(0), thePoints(), theData(), measDataIndex(), scatDataIndex(), externalSeed(
181 aSeed), innerTransformations(), externalDerivatives(), externalMeasurements(), externalPrecisions() {
199 const std::vector<std::pair<std::vector<GblPoint>, TMatrixD> > &aPointsAndTransList) :
200 numAllPoints(), numPoints(), numOffsets(0), numInnerTrans(
201 aPointsAndTransList.size()), numParameters(0), numLocals(0), numMeasurements(
202 0), externalPoint(0), theDimension(0), thePoints(), theData(), measDataIndex(), scatDataIndex(), externalSeed(), innerTransformations(), externalDerivatives(), externalMeasurements(), externalPrecisions() {
204 for (
unsigned int iTraj = 0; iTraj < aPointsAndTransList.size(); ++iTraj) {
205 thePoints.push_back(aPointsAndTransList[iTraj].first);
225 const std::vector<std::pair<std::vector<GblPoint>, TMatrixD> > &aPointsAndTransList,
226 const TMatrixD &extDerivatives,
const TVectorD &extMeasurements,
227 const TVectorD &extPrecisions) :
228 numAllPoints(), numPoints(), numOffsets(0), numInnerTrans(
229 aPointsAndTransList.size()), numParameters(0), numLocals(0), numMeasurements(
230 0), externalPoint(0), theDimension(0), thePoints(), theData(), measDataIndex(), scatDataIndex(), externalSeed(), innerTransformations(), externalDerivatives(
231 extDerivatives), externalMeasurements(extMeasurements), externalPrecisions(
234 for (
unsigned int iTraj = 0; iTraj < aPointsAndTransList.size(); ++iTraj) {
235 thePoints.push_back(aPointsAndTransList[iTraj].first);
255 const std::vector<std::pair<std::vector<GblPoint>, TMatrixD> > &aPointsAndTransList,
256 const TMatrixD &extDerivatives,
const TVectorD &extMeasurements,
257 const TMatrixDSym &extPrecisions) :
258 numAllPoints(), numPoints(), numOffsets(0), numInnerTrans(
259 aPointsAndTransList.size()), numParameters(0), numLocals(0), numMeasurements(
260 0), externalPoint(0), theDimension(0), thePoints(), theData(), measDataIndex(), scatDataIndex(), externalSeed(), innerTransformations() {
263 TMatrixDSymEigen extEigen(extPrecisions);
264 TMatrixD extTransformation = extEigen.GetEigenVectors();
265 extTransformation.T();
273 for (
unsigned int iTraj = 0; iTraj < aPointsAndTransList.size(); ++iTraj) {
274 thePoints.push_back(aPointsAndTransList[iTraj].first);
306 unsigned int aLabel = 0;
308 std::cout <<
" GblTrajectory construction failed: too few GblPoints "
316 std::vector<GblPoint>::iterator itPoint;
318 itPoint <
thePoints[iTraj].end(); ++itPoint) {
321 itPoint->setLabel(++aLabel);
328 }
catch (std::overflow_error &
e) {
329 std::cout <<
" GblTrajectory construction failed: " << e.what()
351 std::vector<GblPoint>::iterator itPoint;
352 for (itPoint =
thePoints[iTraj].begin() + 1;
353 itPoint <
thePoints[iTraj].end() - 1; ++itPoint) {
354 if (itPoint->hasScatterer()) {
373 unsigned int numStep = 0;
374 std::vector<GblPoint>::iterator itPoint;
375 for (itPoint =
thePoints[iTraj].begin() + 1;
376 itPoint <
thePoints[iTraj].end(); ++itPoint) {
378 scatJacobian = itPoint->getP2pJacobian();
380 scatJacobian = itPoint->getP2pJacobian() * scatJacobian;
383 itPoint->addPrevJacobian(scatJacobian);
384 if (itPoint->getOffset() >= 0) {
387 previousPoint = &(*itPoint);
391 for (itPoint =
thePoints[iTraj].end() - 1;
392 itPoint >
thePoints[iTraj].begin(); --itPoint) {
393 if (itPoint->getOffset() >= 0) {
394 scatJacobian = itPoint->getP2pJacobian();
397 itPoint->addNextJacobian(scatJacobian);
398 scatJacobian = scatJacobian * itPoint->getP2pJacobian();
413 int aSignedLabel)
const {
418 unsigned int nBorder = nCurv + nLocals;
419 unsigned int nParBRL = nBorder + 2 * nDim;
420 unsigned int nParLoc = nLocals + 5;
421 std::vector<unsigned int> anIndex;
422 anIndex.reserve(nParBRL);
423 TMatrixD aJacobian(nParLoc, nParBRL);
426 unsigned int aLabel =
abs(aSignedLabel);
427 unsigned int firstLabel = 1;
428 unsigned int lastLabel = 0;
429 unsigned int aTrajectory = 0;
434 if (aLabel <= lastLabel)
436 if (iTraj < numTrajectories - 1)
441 if (aSignedLabel > 0) {
443 if (aLabel >= lastLabel) {
449 if (aLabel <= firstLabel) {
455 std::vector<unsigned int> labDer(5);
460 for (
unsigned int i = 0; i < nLocals; ++i) {
461 aJacobian(i + 5, i) = 1.0;
462 anIndex.push_back(i + 1);
465 unsigned int iCol = nLocals;
466 for (
unsigned int i = 0; i < 5; ++i) {
468 anIndex.push_back(labDer[i]);
469 for (
unsigned int j = 0; j < 5; ++j) {
470 aJacobian(j, iCol) = matDer(j, i);
475 return std::make_pair(anIndex, aJacobian);
490 unsigned int nJacobian)
const {
500 SMatrix22 prevW, prevWJ, nextW, nextWJ, matN;
506 matN = sumWJ.Inverse(ierr);
510 const SVector2 prevNd(matN * prevWd);
511 const SVector2 nextNd(matN * nextWd);
513 unsigned int iOff = nDim * (-nOffset - 1) + nLocals + nCurv + 1;
517 aJacobian.Place_in_col(-prevNd - nextNd, 3, 0);
518 anIndex[0] = nLocals + 1;
520 aJacobian.Place_at(prevNW, 3, 1);
521 aJacobian.Place_at(nextNW, 3, 3);
522 for (
unsigned int i = 0; i < nDim; ++i) {
524 anIndex[3 + theDimension[i]] = iOff + nDim + i;
530 const SMatrix22 prevWPN(nextWJ * prevNW);
531 const SMatrix22 nextWPN(prevWJ * nextNW);
532 const SVector2 prevWNd(nextWJ * prevNd);
533 const SVector2 nextWNd(prevWJ * nextNd);
535 aJacobian(0, 0) = 1.0;
536 aJacobian.Place_in_col(prevWNd - nextWNd, 1, 0);
538 aJacobian.Place_at(-prevWPN, 1, 1);
539 aJacobian.Place_at(nextWPN, 1, 3);
545 unsigned int iOff1 = nDim * nOffset + nCurv + nLocals + 1;
546 unsigned int index1 = 3 - 2 * nJacobian;
547 unsigned int iOff2 = iOff1 + nDim * (nJacobian * 2 - 1);
548 unsigned int index2 = 1 + 2 * nJacobian;
550 aJacobian(3, index1) = 1.0;
551 aJacobian(4, index1 + 1) = 1.0;
552 for (
unsigned int i = 0; i < nDim; ++i) {
561 double sign = (nJacobian > 0) ? 1. : -1.;
563 aJacobian(0, 0) = 1.0;
564 aJacobian.Place_in_col(-sign * vecWd, 1, 0);
565 anIndex[0] = nLocals + 1;
567 aJacobian.Place_at(-sign * matWJ, 1, index1);
568 aJacobian.Place_at(sign * matW, 1, index2);
569 for (
unsigned int i = 0; i < nDim; ++i) {
597 const SVector2 sumWd(prevWd + nextWd);
599 unsigned int iOff = (nOffset - 1) * nDim + nCurv + nLocals + 1;
603 aJacobian.Place_in_col(-sumWd, 0, 0);
604 anIndex[0] = nLocals + 1;
606 aJacobian.Place_at(prevW, 0, 1);
607 aJacobian.Place_at(-sumWJ, 0, 3);
608 aJacobian.Place_at(nextW, 0, 5);
609 for (
unsigned int i = 0; i < nDim; ++i) {
611 anIndex[3 + theDimension[i]] = iOff + nDim + i;
612 anIndex[5 + theDimension[i]] = iOff + nDim * 2 + i;
632 TMatrixDSym &localCov)
const {
635 std::pair<std::vector<unsigned int>, TMatrixD> indexAndJacobian =
637 unsigned int nParBrl = indexAndJacobian.first.size();
638 TVectorD aVec(nParBrl);
639 for (
unsigned int i = 0; i < nParBrl; ++i) {
640 aVec[i] =
theVector(indexAndJacobian.first[i] - 1);
643 localPar = indexAndJacobian.second * aVec;
644 localCov = aMat.Similarity(indexAndJacobian.second);
662 unsigned int &numData, TVectorD &aResiduals, TVectorD &aMeasErrors,
663 TVectorD &aResErrors, TVectorD &aDownWeights) {
670 for (
unsigned int i = 0; i < numData; ++i) {
671 getResAndErr(firstData + i, aResiduals[i], aMeasErrors[i],
672 aResErrors[i], aDownWeights[i]);
691 unsigned int &numData, TVectorD &aResiduals, TVectorD &aMeasErrors,
692 TVectorD &aResErrors, TVectorD &aDownWeights) {
699 for (
unsigned int i = 0; i < numData; ++i) {
700 getResAndErr(firstData + i, aResiduals[i], aMeasErrors[i],
701 aResErrors[i], aDownWeights[i]);
715 unsigned int aLabel = 0;
716 unsigned int nPoint =
thePoints[0].size();
717 aLabelList.resize(nPoint);
718 for (
unsigned i = 0; i < nPoint; ++i) {
719 aLabelList[i] = ++aLabel;
730 std::vector<std::vector<unsigned int> > &aLabelList) {
734 unsigned int aLabel = 0;
737 unsigned int nPoint =
thePoints[iTraj].size();
738 aLabelList[iTraj].resize(nPoint);
739 for (
unsigned i = 0; i < nPoint; ++i) {
740 aLabelList[iTraj][i] = ++aLabel;
757 double &aMeasError,
double &aResError,
double &aDownWeight) {
760 std::vector<unsigned int>* indLocal;
761 std::vector<double>* derLocal;
762 theData[aData].getResidual(aResidual, aMeasVar, aDownWeight, indLocal,
764 unsigned int nParBrl = (*indLocal).size();
765 TVectorD aVec(nParBrl);
766 for (
unsigned int j = 0; j < nParBrl; ++j) {
767 aVec[j] = (*derLocal)[j];
770 double aFitVar = aMat.Similarity(aVec);
771 aMeasError = sqrt(aMeasVar);
772 aResError = (aFitVar < aMeasVar ? sqrt(aMeasVar - aFitVar) : 0.);
780 double aValue, aWeight;
781 std::vector<unsigned int>* indLocal;
782 std::vector<double>* derLocal;
783 std::vector<GblData>::iterator itData;
784 for (itData =
theData.begin(); itData <
theData.end(); ++itData) {
785 itData->getLocalData(aValue, aWeight, indLocal, derLocal);
786 for (
unsigned int j = 0; j < indLocal->size(); ++j) {
787 theVector((*indLocal)[j] - 1) += (*derLocal)[j] * aWeight * aValue;
805 unsigned int nData = 0;
806 std::vector<TMatrixD> innerTransDer;
807 std::vector<std::vector<unsigned int> > innerTransLab;
815 std::vector<unsigned int> firstLabels(5);
820 matLocalToFit = matFitToLocal.Inverse(ierr);
821 TMatrixD localToFit(5, 5);
822 for (
unsigned int i = 0; i < 5; ++i) {
823 for (
unsigned int j = 0; j < 5; ++j) {
824 localToFit(i, j) = matLocalToFit(i, j);
829 innerTransLab.push_back(firstLabels);
835 std::vector<GblPoint>::iterator itPoint;
838 itPoint <
thePoints[iTraj].end(); ++itPoint) {
840 unsigned int nLabel = itPoint->getLabel();
841 unsigned int measDim = itPoint->hasMeasurement();
843 const TMatrixD localDer = itPoint->getLocalDerivatives();
844 const std::vector<int> globalLab = itPoint->getGlobalLabels();
845 const TMatrixD globalDer = itPoint->getGlobalDerivatives();
847 itPoint->getMeasurement(matP, aMeas, aPrec);
848 unsigned int iOff = 5 - measDim;
849 std::vector<unsigned int> labDer(5);
851 unsigned int nJacobian =
852 (itPoint <
thePoints[iTraj].end() - 1) ? 1 : 0;
856 matPDer = matP * matDer;
865 TMatrixD proDer(measDim, 5);
867 unsigned int ifirst = 0;
868 unsigned int ilabel = 0;
870 if (labDer[ilabel] > 0) {
871 while (innerTransLab[iTraj][ifirst]
872 != labDer[ilabel] and ifirst < 5) {
876 labDer[ilabel] -= 2 * nDim * (iTraj + 1);
880 for (
unsigned int k = iOff;
k < 5; ++
k) {
881 proDer(
k - iOff, ifirst) = matPDer(
k,
889 transDer = proDer * innerTransDer[iTraj];
891 for (
unsigned int i = iOff; i < 5; ++i) {
893 GblData aData(nLabel, aMeas(i), aPrec(i));
895 globalLab, globalDer,
numLocals, transDer);
912 for (itPoint =
thePoints[iTraj].begin() + 1;
913 itPoint <
thePoints[iTraj].end() - 1; ++itPoint) {
915 unsigned int nLabel = itPoint->getLabel();
916 if (itPoint->hasScatterer()) {
917 itPoint->getScatterer(matT, aMeas, aPrec);
919 std::vector<unsigned int> labDer(7);
922 matTDer = matT * matDer;
925 TMatrixD proDer(nDim, 5);
927 unsigned int ifirst = 0;
928 unsigned int ilabel = 0;
930 if (labDer[ilabel] > 0) {
931 while (innerTransLab[iTraj][ifirst]
932 != labDer[ilabel] and ifirst < 5) {
936 labDer[ilabel] -= 2 * nDim * (iTraj + 1);
940 for (
unsigned int k = 0;
k < nDim; ++
k) {
941 proDer(
k, ifirst) = matTDer(
k, ilabel);
948 transDer = proDer * innerTransDer[iTraj];
950 for (
unsigned int i = 0; i < nDim; ++i) {
952 if (aPrec(iDim) > 0.) {
953 GblData aData(nLabel, aMeas(iDim), aPrec(iDim));
968 std::pair<std::vector<unsigned int>, TMatrixD> indexAndJacobian =
970 std::vector<unsigned int> externalSeedIndex = indexAndJacobian.first;
971 std::vector<double> externalSeedDerivatives(externalSeedIndex.size());
973 const TVectorD valEigen = externalSeedEigen.GetEigenValues();
974 TMatrixD vecEigen = externalSeedEigen.GetEigenVectors();
975 vecEigen = vecEigen.T() * indexAndJacobian.second;
977 if (valEigen(i) > 0.) {
979 externalSeedDerivatives[j] = vecEigen(i, j);
983 externalSeedDerivatives);
995 for (
unsigned int iExt = 0; iExt < nExt; ++iExt) {
996 for (
unsigned int iCol = 0; iCol <
numCurvature; ++iCol) {
1012 std::vector<GblData>::iterator itData;
1013 for (itData =
theData.begin(); itData <
theData.end(); ++itData) {
1024 std::vector<GblData>::iterator itData;
1025 for (itData =
theData.begin(); itData <
theData.end(); ++itData) {
1026 aLoss += (1. - itData->setDownWeighting(aMethod));
1044 std::string optionList) {
1045 const double normChi2[4] = { 1.0, 0.8737, 0.9326, 0.8228 };
1046 const std::string methodList =
"TtHhCc";
1054 unsigned int aMethod = 0;
1058 unsigned int ierr = 0;
1064 for (
unsigned int i = 0; i < optionList.size(); ++i)
1066 size_t aPosition = methodList.find(optionList[i]);
1067 if (aPosition != std::string::npos) {
1068 aMethod = aPosition / 2 + 1;
1077 for (
unsigned int i = 0; i <
theData.size(); ++i) {
1080 Chi2 /= normChi2[aMethod];
1084 std::cout <<
" GblTrajectory::fit exception " << e << std::endl;
1094 std::vector<unsigned int>* indLocal;
1095 std::vector<double>* derLocal;
1096 std::vector<int>* labGlobal;
1097 std::vector<double>* derGlobal;
1103 std::vector<GblData>::iterator itData;
1104 for (itData =
theData.begin(); itData !=
theData.end(); ++itData) {
1105 itData->getAllData(aValue, aErr, indLocal, derLocal, labGlobal,
1107 aMille.
addData(aValue, aErr, *indLocal, *derLocal, *labGlobal,
1120 <<
" subtrajectories" << std::endl;
1122 std::cout <<
"Simple GblTrajectory" << std::endl;
1125 std::cout <<
" 2D-trajectory" << std::endl;
1129 std::cout <<
" Number of points with offsets: " <<
numOffsets << std::endl;
1130 std::cout <<
" Number of fit parameters : " <<
numParameters
1135 std::cout <<
" Number of ext. measurements : "
1139 std::cout <<
" Label of point with ext. seed: " <<
externalPoint
1143 std::cout <<
" Constructed OK " << std::endl;
1146 std::cout <<
" Fitted OK " << std::endl;
1150 std::cout <<
" Inner transformations" << std::endl;
1156 std::cout <<
" External measurements" << std::endl;
1157 std::cout <<
" Measurements:" << std::endl;
1159 std::cout <<
" Precisions:" << std::endl;
1161 std::cout <<
" Derivatives:" << std::endl;
1165 std::cout <<
" External seed:" << std::endl;
1169 std::cout <<
" Fit results" << std::endl;
1170 std::cout <<
" Parameters:" << std::endl;
1172 std::cout <<
" Covariance matrix (bordered band part):"
1184 std::cout <<
"GblPoints " << std::endl;
1186 std::vector<GblPoint>::iterator itPoint;
1187 for (itPoint =
thePoints[iTraj].begin();
1188 itPoint <
thePoints[iTraj].end(); ++itPoint) {
1189 itPoint->printPoint(level);
1196 std::cout <<
"GblData blocks " << std::endl;
1197 std::vector<GblData>::iterator itData;
1198 for (itData =
theData.begin(); itData <
theData.end(); ++itData) {
1199 itData->printData();