Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
KalmanGlobalCovariance.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file KalmanGlobalCovariance.hpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2020 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 
9 #pragma once
10 
17 
18 #include <unordered_map>
19 
20 namespace Acts {
21 namespace detail {
22 
36 template <typename traj_t, typename parameters_t = BoundTrackParameters>
37 std::pair<ActsDynamicMatrix, std::unordered_map<size_t, size_t>>
38 globalTrackParametersCovariance(const traj_t& multiTraj,
39  const size_t& entryIndex) {
40  using CovMatrix = typename parameters_t::CovarianceMatrix;
41  using GainMatrix = CovMatrix;
42 
43  // The last smoothed state index
44  size_t lastSmoothedIndex = SIZE_MAX;
45  // The total number of smoothed states
46  size_t nSmoothedStates = 0;
47  // Visit all the states
48  multiTraj.visitBackwards(entryIndex, [&](const auto& ts) {
49  if (ts.hasSmoothed()) {
50  if (lastSmoothedIndex == SIZE_MAX) {
51  lastSmoothedIndex = ts.index();
52  }
53  nSmoothedStates++;
54  }
55  });
56 
57  // Set the size of global track parameters covariance for all smoothed states
58  ActsDynamicMatrix fullGlobalTrackParamsCov(nSmoothedStates * eBoundSize,
59  nSmoothedStates * eBoundSize);
60  fullGlobalTrackParamsCov.setZero();
61  // The index of state within the trajectory and the starting row/column for
62  // this state in the global covariance matrix
63  std::unordered_map<size_t, size_t> stateRowIndices;
64  // Visit the smoothed states to calculate the full global track parameters
65  // covariance
66  size_t nProcessed = 0;
67  auto prev_ts = multiTraj.getTrackState(lastSmoothedIndex);
68  multiTraj.visitBackwards(lastSmoothedIndex, [&](const auto& ts) {
69  const size_t iRow =
70  fullGlobalTrackParamsCov.rows() - eBoundSize * (nProcessed + 1);
71  // Fill the covariance of this state
72  fullGlobalTrackParamsCov.block<eBoundSize, eBoundSize>(iRow, iRow) =
73  ts.smoothedCovariance();
74  // Fill the correlation between this state (indexed by i-1) and
75  // beforehand smoothed states (indexed by j): C^n_{i-1, j}= G_{i-1} *
76  // C^n_{i, j} for i <= j
77  if (nProcessed > 0) {
78  // Calculate the gain matrix
79  GainMatrix G = ts.filteredCovariance() * prev_ts.jacobian().transpose() *
80  prev_ts.predictedCovariance().inverse();
81  // Loop over the beforehand smoothed states
82  for (size_t iProcessed = 1; iProcessed <= nProcessed; iProcessed++) {
83  const size_t iCol = iRow + eBoundSize * iProcessed;
84  CovMatrix prev_correlation =
85  fullGlobalTrackParamsCov.block<eBoundSize, eBoundSize>(
86  iRow + eBoundSize, iCol);
87  CovMatrix correlation = G * prev_correlation;
88  fullGlobalTrackParamsCov.block<eBoundSize, eBoundSize>(iRow, iCol) =
89  correlation;
90  fullGlobalTrackParamsCov.block<eBoundSize, eBoundSize>(iCol, iRow) =
91  correlation.transpose();
92  }
93  }
94  stateRowIndices.emplace(ts.index(), iRow);
95  nProcessed++;
96  prev_ts = ts;
97  });
98 
99  return std::make_pair(fullGlobalTrackParamsCov, stateRowIndices);
100 }
101 
102 } // namespace detail
103 } // namespace Acts