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