CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10_patch2/src/DataFormats/GeometryVector/interface/oldBasic3DVector.h

Go to the documentation of this file.
00001 #ifndef GeometryVector_oldBasic3DVector_h
00002 #define GeometryVector_oldBasic3DVector_h
00003 #if defined(__CINT__)  && !defined(__REFLEX__)
00004 #define __REFLEX__
00005 #endif
00006 #include "DataFormats/GeometryVector/interface/Basic2DVector.h"
00007 #include "DataFormats/GeometryVector/interface/Theta.h"
00008 #include "DataFormats/GeometryVector/interface/Phi.h"
00009 #include "DataFormats/GeometryVector/interface/PreciseFloatType.h"
00010 #include "DataFormats/GeometryVector/interface/CoordinateSets.h"
00011 #ifndef __REFLEX__ 
00012 #include "DataFormats/Math/interface/SSEVec.h"
00013 #endif
00014 #include <iosfwd>
00015 #include <cmath>
00016 
00017 namespace detailsBasic3DVector {
00018   inline float __attribute__((always_inline)) __attribute__ ((pure))
00019   eta(float x, float y, float z) { float t(z/std::sqrt(x*x+y*y)); return ::asinhf(t);} 
00020   inline double __attribute__((always_inline)) __attribute__ ((pure))
00021   eta(double x, double y, double z) { double t(z/std::sqrt(x*x+y*y)); return ::asinh(t);} 
00022   inline long double __attribute__((always_inline)) __attribute__ ((pure))
00023   eta(long double x, long double y, long double z) { long double t(z/std::sqrt(x*x+y*y)); return ::asinhl(t);} 
00024 }
00025 
00026 
00027 template < typename T> 
00028 class Basic3DVector {
00029 public:
00030   typedef  Basic3DVector<T> MathVector;
00031 
00032 
00033   typedef T                                   ScalarType;
00034   typedef Geom::Cylindrical2Cartesian<T>      Cylindrical;
00035   typedef Geom::Spherical2Cartesian<T>        Spherical;
00036   typedef Spherical                           Polar; // synonym
00037     
00042   Basic3DVector() : theX(0), theY(0), theZ(0), theW(0) {}
00043 
00045   Basic3DVector( const Basic3DVector & p) : 
00046     theX(p.x()), theY(p.y()), theZ(p.z()), theW(p.w()) {}
00047 
00049   template <class U>
00050   Basic3DVector( const Basic3DVector<U> & p) : 
00051     theX(p.x()), theY(p.y()), theZ(p.z()), theW(p.w()) {}
00052 
00054   Basic3DVector( const Basic2DVector<T> & p) : 
00055     theX(p.x()), theY(p.y()), theZ(0), theW(0) {}
00056 
00065   template <class OtherPoint> 
00066   explicit Basic3DVector( const OtherPoint& p) : 
00067     theX(p.x()), theY(p.y()), theZ(p.z()), theW(0) {}
00068 
00069 
00070 #ifndef __REFLEX__
00071   // constructor from Vec4
00072   template<typename U>
00073   Basic3DVector(mathSSE::Vec4<U> const& iv) :
00074     theX(iv.arr[0]), theY(iv.arr[1]), theZ(iv.arr[2]), theW(0) {}
00075 #endif  
00076 
00077 #ifndef __REFLEX__
00078 
00079   Basic3DVector( const T& x, const T& y, const T& z, const T& w=0) : 
00080     theX(x), theY(y), theZ(z), theW(w) {}
00081 #else
00082 
00083   Basic3DVector( const T& x, const T& y, const T& z) :
00084     theX(x), theY(y), theZ(z), theW(0) {}
00085   Basic3DVector( const T& x, const T& y, const T& z, const T& w) :
00086     theX(x), theY(y), theZ(z), theW(w) {}
00087 #endif
00088 
00089 
00090 
00095   template <typename U>
00096   Basic3DVector( const Geom::Theta<U>& theta, 
00097                  const Geom::Phi<U>& phi, const T& r) {
00098     Polar p( theta.value(), phi.value(), r);
00099     theX = p.x(); theY = p.y(); theZ = p.z();
00100   }
00101 
00103   T x() const { return theX;}
00104 
00106   T y() const { return theY;}
00107 
00109   T z() const { return theZ;}
00110 
00111   T w() const { return theW;}
00112 
00113 
00114   Basic2DVector<T> xy() const { return  Basic2DVector<T>(theX,theY);}
00115 
00116 
00117   // equality
00118   bool operator==(const Basic3DVector& rh) const {
00119     return x()==rh.x() && y()==rh.y() && z()==rh.z();
00120   }
00121 
00123   T mag2() const { return  x()*x() + y()*y()+z()*z();}
00124 
00126   T mag() const  { return std::sqrt( mag2());}
00127 
00129   T perp2() const { return x()*x() + y()*y();}
00130 
00132   T perp() const { return std::sqrt( perp2());}
00133 
00135   T transverse() const { return perp();}
00136 
00141   T barePhi() const {return std::atan2(y(),x());}
00142   Geom::Phi<T> phi() const {return Geom::Phi<T>(barePhi());}
00143 
00148   T bareTheta() const {return std::atan2(perp(),z());}
00149   Geom::Theta<T> theta() const {return Geom::Theta<T>(std::atan2(perp(),z()));}
00150 
00155   // T eta() const { return -log( tan( theta()/2.));} 
00156   T eta() const { return detailsBasic3DVector::eta(x(),y(),z());} // correct 
00157 
00161   Basic3DVector unit() const {
00162     T my_mag = mag2();
00163     if (my_mag==0) return *this;
00164     my_mag = T(1)/std::sqrt(my_mag);
00165     return *this * my_mag;
00166   }
00167 
00170   template <class U> 
00171   Basic3DVector& operator+= ( const Basic3DVector<U>& p) {
00172     theX += p.x();
00173     theY += p.y();
00174     theZ += p.z();
00175     theW += p.w();
00176     return *this;
00177   } 
00178 
00181   template <class U> 
00182   Basic3DVector& operator-= ( const Basic3DVector<U>& p) {
00183     theX -= p.x();
00184     theY -= p.y();
00185     theZ -= p.z();
00186     theW -= p.w();
00187     return *this;
00188   } 
00189 
00191   Basic3DVector operator-() const { return Basic3DVector(-x(),-y(),-z());}
00192 
00194   Basic3DVector& operator*= ( T t) {
00195     theX *= t;
00196     theY *= t;
00197     theZ *= t;
00198     theW *= t;;
00199     return *this;
00200   } 
00201 
00203   Basic3DVector& operator/= ( T t) {
00204     t = T(1)/t;
00205     theX *= t;
00206     theY *= t;   
00207     theZ *= t;
00208     theW *= t;;
00209     return *this;
00210   } 
00211 
00213   T dot( const Basic3DVector& v) const { 
00214     return x()*v.x() + y()*v.y() + z()*v.z();
00215   }
00216 
00222   template <class U> 
00223   typename PreciseFloatType<T,U>::Type dot( const Basic3DVector<U>& v) const { 
00224     return x()*v.x() + y()*v.y() + z()*v.z();
00225   }
00226 
00228   Basic3DVector cross( const Basic3DVector& v) const {
00229     return Basic3DVector( y()*v.z() - v.y()*z(), 
00230                           z()*v.x() - v.z()*x(), 
00231                           x()*v.y() - v.x()*y());
00232   }
00233 
00234 
00240   template <class U> 
00241   Basic3DVector<typename PreciseFloatType<T,U>::Type> 
00242   cross( const Basic3DVector<U>& v) const {
00243     return Basic3DVector<typename PreciseFloatType<T,U>::Type>( y()*v.z() - v.y()*z(), 
00244                                                                 z()*v.x() - v.z()*x(), 
00245                                                                 x()*v.y() - v.x()*y());
00246   }
00247 
00248 private:
00249   T theX;
00250   T theY;
00251   T theZ;
00252   T theW;
00253 }  
00254 #ifndef __CINT__
00255 __attribute__ ((aligned (16)))
00256 #endif
00257 ;
00258 
00259 
00260 namespace geometryDetails {
00261   std::ostream & print3D(std::ostream& s, double x, double y, double z);
00262 }
00263 
00265 template <class T>
00266 inline std::ostream & operator<<( std::ostream& s, const Basic3DVector<T>& v) {
00267   return geometryDetails::print3D(s, v.x(),v.y(), v.z());
00268 }
00269 
00270 
00272 template <class T, class U>
00273 inline Basic3DVector<typename PreciseFloatType<T,U>::Type>
00274 operator+( const Basic3DVector<T>& a, const Basic3DVector<U>& b) {
00275   typedef Basic3DVector<typename PreciseFloatType<T,U>::Type> RT;
00276   return RT(a.x()+b.x(), a.y()+b.y(), a.z()+b.z(), a.w()+b.w());
00277 }
00278 
00279 template <class T, class U>
00280 inline Basic3DVector<typename PreciseFloatType<T,U>::Type>
00281 operator-( const Basic3DVector<T>& a, const Basic3DVector<U>& b) {
00282   typedef Basic3DVector<typename PreciseFloatType<T,U>::Type> RT;
00283   return RT(a.x()-b.x(), a.y()-b.y(), a.z()-b.z(), a.w()-b.w());
00284 }
00285 
00287 template <class T>
00288 inline T operator*( const Basic3DVector<T>& v1, const Basic3DVector<T>& v2) {
00289   return v1.dot(v2);
00290 }
00291 
00293 template <class T, class U>
00294 inline typename PreciseFloatType<T,U>::Type operator*( const Basic3DVector<T>& v1, 
00295                                                        const Basic3DVector<U>& v2) {
00296   return v1.x()*v2.x() + v1.y()*v2.y() + v1.z()*v2.z();
00297 }
00298 
00302 template <class T>
00303 inline Basic3DVector<T> operator*( const Basic3DVector<T>& v, T t) {
00304   return Basic3DVector<T>(v.x()*t, v.y()*t, v.z()*t, v.w()*t);
00305 }
00306 
00308 template <class T>
00309 inline Basic3DVector<T> operator*(T t, const Basic3DVector<T>& v) {
00310   return Basic3DVector<T>(v.x()*t, v.y()*t, v.z()*t, v.w()*t);
00311 }
00312 
00313 template <class T, typename S>
00314 inline Basic3DVector<T> operator*(S t,  const Basic3DVector<T>& v) {
00315   return static_cast<T>(t)*v;
00316 }
00317 
00318 template <class T, typename S>
00319 inline Basic3DVector<T> operator*(const Basic3DVector<T>& v, S t) {
00320   return static_cast<T>(t)*v;
00321 }
00322 
00323 
00327 template <class T, typename S>
00328 inline Basic3DVector<T> operator/( const Basic3DVector<T>& v, S s) {
00329   T t = T(1)/s;
00330   return v*t;
00331 }
00332 
00333 
00334 typedef Basic3DVector<float> Basic3DVectorF;
00335 typedef Basic3DVector<double> Basic3DVectorD;
00336 typedef Basic3DVector<long double> Basic3DVectorLD;
00337 
00338 
00339 #endif // GeometryVector_Basic3DVector_h
00340 
00341