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