12 #ifndef ROOT7_REveVector
13 #define ROOT7_REveVector
21 namespace Experimental {
28 template <
typename TT>
31 TT fX{0}, fY{0}, fZ{0};
33 REveVectorT() =
default;
34 template <
typename OO>
35 REveVectorT(
const REveVectorT<OO>& v) : fX(v.fX), fY(v.fY), fZ(v.fZ) {}
36 REveVectorT(
const Float_t* v) : fX(v[0]), fY(v[1]), fZ(v[2]) {}
37 REveVectorT(
const Double_t* v) : fX(v[0]), fY(v[1]), fZ(v[2]) {}
38 REveVectorT(TT x, TT y, TT z) : fX(x), fY(y), fZ(z) {}
45 if (offsetof(REveVectorT, fZ) == offsetof(REveVectorT, fX) + 2 *
sizeof(TT))
46 Error(
"REveVectorT",
"Subsequent members cannot be accessed as array!");
51 if (offsetof(REveVectorT, fZ) == offsetof(REveVectorT, fX) + 2 *
sizeof(TT))
52 Error(
"REveVectorT",
"Subsequent members cannot be accessed as array!");
58 static_assert(offsetof(REveVectorT, fZ) == offsetof(REveVectorT, fX) + 2 *
sizeof(TT),
59 "Subsequent members cannot be accessed as array!");
64 static_assert(offsetof(REveVectorT, fZ) == offsetof(REveVectorT, fX) + 2 *
sizeof(TT),
65 "Subsequent members cannot be accessed as array!");
70 operator const TT*()
const {
return Arr(); }
71 operator TT*() {
return Arr(); }
73 TT operator [] (Int_t idx)
const {
return Arr()[idx]; }
74 TT& operator [] (Int_t idx) {
return Arr()[idx]; }
76 REveVectorT& operator*=(TT s) { fX *= s; fY *= s; fZ *= s;
return *
this; }
77 REveVectorT& operator+=(
const REveVectorT& v) { fX += v.fX; fY += v.fY; fZ += v.fZ;
return *
this; }
78 REveVectorT& operator-=(
const REveVectorT& v) { fX -= v.fX; fY -= v.fY; fZ -= v.fZ;
return *
this; }
80 void Set(
const Float_t* v) { fX = v[0]; fY = v[1]; fZ = v[2]; }
81 void Set(
const Double_t* v) { fX = v[0]; fY = v[1]; fZ = v[2]; }
82 void Set(TT x, TT y, TT z) { fX = x; fY = y; fZ = z; }
83 void Set(
const TVector3& v);
85 template <
typename OO>
86 void Set(
const REveVectorT<OO>& v) { fX = v.fX; fY = v.fY; fZ = v.fZ; }
88 void NegateXYZ() { fX = - fX; fY = -fY; fZ = -fZ; }
89 TT Normalize(TT length=1);
96 TT Mag2()
const {
return fX*fX + fY*fY + fZ*fZ; }
97 TT Mag()
const {
return TMath::Sqrt(Mag2()); }
99 TT Perp2()
const {
return fX*fX + fY*fY; }
100 TT Perp()
const {
return TMath::Sqrt(Perp2()); }
101 TT R()
const {
return Perp(); }
103 TT Distance(
const REveVectorT& v)
const;
104 TT SquareDistance(
const REveVectorT& v)
const;
106 TT Dot(
const REveVectorT& a)
const;
108 REveVectorT Cross(
const REveVectorT& a)
const;
110 REveVectorT& Sub(
const REveVectorT& a,
const REveVectorT& b);
111 REveVectorT& Mult(
const REveVectorT& a, TT af);
113 REveVectorT Orthogonal()
const;
114 void OrthoNormBase(REveVectorT& a, REveVectorT& b)
const;
116 Bool_t IsZero()
const {
return fX == 0 && fY == 0 && fZ == 0; }
119 typedef REveVectorT<Float_t> REveVector;
120 typedef REveVectorT<Float_t> REveVectorF;
121 typedef REveVectorT<Double_t> REveVectorD;
124 template<
typename TT>
125 inline TT REveVectorT<TT>::Phi()
const
127 return fX == 0 && fY == 0 ? 0 : TMath::ATan2(fY, fX);
131 template<
typename TT>
132 inline TT REveVectorT<TT>::Theta()
const
134 return fX == 0 && fY == 0 && fZ == 0 ? 0 : TMath::ATan2(Perp(), fZ);
138 template<
typename TT>
139 inline TT REveVectorT<TT>::CosTheta()
const
141 Float_t ptot = Mag();
return ptot == 0 ? 1 : fZ/ptot;
145 template<
typename TT>
146 inline TT REveVectorT<TT>::Distance(
const REveVectorT& b)
const
148 return TMath::Sqrt((fX - b.fX)*(fX - b.fX) +
149 (fY - b.fY)*(fY - b.fY) +
150 (fZ - b.fZ)*(fZ - b.fZ));
154 template<
typename TT>
155 inline TT REveVectorT<TT>::SquareDistance(
const REveVectorT& b)
const
157 return ((fX - b.fX) * (fX - b.fX) +
158 (fY - b.fY) * (fY - b.fY) +
159 (fZ - b.fZ) * (fZ - b.fZ));
163 template<
typename TT>
164 inline TT REveVectorT<TT>::Dot(
const REveVectorT& a)
const
166 return a.fX*fX + a.fY*fY + a.fZ*fZ;
170 template<
typename TT>
171 inline REveVectorT<TT> REveVectorT<TT>::Cross(
const REveVectorT<TT>& a)
const
174 r.fX = fY * a.fZ - fZ * a.fY;
175 r.fY = fZ * a.fX - fX * a.fZ;
176 r.fZ = fX * a.fY - fY * a.fX;
181 template<
typename TT>
182 inline REveVectorT<TT>& REveVectorT<TT>::Sub(
const REveVectorT<TT>& a,
const REveVectorT<TT>& b)
191 template<
typename TT>
192 inline REveVectorT<TT>& REveVectorT<TT>::Mult(
const REveVectorT<TT>& a, TT af)
201 template<
typename TT>
202 inline REveVectorT<TT> operator+(
const REveVectorT<TT>& a,
const REveVectorT<TT>& b)
204 REveVectorT<TT> r(a);
209 template<
typename TT>
210 inline REveVectorT<TT> operator-(
const REveVectorT<TT>& a,
const REveVectorT<TT>& b)
212 REveVectorT<TT> r(a);
217 template<
typename TT>
218 inline REveVectorT<TT> operator*(
const REveVectorT<TT>& a, TT b)
220 REveVectorT<TT> r(a);
225 template<
typename TT>
226 inline REveVectorT<TT> operator*(TT b,
const REveVectorT<TT>& a)
228 REveVectorT<TT> r(a);
237 template <
typename TT>
238 class REveVector4T :
public REveVectorT<TT>
240 typedef REveVectorT<TT> TP;
245 REveVector4T() : TP(), fT(0) {}
246 template <
typename OO>
247 REveVector4T(
const REveVectorT<OO>& v) : TP(v.fX, v.fY, v.fZ), fT(0) {}
248 template <
typename OO>
249 REveVector4T(
const REveVectorT<OO>& v, Float_t t) : TP(v.fX, v.fY, v.fZ), fT(t) {}
250 template <
typename OO>
251 REveVector4T(
const REveVector4T<OO>& v) : TP(v.fX, v.fY, v.fZ), fT(v.fT) {}
252 REveVector4T(
const Float_t* v) : TP(v), fT(v[3]) {}
253 REveVector4T(
const Double_t* v) : TP(v), fT(v[3]) {}
254 REveVector4T(TT x, TT y, TT z, TT t=0) : TP(x, y, z), fT(t) {}
258 REveVector4T& operator*=(TT s) { TP::operator*=(s); fT *= s;
return *
this; }
259 REveVector4T& operator+=(
const REveVector4T& v) { TP::operator+=(v); fT += v.fT;
return *
this; }
260 REveVector4T& operator-=(
const REveVector4T& v) { TP::operator-=(v); fT -= v.fT;
return *
this; }
262 using TP::operator+=;
263 using TP::operator-=;
266 typedef REveVector4T<Float_t> REveVector4;
267 typedef REveVector4T<Float_t> REveVector4F;
268 typedef REveVector4T<Double_t> REveVector4D;
271 template<
typename TT>
272 inline REveVector4T<TT> operator+(
const REveVector4T<TT>& a,
const REveVector4T<TT>& b)
274 return REveVector4T<TT>(a.fX + b.fX, a.fY + b.fY, a.fZ + b.fZ, a.fT + b.fT);
278 template<
typename TT>
279 inline REveVector4T<TT> operator-(
const REveVector4T<TT>& a,
const REveVector4T<TT>& b)
281 return REveVector4T<TT>(a.fX - b.fX, a.fY - b.fY, a.fZ - b.fZ, a.fT - b.fT);
285 template<
typename TT>
286 inline REveVector4T<TT> operator*(
const REveVector4T<TT>& a, TT b)
288 return REveVector4T<TT>(a.fX*b, a.fY*b, a.fZ*b, a.fT*b);
292 template<
typename TT>
293 inline REveVector4T<TT> operator*(TT b,
const REveVector4T<TT>& a)
295 return REveVector4T<TT>(a.fX*b, a.fY*b, a.fZ*b, a.fT*b);
303 template <
typename TT>
309 REveVector2T() : fX(0), fY(0) {}
310 template <
typename OO>
311 REveVector2T(
const REveVector2T<OO>& v) : fX(v.fX), fY(v.fY) {}
312 REveVector2T(
const Float_t* v) : fX(v[0]), fY(v[1]) {}
313 REveVector2T(
const Double_t* v) : fX(v[0]), fY(v[1]) {}
314 REveVector2T(TT x, TT y) : fX(x), fY(y) {}
318 operator const TT*()
const {
return &fX; }
319 operator TT*() {
return &fX; }
321 REveVector2T& operator*=(TT s) { fX *= s; fY *= s;
return *
this; }
322 REveVector2T& operator+=(
const REveVector2T& v) { fX += v.fX; fY += v.fY;
return *
this; }
323 REveVector2T& operator-=(
const REveVector2T& v) { fX -= v.fX; fY -= v.fY;
return *
this; }
325 TT& operator[](Int_t idx) {
return (&fX)[idx]; }
326 TT operator[](Int_t idx)
const {
return (&fX)[idx]; }
328 const TT* Arr()
const {
return &fX; }
329 TT* Arr() {
return &fX; }
331 void Set(
const Float_t* v) { fX = v[0]; fY = v[1]; }
332 void Set(
const Double_t* v) { fX = v[0]; fY = v[1]; }
333 void Set(TT x, TT y) { fX = x; fY = y; }
335 template <
typename OO>
336 void Set(
const REveVector2T<OO>& v) { fX = v.fX; fY = v.fY; }
338 void NegateXY() { fX = - fX; fY = -fY; }
339 void Normalize(TT length=1);
343 TT Mag2()
const {
return fX*fX + fY*fY;}
344 TT Mag()
const {
return TMath::Sqrt(Mag2());}
346 TT Distance(
const REveVector2T& v)
const;
347 TT SquareDistance(
const REveVector2T& v)
const;
349 TT Dot(
const REveVector2T& a)
const;
350 TT Cross(
const REveVector2T& a)
const;
352 REveVector2T& Sub(
const REveVector2T& p,
const REveVector2T& q);
354 REveVector2T& Mult(
const REveVector2T& a, TT af);
357 typedef REveVector2T<Float_t> REveVector2;
358 typedef REveVector2T<Float_t> REveVector2F;
359 typedef REveVector2T<Double_t> REveVector2D;
362 template<
typename TT>
363 inline TT REveVector2T<TT>::Phi()
const
365 return fX == 0.0 && fY == 0.0 ? 0.0 : TMath::ATan2(fY, fX);
369 template<
typename TT>
370 inline TT REveVector2T<TT>::Distance(
const REveVector2T<TT>& b)
const
372 return TMath::Sqrt((fX - b.fX)*(fX - b.fX) +
373 (fY - b.fY)*(fY - b.fY));
377 template<
typename TT>
378 inline TT REveVector2T<TT>::SquareDistance(
const REveVector2T<TT>& b)
const
380 return ((fX - b.fX) * (fX - b.fX) +
381 (fY - b.fY) * (fY - b.fY));
385 template<
typename TT>
386 inline TT REveVector2T<TT>::Dot(
const REveVector2T<TT>& a)
const
388 return a.fX*fX + a.fY*fY;
392 template<
typename TT>
393 inline TT REveVector2T<TT>::Cross(
const REveVector2T<TT>& a)
const
395 return fX * a.fY - fY * a.fX;
399 template<
typename TT>
400 inline REveVector2T<TT>& REveVector2T<TT>::Sub(
const REveVector2T<TT>& p,
const REveVector2T<TT>& q)
408 template<
typename TT>
409 inline REveVector2T<TT>& REveVector2T<TT>::Mult(
const REveVector2T<TT>& a, TT af)
417 template<
typename TT>
418 inline REveVector2T<TT> operator+(
const REveVector2T<TT>& a,
const REveVector2T<TT>& b)
420 REveVector2T<TT> r(a);
425 template<
typename TT>
426 inline REveVector2T<TT> operator-(
const REveVector2T<TT>& a,
const REveVector2T<TT>& b)
428 REveVector2T<TT> r(a);
433 template<
typename TT>
434 inline REveVector2T<TT> operator*(
const REveVector2T<TT>& a, TT b)
436 REveVector2T<TT> r(a);
441 template<
typename TT>
442 inline REveVector2T<TT> operator*(TT b,
const REveVector2T<TT>& a)
444 REveVector2T<TT> r(a);