CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/Geometry/CaloGeometry/src/CaloCellGeometry.cc

Go to the documentation of this file.
00001 #include "Geometry/CaloGeometry/interface/CaloCellGeometry.h"
00002 #include <CLHEP/Geometry/Plane3D.h>
00003 
00004 typedef CaloCellGeometry::CCGFloat CCGFloat ;
00005 typedef CaloCellGeometry::Pt3D     Pt3D     ;
00006 typedef CaloCellGeometry::Pt3DVec  Pt3DVec  ;
00007 typedef CaloCellGeometry::Tr3D     Tr3D     ;
00008 
00009 typedef HepGeom::Vector3D<CCGFloat> Vec3D   ;
00010 typedef HepGeom::Plane3D<CCGFloat>  Plane3D ;
00011 
00012 typedef HepGeom::Vector3D<double> DVec3D ;
00013 typedef HepGeom::Plane3D<double>  DPlane3D ;
00014 typedef HepGeom::Point3D<double>  DPt3D ;
00015 typedef std::vector<DPt3D>        DPt3DVec ;
00016 
00017 const float CaloCellGeometry::k_ScaleFromDDDtoGeant ( 0.1 ) ;
00018 
00019 CaloCellGeometry::CaloCellGeometry() :
00020    m_refPoint ( 0., 0., 0. ),
00021    m_corners  (  ) ,
00022    m_parms    ( (CCGFloat*) 0 ) 
00023 {
00024 }
00025 
00026 CaloCellGeometry::CaloCellGeometry( const CaloCellGeometry& cell )
00027 {
00028    *this = cell ;
00029 }
00030 
00031 CaloCellGeometry&
00032 CaloCellGeometry::operator=( const CaloCellGeometry& cell )
00033 {
00034    if( this != &cell )
00035    {
00036       m_refPoint = cell.m_refPoint ;
00037       m_corners  = cell.m_corners  ;
00038       m_parms    = cell.m_parms    ;
00039    }
00040    return *this ;
00041 }
00042 
00043 CaloCellGeometry::~CaloCellGeometry()
00044 {
00045 }
00046 
00047 const GlobalPoint& 
00048 CaloCellGeometry::getPosition() const 
00049 {
00050    return m_refPoint ; 
00051 }
00052 
00053 bool 
00054 CaloCellGeometry::emptyCorners() const 
00055 {
00056    return m_corners.empty() ;
00057 }
00058 
00059 const CCGFloat* 
00060 CaloCellGeometry::param() const 
00061 {
00062    return m_parms ;
00063 }
00064 
00065 CaloCellGeometry::CaloCellGeometry( CornersVec::const_reference gp ,
00066                                     const CornersMgr*           mgr,
00067                                     const CCGFloat*             par ) :
00068    m_refPoint ( gp  ),
00069    m_corners  ( mgr ),
00070    m_parms    ( par ) 
00071 {
00072 }
00073 
00074 CaloCellGeometry::CaloCellGeometry( const CornersVec& cv,
00075                                     const CCGFloat*   par ) : 
00076    m_refPoint ( 0.25*( cv[0].x() + cv[1].x() + cv[2].x() + cv[3].x() ),
00077                 0.25*( cv[0].y() + cv[1].y() + cv[2].y() + cv[3].y() ),
00078                 0.25*( cv[0].z() + cv[1].z() + cv[2].z() + cv[3].z() )  ), 
00079    m_corners  ( cv ),
00080    m_parms    ( par ) 
00081 {
00082 }
00083 
00084 CaloCellGeometry::CornersVec& 
00085 CaloCellGeometry::setCorners() const 
00086 {
00087    return m_corners ; 
00088 }
00089 
00090 const CaloCellGeometry::CornersVec&
00091 CaloCellGeometry::getCorners() const
00092 {
00093    return m_corners ;
00094 }
00095 
00096 std::ostream& operator<<( std::ostream& s, const CaloCellGeometry& cell ) 
00097 {
00098    s << ", Center: " <<  cell.getPosition() << std::endl;
00099 
00100    if( cell.emptyCorners() )
00101    {
00102       s << "Corners vector is empty." << std::endl ;
00103    }
00104    else
00105    {
00106       const CaloCellGeometry::CornersVec& corners ( cell.getCorners() ) ;
00107       for ( unsigned int i ( 0 ) ; i != corners.size() ; ++i ) 
00108       {
00109          s << "Corner: " << corners[ i ] << std::endl;
00110       }
00111    }
00112    return s ;
00113 }
00114 
00115 void
00116 CaloCellGeometry::getTransform( Tr3D& tr , Pt3DVec* lptr ) const 
00117 {
00118    const GlobalPoint& p ( CaloCellGeometry::getPosition() ) ;
00119    const Pt3D gFront ( p.x(), p.y(), p.z() ) ;
00120    const DPt3D dgFront ( p.x(), p.y(), p.z() ) ;
00121 
00122    Pt3D lFront ;
00123    assert( 0 != param() ) ;
00124    Pt3DVec lc ( 8, Pt3D(0,0,0) ) ;
00125    vocalCorners( lc, param(), lFront ) ;
00126 
00127    DPt3D dlFront ( lFront.x(), lFront.y(), lFront.z() ) ;
00128 
00129    const Pt3D lBack ( 0.25*(lc[4]+lc[5]+lc[6]+lc[7]) ) ;
00130    const DPt3D dlBack ( lBack.x(), lBack.y(), lBack.z() ) ;
00131 
00132    const Pt3D dlOne ( lc[0].x(), lc[0].y(), lc[0].z() ) ;
00133 
00134    const CornersVec& cor ( getCorners() ) ;
00135    DPt3DVec dkor ( 8, DPt3D(0,0,0) ) ;
00136    for( unsigned int i ( 0 ) ; i != 8 ; ++i )
00137    {
00138       dkor[i] = DPt3D ( cor[i].x(), cor[i].y(), cor[i].z() ) ;
00139    }
00140 
00141    DPt3D  dgBack ( 0.25*( dkor[4]+dkor[5]+dkor[6]+dkor[7] ) ) ;
00142 
00143    const DVec3D dgAxis ( (dgBack-dgFront).unit() ) ;
00144 
00145    dgBack = ( dgFront + (dlBack-dlFront).mag()*dgAxis ) ;
00146    const DPt3D  dgOneT ( dgFront + ( dlOne - dlFront ).mag()*( dkor[0] - dgFront ).unit() ) ;
00147 
00148    const double dlangle ( ( dlBack - dlFront).angle( dlOne - dlFront ) ) ;
00149    const double dgangle ( ( dgBack - dgFront).angle( dgOneT- dgFront ) ) ;
00150    const double ddangle ( dlangle - dgangle ) ;
00151 
00152    const DPlane3D dgPl ( dgFront, dgOneT, dgBack ) ;
00153    const DPt3D    dp2  ( dgFront + dgPl.normal().unit() ) ;
00154 
00155    const DPt3D  dgOne ( dgFront + HepGeom::Rotate3D( -ddangle, dgFront, dp2 )*
00156                       DVec3D ( dgOneT - dgFront ) ) ;
00157 
00158    tr = Tr3D( dlFront , dlBack , dlOne ,
00159               dgFront , dgBack , dgOne    ) ;
00160 
00161    if( 0 != lptr ) (*lptr) = lc ;
00162 }
00163 
00164 const float* 
00165 CaloCellGeometry::checkParmPtr(
00166    const std::vector<float>&   vv  ,
00167    CaloCellGeometry::ParVecVec& pvv  )
00168 {
00169    const float* pP ( 0 ) ;
00170 
00171    for( unsigned int ii ( 0 ) ; ii != pvv.size() ; ++ii )
00172    {
00173       const ParVec& v ( pvv[ii] ) ;
00174       assert( v.size() == vv.size() ) ;
00175 
00176       bool same ( true ) ;
00177       for( unsigned int j ( 0 ) ; j != vv.size() ; ++j )
00178       {
00179          same = same && ( fabs( vv[j] - v[j] )<1.e-6 ) ;
00180          if( !same ) break ;
00181       }
00182       if( same )
00183       {
00184          pP = &(*v.begin()) ;
00185          break ;
00186       }
00187    }
00188    return pP ;
00189 }
00190 
00191 const float* 
00192 CaloCellGeometry::getParmPtr(
00193    const std::vector<float>&   vv  ,
00194    CaloCellGeometry::ParMgr*    mgr ,
00195    CaloCellGeometry::ParVecVec& pvv  )
00196 {
00197    const float* pP ( checkParmPtr( vv, pvv ) ) ;
00198 
00199    if( 0 == pP )
00200    {
00201       pvv.push_back( ParVec( mgr ) ) ;
00202       ParVec& back ( pvv.back() ) ;
00203       for( unsigned int i ( 0 ) ; i != vv.size() ; ++i )
00204       {
00205          back[i] = vv[i] ;
00206       }
00207       pP = &(*back.begin()) ;
00208    }
00209    return pP ;
00210 }
00211 
00212 bool 
00213 CaloCellGeometry::inside( const GlobalPoint& point ) const
00214 {
00215    bool ans ( false ) ;
00216    const Pt3D  p ( point.x(), point.y(), point.z() ) ;
00217    const CornersVec& cog ( getCorners() ) ;
00218    Pt3D  co[8] ;
00219    for( unsigned int i ( 0 ) ; i != 8 ; ++i )
00220    {
00221       co[i] = Pt3D ( cog[i].x(), cog[i].y(), cog[i].z() ) ;
00222    }
00223 
00224    const Plane3D  AA ( co[0], co[1], co[2] ) ; // z<0
00225    const Plane3D  BB ( co[6], co[5], co[4] ) ; // z>0
00226 
00227    if( AA.distance(p)*BB.distance(p) >= 0 )
00228    {
00229      const Plane3D  CC ( co[0], co[4], co[5] ) ; // x<0
00230      const Plane3D  DD ( co[2], co[6], co[7] ) ; // x>0
00231      if( CC.distance(p)*DD.distance(p) >= 0 )
00232      {
00233        const Plane3D  EE ( co[3], co[7], co[4] ) ; // y<0
00234        const Plane3D  FF ( co[1], co[5], co[6] ) ; // y>0
00235        if( EE.distance(p)*FF.distance(p) >= 0 )
00236        {
00237            ans = true ;
00238        }
00239       }
00240    }
00241    return ans ;
00242 }