CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC4_patch1/src/DataFormats/GeometrySurface/interface/GloballyPositioned.h

Go to the documentation of this file.
00001 #ifndef Geom_GloballyPositioned_H
00002 #define Geom_GloballyPositioned_H
00003 
00004 #include "DataFormats/GeometryVector/interface/Point3DBase.h"
00005 #include "DataFormats/GeometryVector/interface/Vector3DBase.h"
00006 #include "DataFormats/GeometryVector/interface/LocalTag.h"
00007 #include "DataFormats/GeometryVector/interface/GlobalTag.h"
00008 #include "DataFormats/GeometrySurface/interface/TkRotation.h"
00009 
00017 template <class T>
00018 class GloballyPositioned {
00019 public:
00020 
00021   typedef T                             Scalar;
00022   typedef Point3DBase<T,GlobalTag>      PositionType;
00023   typedef TkRotation<T>                 RotationType;
00024   typedef Point3DBase<T,GlobalTag>      GlobalPoint;
00025   typedef Point3DBase<T,LocalTag>       LocalPoint;
00026   typedef Vector3DBase<T,GlobalTag>     GlobalVector;
00027   typedef Vector3DBase<T,LocalTag>      LocalVector;
00028 
00029   static T iniPhi() {
00030     return 999.9978;
00031   }
00032   static T iniEta() {
00033     return 999.9978;
00034   }
00035 
00036   GloballyPositioned( const PositionType& pos, const RotationType& rot) :
00037     thePos(pos), theRot(rot) {resetCache();}
00038 
00039   virtual ~GloballyPositioned() {}
00040 
00041   const PositionType& position() const { return thePos;}
00042 
00043   const RotationType& rotation() const { return theRot;}
00044 
00045   T phi() const {
00046     if (thePhi==iniPhi()) thePhi = thePos.barePhi();
00047     return thePhi;
00048   }
00049   T eta() const { 
00050     if (theEta==iniEta()) theEta = thePos.eta();
00051     return theEta;
00052   }
00053 
00054 
00055   // multiply inverse is faster
00056   class ToLocal {
00057   public:
00058     ToLocal(GloballyPositioned const & frame) :
00059       thePos(frame.position()), theRot(frame.rotation().transposed()){}
00060     
00061     LocalPoint toLocal( const GlobalPoint& gp) const {
00062       return LocalPoint( theRot.multiplyInverse( gp.basicVector() -
00063                          thePos.basicVector()) 
00064                        );
00065     }
00066     
00067     LocalVector toLocal( const GlobalVector& gv) const {
00068       return LocalVector(theRot.multiplyInverse(gv.basicVector()));
00069     } 
00070     
00071   private:
00072     PositionType  thePos;
00073     RotationType  theRot;
00074     
00075   };
00076 
00077   
00078 
00082   GlobalPoint toGlobal( const LocalPoint& lp) const {
00083     return GlobalPoint( rotation().multiplyInverse( lp.basicVector()) +
00084                         position().basicVector());
00085   }
00086 
00091   template <class U>
00092   Point3DBase< U, GlobalTag>
00093   toGlobal( const Point3DBase< U, LocalTag>& lp) const {
00094     return Point3DBase< U, GlobalTag>( rotation().multiplyInverse( lp.basicVector()) +
00095                                        position().basicVector());
00096   }
00097 
00101   GlobalVector toGlobal( const LocalVector& lv) const {
00102     return GlobalVector( rotation().multiplyInverse( lv.basicVector()));
00103   }
00104 
00109   template <class U>
00110   Vector3DBase< U, GlobalTag>
00111   toGlobal( const Vector3DBase< U, LocalTag>& lv) const {
00112     return Vector3DBase< U, GlobalTag>( rotation().multiplyInverse( lv.basicVector()));
00113   }
00114 
00118   LocalPoint toLocal( const GlobalPoint& gp) const {
00119     return LocalPoint( rotation() * (gp.basicVector()-position().basicVector()));
00120   }
00121 
00126   template <class U>
00127   Point3DBase< U, LocalTag>
00128   toLocal( const Point3DBase< U, GlobalTag>& gp) const {
00129     return Point3DBase< U, LocalTag>( rotation() * 
00130                                       (gp.basicVector()-position().basicVector()));
00131   }
00132 
00136   LocalVector toLocal( const GlobalVector& gv) const {
00137     return LocalVector( rotation() * gv.basicVector());
00138   }
00139 
00144   template <class U>
00145   Vector3DBase< U, LocalTag>
00146   toLocal( const Vector3DBase< U, GlobalTag>& gv) const {
00147     return Vector3DBase< U, LocalTag>( rotation() * gv.basicVector());
00148   }
00149 
00153   void move( const GlobalVector& displacement) {
00154     thePos += displacement;
00155     resetCache();
00156   }
00157 
00161   void rotate( const RotationType& rotation) {
00162     theRot *= rotation;
00163     resetCache();
00164   }
00165 
00166 private:
00167 
00168   PositionType  thePos;
00169   RotationType  theRot;
00170 
00171 
00172   void resetCache() {
00173     if ((thePos.x() == 0.) && (thePos.y() == 0.)) {
00174       thePhi = theEta = 0.; // avoid FPE
00175     } else {
00176       thePhi = iniPhi();
00177       theEta = iniEta();
00178     }
00179   }
00180 
00181 
00182   void setCache() {
00183     if ((thePos.x() == 0.) && (thePos.y() == 0.)) {
00184       thePhi = theEta = 0.; // avoid FPE
00185     } else {
00186       thePhi = thePos.barePhi();
00187       theEta = thePos.eta();
00188     }
00189   }
00190   
00191   mutable T thePhi;
00192   mutable T theEta;
00193 
00194 };
00195   
00196 #endif