24 #include "TrackPoint.h"
25 #include "Exception.h"
28 #include "KalmanFitterRefTrack.h"
29 #include "KalmanFitterInfo.h"
30 #include "KalmanFitStatus.h"
32 #include <TDecompChol.h>
33 #include <Math/ProbFunc.h>
47 unsigned int dim = rep->
getDim();
56 debugOut << tr->getNumPoints() <<
" TrackPoints with measurements in this track." << std::endl;
59 bool alreadyFitted(!refitAll_);
62 C_.ResizeTo(dim, dim);
64 for (
size_t i = 0; i < tr->getNumPointsWithMeasurement(); ++i) {
66 assert(direction == +1 || direction == -1);
68 tp = tr->getPointWithMeasurement(i);
69 else if (direction == -1)
70 tp = tr->getPointWithMeasurement(-i-1);
72 if (! tp->hasFitterInfo(rep)) {
74 debugOut <<
"TrackPoint " << i <<
" has no fitterInfo -> continue. \n";
81 if (alreadyFitted && fi->hasUpdate(direction)) {
83 debugOut <<
"TrackPoint " << i <<
" is already fitted -> continue. \n";
86 chi2 += fi->getUpdate(direction)->getChiSquareIncrement();
87 ndf += fi->getUpdate(direction)->getNdf();
91 alreadyFitted =
false;
94 debugOut <<
" process TrackPoint nr. " << i <<
"\n";
96 processTrackPoint(fi, prevFi, tp, chi2, ndf, direction);
109 Exception exc(
"KalmanFitterRefTrack::processTrack: Cannot process pruned track!", __LINE__,__FILE__);
113 double oldChi2FW = 1e6;
114 double oldPvalFW = 0.;
115 double oldChi2BW = 1e6;
116 double oldPvalBW = 0.;
117 double chi2FW(0), ndfFW(0);
118 double chi2BW(0), ndfBW(0);
122 tr->setFitStatus(status, rep);
124 status->setIsFittedWithReferenceTrack(
true);
131 debugOut <<
" KalmanFitterRefTrack::processTrack with rep " << rep
132 <<
" (id == " << tr->
getIdForRep(rep) <<
")"<<
", iteration nr. " << nIt <<
"\n";
136 if (!
prepareTrack(tr, rep, resortHits, nFailedHits) && !refitAll_) {
138 debugOut <<
"KalmanFitterRefTrack::processTrack. Track preparation did not change anything!\n";
141 status->setIsFitted();
143 status->setIsFitConvergedPartially();
144 if (nFailedHits == 0)
145 status->setIsFitConvergedFully();
147 status->setIsFitConvergedFully(
false);
149 status->setNFailedPoints(nFailedHits);
151 status->setHasTrackChanged(
false);
153 status->setNumIterations(nIt);
154 status->setForwardChi2(chi2FW);
155 status->setBackwardChi2(chi2BW);
156 status->setForwardNdf(std::max(0., ndfFW));
157 status->setBackwardNdf(std::max(0., ndfBW));
165 debugOut <<
"KalmanFitterRefTrack::processTrack. Prepared Track:";
174 debugOut <<
"KalmanFitterRefTrack::processTrack. Resorted Track:";
178 status->setNFailedPoints(nFailedHits);
180 debugOut <<
"KalmanFitterRefTrack::processTrack. Prepared resorted Track:";
189 debugOut <<
"KalmanFitterRefTrack::forward fit\n";
194 debugOut <<
"KalmanFitterRefTrack::backward fit\n";
198 if (lastProcessedPoint !=
nullptr) {
200 if (! lastInfo->hasBackwardPrediction()) {
204 debugOut <<
"blow up cov for backward fit at TrackPoint " << lastProcessedPoint <<
"\n";
209 fitTrack(tr, rep, chi2BW, ndfBW, -1);
214 double PvalBW = std::max(0.,ROOT::Math::chisquared_cdf_c(chi2BW, ndfBW));
215 double PvalFW = (debugLvl_ > 0) ? std::max(0.,ROOT::Math::chisquared_cdf_c(chi2FW, ndfFW)) : 0;
218 debugOut <<
"KalmanFitterRefTrack::Track after fit:"; tr->Print(
"C");
220 debugOut <<
"old chi2s: " << oldChi2BW <<
", " << oldChi2FW
221 <<
" new chi2s: " << chi2BW <<
", " << chi2FW << std::endl;
222 debugOut <<
"old pVals: " << oldPvalBW <<
", " << oldPvalFW
223 <<
" new pVals: " << PvalBW <<
", " << PvalFW << std::endl;
231 bool converged(
false);
232 bool finished(
false);
255 if (nFailedHits == 0)
256 status->setIsFitConvergedFully(converged);
258 status->setIsFitConvergedFully(
false);
260 status->setIsFitConvergedPartially(converged);
261 status->setNFailedPoints(nFailedHits);
276 debugOut <<
"KalmanFitterRefTrack::number of max iterations reached!\n";
283 status->setIsFitted(
false);
284 status->setIsFitConvergedFully(
false);
285 status->setIsFitConvergedPartially(
false);
286 status->setNFailedPoints(nFailedHits);
296 TrackPoint* tp = tr->getPointWithMeasurementAndFitterInfo(0, rep);
303 status->setCharge(charge);
306 status->setIsFitted();
309 status->setIsFitted(
false);
310 status->setIsFitConvergedFully(
false);
311 status->setIsFitConvergedPartially(
false);
312 status->setNFailedPoints(nFailedHits);
315 status->setHasTrackChanged(
false);
316 status->setNumIterations(nIt);
317 status->setForwardChi2(chi2FW);
318 status->setBackwardChi2(chi2BW);
319 status->setForwardNdf(ndfFW);
320 status->setBackwardNdf(ndfBW);
331 debugOut <<
"KalmanFitterRefTrack::prepareTrack \n";
334 int notChangedUntil, notChangedFrom;
337 bool changedSmthg =
removeOutdated(tr, rep, notChangedUntil, notChangedFrom);
341 FTransportMatrix_.ResizeTo(rep->
getDim(), rep->
getDim());
342 FTransportMatrix_.UnitMatrix();
343 BTransportMatrix_.ResizeTo(rep->
getDim(), rep->
getDim());
348 forwardDeltaState_.ResizeTo(rep->
getDim());
349 backwardDeltaState_.ResizeTo(rep->
getDim());
353 std::unique_ptr<MeasuredStateOnPlane> firstBackwardUpdate;
363 bool newRefState(
false);
364 bool prevNewRefState(
false);
366 unsigned int nPoints = tr->getNumPoints();
374 for (; i<nPoints; ++i) {
379 debugOut <<
"Prepare TrackPoint " << i <<
"\n";
385 if (!trackPoint->hasRawMeasurements()) {
387 debugOut <<
"TrackPoint has no rawMeasurements -> continue \n";
397 if (trackPoint->hasFitterInfo(rep))
401 if (fitterInfo ==
nullptr) {
403 debugOut <<
"create new KalmanFitterInfo \n";
411 debugOut <<
"TrackPoint " << i <<
" (" << trackPoint <<
") already has KalmanFitterInfo \n";
414 if (prevFitterInfo ==
nullptr) {
415 if (fitterInfo->hasBackwardUpdate())
421 if (fitterInfo->hasPredictionsAndUpdates()) {
424 debugOut <<
"got smoothed state \n";
429 smoothedState =
nullptr;
433 if (fitterInfo->hasReferenceState()) {
435 referenceState = fitterInfo->getReferenceState();
438 if (!prevNewRefState) {
440 debugOut <<
"TrackPoint already has referenceState and previous referenceState has not been altered -> continue \n";
442 trackLen += referenceState->getForwardSegmentLength();
443 if (setSortingParams)
444 trackPoint->setSortingParameter(trackLen);
446 prevNewRefState = newRefState;
447 prevReferenceState = referenceState;
448 prevFitterInfo = fitterInfo;
449 prevSmoothedState = smoothedState;
455 if (prevReferenceState ==
nullptr) {
457 debugOut <<
"TrackPoint already has referenceState but previous referenceState is nullptr -> reset forward info of current reference state and continue \n";
460 referenceState->resetForward();
462 if (setSortingParams)
463 trackPoint->setSortingParameter(trackLen);
465 prevNewRefState = newRefState;
466 prevReferenceState = referenceState;
467 prevFitterInfo = fitterInfo;
468 prevSmoothedState = smoothedState;
475 debugOut <<
"TrackPoint already has referenceState but previous referenceState has been altered -> update transport matrices and continue \n";
480 prevReferenceState->resetBackward();
481 referenceState->resetForward();
483 double segmentLen = rep->
extrapolateToPlane(stateToExtrapolate, fitterInfo->getReferenceState()->getPlane(),
false,
true);
485 debugOut <<
"extrapolated stateToExtrapolate (prevReferenceState) by " << segmentLen <<
" cm.\n";
487 trackLen += segmentLen;
489 if (segmentLen == 0) {
490 FTransportMatrix_.UnitMatrix();
491 FNoiseMatrix_.Zero();
492 forwardDeltaState_.Zero();
493 BTransportMatrix_.UnitMatrix();
494 BNoiseMatrix_.Zero();
495 backwardDeltaState_.Zero();
502 prevReferenceState->setBackwardSegmentLength(-segmentLen);
503 prevReferenceState->setBackwardTransportMatrix(BTransportMatrix_);
504 prevReferenceState->setBackwardNoiseMatrix(BNoiseMatrix_);
505 prevReferenceState->setBackwardDeltaState(backwardDeltaState_);
507 referenceState->setForwardSegmentLength(segmentLen);
508 referenceState->setForwardTransportMatrix(FTransportMatrix_);
509 referenceState->setForwardNoiseMatrix(FNoiseMatrix_);
510 referenceState->setForwardDeltaState(forwardDeltaState_);
514 if (setSortingParams)
515 trackPoint->setSortingParameter(trackLen);
517 prevNewRefState = newRefState;
518 prevReferenceState = referenceState;
519 prevFitterInfo = fitterInfo;
520 prevSmoothedState = smoothedState;
528 if (smoothedState !=
nullptr) {
530 debugOut <<
"construct plane with smoothedState \n";
531 plane = trackPoint->getRawMeasurement(0)->
constructPlane(*smoothedState);
533 else if (prevSmoothedState !=
nullptr) {
535 debugOut <<
"construct plane with prevSmoothedState \n";
538 plane = trackPoint->getRawMeasurement(0)->
constructPlane(*prevSmoothedState);
540 else if (prevReferenceState !=
nullptr) {
542 debugOut <<
"construct plane with prevReferenceState \n";
544 plane = trackPoint->getRawMeasurement(0)->
constructPlane(*prevReferenceState);
551 debugOut <<
"construct plane with smoothed state of cardinal rep fit \n";
559 plane = trackPoint->getRawMeasurement(0)->
constructPlane(cardinalState);
563 debugOut <<
"construct plane with state from track \n";
566 rep->
setPosMom(seedFromTrack, tr->getStateSeed());
567 plane = trackPoint->getRawMeasurement(0)->
constructPlane(seedFromTrack);
570 if (plane.get() ==
nullptr) {
571 Exception exc(
"KalmanFitterRefTrack::prepareTrack ==> construced plane is nullptr!",__LINE__,__FILE__);
579 std::unique_ptr<StateOnPlane> stateToExtrapolate(
nullptr);
580 if (prevFitterInfo ==
nullptr) {
582 debugOut <<
"prevFitterInfo == nullptr \n";
584 if (smoothedState !=
nullptr) {
586 debugOut <<
"extrapolate smoothedState to plane\n";
588 stateToExtrapolate.reset(
new StateOnPlane(*smoothedState));
590 else if (referenceState !=
nullptr) {
592 debugOut <<
"extrapolate referenceState to plane\n";
594 stateToExtrapolate.reset(
new StateOnPlane(*referenceState));
601 debugOut <<
"extrapolate smoothed state of cardinal rep fit to plane\n";
605 stateToExtrapolate.reset(
new StateOnPlane(fittedState));
606 stateToExtrapolate->setRep(rep);
610 debugOut <<
"extrapolate seed from track to plane\n";
613 rep->
setPosMom(*stateToExtrapolate, tr->getStateSeed());
614 rep->
setTime(*stateToExtrapolate, tr->getTimeSeed());
618 if (prevSmoothedState !=
nullptr) {
620 debugOut <<
"extrapolate prevSmoothedState to plane \n";
622 stateToExtrapolate.reset(
new StateOnPlane(*prevSmoothedState));
625 assert (prevReferenceState !=
nullptr);
627 debugOut <<
"extrapolate prevReferenceState to plane \n";
629 stateToExtrapolate.reset(
new StateOnPlane(*prevReferenceState));
634 if (prevReferenceState !=
nullptr)
635 prevReferenceState->resetBackward();
636 fitterInfo->deleteReferenceInfo();
638 if (prevFitterInfo !=
nullptr) {
641 debugOut <<
"extrapolated stateToExtrapolate to plane of prevFitterInfo (plane could have changed!) \n";
646 trackLen += segmentLen;
648 debugOut <<
"extrapolated stateToExtrapolate by " << segmentLen <<
" cm.\t";
649 debugOut <<
"charge of stateToExtrapolate: " << rep->
getCharge(*stateToExtrapolate) <<
" \n";
653 if (segmentLen == 0) {
654 FTransportMatrix_.UnitMatrix();
655 FNoiseMatrix_.Zero();
656 forwardDeltaState_.Zero();
657 BTransportMatrix_.UnitMatrix();
658 BNoiseMatrix_.Zero();
659 backwardDeltaState_.Zero();
673 if (setSortingParams)
674 trackPoint->setSortingParameter(trackLen);
678 if (prevReferenceState !=
nullptr) {
679 prevReferenceState->setBackwardSegmentLength(-segmentLen);
680 prevReferenceState->setBackwardTransportMatrix(BTransportMatrix_);
681 prevReferenceState->setBackwardNoiseMatrix(BNoiseMatrix_);
682 prevReferenceState->setBackwardDeltaState(backwardDeltaState_);
690 stateToExtrapolate->getPlane(),
691 stateToExtrapolate->getRep(),
692 stateToExtrapolate->getAuxInfo());
693 referenceState->setForwardSegmentLength(segmentLen);
694 referenceState->setForwardTransportMatrix(FTransportMatrix_);
695 referenceState->setForwardNoiseMatrix(FNoiseMatrix_);
696 referenceState->setForwardDeltaState(forwardDeltaState_);
698 referenceState->resetBackward();
700 fitterInfo->setReferenceState(referenceState);
704 std::vector<double> oldWeights = fitterInfo->
getWeights();
706 fitterInfo->deleteMeasurementInfo();
707 const std::vector<AbsMeasurement*>& rawMeasurements = trackPoint->getRawMeasurements();
708 for ( std::vector< genfit::AbsMeasurement* >::const_iterator measurement = rawMeasurements.begin(), lastMeasurement = rawMeasurements.end(); measurement != lastMeasurement; ++measurement) {
709 assert((*measurement) !=
nullptr);
710 fitterInfo->addMeasurementsOnPlane((*measurement)->constructMeasurementsOnPlane(*referenceState));
712 if (oldWeights.size() == fitterInfo->getNumMeasurements()) {
714 fitterInfo->fixWeights(oldWeightsFixed);
719 prevNewRefState = newRefState;
720 prevReferenceState = referenceState;
721 prevFitterInfo = fitterInfo;
722 prevSmoothedState = smoothedState;
728 errorOut <<
"exception at hit " << i <<
"\n";
735 prevNewRefState =
true;
736 referenceState =
nullptr;
737 smoothedState =
nullptr;
738 tr->getPoint(i)->deleteFitterInfo(rep);
740 if (setSortingParams)
741 tr->getPoint(i)->setSortingParameter(trackLen);
744 debugOut <<
"There was an exception, try to continue with next TrackPoint " << i+1 <<
" \n";
755 for (; i<nPoints; ++i) {
758 if (setSortingParams)
759 trackPoint->setSortingParameter(trackLen);
761 trackPoint->deleteFitterInfo(rep);
774 if (firstBackwardUpdate && tr->getPointWithMeasurementAndFitterInfo(0, rep)) {
776 if (fi && ! fi->hasForwardPrediction()) {
778 debugOut <<
"set backwards update of first point as forward prediction (with blown up cov) \n";
780 if (fi->getPlane() != firstBackwardUpdate->getPlane()) {
789 if (fitStatus !=
nullptr)
790 fitStatus->setTrackLen(trackLen);
793 debugOut <<
"trackLen of reference track = " << trackLen <<
"\n";
798 assert(isTrackPrepared(tr, rep));
808 debugOut <<
"KalmanFitterRefTrack::removeOutdated \n";
811 bool changedSmthg(
false);
813 unsigned int nPoints = tr->getNumPoints();
814 notChangedUntil = nPoints-1;
818 for (
unsigned int i=0; i<nPoints; ++i) {
823 if (!trackPoint->hasRawMeasurements()) {
825 debugOut <<
"TrackPoint has no rawMeasurements -> continue \n";
832 if (trackPoint->hasFitterInfo(rep))
835 if (fitterInfo ==
nullptr)
840 if (fitterInfo->hasReferenceState()) {
842 if (! fitterInfo->hasPredictionsAndUpdates()) {
844 debugOut <<
"reference state but not all predictions & updates -> do not touch reference state. \n";
851 resM_.ResizeTo(smoothedState.getState());
852 resM_ = smoothedState.getState();
853 resM_ -= fitterInfo->getReferenceState()->getState();
857 double* resArray = resM_.GetMatrixArray();
858 for (
int j=0; j<smoothedState.getCov().GetNcols(); ++j)
859 chi2 += resArray[j]*resArray[j] / smoothedState.getCov()(j,j);
861 if (chi2 < deltaChi2Ref_) {
864 debugOut <<
"reference state is near smoothed state -> do not update reference state, chi2 = " << chi2 <<
"\n";
869 debugOut <<
"reference state is not close to smoothed state, chi2 = " << chi2 <<
"\n";
875 debugOut <<
"remove reference info \n";
878 fitterInfo->deleteReferenceInfo();
882 if (notChangedUntil == (
int)nPoints-1)
883 notChangedUntil = i-1;
885 notChangedFrom = i+1;
901 unsigned int nPoints = tr->getNumPoints();
904 tr->deleteForwardInfo(0, -1, rep);
905 tr->deleteBackwardInfo(0, -1, rep);
910 if (notChangedUntil != (
int)nPoints-1) {
911 tr->deleteForwardInfo(notChangedUntil+1, -1, rep);
913 if (notChangedFrom != 0)
914 tr->deleteBackwardInfo(0, notChangedFrom-1, rep);
922 if(squareRootFormalism_) {
923 processTrackPointSqrt(fi, prevFi, tp, chi2, ndf, direction);
928 debugOut <<
" KalmanFitterRefTrack::processTrackPoint " << fi->getTrackPoint() <<
"\n";
931 unsigned int dim = fi->getRep()->getDim();
937 if (prevFi !=
nullptr) {
938 const TMatrixD& F = fi->getReferenceState()->getTransportMatrix(direction);
939 assert(F.GetNcols() == (
int)dim);
940 const TMatrixDSym& N = fi->getReferenceState()->getNoiseMatrix(direction);
942 p_ = prevFi->getUpdate(direction)->getState();
944 p_ += fi->getReferenceState()->getDeltaState(direction);
946 C_ = prevFi->getUpdate(direction)->getCov();
949 fi->setPrediction(
new MeasuredStateOnPlane(p_, C_, fi->getReferenceState()->getPlane(), fi->getReferenceState()->getRep(), fi->getReferenceState()->getAuxInfo()), direction);
952 debugOut <<
"F (Transport Matrix) "; F.Print();
953 debugOut <<
"p_{k,r} (reference state) "; fi->getReferenceState()->getState().Print();
954 debugOut <<
"c (delta state) "; fi->getReferenceState()->getDeltaState(direction).Print();
955 debugOut <<
"F*p_{k-1,r} + c "; (F *prevFi->getReferenceState()->getState() + fi->getReferenceState()->getDeltaState(direction)).Print();
959 if (fi->hasPrediction(direction)) {
961 debugOut <<
" Use prediction as start \n";
963 p_ = fi->getPrediction(direction)->getState();
964 C_ = fi->getPrediction(direction)->getCov();
968 debugOut <<
" Use reference state and seed cov as start \n";
970 const AbsTrackRep *rep = fi->getReferenceState()->getRep();
971 p_ = fi->getReferenceState()->getState();
974 TMatrixDSym dummy(p_.GetNrows());
975 MeasuredStateOnPlane mop(p_, dummy, fi->getReferenceState()->getPlane(), rep, fi->getReferenceState()->getAuxInfo());
978 rep->
setPosMomCov(mop, pos, mom, fi->getTrackPoint()->getTrack()->getCovSeed());
986 debugOut <<
"p_{k,r} (reference state)"; fi->getReferenceState()->getState().Print();
991 debugOut <<
" p_{k|k-1} (state prediction)"; p_.Print();
992 debugOut <<
" C_{k|k-1} (covariance prediction)"; C_.Print();
999 const std::vector<MeasurementOnPlane *> measurements =
getMeasurements(fi, tp, direction);
1000 for (std::vector<MeasurementOnPlane *>::const_iterator it = measurements.begin(); it != measurements.end(); ++it) {
1004 if (debugLvl_ > 1) {
1005 debugOut <<
"Weight of measurement is almost 0, continue ... /n";
1013 1./m.getWeight() * m.getCov() :
1016 covSumInv_.ResizeTo(C_);
1018 H->HMHt(covSumInv_);
1021 tools::invertMatrix(covSumInv_);
1023 const TMatrixD& CHt(H->MHt(C_));
1025 res_.ResizeTo(m.getState());
1026 res_ = m.getState();
1028 if (debugLvl_ > 1) {
1030 debugOut <<
"m (measurement) "; m.getState().Print();
1031 debugOut <<
"V ((weighted) measurement covariance) "; (1./m.getWeight() * m.getCov()).Print();
1032 debugOut <<
"residual "; res_.Print();
1035 p_ += TMatrixD(CHt, TMatrixD::kMult, covSumInv_) * res_;
1036 if (debugLvl_ > 1) {
1038 debugOut <<
" update"; (TMatrixD(CHt, TMatrixD::kMult, covSumInv_) * res_).Print();
1042 covSumInv_.Similarity(CHt);
1045 if (debugLvl_ > 1) {
1048 debugOut <<
" p_{k|k} (updated state)"; p_.Print();
1049 debugOut <<
" C_{k|k} (updated covariance)"; C_.Print();
1055 res_ = m.getState();
1057 if (debugLvl_ > 1) {
1071 bool couldInvert(
true);
1073 tools::invertMatrix(Rinv_);
1076 if (debugLvl_ > 1) {
1079 couldInvert =
false;
1083 if (debugLvl_ > 1) {
1087 chi2inc += Rinv_.Similarity(res_);
1093 ndfInc += m.getWeight() * m.getState().GetNrows();
1096 ndfInc += m.getState().GetNrows();
1106 upState->setAuxInfo(fi->getReferenceState()->getAuxInfo());
1107 fi->setUpdate(upState, direction);
1110 if (debugLvl_ > 0) {
1111 debugOut <<
" chi² inc " << chi2inc <<
"\t";
1112 debugOut <<
" ndf inc " << ndfInc <<
"\t";
1113 debugOut <<
" charge of update " << fi->getRep()->getCharge(*upState) <<
"\n";
1117 if (not fi->checkConsistency()) {
1128 const TrackPoint* tp,
double& chi2,
double& ndf,
int direction)
1130 if (debugLvl_ > 0) {
1131 debugOut <<
" KalmanFitterRefTrack::processTrackPointSqrt " << fi->getTrackPoint() <<
"\n";
1134 unsigned int dim = fi->getRep()->getDim();
1139 TMatrixD S(dim, dim);
1142 if (prevFi !=
nullptr) {
1143 const TMatrixD& F = fi->getReferenceState()->getTransportMatrix(direction);
1144 assert(F.GetNcols() == (
int)dim);
1145 const TMatrixDSym& N = fi->getReferenceState()->getNoiseMatrix(direction);
1149 p_ = prevFi->getUpdate(direction)->getState();
1151 p_ += fi->getReferenceState()->getDeltaState(direction);
1154 TDecompChol decompS(prevFi->getUpdate(direction)->getCov());
1155 decompS.Decompose();
1157 tools::noiseMatrixSqrt(N, Q);
1158 tools::kalmanPredictionCovSqrt(decompS.GetU(), F, Q, S);
1160 fi->setPrediction(
new MeasuredStateOnPlane(p_, TMatrixDSym(TMatrixDSym::kAtA, S), fi->getReferenceState()->getPlane(), fi->getReferenceState()->getRep(), fi->getReferenceState()->getAuxInfo()), direction);
1161 if (debugLvl_ > 1) {
1163 debugOut <<
"F (Transport Matrix) "; F.Print();
1164 debugOut <<
"p_{k,r} (reference state) "; fi->getReferenceState()->getState().Print();
1165 debugOut <<
"c (delta state) "; fi->getReferenceState()->getDeltaState(direction).Print();
1166 debugOut <<
"F*p_{k-1,r} + c "; (F *prevFi->getReferenceState()->getState() + fi->getReferenceState()->getDeltaState(direction)).Print();
1170 if (fi->hasPrediction(direction)) {
1171 if (debugLvl_ > 0) {
1172 debugOut <<
" Use prediction as start \n";
1174 p_ = fi->getPrediction(direction)->getState();
1175 TDecompChol decompS(fi->getPrediction(direction)->getCov());
1176 decompS.Decompose();
1180 if (debugLvl_ > 0) {
1181 debugOut <<
" Use reference state and seed cov as start \n";
1183 const AbsTrackRep *rep = fi->getReferenceState()->getRep();
1184 p_ = fi->getReferenceState()->getState();
1187 TMatrixDSym dummy(p_.GetNrows());
1188 MeasuredStateOnPlane mop(p_, dummy, fi->getReferenceState()->getPlane(), rep, fi->getReferenceState()->getAuxInfo());
1191 rep->
setPosMomCov(mop, pos, mom, fi->getTrackPoint()->getTrack()->getCovSeed());
1195 TDecompChol decompS(mop.getCov());
1196 decompS.Decompose();
1199 if (debugLvl_ > 1) {
1201 debugOut <<
"p_{k,r} (reference state)"; fi->getReferenceState()->getState().Print();
1205 if (debugLvl_ > 1) {
1206 debugOut <<
" p_{k|k-1} (state prediction)"; p_.Print();
1207 debugOut <<
" C_{k|k-1} (covariance prediction)"; C_.Print();
1215 const std::vector<MeasurementOnPlane *> measurements =
getMeasurements(fi, tp, direction);
1216 for (std::vector<MeasurementOnPlane *>::const_iterator it = measurements.begin(); it != measurements.end(); ++it) {
1220 if (debugLvl_ > 1) {
1221 debugOut <<
"Weight of measurement is almost 0, continue ... /n";
1229 1./m.getWeight() * m.getCov() :
1231 TDecompChol decompR(V);
1232 decompR.Decompose();
1233 const TMatrixD&
R(decompR.GetU());
1235 res_.ResizeTo(m.getState());
1236 res_ = m.getState();
1240 tools::kalmanUpdateSqrt(S, res_,
R, H,
1243 if (debugLvl_ > 1) {
1245 debugOut <<
"m (measurement) "; m.getState().Print();
1246 debugOut <<
"V ((weighted) measurement covariance) "; (1./m.getWeight() * m.getCov()).Print();
1247 debugOut <<
"residual "; res_.Print();
1252 if (debugLvl_ > 1) {
1254 debugOut <<
" update"; update.Print();
1258 if (debugLvl_ > 1) {
1261 debugOut <<
" p_{k|k} (updated state)"; p_.Print();
1262 debugOut <<
" C_{k|k} (updated covariance)"; C_.Print();
1268 res_ -= H->Hv(update);
1269 if (debugLvl_ > 1) {
1278 Rinv_ = V - TMatrixDSym(TMatrixDSym::kAtA, H->MHt(S));
1280 bool couldInvert(
true);
1282 tools::invertMatrix(Rinv_);
1285 if (debugLvl_ > 1) {
1288 couldInvert =
false;
1292 if (debugLvl_ > 1) {
1296 chi2inc += Rinv_.Similarity(res_);
1301 ndfInc += m.getWeight() * m.getState().GetNrows();
1304 ndfInc += m.getState().GetNrows();
1309 C_ = TMatrixDSym(TMatrixDSym::kAtA, S);
1316 upState->setAuxInfo(fi->getReferenceState()->getAuxInfo());
1317 fi->setUpdate(upState, direction);
1320 if (debugLvl_ > 0) {
1321 debugOut <<
" chi² inc " << chi2inc <<
"\t";
1322 debugOut <<
" ndf inc " << ndfInc <<
"\t";
1323 debugOut <<
" charge of update " << fi->getRep()->getCharge(*upState) <<
"\n";
1327 if (not fi->checkConsistency()) {
HMatrix for projecting from AbsTrackRep parameters to measured parameters in a DetPlane.
bool canIgnoreWeights() const
returns if the fitter can ignore the weights and handle the MeasurementOnPlanes as if they had weight...
double deltaPval_
Convergence criterion.
double blowUpFactor_
Blow up the covariance of the forward (backward) fit by this factor before seeding the backward (forw...
int maxFailedHits_
after how many failed hits (exception during construction of plane, extrapolation etc....
double relChi2Change_
@ brief Non-convergence criterion
unsigned int minIterations_
Minimum number of iterations to attempt. Forward and backward are counted as one iteration.
double blowUpMaxVal_
Limit the cov entries to this maxuimum value when blowing up the cov.
unsigned int maxIterations_
Maximum number of iterations to attempt. Forward and backward are counted as one iteration.
bool resetOffDiagonals_
Reset the off-diagonals to 0 when blowing up the cov.
const std::vector< MeasurementOnPlane * > getMeasurements(const KalmanFitterInfo *fi, const TrackPoint *tp, int direction) const
get the measurementsOnPlane taking the multipleMeasurementHandling_ into account
virtual SharedPlanePtr constructPlane(const StateOnPlane &state) const =0
Construct (virtual) detector plane (use state's AbsTrackRep).
Abstract base class for a track representation.
virtual void setTime(StateOnPlane &state, double time) const =0
Set time at which the state was defined.
virtual unsigned int getDim() const =0
Get the dimension of the state vector used by the track representation.
virtual void setPosMomCov(MeasuredStateOnPlane &state, const TVector3 &pos, const TVector3 &mom, const TMatrixDSym &cov6x6) const =0
Set position, momentum and covariance of state.
virtual double getCharge(const StateOnPlane &state) const =0
Get the (fitted) charge of a state.
virtual double getQop(const StateOnPlane &state) const =0
Get charge over momentum.
virtual void setQop(StateOnPlane &state, double qop) const =0
Set charge/momentum.
virtual void setPosMom(StateOnPlane &state, const TVector3 &pos, const TVector3 &mom) const =0
Set position and momentum of state.
virtual void getForwardJacobianAndNoise(TMatrixD &jacobian, TMatrixDSym &noise, TVectorD &deltaState) const =0
Get the jacobian and noise matrix of the last extrapolation.
virtual double extrapolateToPlane(StateOnPlane &state, const genfit::SharedPlanePtr &plane, bool stopAtBoundary=false, bool calcJacobianNoise=false) const =0
Extrapolates the state to plane, and returns the extrapolation length and, via reference,...
virtual void getBackwardJacobianAndNoise(TMatrixD &jacobian, TMatrixDSym &noise, TVectorD &deltaState) const =0
Get the jacobian and noise matrix of the last extrapolation if it would have been done in opposite di...
virtual void getPosMom(const StateOnPlane &state, TVector3 &pos, TVector3 &mom) const =0
Get cartesian position and momentum vector of a state.
Exception class for error handling in GENFIT (provides storage for diagnostic information)
void setFatal(bool b=true)
Set fatal flag.
bool isTrackPruned() const
Has the track been pruned after the fit?
FitStatus for use with AbsKalmanFitter implementations.
#MeasuredStateOnPlane with additional info produced by a Kalman filter or DAF.
Collects information needed and produced by a AbsKalmanFitter implementations and is specific to one ...
void setWeights(const std::vector< double > &)
Set weights of measurements.
const MeasuredStateOnPlane & getFittedState(bool biased=true) const override
Get unbiased or biased (default) smoothed state.
std::vector< double > getWeights() const
Get weights of measurements.
bool areWeightsFixed() const
Are the weights fixed?
TrackPoint * fitTrack(Track *tr, const AbsTrackRep *rep, double &chi2, double &ndf, int direction)
Fit the track.
void processTrackWithRep(Track *tr, const AbsTrackRep *rep, bool resortHits=false) override
Process Track with one AbsTrackRep of the Track.
bool prepareTrack(Track *tr, const AbsTrackRep *rep, bool setSortingParams, int &nFailedHits)
Prepare the track.
void removeForwardBackwardInfo(Track *tr, const AbsTrackRep *rep, int notChangedUntil, int notChangedFrom) const
If refitAll_, remove all information.
bool removeOutdated(Track *tr, const AbsTrackRep *rep, int ¬ChangedUntil, int ¬ChangedFrom)
Remove referenceStates if they are too far from smoothed states.
#StateOnPlane with additional covariance matrix.
void blowUpCov(double blowUpFac, bool resetOffDiagonals=true, double maxVal=-1.)
Blow up covariance matrix with blowUpFac. Per default, off diagonals are reset to 0 and the maximum v...
Measured coordinates on a plane.
#StateOnPlane with linearized transport to that #ReferenceStateOnPlane from previous and next #Refere...
A state with arbitrary dimension defined in a DetPlane.
Object containing AbsMeasurement and AbsFitterInfo objects.
AbsFitterInfo * getFitterInfo(const AbsTrackRep *rep=nullptr) const
Get fitterInfo for rep. Per default, use cardinal rep.
void setFitterInfo(genfit::AbsFitterInfo *fitterInfo)
Takes Ownership.
Collection of TrackPoint objects, AbsTrackRep objects and FitStatus objects.
bool sort()
Sort TrackPoint and according to their sorting parameters.
bool hasFitStatus(const AbsTrackRep *rep=nullptr) const
Check if track has a FitStatus for given AbsTrackRep. Per default, check for cardinal rep.
FitStatus * getFitStatus(const AbsTrackRep *rep=nullptr) const
Get FitStatus for a AbsTrackRep. Per default, return FitStatus for cardinalRep.
AbsTrackRep * getCardinalRep() const
Get cardinal track representation.
int getIdForRep(const AbsTrackRep *rep) const
This is used when streaming TrackPoints.
Defines for I/O streams used for error and debug printing.
std::ostream debugOut
Default stream for debug output.
std::shared_ptr< genfit::DetPlane > SharedPlanePtr
Shared Pointer to a DetPlane.
std::ostream errorOut
Default stream for error output.