CMS 3D CMS Logo

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