Belle II Software  release-08-01-10
PrecisionMatrixUtil.h
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 #pragma once
9 
10 #include <tracking/trackFindingCDC/numerics/JacobianMatrixUtil.h>
11 
12 #include <tracking/trackFindingCDC/numerics/PrecisionMatrix.h>
13 #include <tracking/trackFindingCDC/numerics/JacobianMatrix.h>
14 #include <tracking/trackFindingCDC/numerics/ParameterVector.h>
15 
16 #include <tracking/trackFindingCDC/numerics/EigenView.h>
17 
18 #include <Eigen/Core>
19 
20 #include <type_traits>
21 
22 namespace Belle2 {
27  namespace TrackFindingCDC {
28 
31 
33  template <int N>
35  {
37  }
38 
40  template <int N>
41  static void transport(const JacobianMatrix<N, N>& ambiguity, PrecisionMatrix<N>& precision)
42  {
43  mapToEigen(precision) =
44  mapToEigen(ambiguity).transpose() * mapToEigen(precision) * mapToEigen(ambiguity);
45  }
46 
49  template <int M, int N>
51  const PrecisionMatrix<N>& precision)
52  {
53  return mapToEigen(ambiguity).transpose() * mapToEigen(precision) * mapToEigen(ambiguity);
54  }
55 
57  template <int N>
58  static void scale(const ParameterVector<N>& scales, PrecisionMatrix<N>& precision)
59  {
60  return transport(JacobianMatrixUtil::scale(1 / scales), precision);
61  }
62 
64  template <int N>
66  const PrecisionMatrix<N>& precision)
67  {
68  return transported(JacobianMatrixUtil::scale(1 / scales), precision);
69  }
70 
73  template <int N1, int N2>
75  const PrecisionMatrix<N2>& block2)
76  {
78  mapToEigen(result) << mapToEigen(block1), Eigen::Matrix<double, N1, N2>::Zero(),
79  Eigen::Matrix<double, N2, N1>::Zero(), mapToEigen(block2);
80  return result;
81  }
82 
84  template <class APrecisionMatrix, int I = 0, int N = 0>
85  static APrecisionMatrix getSub(const PrecisionMatrix<N>& precision)
86  {
87  constexpr const int M =
88  std::remove_reference_t<decltype(mapToEigen(APrecisionMatrix()))>::RowsAtCompileTime;
89  return precision.template block<M, M>(I, I);
90  }
91 
103  template <int N>
104  static double average(const ParameterVector<N>& parameter1,
105  const PrecisionMatrix<N>& precision1,
106  const ParameterVector<N>& parameter2,
107  const PrecisionMatrix<N>& precision2,
108  ParameterVector<N>& parameter,
109  PrecisionMatrix<N>& precision)
110  {
111  const auto& ePrecision1 = mapToEigen(precision1);
112  const auto& ePrecision2 = mapToEigen(precision2);
113  auto&& ePrecision = mapToEigen(precision);
114 
115  const auto& eParameter1 = mapToEigen(parameter1);
116  const auto& eParameter2 = mapToEigen(parameter2);
117  auto&& eParameter = mapToEigen(parameter);
118 
119  ePrecision = ePrecision1 + ePrecision2;
120  eParameter = ePrecision.colPivHouseholderQr().solve(ePrecision1 * eParameter1 +
121  ePrecision2 * eParameter2);
122 
123  auto eResidual1 = eParameter1 - eParameter;
124  auto eResidual2 = eParameter2 - eParameter;
125 
126  Eigen::Matrix<double, 1, 1> chi2 = (eResidual1.transpose() * ePrecision1 * eResidual1 +
127  eResidual2.transpose() * ePrecision2 * eResidual2);
128  return chi2[0];
129  }
130 
145  template <int M, int N1, int N2>
146  static double average(const ParameterVector<N1>& parameter1,
147  const PrecisionMatrix<N1>& precision1,
148  const JacobianMatrix<N1, M>& ambiguity1,
149  const ParameterVector<N2>& parameter2,
150  const PrecisionMatrix<N2>& precision2,
151  const JacobianMatrix<N2, M>& ambiguity2,
152  ParameterVector<M>& parameter,
153  PrecisionMatrix<M>& precision)
154  {
155  const auto& eParameter1 = mapToEigen(parameter1);
156  const auto& ePrecision1 = mapToEigen(precision1);
157  const auto& eAmbiguity1 = mapToEigen(ambiguity1);
158 
159  const auto& eParameter2 = mapToEigen(parameter2);
160  const auto& ePrecision2 = mapToEigen(precision2);
161  const auto& eAmbiguity2 = mapToEigen(ambiguity2);
162 
163  auto&& ePrecision = mapToEigen(precision);
164  auto&& eParameter = mapToEigen(parameter);
165 
166  ePrecision = (eAmbiguity1.transpose() * ePrecision1 * eAmbiguity1 +
167  eAmbiguity2.transpose() * ePrecision2 * eAmbiguity2);
168 
169  eParameter = ePrecision.colPivHouseholderQr().solve(
170  eAmbiguity1.transpose() * ePrecision1 * eParameter1 +
171  eAmbiguity2.transpose() * ePrecision2 * eParameter2);
172 
173  auto eResidual1 = eParameter1 - eAmbiguity1 * eParameter;
174  auto eResidual2 = eParameter2 - eAmbiguity2 * eParameter;
175 
176  Eigen::Matrix<double, 1, 1> chi2 = (eResidual1.transpose() * ePrecision1 * eResidual1 +
177  eResidual2.transpose() * ePrecision2 * eResidual2);
178  return chi2[0];
179  }
180  };
181  }
183 }
A matrix implementation to be used as an interface typ through out the track finder.
Definition: PlainMatrix.h:40
static PlainMatrix< T, M, N > Identity()
Construct an identity matrix.
Definition: PlainMatrix.h:74
Abstract base class for different kinds of events.
static JacobianMatrix< N > scale(const ParameterVector< N > &scales)
Calculates the jacobian matrix for a scaling in each parameter.
Collection of functions related to precision matrices.
static double average(const ParameterVector< N > &parameter1, const PrecisionMatrix< N > &precision1, const ParameterVector< N > &parameter2, const PrecisionMatrix< N > &precision2, ParameterVector< N > &parameter, PrecisionMatrix< N > &precision)
Averages two parameter vectors taking into account their respective precision.
static PrecisionMatrix< N > scale(const ParameterVector< N > &scales, const PrecisionMatrix< N > &precision)
Return a copy of the precision scaled by the given factors in each parameter.
static APrecisionMatrix getSub(const PrecisionMatrix< N > &precision)
Gets a subprecision from a precision matrix.
static void transport(const JacobianMatrix< N, N > &ambiguity, PrecisionMatrix< N > &precision)
Transport the precision matrix inplace with the given jacobian matrix.
static PrecisionMatrix< N > identity()
Constructs an identity matrix.
static PrecisionMatrix< N1+N2 > stackBlocks(const PrecisionMatrix< N1 > &block1, const PrecisionMatrix< N2 > &block2)
Combines two precision matrices by putting them in two blocks on the diagonal of a larger matrix.
static PrecisionMatrix< M > transported(const JacobianMatrix< N, M > &ambiguity, const PrecisionMatrix< N > &precision)
Return a copy of the precision matrix transported with the given back projection jacobian matrix.
static double average(const ParameterVector< N1 > &parameter1, const PrecisionMatrix< N1 > &precision1, const JacobianMatrix< N1, M > &ambiguity1, const ParameterVector< N2 > &parameter2, const PrecisionMatrix< N2 > &precision2, const JacobianMatrix< N2, M > &ambiguity2, ParameterVector< M > &parameter, PrecisionMatrix< M > &precision)
Averages two parameter vectors from a projected space taking into account their respective precisions...
static void scale(const ParameterVector< N > &scales, PrecisionMatrix< N > &precision)
Scale the precision inplace by the given factors in each parameter.