CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/Geometry/CaloEventSetup/interface/CaloGeometryDBEP.h

Go to the documentation of this file.
00001 #ifndef GEOMETRY_CALOGEOMETRY_CALOGEOMETRYDBEP_H
00002 #define GEOMETRY_CALOGEOMETRY_CALOGEOMETRYDBEP_H 1
00003 
00004 // system include files
00005 #include <memory>
00006 #include "boost/shared_ptr.hpp"
00007 
00008 // user include files
00009 #include "FWCore/Framework/interface/ModuleFactory.h"
00010 #include "FWCore/Framework/interface/ESProducer.h"
00011 
00012 #include "FWCore/Framework/interface/ESHandle.h"
00013 #include "Geometry/Records/interface/IdealGeometryRecord.h"
00014 #include "CondFormats/AlignmentRecord/interface/GlobalPositionRcd.h"
00015 #include "Geometry/CaloGeometry/interface/CaloSubdetectorGeometry.h"
00016 #include "Geometry/CaloGeometry/interface/TruncatedPyramid.h"
00017 #include "Geometry/CaloGeometry/interface/PreshowerStrip.h"
00018 #include "Geometry/CaloGeometry/interface/CaloGenericDetId.h"
00019 #include "Geometry/CaloEventSetup/interface/CaloGeometryLoader.h"
00020 #include "DetectorDescription/Core/interface/DDCompactView.h"
00021 
00022 #include "CondFormats/GeometryObjects/interface/PCaloGeometry.h"
00023 
00024 #include "CondFormats/Alignment/interface/Alignments.h"
00025 
00026 #include <Math/Transform3D.h>
00027 #include <Math/EulerAngles.h>
00028 
00029 //Forward declaration
00030 
00031 //
00032 // class declaration
00033 //
00034 
00035 template <class T, class U>
00036 class CaloGeometryDBEP : public edm::ESProducer 
00037 {
00038    public:
00039 
00040       typedef CaloCellGeometry::CCGFloat CCGFloat ;
00041       typedef CaloCellGeometry::Pt3D     Pt3D     ;
00042       typedef CaloCellGeometry::Pt3DVec  Pt3DVec  ;
00043       typedef CaloCellGeometry::Tr3D     Tr3D     ;
00044 
00045       typedef boost::shared_ptr< CaloSubdetectorGeometry > PtrType ;
00046       typedef CaloSubdetectorGeometry::TrVec  TrVec      ;
00047       typedef CaloSubdetectorGeometry::DimVec DimVec     ;
00048       typedef CaloSubdetectorGeometry::IVec   IVec       ;
00049       
00050       CaloGeometryDBEP<T,U>( const edm::ParameterSet& ps ) :
00051          m_applyAlignment ( ps.getParameter<bool>("applyAlignment") )
00052       {
00053          setWhatProduced( this,
00054                           &CaloGeometryDBEP<T,U>::produceAligned,
00055                           edm::es::Label( T::producerTag() ) ) ;//+std::string("TEST") ) ) ;
00056       }
00057 
00058       virtual ~CaloGeometryDBEP<T,U>() {}
00059       PtrType produceAligned( const typename T::AlignedRecord& iRecord ) 
00060       {
00061          const Alignments* alignPtr  ( 0 ) ;
00062          const Alignments* globalPtr ( 0 ) ;
00063          if( m_applyAlignment ) // get ptr if necessary
00064          {
00065             edm::ESHandle< Alignments >                                      alignments ;
00066             iRecord.template getRecord< typename T::AlignmentRecord >().get( alignments ) ;
00067 
00068             assert( alignments.isValid() && // require valid alignments and expected size
00069                     ( alignments->m_align.size() == T::numberOfAlignments() ) ) ;
00070             alignPtr = alignments.product() ;
00071 
00072             edm::ESHandle< Alignments >                          globals   ;
00073             iRecord.template getRecord<GlobalPositionRcd>().get( globals ) ;
00074 
00075             assert( globals.isValid() ) ;
00076             globalPtr = globals.product() ;
00077          }
00078 
00079          TrVec  tvec ;
00080          DimVec dvec ;
00081          IVec   ivec ;
00082 
00083          if( U::writeFlag() )
00084          {
00085             edm::ESHandle<CaloSubdetectorGeometry> pG ;
00086             iRecord.get( T::producerTag() + std::string("_master"), pG ) ; 
00087 
00088             const CaloSubdetectorGeometry* pGptr ( pG.product() ) ;
00089 
00090             pGptr->getSummary( tvec, ivec, dvec ) ;
00091 
00092             U::write( tvec, dvec, ivec, T::dbString() ) ;
00093          }
00094          else
00095          {
00096            //std::cout<<"Getting Geometry from DB for "<<T::producerTag()<<std::endl ;
00097             edm::ESHandle<PCaloGeometry> pG ;
00098             iRecord.template getRecord<typename T::PGeometryRecord >().get( pG ) ; 
00099 
00100             tvec = pG->getTranslation() ;
00101             dvec = pG->getDimension() ;
00102             ivec = pG->getIndexes() ;
00103          }       
00104 //*********************************************************************************************
00105 
00106          const unsigned int nTrParm ( tvec.size()/T::k_NumberOfCellsForCorners ) ;
00107 
00108          assert( dvec.size() == T::k_NumberOfShapes * T::k_NumberOfParametersPerShape ) ;
00109 
00110          PtrType ptr ( new T ) ;
00111 
00112          ptr->fillDefaultNamedParameters() ;
00113 
00114          ptr->allocateCorners( T::k_NumberOfCellsForCorners ) ;
00115 
00116          ptr->allocatePar(    dvec.size() ,
00117                               T::k_NumberOfParametersPerShape ) ;
00118 
00119          for( unsigned int i ( 0 ) ; i != T::k_NumberOfCellsForCorners ; ++i )
00120          {
00121             const unsigned int nPerShape ( T::k_NumberOfParametersPerShape ) ;
00122             DimVec dims ;
00123             dims.reserve( nPerShape ) ;
00124 
00125             const unsigned int indx ( ivec.size()==1 ? 0 : i ) ;
00126 
00127             DimVec::const_iterator dsrc ( dvec.begin() + ivec[indx]*nPerShape ) ;
00128 
00129             for( unsigned int j ( 0 ) ; j != nPerShape ; ++j )
00130             {
00131                dims.push_back( *dsrc ) ;
00132                ++dsrc ;
00133             }
00134 
00135             const CCGFloat* myParm ( CaloCellGeometry::getParmPtr( dims, 
00136                                                                    ptr->parMgr(), 
00137                                                                    ptr->parVecVec() ) ) ;
00138 
00139 
00140             const DetId id ( T::DetIdType::detIdFromDenseIndex( i ) ) ;
00141     
00142             const unsigned int iGlob ( 0 == globalPtr ? 0 :
00143                                        T::alignmentTransformIndexGlobal( id ) ) ;
00144 
00145             assert( 0 == globalPtr || iGlob < globalPtr->m_align.size() ) ;
00146 
00147             const AlignTransform* gt ( 0 == globalPtr ? 0 : &globalPtr->m_align[ iGlob ] ) ;
00148 
00149             assert( 0 == gt || iGlob == T::alignmentTransformIndexGlobal( DetId( gt->rawId() ) ) ) ;
00150 
00151             const unsigned int iLoc ( 0 == alignPtr ? 0 :
00152                                       T::alignmentTransformIndexLocal( id ) ) ;
00153 
00154             assert( 0 == alignPtr || iLoc < alignPtr->m_align.size() ) ;
00155 
00156             const AlignTransform* at ( 0 == alignPtr ? 0 :
00157                                        &alignPtr->m_align[ iLoc ] ) ;
00158 
00159             assert( 0 == at || ( T::alignmentTransformIndexLocal( DetId( at->rawId() ) ) == iLoc ) ) ;
00160 
00161             const CaloGenericDetId gId ( id ) ;
00162 
00163             Pt3D  lRef ;
00164             Pt3DVec lc ( 8, Pt3D(0,0,0) ) ;
00165             T::localCorners( lc, &dims.front(), i, lRef ) ;
00166 
00167             const Pt3D lBck ( 0.25*(lc[4]+lc[5]+lc[6]+lc[7] ) ) ; // ctr rear  face in local
00168             const Pt3D lCor ( lc[0] ) ;
00169 
00170             //----------------------------------- create transform from 6 numbers ---
00171             const unsigned int jj ( i*nTrParm ) ;
00172             Tr3D tr ;
00173             const ROOT::Math::Translation3D tl ( tvec[jj], tvec[jj+1], tvec[jj+2] ) ;
00174             const ROOT::Math::EulerAngles ea (
00175                6==nTrParm ?
00176                ROOT::Math::EulerAngles( tvec[jj+3], tvec[jj+4], tvec[jj+5] ) :
00177                ROOT::Math::EulerAngles() ) ;
00178             const ROOT::Math::Transform3D rt ( ea, tl ) ;
00179             double xx,xy,xz,dx,yx,yy,yz,dy,zx,zy,zz,dz;
00180             rt.GetComponents(xx,xy,xz,dx,yx,yy,yz,dy,zx,zy,zz,dz) ;
00181             tr = Tr3D( CLHEP::HepRep3x3( xx, xy, xz,
00182                                          yx, yy, yz,
00183                                          zx, zy, zz ), 
00184                        CLHEP::Hep3Vector(dx,dy,dz)     );
00185 
00186             // now prepend alignment(s) for final transform
00187             const Tr3D atr ( 0 == at ? tr :
00188                              ( 0 == gt ? at->transform()*tr :
00189                                at->transform()*gt->transform()*tr ) ) ;
00190             //--------------------------------- done making transform  ---------------
00191 
00192             const Pt3D        gRef ( atr*lRef ) ;
00193             const GlobalPoint fCtr ( gRef.x(), gRef.y(), gRef.z() ) ;
00194             const Pt3D        gBck ( atr*lBck ) ;
00195             const GlobalPoint fBck ( gBck.x(), gBck.y(), gBck.z() ) ;
00196             const Pt3D        gCor ( atr*lCor ) ;
00197             const GlobalPoint fCor ( gCor.x(), gCor.y(), gCor.z() ) ;
00198 
00199             ptr->newCell(  fCtr, fBck, fCor, myParm, id ) ;
00200          }
00201 
00202          ptr->initializeParms() ; // initializations; must happen after cells filled
00203 
00204          return ptr ; 
00205       }
00206 
00207    private:
00208 
00209       bool        m_applyAlignment ;
00210 };
00211 
00212 #endif