CMS 3D CMS Logo

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