Analysis Software
Documentation for sPHENIX simulation 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>
65  const Acts::Vertex<input_track_t>& vtx,
66  const Acts::LinearizedTrack& linTrack, double trackWeight, int sign,
67  MatrixCache& matrixCache) {
68  // Retrieve linTrack information
69  const ActsMatrix<5, 3> posJac = linTrack.positionJacobian.block<5, 3>(0, 0);
70  const ActsMatrix<5, 3> momJac =
71  linTrack.momentumJacobian.block<5, 3>(0, 0); // B_k in comments below
72  const ActsVector<5> trkParams = linTrack.parametersAtPCA.head<5>();
73  const ActsVector<5> constTerm = linTrack.constantTerm.head<5>();
74  // TODO we could use `linTrack.weightAtPCA` but only if we would use time
75  const ActsSquareMatrix<5> trkParamWeight =
76  linTrack.covarianceAtPCA.block<5, 5>(0, 0).inverse();
77 
78  // Vertex to be updated
79  const Vector3& oldVtxPos = vtx.position();
80  matrixCache.oldVertexWeight = (vtx.covariance()).inverse();
81 
82  // W_k matrix
83  matrixCache.momWeightInv =
84  (momJac.transpose() * (trkParamWeight * momJac)).inverse();
85 
86  // G_b = G_k - G_k*B_k*W_k*B_k^(T)*G_k^T
87  ActsSquareMatrix<5> gBmat =
88  trkParamWeight -
89  trkParamWeight *
90  (momJac * (matrixCache.momWeightInv * momJac.transpose())) *
91  trkParamWeight.transpose();
92 
93  // New vertex cov matrix
94  matrixCache.newVertexWeight =
95  matrixCache.oldVertexWeight +
96  trackWeight * sign * posJac.transpose() * (gBmat * posJac);
97  matrixCache.newVertexCov = matrixCache.newVertexWeight.inverse();
98 
99  // New vertex position
100  matrixCache.newVertexPos =
101  matrixCache.newVertexCov * (matrixCache.oldVertexWeight * oldVtxPos +
102  trackWeight * sign * posJac.transpose() *
103  gBmat * (trkParams - constTerm));
104 }
105 
106 template <typename input_track_t>
108  const Vertex<input_track_t>& oldVtx, const MatrixCache& matrixCache) {
109  Vector3 posDiff = matrixCache.newVertexPos - oldVtx.position();
110 
111  // Calculate and return corresponding chi2
112  return posDiff.transpose() * (matrixCache.oldVertexWeight * posDiff);
113 }
114 
115 template <typename input_track_t>
117  const LinearizedTrack& linTrack, const MatrixCache& matrixCache) {
118  // Track properties
119  const ActsMatrix<5, 3> posJac = linTrack.positionJacobian.block<5, 3>(0, 0);
120  const ActsMatrix<5, 3> momJac = linTrack.momentumJacobian.block<5, 3>(0, 0);
121  const ActsVector<5> trkParams = linTrack.parametersAtPCA.head<5>();
122  const ActsVector<5> constTerm = linTrack.constantTerm.head<5>();
123  // TODO we could use `linTrack.weightAtPCA` but only if we would use time
124  const ActsSquareMatrix<5> trkParamWeight =
125  linTrack.covarianceAtPCA.block<5, 5>(0, 0).inverse();
126 
127  const ActsVector<5> jacVtx = posJac * matrixCache.newVertexPos;
128 
129  // Refitted track momentum
130  Vector3 newTrackMomentum = matrixCache.momWeightInv * momJac.transpose() *
131  trkParamWeight * (trkParams - constTerm - jacVtx);
132 
133  // Refitted track parameters
134  ActsVector<5> newTrkParams = constTerm + jacVtx + momJac * newTrackMomentum;
135 
136  // Parameter difference
137  ActsVector<5> paramDiff = trkParams - newTrkParams;
138 
139  // Return chi2
140  return paramDiff.transpose() * (trkParamWeight * paramDiff);
141 }