CMS 3D CMS Logo

extBasic3DVector.h
Go to the documentation of this file.
1 #ifndef GeometryVector_newBasic3DVector_h
2 #define GeometryVector_newBasic3DVector_h
3 
11 #include <iosfwd>
12 #include <cmath>
13 
15  inline float __attribute__((always_inline)) __attribute__((pure)) eta(float x, float y, float z) {
16  float t(z / std::sqrt(x * x + y * y));
17  return ::asinhf(t);
18  }
19  inline double __attribute__((always_inline)) __attribute__((pure)) eta(double x, double y, double z) {
20  double t(z / std::sqrt(x * x + y * y));
21  return ::asinh(t);
22  }
23  inline long double __attribute__((always_inline)) __attribute__((pure)) eta(long double x,
24  long double y,
25  long double z) {
26  long double t(z / std::sqrt(x * x + y * y));
27  return ::asinhl(t);
28  }
29 } // namespace detailsBasic3DVector
30 
31 template <typename T>
33 public:
34  typedef T ScalarType;
39  typedef Spherical Polar; // synonym
40 
45  Basic3DVector() : v{0, 0, 0, 0} {}
46 
48  Basic3DVector(const Basic3DVector& p) : v(p.v) {}
49 
51  Basic3DVector& operator=(const Basic3DVector&) = default;
52 
54  template <class U>
55  Basic3DVector(const Basic3DVector<U>& p) : v{T(p.v[0]), T(p.v[1]), T(p.v[2]), T(p.v[3])} {}
56 
58  Basic3DVector(const Basic2DVector<T>& p) : v{p.x(), p.y(), 0} {}
59 
68  template <class OtherPoint>
69  explicit Basic3DVector(const OtherPoint& p) : v{T(p.x()), T(p.y()), T(p.z())} {}
70 
71  // constructor from Vec4
72  Basic3DVector(MathVector const& iv) : v(iv) {}
73 
74  template <class U>
75  Basic3DVector(Vec4<U> const& iv) : v{T(iv[0]), T(iv[1]), T(iv[2]), T(iv[3])} {}
76 
78  Basic3DVector(const T& x, const T& y, const T& z, const T& w = 0) : v{x, y, z, w} {}
79 
84  template <typename U>
85  Basic3DVector(const Geom::Theta<U>& theta, const Geom::Phi<U>& phi, const T& r) {
86  Polar p(theta.value(), phi.value(), r);
87  v[0] = p.x();
88  v[1] = p.y();
89  v[2] = p.z();
90  }
91 
92  MathVector const& mathVector() const { return v; }
93  MathVector& mathVector() { return v; }
94 
95  T operator[](int i) const { return v[i]; }
96 
98  T x() const { return v[0]; }
99 
101  T y() const { return v[1]; }
102 
104  T z() const { return v[2]; }
105 
106  T w() const { return v[3]; }
107 
109 
110  // equality
111  bool operator==(const Basic3DVector& rh) const {
112  auto res = v == rh.v;
113  return res[0] & res[1] & res[2] & res[3];
114  }
115 
117  T mag2() const { return ::dot(v, v); }
118 
120  T mag() const { return std::sqrt(mag2()); }
121 
123  T perp2() const { return ::dot2(v, v); }
124 
126  T perp() const { return std::sqrt(perp2()); }
127 
129  T transverse() const { return perp(); }
130 
135  T barePhi() const { return std::atan2(y(), x()); }
136  Geom::Phi<T> phi() const { return Geom::Phi<T>(barePhi()); }
137 
142  T bareTheta() const { return std::atan2(perp(), z()); }
143  Geom::Theta<T> theta() const { return Geom::Theta<T>(std::atan2(perp(), z())); }
144 
149  // T eta() const { return -log( tan( theta()/2.));}
150  T eta() const { return detailsBasic3DVector::eta(x(), y(), z()); } // correct
151 
155  Basic3DVector unit() const {
156  T my_mag = mag2();
157  return LIKELY(0 != my_mag) ? (*this) * (T(1) / std::sqrt(my_mag)) : *this;
158  }
162  template <class U>
164  v = v + p.v;
165  return *this;
166  }
167 
170  template <class U>
172  v = v - p.v;
173  return *this;
174  }
175 
177  Basic3DVector operator-() const { return Basic3DVector(-v); }
181  v = t * v;
182  return *this;
183  }
184 
187  //t = T(1)/t;
188  v = v / t;
189  return *this;
190  }
191 
193  T dot(const Basic3DVector& rh) const { return ::dot(v, rh.v); }
194 
200  template <class U>
204  }
205 
214  template <class U>
218  }
219 
220 public:
222 } __attribute__((aligned(16)));
223 
224 namespace geometryDetails {
225  std::ostream& print3D(std::ostream& s, double x, double y, double z);
226 }
229 template <class T>
230 inline std::ostream& operator<<(std::ostream& s, const Basic3DVector<T>& v) {
231  return geometryDetails::print3D(s, v.x(), v.y(), v.z());
232 }
233 
235 template <class T>
237  return a.v + b.v;
238 }
239 template <class T>
241  return a.v - b.v;
242 }
244 template <class T, class U>
246  const Basic3DVector<U>& b) {
248  return RT(a).v + RT(b).v;
249 }
250 
251 template <class T, class U>
253  const Basic3DVector<U>& b) {
255  return RT(a).v - RT(b).v;
256 }
257 
259 template <class T>
260 inline T operator*(const Basic3DVector<T>& v1, const Basic3DVector<T>& v2) {
261  return v1.dot(v2);
262 }
263 
265 template <class T, class U>
267  return v1.dot(v2);
268 }
269 
273 template <class T>
275  return v.v * t;
276 }
277 
279 template <class T>
281  return v.v * t;
282 }
283 
284 template <class T, typename S>
286  return static_cast<T>(t) * v;
287 }
288 
289 template <class T, typename S>
291  return static_cast<T>(t) * v;
292 }
293 
297 template <class T>
299  return v.v / t;
300 }
301 
302 template <class T, typename S>
304  // T t = S(1)/s; return v*t;
305  T t = s;
306  return v / t;
307 }
308 
311 
312 // add long double specialization
313 #include "Basic3DVectorLD.h"
314 
315 #endif // GeometryVector_Basic3DVector_h
Basic3DVector(MathVector const &iv)
Basic3DVector< T > operator+(const Basic3DVector< T > &a, const Basic3DVector< T > &b)
vector sum and subtraction of vectors of possibly different precision
T x() const
Cartesian x coordinate.
T perp2() const
Squared magnitude of transverse component.
Basic3DVector(const T &x, const T &y, const T &z, const T &w=0)
construct from cartesian coordinates
T dot(const Basic3DVector &rh) const
Scalar product, or "dot" product, with a vector of same type.
T mag() const
The vector magnitude. Equivalent to sqrt(vec.mag2())
class Basic3DVector __attribute__((aligned(16)))
Basic3DVector(const Basic3DVector &p)
Copy constructor from same type. Should not be needed but for gcc bug 12685.
bool operator==(const Basic3DVector &rh) const
Basic2DVector< T > xy() const
#define LIKELY(x)
Definition: Likely.h:20
Vec4< T > MathVector
Vec cross3(Vec x, Vec y)
Definition: ExtVec.h:100
bool int lh
Definition: SIMDVec.h:27
T y() const
Cartesian y coordinate.
T operator[](int i) const
float float float z
Definition: Electron.h:6
Geom::Theta< T > theta() const
Basic3DVector & operator/=(T t)
Scaling by a scalar value (division)
Basic3DVector(const Geom::Theta< U > &theta, const Geom::Phi< U > &phi, const T &r)
T bareTheta() const
T sqrt(T t)
Definition: SSEVec.h:23
Basic3DVector & operator*=(T t)
Scaling by a scalar value (multiplication)
Basic3DVector cross(const Basic3DVector &lh) const
Vector product, or "cross" product, with a vector of same type.
Basic3DVector & operator+=(const Basic3DVector< U > &p)
T transverse() const
Another name for perp()
ExtVec< T, 4 > Vec4
Definition: ExtVec.h:60
Basic3DVector operator-() const
Unary minus, returns a vector with components (-x(),-y(),-z())
Basic2DVector< T > xy() const
MathVector const & mathVector() const
Basic3DVector< T > operator/(const Basic3DVector< T > &v, T t)
Basic3DVector & operator-=(const Basic3DVector< U > &p)
T operator*(const Basic3DVector< T > &v1, const Basic3DVector< T > &v2)
scalar product of vectors of same precision
Basic3DVector(Vec4< U > const &iv)
T perp() const
Magnitude of transverse component.
Basic3DVector(const Basic3DVector< U > &p)
Copy constructor and implicit conversion from Basic3DVector of different precision.
Basic3DVector(const Basic2DVector< T > &p)
constructor from 2D vector (X and Y from 2D vector, z set to zero)
Basic3DVector< double > Basic3DVectorD
T z() const
Cartesian z coordinate.
Geom::Spherical2Cartesian< T > Spherical
double b
Definition: hdecay.h:120
Vec4< T > VectorType
MathVector & mathVector()
Geom::Cylindrical2Cartesian< T > Cylindrical
T mag2() const
The vector magnitude squared. Equivalent to vec.dot(vec)
float __attribute__((always_inline)) __attribute__((pure)) eta(float x
double a
Definition: hdecay.h:121
Basic3DVector operator-() const
Unary minus, returns a vector with components (-x(),-y(),-z())
float x
Basic3DVector unit() const
Basic3DVector< float > Basic3DVectorF
Geom::Phi< T > phi() const
std::ostream & print3D(std::ostream &s, double x, double y, double z)
Definition: print.cc:4
T eta() const
long double T
T dot(const Basic3DVector &rh) const
Scalar product, or "dot" product, with a vector of same type.
Basic3DVector(const OtherPoint &p)
Definition: Phi.h:52
auto dot2(V1 x, V2 y) -> typename std::remove_reference< decltype(x[0])>::type
Definition: ExtVec.h:152
Basic3DVector & operator=(const Basic3DVector &)=default
Assignment operator.
Vec4< T > v
MPlex< T, D1, D2, N > atan2(const MPlex< T, D1, D2, N > &y, const MPlex< T, D1, D2, N > &x)
Definition: Matriplex.h:648