Belle II Software  release-08-01-10
KalmanFitter Class Reference

Simple Kalman filter implementation. More...

#include <KalmanFitter.h>

Inheritance diagram for KalmanFitter:
Collaboration diagram for KalmanFitter:

Public Member Functions

 KalmanFitter (unsigned int maxIterations=4, double deltaPval=1e-3, double blowUpFactor=1e3, bool squareRootFormalism=false)
 
void processTrackWithRep (Track *tr, const AbsTrackRep *rep, bool resortHits=false) override
 Hit resorting currently NOT supported.
 
void processTrackPartially (Track *tr, const AbsTrackRep *rep, int startId=0, int endId=-1)
 process only a part of the track. More...
 
void useSquareRootFormalism (bool squareRootFormalism=true)
 
void getChiSquNdf (const Track *tr, const AbsTrackRep *rep, double &bChi2, double &fChi2, double &bNdf, double &fNdf) const
 
double getChiSqu (const Track *tr, const AbsTrackRep *rep, int direction=-1) const
 
double getNdf (const Track *tr, const AbsTrackRep *rep, int direction=-1) const
 
double getRedChiSqu (const Track *tr, const AbsTrackRep *rep, int direction=-1) const
 
double getPVal (const Track *tr, const AbsTrackRep *rep, int direction=-1) const
 
unsigned int getMinIterations () const
 
unsigned int getMaxIterations () const
 
double getDeltaPval () const
 
double getRelChi2Change () const
 
double getBlowUpFactor () const
 
bool getResetOffDiagonals () const
 
double getBlowUpMaxVal () const
 
eMultipleMeasurementHandling getMultipleMeasurementHandling () const
 
int getMaxFailedHits () const
 
virtual void setMinIterations (unsigned int n)
 Set the minimum number of iterations.
 
virtual void setMaxIterations (unsigned int n)
 Set the maximum number of iterations.
 
void setDeltaPval (double deltaPval)
 Set Convergence criterion. More...
 
void setRelChi2Change (double relChi2Change)
 @ brief Set Non-convergence criterion More...
 
void setBlowUpFactor (double blowUpFactor)
 
void setResetOffDiagonals (bool resetOffDiagonals)
 
void setBlowUpMaxVal (double blowUpMaxVal)
 Limit the cov entries to this maximum value when blowing up the cov. Set to negative value to disable. Default is 1.E6. More...
 
void setMultipleMeasurementHandling (eMultipleMeasurementHandling mmh)
 How should multiple measurements be handled?
 
virtual void setMaxFailedHits (int val)
 
bool isTrackPrepared (const Track *tr, const AbsTrackRep *rep) const
 
bool isTrackFitted (const Track *tr, const AbsTrackRep *rep) const
 
bool canIgnoreWeights () const
 returns if the fitter can ignore the weights and handle the MeasurementOnPlanes as if they had weight 1.
 
void processTrack (Track *, bool resortHits=false)
 Process all reps. More...
 
virtual void setDebugLvl (unsigned int lvl=1)
 

Protected Member Functions

const std::vector< MeasurementOnPlane * > getMeasurements (const KalmanFitterInfo *fi, const TrackPoint *tp, int direction) const
 get the measurementsOnPlane taking the multipleMeasurementHandling_ into account
 

Protected Attributes

unsigned int minIterations_
 Minimum number of iterations to attempt. Forward and backward are counted as one iteration.
 
unsigned int maxIterations_
 Maximum number of iterations to attempt. Forward and backward are counted as one iteration.
 
double deltaPval_
 Convergence criterion. More...
 
double relChi2Change_
 @ brief Non-convergence criterion More...
 
double blowUpFactor_
 Blow up the covariance of the forward (backward) fit by this factor before seeding the backward (forward) fit.
 
bool resetOffDiagonals_
 Reset the off-diagonals to 0 when blowing up the cov.
 
double blowUpMaxVal_
 Limit the cov entries to this maxuimum value when blowing up the cov.
 
eMultipleMeasurementHandling multipleMeasurementHandling_
 How to handle if there are multiple MeasurementsOnPlane.
 
int maxFailedHits_
 after how many failed hits (exception during construction of plane, extrapolation etc.) should the fit be cancelled. More...
 
unsigned int debugLvl_
 

Private Member Functions

 KalmanFitter (const KalmanFitter &)
 
KalmanFitteroperator= (KalmanFitter const &)
 
bool fitTrack (Track *tr, const AbsTrackRep *rep, double &chi2, double &ndf, int startId, int endId, int &nFailedHits)
 
void processTrackPoint (TrackPoint *tp, const AbsTrackRep *rep, double &chi2, double &ndf, int direction)
 

