EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
KalmanVertexUpdater.ipp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file KalmanVertexUpdater.ipp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2019 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
10 
11 #include <algorithm>
12 
13 template <typename input_track_t>
16  detail::update<input_track_t>(vtx, trk, 1);
17 }
18 
19 template <typename input_track_t>
22  double trackWeight = trk.trackWeight;
23 
24  MatrixCache matrixCache;
25 
26  updatePosition(vtx, trk.linearizedState, trackWeight, sign, matrixCache);
27 
28  // Get fit quality parameters wrt to old vertex
29  std::pair fitQuality = vtx.fitQuality();
30  double chi2 = fitQuality.first;
31  double ndf = fitQuality.second;
32 
33  // Chi2 wrt to track parameters
34  double trkChi2 = detail::trackParametersChi2<input_track_t>(
35  trk.linearizedState, matrixCache);
36 
37  // Calculate new chi2
38  chi2 += sign * (detail::vertexPositionChi2<input_track_t>(vtx, matrixCache) +
39  trackWeight * trkChi2);
40 
41  // Calculate ndf
42  ndf += sign * trackWeight * 2.;
43 
44  // Updating the vertex
45  vtx.setPosition(matrixCache.newVertexPos);
46  vtx.setCovariance(matrixCache.newVertexCov);
47  vtx.setFitQuality(chi2, ndf);
48 
49  // Updates track at vertex if already there
50  // by removing it first and adding new one.
51  // Otherwise just adds track to existing list of tracks at vertex
52  if (sign > 0) {
53  // Update track
54  trk.chi2Track = trkChi2;
55  trk.ndf = 2 * trackWeight;
56  }
57  // Remove trk from current vertex
58  if (sign < 0) {
59  trk.trackWeight = 0;
60  }
61 }
62 
63 template <typename input_track_t>
66  const Acts::LinearizedTrack& linTrack, double trackWeight, int sign,
67  MatrixCache& matrixCache) {
68  // Retrieve linTrack information
69  const ActsMatrixD<5, 3> posJac = linTrack.positionJacobian.block<5, 3>(0, 0);
70  const ActsMatrixD<5, 3> momJac =
71  linTrack.momentumJacobian.block<5, 3>(0, 0); // B_k in comments below
72  const ActsVectorD<5> trkParams = linTrack.parametersAtPCA.head<5>();
73  const ActsVectorD<5> constTerm = linTrack.constantTerm.head<5>();
74  const ActsSymMatrixD<5> trkParamWeight =
75  linTrack.weightAtPCA.block<5, 5>(0, 0);
76 
77  // Vertex to be updated
78  const Vector3D& oldVtxPos = vtx.position();
79  matrixCache.oldVertexWeight = (vtx.covariance()).inverse();
80 
81  // W_k matrix
82  matrixCache.momWeightInv =
83  (momJac.transpose() * (trkParamWeight * momJac)).inverse();
84 
85  // G_b = G_k - G_k*B_k*W_k*B_k^(T)*G_k^T
86  ActsSymMatrixD<5> gBmat =
87  trkParamWeight -
88  trkParamWeight *
89  (momJac * (matrixCache.momWeightInv * momJac.transpose())) *
90  trkParamWeight.transpose();
91 
92  // New vertex cov matrix
93  matrixCache.newVertexWeight =
94  matrixCache.oldVertexWeight +
95  trackWeight * sign * posJac.transpose() * (gBmat * posJac);
96  matrixCache.newVertexCov = matrixCache.newVertexWeight.inverse();
97 
98  // New vertex position
99  matrixCache.newVertexPos =
100  matrixCache.newVertexCov * (matrixCache.oldVertexWeight * oldVtxPos +
101  trackWeight * sign * posJac.transpose() *
102  gBmat * (trkParams - constTerm));
103 }
104 
105 template <typename input_track_t>
107  const Vertex<input_track_t>& oldVtx, const MatrixCache& matrixCache) {
108  Vector3D posDiff = matrixCache.newVertexPos - oldVtx.position();
109 
110  // Calculate and return corresponding chi2
111  return posDiff.transpose() * (matrixCache.oldVertexWeight * posDiff);
112 }
113 
114 template <typename input_track_t>
116  const LinearizedTrack& linTrack, const MatrixCache& matrixCache) {
117  // Track properties
118  const ActsMatrixD<5, 3> posJac = linTrack.positionJacobian.block<5, 3>(0, 0);
119  const ActsMatrixD<5, 3> momJac = linTrack.momentumJacobian.block<5, 3>(0, 0);
120  const ActsVectorD<5> trkParams = linTrack.parametersAtPCA.head<5>();
121  const ActsVectorD<5> constTerm = linTrack.constantTerm.head<5>();
122  const ActsSymMatrixD<5> trkParamWeight =
123  linTrack.weightAtPCA.block<5, 5>(0, 0);
124 
125  const ActsVectorD<5> jacVtx = posJac * matrixCache.newVertexPos;
126 
127  // Refitted track momentum
128  Vector3D newTrackMomentum = matrixCache.momWeightInv * momJac.transpose() *
129  trkParamWeight * (trkParams - constTerm - jacVtx);
130 
131  // Refitted track parameters
132  ActsVectorD<5> newTrkParams = constTerm + jacVtx + momJac * newTrackMomentum;
133 
134  // Parameter difference
135  ActsVectorD<5> paramDiff = trkParams - newTrkParams;
136 
137  // Return chi2
138  return paramDiff.transpose() * (trkParamWeight * paramDiff);
139 }