| 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 |
| 17 |
namespace 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 |
*/ |
| 35 |
class FourVector { |
| 36 |
public: |
| 37 |
|
| 38 |
/** @brief Default constructor */ |
| 39 |
FourVector() |
| 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 */ |
| 45 |
FourVector(const FourVector & v) |
| 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 |
|
| 280 |
private: |
| 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 |
| 294 |
inline 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 |
| 297 |
inline 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 |
| 300 |
inline 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 |
| 303 |
inline 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 |
| 306 |
inline 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 |
| 309 |
inline 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 |
| 312 |
inline 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 |