CMS 3D CMS Logo

KFbase.h
Go to the documentation of this file.
1 #ifndef L1Trigger_TrackFindingTMTT_KFbase_h
2 #define L1Trigger_TrackFindingTMTT_KFbase_h
3 
10 #include <TMatrixD.h>
11 #include <TVectorD.h>
12 
13 #include <map>
14 #include <vector>
15 #include <fstream>
16 #include <memory>
17 #include <TString.h>
18 
19 class TH1F;
20 class TH2F;
21 
25 
26 namespace tmtt {
27 
28  class TP;
29  class KalmanState;
30 
31  class KFbase : public TrackFitGeneric {
32  public:
33  enum PAR_IDS { INV2R, PHI0, T, Z0, D0 };
34  enum PAR_IDS_VAR { QOVERPT };
35  enum MEAS_IDS { PHI, Z };
36 
37  public:
38  // Initialize configuration
39  KFbase(const Settings *settings, const uint nHelixPar, const std::string &fitterName = "", const uint nMeas = 2);
40 
41  ~KFbase() override { this->resetStates(); }
42 
43  KFbase(const KFbase &kf) = delete; // Disable copy & move of this class.
44  KFbase(KFbase &&kf) = delete;
45  KFbase &operator=(const KFbase &kf) = delete;
46  KFbase &operator=(KFbase &&kf) = delete;
47 
48  // Do KF fit
49  L1fittedTrack fit(const L1track3D &l1track3D) override;
50 
51  protected:
52  // Do KF fit (internal)
53  const KalmanState *doKF(const L1track3D &l1track3D, const std::vector<Stub *> &stubs, const TP *tpa);
54 
55  // Add one stub to a helix state
56  virtual const KalmanState *kalmanUpdate(
57  unsigned nSkipped, unsigned layer, Stub *stub, const KalmanState *state, const TP *tp);
58 
59  // Create a KalmanState, containing a helix state & next stub it is to be updated with.
60  const KalmanState *mkState(const L1track3D &candidate,
61  unsigned nSkipped,
62  unsigned layer,
63  const KalmanState *last_state,
64  const TVectorD &x,
65  const TMatrixD &pxx,
66  const TMatrixD &K,
67  const TMatrixD &dcov,
68  Stub *stub,
69  double chi2rphi,
70  double chi2rz);
71 
72  //--- Input data
73 
74  // Seed track helix params & covariance matrix
75  virtual TVectorD seedX(const L1track3D &l1track3D) const = 0;
76  virtual TMatrixD seedC(const L1track3D &l1track3D) const = 0;
77 
78  // Stub coordinate measurements & resolution
79  virtual TVectorD vectorM(const Stub *stub) const = 0;
80  virtual TMatrixD matrixV(const Stub *stub, const KalmanState *state) const = 0;
81 
82  //--- KF maths matrix multiplications
83 
84  // Derivate of helix intercept point w.r.t. helix params.
85  virtual TMatrixD matrixH(const Stub *stub) const = 0;
86  // Kalman helix ref point extrapolation matrix
87  virtual TMatrixD matrixF(const Stub *stub = nullptr, const KalmanState *state = nullptr) const = 0;
88  // Product of H*C*H(transpose) (where C = helix covariance matrix)
89  TMatrixD matrixHCHt(const TMatrixD &h, const TMatrixD &c) const;
90  // Get inverted Kalman R matrix: inverse(V + HCHt)
91  TMatrixD matrixRinv(const TMatrixD &matH, const TMatrixD &matCref, const TMatrixD &matV) const;
92  // Kalman gain matrix
93  TMatrixD getKalmanGainMatrix(const TMatrixD &h, const TMatrixD &pxcov, const TMatrixD &covRinv) const;
94 
95  // Residuals of stub with respect to helix.
96  virtual TVectorD residual(const Stub *stub, const TVectorD &x, double candQoverPt) const;
97 
98  // Update helix state & its covariance matrix with new stub
99  void adjustState(const TMatrixD &K,
100  const TMatrixD &pxcov,
101  const TVectorD &x,
102  const TMatrixD &h,
103  const TVectorD &delta,
104  TVectorD &new_x,
105  TMatrixD &new_xcov) const;
106  // Update track fit chi2 with new stub
107  virtual void adjustChi2(const KalmanState *state,
108  const TMatrixD &covRinv,
109  const TVectorD &delta,
110  double &chi2rphi,
111  double &chi2rz) const;
112 
113  //--- Utilities
114 
115  // Reset internal data ready for next track.
116  void resetStates();
117 
118  // Convert to physical helix params instead of local ones used by KF
119  virtual TVectorD trackParams(const KalmanState *state) const = 0;
120  // Ditto after applying beam-spot constraint.
121  virtual TVectorD trackParams_BeamConstr(const KalmanState *state, double &chi2rphi_bcon) const = 0;
122 
123  // Get phi of centre of sector containing track.
124  double sectorPhi() const {
125  float phiCentreSec0 = -M_PI / float(settings_->numPhiNonants()) + M_PI / float(settings_->numPhiSectors());
126  return 2. * M_PI * float(iPhiSec_) / float(settings_->numPhiSectors()) + phiCentreSec0;
127  }
128 
129  // Get KF layer (which is integer representing order layers cross)
130  virtual unsigned int kalmanLayer(
131  unsigned int iEtaReg, unsigned int layerIDreduced, bool barrel, float r, float z) const;
132  // Check if it is unclear whether a particle is expect to cross this layer.
133  virtual bool kalmanAmbiguousLayer(unsigned int iEtaReg, unsigned int kfLayer);
134  // KF algo mods to cope with dead tracker layers.
135  std::set<unsigned> kalmanDeadLayers(bool &remove2PSCut) const;
136 
137  // Function to calculate approximation for tilted barrel modules (aka B) copied from Stub class.
138  float approxB(float z, float r) const;
139 
140  // Is this HLS code?
141  virtual bool isHLS() { return false; };
142 
143  // Helix state pases cuts.
144  virtual bool isGoodState(const KalmanState &state) const = 0;
145 
146  //--- Debug printout
147  void printTP(const TP *tp) const;
148  void printStubLayers(const std::vector<Stub *> &stubs, unsigned int iEtaReg) const;
149  void printStub(const Stub *stub) const;
150  void printStubs(const std::vector<Stub *> &stubs) const;
151 
152  protected:
153  unsigned nHelixPar_;
154  unsigned nMeas_;
155  unsigned numEtaRegions_;
156 
157  unsigned int iPhiSec_;
158  unsigned int iEtaReg_;
159 
160  unsigned int numUpdateCalls_;
161 
162  // All helix states KF produces for current track.
163  std::vector<std::unique_ptr<const KalmanState>> listAllStates_;
164 
165  const TP *tpa_;
166  };
167 
168 } // namespace tmtt
169 
170 #endif
unsigned int numPhiNonants() const
Definition: Settings.h:109
KFbase & operator=(const KFbase &kf)=delete
unsigned numEtaRegions_
Definition: KFbase.h:155
void printStubLayers(const std::vector< Stub *> &stubs, unsigned int iEtaReg) const
Definition: KFbase.cc:933
void adjustState(const TMatrixD &K, const TMatrixD &pxcov, const TVectorD &x, const TMatrixD &h, const TVectorD &delta, TVectorD &new_x, TMatrixD &new_xcov) const
Definition: KFbase.cc:652
void printTP(const TP *tp) const
Definition: KFbase.cc:905
virtual unsigned int kalmanLayer(unsigned int iEtaReg, unsigned int layerIDreduced, bool barrel, float r, float z) const
Definition: KFbase.cc:690
void printStubs(const std::vector< Stub *> &stubs) const
Definition: KFbase.cc:981
L1fittedTrack fit(const L1track3D &l1track3D) override
Definition: KFbase.cc:38
std::set< unsigned > kalmanDeadLayers(bool &remove2PSCut) const
Definition: KFbase.cc:844
float approxB(float z, float r) const
Definition: KFbase.cc:899
unsigned int iEtaReg_
Definition: KFbase.h:158
virtual TVectorD trackParams(const KalmanState *state) const =0
constexpr std::array< uint8_t, layerIndexSize > layer
const KalmanState * mkState(const L1track3D &candidate, unsigned nSkipped, unsigned layer, const KalmanState *last_state, const TVectorD &x, const TMatrixD &pxx, const TMatrixD &K, const TMatrixD &dcov, Stub *stub, double chi2rphi, double chi2rz)
Definition: KFbase.cc:536
Definition: TP.h:23
virtual const KalmanState * kalmanUpdate(unsigned nSkipped, unsigned layer, Stub *stub, const KalmanState *state, const TP *tp)
Definition: KFbase.cc:416
virtual bool kalmanAmbiguousLayer(unsigned int iEtaReg, unsigned int kfLayer)
Definition: KFbase.cc:811
virtual TMatrixD matrixV(const Stub *stub, const KalmanState *state) const =0
const TP * tpa_
Definition: KFbase.h:165
virtual TVectorD seedX(const L1track3D &l1track3D) const =0
const KalmanState * doKF(const L1track3D &l1track3D, const std::vector< Stub *> &stubs, const TP *tpa)
Definition: KFbase.cc:174
const Settings * settings_
void resetStates()
Definition: KFbase.cc:686
void printStub(const Stub *stub) const
Definition: KFbase.cc:961
virtual TMatrixD seedC(const L1track3D &l1track3D) const =0
virtual TMatrixD matrixH(const Stub *stub) const =0
TMatrixD matrixHCHt(const TMatrixD &h, const TMatrixD &c) const
Definition: KFbase.cc:557
#define M_PI
virtual TVectorD trackParams_BeamConstr(const KalmanState *state, double &chi2rphi_bcon) const =0
virtual TVectorD residual(const Stub *stub, const TVectorD &x, double candQoverPt) const
Definition: KFbase.cc:596
TMatrixD matrixRinv(const TMatrixD &matH, const TMatrixD &matCref, const TMatrixD &matV) const
Definition: KFbase.cc:564
KFbase(const Settings *settings, const uint nHelixPar, const std::string &fitterName="", const uint nMeas=2)
TMatrixD getKalmanGainMatrix(const TMatrixD &h, const TMatrixD &pxcov, const TMatrixD &covRinv) const
Definition: KFbase.cc:587
=== This is the base class for the linearised chi-squared track fit algorithms.
Definition: Array2D.h:16
double sectorPhi() const
Definition: KFbase.h:124
virtual bool isGoodState(const KalmanState &state) const =0
unsigned int numUpdateCalls_
Definition: KFbase.h:160
unsigned nHelixPar_
Definition: KFbase.h:153
unsigned int iPhiSec_
Definition: KFbase.h:157
~KFbase() override
Definition: KFbase.h:41
virtual TVectorD vectorM(const Stub *stub) const =0
unsigned nMeas_
Definition: KFbase.h:154
std::vector< std::unique_ptr< const KalmanState > > listAllStates_
Definition: KFbase.h:163
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
unsigned int numPhiSectors() const
Definition: Settings.h:110
virtual TMatrixD matrixF(const Stub *stub=nullptr, const KalmanState *state=nullptr) const =0
virtual void adjustChi2(const KalmanState *state, const TMatrixD &covRinv, const TVectorD &delta, double &chi2rphi, double &chi2rz) const
Definition: KFbase.cc:667
virtual bool isHLS()
Definition: KFbase.h:141