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))
24  eta(long double x, long double y, long double z) {
25  long double t(z / std::sqrt(x * x + y * y));
26  return ::asinhl(t);
27  }
28 } // namespace detailsBasic3DVector
29 
30 template <typename T>
32 public:
33  typedef T ScalarType;
38  typedef Spherical Polar; // synonym
39 
44  Basic3DVector() : v{0, 0, 0, 0} {}
45 
47  Basic3DVector(const Basic3DVector& p) : v(p.v) {}
48 
50  Basic3DVector& operator=(const Basic3DVector&) = default;
51 
53  template <class U>
54  Basic3DVector(const Basic3DVector<U>& p) : v{T(p.v[0]), T(p.v[1]), T(p.v[2]), T(p.v[3])} {}
55 
57  Basic3DVector(const Basic2DVector<T>& p) : v{p.x(), p.y(), 0} {}
58 
67  template <class OtherPoint>
68  explicit Basic3DVector(const OtherPoint& p) : v{T(p.x()), T(p.y()), T(p.z())} {}
69 
70  // constructor from Vec4
71  Basic3DVector(MathVector const& iv) : v(iv) {}
72 
73  template <class U>
74  Basic3DVector(Vec4<U> const& iv) : v{T(iv[0]), T(iv[1]), T(iv[2]), T(iv[3])} {}
75 
77  Basic3DVector(const T& x, const T& y, const T& z, const T& w = 0) : v{x, y, z, w} {}
78 
83  template <typename U>
84  Basic3DVector(const Geom::Theta<U>& theta, const Geom::Phi<U>& phi, const T& r) {
85  Polar p(theta.value(), phi.value(), r);
86  v[0] = p.x();
87  v[1] = p.y();
88  v[2] = p.z();
89  }
90 
91  MathVector const& mathVector() const { return v; }
92  MathVector& mathVector() { return v; }
93 
94  T operator[](int i) const { return v[i]; }
95 
97  T x() const { return v[0]; }
98 
100  T y() const { return v[1]; }
101 
103  T z() const { return v[2]; }
104 
105  T w() const { return v[3]; }
106 
108 
109  // equality
110  bool operator==(const Basic3DVector& rh) const {
111  auto res = v == rh.v;
112  return res[0] & res[1] & res[2] & res[3];
113  }
114 
116  T mag2() const { return ::dot(v, v); }
117 
119  T mag() const { return std::sqrt(mag2()); }
120 
122  T perp2() const { return ::dot2(v, v); }
123 
125  T perp() const { return std::sqrt(perp2()); }
126 
128  T transverse() const { return perp(); }
129 
134  T barePhi() const { return std::atan2(y(), x()); }
135  Geom::Phi<T> phi() const { return Geom::Phi<T>(barePhi()); }
136 
141  T bareTheta() const { return std::atan2(perp(), z()); }
142  Geom::Theta<T> theta() const { return Geom::Theta<T>(std::atan2(perp(), z())); }
143 
148  // T eta() const { return -log( tan( theta()/2.));}
149  T eta() const { return detailsBasic3DVector::eta(x(), y(), z()); } // correct
150 
154  Basic3DVector unit() const {
155  T my_mag = mag2();
156  return LIKELY(0 != my_mag) ? (*this) * (T(1) / std::sqrt(my_mag)) : *this;
157  }
161  template <class U>
163  v = v + p.v;
164  return *this;
165  }
166 
169  template <class U>
171  v = v - p.v;
172  return *this;
173  }
174 
176  Basic3DVector operator-() const { return Basic3DVector(-v); }
177 
180  v = t * v;
181  return *this;
182  }
183 
186  //t = T(1)/t;
187  v = v / t;
188  return *this;
189  }
190 
192  T dot(const Basic3DVector& rh) const { return ::dot(v, rh.v); }
193 
199  template <class U>
203  }
204 
207 
213  template <class U>
217  }
219 public:
221 } __attribute__((aligned(16)));
222 
223 namespace geometryDetails {
224  std::ostream& print3D(std::ostream& s, double x, double y, double z);
225 }
226 
228 template <class T>
229 inline std::ostream& operator<<(std::ostream& s, const Basic3DVector<T>& v) {
230  return geometryDetails::print3D(s, v.x(), v.y(), v.z());
231 }
232 
234 template <class T>
236  return a.v + b.v;
237 }
238 template <class T>
240  return a.v - b.v;
241 }
242 
243 template <class T, class U>
245  const Basic3DVector<U>& b) {
247  return RT(a).v + RT(b).v;
248 }
250 template <class T, class U>
252  const Basic3DVector<U>& b) {
254  return RT(a).v - RT(b).v;
255 }
256 
258 template <class T>
259 inline T operator*(const Basic3DVector<T>& v1, const Basic3DVector<T>& v2) {
260  return v1.dot(v2);
261 }
262 
264 template <class T, class U>
266  return v1.dot(v2);
267 }
268 
272 template <class T>
274  return v.v * t;
275 }
276 
278 template <class T>
280  return v.v * t;
281 }
282 
283 template <class T, typename S>
285  return static_cast<T>(t) * v;
286 }
287 
288 template <class T, typename S>
290  return static_cast<T>(t) * v;
291 }
292 
296 template <class T>
298  return v.v / t;
299 }
301 template <class T, typename S>
303  // T t = S(1)/s; return v*t;
304  T t = s;
305  return v / t;
306 }
307 
310 
311 // add long double specialization
312 #include "Basic3DVectorLD.h"
313 
314 #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)
PreciseFloatType< T, U >::Type dot(const Basic3DVector< U > &lh) const
Basic3DVector< typename PreciseFloatType< T, U >::Type > cross(const Basic3DVector< U > &lh) const
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:5
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