ATLAS Offline Software
AmgMatrixBasePlugin.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 // AmgMatrixBasePlugin.h, (c) ATLAS Detector software
8 
9 #ifndef EVENTPRIMITIVES_AMGMATRIXBASEPLUGIN_H
10 #define EVENTPRIMITIVES_AMGMATRIXBASEPLUGIN_H
11 
12 #include <cmath>
13 
14 
18 // ------- Methods for 3D vector type objects ---------------------- //
19 
21 inline const PlainObject unit() const {
22  return (*this).normalized();
23 }
24 
26 inline Scalar mag() const {
27  return (*this).norm();
28 }
29 
31 inline Scalar mag2() const {
32  return (*this).squaredNorm();
33 }
34 
36 inline Scalar perp2() const {
37  constexpr int size = Eigen::MatrixBase<Derived>::SizeAtCompileTime;
38  constexpr int isVector = Eigen::MatrixBase<Derived>::IsVectorAtCompileTime;
39  static_assert(isVector && size>=2, "Method applicable for vectors of size >=2");
40  return ((*this)[0] * (*this)[0] + (*this)[1] * (*this)[1]);
41 }
42 
44 inline Scalar perp() const {
45  constexpr int size = Eigen::MatrixBase<Derived>::SizeAtCompileTime;
46  constexpr int isVector = Eigen::MatrixBase<Derived>::IsVectorAtCompileTime;
47  static_assert(isVector && size>=2, "Method applicable for vectors of size >=2");
48  return std::sqrt(this->perp2());
49 }
50 
52 inline Scalar perp2(const MatrixBase<Derived>& vec) {
53  Scalar tot = vec.mag2();
54  if (tot > 0) {
55  Scalar s = this->dot(vec);
56  return this->mag2() - s * s / tot;
57  }
58  return this->mag2();
59 }
60 
62 inline Scalar perp(const MatrixBase<Derived>& vec) {
63  return std::sqrt(this->perp2(vec));
64 }
65 
67 inline Scalar phi() const {
68  constexpr int size = Eigen::MatrixBase<Derived>::SizeAtCompileTime;
69  constexpr int isVector = Eigen::MatrixBase<Derived>::IsVectorAtCompileTime;
70  static_assert(isVector && size>=2, "Method applicable for vectors of size >=2");
71  return std::atan2((*this)[1], (*this)[0]);
72 }
73 
75 inline Scalar theta() const {
76  constexpr int size = Eigen::MatrixBase<Derived>::SizeAtCompileTime;
77  constexpr int isVector = Eigen::MatrixBase<Derived>::IsVectorAtCompileTime;
78  static_assert(isVector && size>=3, "Method applicable for vectors of size >=3");
79  return std::atan2(this->perp(), (*this)[2]);
80 }
81 
83 inline Scalar eta() const {
84  constexpr int size = Eigen::MatrixBase<Derived>::SizeAtCompileTime;
85  constexpr int isVector = Eigen::MatrixBase<Derived>::IsVectorAtCompileTime;
86  static_assert(isVector && size>=3, "Method applicable for vectors of size >=3");
87  const Scalar rho2 = (*this).x() * (*this).x() + (*this).y() * (*this).y();
88  const Scalar z = (*this).z();
89  const Scalar z2 = z * z;
90  constexpr Scalar epsilon = 2. * std::numeric_limits<Scalar>::epsilon();
91  // avoid magnitude being parallel to z
92  if (rho2 > z2 * epsilon) {
93  const double m = std::sqrt(rho2 + z2);
94  return 0.5 * std::log((m + z) / (m - z));
95  }
96  if (z == 0) {
97  return 0.0;
98  }
99  // Following math/genvector/inc/Math/GenVector/etaMax.h in ROOT 6.26
100  constexpr Scalar s_etaMax = static_cast<Scalar>(22756.0);
101  // Following math/genvector/inc/Math/GenVector/eta.h in ROOT 6.26
102  return (z > 0) ? z + s_etaMax : z - s_etaMax;
103 }
104 
105 inline Scalar deltaR(const MatrixBase<Derived>& vec) const {
106  //we assert in eta/phi methods
107  double a = this->eta() - vec.eta();
108  double b = this->deltaPhi(vec);
109  return std::sqrt(a * a + b * b);
110 }
111 
112 inline Scalar deltaPhi(const MatrixBase<Derived>& vec) const {
113  //we assert in eta/phi methods
114  double dphi = vec.phi() - this->phi();
115  if (dphi > M_PI) {
116  dphi -= M_PI * 2;
117  } else if (dphi <= -M_PI) {
118  dphi += M_PI * 2;
119  }
120  return dphi;
121 }
122 
124 inline void fillSymmetric(size_t i, size_t j, Scalar value) {
125  (*this)(i, j) = value;
126  (*this)(j, i) = value;
127 }
128 
130 template <typename OtherDerived>
131 inline Matrix<Scalar, OtherDerived::RowsAtCompileTime,
132  OtherDerived::RowsAtCompileTime>
133 similarity(const MatrixBase<OtherDerived>& m) const {
134  return m * (this->derived() * m.transpose());
135 }
136 
138 template <typename OtherDerived>
139 inline Matrix<Scalar, OtherDerived::RowsAtCompileTime,
140  OtherDerived::RowsAtCompileTime>
141 similarityT(const MatrixBase<OtherDerived>& m) const {
142  return m.transpose() * (this->derived() * m);
143 }
144 
145 #endif
Matrix
Definition: Trigger/TrigT1/TrigT1RPChardware/TrigT1RPChardware/Matrix.h:15
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
python.SystemOfUnits.m
int m
Definition: SystemOfUnits.py:91
phi
Scalar phi() const
phi method
Definition: AmgMatrixBasePlugin.h:67
deltaR
Scalar deltaR(const MatrixBase< Derived > &vec) const
Definition: AmgMatrixBasePlugin.h:105
perp
Scalar perp() const
perp method - perpenticular length
Definition: AmgMatrixBasePlugin.h:44
eta
Scalar eta() const
pseudorapidity method
Definition: AmgMatrixBasePlugin.h:83
theta
Scalar theta() const
theta method
Definition: AmgMatrixBasePlugin.h:75
M_PI
#define M_PI
Definition: ActiveFraction.h:11
athena.value
value
Definition: athena.py:124
vec
std::vector< size_t > vec
Definition: CombinationsGeneratorTest.cxx:12
perp2
Scalar perp2() const
perp2 method - perpendicular length squared
Definition: AmgMatrixBasePlugin.h:36
fitman.rho2
rho2
Definition: fitman.py:544
similarity
Matrix< Scalar, OtherDerived::RowsAtCompileTime, OtherDerived::RowsAtCompileTime > similarity(const MatrixBase< OtherDerived > &m) const
similarity method : yields ms = m*s*m^T
Definition: AmgMatrixBasePlugin.h:133
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
lumiFormat.i
int i
Definition: lumiFormat.py:85
z
#define z
deltaPhi
Scalar deltaPhi(const MatrixBase< Derived > &vec) const
Definition: AmgMatrixBasePlugin.h:112
dot.dot
def dot(G, fn, nodesToHighlight=[])
Definition: dot.py:5
LB_AnalMapSplitter.tot
tot
Definition: LB_AnalMapSplitter.py:46
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
similarityT
Matrix< Scalar, OtherDerived::RowsAtCompileTime, OtherDerived::RowsAtCompileTime > similarityT(const MatrixBase< OtherDerived > &m) const
similarityT method : yields ms = m^T*s*m
Definition: AmgMatrixBasePlugin.h:141
a
TList * a
Definition: liststreamerinfos.cxx:10
unit
const PlainObject unit() const
This is a plugin that makes Eigen look like CLHEP & defines some convenience methods.
Definition: AmgMatrixBasePlugin.h:21
python.CaloCondTools.log
log
Definition: CaloCondTools.py:20
runLayerRecalibration.isVector
string isVector
Definition: runLayerRecalibration.py:125
fillSymmetric
void fillSymmetric(size_t i, size_t j, Scalar value)
method to fill elements for a symmetric matrix
Definition: AmgMatrixBasePlugin.h:124
mag2
Scalar mag2() const
mag2 method - forward to squaredNorm()
Definition: AmgMatrixBasePlugin.h:31
mag
Scalar mag() const
mag method
Definition: AmgMatrixBasePlugin.h:26