CMS 3D CMS Logo

GeometryAligner.h
Go to the documentation of this file.
1 #ifndef Geometry_TrackingGeometryAligner_GeometryAligner_h
2 #define Geometry_TrackingGeometryAligner_GeometryAligner_h
3 
4 #include <vector>
5 #include <algorithm>
6 #include <iterator>
7 
10 
12 
20 
27 
28 class Alignments;
30 
32 
33 class GeometryAligner : public DetPositioner {
34 
35 public:
36  template<class C>
37  void applyAlignments( C* geometry,
38  const Alignments* alignments,
39  const AlignmentErrorsExtended* alignmentErrors,
40  const AlignTransform& globalCoordinates );
41 
42  template<class C>
43  void attachSurfaceDeformations( C* geometry,
44  const AlignmentSurfaceDeformations* surfaceDeformations );
45 
46  inline void removeGlobalTransform( const Alignments* alignments,
47  const AlignmentErrorsExtended* alignmentErrors,
48  const AlignTransform& globalCoordinates,
49  Alignments* newAlignments,
50  AlignmentErrorsExtended* newAlignmentErrorsExtended );
51 };
52 
53 
54 template<class C>
56  const Alignments* alignments,
57  const AlignmentErrorsExtended* alignmentErrors,
58  const AlignTransform& globalCoordinates )
59 {
60 
61  edm::LogInfo("Alignment") << "@SUB=GeometryAligner::applyAlignments"
62  << "Starting to apply alignments.";
63 
64  // Preliminary checks (or we can't loop!)
65  if ( alignments->m_align.size() != geometry->theMap.size() )
66  throw cms::Exception("GeometryMismatch")
67  << "Size mismatch between geometry (size=" << geometry->theMap.size()
68  << ") and alignments (size=" << alignments->m_align.size() << ")";
69  if ( alignments->m_align.size() != alignmentErrors->m_alignError.size() )
70  throw cms::Exception("GeometryMismatch")
71  << "Size mismatch between geometry (size=" << geometry->theMap.size()
72  << ") and alignment errors (size=" << alignmentErrors->m_alignError.size() << ")";
73 
74  const AlignTransform::Translation &globalShift = globalCoordinates.translation();
75  const AlignTransform::Rotation globalRotation = globalCoordinates.rotation(); // by value!
76  const AlignTransform::Rotation inverseGlobalRotation = globalRotation.inverse();
77 
78  // Parallel loop on alignments, alignment errors and geomdets
79  std::vector<AlignTransform>::const_iterator iAlign = alignments->m_align.begin();
80  std::vector<AlignTransformErrorExtended>::const_iterator
81  iAlignError = alignmentErrors->m_alignError.begin();
82  //copy geometry->theMap to a real map to order it....
83  std::map<unsigned int, GeomDet const *> theMap;
84  std::copy(geometry->theMap.begin(), geometry->theMap.end(), std::inserter(theMap,theMap.begin()));
85  unsigned int nAPE = 0;
86  for ( auto iPair = theMap.begin();
87  iPair != theMap.end(); ++iPair, ++iAlign, ++iAlignError )
88  {
89  // Check DetIds
90  if ( (*iPair).first != (*iAlign).rawId() )
91  throw cms::Exception("GeometryMismatch")
92  << "DetId mismatch between geometry (rawId=" << (*iPair).first
93  << ") and alignments (rawId=" << (*iAlign).rawId();
94 
95  if ( (*iPair).first != (*iAlignError).rawId() )
96  throw cms::Exception("GeometryMismatch")
97  << "DetId mismatch between geometry (rawId=" << (*iPair).first
98  << ") and alignment errors (rawId=" << (*iAlignError).rawId();
99 
100  // Apply global correction
101  CLHEP::Hep3Vector positionHep = globalRotation * CLHEP::Hep3Vector( (*iAlign).translation() ) + globalShift;
102  CLHEP::HepRotation rotationHep = CLHEP::HepRotation( (*iAlign).rotation() ) * inverseGlobalRotation;
103 
104  // Define new position/rotation objects and apply
105  Surface::PositionType position( positionHep.x(), positionHep.y(), positionHep.z() );
106  Surface::RotationType rotation( rotationHep.xx(), rotationHep.xy(), rotationHep.xz(),
107  rotationHep.yx(), rotationHep.yy(), rotationHep.yz(),
108  rotationHep.zx(), rotationHep.zy(), rotationHep.zz() );
109  GeomDet* iGeomDet = const_cast<GeomDet*>((*iPair).second);
110  this->setGeomDetPosition( *iGeomDet, position, rotation );
111 
112  // Alignment Position Error only if non-zero to save memory
113  GlobalErrorExtended error( asSMatrix<6>((*iAlignError).matrix()) );
114 
115  AlignmentPositionError ape( error );
116  if (this->setAlignmentPositionError( *iGeomDet, ape ))
117  ++nAPE;
118 
119  }
120 
121  edm::LogInfo("Alignment") << "@SUB=GeometryAligner::applyAlignments"
122  << "Finished to apply " << theMap.size() << " alignments with "
123  << nAPE << " non-zero APE.";
124 }
125 
126 
127 template<class C>
129  const AlignmentSurfaceDeformations* surfaceDeformations )
130 {
131  edm::LogInfo("Alignment") << "@SUB=GeometryAligner::attachSurfaceDeformations"
132  << "Starting to attach surface deformations.";
133 
134  //copy geometry->theMapUnit to a real map to order it....
135  std::map<unsigned int, GeomDetUnit const*> theMap;
136  std::copy(geometry->theMapUnit.begin(), geometry->theMapUnit.end(), std::inserter(theMap, theMap.begin()));
137 
138  unsigned int nSurfDef = 0;
139  unsigned int itemIndex = 0;
140  auto iPair = theMap.begin();
141  for ( std::vector<AlignmentSurfaceDeformations::Item>::const_iterator iItem = surfaceDeformations->items().begin();
142  iItem != surfaceDeformations->items().end();
143  ++iItem, ++iPair) {
144 
145  // Check DetIds
146  // go forward in map of GeomDetUnits until DetId is found
147  while ( (*iPair).first != (*iItem).m_rawId ) {
148 
149  // remove SurfaceDeformation from GeomDetUnit (i.e. set NULL pointer)
150  GeomDetUnit* geomDetUnit = const_cast<GeomDetUnit*>((*iPair).second);
151  this->setSurfaceDeformation( *geomDetUnit, nullptr );
152 
153  ++iPair;
154  if ( iPair==theMap.end() )
155  throw cms::Exception("GeometryMismatch")
156  << "GeomDetUnit with rawId=" << (*iItem).m_rawId
157  << " not found in geometry";
158  }
159 
160  // get the parameters and put them into a vector
161  AlignmentSurfaceDeformations::ParametersConstIteratorPair iteratorPair = surfaceDeformations->parameters(itemIndex);
162  std::vector<double> parameters;
163  std::copy(iteratorPair.first, iteratorPair.second, std::back_inserter(parameters));
164 
165  // create SurfaceDeformation via factory
166  SurfaceDeformation * surfDef = SurfaceDeformationFactory::create( (*iItem).m_parametrizationType, parameters);
167  GeomDetUnit* geomDetUnit = const_cast<GeomDetUnit*>((*iPair).second);
168  this->setSurfaceDeformation( *geomDetUnit, surfDef );
169  // delete is not needed since SurfaceDeformation is passed as a
170  // DeepCopyPointerByClone which takes over ownership. Needs to be
171  // cleaned up and checked once SurfaceDeformation are moved to
172  // proxy topology classes
173  //delete surfDef;
174 
175  ++nSurfDef;
176 
177  ++itemIndex;
178  }
179 
180  edm::LogInfo("Alignment") << "@SUB=GeometryAligner::attachSurfaceDeformations"
181  << "Finished to attach " << nSurfDef << " surface deformations.";
182 }
183 
185  const AlignmentErrorsExtended* alignmentErrors,
186  const AlignTransform& globalCoordinates,
187  Alignments* newAlignments,
188  AlignmentErrorsExtended* newAlignmentErrorsExtended )
189 {
190  edm::LogInfo("Alignment") << "@SUB=GeometryAligner::removeGlobalTransform"
191  << "Starting to remove global position from alignments and errors";
192 
193  if ( alignments->m_align.size() != alignmentErrors->m_alignError.size() )
194  throw cms::Exception("GeometryMismatch")
195  << "Size mismatch between alignments (size=" << alignments->m_align.size()
196  << ") and alignment errors (size=" << alignmentErrors->m_alignError.size() << ")";
197 
198  const AlignTransform::Translation &globalShift = globalCoordinates.translation();
199  const AlignTransform::Rotation globalRotation = globalCoordinates.rotation(); // by value!
200  const AlignTransform::Rotation inverseGlobalRotation = globalRotation.inverse();
201 
202  AlignTransform::Translation newPosition;
203  AlignTransform::Rotation newRotation;
204 
205  std::vector<AlignTransform>::const_iterator iAlign = alignments->m_align.begin();
206  std::vector<AlignTransformErrorExtended>::const_iterator iAlignError = alignmentErrors->m_alignError.begin();
207  unsigned int nAPE = 0;
208  for ( iAlign = alignments->m_align.begin();
209  iAlign != alignments->m_align.end();
210  ++iAlign, ++iAlignError ) {
211 
212  // Remove global position transformation from alignment
213  newPosition = inverseGlobalRotation * ( (*iAlign).translation() - globalShift );
214  newRotation = (*iAlign).rotation() * globalRotation;
215 
216  newAlignments->m_align.emplace_back( AlignTransform(newPosition,
217  newRotation,
218  (*iAlign).rawId()) );
219 
220  // Don't remove global position transformation from APE
221  // as it wasn't applied. Just fill vector with original
222  // values
223  GlobalErrorExtended error( asSMatrix<6>((*iAlignError).matrix()) );
224  newAlignmentErrorsExtended->m_alignError.emplace_back( AlignTransformErrorExtended( (*iAlignError).matrix(),
225  (*iAlignError).rawId() ) );
226 
227  //if ( error.cxx() || error.cyy() || error.czz() ||
228 // error.cyx() || error.czx() || error.czy() ) {
229 // ++nAPE;
230 // }
231 
232  // Code that removes the global postion transformation
233  // from the APE.
234  //
235  //AlgebraicSymMatrix as(3,0);
236  //as[0][0] = error.cxx();
237  //as[1][0] = error.cyx(); as[1][1] = error.cyy();
238  //as[2][0] = error.czx(); as[2][1] = error.czy(); as[2][2] = error.czz();
239 
240  //AlgebraicMatrix am(3,3);
241  //am[0][0] = inverseGlobalRotation.xx(); am[0][1] = inverseGlobalRotation.xy(); am[0][2] = inverseGlobalRotation.xz();
242  //am[1][0] = inverseGlobalRotation.yx(); am[1][1] = inverseGlobalRotation.yy(); am[1][2] = inverseGlobalRotation.yz();
243  //am[2][0] = inverseGlobalRotation.zx(); am[2][1] = inverseGlobalRotation.zy(); am[2][2] = inverseGlobalRotation.zz();
244  //as = as.similarityT( am );
245 
246  //GlobalErrorExtended newError( as );
247  //newAlignmentErrorsExtended->m_alignError.emplace_back( AlignTransformErrorExtended( newError.matrix(),
248  // (*iAlignError).rawId() ) );
249  //++nAPE;
250  }
251 
252  edm::LogInfo("Alignment") << "@SUB=GeometryAligner::removeGlobalTransform"
253  << "Finished to remove global transformation from "
254  << alignments->m_align.size() << " alignments with "
255  << nAPE << " non-zero APE.";
256 }
257 
258 #endif
bool setAlignmentPositionError(GeomDet &det, const AlignmentPositionError &ape)
Definition: DetPositioner.h:46
Class to update a given geometry with a set of alignments.
CLHEP::Hep3Vector Translation
std::vector< AlignTransform > m_align
Definition: Alignments.h:19
std::pair< ParametersConstIterator, ParametersConstIterator > ParametersConstIteratorPair
const Translation & translation() const
void setSurfaceDeformation(GeomDetUnit &detUnit, const SurfaceDeformation *deformation)
Definition: DetPositioner.h:54
void attachSurfaceDeformations(C *geometry, const AlignmentSurfaceDeformations *surfaceDeformations)
void removeGlobalTransform(const Alignments *alignments, const AlignmentErrorsExtended *alignmentErrors, const AlignTransform &globalCoordinates, Alignments *newAlignments, AlignmentErrorsExtended *newAlignmentErrorsExtended)
void setGeomDetPosition(GeomDet &det, const Surface::PositionType &position, const Surface::RotationType &rotation)
Definition: DetPositioner.h:35
ParametersConstIteratorPair parameters(size_t index) const
const ItemVector & items() const
Get vector of all items.
void applyAlignments(C *geometry, const Alignments *alignments, const AlignmentErrorsExtended *alignmentErrors, const AlignTransform &globalCoordinates)
std::vector< AlignTransformErrorExtended > m_alignError
static int position[264][3]
Definition: ReadPGInfo.cc:509
Rotation rotation() const
SurfaceDeformation * create(int type, const std::vector< double > &params)
CLHEP::HepRotation Rotation