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