test
CMS 3D CMS Logo

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