Belle II Software  release-05-01-25
RootEigenTransformations.h
1 #pragma once
2 
3 #include <Eigen/Dense>
4 
5 #include <cassert>
6 
7 namespace genfit {
8 
9  template <unsigned int dim>
10  Eigen::Matrix<double, dim, 1> rootVectorToEigenVector(const TVectorD& rootVector) {
11  const unsigned int rootVectorRows = rootVector.GetNrows();
12  assert(rootVectorRows == dim);
13 
14  double eigenArray[dim];
15  const auto* rootArray = rootVector.GetMatrixArray();
16 
17  std::copy(rootArray,
18  rootArray + rootVectorRows,
19  std::begin(eigenArray));
20 
21  return Eigen::Map<Eigen::Matrix<double, dim, 1>>(eigenArray);
22  }
23 
24  template <unsigned int dim>
25  TVectorD eigenVectorToRootVector(const Eigen::Matrix<double, dim, 1>& eigenVector) {
26  const unsigned int eigenVectorRows = eigenVector.rows();
27 
28  const double* eigenArray = eigenVector.data();
29 
30  TVectorD rootVector(eigenVectorRows);
31  std::copy(eigenArray,
32  eigenArray + eigenVectorRows,
33  rootVector.GetMatrixArray());
34 
35  return rootVector;
36  }
37 
38  template <unsigned int dim>
39  Eigen::Matrix<double, dim, dim> rootMatrixSymToEigenMatrix(const TMatrixDSym& rootMatrix) {
40  assert(rootMatrix.GetNrows() == dim);
41  assert(rootMatrix.GetNcols() == dim);
42  Eigen::Matrix<double, dim, dim> eigenMatrix;
43 
44  for (unsigned int row=0; row<dim; ++row) {
45  for (unsigned int col=0; col<dim; ++col) {
46  eigenMatrix(row, col) = rootMatrix(row, col);
47  }
48  }
49 
50  return eigenMatrix;
51  }
52 
53  template <unsigned int dim>
54  TMatrixDSym eigenMatrixToRootMatrixSym(const Eigen::Matrix<double, dim, dim>& eigenMatrix) {
55  TMatrixDSym rootMatrix(dim);
56 
57  for (unsigned int row=0; row<dim; ++row) {
58  for (unsigned int col=0; col<dim; ++col) {
59  rootMatrix(row, col) = eigenMatrix(row, col);
60  }
61  }
62 
63  return rootMatrix;
64  }
65 
66  template <unsigned int rows, unsigned int cols>
67  Eigen::Matrix<double, rows, cols> rootMatrixToEigenMatrix(const TMatrixD& rootMatrix) {
68  assert(rootMatrix.GetNrows() == rows);
69  assert(rootMatrix.GetNcols() == cols);
70  Eigen::Matrix<double, rows, cols> eigenMatrix;
71 
72  for (unsigned int row=0; row<rows; ++row) {
73  for (unsigned int col=0; col<cols; ++col) {
74  eigenMatrix(row, col) = rootMatrix(row, col);
75  }
76  }
77 
78  return eigenMatrix;
79  }
80 
81  template <unsigned int rows, unsigned int cols>
82  TMatrixD eigenMatrixToRootMatrix(const Eigen::Matrix<double, rows, cols>& eigenMatrix) {
83  TMatrixD rootMatrix(rows, cols);
84 
85  for (unsigned int row=0; row<rows; ++row) {
86  for (unsigned int col=0; col<cols; ++col) {
87  rootMatrix(row, col) = eigenMatrix(row, col);
88  }
89  }
90 
91  return rootMatrix;
92  }
93 
94 }
genfit
Defines for I/O streams used for error and debug printing.
Definition: AlignablePXDRecoHit.h:19