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( asSMatrix<3>((*iAlignError).matrix()) );
113 
114  AlignmentPositionError ape( error );
115  if (this->setAlignmentPositionError( *iGeomDet, ape ))
116  ++nAPE;
117 
118  }
119 
120  edm::LogInfo("Alignment") << "@SUB=GeometryAligner::applyAlignments"
121  << "Finished to apply " << theMap.size() << " alignments with "
122  << nAPE << " non-zero APE.";
123 }
124 
125 
126 template<class C>
128  const AlignmentSurfaceDeformations* surfaceDeformations )
129 {
130  edm::LogInfo("Alignment") << "@SUB=GeometryAligner::attachSurfaceDeformations"
131  << "Starting to attach surface deformations.";
132 
133  //copy geometry->theMapUnit to a real map to order it....
134  std::map<unsigned int, GeomDetUnit*> theMap;
135  std::copy(geometry->theMapUnit.begin(), geometry->theMapUnit.end(), std::inserter(theMap, theMap.begin()));
136 
137  unsigned int nSurfDef = 0;
138  unsigned int itemIndex = 0;
139  std::map<unsigned int, GeomDetUnit*>::const_iterator iPair = theMap.begin();
140  for ( std::vector<AlignmentSurfaceDeformations::Item>::const_iterator iItem = surfaceDeformations->items().begin();
141  iItem != surfaceDeformations->items().end();
142  ++iItem, ++iPair) {
143 
144  // Check DetIds
145  // go forward in map of GeomDetUnits until DetId is found
146  while ( (*iPair).first != (*iItem).m_rawId ) {
147 
148  // remove SurfaceDeformation from GeomDetUnit (i.e. set NULL pointer)
149  GeomDetUnit* geomDetUnit = (*iPair).second;
150  this->setSurfaceDeformation( *geomDetUnit, 0 );
151 
152  ++iPair;
153  if ( iPair==theMap.end() )
154  throw cms::Exception("GeometryMismatch")
155  << "GeomDetUnit with rawId=" << (*iItem).m_rawId
156  << " not found in geometry";
157  }
158 
159  // get the parameters and put them into a vector
160  AlignmentSurfaceDeformations::ParametersConstIteratorPair iteratorPair = surfaceDeformations->parameters(itemIndex);
161  std::vector<double> parameters;
162  std::copy(iteratorPair.first, iteratorPair.second, std::back_inserter(parameters));
163 
164  // create SurfaceDeformation via factory
165  SurfaceDeformation * surfDef = SurfaceDeformationFactory::create( (*iItem).m_parametrizationType, parameters);
166  GeomDetUnit* geomDetUnit = (*iPair).second;
167  this->setSurfaceDeformation( *geomDetUnit, surfDef );
168  // delete is not needed since SurfaceDeformation is passed as a
169  // DeepCopyPointerByClone which takes over ownership. Needs to be
170  // cleaned up and checked once SurfaceDeformation are moved to
171  // proxy topology classes
172  //delete surfDef;
173 
174  ++nSurfDef;
175 
176  ++itemIndex;
177  }
178 
179  edm::LogInfo("Alignment") << "@SUB=GeometryAligner::attachSurfaceDeformations"
180  << "Finished to attach " << nSurfDef << " surface deformations.";
181 }
182 
184  const AlignmentErrors* alignmentErrors,
185  const AlignTransform& globalCoordinates,
186  Alignments* newAlignments,
187  AlignmentErrors* newAlignmentErrors )
188 {
189  edm::LogInfo("Alignment") << "@SUB=GeometryAligner::removeGlobalTransform"
190  << "Starting to remove global position from alignments and errors";
191 
192  if ( alignments->m_align.size() != alignmentErrors->m_alignError.size() )
193  throw cms::Exception("GeometryMismatch")
194  << "Size mismatch between alignments (size=" << alignments->m_align.size()
195  << ") and alignment errors (size=" << alignmentErrors->m_alignError.size() << ")";
196 
197  const AlignTransform::Translation &globalShift = globalCoordinates.translation();
198  const AlignTransform::Rotation globalRotation = globalCoordinates.rotation(); // by value!
199  const AlignTransform::Rotation inverseGlobalRotation = globalRotation.inverse();
200 
201  AlignTransform::Translation newPosition;
202  AlignTransform::Rotation newRotation;
203 
204  std::vector<AlignTransform>::const_iterator iAlign = alignments->m_align.begin();
205  std::vector<AlignTransformError>::const_iterator iAlignError = alignmentErrors->m_alignError.begin();
206  unsigned int nAPE = 0;
207  for ( iAlign = alignments->m_align.begin();
208  iAlign != alignments->m_align.end();
209  ++iAlign, ++iAlignError ) {
210 
211  // Remove global position transformation from alignment
212  newPosition = inverseGlobalRotation * ( (*iAlign).translation() - globalShift );
213  newRotation = (*iAlign).rotation() * globalRotation;
214 
215  newAlignments->m_align.push_back( AlignTransform(newPosition,
216  newRotation,
217  (*iAlign).rawId()) );
218 
219  // Don't remove global position transformation from APE
220  // as it wasn't applied. Just fill vector with original
221  // values
222  GlobalError error( asSMatrix<3>((*iAlignError).matrix()) );
223  newAlignmentErrors->m_alignError.push_back( AlignTransformError( (*iAlignError).matrix(),
224  (*iAlignError).rawId() ) );
225  if ( error.cxx() || error.cyy() || error.czz() ||
226  error.cyx() || error.czx() || error.czy() ) {
227  ++nAPE;
228  }
229 
230  // Code that removes the global postion transformation
231  // from the APE.
232  //
233  //AlgebraicSymMatrix as(3,0);
234  //as[0][0] = error.cxx();
235  //as[1][0] = error.cyx(); as[1][1] = error.cyy();
236  //as[2][0] = error.czx(); as[2][1] = error.czy(); as[2][2] = error.czz();
237 
238  //AlgebraicMatrix am(3,3);
239  //am[0][0] = inverseGlobalRotation.xx(); am[0][1] = inverseGlobalRotation.xy(); am[0][2] = inverseGlobalRotation.xz();
240  //am[1][0] = inverseGlobalRotation.yx(); am[1][1] = inverseGlobalRotation.yy(); am[1][2] = inverseGlobalRotation.yz();
241  //am[2][0] = inverseGlobalRotation.zx(); am[2][1] = inverseGlobalRotation.zy(); am[2][2] = inverseGlobalRotation.zz();
242  //as = as.similarityT( am );
243 
244  //GlobalError newError( as );
245  //newAlignmentErrors->m_alignError.push_back( AlignTransformError( newError.matrix(),
246  // (*iAlignError).rawId() ) );
247  //++nAPE;
248  }
249 
250  edm::LogInfo("Alignment") << "@SUB=GeometryAligner::removeGlobalTransform"
251  << "Finished to remove global transformation from "
252  << alignments->m_align.size() << " alignments with "
253  << nAPE << " non-zero APE.";
254 }
255 
256 #endif
dictionary parameters
Definition: Parameters.py:2
bool setAlignmentPositionError(GeomDet &det, const AlignmentPositionError &ape)
Definition: DetPositioner.h:46
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:17
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)
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