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