31#include "boost/scoped_ptr.hpp"
33#include <Math/ProbFunc.h>
47 unsigned int dim = rep->
getDim();
56 std::cout << tr->
getNumPoints() <<
" TrackPoints with measurements in this track." << std::endl;
62 C_.ResizeTo(dim, dim);
66 assert(direction == +1 || direction == -1);
69 else if (direction == -1)
72 if (! tp->hasFitterInfo(rep)) {
74 std::cout <<
"TrackPoint " << i <<
" has no fitterInfo -> continue. \n";
81 if (alreadyFitted && fi->
hasUpdate(direction)) {
83 std::cout <<
"TrackPoint " << i <<
" is already fitted -> continue. \n";
91 alreadyFitted =
false;
94 std::cout <<
" process TrackPoint nr. " << i <<
"\n";
110 Exception exc(
"KalmanFitterRefTrack::processTrack: Cannot process pruned track!", __LINE__,__FILE__);
114 double oldChi2FW = 1e6;
115 double oldPvalFW = 0.;
116 double oldChi2BW = 1e6;
117 double oldPvalBW = 0.;
118 double chi2FW(0), ndfFW(0);
119 double chi2BW(0), ndfBW(0);
125 status->setIsFittedWithReferenceTrack(
true);
132 std::cout <<
" KalmanFitterRefTrack::processTrack with rep " << rep
133 <<
" (id == " << tr->
getIdForRep(rep) <<
")"<<
", iteration nr. " << nIt <<
"\n";
139 std::cout <<
"KalmanFitterRefTrack::processTrack. Track preparation did not change anything!\n";
142 status->setIsFitted();
144 status->setIsFitConvergedPartially();
145 if (nFailedHits == 0)
146 status->setIsFitConvergedFully();
148 status->setIsFitConvergedFully(
false);
150 status->setNFailedPoints(nFailedHits);
152 status->setHasTrackChanged(
false);
154 status->setNumIterations(nIt);
155 status->setForwardChi2(chi2FW);
156 status->setBackwardChi2(chi2BW);
157 status->setForwardNdf(std::max(0., ndfFW));
158 status->setBackwardNdf(std::max(0., ndfBW));
166 std::cout <<
"KalmanFitterRefTrack::processTrack. Prepared Track:";
175 std::cout <<
"KalmanFitterRefTrack::processTrack. Resorted Track:";
179 status->setNFailedPoints(nFailedHits);
181 std::cout <<
"KalmanFitterRefTrack::processTrack. Prepared resorted Track:";
190 std::cout <<
"KalmanFitterRefTrack::forward fit\n";
195 std::cout <<
"KalmanFitterRefTrack::backward fit\n";
199 if (lastProcessedPoint != NULL) {
205 std::cout <<
"blow up cov for backward fit at TrackPoint " << lastProcessedPoint <<
"\n";
210 fitTrack(tr, rep, chi2BW, ndfBW, -1);
215 double PvalBW = ROOT::Math::chisquared_cdf_c(chi2BW, ndfBW);
216 double PvalFW = (
debugLvl_ > 0) ? ROOT::Math::chisquared_cdf_c(chi2FW, ndfFW) : 0;
219 std::cout <<
"KalmanFitterRefTrack::Track after fit:"; tr->
Print(
"C");
221 std::cout <<
"old chi2s: " << oldChi2BW <<
", " << oldChi2FW
222 <<
" new chi2s: " << chi2BW <<
", " << chi2FW << std::endl;
223 std::cout <<
"old pVals: " << oldPvalBW <<
", " << oldPvalFW
224 <<
" new pVals: " << PvalBW <<
", " << PvalFW << std::endl;
232 bool converged(
false);
233 bool finished(
false);
250 std::cout <<
"Fit is finished! ";
252 std::cout <<
"Fit is converged! ";
256 if (nFailedHits == 0)
257 status->setIsFitConvergedFully(converged);
259 status->setIsFitConvergedFully(
false);
261 status->setIsFitConvergedPartially(converged);
262 status->setNFailedPoints(nFailedHits);
277 std::cout <<
"KalmanFitterRefTrack::number of max iterations reached!\n";
283 std::cerr << e.
what();
284 status->setIsFitted(
false);
285 status->setIsFitConvergedFully(
false);
286 status->setIsFitConvergedPartially(
false);
287 status->setNFailedPoints(nFailedHits);
302 charge =
static_cast<KalmanFitterInfo*
>(tp->getFitterInfo(rep))->getBackwardUpdate()->getCharge();
304 status->setCharge(charge);
307 status->setIsFitted();
310 status->setIsFitted(
false);
311 status->setIsFitConvergedFully(
false);
312 status->setIsFitConvergedPartially(
false);
313 status->setNFailedPoints(nFailedHits);
316 status->setHasTrackChanged(
false);
317 status->setNumIterations(nIt);
318 status->setForwardChi2(chi2FW);
319 status->setBackwardChi2(chi2BW);
320 status->setForwardNdf(ndfFW);
321 status->setBackwardNdf(ndfBW);
332 std::cout <<
"KalmanFitterRefTrack::prepareTrack \n";
335 int notChangedUntil, notChangedFrom;
338 bool changedSmthg =
removeOutdated(tr, rep, notChangedUntil, notChangedFrom);
354 boost::scoped_ptr<MeasuredStateOnPlane> firstBackwardUpdate;
364 bool newRefState(
false);
365 bool prevNewRefState(
false);
375 for (; i<nPoints; ++i) {
380 std::cout <<
"Prepare TrackPoint " << i <<
"\n";
388 std::cout <<
"TrackPoint has no rawMeasurements -> continue \n";
402 if (fitterInfo == NULL) {
404 std::cout <<
"create new KalmanFitterInfo \n";
412 std::cout <<
"TrackPoint " << i <<
" (" << trackPoint <<
") already has KalmanFitterInfo \n";
415 if (prevFitterInfo == NULL) {
425 std::cout <<
"got smoothed state \n";
430 smoothedState = NULL;
439 if (!prevNewRefState) {
441 std::cout <<
"TrackPoint already has referenceState and previous referenceState has not been altered -> continue \n";
444 if (setSortingParams)
447 prevNewRefState = newRefState;
448 prevReferenceState = referenceState;
449 prevFitterInfo = fitterInfo;
450 prevSmoothedState = smoothedState;
456 if (prevReferenceState == NULL) {
458 std::cout <<
"TrackPoint already has referenceState but previous referenceState is NULL -> reset forward info of current reference state and continue \n";
463 if (setSortingParams)
466 prevNewRefState = newRefState;
467 prevReferenceState = referenceState;
468 prevFitterInfo = fitterInfo;
469 prevSmoothedState = smoothedState;
476 std::cout <<
"TrackPoint already has referenceState but previous referenceState has been altered -> update transport matrices and continue \n";
486 std::cout <<
"extrapolated stateToExtrapolate (prevReferenceState) by " << segmentLen <<
" cm.\n";
488 trackLen += segmentLen;
490 if (segmentLen == 0) {
515 if (setSortingParams)
518 prevNewRefState = newRefState;
519 prevReferenceState = referenceState;
520 prevFitterInfo = fitterInfo;
521 prevSmoothedState = smoothedState;
529 if (smoothedState != NULL) {
531 std::cout <<
"construct plane with smoothedState \n";
534 else if (prevSmoothedState != NULL) {
536 std::cout <<
"construct plane with prevSmoothedState \n";
541 else if (prevReferenceState != NULL) {
543 std::cout <<
"construct plane with prevReferenceState \n";
552 std::cout <<
"construct plane with smoothed state of cardinal rep fit \n";
564 std::cout <<
"construct plane with state from track \n";
571 if (plane.get() == NULL) {
572 Exception exc(
"KalmanFitterRefTrack::prepareTrack ==> construced plane is NULL!",__LINE__,__FILE__);
580 boost::scoped_ptr<StateOnPlane> stateToExtrapolate(NULL);
581 if (prevFitterInfo == NULL) {
583 std::cout <<
"prevFitterInfo == NULL \n";
585 if (smoothedState != NULL) {
587 std::cout <<
"extrapolate smoothedState to plane\n";
589 stateToExtrapolate.reset(
new StateOnPlane(*smoothedState));
591 else if (referenceState != NULL) {
593 std::cout <<
"extrapolate referenceState to plane\n";
595 stateToExtrapolate.reset(
new StateOnPlane(*referenceState));
602 std::cout <<
"extrapolate smoothed state of cardinal rep fit to plane\n";
608 rep->
setPosMom(*stateToExtrapolate, pos, mom);
613 std::cout <<
"extrapolate seed from track to plane\n";
620 if (prevSmoothedState != NULL) {
622 std::cout <<
"extrapolate prevSmoothedState to plane \n";
624 stateToExtrapolate.reset(
new StateOnPlane(*prevSmoothedState));
627 assert (prevReferenceState != NULL);
629 std::cout <<
"extrapolate prevReferenceState to plane \n";
631 stateToExtrapolate.reset(
new StateOnPlane(*prevReferenceState));
636 if (prevReferenceState != NULL)
640 if (prevFitterInfo != NULL) {
643 std::cout <<
"extrapolated stateToExtrapolate to plane of prevFitterInfo (plane could have changed!) \n";
648 trackLen += segmentLen;
650 std::cout <<
"extrapolated stateToExtrapolate by " << segmentLen <<
" cm.\t";
651 std::cout <<
"charge of stateToExtrapolate: " << rep->
getCharge(*stateToExtrapolate) <<
" \n";
655 if (segmentLen == 0) {
675 if (setSortingParams)
680 if (prevReferenceState != NULL) {
692 stateToExtrapolate->getPlane(),
693 stateToExtrapolate->getRep(),
694 stateToExtrapolate->getAuxInfo());
706 std::vector<double> oldWeights = fitterInfo->
getWeights();
709 const std::vector<AbsMeasurement*>& rawMeasurements = trackPoint->
getRawMeasurements();
710 for ( std::vector< genfit::AbsMeasurement* >::const_iterator measurement = rawMeasurements.begin(), lastMeasurement = rawMeasurements.end(); measurement != lastMeasurement; ++measurement) {
711 assert((*measurement) != NULL);
721 prevNewRefState = newRefState;
722 prevReferenceState = referenceState;
723 prevFitterInfo = fitterInfo;
724 prevSmoothedState = smoothedState;
730 std::cout <<
"exception at hit " << i <<
"\n";
731 std::cerr << e.
what();
737 prevNewRefState =
true;
738 referenceState = NULL;
739 smoothedState = NULL;
742 if (setSortingParams)
746 std::cout <<
"There was an exception, try to continue with next TrackPoint " << i+1 <<
" \n";
757 for (; i<nPoints; ++i) {
760 if (setSortingParams)
780 std::cout <<
"set backwards update of first point as forward prediction (with blown up cov) \n";
782 if (fi->
getPlane() != firstBackwardUpdate->getPlane()) {
791 if (fitStatus != NULL)
795 std::cout <<
"trackLen of reference track = " << trackLen <<
"\n";
810 std::cout <<
"KalmanFitterRefTrack::removeOutdated \n";
813 bool changedSmthg(
false);
816 notChangedUntil = nPoints-1;
820 for (
unsigned int i=0; i<nPoints; ++i) {
827 std::cout <<
"TrackPoint has no rawMeasurements -> continue \n";
837 if (fitterInfo == NULL)
846 std::cout <<
"reference state but not all predictions & updates -> do not touch reference state. \n";
859 double* resArray =
resM_.GetMatrixArray();
860 for (
int j=0; j<smoothedState.
getCov().GetNcols(); ++j)
861 chi2 += resArray[j]*resArray[j] / smoothedState.
getCov()(j,j);
866 std::cout <<
"reference state is near smoothed state -> do not update reference state, chi2 = " << chi2 <<
"\n";
871 std::cout <<
"reference state is not close to smoothed state, chi2 = " << chi2 <<
"\n";
877 std::cout <<
"remove reference info \n";
884 if (notChangedUntil == (
int)nPoints-1)
885 notChangedUntil = i-1;
887 notChangedFrom = i+1;
912 if (notChangedUntil != (
int)nPoints-1) {
915 if (notChangedFrom != 0)
925 std::cout <<
" KalmanFitterRefTrack::processTrackPoint " << fi->
getTrackPoint() <<
"\n";
934 if (prevFi != NULL) {
936 assert(F.GetNcols() == (
int)dim);
948 std::cout <<
"\033[31m";
949 std::cout <<
"F (Transport Matrix) "; F.Print();
958 std::cout <<
" Use prediction as start \n";
965 std::cout <<
" Use reference state and seed cov as start \n";
971 TMatrixDSym dummy(
p_.GetNrows());
982 std::cout <<
"\033[31m";
988 std::cout <<
" p_{k|k-1} (state prediction)";
p_.Print();
989 std::cout <<
" C_{k|k-1} (covariance prediction)";
C_.Print();
990 std::cout <<
"\033[0m";
996 const std::vector<MeasurementOnPlane *> measurements =
getMeasurements(fi, tp, direction);
997 for (std::vector<MeasurementOnPlane *>::const_iterator it = measurements.begin(); it != measurements.end(); ++it) {
1002 std::cout <<
"Weight of measurement is almost 0, continue ... /n";
1010 1./
m.getWeight() *
m.getCov() :
1020 const TMatrixD& CHt(H->MHt(
C_));
1022 res_.ResizeTo(
m.getState());
1023 res_ =
m.getState();
1026 std::cout <<
"\033[34m";
1027 std::cout <<
"m (measurement) ";
m.getState().Print();
1028 std::cout <<
"V ((weighted) measurement covariance) "; (1./
m.getWeight() *
m.getCov()).Print();
1029 std::cout <<
"residual ";
res_.Print();
1030 std::cout <<
"\033[0m";
1034 std::cout <<
"\033[32m";
1035 std::cout <<
" update"; (TMatrixD(CHt, TMatrixD::kMult,
covSumInv_) *
res_).Print();
1036 std::cout <<
"\033[0m";
1044 std::cout <<
"\033[32m";
1045 std::cout <<
" p_{k|k} (updated state)";
p_.Print();
1046 std::cout <<
" C_{k|k} (updated covariance)";
C_.Print();
1047 std::cout <<
"\033[0m";
1052 res_ =
m.getState();
1055 std::cout <<
" resNew ";
1068 bool couldInvert(
true);
1074 std::cerr << e.
what();
1076 couldInvert =
false;
1081 std::cout <<
" Rinv ";
1090 ndfInc +=
m.getWeight() *
m.getState().GetNrows();
1093 ndfInc +=
m.getState().GetNrows();
1108 std::cout <<
" chi² inc " << chi2inc <<
"\t";
1109 std::cout <<
" ndf inc " << ndfInc <<
"\t";
1110 std::cout <<
" charge of update " << fi->
getRep()->
getCharge(*upState) <<
"\n";
const AbsTrackRep * getRep() const
virtual bool hasPrediction(int direction) const
const SharedPlanePtr & getPlane() const
const TrackPoint * getTrackPoint() const
HMatrix for projecting from AbsTrackRep parameters to measured parameters in a DetPlane.
double blowUpFactor_
Blow up the covariance of the forward (backward) fit by this factor before seeding the backward (forw...
unsigned int maxIterations_
Maximum number of iterations to attempt. Forward and backward are counted as one iteration.
bool canIgnoreWeights() const
returns if the fitter can ignore the weights and handle the MeasurementOnPlanes as if they had weight...
const std::vector< MeasurementOnPlane * > getMeasurements(const KalmanFitterInfo *fi, const TrackPoint *tp, int direction) const
get the measurementsOnPlane taking the multipleMeasurementHandling_ into account
double deltaPval_
Convergence criterion.
unsigned int minIterations_
Minimum number of iterations to attempt. Forward and backward are counted as one iteration.
bool isTrackPrepared(const Track *tr, const AbsTrackRep *rep) const
virtual SharedPlanePtr constructPlane(const StateOnPlane &state) const =0
Abstract base class for a track representation.
virtual void getPosMom(const StateOnPlane &state, TVector3 &pos, TVector3 &mom) const =0
Get cartesian position and momentum vector of a state.
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 double getQop(const StateOnPlane &state) const =0
Get charge over momentum.
virtual void setPosMom(StateOnPlane &state, const TVector3 &pos, const TVector3 &mom) const =0
Set position and momentum of state.
virtual double getCharge(const StateOnPlane &state) const =0
Get the (fitted) charge of a state. This is not always equal the pdg charge (e.g. if the charge sign ...
virtual void setPosMomCov(MeasuredStateOnPlane &state, const TVector3 &pos, const TVector3 &mom, const TMatrixDSym &cov6x6) const =0
Set position, momentum and covariance of state.
virtual void getForwardJacobianAndNoise(TMatrixD &jacobian, TMatrixDSym &noise, TVectorD &deltaState) const =0
Get the jacobian and noise matrix of the last extrapolation.
virtual void setQop(StateOnPlane &state, double qop) const =0
Set charge/momentum.
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 unsigned int getDim() const =0
Get the dimension of the state vector used by the track representation.
Exception class for error handling in GENFIT (provides storage for diagnostic information)
void setFatal(bool b=true)
Set fatal flag.
virtual const char * what() const
Standard error message handling for exceptions. use like "std::cerr << e.what();".
bool isTrackPruned() const
Has the track been pruned after the fit?
FitStatus for use with AbsKalmanFitter implementations.
void setTrackLen(double trackLen)
MeasuredStateOnPlane with additional info produced by a Kalman filter or DAF.
double getChiSquareIncrement() const
Collects information needed and produced by a AbsKalmanFitter implementations and is specific to one ...
bool hasPredictionsAndUpdates() const
const MeasuredStateOnPlane & getFittedState(bool biased=true) const
Get unbiased or biased (default) smoothed state.
ReferenceStateOnPlane * getReferenceState() const
virtual bool checkConsistency() const
KalmanFittedStateOnPlane * getUpdate(int direction) const
void setWeights(const std::vector< double > &)
Set weights of measurements.
void setBackwardPrediction(MeasuredStateOnPlane *backwardPrediction)
bool hasForwardPrediction() const
void deleteReferenceInfo()
bool hasReferenceState() const
void addMeasurementsOnPlane(const std::vector< genfit::MeasurementOnPlane * > &measurementsOnPlane)
bool hasBackwardPrediction() const
void setReferenceState(ReferenceStateOnPlane *referenceState)
std::vector< double > getWeights() const
Get weights of measurements.
void setPrediction(MeasuredStateOnPlane *prediction, int direction)
unsigned int getNumMeasurements() const
KalmanFittedStateOnPlane * getBackwardUpdate() const
MeasuredStateOnPlane * getBackwardPrediction() const
MeasuredStateOnPlane * getPrediction(int direction) const
bool hasBackwardUpdate() const
void fixWeights(bool arg=true)
bool hasUpdate(int direction) const
bool areWeightsFixed() const
Are the weights fixed?
void setForwardPrediction(MeasuredStateOnPlane *forwardPrediction)
KalmanFittedStateOnPlane * getForwardUpdate() const
void setUpdate(KalmanFittedStateOnPlane *update, int direction)
void deleteMeasurementInfo()
void processTrackPoint(KalmanFitterInfo *fi, const KalmanFitterInfo *prevFi, const TrackPoint *tp, double &chi2, double &ndf, int direction)
void processTrackWithRep(Track *tr, const AbsTrackRep *rep, bool resortHits=false)
TVectorD backwardDeltaState_
TMatrixDSym BNoiseMatrix_
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.
TVectorD forwardDeltaState_
TMatrixD BTransportMatrix_
bool prepareTrack(Track *tr, const AbsTrackRep *rep, bool setSortingParams, int &nFailedHits)
Prepare the track.
TMatrixD FTransportMatrix_
TMatrixDSym FNoiseMatrix_
TrackPoint * fitTrack(Track *tr, const AbsTrackRep *rep, double &chi2, double &ndf, int direction)
Fit the track.
StateOnPlane with additional covariance matrix.
const TMatrixDSym & getCov() const
void blowUpCov(double blowUpFac, bool resetOffDiagonals=true)
Measured coordinates on a plane.
StateOnPlane with linearized transport to that ReferenceStateOnPlane from previous and next Reference...
void setForwardTransportMatrix(const TMatrixD &mat)
void setForwardDeltaState(const TVectorD &mat)
void setForwardSegmentLength(double len)
const TVectorD & getDeltaState(int direction) const
void setBackwardTransportMatrix(const TMatrixD &mat)
double getForwardSegmentLength() const
void setBackwardDeltaState(const TVectorD &mat)
void setForwardNoiseMatrix(const TMatrixDSym &mat)
void setBackwardSegmentLength(double len)
const TMatrixD & getTransportMatrix(int direction) const
const TMatrixDSym & getNoiseMatrix(int direction) const
void setBackwardNoiseMatrix(const TMatrixDSym &mat)
A state with arbitrary dimension defined in a DetPlane.
const TVectorD & getState() const
void setAuxInfo(const TVectorD &auxInfo)
const AbsTrackRep * getRep() const
const TVectorD & getAuxInfo() const
const SharedPlanePtr & getPlane() const
Object containing AbsMeasurement and AbsFitterInfo objects.
bool hasFitterInfo(const AbsTrackRep *rep) const
void deleteFitterInfo(const AbsTrackRep *rep)
AbsFitterInfo * getFitterInfo(const AbsTrackRep *rep=NULL) const
Get fitterInfo for rep. Per default, use cardinal rep.
bool hasRawMeasurements() const
const std::vector< genfit::AbsMeasurement * > & getRawMeasurements() const
void setSortingParameter(double sortingParameter)
void setFitterInfo(genfit::AbsFitterInfo *fitterInfo)
Takes Ownership.
AbsMeasurement * getRawMeasurement(int i=0) const
Collection of TrackPoint objects, AbsTrackRep objects and FitStatus objects.
TrackPoint * getPoint(int id) const
unsigned int getNumPoints() const
int getIdForRep(const AbsTrackRep *rep) const
This is used when streaming TrackPoints.
const TVectorD & getStateSeed() const
void deleteForwardInfo(int startId=0, int endId=-1, const AbsTrackRep *rep=NULL)
TrackPoint * getPointWithMeasurement(int id) const
unsigned int getNumPointsWithMeasurement() const
void deleteBackwardInfo(int startId=0, int endId=-1, const AbsTrackRep *rep=NULL)
const TMatrixDSym & getCovSeed() const
FitStatus * getFitStatus(const AbsTrackRep *rep=NULL) const
Get FitStatus for a AbsTrackRep. Per default, return FitStatus for cardinalRep.
void Print(const Option_t *="") const
AbsTrackRep * getCardinalRep() const
Get cardinal track representation.
TrackPoint * getPointWithMeasurementAndFitterInfo(int id, const AbsTrackRep *rep) const
bool sort()
Sort TrackPoint and according to their sorting parameters.
void setFitStatus(FitStatus *fitStatus, const AbsTrackRep *rep)
boost::shared_ptr< genfit::DetPlane > SharedPlanePtr
Shared Pointer to a DetPlane.