12 #ifndef ROOT_TEveVector
13 #define ROOT_TEveVector
25 template <
typename TT>
31 TEveVectorT() : fX(0), fY(0), fZ(0) {}
32 template <
typename OO>
33 TEveVectorT(
const TEveVectorT<OO>& v) : fX(v.fX), fY(v.fY), fZ(v.fZ) {}
34 TEveVectorT(
const Float_t* v) : fX(v[0]), fY(v[1]), fZ(v[2]) {}
35 TEveVectorT(
const Double_t* v) : fX(v[0]), fY(v[1]), fZ(v[2]) {}
36 TEveVectorT(TT x, TT y, TT z) : fX(x), fY(y), fZ(z) {}
46 if (offsetof(TEveVectorT, fZ) != offsetof(TEveVectorT, fX) + 2 *
sizeof(TT))
47 Error(
"TEveVectorT",
"Subsequent nembers cannot be accessed as array!");
52 if (offsetof(TEveVectorT, fZ) != offsetof(TEveVectorT, fX) + 2 *
sizeof(TT))
53 Error(
"TEveVectorT",
"Subsequent nembers cannot be accessed as array!");
59 static_assert(offsetof(TEveVectorT, fZ) == offsetof(TEveVectorT, fX) + 2 *
sizeof(TT),
60 "Subsequent nembers cannot be accessed as array!");
65 static_assert(offsetof(TEveVectorT, fZ) == offsetof(TEveVectorT, fX) + 2 *
sizeof(TT),
66 "Subsequent nembers cannot be accessed as array!");
71 operator const TT*()
const {
return Arr(); }
72 operator TT*() {
return Arr(); }
74 TT operator [] (Int_t idx)
const {
return Arr()[idx]; }
75 TT& operator [] (Int_t idx) {
return Arr()[idx]; }
77 TEveVectorT& operator*=(TT s) { fX *= s; fY *= s; fZ *= s;
return *
this; }
78 TEveVectorT& operator+=(
const TEveVectorT& v) { fX += v.fX; fY += v.fY; fZ += v.fZ;
return *
this; }
79 TEveVectorT& operator-=(
const TEveVectorT& v) { fX -= v.fX; fY -= v.fY; fZ -= v.fZ;
return *
this; }
81 void Set(
const Float_t* v) { fX = v[0]; fY = v[1]; fZ = v[2]; }
82 void Set(
const Double_t* v) { fX = v[0]; fY = v[1]; fZ = v[2]; }
83 void Set(TT x, TT y, TT z) { fX = x; fY = y; fZ = z; }
84 void Set(
const TVector3& v);
86 template <
typename OO>
87 void Set(
const TEveVectorT<OO>& v) { fX = v.fX; fY = v.fY; fZ = v.fZ; }
89 void NegateXYZ() { fX = - fX; fY = -fY; fZ = -fZ; }
90 TT Normalize(TT length=1);
97 TT Mag2()
const {
return fX*fX + fY*fY + fZ*fZ; }
98 TT Mag()
const {
return TMath::Sqrt(Mag2()); }
100 TT Perp2()
const {
return fX*fX + fY*fY; }
101 TT Perp()
const {
return TMath::Sqrt(Perp2()); }
102 TT R()
const {
return Perp(); }
104 TT Distance(
const TEveVectorT& v)
const;
105 TT SquareDistance(
const TEveVectorT& v)
const;
107 TT Dot(
const TEveVectorT& a)
const;
109 TEveVectorT Cross(
const TEveVectorT& a)
const;
111 TEveVectorT& Sub(
const TEveVectorT& a,
const TEveVectorT& b);
112 TEveVectorT& Mult(
const TEveVectorT& a, TT af);
114 TEveVectorT Orthogonal()
const;
115 void OrthoNormBase(TEveVectorT& a, TEveVectorT& b)
const;
117 Bool_t IsZero()
const {
return fX == 0 && fY == 0 && fZ == 0; }
119 ClassDefNV(TEveVectorT, 2);
122 typedef TEveVectorT<Float_t> TEveVector;
123 typedef TEveVectorT<Float_t> TEveVectorF;
124 typedef TEveVectorT<Double_t> TEveVectorD;
127 template<
typename TT>
128 inline TT TEveVectorT<TT>::Phi()
const
130 return fX == 0 && fY == 0 ? 0 : TMath::ATan2(fY, fX);
134 template<
typename TT>
135 inline TT TEveVectorT<TT>::Theta()
const
137 return fX == 0 && fY == 0 && fZ == 0 ? 0 : TMath::ATan2(Perp(), fZ);
141 template<
typename TT>
142 inline TT TEveVectorT<TT>::CosTheta()
const
144 Float_t ptot = Mag();
return ptot == 0 ? 1 : fZ/ptot;
148 template<
typename TT>
149 inline TT TEveVectorT<TT>::Distance(
const TEveVectorT& b)
const
151 return TMath::Sqrt((fX - b.fX)*(fX - b.fX) +
152 (fY - b.fY)*(fY - b.fY) +
153 (fZ - b.fZ)*(fZ - b.fZ));
157 template<
typename TT>
158 inline TT TEveVectorT<TT>::SquareDistance(
const TEveVectorT& b)
const
160 return ((fX - b.fX) * (fX - b.fX) +
161 (fY - b.fY) * (fY - b.fY) +
162 (fZ - b.fZ) * (fZ - b.fZ));
166 template<
typename TT>
167 inline TT TEveVectorT<TT>::Dot(
const TEveVectorT& a)
const
169 return a.fX*fX + a.fY*fY + a.fZ*fZ;
173 template<
typename TT>
174 inline TEveVectorT<TT> TEveVectorT<TT>::Cross(
const TEveVectorT<TT>& a)
const
177 r.fX = fY * a.fZ - fZ * a.fY;
178 r.fY = fZ * a.fX - fX * a.fZ;
179 r.fZ = fX * a.fY - fY * a.fX;
184 template<
typename TT>
185 inline TEveVectorT<TT>& TEveVectorT<TT>::Sub(
const TEveVectorT<TT>& a,
const TEveVectorT<TT>& b)
194 template<
typename TT>
195 inline TEveVectorT<TT>& TEveVectorT<TT>::Mult(
const TEveVectorT<TT>& a, TT af)
204 template<
typename TT>
205 inline TEveVectorT<TT> operator+(
const TEveVectorT<TT>& a,
const TEveVectorT<TT>& b)
207 TEveVectorT<TT> r(a);
212 template<
typename TT>
213 inline TEveVectorT<TT> operator-(
const TEveVectorT<TT>& a,
const TEveVectorT<TT>& b)
215 TEveVectorT<TT> r(a);
220 template<
typename TT>
221 inline TEveVectorT<TT> operator*(
const TEveVectorT<TT>& a, TT b)
223 TEveVectorT<TT> r(a);
228 template<
typename TT>
229 inline TEveVectorT<TT> operator*(TT b,
const TEveVectorT<TT>& a)
231 TEveVectorT<TT> r(a);
240 template <
typename TT>
241 class TEveVector4T :
public TEveVectorT<TT>
243 typedef TEveVectorT<TT> TP;
248 TEveVector4T() : TP(), fT(0) {}
249 template <
typename OO>
250 TEveVector4T(
const TEveVectorT<OO>& v) : TP(v.fX, v.fY, v.fZ), fT(0) {}
251 template <
typename OO>
252 TEveVector4T(
const TEveVectorT<OO>& v, Float_t t) : TP(v.fX, v.fY, v.fZ), fT(t) {}
253 template <
typename OO>
254 TEveVector4T(
const TEveVector4T<OO>& v) : TP(v.fX, v.fY, v.fZ), fT(v.fT) {}
255 TEveVector4T(
const Float_t* v) : TP(v), fT(v[3]) {}
256 TEveVector4T(
const Double_t* v) : TP(v), fT(v[3]) {}
257 TEveVector4T(TT x, TT y, TT z, TT t=0) : TP(x, y, z), fT(t) {}
261 TEveVector4T& operator*=(TT s) { TP::operator*=(s); fT *= s;
return *
this; }
262 TEveVector4T& operator+=(
const TEveVector4T& v) { TP::operator+=(v); fT += v.fT;
return *
this; }
263 TEveVector4T& operator-=(
const TEveVector4T& v) { TP::operator-=(v); fT -= v.fT;
return *
this; }
265 using TP::operator+=;
266 using TP::operator-=;
268 ClassDefNV(TEveVector4T, 1);
271 typedef TEveVector4T<Float_t> TEveVector4;
272 typedef TEveVector4T<Float_t> TEveVector4F;
273 typedef TEveVector4T<Double_t> TEveVector4D;
276 template<
typename TT>
277 inline TEveVector4T<TT> operator+(
const TEveVector4T<TT>& a,
const TEveVector4T<TT>& b)
279 return TEveVector4T<TT>(a.fX + b.fX, a.fY + b.fY, a.fZ + b.fZ, a.fT + b.fT);
283 template<
typename TT>
284 inline TEveVector4T<TT> operator-(
const TEveVector4T<TT>& a,
const TEveVector4T<TT>& b)
286 return TEveVector4T<TT>(a.fX - b.fX, a.fY - b.fY, a.fZ - b.fZ, a.fT - b.fT);
290 template<
typename TT>
291 inline TEveVector4T<TT> operator*(
const TEveVector4T<TT>& a, TT b)
293 return TEveVector4T<TT>(a.fX*b, a.fY*b, a.fZ*b, a.fT*b);
297 template<
typename TT>
298 inline TEveVector4T<TT> operator*(TT b,
const TEveVector4T<TT>& a)
300 return TEveVector4T<TT>(a.fX*b, a.fY*b, a.fZ*b, a.fT*b);
308 template <
typename TT>
314 TEveVector2T() : fX(0), fY(0) {}
315 template <
typename OO>
316 TEveVector2T(
const TEveVector2T<OO>& v) : fX(v.fX), fY(v.fY) {}
317 TEveVector2T(
const Float_t* v) : fX(v[0]), fY(v[1]) {}
318 TEveVector2T(
const Double_t* v) : fX(v[0]), fY(v[1]) {}
319 TEveVector2T(TT x, TT y) : fX(x), fY(y) {}
323 operator const TT*()
const {
return &fX; }
324 operator TT*() {
return &fX; }
326 TEveVector2T& operator*=(TT s) { fX *= s; fY *= s;
return *
this; }
327 TEveVector2T& operator+=(
const TEveVector2T& v) { fX += v.fX; fY += v.fY;
return *
this; }
328 TEveVector2T& operator-=(
const TEveVector2T& v) { fX -= v.fX; fY -= v.fY;
return *
this; }
330 TT& operator[](Int_t idx) {
return (&fX)[idx]; }
331 TT operator[](Int_t idx)
const {
return (&fX)[idx]; }
333 const TT* Arr()
const {
return &fX; }
334 TT* Arr() {
return &fX; }
336 void Set(
const Float_t* v) { fX = v[0]; fY = v[1]; }
337 void Set(
const Double_t* v) { fX = v[0]; fY = v[1]; }
338 void Set(TT x, TT y) { fX = x; fY = y; }
340 template <
typename OO>
341 void Set(
const TEveVector2T<OO>& v) { fX = v.fX; fY = v.fY; }
343 void NegateXY() { fX = - fX; fY = -fY; }
344 void Normalize(TT length=1);
348 TT Mag2()
const {
return fX*fX + fY*fY;}
349 TT Mag()
const {
return TMath::Sqrt(Mag2());}
351 TT Distance(
const TEveVector2T& v)
const;
352 TT SquareDistance(
const TEveVector2T& v)
const;
354 TT Dot(
const TEveVector2T& a)
const;
355 TT Cross(
const TEveVector2T& a)
const;
357 TEveVector2T& Sub(
const TEveVector2T& p,
const TEveVector2T& q);
359 TEveVector2T& Mult(
const TEveVector2T& a, TT af);
361 ClassDefNV(TEveVector2T, 1);
364 typedef TEveVector2T<Float_t> TEveVector2;
365 typedef TEveVector2T<Float_t> TEveVector2F;
366 typedef TEveVector2T<Double_t> TEveVector2D;
369 template<
typename TT>
370 inline TT TEveVector2T<TT>::Phi()
const
372 return fX == 0.0 && fY == 0.0 ? 0.0 : TMath::ATan2(fY, fX);
376 template<
typename TT>
377 inline TT TEveVector2T<TT>::Distance(
const TEveVector2T<TT>& b)
const
379 return TMath::Sqrt((fX - b.fX)*(fX - b.fX) +
380 (fY - b.fY)*(fY - b.fY));
384 template<
typename TT>
385 inline TT TEveVector2T<TT>::SquareDistance(
const TEveVector2T<TT>& b)
const
387 return ((fX - b.fX) * (fX - b.fX) +
388 (fY - b.fY) * (fY - b.fY));
392 template<
typename TT>
393 inline TT TEveVector2T<TT>::Dot(
const TEveVector2T<TT>& a)
const
395 return a.fX*fX + a.fY*fY;
399 template<
typename TT>
400 inline TT TEveVector2T<TT>::Cross(
const TEveVector2T<TT>& a)
const
402 return fX * a.fY - fY * a.fX;
406 template<
typename TT>
407 inline TEveVector2T<TT>& TEveVector2T<TT>::Sub(
const TEveVector2T<TT>& p,
const TEveVector2T<TT>& q)
415 template<
typename TT>
416 inline TEveVector2T<TT>& TEveVector2T<TT>::Mult(
const TEveVector2T<TT>& a, TT af)
424 template<
typename TT>
425 inline TEveVector2T<TT> operator+(
const TEveVector2T<TT>& a,
const TEveVector2T<TT>& b)
427 TEveVector2T<TT> r(a);
432 template<
typename TT>
433 inline TEveVector2T<TT> operator-(
const TEveVector2T<TT>& a,
const TEveVector2T<TT>& b)
435 TEveVector2T<TT> r(a);
440 template<
typename TT>
441 inline TEveVector2T<TT> operator*(
const TEveVector2T<TT>& a, TT b)
443 TEveVector2T<TT> r(a);
448 template<
typename TT>
449 inline TEveVector2T<TT> operator*(TT b,
const TEveVector2T<TT>& a)
451 TEveVector2T<TT> r(a);