Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
KalmanVertexTrackUpdater.ipp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file KalmanVertexTrackUpdater.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 
13 
14 template <typename input_track_t>
16  const Vertex<input_track_t>& vtx) {
17  const Vector3 vtxPos = vtx.fullPosition().template head<3>();
18 
19  // Get the linearized track
20  const LinearizedTrack& linTrack = track.linearizedState;
21 
22  // Check if linearized state exists
23  if (linTrack.covarianceAtPCA.determinant() == 0.) {
24  // Track has no linearized state, returning w/o update
25  return;
26  }
27 
28  // Retrieve linTrack information
29  const ActsMatrix<5, 3> posJac = linTrack.positionJacobian.block<5, 3>(0, 0);
30  const ActsMatrix<5, 3> momJac = linTrack.momentumJacobian.block<5, 3>(0, 0);
31  const ActsVector<5> trkParams = linTrack.parametersAtPCA.head<5>();
32  // TODO we could use `linTrack.weightAtPCA` but only if we would use time
33  const ActsSquareMatrix<5> trkParamWeight =
34  linTrack.covarianceAtPCA.block<5, 5>(0, 0).inverse();
35 
36  // Calculate S matrix
37  ActsSquareMatrix<3> sMat =
38  (momJac.transpose() * (trkParamWeight * momJac)).inverse();
39 
40  const ActsVector<5> residual = linTrack.constantTerm.head<5>();
41 
42  // Refit track momentum
43  Vector3 newTrkMomentum = sMat * momJac.transpose() * trkParamWeight *
44  (trkParams - residual - posJac * vtxPos);
45 
46  // Refit track parameters
47  BoundVector newTrkParams(BoundVector::Zero());
48 
49  // Get phi and theta and correct for possible periodicity changes
50  const auto correctedPhiTheta =
51  Acts::detail::normalizePhiTheta(newTrkMomentum(0), newTrkMomentum(1));
52  newTrkParams(BoundIndices::eBoundPhi) = correctedPhiTheta.first; // phi
53  newTrkParams(BoundIndices::eBoundTheta) = correctedPhiTheta.second; // theta
54  newTrkParams(BoundIndices::eBoundQOverP) = newTrkMomentum(2); // qOverP
55 
56  // Vertex covariance and weight matrices
57  const SquareMatrix3 vtxCov = vtx.fullCovariance().template block<3, 3>(0, 0);
58  const SquareMatrix3 vtxWeight = vtxCov.inverse();
59 
60  // New track covariance matrix
61  const SquareMatrix3 newTrkCov =
62  -vtxCov * posJac.transpose() * trkParamWeight * momJac * sMat;
63 
65 
66  // Now determine the smoothed chi2 of the track in the following
67  KalmanVertexUpdater::updatePosition<input_track_t>(
68  vtx, linTrack, track.trackWeight, -1, matrixCache);
69 
70  // Corresponding weight matrix
71  const SquareMatrix3& reducedVtxWeight = matrixCache.newVertexWeight;
72 
73  // Difference in positions
74  Vector3 posDiff = vtx.position() - matrixCache.newVertexPos;
75 
76  // Get smoothed params
77  ActsVector<5> smParams =
78  trkParams - (residual + posJac * vtx.fullPosition().template head<3>() +
79  momJac * newTrkMomentum);
80 
81  // New chi2 to be set later
82  double chi2 = posDiff.dot(reducedVtxWeight * posDiff) +
83  smParams.dot(trkParamWeight * smParams);
84 
85  // Not yet 4d ready. This can be removed together will all head<> statements,
86  // once time is consistently introduced to vertexing
88  newFullTrkCov.block<3, 3>(0, 0) = newTrkCov;
89 
90  SquareMatrix4 vtxFullWeight(SquareMatrix4::Zero());
91  vtxFullWeight.block<3, 3>(0, 0) = vtxWeight;
92 
93  SquareMatrix4 vtxFullCov(SquareMatrix4::Zero());
94  vtxFullCov.block<3, 3>(0, 0) = vtxCov;
95 
97  sMat, newFullTrkCov, vtxFullWeight, vtxFullCov, newTrkParams);
98 
99  // Create new refitted parameters
100  std::shared_ptr<PerigeeSurface> perigeeSurface =
101  Surface::makeShared<PerigeeSurface>(vtx.position());
102 
103  BoundTrackParameters refittedPerigee = BoundTrackParameters(
104  perigeeSurface, newTrkParams, std::move(fullPerTrackCov),
105  track.fittedParams.particleHypothesis());
106 
107  // Set new properties
108  track.fittedParams = refittedPerigee;
109  track.chi2Track = chi2;
110  track.ndf = 2 * track.trackWeight;
111 
112  return;
113 }
114 
115 inline Acts::BoundMatrix
117  const SquareMatrix3& sMat, const ActsMatrix<4, 3>& newTrkCov,
118  const SquareMatrix4& vtxWeight, const SquareMatrix4& vtxCov,
119  const BoundVector& newTrkParams) {
120  // Now new momentum covariance
121  ActsSquareMatrix<3> momCov =
122  sMat + (newTrkCov.block<3, 3>(0, 0)).transpose() *
123  (vtxWeight.block<3, 3>(0, 0) * newTrkCov.block<3, 3>(0, 0));
124 
125  // Full (x,y,z,phi, theta, q/p) covariance matrix
126  // To be made 7d again after switching to (x,y,z,phi, theta, q/p, t)
128 
129  fullTrkCov.block<3, 3>(0, 0) = vtxCov.block<3, 3>(0, 0);
130  fullTrkCov.block<3, 3>(0, 3) = newTrkCov.block<3, 3>(0, 0);
131  fullTrkCov.block<3, 3>(3, 0) = (newTrkCov.block<3, 3>(0, 0)).transpose();
132  fullTrkCov.block<3, 3>(3, 3) = momCov;
133 
134  // Combined track jacobian
136 
137  // First row
138  trkJac(0, 0) = -std::sin(newTrkParams[2]);
139  trkJac(0, 1) = std::cos(newTrkParams[2]);
140 
141  double tanTheta = std::tan(newTrkParams[3]);
142 
143  // Second row
144  trkJac(1, 0) = -trkJac(0, 1) / tanTheta;
145  trkJac(1, 1) = trkJac(0, 0) / tanTheta;
146 
147  trkJac.block<4, 4>(1, 2) = ActsMatrix<4, 4>::Identity();
148 
149  // Full perigee track covariance
150  BoundMatrix fullPerTrackCov(BoundMatrix::Identity());
151  fullPerTrackCov.block<5, 5>(0, 0) =
152  (trkJac * (fullTrkCov * trkJac.transpose()));
153 
154  return fullPerTrackCov;
155 }