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-2021 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
18 namespace 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 {
37 public:
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 */
47  : m_v1(v.m_v1), m_v2(v.m_v2), m_v3(v.m_v3), m_v4(v.m_v4) {}
48 
49 
50  /// @name Component accessors
51  /// @{
52 
53  /** @brief Set all FourVector fields, in order x,y,z,t */
54  void set(double x1, double x2, double x3, double x4) {
55  m_v1 = x1;
56  m_v2 = x2;
57  m_v3 = x3;
58  m_v4 = x4;
59  }
60 
61  /// set component of position/displacement
62  void set_component(const int i, const double x)
63  {
64  if (i==0) {m_v1=x; return; }
65  if (i==1) {m_v2=x; return; }
66  if (i==2) {m_v3=x; return; }
67  if (i==3) {m_v4=x; return; }
68  }
69  /// get component of position/displacement
70  double get_component(const int i) const
71  {
72  if (i==0) return m_v1;
73  if (i==1) return m_v2;
74  if (i==2) return m_v3;
75  if (i==3) return m_v4;
76  return 0.0;
77  }
78 
79 
80  /// x-component of position/displacement
81  double x() const { return m_v1; }
82  /// Set x-component of position/displacement
83  void set_x(double xx) { m_v1 = xx; }
84  /// @deprecated Prefer the HepMC-style set_x() function
85  void setX(double xx) { set_x(xx); }
86 
87  /// y-component of position/displacement
88  double y() const { return m_v2; }
89  /// Set y-component of position/displacement
90  void set_y(double yy) { m_v2 = yy; }
91  /// @deprecated Prefer the HepMC-style set_y() function
92  void setY(double yy) { set_y(yy); }
93 
94  /// z-component of position/displacement
95  double z() const { return m_v3; }
96  /// Set z-component of position/displacement
97  void set_z(double zz) { m_v3 = zz; }
98  /// @deprecated Prefer the HepMC-style set_z() function
99  void setZ(double zz) { set_z(zz); }
100 
101  /// Time component of position/displacement
102  double t() const { return m_v4; }
103  /// Set time component of position/displacement
104  void set_t(double tt) { m_v4 = tt; }
105  /// @deprecated Prefer the HepMC-style set_t() function
106  void setT(double tt) { set_t(tt); }
107 
108 
109  /// x-component of momentum
110  double px() const { return x(); }
111  /// Set x-component of momentum
112  void set_px(double pxx) { set_x(pxx); }
113  /// @deprecated Prefer the HepMC-style set_px() function
114  void setPx(double pxx) { set_px(pxx); }
115 
116  /// y-component of momentum
117  double py() const { return y(); }
118  /// Set y-component of momentum
119  void set_py(double pyy) { set_y(pyy); }
120  /// @deprecated Prefer the HepMC-style set_py() function
121  void setPy(double pyy) { set_py(pyy); }
122 
123  /// z-component of momentum
124  double pz() const { return z(); }
125  /// Set z-component of momentum
126  void set_pz(double pzz) { set_z(pzz); }
127  /// @deprecated Prefer the HepMC-style set_pz() function
128  void setPz(double pzz) { set_pz(pzz); }
129 
130  /// Energy component of momentum
131  double e() const { return t(); }
132  /// Set energy component of momentum
133  void set_e(double ee ) { this->set_t(ee); }
134  /// @deprecated Prefer the HepMC-style set_y() function
135  void setE(double ee) { set_e(ee); }
136 
137  /// @}
138 
139 
140  /// @name Computed properties
141  /// @{
142 
143  /// Squared magnitude of (x, y, z) 3-vector
144  double length2() const { return x()*x() + y()*y() + z()*z(); }
145  /// Magnitude of spatial (x, y, z) 3-vector
146  double length() const { return std::sqrt(length2()); }
147  /// Magnitude of spatial (x, y, z) 3-vector, for HepMC2 compatibility
148  double rho() const { return length(); }
149  /// Squared magnitude of (x, y) vector
150  double perp2() const { return x()*x() + y()*y(); }
151  /// Magnitude of (x, y) vector
152  double perp() const { return std::sqrt(perp2()); }
153  /// Spacetime invariant interval s^2 = t^2 - x^2 - y^2 - z^2
154  double interval() const { return t()*t() - length2(); }
155 
156  /// Squared magnitude of p3 = (px, py, pz) vector
157  double p3mod2() const { return length2(); }
158  /// Magnitude of p3 = (px, py, pz) vector
159  double p3mod() const { return length(); }
160  /// Squared transverse momentum px^2 + py^2
161  double pt2() const { return perp2(); }
162  /// Transverse momentum
163  double pt() const { return perp(); }
164  /// Squared invariant mass m^2 = E^2 - px^2 - py^2 - pz^2
165  double m2() const { return interval(); }
166  /// Invariant mass. Returns -sqrt(-m) if e^2 - P^2 is negative
167  double m() const { return (m2() > 0.0) ? std::sqrt(m2()) : -std::sqrt(-m2()); }
168 
169  /// Azimuthal angle
170  double phi() const { return std::atan2( y(), x() ); }
171  /// Polar angle w.r.t. z direction
172  double theta() const { return std::atan2( perp(), z() ); }
173  /// Pseudorapidity
174  double eta() const { return ( p3mod() == 0.0 ) ? 0.0: (0.5*std::log( (p3mod() + pz()) / (p3mod() - pz()) )); }
175  /// Rapidity
176  double rap() const { return ( e() == 0.0 ) ? 0.0: (0.5*std::log( (e() + pz()) / (e() - pz()) )); }
177  /// Absolute pseudorapidity
178  double abs_eta() const { return std::abs( eta() ); }
179  /// Absolute rapidity
180  double abs_rap() const { return std::abs( rap() ); }
181 
182  /// Same as eta()
183  /// @deprecated Prefer 'only one way to do it', and we don't have equivalent long names for e.g. pid, phi or eta
184  double pseudoRapidity() const { return eta(); }
185 
186  /// @}
187 
188 
189  /// @name Comparisons to another FourVector
190  /// @{
191 
192  /// Check if the length of this vertex is zero
193  bool is_zero() const { return x() == 0 && y() == 0 && z() == 0 && t() == 0; }
194 
195  /// Signed azimuthal angle separation in [-pi, pi]
196  double delta_phi(const FourVector &v) const {
197  double dphi = phi() - v.phi();
198  if (dphi != dphi) return dphi;
199  while (dphi >= M_PI) dphi -= 2.*M_PI;
200  while (dphi < -M_PI) dphi += 2.*M_PI;
201  return dphi;
202  }
203 
204  /// Pseudorapidity separation
205  double delta_eta(const FourVector &v) const { return eta() - v.eta(); }
206 
207  /// Rapidity separation
208  double delta_rap(const FourVector &v) const { return rap() - v.rap(); }
209 
210  /// R_eta^2-distance separation dR^2 = dphi^2 + deta^2
211  double delta_r2_eta(const FourVector &v) const {
212  return delta_phi(v)*delta_phi(v) + delta_eta(v)*delta_eta(v);
213  }
214 
215  /// R_eta-distance separation dR = sqrt(dphi^2 + deta^2)
216  double delta_r_eta(const FourVector &v) const {
217  return std::sqrt( delta_r2_eta(v) );
218  }
219 
220  /// R_rap^2-distance separation dR^2 = dphi^2 + drap^2
221  double delta_r2_rap(const FourVector &v) const {
222  return delta_phi(v)*delta_phi(v) + delta_rap(v)*delta_rap(v);
223  }
224 
225  /// R-rap-distance separation dR = sqrt(dphi^2 + drap^2)
226  double delta_r_rap(const FourVector &v) const {
227  return std::sqrt( delta_r2_rap(v) );
228  }
229 
230  /// @}
231 
232 
233  /// @name Operators
234  /// @{
235 
236  /// Equality
237  bool operator==(const FourVector& rhs) const {
238  return x() == rhs.x() && y() == rhs.y() && z() == rhs.z() && t() == rhs.t();
239  }
240  /// Inequality
241  bool operator!=(const FourVector& rhs) const { return !(*this == rhs); }
242 
243  /// Arithmetic operator +
244  FourVector operator+ (const FourVector& rhs) const {
245  return FourVector( x() + rhs.x(), y() + rhs.y(), z() + rhs.z(), t() + rhs.t() );
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 * by scalar
252  FourVector operator* (const double rhs) const {
253  return FourVector( x()*rhs, y()*rhs, z()*rhs, t()*rhs );
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 
260  /// Arithmetic operator +=
261  void operator += (const FourVector& rhs) {
262  setX(x() + rhs.x());
263  setY(y() + rhs.y());
264  setZ(z() + rhs.z());
265  setT(t() + rhs.t());
266  }
267  /// Arithmetic operator -=
268  void operator -= (const FourVector& rhs) {
269  setX(x() - rhs.x());
270  setY(y() - rhs.y());
271  setZ(z() - rhs.z());
272  setT(t() - rhs.t());
273  }
274  /// Arithmetic operator *= by scalar
275  void operator *= (const double rhs) {
276  setX(x()*rhs);
277  setY(y()*rhs);
278  setZ(z()*rhs);
279  setT(t()*rhs);
280  }
281  /// Arithmetic operator /= by scalar
282  void operator /= (const double rhs) {
283  setX(x()/rhs);
284  setY(y()/rhs);
285  setZ(z()/rhs);
286  setT(t()/rhs);
287  }
288 
289  /// @}
290 
291 
292  /// Static null FourVector = (0,0,0,0)
293  static const FourVector& ZERO_VECTOR() {
294  static const FourVector v;
295  return v;
296  }
297 
298 
299 private:
300 
301  double m_v1; ///< px or x. Interpretation depends on accessors used
302  double m_v2; ///< py or y. Interpretation depends on accessors used
303  double m_v3; ///< pz or z. Interpretation depends on accessors used
304  double m_v4; ///< e or t. Interpretation depends on accessors used
305 
306 };
307 
308 
309 /// @name Unbound vector comparison functions
310 /// @{
311 
312 /// Signed azimuthal angle separation in [-pi, pi] between vecs @c a and @c b
313 inline double delta_phi(const FourVector &a, const FourVector &b) { return b.delta_phi(a); }
314 
315 /// Pseudorapidity separation between vecs @c a and @c b
316 inline double delta_eta(const FourVector &a, const FourVector &b) { return b.delta_eta(a); }
317 
318 /// Rapidity separation between vecs @c a and @c b
319 inline double delta_rap(const FourVector &a, const FourVector &b) { return b.delta_rap(a); }
320 
321 /// R_eta^2-distance separation dR^2 = dphi^2 + deta^2 between vecs @c a and @c b
322 inline double delta_r2_eta(const FourVector &a, const FourVector &b) { return b.delta_r2_eta(a); }
323 
324 /// R_eta-distance separation dR = sqrt(dphi^2 + deta^2) between vecs @c a and @c b
325 inline double delta_r_eta(const FourVector &a, const FourVector &b) { return b.delta_r_eta(a); }
326 
327 /// R_rap^2-distance separation dR^2 = dphi^2 + drap^2 between vecs @c a and @c b
328 inline double delta_r2_rap(const FourVector &a, const FourVector &b) { return b.delta_r2_rap(a); }
329 
330 /// R_rap-distance separation dR = sqrt(dphi^2 + drap^2) between vecs @c a and @c b
331 inline double delta_r_rap(const FourVector &a, const FourVector &b) { return b.delta_r_rap(a); }
332 
333 /// @}
334 
335 
336 } // namespace HepMC3
337 #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:135
double pt2() const
Squared transverse momentum px^2 + py^2.
Definition: FourVector.h:161
void set_t(double tt)
Set time component of position/displacement.
Definition: FourVector.h:104
double e() const
Energy component of momentum.
Definition: FourVector.h:131
void setT(double tt)
Definition: FourVector.h:106
FourVector()
Default constructor.
Definition: FourVector.h:40
double p3mod() const
Magnitude of p3 = (px, py, pz) vector.
Definition: FourVector.h:159
double pz() const
z-component of momentum
Definition: FourVector.h:124
double t() const
Time component of position/displacement.
Definition: FourVector.h:102
double m2() const
Squared invariant mass m^2 = E^2 - px^2 - py^2 - pz^2.
Definition: FourVector.h:165
double interval() const
Spacetime invariant interval s^2 = t^2 - x^2 - y^2 - z^2.
Definition: FourVector.h:154
double delta_r_eta(const FourVector &v) const
R_eta-distance separation dR = sqrt(dphi^2 + deta^2)
Definition: FourVector.h:216
bool is_zero() const
Check if the length of this vertex is zero.
Definition: FourVector.h:193
double m_v4
e or t. Interpretation depends on accessors used
Definition: FourVector.h:304
double m_v3
pz or z. Interpretation depends on accessors used
Definition: FourVector.h:303
double m_v2
py or y. Interpretation depends on accessors used
Definition: FourVector.h:302
void set_x(double xx)
Set x-component of position/displacement.
Definition: FourVector.h:83
void setPz(double pzz)
Definition: FourVector.h:128
void set_px(double pxx)
Set x-component of momentum.
Definition: FourVector.h:112
double eta() const
Pseudorapidity.
Definition: FourVector.h:174
void setY(double yy)
Definition: FourVector.h:92
void setPy(double pyy)
Definition: FourVector.h:121
double px() const
x-component of momentum
Definition: FourVector.h:110
double delta_r2_eta(const FourVector &v) const
R_eta^2-distance separation dR^2 = dphi^2 + deta^2.
Definition: FourVector.h:211
double delta_r2_rap(const FourVector &v) const
R_rap^2-distance separation dR^2 = dphi^2 + drap^2.
Definition: FourVector.h:221
void operator*=(const double rhs)
Arithmetic operator *= by scalar.
Definition: FourVector.h:275
bool operator==(const FourVector &rhs) const
Equality.
Definition: FourVector.h:237
double abs_rap() const
Absolute rapidity.
Definition: FourVector.h:180
double py() const
y-component of momentum
Definition: FourVector.h:117
void setX(double xx)
Definition: FourVector.h:85
void set_pz(double pzz)
Set z-component of momentum.
Definition: FourVector.h:126
void operator-=(const FourVector &rhs)
Arithmetic operator -=.
Definition: FourVector.h:268
double delta_phi(const FourVector &v) const
Signed azimuthal angle separation in [-pi, pi].
Definition: FourVector.h:196
void operator+=(const FourVector &rhs)
Arithmetic operator +=.
Definition: FourVector.h:261
void operator/=(const double rhs)
Arithmetic operator /= by scalar.
Definition: FourVector.h:282
double length() const
Magnitude of spatial (x, y, z) 3-vector.
Definition: FourVector.h:146
double x() const
x-component of position/displacement
Definition: FourVector.h:81
double perp2() const
Squared magnitude of (x, y) vector.
Definition: FourVector.h:150
double delta_r_rap(const FourVector &v) const
R-rap-distance separation dR = sqrt(dphi^2 + drap^2)
Definition: FourVector.h:226
double pt() const
Transverse momentum.
Definition: FourVector.h:163
FourVector(const FourVector &v)
Copy constructor.
Definition: FourVector.h:46
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:248
double p3mod2() const
Squared magnitude of p3 = (px, py, pz) vector.
Definition: FourVector.h:157
double pseudoRapidity() const
Definition: FourVector.h:184
double phi() const
Azimuthal angle.
Definition: FourVector.h:170
double delta_rap(const FourVector &v) const
Rapidity separation.
Definition: FourVector.h:208
double abs_eta() const
Absolute pseudorapidity.
Definition: FourVector.h:178
double length2() const
Squared magnitude of (x, y, z) 3-vector.
Definition: FourVector.h:144
double rho() const
Magnitude of spatial (x, y, z) 3-vector, for HepMC2 compatibility.
Definition: FourVector.h:148
double m() const
Invariant mass. Returns -sqrt(-m) if e^2 - P^2 is negative.
Definition: FourVector.h:167
double get_component(const int i) const
get component of position/displacement
Definition: FourVector.h:70
double perp() const
Magnitude of (x, y) vector.
Definition: FourVector.h:152
double y() const
y-component of position/displacement
Definition: FourVector.h:88
bool operator!=(const FourVector &rhs) const
Inequality.
Definition: FourVector.h:241
void set_z(double zz)
Set z-component of position/displacement.
Definition: FourVector.h:97
void set_component(const int i, const double x)
set component of position/displacement
Definition: FourVector.h:62
FourVector operator*(const double rhs) const
Arithmetic operator * by scalar.
Definition: FourVector.h:252
double m_v1
px or x. Interpretation depends on accessors used
Definition: FourVector.h:301
void set(double x1, double x2, double x3, double x4)
Set all FourVector fields, in order x,y,z,t.
Definition: FourVector.h:54
void setPx(double pxx)
Definition: FourVector.h:114
double z() const
z-component of position/displacement
Definition: FourVector.h:95
double delta_eta(const FourVector &v) const
Pseudorapidity separation.
Definition: FourVector.h:205
double rap() const
Rapidity.
Definition: FourVector.h:176
void set_y(double yy)
Set y-component of position/displacement.
Definition: FourVector.h:90
void set_e(double ee)
Set energy component of momentum.
Definition: FourVector.h:133
double theta() const
Polar angle w.r.t. z direction.
Definition: FourVector.h:172
void setZ(double zz)
Definition: FourVector.h:99
FourVector operator/(const double rhs) const
Arithmetic operator / by scalar.
Definition: FourVector.h:256
void set_py(double pyy)
Set y-component of momentum.
Definition: FourVector.h:119
FourVector operator+(const FourVector &rhs) const
Arithmetic operator +.
Definition: FourVector.h:244
static const FourVector & ZERO_VECTOR()
Static null FourVector = (0,0,0,0)
Definition: FourVector.h:293
HepMC3 main namespace.
Feature< Feature_type > abs(const Feature< Feature_type > &input)
Obtain the absolute value of a Feature. This works as you'd expect. If foo is a valid Feature,...
Definition: Feature.h:323
double delta_eta(const FourVector &a, const FourVector &b)
Pseudorapidity separation between vecs a and b.
Definition: FourVector.h:316
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:328
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:322
double delta_rap(const FourVector &a, const FourVector &b)
Rapidity separation between vecs a and b.
Definition: FourVector.h:319
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:325
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:331
double delta_phi(const FourVector &a, const FourVector &b)
Signed azimuthal angle separation in [-pi, pi] between vecs a and b.
Definition: FourVector.h:313