Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:18:23

0001 /* Copyright 2013, Ludwig-Maximilians Universität München,
0002    Authors: Tobias Schlüter & Johannes Rauch
0003 
0004    This file is part of GENFIT.
0005 
0006    GENFIT is free software: you can redistribute it and/or modify
0007    it under the terms of the GNU Lesser General Public License as published
0008    by the Free Software Foundation, either version 3 of the License, or
0009    (at your option) any later version.
0010 
0011    GENFIT is distributed in the hope that it will be useful,
0012    but WITHOUT ANY WARRANTY; without even the implied warranty of
0013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014    GNU Lesser General Public License for more details.
0015 
0016    You should have received a copy of the GNU Lesser General Public License
0017    along with GENFIT.  If not, see <http://www.gnu.org/licenses/>.
0018 */
0019 /** @addtogroup genfit
0020  * @{
0021  */
0022 
0023 #ifndef genfit_AbsKalmanFitter_h
0024 #define genfit_AbsKalmanFitter_h
0025 
0026 #include "AbsFitter.h"
0027 #include "MeasurementOnPlane.h"
0028 #include "TrackPoint.h"
0029 
0030 
0031 namespace genfit {
0032 
0033 class KalmanFitterInfo;
0034 
0035 enum eMultipleMeasurementHandling {
0036   weightedAverage, /**<  weighted average between measurements; used by DAF */
0037   unweightedAverage, /**<  average between measurements, all weighted with 1 */
0038   weightedClosestToReference, /**<  closest to reference, weighted with its weight_ */
0039   unweightedClosestToReference, /**<  closest to reference, weighted with 1 */
0040   weightedClosestToPrediction, /**<  closest to prediction, weighted with its weight_ */
0041   unweightedClosestToPrediction, /**<  closest to prediction, weighted with 1 */
0042   weightedClosestToReferenceWire, /**<  if corresponding TrackPoint has one WireMeasurement, select closest to reference, weighted with its weight_. Otherwise use weightedAverage. */
0043   unweightedClosestToReferenceWire, /**<  if corresponding TrackPoint has one WireMeasurement, select closest to reference, weighted with 1. Otherwise use unweightedAverage. */
0044   weightedClosestToPredictionWire, /**<  if corresponding TrackPoint has one WireMeasurement, select closest to prediction, weighted with its weight_. Otherwise use weightedAverage. */
0045   unweightedClosestToPredictionWire /**<  if corresponding TrackPoint has one WireMeasurement, select closest to prediction, weighted with 1. Otherwise use unweightedAverage. Recommended for KalmanFitter to 'resolve' l/r ambiguities */
0046 };
0047 
0048 /**
0049  * @brief Abstract base class for Kalman fitter and derived fitting algorithms
0050  */
0051 class AbsKalmanFitter : public AbsFitter {
0052 
0053  public:
0054 
0055   AbsKalmanFitter(unsigned int maxIterations = 4, double deltaPval = 1e-3, double blowUpFactor = 1e3)
0056     : AbsFitter(), minIterations_(2), maxIterations_(maxIterations), deltaPval_(deltaPval), relChi2Change_(0.2),
0057       blowUpFactor_(blowUpFactor), resetOffDiagonals_(true), blowUpMaxVal_(1.E6),
0058       multipleMeasurementHandling_(unweightedClosestToPredictionWire),
0059       maxFailedHits_(-1) {
0060     if (minIterations_ > maxIterations_)
0061       minIterations_ = maxIterations_;
0062   }
0063 
0064   virtual ~AbsKalmanFitter() {;}
0065 
0066   //virtual void fitTrack(Track* tr, const AbsTrackRep* rep, double& chi2, double& ndf, int direction) = 0;
0067 
0068   void getChiSquNdf(const Track* tr, const AbsTrackRep* rep, double& bChi2, double& fChi2, double& bNdf,  double& fNdf) const;
0069   double getChiSqu(const Track* tr, const AbsTrackRep* rep, int direction = -1) const;
0070   double getNdf(const Track* tr, const AbsTrackRep* rep, int direction = -1) const;
0071   double getRedChiSqu(const Track* tr, const AbsTrackRep* rep, int direction = -1) const;
0072   double getPVal(const Track* tr, const AbsTrackRep* rep, int direction = -1) const;
0073 
0074   unsigned int getMinIterations() const {return minIterations_;}
0075   unsigned int getMaxIterations() const {return maxIterations_;}
0076   double getDeltaPval() const {return deltaPval_;}
0077   double getRelChi2Change() const {return relChi2Change_;}
0078   double getBlowUpFactor() const {return blowUpFactor_;}
0079   bool getResetOffDiagonals() const {return resetOffDiagonals_;}
0080   double getBlowUpMaxVal() const {return blowUpMaxVal_;}
0081   eMultipleMeasurementHandling getMultipleMeasurementHandling() const {return multipleMeasurementHandling_;}
0082   int getMaxFailedHits() const {return maxFailedHits_;}
0083 
0084   //! Set the minimum number of iterations
0085   virtual void setMinIterations(unsigned int n) {minIterations_ = std::max((unsigned int)1,n); if (maxIterations_ < minIterations_) maxIterations_ = minIterations_;}
0086   //! Set the maximum number of iterations
0087   virtual void setMaxIterations(unsigned int n) {maxIterations_ = n; if (minIterations_ > maxIterations_) minIterations_ = maxIterations_;}
0088 
0089   /**
0090    * @brief Set Convergence criterion
0091    *
0092    * if track total P-value changes less than this between consecutive iterations, consider the track converged.
0093    * chi² from the backwards fit is used.
0094    */
0095   void setDeltaPval(double deltaPval) {deltaPval_ = deltaPval;}
0096 
0097   /**
0098    * @ brief Set Non-convergence criterion
0099    *
0100    * if the relative chi^2 between two iterations is larger than relChi2Change_, the fit is NOT converged and
0101    * further iterations will be done, even if the deltaPval_ convergence criterium is met.
0102    * This is especially useful for fits which have a p-value of almost 0 (possibly due to bad start values),
0103    * where the p-value from one iteration to the next might not change much. However, a significant change in
0104    * chi^2 tells us, that the fit might not yet be converged.
0105    */
0106   void setRelChi2Change(double relChi2Change) {relChi2Change_ = relChi2Change;}
0107 
0108   void setBlowUpFactor(double blowUpFactor) {blowUpFactor_ = blowUpFactor;}
0109   void setResetOffDiagonals(bool resetOffDiagonals) {resetOffDiagonals_ = resetOffDiagonals;}
0110   //! Limit the cov entries to this maximum value when blowing up the cov. Set to negative value to disable. Default is 1.E6.
0111   /**
0112    * This is especially useful for fits where the measurements do not constrain one direction,
0113    * e.g. parallel wire measurements. The fit will not be constrained along the wire direction.
0114    * This also means that the covariance along the wire direction will not get smaller during the fit.
0115    * However, it will be blown up before the next iteration, leading to an exponential growth of
0116    * the covariance element(s) corresponding to the wire direction. This can then lead to numerical problems.
0117    * To prevent this, the maximum value of the covariance elements after blowing up can be limited.
0118    */
0119   void setBlowUpMaxVal(double blowUpMaxVal) {blowUpMaxVal_= blowUpMaxVal;}
0120 
0121   //! How should multiple measurements be handled?
0122   void setMultipleMeasurementHandling(eMultipleMeasurementHandling mmh) {multipleMeasurementHandling_ = mmh;}
0123 
0124   virtual void setMaxFailedHits(int val) {maxFailedHits_ = val;}
0125 
0126   bool isTrackPrepared(const Track* tr, const AbsTrackRep* rep) const;
0127   bool isTrackFitted(const Track* tr, const AbsTrackRep* rep) const;
0128 
0129   //! returns if the fitter can ignore the weights and handle the MeasurementOnPlanes as if they had weight 1.
0130   bool canIgnoreWeights() const;
0131 
0132  protected:
0133 
0134   //! get the measurementsOnPlane taking the multipleMeasurementHandling_ into account
0135   const std::vector<MeasurementOnPlane *> getMeasurements(const KalmanFitterInfo* fi, const TrackPoint* tp, int direction) const;
0136 
0137   //! Minimum number of iterations to attempt.  Forward and backward are counted as one iteration.
0138   unsigned int minIterations_;
0139 
0140   //! Maximum number of iterations to attempt.  Forward and backward are counted as one iteration.
0141   unsigned int maxIterations_;
0142   /**
0143    * @brief Convergence criterion
0144    *
0145    * if track total P-value changes less than this between consecutive iterations, consider the track converged.
0146    * chi² from the backwards fit is used.
0147    */
0148   double deltaPval_;
0149   /**
0150    * @ brief Non-convergence criterion
0151    *
0152    * if the relative chi^2 between two iterations is larger than relChi2Change_, the fit is NOT converged and
0153    * further iterations will be done, even if the deltaPval_ convergence criterium is met.
0154    * This is especially useful for fits which have a p-value of almost 0 (possibly due to bad start values),
0155    * where the p-value from one iteration to the next might not change much. However, a significant change in
0156    * chi^2 tells us, that the fit might not yet be converged.
0157    */
0158   double relChi2Change_;
0159 
0160   //! Blow up the covariance of the forward (backward) fit by this factor before seeding the backward (forward) fit.
0161   double blowUpFactor_;
0162   //! Reset the off-diagonals to 0 when blowing up the cov
0163   bool resetOffDiagonals_;
0164   //! Limit the cov entries to this maxuimum value when blowing up the cov.
0165   double blowUpMaxVal_;
0166 
0167   //! How to handle if there are multiple MeasurementsOnPlane
0168   eMultipleMeasurementHandling multipleMeasurementHandling_;
0169 
0170   //! after how many failed hits (exception during construction of plane, extrapolation etc.) should the fit be cancelled.
0171   //! -1 means don't cancel
0172   int maxFailedHits_;
0173 
0174  public:
0175 
0176   ClassDef(AbsKalmanFitter, 2)
0177 
0178 };
0179 
0180 } /* End of namespace genfit */
0181 /** @} */
0182 
0183 #endif //genfit_AbsKalmanFitter_h