EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
covariance_helper.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file covariance_helper.hpp
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 
9 #pragma once
10 
12 
13 #include <type_traits>
14 
15 namespace Acts {
16 namespace detail {
29 template <typename CovMatrix_t, signed int NumIter = 1>
32  static bool validate(CovMatrix_t& covariance) {
33  if (covariance.hasNaN()) {
34  return false;
35  }
36  size_t nIteration = 0;
37  while (nIteration < NumIter) {
38  if (isSemiPositive(covariance)) {
39  return true;
40  } else {
41  Eigen::JacobiSVD<CovMatrix_t> svdCov(
42  covariance, Eigen::ComputeFullU | Eigen::ComputeFullV);
43  CovMatrix_t S = svdCov.singularValues().asDiagonal();
44  CovMatrix_t V = svdCov.matrixV();
45  CovMatrix_t H = V * S * V.transpose();
46  covariance = (covariance + H) / 2;
47  nIteration++;
48  }
49  }
51  return isSemiPositive(covariance);
52  }
53 
55  static bool isSemiPositive(const CovMatrix_t& covariance) {
56  if (covariance.hasNaN()) {
57  return false;
58  }
59  Eigen::LDLT<CovMatrix_t> ldltCov(covariance);
60  return ldltCov.isPositive();
61  }
62 
64  static bool isPositive(const CovMatrix_t& covariance) {
65  if (covariance.hasNaN()) {
66  return false;
67  }
68  Eigen::LLT<CovMatrix_t> lltCov(covariance);
69  return lltCov.info() == Eigen::Success ? true : false;
70  }
71 };
72 
73 } // namespace detail
74 } // namespace Acts