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  static const unsigned int nKFlayer_ = 7;
52  static const unsigned int nEta_ = 16;
53  static const unsigned int nGPlayer_ = 7;
54  static constexpr unsigned int invalidKFlayer_ = nKFlayer_;
55 
56  // index across is GP encoded layer ID (where barrel layers=1,2,7,5,4,3 & endcap wheels=3,4,5,6,7 & 0 never occurs)
57  // index down is eta reg
58  // element.first is kalman layer when stub is from barrel, 7 is invalid
59  // element.second is kalman layer when stub is from endcap, 7 is invalid
60 
61  static constexpr std::pair<unsigned, unsigned> layerMap_[nEta_ / 2][nGPlayer_ + 1] = {
62  {{7, 7}, {0, 7}, {1, 7}, {5, 7}, {4, 7}, {3, 7}, {7, 7}, {2, 7}}, // B1 B2 B3 B4 B5 B6
63  {{7, 7}, {0, 7}, {1, 7}, {5, 7}, {4, 7}, {3, 7}, {7, 7}, {2, 7}}, // B1 B2 B3 B4 B5 B6
64  {{7, 7}, {0, 7}, {1, 7}, {5, 7}, {4, 7}, {3, 7}, {7, 7}, {2, 7}}, // B1 B2 B3 B4 B5 B6
65  {{7, 7}, {0, 7}, {1, 7}, {5, 7}, {4, 7}, {3, 7}, {7, 7}, {2, 7}}, // B1 B2 B3 B4 B5 B6
66  {{7, 7}, {0, 7}, {1, 7}, {5, 4}, {4, 5}, {3, 6}, {7, 7}, {2, 7}}, // B1 B2 B3 B4(/D3) B5(/D2) B6(/D1)
67  {{7, 7}, {0, 7}, {1, 7}, {7, 3}, {7, 4}, {2, 5}, {7, 6}, {2, 6}}, // B1 B2 B3(/D5)+B4(/D3) D1 D2 X D4
68  {{7, 7}, {0, 7}, {1, 7}, {7, 2}, {7, 3}, {7, 4}, {7, 5}, {7, 6}}, // B1 B2 D1 D2 D3 D4 D5
69  {{7, 7}, {0, 7}, {7, 7}, {7, 1}, {7, 2}, {7, 3}, {7, 4}, {7, 5}}, // B1 D1 D2 D3 D4 D5
70  };
71 
72  protected:
73  // Do KF fit (internal)
74  const KalmanState *doKF(const L1track3D &l1track3D, const std::vector<Stub *> &stubs, const TP *tpa);
75 
76  // Add one stub to a helix state
77  virtual const KalmanState *kalmanUpdate(
78  unsigned nSkipped, unsigned layer, Stub *stub, const KalmanState *state, const TP *tp);
79 
80  // Create a KalmanState, containing a helix state & next stub it is to be updated with.
81  const KalmanState *mkState(const L1track3D &candidate,
82  unsigned nSkipped,
83  unsigned layer,
84  const KalmanState *last_state,
85  const TVectorD &x,
86  const TMatrixD &pxx,
87  const TMatrixD &K,
88  const TMatrixD &dcov,
89  Stub *stub,
90  double chi2rphi,
91  double chi2rz);
92 
93  //--- Input data
94 
95  // Seed track helix params & covariance matrix
96  virtual TVectorD seedX(const L1track3D &l1track3D) const = 0;
97  virtual TMatrixD seedC(const L1track3D &l1track3D) const = 0;
98 
99  // Stub coordinate measurements & resolution
100  virtual TVectorD vectorM(const Stub *stub) const = 0;
101  virtual TMatrixD matrixV(const Stub *stub, const KalmanState *state) const = 0;
102 
103  //--- KF maths matrix multiplications
104 
105  // Derivate of helix intercept point w.r.t. helix params.
106  virtual TMatrixD matrixH(const Stub *stub) const = 0;
107  // Kalman helix ref point extrapolation matrix
108  virtual TMatrixD matrixF(const Stub *stub = nullptr, const KalmanState *state = nullptr) const = 0;
109  // Product of H*C*H(transpose) (where C = helix covariance matrix)
110  TMatrixD matrixHCHt(const TMatrixD &h, const TMatrixD &c) const;
111  // Get inverted Kalman R matrix: inverse(V + HCHt)
112  TMatrixD matrixRinv(const TMatrixD &matH, const TMatrixD &matCref, const TMatrixD &matV) const;
113  // Kalman gain matrix
114  TMatrixD getKalmanGainMatrix(const TMatrixD &h, const TMatrixD &pxcov, const TMatrixD &covRinv) const;
115 
116  // Residuals of stub with respect to helix.
117  virtual TVectorD residual(const Stub *stub, const TVectorD &x, double candQoverPt) const;
118 
119  // Update helix state & its covariance matrix with new stub
120  void adjustState(const TMatrixD &K,
121  const TMatrixD &pxcov,
122  const TVectorD &x,
123  const TMatrixD &h,
124  const TVectorD &delta,
125  TVectorD &new_x,
126  TMatrixD &new_xcov) const;
127  // Update track fit chi2 with new stub
128  virtual void adjustChi2(const KalmanState *state,
129  const TMatrixD &covRinv,
130  const TVectorD &delta,
131  double &chi2rphi,
132  double &chi2rz) const;
133 
134  //--- Utilities
135 
136  // Reset internal data ready for next track.
137  void resetStates();
138 
139  // Convert to physical helix params instead of local ones used by KF
140  virtual TVectorD trackParams(const KalmanState *state) const = 0;
141  // Ditto after applying beam-spot constraint.
142  virtual TVectorD trackParams_BeamConstr(const KalmanState *state, double &chi2rphi_bcon) const = 0;
143 
144  // Get phi of centre of sector containing track.
145  double sectorPhi() const {
146  float phiCentreSec0 = -M_PI / float(settings_->numPhiNonants()) + M_PI / float(settings_->numPhiSectors());
147  return 2. * M_PI * float(iPhiSec_) / float(settings_->numPhiSectors()) + phiCentreSec0;
148  }
149 
150  // Get KF layer (which is integer representing order layers cross)
151  virtual unsigned int kalmanLayer(
152  unsigned int iEtaReg, unsigned int layerIDreduced, bool barrel, float r, float z) const;
153  // Check if it is unclear whether a particle is expect to cross this layer.
154  virtual bool kalmanAmbiguousLayer(unsigned int iEtaReg, unsigned int kfLayer);
155  // KF algo mods to cope with dead tracker layers.
156  std::set<unsigned> kalmanDeadLayers(bool &remove2PSCut) const;
157 
158  // Function to calculate approximation for tilted barrel modules (aka B) copied from Stub class.
159  float approxB(float z, float r) const;
160 
161  // Is this HLS code?
162  virtual bool isHLS() { return false; };
163 
164  // Helix state pases cuts.
165  virtual bool isGoodState(const KalmanState &state) const = 0;
166 
167  //--- Debug printout
168  void printTP(const TP *tp) const;
169  void printStubLayers(const std::vector<Stub *> &stubs, unsigned int iEtaReg) const;
170  void printStub(const Stub *stub) const;
171  void printStubs(const std::vector<Stub *> &stubs) const;
172 
173  protected:
174  unsigned nHelixPar_;
175  unsigned nMeas_;
176  unsigned numEtaRegions_;
177 
178  unsigned int iPhiSec_;
179  unsigned int iEtaReg_;
180 
181  unsigned int numUpdateCalls_;
182 
183  // All helix states KF produces for current track.
184  std::vector<std::unique_ptr<const KalmanState>> listAllStates_;
185 
186  const TP *tpa_;
187  };
188 
189 } // namespace tmtt
190 
191 #endif
static constexpr std::pair< unsigned, unsigned > layerMap_[nEta_/2][nGPlayer_+1]
Definition: KFbase.h:61
unsigned int numPhiNonants() const
Definition: Settings.h:109
KFbase & operator=(const KFbase &kf)=delete
unsigned numEtaRegions_
Definition: KFbase.h:176
void printStubLayers(const std::vector< Stub *> &stubs, unsigned int iEtaReg) const
Definition: KFbase.cc:866
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:641
void printTP(const TP *tp) const
Definition: KFbase.cc:838
virtual unsigned int kalmanLayer(unsigned int iEtaReg, unsigned int layerIDreduced, bool barrel, float r, float z) const
Definition: KFbase.cc:679
void printStubs(const std::vector< Stub *> &stubs) const
Definition: KFbase.cc:914
L1fittedTrack fit(const L1track3D &l1track3D) override
Definition: KFbase.cc:38
std::set< unsigned > kalmanDeadLayers(bool &remove2PSCut) const
Definition: KFbase.cc:777
float approxB(float z, float r) const
Definition: KFbase.cc:832
static const unsigned int nEta_
Definition: KFbase.h:52
unsigned int iEtaReg_
Definition: KFbase.h:179
virtual TVectorD trackParams(const KalmanState *state) const =0
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:518
Definition: TP.h:23
virtual const KalmanState * kalmanUpdate(unsigned nSkipped, unsigned layer, Stub *stub, const KalmanState *state, const TP *tp)
Definition: KFbase.cc:398
static const unsigned int nKFlayer_
Definition: KFbase.h:51
virtual bool kalmanAmbiguousLayer(unsigned int iEtaReg, unsigned int kfLayer)
Definition: KFbase.cc:746
virtual TMatrixD matrixV(const Stub *stub, const KalmanState *state) const =0
const TP * tpa_
Definition: KFbase.h:186
static constexpr unsigned int invalidKFlayer_
Definition: KFbase.h:54
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:675
void printStub(const Stub *stub) const
Definition: KFbase.cc:894
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:539
#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:578
TMatrixD matrixRinv(const TMatrixD &matH, const TMatrixD &matCref, const TMatrixD &matV) const
Definition: KFbase.cc:546
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:569
=== This is the base class for the linearised chi-squared track fit algorithms.
Definition: Array2D.h:16
double sectorPhi() const
Definition: KFbase.h:145
virtual bool isGoodState(const KalmanState &state) const =0
unsigned int numUpdateCalls_
Definition: KFbase.h:181
unsigned nHelixPar_
Definition: KFbase.h:174
unsigned int iPhiSec_
Definition: KFbase.h:178
~KFbase() override
Definition: KFbase.h:41
virtual TVectorD vectorM(const Stub *stub) const =0
unsigned nMeas_
Definition: KFbase.h:175
static const unsigned int nGPlayer_
Definition: KFbase.h:53
std::vector< std::unique_ptr< const KalmanState > > listAllStates_
Definition: KFbase.h:184
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:656
virtual bool isHLS()
Definition: KFbase.h:162