ATLAS Offline Software
AmgMatrixBasePlugin.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 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 
17 // ------- Methods for 3D vector type objects ---------------------- //
18 
20 inline const PlainObject unit() const {
21  return (*this).normalized();
22 }
23 
25 inline Scalar mag() const {
26  return (*this).norm();
27 }
28 
30 inline Scalar mag2() const {
31  return (*this).squaredNorm();
32 }
33 
35 inline Scalar perp() const {
36  if (this->rows() < 2)
37  return 0.;
38  return std::sqrt((*this)[0] * (*this)[0] + (*this)[1] * (*this)[1]);
39 }
40 
42 inline Scalar perp2() const {
43  if (this->rows() < 2)
44  return 0.;
45  return ((*this)[0] * (*this)[0] + (*this)[1] * (*this)[1]);
46 }
47 
48 inline Scalar perp2(const MatrixBase<Derived>& vec) {
49  if (this->rows() < 2)
50  return 0.;
51  Scalar tot = vec.mag2();
52  if (tot > 0) {
53  Scalar s = this->dot(vec);
54  return this->mag2() - s * s / tot;
55  }
56  return this->mag2();
57 }
58 
59 inline Scalar perp(const MatrixBase<Derived>& vec) {
60  return std::sqrt(this->perp2(vec));
61 }
62 
64 inline Scalar phi() const {
65  if (this->rows() < 2)
66  return 0.;
67  return std::atan2((*this)[1], (*this)[0]);
68 }
69 
71 inline Scalar theta() const {
72  if (this->rows() < 3)
73  return 0.;
74  return std::atan2(
75  std::sqrt((*this)[0] * (*this)[0] + (*this)[1] * (*this)[1]), (*this)[2]);
76 }
77 
79 inline Scalar eta() const {
80  EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(MatrixBase, 3)
81  const Scalar rho2 = (*this).x() * (*this).x() + (*this).y() * (*this).y();
82  const Scalar z = (*this).z();
83  const Scalar z2 = z * z;
84  constexpr Scalar epsilon = 2. * std::numeric_limits<Scalar>::epsilon();
85  // avoid magnitude being parallel to z
86  if (rho2 > z2 * epsilon) {
87  const double m = std::sqrt(rho2 + z2);
88  return 0.5 * std::log((m + z) / (m - z));
89  }
90  if (z == 0) {
91  return 0.0;
92  }
93  // Following math/genvector/inc/Math/GenVector/etaMax.h in ROOT 6.26
94  constexpr Scalar s_etaMax = static_cast<Scalar>(22756.0);
95  // Following math/genvector/inc/Math/GenVector/eta.h in ROOT 6.26
96  return (z > 0) ? z + s_etaMax : z - s_etaMax;
97 }
98 
99 inline Scalar deltaR(const MatrixBase<Derived>& vec) const {
100  if (this->rows() < 2)
101  return 0.;
102  double a = this->eta() - vec.eta();
103  double b = this->deltaPhi(vec);
104  return std::sqrt(a * a + b * b);
105 }
106 
107 inline Scalar deltaPhi(const MatrixBase<Derived>& vec) const {
108  if (this->rows() < 2)
109  return 0.;
110  double dphi = vec.phi() - this->phi();
111  if (dphi > M_PI) {
112  dphi -= M_PI * 2;
113  } else if (dphi <= -M_PI) {
114  dphi += M_PI * 2;
115  }
116  return dphi;
117 }
118 
119 // ------- Methods for symmetric matrix objects ---------------------- //
121 void fillSymmetric(size_t i, size_t j, Scalar value) {
122  (*this)(i, j) = value;
123  (*this)(j, i) = value;
124 }
125 
126 // /** similarity method : yields ms = m*s*m^T */
127 template <typename OtherDerived>
128 inline Matrix<Scalar, OtherDerived::RowsAtCompileTime,
129  OtherDerived::RowsAtCompileTime>
130 similarity(const MatrixBase<OtherDerived>& m) const {
131  return m * (this->derived() * m.transpose());
132 }
133 
135 template <typename OtherDerived>
136 inline Matrix<Scalar, OtherDerived::RowsAtCompileTime,
137  OtherDerived::RowsAtCompileTime>
138 similarityT(const MatrixBase<OtherDerived>& m) const {
139  return m.transpose() * (this->derived() * m);
140 }
141 
142 #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:64
deltaR
Scalar deltaR(const MatrixBase< Derived > &vec) const
Definition: AmgMatrixBasePlugin.h:99
perp
Scalar perp() const
perp method - perpenticular length
Definition: AmgMatrixBasePlugin.h:35
eta
Scalar eta() const
pseudorapidity method
Definition: AmgMatrixBasePlugin.h:79
theta
Scalar theta() const
theta method
Definition: AmgMatrixBasePlugin.h:71
M_PI
#define M_PI
Definition: ActiveFraction.h:11
athena.value
value
Definition: athena.py:122
vec
std::vector< size_t > vec
Definition: CombinationsGeneratorTest.cxx:12
perp2
Scalar perp2() const
perp2 method - perpendicular length squared
Definition: AmgMatrixBasePlugin.h:42
fitman.rho2
rho2
Definition: fitman.py:544
similarity
Matrix< Scalar, OtherDerived::RowsAtCompileTime, OtherDerived::RowsAtCompileTime > similarity(const MatrixBase< OtherDerived > &m) const
Definition: AmgMatrixBasePlugin.h:130
lumiFormat.i
int i
Definition: lumiFormat.py:92
z
#define z
deltaPhi
Scalar deltaPhi(const MatrixBase< Derived > &vec) const
Definition: AmgMatrixBasePlugin.h:107
beamspotnt.rows
list rows
Definition: bin/beamspotnt.py:1112
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:138
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:20
python.CaloCondTools.log
log
Definition: CaloCondTools.py:20
fillSymmetric
void fillSymmetric(size_t i, size_t j, Scalar value)
method to fill symmetrically elments
Definition: AmgMatrixBasePlugin.h:121
mag2
Scalar mag2() const
mag2 method - forward to squaredNorm()
Definition: AmgMatrixBasePlugin.h:30
mag
Scalar mag() const
mag method
Definition: AmgMatrixBasePlugin.h:25
GlobalSim::dot
void dot(const Digraph &G, const std::string &fn)
Definition: dot.cxx:10