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