TEveVector.h

Go to the documentation of this file.
00001 // @(#)root/eve:$Id: TEveVector.h 35221 2010-09-10 11:46:37Z matevz $
00002 // Author: Matevz Tadel 2007
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2007, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 #ifndef ROOT_TEveVector
00013 #define ROOT_TEveVector
00014 
00015 #include "TMath.h"
00016 
00017 class TVector3;
00018 
00019 
00020 //==============================================================================
00021 // TEveVectorT
00022 //==============================================================================
00023 
00024 template <typename TT>
00025 class TEveVectorT
00026 {
00027 public:
00028    TT fX, fY, fZ; // Components of the vector.
00029 
00030    TEveVectorT() : fX(0), fY(0), fZ(0) {}
00031    template <typename OO>
00032    TEveVectorT(const TEveVectorT<OO>& v) : fX(v.fX), fY(v.fY), fZ(v.fZ) {}
00033    TEveVectorT(const Float_t*  v) : fX(v[0]), fY(v[1]), fZ(v[2]) {}
00034    TEveVectorT(const Double_t* v) : fX(v[0]), fY(v[1]), fZ(v[2]) {}
00035    TEveVectorT(TT x, TT y, TT  z) : fX(x), fY(y), fZ(z) {}
00036 
00037    void Dump() const;
00038 
00039    operator const TT*() const { return &fX; }
00040    operator       TT*()       { return &fX; }
00041 
00042    TT  operator [] (Int_t idx) const { return (&fX)[idx]; }
00043    TT& operator [] (Int_t idx)       { return (&fX)[idx]; }
00044 
00045    const TT* Arr() const { return &fX; }
00046    TT* Arr()             { return &fX; }
00047 
00048    TEveVectorT& operator*=(TT s)                 { fX *= s;    fY *= s;    fZ *= s;    return *this; }
00049    TEveVectorT& operator+=(const TEveVectorT& v) { fX += v.fX; fY += v.fY; fZ += v.fZ; return *this; }
00050    TEveVectorT& operator-=(const TEveVectorT& v) { fX -= v.fX; fY -= v.fY; fZ -= v.fZ; return *this; }
00051 
00052    void Set(const Float_t*  v) { fX = v[0]; fY = v[1]; fZ = v[2]; }
00053    void Set(const Double_t* v) { fX = v[0]; fY = v[1]; fZ = v[2]; }
00054    void Set(TT x, TT  y, TT z) { fX = x; fY = y; fZ = z; }
00055    void Set(const TVector3& v);
00056 
00057    template <typename OO>
00058    void Set(const TEveVectorT<OO>& v) { fX = v.fX;  fY = v.fY;  fZ = v.fZ; }
00059 
00060    void NegateXYZ() { fX = - fX; fY = -fY; fZ = -fZ; }
00061    TT   Normalize(TT length=1);
00062 
00063    TT   Phi()      const;
00064    TT   Theta()    const;
00065    TT   CosTheta() const;
00066    TT   Eta()      const;
00067 
00068    TT   Mag2()  const { return fX*fX + fY*fY + fZ*fZ; }
00069    TT   Mag()   const { return TMath::Sqrt(Mag2());   }
00070 
00071    TT   Perp2() const { return fX*fX + fY*fY;        }
00072    TT   Perp()  const { return TMath::Sqrt(Perp2()); }
00073    TT   R()     const { return Perp();               }
00074 
00075    TT   Distance(const TEveVectorT& v) const;
00076    TT   SquareDistance(const TEveVectorT& v) const;
00077 
00078    TT   Dot(const TEveVectorT& a) const;
00079 
00080    TEveVectorT  Cross(const TEveVectorT& a) const;
00081 
00082    TEveVectorT& Sub(const TEveVectorT& a, const TEveVectorT& b);
00083    TEveVectorT& Mult(const TEveVectorT& a, TT af);
00084 
00085    TEveVectorT  Orthogonal() const;
00086    void         OrthoNormBase(TEveVectorT& a, TEveVectorT& b) const;
00087 
00088    Bool_t       IsZero() const { return fX == 0 && fY == 0 && fZ == 0; }
00089 
00090    ClassDefNV(TEveVectorT, 2); // A three-vector template without TObject inheritance and virtual functions.
00091 };
00092 
00093 typedef TEveVectorT<Float_t>  TEveVector;
00094 typedef TEveVectorT<Float_t>  TEveVectorF;
00095 typedef TEveVectorT<Double_t> TEveVectorD;
00096 
00097 //______________________________________________________________________________
00098 template<typename TT>
00099 inline TT TEveVectorT<TT>::Phi() const
00100 {
00101    return fX == 0 && fY == 0 ? 0 : TMath::ATan2(fY, fX);
00102 }
00103 
00104 //______________________________________________________________________________
00105 template<typename TT> 
00106 inline TT TEveVectorT<TT>::Theta() const
00107 {
00108    return fX == 0 && fY == 0 && fZ == 0 ? 0 : TMath::ATan2(Perp(), fZ);
00109 }
00110 
00111 //______________________________________________________________________________
00112 template<typename TT>
00113 inline TT TEveVectorT<TT>::CosTheta() const
00114 {
00115    Float_t ptot = Mag(); return ptot == 0 ? 1 : fZ/ptot;
00116 }
00117 
00118 //______________________________________________________________________________
00119 template<typename TT>
00120 inline TT TEveVectorT<TT>::Distance(const TEveVectorT& b) const
00121 {
00122    return TMath::Sqrt((fX - b.fX)*(fX - b.fX) +
00123                       (fY - b.fY)*(fY - b.fY) +
00124                       (fZ - b.fZ)*(fZ - b.fZ));
00125 }
00126 
00127 //______________________________________________________________________________
00128 template<typename TT>
00129 inline TT TEveVectorT<TT>::SquareDistance(const TEveVectorT& b) const
00130 {
00131    return ((fX - b.fX) * (fX - b.fX) +
00132            (fY - b.fY) * (fY - b.fY) +
00133            (fZ - b.fZ) * (fZ - b.fZ));
00134 }
00135 
00136 //______________________________________________________________________________
00137 template<typename TT>
00138 inline TT TEveVectorT<TT>::Dot(const TEveVectorT& a) const
00139 {
00140    return a.fX*fX + a.fY*fY + a.fZ*fZ;
00141 }
00142 
00143 //______________________________________________________________________________
00144 template<typename TT>
00145 inline TEveVectorT<TT> TEveVectorT<TT>::Cross(const TEveVectorT<TT>& a) const
00146 {
00147    TEveVectorT<TT> r;
00148    r.fX = fY * a.fZ - fZ * a.fY;
00149    r.fY = fZ * a.fX - fX * a.fZ;
00150    r.fZ = fX * a.fY - fY * a.fX;
00151    return r;
00152 }
00153 
00154 //______________________________________________________________________________
00155 template<typename TT>
00156 inline TEveVectorT<TT>& TEveVectorT<TT>::Sub(const TEveVectorT<TT>& a, const TEveVectorT<TT>& b)
00157 {
00158    fX = a.fX - b.fX;
00159    fY = a.fY - b.fY;
00160    fZ = a.fZ - b.fZ;
00161    return *this;
00162 }
00163 
00164 //______________________________________________________________________________
00165 template<typename TT>
00166 inline TEveVectorT<TT>& TEveVectorT<TT>::Mult(const TEveVectorT<TT>& a, TT af)
00167 {
00168    fX = a.fX * af;
00169    fY = a.fY * af;
00170    fZ = a.fZ * af;
00171    return *this;
00172 }
00173 
00174 //______________________________________________________________________________
00175 template<typename TT>
00176 inline TEveVectorT<TT> operator+(const TEveVectorT<TT>& a, const TEveVectorT<TT>& b)
00177 {
00178    TEveVectorT<TT> r(a);
00179    return r += b;
00180 }
00181 
00182 //______________________________________________________________________________
00183 template<typename TT>
00184 inline TEveVectorT<TT> operator-(const TEveVectorT<TT>& a, const TEveVectorT<TT>& b)
00185 {
00186    TEveVectorT<TT> r(a);
00187    return r -= b;
00188 }
00189 
00190 //______________________________________________________________________________
00191 template<typename TT>
00192 inline TEveVectorT<TT> operator*(const TEveVectorT<TT>& a, TT b)
00193 {
00194    TEveVectorT<TT> r(a);
00195    return r *= b;
00196 }
00197 
00198 //______________________________________________________________________________
00199 template<typename TT>
00200 inline TEveVectorT<TT> operator*(TT b, const TEveVectorT<TT>& a)
00201 {
00202    TEveVectorT<TT> r(a);
00203    return r *= b;
00204 }
00205 
00206 
00207 //==============================================================================
00208 // TEveVector4T
00209 //==============================================================================
00210 
00211 template <typename TT>
00212 class TEveVector4T : public TEveVectorT<TT>
00213 {
00214    typedef TEveVectorT<TT> TP;
00215 
00216 public:
00217    TT fT;
00218 
00219    TEveVector4T() : TP(),  fT(0) {}
00220    template <typename OO>
00221    TEveVector4T(const TEveVectorT<OO>& v) : TP(v.fX, v.fY, v.fZ), fT(0) {}
00222    template <typename OO>
00223    TEveVector4T(const TEveVectorT<OO>& v, Float_t t) : TP(v.fX, v.fY, v.fZ), fT(t) {}
00224    template <typename OO>
00225    TEveVector4T(const TEveVector4T<OO>& v) : TP(v.fX, v.fY, v.fZ), fT(v.fT) {}
00226    TEveVector4T(const Float_t*  v) : TP(v), fT(v[3]) {}
00227    TEveVector4T(const Double_t* v) : TP(v), fT(v[3]) {}
00228    TEveVector4T(TT x, TT y, TT z, TT t=0) : TP(x, y, z), fT(t) {}
00229 
00230    void Dump() const;
00231 
00232    TEveVector4T& operator*=(TT s)                  { TP::operator*=(s); fT *= s;    return *this; }
00233    TEveVector4T& operator+=(const TEveVector4T& v) { TP::operator+=(v); fT += v.fT; return *this; }
00234    TEveVector4T& operator-=(const TEveVector4T& v) { TP::operator-=(v); fT -= v.fT; return *this; }
00235 
00236    using TP::operator+=;
00237    using TP::operator-=;
00238 
00239    ClassDefNV(TEveVector4T, 1); // A four-vector template without TObject inheritance and virtual functions.
00240 };
00241 
00242 typedef TEveVector4T<Float_t>  TEveVector4;
00243 typedef TEveVector4T<Float_t>  TEveVector4F;
00244 typedef TEveVector4T<Double_t> TEveVector4D;
00245 
00246 //______________________________________________________________________________
00247 template<typename TT>
00248 inline TEveVector4T<TT> operator+(const TEveVector4T<TT>& a, const TEveVector4T<TT>& b)
00249 {
00250    return TEveVector4T<TT>(a.fX + b.fX, a.fY + b.fY, a.fZ + b.fZ, a.fT + b.fT);
00251 }
00252 
00253 //______________________________________________________________________________
00254 template<typename TT>
00255 inline TEveVector4T<TT> operator-(const TEveVector4T<TT>& a, const TEveVector4T<TT>& b)
00256 {
00257    return TEveVector4T<TT>(a.fX - b.fX, a.fY - b.fY, a.fZ - b.fZ, a.fT - b.fT);
00258 }
00259 
00260 //______________________________________________________________________________
00261 template<typename TT>
00262 inline TEveVector4T<TT> operator*(const TEveVector4T<TT>& a, TT b)
00263 {
00264    return TEveVector4T<TT>(a.fX*b, a.fY*b, a.fZ*b, a.fT*b);
00265 }
00266 
00267 //______________________________________________________________________________
00268 template<typename TT>
00269 inline TEveVector4T<TT> operator*(TT b, const TEveVector4T<TT>& a)
00270 {
00271    return TEveVector4T<TT>(a.fX*b, a.fY*b, a.fZ*b, a.fT*b);
00272 }
00273 
00274 
00275 //==============================================================================
00276 // TEveVector2T
00277 //==============================================================================
00278 
00279 template <typename TT>
00280 class TEveVector2T
00281 {
00282 public:
00283    TT fX, fY; // Components of the point.
00284 
00285    TEveVector2T() : fX(0), fY(0) {}
00286    template <typename OO>
00287    TEveVector2T(const TEveVector2T<OO>& v) : fX(v.fX), fY(v.fY) {}
00288    TEveVector2T(const Float_t* v)  : fX(v[0]), fY(v[1]) {}
00289    TEveVector2T(const Double_t* v) : fX(v[0]), fY(v[1]) {}
00290    TEveVector2T(TT x, TT y) : fX(x), fY(y)    {}
00291 
00292    void Dump() const;
00293 
00294    operator const TT*() const { return &fX; }
00295    operator       TT*()       { return &fX; }
00296 
00297    TEveVector2T& operator*=(TT s)                  { fX *= s;    fY *= s;    return *this; }
00298    TEveVector2T& operator+=(const TEveVector2T& v) { fX += v.fX; fY += v.fY; return *this; }
00299    TEveVector2T& operator-=(const TEveVector2T& v) { fX -= v.fX; fY -= v.fY; return *this; }
00300 
00301    TT& operator[](Int_t idx)       { return (&fX)[idx]; }
00302    TT  operator[](Int_t idx) const { return (&fX)[idx]; }
00303 
00304    const TT* Arr() const { return &fX; }
00305    TT* Arr()             { return &fX; }
00306 
00307    void Set(const Float_t*  v) { fX = v[0]; fY = v[1]; }
00308    void Set(const Double_t* v) { fX = v[0]; fY = v[1]; }
00309    void Set(TT x, TT y) { fX = x; fY = y; }
00310 
00311    template <typename OO>
00312    void Set(const TEveVector2T<OO>& v) { fX = v.fX; fY = v.fY; }
00313 
00314    void NegateXY() { fX = - fX; fY = -fY; }
00315    void Normalize(TT length=1);
00316 
00317    TT Phi()  const;
00318 
00319    TT Mag2() const { return fX*fX + fY*fY;}
00320    TT Mag()  const { return TMath::Sqrt(Mag2());}
00321 
00322    TT Distance(const TEveVector2T& v) const;
00323    TT SquareDistance(const TEveVector2T& v) const;
00324 
00325    TT Dot(const TEveVector2T& a) const;
00326    TT Cross(const TEveVector2T& a) const;
00327 
00328    TEveVector2T& Sub(const TEveVector2T& p, const TEveVector2T& q);
00329 
00330    TEveVector2T& Mult(const TEveVector2T& a, TT af);
00331 
00332    ClassDefNV(TEveVector2T, 1); // // A two-vector template without TObject inheritance and virtual functions.
00333 };
00334 
00335 typedef TEveVector2T<Float_t>  TEveVector2;
00336 typedef TEveVector2T<Float_t>  TEveVector2F;
00337 typedef TEveVector2T<Double_t> TEveVector2D;
00338 
00339 //______________________________________________________________________________
00340 template<typename TT>
00341 inline TT TEveVector2T<TT>::Phi() const
00342 {
00343    return fX == 0.0 && fY == 0.0 ? 0.0 : TMath::ATan2(fY, fX);
00344 }
00345 
00346 //______________________________________________________________________________
00347 template<typename TT>
00348 inline TT TEveVector2T<TT>::Distance( const TEveVector2T<TT>& b) const
00349 {
00350    return TMath::Sqrt((fX - b.fX)*(fX - b.fX) +
00351                       (fY - b.fY)*(fY - b.fY));
00352 }
00353 
00354 //______________________________________________________________________________
00355 template<typename TT>
00356 inline TT TEveVector2T<TT>::SquareDistance(const TEveVector2T<TT>& b) const
00357 {
00358    return ((fX - b.fX) * (fX - b.fX) +
00359            (fY - b.fY) * (fY - b.fY));
00360 }
00361 
00362 //______________________________________________________________________________
00363 template<typename TT>
00364 inline TT TEveVector2T<TT>::Dot(const TEveVector2T<TT>& a) const
00365 {
00366    return a.fX*fX + a.fY*fY;
00367 }
00368 
00369 //______________________________________________________________________________
00370 template<typename TT>
00371 inline TT TEveVector2T<TT>::Cross(const TEveVector2T<TT>& a) const
00372 {
00373    return fX * a.fY - fY * a.fX;
00374 }
00375 
00376 //______________________________________________________________________________
00377 template<typename TT>
00378 inline TEveVector2T<TT>& TEveVector2T<TT>::Sub(const TEveVector2T<TT>& p, const TEveVector2T<TT>& q)
00379 {
00380    fX = p.fX - q.fX;
00381    fY = p.fY - q.fY;
00382    return *this;
00383 }
00384 
00385 //______________________________________________________________________________
00386 template<typename TT>
00387 inline TEveVector2T<TT>& TEveVector2T<TT>::Mult(const TEveVector2T<TT>& a, TT af)
00388 {
00389    fX = a.fX * af;
00390    fY = a.fY * af;
00391    return *this;
00392 }
00393 
00394 //______________________________________________________________________________
00395 template<typename TT>
00396 inline TEveVector2T<TT> operator+(const TEveVector2T<TT>& a, const TEveVector2T<TT>& b)
00397 {
00398    TEveVector2T<TT> r(a);
00399    return r += b;
00400 }
00401 
00402 //______________________________________________________________________________
00403 template<typename TT>
00404 inline TEveVector2T<TT> operator-(const TEveVector2T<TT>& a, const TEveVector2T<TT>& b)
00405 {
00406    TEveVector2T<TT> r(a);
00407    return r -= b;
00408 }
00409 
00410 //______________________________________________________________________________
00411 template<typename TT>
00412 inline TEveVector2T<TT> operator*(const TEveVector2T<TT>& a, TT b)
00413 {
00414    TEveVector2T<TT> r(a);
00415    return r *= b;
00416 }
00417 
00418 //______________________________________________________________________________
00419 template<typename TT>
00420 inline TEveVector2T<TT> operator*(TT b, const TEveVector2T<TT>& a)
00421 {
00422    TEveVector2T<TT> r(a);
00423    return r *= b;
00424 }
00425 
00426 #endif

Generated on Tue Jul 5 14:15:05 2011 for ROOT_528-00b_version by  doxygen 1.5.1