00001 #ifndef Geometry_TrackingGeometryAligner_GeometryAligner_h
00002 #define Geometry_TrackingGeometryAligner_GeometryAligner_h
00003
00004 #include <vector>
00005 #include <algorithm>
00006 #include <iterator>
00007
00008 #include "FWCore/Utilities/interface/Exception.h"
00009 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00010
00011 #include "Geometry/CommonDetUnit/interface/DetPositioner.h"
00012
00013 #include "CondFormats/Alignment/interface/Alignments.h"
00014 #include "CondFormats/Alignment/interface/AlignmentErrors.h"
00015 #include "CondFormats/Alignment/interface/AlignTransform.h"
00016 #include "CondFormats/Alignment/interface/AlignTransformError.h"
00017 #include "CondFormats/Alignment/interface/AlignmentSurfaceDeformations.h"
00018
00019 #include "Geometry/CommonDetUnit/interface/GeomDet.h"
00020 #include "Geometry/CommonDetUnit/interface/GeomDetUnit.h"
00021 #include "DataFormats/TrackingRecHit/interface/AlignmentPositionError.h"
00022 #include "DataFormats/GeometryCommonDetAlgo/interface/GlobalError.h"
00023 #include "DataFormats/GeometrySurface/interface/Surface.h"
00024 #include "Geometry/CommonTopologies/interface/SurfaceDeformationFactory.h"
00025 #include "Geometry/CommonTopologies/interface/SurfaceDeformation.h"
00026
00027 class Alignments;
00028 class AlignmentSurfaceDeformations;
00029
00031
00032 class GeometryAligner : public DetPositioner {
00033
00034 public:
00035 template<class C>
00036 void applyAlignments( C* geometry,
00037 const Alignments* alignments,
00038 const AlignmentErrors* alignmentErrors,
00039 const AlignTransform& globalCoordinates );
00040
00041 template<class C>
00042 void attachSurfaceDeformations( C* geometry,
00043 const AlignmentSurfaceDeformations* surfaceDeformations );
00044
00045 inline void removeGlobalTransform( const Alignments* alignments,
00046 const AlignmentErrors* alignmentErrors,
00047 const AlignTransform& globalCoordinates,
00048 Alignments* newAlignments,
00049 AlignmentErrors* newAlignmentErrors );
00050 };
00051
00052
00053 template<class C>
00054 void GeometryAligner::applyAlignments( C* geometry,
00055 const Alignments* alignments,
00056 const AlignmentErrors* alignmentErrors,
00057 const AlignTransform& globalCoordinates )
00058 {
00059
00060 edm::LogInfo("Alignment") << "@SUB=GeometryAligner::applyAlignments"
00061 << "Starting to apply alignments.";
00062
00063
00064 if ( alignments->m_align.size() != geometry->theMap.size() )
00065 throw cms::Exception("GeometryMismatch")
00066 << "Size mismatch between geometry (size=" << geometry->theMap.size()
00067 << ") and alignments (size=" << alignments->m_align.size() << ")";
00068 if ( alignments->m_align.size() != alignmentErrors->m_alignError.size() )
00069 throw cms::Exception("GeometryMismatch")
00070 << "Size mismatch between geometry (size=" << geometry->theMap.size()
00071 << ") and alignment errors (size=" << alignmentErrors->m_alignError.size() << ")";
00072
00073 const AlignTransform::Translation &globalShift = globalCoordinates.translation();
00074 const AlignTransform::Rotation globalRotation = globalCoordinates.rotation();
00075 const AlignTransform::Rotation inverseGlobalRotation = globalRotation.inverse();
00076
00077
00078 std::vector<AlignTransform>::const_iterator iAlign = alignments->m_align.begin();
00079 std::vector<AlignTransformError>::const_iterator
00080 iAlignError = alignmentErrors->m_alignError.begin();
00081
00082 std::map<unsigned int, GeomDet*> theMap;
00083 std::copy(geometry->theMap.begin(), geometry->theMap.end(), std::inserter(theMap,theMap.begin()));
00084 unsigned int nAPE = 0;
00085 for ( std::map<unsigned int, GeomDet*>::const_iterator iPair = theMap.begin();
00086 iPair != theMap.end(); ++iPair, ++iAlign, ++iAlignError )
00087 {
00088
00089 if ( (*iPair).first != (*iAlign).rawId() )
00090 throw cms::Exception("GeometryMismatch")
00091 << "DetId mismatch between geometry (rawId=" << (*iPair).first
00092 << ") and alignments (rawId=" << (*iAlign).rawId();
00093
00094 if ( (*iPair).first != (*iAlignError).rawId() )
00095 throw cms::Exception("GeometryMismatch")
00096 << "DetId mismatch between geometry (rawId=" << (*iPair).first
00097 << ") and alignment errors (rawId=" << (*iAlignError).rawId();
00098
00099
00100 CLHEP::Hep3Vector positionHep = globalRotation * CLHEP::Hep3Vector( (*iAlign).translation() ) + globalShift;
00101 CLHEP::HepRotation rotationHep = CLHEP::HepRotation( (*iAlign).rotation() ) * inverseGlobalRotation;
00102
00103
00104 Surface::PositionType position( positionHep.x(), positionHep.y(), positionHep.z() );
00105 Surface::RotationType rotation( rotationHep.xx(), rotationHep.xy(), rotationHep.xz(),
00106 rotationHep.yx(), rotationHep.yy(), rotationHep.yz(),
00107 rotationHep.zx(), rotationHep.zy(), rotationHep.zz() );
00108 GeomDet* iGeomDet = (*iPair).second;
00109 this->setGeomDetPosition( *iGeomDet, position, rotation );
00110
00111
00112 GlobalError error( asSMatrix<3>((*iAlignError).matrix()) );
00113
00114 AlignmentPositionError ape( error );
00115 if (this->setAlignmentPositionError( *iGeomDet, ape ))
00116 ++nAPE;
00117
00118 }
00119
00120 edm::LogInfo("Alignment") << "@SUB=GeometryAligner::applyAlignments"
00121 << "Finished to apply " << theMap.size() << " alignments with "
00122 << nAPE << " non-zero APE.";
00123 }
00124
00125
00126 template<class C>
00127 void GeometryAligner::attachSurfaceDeformations( C* geometry,
00128 const AlignmentSurfaceDeformations* surfaceDeformations )
00129 {
00130 edm::LogInfo("Alignment") << "@SUB=GeometryAligner::attachSurfaceDeformations"
00131 << "Starting to attach surface deformations.";
00132
00133
00134 std::map<unsigned int, GeomDetUnit*> theMap;
00135 std::copy(geometry->theMapUnit.begin(), geometry->theMapUnit.end(), std::inserter(theMap, theMap.begin()));
00136
00137 unsigned int nSurfDef = 0;
00138 unsigned int itemIndex = 0;
00139 std::map<unsigned int, GeomDetUnit*>::const_iterator iPair = theMap.begin();
00140 for ( std::vector<AlignmentSurfaceDeformations::Item>::const_iterator iItem = surfaceDeformations->items().begin();
00141 iItem != surfaceDeformations->items().end();
00142 ++iItem, ++iPair) {
00143
00144
00145
00146 while ( (*iPair).first != (*iItem).m_rawId ) {
00147
00148
00149 GeomDetUnit* geomDetUnit = (*iPair).second;
00150 this->setSurfaceDeformation( *geomDetUnit, 0 );
00151
00152 ++iPair;
00153 if ( iPair==theMap.end() )
00154 throw cms::Exception("GeometryMismatch")
00155 << "GeomDetUnit with rawId=" << (*iItem).m_rawId
00156 << " not found in geometry";
00157 }
00158
00159
00160 AlignmentSurfaceDeformations::ParametersConstIteratorPair iteratorPair = surfaceDeformations->parameters(itemIndex);
00161 std::vector<double> parameters;
00162 std::copy(iteratorPair.first, iteratorPair.second, std::back_inserter(parameters));
00163
00164
00165 SurfaceDeformation * surfDef = SurfaceDeformationFactory::create( (*iItem).m_parametrizationType, parameters);
00166 GeomDetUnit* geomDetUnit = (*iPair).second;
00167 this->setSurfaceDeformation( *geomDetUnit, surfDef );
00168
00169
00170
00171
00172
00173
00174 ++nSurfDef;
00175
00176 ++itemIndex;
00177 }
00178
00179 edm::LogInfo("Alignment") << "@SUB=GeometryAligner::attachSurfaceDeformations"
00180 << "Finished to attach " << nSurfDef << " surface deformations.";
00181 }
00182
00183 void GeometryAligner::removeGlobalTransform( const Alignments* alignments,
00184 const AlignmentErrors* alignmentErrors,
00185 const AlignTransform& globalCoordinates,
00186 Alignments* newAlignments,
00187 AlignmentErrors* newAlignmentErrors )
00188 {
00189 edm::LogInfo("Alignment") << "@SUB=GeometryAligner::removeGlobalTransform"
00190 << "Starting to remove global position from alignments and errors";
00191
00192 if ( alignments->m_align.size() != alignmentErrors->m_alignError.size() )
00193 throw cms::Exception("GeometryMismatch")
00194 << "Size mismatch between alignments (size=" << alignments->m_align.size()
00195 << ") and alignment errors (size=" << alignmentErrors->m_alignError.size() << ")";
00196
00197 const AlignTransform::Translation &globalShift = globalCoordinates.translation();
00198 const AlignTransform::Rotation globalRotation = globalCoordinates.rotation();
00199 const AlignTransform::Rotation inverseGlobalRotation = globalRotation.inverse();
00200
00201 AlignTransform::Translation newPosition;
00202 AlignTransform::Rotation newRotation;
00203
00204 std::vector<AlignTransform>::const_iterator iAlign = alignments->m_align.begin();
00205 std::vector<AlignTransformError>::const_iterator iAlignError = alignmentErrors->m_alignError.begin();
00206 unsigned int nAPE = 0;
00207 for ( iAlign = alignments->m_align.begin();
00208 iAlign != alignments->m_align.end();
00209 ++iAlign, ++iAlignError ) {
00210
00211
00212 newPosition = inverseGlobalRotation * ( (*iAlign).translation() - globalShift );
00213 newRotation = (*iAlign).rotation() * globalRotation;
00214
00215 newAlignments->m_align.push_back( AlignTransform(newPosition,
00216 newRotation,
00217 (*iAlign).rawId()) );
00218
00219
00220
00221
00222 GlobalError error( asSMatrix<3>((*iAlignError).matrix()) );
00223 newAlignmentErrors->m_alignError.push_back( AlignTransformError( (*iAlignError).matrix(),
00224 (*iAlignError).rawId() ) );
00225 if ( error.cxx() || error.cyy() || error.czz() ||
00226 error.cyx() || error.czx() || error.czy() ) {
00227 ++nAPE;
00228 }
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248 }
00249
00250 edm::LogInfo("Alignment") << "@SUB=GeometryAligner::removeGlobalTransform"
00251 << "Finished to remove global transformation from "
00252 << alignments->m_align.size() << " alignments with "
00253 << nAPE << " non-zero APE.";
00254 }
00255
00256 #endif