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 */
37public:
38
39 /** @brief Default constructor */
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 = */
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 { return ( p3mod() == 0.0 ) ? 0.0: (0.5*std::log( (p3mod() + pz()) / (p3mod() - pz()) )); }
179 /// Rapidity
180 double rap() const { return ( e() == 0.0 ) ? 0.0: (0.5*std::log( (e() + pz()) / (e() - pz()) )); }
181 /// Absolute pseudorapidity
182 double abs_eta() const { return std::abs( eta() ); }
183 /// Absolute rapidity
184 double abs_rap() const { return std::abs( rap() ); }
185
186 /// Same as eta()
187 /// @deprecated Prefer 'only one way to do it', and we don't have equivalent long names for e.g. pid, phi or eta
188 double pseudoRapidity() const { return eta(); }
189
190 /// @}
191
192
193 /// @name Comparisons to another FourVector
194 /// @{
195
196 /// Check if the length of this vertex is zero
197 bool is_zero() const { return x() == 0 && y() == 0 && z() == 0 && t() == 0; }
198
199 /// Signed azimuthal angle separation in [-pi, pi]
200 double delta_phi(const FourVector &v) const {
201 double dphi = phi() - v.phi();
202 if (dphi != dphi) return dphi;
203 while (dphi >= M_PI) dphi -= 2.*M_PI;
204 while (dphi < -M_PI) dphi += 2.*M_PI;
205 return dphi;
206 }
207
208 /// Pseudorapidity separation
209 double delta_eta(const FourVector &v) const { return eta() - v.eta(); }
210
211 /// Rapidity separation
212 double delta_rap(const FourVector &v) const { return rap() - v.rap(); }
213
214 /// R_eta^2-distance separation dR^2 = dphi^2 + deta^2
215 double delta_r2_eta(const FourVector &v) const {
216 return delta_phi(v)*delta_phi(v) + delta_eta(v)*delta_eta(v);
217 }
218
219 /// R_eta-distance separation dR = sqrt(dphi^2 + deta^2)
220 double delta_r_eta(const FourVector &v) const {
221 return std::sqrt( delta_r2_eta(v) );
222 }
223
224 /// R_rap^2-distance separation dR^2 = dphi^2 + drap^2
225 double delta_r2_rap(const FourVector &v) const {
226 return delta_phi(v)*delta_phi(v) + delta_rap(v)*delta_rap(v);
227 }
228
229 /// R-rap-distance separation dR = sqrt(dphi^2 + drap^2)
230 double delta_r_rap(const FourVector &v) const {
231 return std::sqrt( delta_r2_rap(v) );
232 }
233
234 /// @}
235
236
237 /// @name Operators
238 /// @{
239
240 /// Equality
241 bool operator==(const FourVector& rhs) const {
242 return x() == rhs.x() && y() == rhs.y() && z() == rhs.z() && t() == rhs.t();
243 }
244 /// Inequality
245 bool operator!=(const FourVector& rhs) const { return !(*this == rhs); }
246
247 /// Arithmetic operator +
248 FourVector operator+ (const FourVector& rhs) const {
249 return FourVector( x() + rhs.x(), y() + rhs.y(), z() + rhs.z(), t() + rhs.t() );
250 }
251 /// Arithmetic operator -
252 FourVector operator- (const FourVector& rhs) const {
253 return FourVector( x() - rhs.x(), y() - rhs.y(), z() - rhs.z(), t() - rhs.t() );
254 }
255 /// Arithmetic operator * by scalar
256 FourVector operator* (const double rhs) const {
257 return FourVector( x()*rhs, y()*rhs, z()*rhs, t()*rhs );
258 }
259 /// Arithmetic operator / by scalar
260 FourVector operator/ (const double rhs) const {
261 return FourVector( x()/rhs, y()/rhs, z()/rhs, t()/rhs );
262 }
263
264 /// Arithmetic operator +=
265 void operator += (const FourVector& rhs) {
266 setX(x() + rhs.x());
267 setY(y() + rhs.y());
268 setZ(z() + rhs.z());
269 setT(t() + rhs.t());
270 }
271 /// Arithmetic operator -=
272 void operator -= (const FourVector& rhs) {
273 setX(x() - rhs.x());
274 setY(y() - rhs.y());
275 setZ(z() - rhs.z());
276 setT(t() - rhs.t());
277 }
278 /// Arithmetic operator *= by scalar
279 void operator *= (const double rhs) {
280 setX(x()*rhs);
281 setY(y()*rhs);
282 setZ(z()*rhs);
283 setT(t()*rhs);
284 }
285 /// Arithmetic operator /= by scalar
286 void operator /= (const double rhs) {
287 setX(x()/rhs);
288 setY(y()/rhs);
289 setZ(z()/rhs);
290 setT(t()/rhs);
291 }
292
293 /// @}
294
295
296 /// Static null FourVector = (0,0,0,0)
297 static const FourVector& ZERO_VECTOR() {
298 static const FourVector v;
299 return v;
300 }
301
302
303private:
304
305 double m_v1; ///< px or x. Interpretation depends on accessors used
306 double m_v2; ///< py or y. Interpretation depends on accessors used
307 double m_v3; ///< pz or z. Interpretation depends on accessors used
308 double m_v4; ///< e or t. Interpretation depends on accessors used
309
310};
311
312
313/// @name Unbound vector comparison functions
314/// @{
315
316/// Signed azimuthal angle separation in [-pi, pi] between vecs @c a and @c b
317inline double delta_phi(const FourVector &a, const FourVector &b) { return b.delta_phi(a); }
318
319/// Pseudorapidity separation between vecs @c a and @c b
320inline double delta_eta(const FourVector &a, const FourVector &b) { return b.delta_eta(a); }
321
322/// Rapidity separation between vecs @c a and @c b
323inline double delta_rap(const FourVector &a, const FourVector &b) { return b.delta_rap(a); }
324
325/// R_eta^2-distance separation dR^2 = dphi^2 + deta^2 between vecs @c a and @c b
326inline double delta_r2_eta(const FourVector &a, const FourVector &b) { return b.delta_r2_eta(a); }
327
328/// R_eta-distance separation dR = sqrt(dphi^2 + deta^2) between vecs @c a and @c b
329inline double delta_r_eta(const FourVector &a, const FourVector &b) { return b.delta_r_eta(a); }
330
331/// R_rap^2-distance separation dR^2 = dphi^2 + drap^2 between vecs @c a and @c b
332inline double delta_r2_rap(const FourVector &a, const FourVector &b) { return b.delta_r2_rap(a); }
333
334/// R_rap-distance separation dR = sqrt(dphi^2 + drap^2) between vecs @c a and @c b
335inline double delta_r_rap(const FourVector &a, const FourVector &b) { return b.delta_r_rap(a); }
336
337/// @}
338
339
340} // namespace HepMC3
341#endif
#define M_PI
Definition of PI. Needed on some platforms.
Definition: FourVector.h:16
Generic 4-vector.
Definition: FourVector.h:36
void setE(double ee)
Definition: FourVector.h:139
double pt2() const
Squared transverse momentum px^2 + py^2.
Definition: FourVector.h:165
void set_t(double tt)
Set time component of position/displacement.
Definition: FourVector.h:108
double e() const
Energy component of momentum.
Definition: FourVector.h:135
void setT(double tt)
Definition: FourVector.h:110
FourVector()
Default constructor.
Definition: FourVector.h:40
double p3mod() const
Magnitude of p3 = (px, py, pz) vector.
Definition: FourVector.h:163
double pz() const
z-component of momentum
Definition: FourVector.h:128
double t() const
Time component of position/displacement.
Definition: FourVector.h:106
double m2() const
Squared invariant mass m^2 = E^2 - px^2 - py^2 - pz^2.
Definition: FourVector.h:169
double interval() const
Spacetime invariant interval s^2 = t^2 - x^2 - y^2 - z^2.
Definition: FourVector.h:158
double delta_r_eta(const FourVector &v) const
R_eta-distance separation dR = sqrt(dphi^2 + deta^2)
Definition: FourVector.h:220
bool is_zero() const
Check if the length of this vertex is zero.
Definition: FourVector.h:197
double m_v4
e or t. Interpretation depends on accessors used
Definition: FourVector.h:308
double m_v3
pz or z. Interpretation depends on accessors used
Definition: FourVector.h:307
double m_v2
py or y. Interpretation depends on accessors used
Definition: FourVector.h:306
void set_x(double xx)
Set x-component of position/displacement.
Definition: FourVector.h:87
void setPz(double pzz)
Definition: FourVector.h:132
void set_px(double pxx)
Set x-component of momentum.
Definition: FourVector.h:116
FourVector & operator=(const FourVector &)=default
=
double eta() const
Pseudorapidity.
Definition: FourVector.h:178
void setY(double yy)
Definition: FourVector.h:96
void setPy(double pyy)
Definition: FourVector.h:125
double px() const
x-component of momentum
Definition: FourVector.h:114
FourVector & operator=(FourVector &&)=default
=
double delta_r2_eta(const FourVector &v) const
R_eta^2-distance separation dR^2 = dphi^2 + deta^2.
Definition: FourVector.h:215
double delta_r2_rap(const FourVector &v) const
R_rap^2-distance separation dR^2 = dphi^2 + drap^2.
Definition: FourVector.h:225
void operator*=(const double rhs)
Arithmetic operator *= by scalar.
Definition: FourVector.h:279
FourVector(const FourVector &)=default
Copy constructor.
bool operator==(const FourVector &rhs) const
Equality.
Definition: FourVector.h:241
double abs_rap() const
Absolute rapidity.
Definition: FourVector.h:184
double py() const
y-component of momentum
Definition: FourVector.h:121
void setX(double xx)
Definition: FourVector.h:89
void set_pz(double pzz)
Set z-component of momentum.
Definition: FourVector.h:130
static const FourVector & ZERO_VECTOR()
Static null FourVector = (0,0,0,0)
Definition: FourVector.h:297
void operator-=(const FourVector &rhs)
Arithmetic operator -=.
Definition: FourVector.h:272
double delta_phi(const FourVector &v) const
Signed azimuthal angle separation in [-pi, pi].
Definition: FourVector.h:200
void operator+=(const FourVector &rhs)
Arithmetic operator +=.
Definition: FourVector.h:265
void operator/=(const double rhs)
Arithmetic operator /= by scalar.
Definition: FourVector.h:286
double length() const
Magnitude of spatial (x, y, z) 3-vector.
Definition: FourVector.h:150
double x() const
x-component of position/displacement
Definition: FourVector.h:85
double perp2() const
Squared magnitude of (x, y) vector.
Definition: FourVector.h:154
double delta_r_rap(const FourVector &v) const
R-rap-distance separation dR = sqrt(dphi^2 + drap^2)
Definition: FourVector.h:230
double pt() const
Transverse momentum.
Definition: FourVector.h:167
FourVector(double xx, double yy, double zz, double ee)
Sets all FourVector fields.
Definition: FourVector.h:43
FourVector operator-(const FourVector &rhs) const
Arithmetic operator -.
Definition: FourVector.h:252
double p3mod2() const
Squared magnitude of p3 = (px, py, pz) vector.
Definition: FourVector.h:161
FourVector(FourVector &&)=default
Move constructor.
double pseudoRapidity() const
Definition: FourVector.h:188
double phi() const
Azimuthal angle.
Definition: FourVector.h:174
double delta_rap(const FourVector &v) const
Rapidity separation.
Definition: FourVector.h:212
double abs_eta() const
Absolute pseudorapidity.
Definition: FourVector.h:182
double length2() const
Squared magnitude of (x, y, z) 3-vector.
Definition: FourVector.h:148
double rho() const
Magnitude of spatial (x, y, z) 3-vector, for HepMC2 compatibility.
Definition: FourVector.h:152
double m() const
Invariant mass. Returns -sqrt(-m) if e^2 - P^2 is negative.
Definition: FourVector.h:171
double get_component(const int i) const
get component of position/displacement
Definition: FourVector.h:74
double perp() const
Magnitude of (x, y) vector.
Definition: FourVector.h:156
double y() const
y-component of position/displacement
Definition: FourVector.h:92
bool operator!=(const FourVector &rhs) const
Inequality.
Definition: FourVector.h:245
void set_z(double zz)
Set z-component of position/displacement.
Definition: FourVector.h:101
void set_component(const int i, const double x)
set component of position/displacement
Definition: FourVector.h:66
FourVector operator*(const double rhs) const
Arithmetic operator * by scalar.
Definition: FourVector.h:256
double m_v1
px or x. Interpretation depends on accessors used
Definition: FourVector.h:305
void set(double x1, double x2, double x3, double x4)
Set all FourVector fields, in order x,y,z,t.
Definition: FourVector.h:58
void setPx(double pxx)
Definition: FourVector.h:118
double z() const
z-component of position/displacement
Definition: FourVector.h:99
double delta_eta(const FourVector &v) const
Pseudorapidity separation.
Definition: FourVector.h:209
double rap() const
Rapidity.
Definition: FourVector.h:180
void set_y(double yy)
Set y-component of position/displacement.
Definition: FourVector.h:94
void set_e(double ee)
Set energy component of momentum.
Definition: FourVector.h:137
double theta() const
Polar angle w.r.t. z direction.
Definition: FourVector.h:176
void setZ(double zz)
Definition: FourVector.h:103
FourVector operator/(const double rhs) const
Arithmetic operator / by scalar.
Definition: FourVector.h:260
void set_py(double pyy)
Set y-component of momentum.
Definition: FourVector.h:123
FourVector operator+(const FourVector &rhs) const
Arithmetic operator +.
Definition: FourVector.h:248
HepMC3 main namespace.
double delta_phi(const FourVector &a, const FourVector &b)
Signed azimuthal angle separation in [-pi, pi] between vecs a and b.
Definition: FourVector.h:317
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:329
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:332
double delta_rap(const FourVector &a, const FourVector &b)
Rapidity separation between vecs a and b.
Definition: FourVector.h:323
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:335
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:326
double delta_eta(const FourVector &a, const FourVector &b)
Pseudorapidity separation between vecs a and b.
Definition: FourVector.h:320