// @(#)root/eve:$Id$ // Author: Matevz Tadel 2007 /************************************************************************* * Copyright (C) 1995-2007, Rene Brun and Fons Rademakers. * * All rights reserved. * * * * For the licensing terms see $ROOTSYS/LICENSE. * * For the list of contributors see $ROOTSYS/README/CREDITS. * *************************************************************************/ #ifndef ROOT_TEveVector #define ROOT_TEveVector #include "TMath.h" class TVector3; //============================================================================== // TEveVectorT //============================================================================== template class TEveVectorT { public: TT fX, fY, fZ; // Components of the vector. TEveVectorT() : fX(0), fY(0), fZ(0) {} template TEveVectorT(const TEveVectorT& v) : fX(v.fX), fY(v.fY), fZ(v.fZ) {} TEveVectorT(const Float_t* v) : fX(v[0]), fY(v[1]), fZ(v[2]) {} TEveVectorT(const Double_t* v) : fX(v[0]), fY(v[1]), fZ(v[2]) {} TEveVectorT(TT x, TT y, TT z) : fX(x), fY(y), fZ(z) {} void Dump() const; operator const TT*() const { return &fX; } operator TT*() { return &fX; } TT operator [] (Int_t idx) const { return (&fX)[idx]; } TT& operator [] (Int_t idx) { return (&fX)[idx]; } const TT* Arr() const { return &fX; } TT* Arr() { return &fX; } TEveVectorT& operator*=(TT s) { fX *= s; fY *= s; fZ *= s; return *this; } TEveVectorT& operator+=(const TEveVectorT& v) { fX += v.fX; fY += v.fY; fZ += v.fZ; return *this; } TEveVectorT& operator-=(const TEveVectorT& v) { fX -= v.fX; fY -= v.fY; fZ -= v.fZ; return *this; } void Set(const Float_t* v) { fX = v[0]; fY = v[1]; fZ = v[2]; } void Set(const Double_t* v) { fX = v[0]; fY = v[1]; fZ = v[2]; } void Set(TT x, TT y, TT z) { fX = x; fY = y; fZ = z; } void Set(const TVector3& v); template void Set(const TEveVectorT& v) { fX = v.fX; fY = v.fY; fZ = v.fZ; } void NegateXYZ() { fX = - fX; fY = -fY; fZ = -fZ; } TT Normalize(TT length=1); TT Phi() const; TT Theta() const; TT CosTheta() const; TT Eta() const; TT Mag2() const { return fX*fX + fY*fY + fZ*fZ; } TT Mag() const { return TMath::Sqrt(Mag2()); } TT Perp2() const { return fX*fX + fY*fY; } TT Perp() const { return TMath::Sqrt(Perp2()); } TT R() const { return Perp(); } TT Distance(const TEveVectorT& v) const; TT SquareDistance(const TEveVectorT& v) const; TT Dot(const TEveVectorT& a) const; TEveVectorT Cross(const TEveVectorT& a) const; TEveVectorT& Sub(const TEveVectorT& a, const TEveVectorT& b); TEveVectorT& Mult(const TEveVectorT& a, TT af); TEveVectorT Orthogonal() const; void OrthoNormBase(TEveVectorT& a, TEveVectorT& b) const; Bool_t IsZero() const { return fX == 0 && fY == 0 && fZ == 0; } ClassDefNV(TEveVectorT, 2); // A three-vector template without TObject inheritance and virtual functions. }; typedef TEveVectorT TEveVector; typedef TEveVectorT TEveVectorF; typedef TEveVectorT TEveVectorD; //______________________________________________________________________________ template inline TT TEveVectorT::Phi() const { return fX == 0 && fY == 0 ? 0 : TMath::ATan2(fY, fX); } //______________________________________________________________________________ template inline TT TEveVectorT::Theta() const { return fX == 0 && fY == 0 && fZ == 0 ? 0 : TMath::ATan2(Perp(), fZ); } //______________________________________________________________________________ template inline TT TEveVectorT::CosTheta() const { Float_t ptot = Mag(); return ptot == 0 ? 1 : fZ/ptot; } //______________________________________________________________________________ template inline TT TEveVectorT::Distance(const TEveVectorT& b) const { return TMath::Sqrt((fX - b.fX)*(fX - b.fX) + (fY - b.fY)*(fY - b.fY) + (fZ - b.fZ)*(fZ - b.fZ)); } //______________________________________________________________________________ template inline TT TEveVectorT::SquareDistance(const TEveVectorT& b) const { return ((fX - b.fX) * (fX - b.fX) + (fY - b.fY) * (fY - b.fY) + (fZ - b.fZ) * (fZ - b.fZ)); } //______________________________________________________________________________ template inline TT TEveVectorT::Dot(const TEveVectorT& a) const { return a.fX*fX + a.fY*fY + a.fZ*fZ; } //______________________________________________________________________________ template inline TEveVectorT TEveVectorT::Cross(const TEveVectorT& a) const { TEveVectorT r; r.fX = fY * a.fZ - fZ * a.fY; r.fY = fZ * a.fX - fX * a.fZ; r.fZ = fX * a.fY - fY * a.fX; return r; } //______________________________________________________________________________ template inline TEveVectorT& TEveVectorT::Sub(const TEveVectorT& a, const TEveVectorT& b) { fX = a.fX - b.fX; fY = a.fY - b.fY; fZ = a.fZ - b.fZ; return *this; } //______________________________________________________________________________ template inline TEveVectorT& TEveVectorT::Mult(const TEveVectorT& a, TT af) { fX = a.fX * af; fY = a.fY * af; fZ = a.fZ * af; return *this; } //______________________________________________________________________________ template inline TEveVectorT operator+(const TEveVectorT& a, const TEveVectorT& b) { TEveVectorT r(a); return r += b; } //______________________________________________________________________________ template inline TEveVectorT operator-(const TEveVectorT& a, const TEveVectorT& b) { TEveVectorT r(a); return r -= b; } //______________________________________________________________________________ template inline TEveVectorT operator*(const TEveVectorT& a, TT b) { TEveVectorT r(a); return r *= b; } //______________________________________________________________________________ template inline TEveVectorT operator*(TT b, const TEveVectorT& a) { TEveVectorT r(a); return r *= b; } //============================================================================== // TEveVector4T //============================================================================== template class TEveVector4T : public TEveVectorT { typedef TEveVectorT TP; public: TT fT; TEveVector4T() : TP(), fT(0) {} template TEveVector4T(const TEveVectorT& v) : TP(v.fX, v.fY, v.fZ), fT(0) {} template TEveVector4T(const TEveVectorT& v, Float_t t) : TP(v.fX, v.fY, v.fZ), fT(t) {} template TEveVector4T(const TEveVector4T& v) : TP(v.fX, v.fY, v.fZ), fT(v.fT) {} TEveVector4T(const Float_t* v) : TP(v), fT(v[3]) {} TEveVector4T(const Double_t* v) : TP(v), fT(v[3]) {} TEveVector4T(TT x, TT y, TT z, TT t=0) : TP(x, y, z), fT(t) {} void Dump() const; TEveVector4T& operator*=(TT s) { TP::operator*=(s); fT *= s; return *this; } TEveVector4T& operator+=(const TEveVector4T& v) { TP::operator+=(v); fT += v.fT; return *this; } TEveVector4T& operator-=(const TEveVector4T& v) { TP::operator-=(v); fT -= v.fT; return *this; } using TP::operator+=; using TP::operator-=; ClassDefNV(TEveVector4T, 1); // A four-vector template without TObject inheritance and virtual functions. }; typedef TEveVector4T TEveVector4; typedef TEveVector4T TEveVector4F; typedef TEveVector4T TEveVector4D; //______________________________________________________________________________ template inline TEveVector4T operator+(const TEveVector4T& a, const TEveVector4T& b) { return TEveVector4T(a.fX + b.fX, a.fY + b.fY, a.fZ + b.fZ, a.fT + b.fT); } //______________________________________________________________________________ template inline TEveVector4T operator-(const TEveVector4T& a, const TEveVector4T& b) { return TEveVector4T(a.fX - b.fX, a.fY - b.fY, a.fZ - b.fZ, a.fT - b.fT); } //______________________________________________________________________________ template inline TEveVector4T operator*(const TEveVector4T& a, TT b) { return TEveVector4T(a.fX*b, a.fY*b, a.fZ*b, a.fT*b); } //______________________________________________________________________________ template inline TEveVector4T operator*(TT b, const TEveVector4T& a) { return TEveVector4T(a.fX*b, a.fY*b, a.fZ*b, a.fT*b); } //============================================================================== // TEveVector2T //============================================================================== template class TEveVector2T { public: TT fX, fY; // Components of the point. TEveVector2T() : fX(0), fY(0) {} template TEveVector2T(const TEveVector2T& v) : fX(v.fX), fY(v.fY) {} TEveVector2T(const Float_t* v) : fX(v[0]), fY(v[1]) {} TEveVector2T(const Double_t* v) : fX(v[0]), fY(v[1]) {} TEveVector2T(TT x, TT y) : fX(x), fY(y) {} void Dump() const; operator const TT*() const { return &fX; } operator TT*() { return &fX; } TEveVector2T& operator*=(TT s) { fX *= s; fY *= s; return *this; } TEveVector2T& operator+=(const TEveVector2T& v) { fX += v.fX; fY += v.fY; return *this; } TEveVector2T& operator-=(const TEveVector2T& v) { fX -= v.fX; fY -= v.fY; return *this; } TT& operator[](Int_t idx) { return (&fX)[idx]; } TT operator[](Int_t idx) const { return (&fX)[idx]; } const TT* Arr() const { return &fX; } TT* Arr() { return &fX; } void Set(const Float_t* v) { fX = v[0]; fY = v[1]; } void Set(const Double_t* v) { fX = v[0]; fY = v[1]; } void Set(TT x, TT y) { fX = x; fY = y; } template void Set(const TEveVector2T& v) { fX = v.fX; fY = v.fY; } void NegateXY() { fX = - fX; fY = -fY; } void Normalize(TT length=1); TT Phi() const; TT Mag2() const { return fX*fX + fY*fY;} TT Mag() const { return TMath::Sqrt(Mag2());} TT Distance(const TEveVector2T& v) const; TT SquareDistance(const TEveVector2T& v) const; TT Dot(const TEveVector2T& a) const; TT Cross(const TEveVector2T& a) const; TEveVector2T& Sub(const TEveVector2T& p, const TEveVector2T& q); TEveVector2T& Mult(const TEveVector2T& a, TT af); ClassDefNV(TEveVector2T, 1); // // A two-vector template without TObject inheritance and virtual functions. }; typedef TEveVector2T TEveVector2; typedef TEveVector2T TEveVector2F; typedef TEveVector2T TEveVector2D; //______________________________________________________________________________ template inline TT TEveVector2T::Phi() const { return fX == 0.0 && fY == 0.0 ? 0.0 : TMath::ATan2(fY, fX); } //______________________________________________________________________________ template inline TT TEveVector2T::Distance( const TEveVector2T& b) const { return TMath::Sqrt((fX - b.fX)*(fX - b.fX) + (fY - b.fY)*(fY - b.fY)); } //______________________________________________________________________________ template inline TT TEveVector2T::SquareDistance(const TEveVector2T& b) const { return ((fX - b.fX) * (fX - b.fX) + (fY - b.fY) * (fY - b.fY)); } //______________________________________________________________________________ template inline TT TEveVector2T::Dot(const TEveVector2T& a) const { return a.fX*fX + a.fY*fY; } //______________________________________________________________________________ template inline TT TEveVector2T::Cross(const TEveVector2T& a) const { return fX * a.fY - fY * a.fX; } //______________________________________________________________________________ template inline TEveVector2T& TEveVector2T::Sub(const TEveVector2T& p, const TEveVector2T& q) { fX = p.fX - q.fX; fY = p.fY - q.fY; return *this; } //______________________________________________________________________________ template inline TEveVector2T& TEveVector2T::Mult(const TEveVector2T& a, TT af) { fX = a.fX * af; fY = a.fY * af; return *this; } //______________________________________________________________________________ template inline TEveVector2T operator+(const TEveVector2T& a, const TEveVector2T& b) { TEveVector2T r(a); return r += b; } //______________________________________________________________________________ template inline TEveVector2T operator-(const TEveVector2T& a, const TEveVector2T& b) { TEveVector2T r(a); return r -= b; } //______________________________________________________________________________ template inline TEveVector2T operator*(const TEveVector2T& a, TT b) { TEveVector2T r(a); return r *= b; } //______________________________________________________________________________ template inline TEveVector2T operator*(TT b, const TEveVector2T& a) { TEveVector2T r(a); return r *= b; } #endif