HepMC3 event record library: FourVector.h Source File

HepMC3 event record library

FourVector.h
Go to the documentation of this file.
1// -*- C++ -*-
2//
3// This file is part of HepMC
4// Copyright (C) 2014-2023 The HepMC collaboration (see AUTHORS for details)
5//
6#ifndef HEPMC3_FOURVECTOR_H
7#define HEPMC3_FOURVECTOR_H
8/**
9 * @file FourVector.h
10 * @brief Definition of \b class FourVector
11 */
12#include <cmath>
13#include <limits>
14#ifndef M_PI
15/** @brief Definition of PI. Needed on some platforms */
16#define M_PI 3.14159265358979323846264338327950288
17#endif
18namespace HepMC3 {
19
20
21/**
22 * @brief Generic 4-vector
23 *
24 * Interpretation of its content depends on accessors used: it's much simpler to do this
25 * than to distinguish between space and momentum vectors via the type system (especially
26 * given the need for backward compatibility with HepMC2). Be sensible and don't call
27 * energy functions on spatial vectors! To avoid duplication, most definitions are only
28 * implemented on the spatial function names, with the energy-momentum functions as aliases.
29 *
30 * This is @a not intended to be a fully featured 4-vector, but does contain the majority
31 * of common non-boosting functionality, as well as a few support operations on
32 * 4-vectors.
33 *
34 * The implementations in this class are fully inlined.
35 */
36 class FourVector {
37public:
38
39 /** @brief Default constructor */
40 FourVector()
41 : m_v1(0.0), m_v2(0.0), m_v3(0.0), m_v4(0.0) {}
42 /** @brief Sets all FourVector fields */
43 FourVector(double xx, double yy, double zz, double ee)
44 : m_v1(xx), m_v2(yy), m_v3(zz), m_v4(ee) {}
45 /** @brief Copy constructor */
46 FourVector(const FourVector &) = default;
47 /** @brief Move constructor */
48 FourVector(FourVector && ) = default;
49 /** @brief = */
50 FourVector& operator=(const FourVector&) = default;
51 /** @brief = */
52 FourVector& operator=(FourVector&&) = default;
53
54 /// @name Component accessors
55 /// @{
56
57 /** @brief Set all FourVector fields, in order x,y,z,t */
58 void set(double x1, double x2, double x3, double x4) {
59 m_v1 = x1;
60 m_v2 = x2;
61 m_v3 = x3;
62 m_v4 = x4;
63 }
64
65 /// set component of position/displacement
66 void set_component(const int i, const double x)
67 {
68 if (i==0) {m_v1=x; return; }
69 if (i==1) {m_v2=x; return; }
70 if (i==2) {m_v3=x; return; }
71 if (i==3) {m_v4=x; return; }
72 }
73 /// get component of position/displacement
74 double get_component(const int i) const
75 {
76 if (i==0) return m_v1;
77 if (i==1) return m_v2;
78 if (i==2) return m_v3;
79 if (i==3) return m_v4;
80 return 0.0;
81 }
82
83
84 /// x-component of position/displacement
85 double x() const { return m_v1; }
86 /// Set x-component of position/displacement
87 void set_x(double xx) { m_v1 = xx; }
88 /// @deprecated Prefer the HepMC-style set_x() function
89 void setX(double xx) { set_x(xx); }
90
91 /// y-component of position/displacement
92 double y() const { return m_v2; }
93 /// Set y-component of position/displacement
94 void set_y(double yy) { m_v2 = yy; }
95 /// @deprecated Prefer the HepMC-style set_y() function
96 void setY(double yy) { set_y(yy); }
97
98 /// z-component of position/displacement
99 double z() const { return m_v3; }
100 /// Set z-component of position/displacement
101 void set_z(double zz) { m_v3 = zz; }
102 /// @deprecated Prefer the HepMC-style set_z() function
103 void setZ(double zz) { set_z(zz); }
104
105 /// Time component of position/displacement
106 double t() const { return m_v4; }
107 /// Set time component of position/displacement
108 void set_t(double tt) { m_v4 = tt; }
109 /// @deprecated Prefer the HepMC-style set_t() function
110 void setT(double tt) { set_t(tt); }
111
112
113 /// x-component of momentum
114 double px() const { return x(); }
115 /// Set x-component of momentum
116 void set_px(double pxx) { set_x(pxx); }
117 /// @deprecated Prefer the HepMC-style set_px() function
118 void setPx(double pxx) { set_px(pxx); }
119
120 /// y-component of momentum
121 double py() const { return y(); }
122 /// Set y-component of momentum
123 void set_py(double pyy) { set_y(pyy); }
124 /// @deprecated Prefer the HepMC-style set_py() function
125 void setPy(double pyy) { set_py(pyy); }
126
127 /// z-component of momentum
128 double pz() const { return z(); }
129 /// Set z-component of momentum
130 void set_pz(double pzz) { set_z(pzz); }
131 /// @deprecated Prefer the HepMC-style set_pz() function
132 void setPz(double pzz) { set_pz(pzz); }
133
134 /// Energy component of momentum
135 double e() const { return t(); }
136 /// Set energy component of momentum
137 void set_e(double ee ) { this->set_t(ee); }
138 /// @deprecated Prefer the HepMC-style set_y() function
139 void setE(double ee) { set_e(ee); }
140
141 /// @}
142
143
144 /// @name Computed properties
145 /// @{
146
147 /// Squared magnitude of (x, y, z) 3-vector
148 double length2() const { return x()*x() + y()*y() + z()*z(); }
149 /// Magnitude of spatial (x, y, z) 3-vector
150 double length() const { return std::sqrt(length2()); }
151 /// Magnitude of spatial (x, y, z) 3-vector, for HepMC2 compatibility
152 double rho() const { return length(); }
153 /// Squared magnitude of (x, y) vector
154 double perp2() const { return x()*x() + y()*y(); }
155 /// Magnitude of (x, y) vector
156 double perp() const { return std::sqrt(perp2()); }
157 /// Spacetime invariant interval s^2 = t^2 - x^2 - y^2 - z^2
158 double interval() const { return t()*t() - length2(); }
159
160 /// Squared magnitude of p3 = (px, py, pz) vector
161 double p3mod2() const { return length2(); }
162 /// Magnitude of p3 = (px, py, pz) vector
163 double p3mod() const { return length(); }
164 /// Squared transverse momentum px^2 + py^2
165 double pt2() const { return perp2(); }
166 /// Transverse momentum
167 double pt() const { return perp(); }
168 /// Squared invariant mass m^2 = E^2 - px^2 - py^2 - pz^2
169 double m2() const { return interval(); }
170 /// Invariant mass. Returns -sqrt(-m) if e^2 - P^2 is negative
171 double m() const { return (m2() > 0.0) ? std::sqrt(m2()) : -std::sqrt(-m2()); }
172
173 /// Azimuthal angle
174 double phi() const { return std::atan2( y(), x() ); }
175 /// Polar angle w.r.t. z direction
176 double theta() const { return std::atan2( perp(), z() ); }
177 /// Pseudorapidity
178 double eta() const {
179 if ( p3mod() == 0.0 ) return 0.0;
180 if ( p3mod() == fabs(pz()) ) return std::copysign(HUGE_VAL, pz());
181 return 0.5*std::log( (p3mod() + pz()) / (p3mod() - pz()) );
182 }
183 /// Rapidity
184 double rap() const {
185 if ( e() == 0.0 ) return 0.0;
186 if ( e() == fabs(pz()) ) return std::copysign(HUGE_VAL, pz());
187 return 0.5*std::log( (e() + pz()) / (e() - pz()) );
188 }
189 /// Absolute pseudorapidity
190 double abs_eta() const { return std::abs( eta() ); }
191 /// Absolute rapidity
192 double abs_rap() const { return std::abs( rap() ); }
193
194 /// Same as eta()
195 ///
196 /// @deprecated Prefer 'only one way to do it', and we don't have equivalent long names for e.g. pid, phi or eta
197 double pseudoRapidity() const { return eta(); }
198
199 /// @}
200
201
202 /// @name Comparisons to another FourVector
203 /// @{
204
205 /// Check if the length of this vertex is zero
206 bool is_zero() const { return x() == 0 && y() == 0 && z() == 0 && t() == 0; }
207
208 /// Signed azimuthal angle separation in [-pi, pi]
209 double delta_phi(const FourVector &v) const {
210 double dphi = phi() - v.phi();
211 if (dphi != dphi) return dphi;
212 while (dphi >= M_PI) dphi -= 2.*M_PI;
213 while (dphi < -M_PI) dphi += 2.*M_PI;
214 return dphi;
215 }
216
217 /// Pseudorapidity separation
218 double delta_eta(const FourVector &v) const { return eta() - v.eta(); }
219
220 /// Rapidity separation
221 double delta_rap(const FourVector &v) const { return rap() - v.rap(); }
222
223 /// R_eta^2-distance separation dR^2 = dphi^2 + deta^2
224 double delta_r2_eta(const FourVector &v) const {
225 return delta_phi(v)*delta_phi(v) + delta_eta(v)*delta_eta(v);
226 }
227
228 /// R_eta-distance separation dR = sqrt(dphi^2 + deta^2)
229 double delta_r_eta(const FourVector &v) const {
230 return std::sqrt( delta_r2_eta(v) );
231 }
232
233 /// R_rap^2-distance separation dR^2 = dphi^2 + drap^2
234 double delta_r2_rap(const FourVector &v) const {
235 return delta_phi(v)*delta_phi(v) + delta_rap(v)*delta_rap(v);
236 }
237
238 /// R-rap-distance separation dR = sqrt(dphi^2 + drap^2)
239 double delta_r_rap(const FourVector &v) const {
240 return std::sqrt( delta_r2_rap(v) );
241 }
242
243 /// @}
244
245
246 /// @name Operators
247 /// @{
248
249 /// Equality
250 bool operator==(const FourVector& rhs) const {
251 return x() == rhs.x() && y() == rhs.y() && z() == rhs.z() && t() == rhs.t();
252 }
253 /// Inequality
254 bool operator!=(const FourVector& rhs) const { return !(*this == rhs); }
255
256 /// Arithmetic operator +
257 FourVector operator+ (const FourVector& rhs) const {
258 return FourVector( x() + rhs.x(), y() + rhs.y(), z() + rhs.z(), t() + rhs.t() );
259 }
260 /// Arithmetic operator -
261 FourVector operator- (const FourVector& rhs) const {
262 return FourVector( x() - rhs.x(), y() - rhs.y(), z() - rhs.z(), t() - rhs.t() );
263 }
264 /// Arithmetic operator * by scalar
265 FourVector operator* (const double rhs) const {
266 return FourVector( x()*rhs, y()*rhs, z()*rhs, t()*rhs );
267 }
268 /// Arithmetic operator / by scalar
269 FourVector operator/ (const double rhs) const {
270 return FourVector( x()/rhs, y()/rhs, z()/rhs, t()/rhs );
271 }
272
273 /// Arithmetic operator +=
274 void operator += (const FourVector& rhs) {
275 setX(x() + rhs.x());
276 setY(y() + rhs.y());
277 setZ(z() + rhs.z());
278 setT(t() + rhs.t());
279 }
280 /// Arithmetic operator -=
281 void operator -= (const FourVector& rhs) {
282 setX(x() - rhs.x());
283 setY(y() - rhs.y());
284 setZ(z() - rhs.z());
285 setT(t() - rhs.t());
286 }
287 /// Arithmetic operator *= by scalar
288 void operator *= (const double rhs) {
289 setX(x()*rhs);
290 setY(y()*rhs);
291 setZ(z()*rhs);
292 setT(t()*rhs);
293 }
294 /// Arithmetic operator /= by scalar
295 void operator /= (const double rhs) {
296 setX(x()/rhs);
297 setY(y()/rhs);
298 setZ(z()/rhs);
299 setT(t()/rhs);
300 }
301
302 /// @}
303
304
305 /// Static null FourVector = (0,0,0,0)
306 static const FourVector& ZERO_VECTOR() {
307 static const FourVector v;
308 return v;
309 }
310
311
312private:
313
314 double m_v1; ///< px or x. Interpretation depends on accessors used
315 double m_v2; ///< py or y. Interpretation depends on accessors used
316 double m_v3; ///< pz or z. Interpretation depends on accessors used
317 double m_v4; ///< e or t. Interpretation depends on accessors used
318
319};
320
321
322/// @name Unbound vector comparison functions
323/// @{
324
325/// Signed azimuthal angle separation in [-pi, pi] between vecs @c a and @c b
326 inline double delta_phi(const FourVector &a, const FourVector &b) { return b.delta_phi(a); }
327
328/// Pseudorapidity separation between vecs @c a and @c b
329 inline double delta_eta(const FourVector &a, const FourVector &b) { return b.delta_eta(a); }
330
331/// Rapidity separation between vecs @c a and @c b
332 inline double delta_rap(const FourVector &a, const FourVector &b) { return b.delta_rap(a); }
333
334/// R_eta^2-distance separation dR^2 = dphi^2 + deta^2 between vecs @c a and @c b
335 inline double delta_r2_eta(const FourVector &a, const FourVector &b) { return b.delta_r2_eta(a); }
336
337/// R_eta-distance separation dR = sqrt(dphi^2 + deta^2) between vecs @c a and @c b
338 inline double delta_r_eta(const FourVector &a, const FourVector &b) { return b.delta_r_eta(a); }
339
340/// R_rap^2-distance separation dR^2 = dphi^2 + drap^2 between vecs @c a and @c b
341 inline double delta_r2_rap(const FourVector &a, const FourVector &b) { return b.delta_r2_rap(a); }
342
343/// R_rap-distance separation dR = sqrt(dphi^2 + drap^2) between vecs @c a and @c b
344 inline double delta_r_rap(const FourVector &a, const FourVector &b) { return b.delta_r_rap(a); }
345
346/// @}
347
348
349} // namespace HepMC3
350#endif
M_PI
#define M_PI
Definition of PI. Needed on some platforms.
Definition FourVector.h:16
HepMC3::FourVector
Generic 4-vector.
Definition FourVector.h:36
HepMC3::FourVector::setE
void setE(double ee)
Definition FourVector.h:139
HepMC3::FourVector::pt2
double pt2() const
Squared transverse momentum px^2 + py^2.
Definition FourVector.h:165
HepMC3::FourVector::set_t
void set_t(double tt)
Set time component of position/displacement.
Definition FourVector.h:108
HepMC3::FourVector::e
double e() const
Energy component of momentum.
Definition FourVector.h:135
HepMC3::FourVector::setT
void setT(double tt)
Definition FourVector.h:110
HepMC3::FourVector::FourVector
FourVector()
Default constructor.
Definition FourVector.h:40
HepMC3::FourVector::p3mod
double p3mod() const
Magnitude of p3 = (px, py, pz) vector.
Definition FourVector.h:163
HepMC3::FourVector::pz
double pz() const
z-component of momentum
Definition FourVector.h:128
HepMC3::FourVector::t
double t() const
Time component of position/displacement.
Definition FourVector.h:106
HepMC3::FourVector::m2
double m2() const
Squared invariant mass m^2 = E^2 - px^2 - py^2 - pz^2.
Definition FourVector.h:169
HepMC3::FourVector::interval
double interval() const
Spacetime invariant interval s^2 = t^2 - x^2 - y^2 - z^2.
Definition FourVector.h:158
HepMC3::FourVector::delta_r_eta
double delta_r_eta(const FourVector &v) const
R_eta-distance separation dR = sqrt(dphi^2 + deta^2).
Definition FourVector.h:229
HepMC3::FourVector::is_zero
bool is_zero() const
Check if the length of this vertex is zero.
Definition FourVector.h:206
HepMC3::FourVector::m_v4
double m_v4
e or t. Interpretation depends on accessors used
Definition FourVector.h:317
HepMC3::FourVector::m_v3
double m_v3
pz or z. Interpretation depends on accessors used
Definition FourVector.h:316
HepMC3::FourVector::m_v2
double m_v2
py or y. Interpretation depends on accessors used
Definition FourVector.h:315
HepMC3::FourVector::set_x
void set_x(double xx)
Set x-component of position/displacement.
Definition FourVector.h:87
HepMC3::FourVector::setPz
void setPz(double pzz)
Definition FourVector.h:132
HepMC3::FourVector::set_px
void set_px(double pxx)
Set x-component of momentum.
Definition FourVector.h:116
HepMC3::FourVector::operator=
FourVector & operator=(const FourVector &)=default
=
HepMC3::FourVector::eta
double eta() const
Pseudorapidity.
Definition FourVector.h:178
HepMC3::FourVector::setY
void setY(double yy)
Definition FourVector.h:96
HepMC3::FourVector::setPy
void setPy(double pyy)
Definition FourVector.h:125
HepMC3::FourVector::px
double px() const
x-component of momentum
Definition FourVector.h:114
HepMC3::FourVector::operator=
FourVector & operator=(FourVector &&)=default
=
HepMC3::FourVector::delta_r2_eta
double delta_r2_eta(const FourVector &v) const
R_eta^2-distance separation dR^2 = dphi^2 + deta^2.
Definition FourVector.h:224
HepMC3::FourVector::delta_r2_rap
double delta_r2_rap(const FourVector &v) const
R_rap^2-distance separation dR^2 = dphi^2 + drap^2.
Definition FourVector.h:234
HepMC3::FourVector::operator*=
void operator*=(const double rhs)
Arithmetic operator *= by scalar.
Definition FourVector.h:288
HepMC3::FourVector::FourVector
FourVector(const FourVector &)=default
Copy constructor.
HepMC3::FourVector::operator==
bool operator==(const FourVector &rhs) const
Equality.
Definition FourVector.h:250
HepMC3::FourVector::abs_rap
double abs_rap() const
Absolute rapidity.
Definition FourVector.h:192
HepMC3::FourVector::py
double py() const
y-component of momentum
Definition FourVector.h:121
HepMC3::FourVector::setX
void setX(double xx)
Definition FourVector.h:89
HepMC3::FourVector::set_pz
void set_pz(double pzz)
Set z-component of momentum.
Definition FourVector.h:130
HepMC3::FourVector::ZERO_VECTOR
static const FourVector & ZERO_VECTOR()
Static null FourVector = (0,0,0,0).
Definition FourVector.h:306
HepMC3::FourVector::operator-=
void operator-=(const FourVector &rhs)
Arithmetic operator -=.
Definition FourVector.h:281
HepMC3::FourVector::delta_phi
double delta_phi(const FourVector &v) const
Signed azimuthal angle separation in [-pi, pi].
Definition FourVector.h:209
HepMC3::FourVector::operator+=
void operator+=(const FourVector &rhs)
Arithmetic operator +=.
Definition FourVector.h:274
HepMC3::FourVector::operator/=
void operator/=(const double rhs)
Arithmetic operator /= by scalar.
Definition FourVector.h:295
HepMC3::FourVector::length
double length() const
Magnitude of spatial (x, y, z) 3-vector.
Definition FourVector.h:150
HepMC3::FourVector::x
double x() const
x-component of position/displacement
Definition FourVector.h:85
HepMC3::FourVector::perp2
double perp2() const
Squared magnitude of (x, y) vector.
Definition FourVector.h:154
HepMC3::FourVector::delta_r_rap
double delta_r_rap(const FourVector &v) const
R-rap-distance separation dR = sqrt(dphi^2 + drap^2).
Definition FourVector.h:239
HepMC3::FourVector::pt
double pt() const
Transverse momentum.
Definition FourVector.h:167
HepMC3::FourVector::FourVector
FourVector(double xx, double yy, double zz, double ee)
Sets all FourVector fields.
Definition FourVector.h:43
HepMC3::FourVector::operator-
FourVector operator-(const FourVector &rhs) const
Arithmetic operator -.
Definition FourVector.h:261
HepMC3::FourVector::p3mod2
double p3mod2() const
Squared magnitude of p3 = (px, py, pz) vector.
Definition FourVector.h:161
HepMC3::FourVector::FourVector
FourVector(FourVector &&)=default
Move constructor.
HepMC3::FourVector::pseudoRapidity
double pseudoRapidity() const
Definition FourVector.h:197
HepMC3::FourVector::phi
double phi() const
Azimuthal angle.
Definition FourVector.h:174
HepMC3::FourVector::delta_rap
double delta_rap(const FourVector &v) const
Rapidity separation.
Definition FourVector.h:221
HepMC3::FourVector::abs_eta
double abs_eta() const
Absolute pseudorapidity.
Definition FourVector.h:190
HepMC3::FourVector::length2
double length2() const
Squared magnitude of (x, y, z) 3-vector.
Definition FourVector.h:148
HepMC3::FourVector::rho
double rho() const
Magnitude of spatial (x, y, z) 3-vector, for HepMC2 compatibility.
Definition FourVector.h:152
HepMC3::FourVector::m
double m() const
Invariant mass. Returns -sqrt(-m) if e^2 - P^2 is negative.
Definition FourVector.h:171
HepMC3::FourVector::get_component
double get_component(const int i) const
get component of position/displacement
Definition FourVector.h:74
HepMC3::FourVector::perp
double perp() const
Magnitude of (x, y) vector.
Definition FourVector.h:156
HepMC3::FourVector::y
double y() const
y-component of position/displacement
Definition FourVector.h:92
HepMC3::FourVector::operator!=
bool operator!=(const FourVector &rhs) const
Inequality.
Definition FourVector.h:254
HepMC3::FourVector::set_z
void set_z(double zz)
Set z-component of position/displacement.
Definition FourVector.h:101
HepMC3::FourVector::set_component
void set_component(const int i, const double x)
set component of position/displacement
Definition FourVector.h:66
HepMC3::FourVector::operator*
FourVector operator*(const double rhs) const
Arithmetic operator * by scalar.
Definition FourVector.h:265
HepMC3::FourVector::m_v1
double m_v1
px or x. Interpretation depends on accessors used
Definition FourVector.h:314
HepMC3::FourVector::set
void set(double x1, double x2, double x3, double x4)
Set all FourVector fields, in order x,y,z,t.
Definition FourVector.h:58
HepMC3::FourVector::setPx
void setPx(double pxx)
Definition FourVector.h:118
HepMC3::FourVector::z
double z() const
z-component of position/displacement
Definition FourVector.h:99
HepMC3::FourVector::delta_eta
double delta_eta(const FourVector &v) const
Pseudorapidity separation.
Definition FourVector.h:218
HepMC3::FourVector::rap
double rap() const
Rapidity.
Definition FourVector.h:184
HepMC3::FourVector::set_y
void set_y(double yy)
Set y-component of position/displacement.
Definition FourVector.h:94
HepMC3::FourVector::set_e
void set_e(double ee)
Set energy component of momentum.
Definition FourVector.h:137
HepMC3::FourVector::theta
double theta() const
Polar angle w.r.t. z direction.
Definition FourVector.h:176
HepMC3::FourVector::setZ
void setZ(double zz)
Definition FourVector.h:103
HepMC3::FourVector::operator/
FourVector operator/(const double rhs) const
Arithmetic operator / by scalar.
Definition FourVector.h:269
HepMC3::FourVector::set_py
void set_py(double pyy)
Set y-component of momentum.
Definition FourVector.h:123
HepMC3::FourVector::operator+
FourVector operator+(const FourVector &rhs) const
Arithmetic operator +.
Definition FourVector.h:257
HepMC3
HepMC3 main namespace.
Definition AnalysisExample.h:24
HepMC3::delta_phi
double delta_phi(const FourVector &a, const FourVector &b)
Signed azimuthal angle separation in [-pi, pi] between vecs a and b.
Definition FourVector.h:326
HepMC3::delta_r_eta
double delta_r_eta(const FourVector &a, const FourVector &b)
R_eta-distance separation dR = sqrt(dphi^2 + deta^2) between vecs a and b.
Definition FourVector.h:338
HepMC3::delta_r2_rap
double delta_r2_rap(const FourVector &a, const FourVector &b)
R_rap^2-distance separation dR^2 = dphi^2 + drap^2 between vecs a and b.
Definition FourVector.h:341
HepMC3::delta_rap
double delta_rap(const FourVector &a, const FourVector &b)
Rapidity separation between vecs a and b.
Definition FourVector.h:332
HepMC3::delta_r_rap
double delta_r_rap(const FourVector &a, const FourVector &b)
R_rap-distance separation dR = sqrt(dphi^2 + drap^2) between vecs a and b.
Definition FourVector.h:344
HepMC3::delta_r2_eta
double delta_r2_eta(const FourVector &a, const FourVector &b)
R_eta^2-distance separation dR^2 = dphi^2 + deta^2 between vecs a and b.
Definition FourVector.h:335
HepMC3::delta_eta
double delta_eta(const FourVector &a, const FourVector &b)
Pseudorapidity separation between vecs a and b.
Definition FourVector.h:329