Private Attributes

std::unique_ptr< MeasuredStateOnPlanecurrentState_
 
bool squareRootFormalism_
 

Detailed Description

Simple Kalman filter implementation.

Definition at line 40 of file KalmanFitter.h.

Member Function Documentation

◆ processTrack()

void processTrack ( Track tr,
bool  resortHits = false 
)
inherited

Process all reps.

Start with the cardinalRep and resort the hits if necessary (and supported by the fitter)

Definition at line 25 of file AbsFitter.cc.

◆ processTrackPartially()

void processTrackPartially ( Track tr,
const AbsTrackRep rep,
int  startId = 0,
int  endId = -1 
)

process only a part of the track.

Can also be used to process the track only in backward direction. Does not alter the FitStatus and does not do multiple iterations.

Definition at line 304 of file KalmanFitter.cc.

304  {
305 
306  if (tr->getFitStatus(rep) != nullptr && tr->getFitStatus(rep)->isTrackPruned()) {
307  Exception exc("KalmanFitter::processTrack: Cannot process pruned track!", __LINE__,__FILE__);
308  throw exc;
309  }
310 
311  if (startId < 0)
312  startId += tr->getNumPointsWithMeasurement();
313  if (endId < 0)
314  endId += tr->getNumPointsWithMeasurement();
315 
316  int direction(1);
317  if (endId < startId)
318  direction = -1;
319 
320 
321  TrackPoint* trackPoint = tr->getPointWithMeasurement(startId);
322  TrackPoint* prevTrackPoint(nullptr);
323 
324 
325  if (direction == 1 && startId > 0)
326  prevTrackPoint = tr->getPointWithMeasurement(startId - 1);
327  else if (direction == -1 && startId < (int)tr->getNumPointsWithMeasurement() - 1)
328  prevTrackPoint = tr->getPointWithMeasurement(startId + 1);
329 
330 
331  if (prevTrackPoint != nullptr &&
332  prevTrackPoint->hasFitterInfo(rep) &&
333  dynamic_cast<KalmanFitterInfo*>(prevTrackPoint->getFitterInfo(rep)) != nullptr &&
334  static_cast<KalmanFitterInfo*>(prevTrackPoint->getFitterInfo(rep))->hasUpdate(direction)) {
335  currentState_.reset(new MeasuredStateOnPlane(*(static_cast<KalmanFitterInfo*>(prevTrackPoint->getFitterInfo(rep))->getUpdate(direction))));
336  if (debugLvl_ > 0)
337  debugOut << "take update of previous fitter info as seed \n";
338  }
339  else if (rep != tr->getCardinalRep() &&
340  trackPoint->hasFitterInfo(tr->getCardinalRep()) &&
341  dynamic_cast<KalmanFitterInfo*>(trackPoint->getFitterInfo(tr->getCardinalRep())) != nullptr &&
342  static_cast<KalmanFitterInfo*>(trackPoint->getFitterInfo(tr->getCardinalRep()))->hasPredictionsAndUpdates() ) {
343  if (debugLvl_ > 0)
344  debugOut << "take smoothed state of cardinal rep fit as seed \n";
345  TVector3 pos, mom;
346  TMatrixDSym cov;
347  const MeasuredStateOnPlane& fittedState = static_cast<KalmanFitterInfo*>(trackPoint->getFitterInfo(tr->getCardinalRep()))->getFittedState(true);
348  tr->getCardinalRep()->getPosMomCov(fittedState, pos, mom, cov);
349  currentState_.reset(new MeasuredStateOnPlane(rep));
350  rep->setPosMomCov(*currentState_, pos, mom, cov);
351  rep->setQop(*currentState_, tr->getCardinalRep()->getQop(fittedState));
352  }
353  else {
354  currentState_.reset(new MeasuredStateOnPlane(rep));
355  rep->setTime(*currentState_, tr->getTimeSeed());
356  rep->setPosMomCov(*currentState_, tr->getStateSeed(), tr->getCovSeed());
357  if (debugLvl_ > 0)
358  debugOut << "take seed of track as seed \n";
359  }
360 
361  // if at first or last hit, blow up
362  if (startId == 0 || startId == (int)tr->getNumPointsWithMeasurement() - 1) {
363  currentState_->blowUpCov(blowUpFactor_, resetOffDiagonals_, blowUpMaxVal_);
364  if (debugLvl_ > 0)
365  debugOut << "blow up seed \n";
366  }
367 
368  if (debugLvl_ > 0) {
369  debugOut << "\033[1;21mstate pre" << std::endl;
370  currentState_->Print();
371  debugOut << "\033[0mfitting" << std::endl;
372  }
373 
374  double chi2, ndf;
375  int nFailedHits;
376  fitTrack(tr, rep, chi2, ndf, startId, endId, nFailedHits); // return value has no consequences here
377 
378 }
double blowUpFactor_
Blow up the covariance of the forward (backward) fit by this factor before seeding the backward (forw...
double blowUpMaxVal_
Limit the cov entries to this maxuimum value when blowing up the cov.
bool resetOffDiagonals_
Reset the off-diagonals to 0 when blowing up the cov.
virtual void setTime(StateOnPlane &state, double time) const =0
Set time at which the state was defined.
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 getQop(const StateOnPlane &state) const =0
Get charge over momentum.
virtual void setQop(StateOnPlane &state, double qop) const =0
Set charge/momentum.
virtual void getPosMomCov(const MeasuredStateOnPlane &state, TVector3 &pos, TVector3 &mom, TMatrixDSym &cov) const =0
Translates MeasuredStateOnPlane into 3D position, momentum and 6x6 covariance.
Exception class for error handling in GENFIT (provides storage for diagnostic information)
Definition: Exception.h:48
bool isTrackPruned() const
Has the track been pruned after the fit?
Definition: FitStatus.h:116
Collects information needed and produced by a AbsKalmanFitter implementations and is specific to one ...
#StateOnPlane with additional covariance matrix.
Object containing AbsMeasurement and AbsFitterInfo objects.
Definition: TrackPoint.h:46
AbsFitterInfo * getFitterInfo(const AbsTrackRep *rep=nullptr) const
Get fitterInfo for rep. Per default, use cardinal rep.
Definition: TrackPoint.cc:170
FitStatus * getFitStatus(const AbsTrackRep *rep=nullptr) const
Get FitStatus for a AbsTrackRep. Per default, return FitStatus for cardinalRep.
Definition: Track.h:154
AbsTrackRep * getCardinalRep() const
Get cardinal track representation.
Definition: Track.h:145
std::ostream debugOut
Default stream for debug output.

◆ setBlowUpMaxVal()

void setBlowUpMaxVal ( double  blowUpMaxVal)
inlineinherited

Limit the cov entries to this maximum value when blowing up the cov. Set to negative value to disable. Default is 1.E6.

This is especially useful for fits where the measurements do not constrain one direction, e.g. parallel wire measurements. The fit will not be constrained along the wire direction. This also means that the covariance along the wire direction will not get smaller during the fit. However, it will be blown up before the next iteration, leading to an exponential growth of the covariance element(s) corresponding to the wire direction. This can then lead to numerical problems. To prevent this, the maximum value of the covariance elements after blowing up can be limited.

Definition at line 119 of file AbsKalmanFitter.h.

◆ setDeltaPval()

void setDeltaPval ( double  deltaPval)
inlineinherited

Set Convergence criterion.

if track total P-value changes less than this between consecutive iterations, consider the track converged. chiĀ² from the backwards fit is used.

Definition at line 95 of file AbsKalmanFitter.h.

◆ setRelChi2Change()

void setRelChi2Change ( double  relChi2Change)
inlineinherited

@ brief Set Non-convergence criterion

if the relative chi^2 between two iterations is larger than relChi2Change_, the fit is NOT converged and further iterations will be done, even if the deltaPval_ convergence criterium is met. This is especially useful for fits which have a p-value of almost 0 (possibly due to bad start values), where the p-value from one iteration to the next might not change much. However, a significant change in chi^2 tells us, that the fit might not yet be converged.

Definition at line 106 of file AbsKalmanFitter.h.

Member Data Documentation

◆ deltaPval_

double deltaPval_
protectedinherited

Convergence criterion.

if track total P-value changes less than this between consecutive iterations, consider the track converged. chiĀ² from the backwards fit is used.

Definition at line 148 of file AbsKalmanFitter.h.

◆ maxFailedHits_

int maxFailedHits_
protectedinherited

after how many failed hits (exception during construction of plane, extrapolation etc.) should the fit be cancelled.

-1 means don't cancel

Definition at line 172 of file AbsKalmanFitter.h.

◆ relChi2Change_

double relChi2Change_
protectedinherited

@ brief Non-convergence criterion

if the relative chi^2 between two iterations is larger than relChi2Change_, the fit is NOT converged and further iterations will be done, even if the deltaPval_ convergence criterium is met. This is especially useful for fits which have a p-value of almost 0 (possibly due to bad start values), where the p-value from one iteration to the next might not change much. However, a significant change in chi^2 tells us, that the fit might not yet be converged.

Definition at line 158 of file AbsKalmanFitter.h.


The documentation for this class was generated from the following files: