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 
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 
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
AlignmentErrorsExtended.h
TkRotation< float >
BeamSpotPI::parameters
parameters
Definition: BeamSpotPayloadInspectorHelper.h:30
MessageLogger.h
GeomDet
Definition: GeomDet.h:27
filterCSVwithJSON.copy
copy
Definition: filterCSVwithJSON.py:36
DetPositioner.h
AlignTransform::Rotation
CLHEP::HepRotation Rotation
Definition: AlignTransform.h:19
geometry
Definition: geometry.py:1
GeometryAligner::removeGlobalTransform
void removeGlobalTransform(const Alignments *alignments, const AlignmentErrorsExtended *alignmentErrors, const AlignTransform &globalCoordinates, Alignments *newAlignments, AlignmentErrorsExtended *newAlignmentErrorsExtended)
Definition: GeometryAligner.h:172
Alignments.h
AlignTransform::Translation
CLHEP::Hep3Vector Translation
Definition: AlignTransform.h:18
AlignTransform.h
edm::LogInfo
Log< level::Info, false > LogInfo
Definition: MessageLogger.h:125
relativeConstraints.error
error
Definition: relativeConstraints.py:53
SurfaceDeformation
Definition: SurfaceDeformation.h:18
GeometryAligner::applyAlignments
void applyAlignments(const C *geometry, const Alignments *alignments, const AlignmentErrorsExtended *alignmentErrors, const AlignTransform &globalCoordinates)
Definition: GeometryAligner.h:52
DetPositioner::setGeomDetPosition
void setGeomDetPosition(GeomDet &det, const Surface::PositionType &position, const Surface::RotationType &rotation)
Definition: DetPositioner.h:28
Surface.h
AlignTransformError.h
Point3DBase< float, GlobalTag >
DetPositioner
Definition: DetPositioner.h:12
AlignTransform
Definition: AlignTransform.h:15
SurfaceDeformation.h
idealTransformation.rotation
dictionary rotation
Definition: idealTransformation.py:1
AlignTransformErrorExtended.h
DetPositioner::setAlignmentPositionError
bool setAlignmentPositionError(GeomDet &det, const AlignmentPositionError &ape)
Definition: DetPositioner.h:37
AlignTransform::rotation
Rotation rotation() const
Definition: AlignTransform.h:38
position
static int position[264][3]
Definition: ReadPGInfo.cc:289
AlignTransform::translation
const Translation & translation() const
Definition: AlignTransform.h:33
GlobalError.h
AlignmentSurfaceDeformations
Definition: AlignmentSurfaceDeformations.h:20
AlignmentErrors.h
AlignmentErrorsExtended
Definition: AlignmentErrorsExtended.h:10
SurfaceDeformationFactory.h
GeometryAligner::attachSurfaceDeformations
void attachSurfaceDeformations(const C *geometry, const AlignmentSurfaceDeformations *surfaceDeformations)
Definition: GeometryAligner.h:120
gen::C
C
Definition: PomwigHadronizer.cc:78
AlignmentSurfaceDeformations.h
AlignmentPositionError.h
AlignTransformErrorExtended
Definition: AlignTransformErrorExtended.h:13
DetPositioner::setSurfaceDeformation
void setSurfaceDeformation(GeomDetUnit &detUnit, const SurfaceDeformation *deformation)
Definition: DetPositioner.h:44
Exception.h
AlignmentSurfaceDeformations::parameters
ParametersConstIteratorPair parameters(size_t index) const
Definition: AlignmentSurfaceDeformations.h:59
Alignments::m_align
std::vector< AlignTransform > m_align
Definition: Alignments.h:19
AlignmentErrorsExtended::m_alignError
std::vector< AlignTransformErrorExtended > m_alignError
Definition: AlignmentErrorsExtended.h:19
cms::Exception
Definition: Exception.h:70
GlobalErrorBaseExtended< double, ErrorMatrixTag >
AlignmentPositionError
Definition: AlignmentPositionError.h:10
GeomDet.h
Alignments
Definition: Alignments.h:10
AlignmentSurfaceDeformations::items
const ItemVector & items() const
Get vector of all items.
Definition: AlignmentSurfaceDeformations.h:55
AlignmentSurfaceDeformations::ParametersConstIteratorPair
std::pair< ParametersConstIterator, ParametersConstIterator > ParametersConstIteratorPair
Definition: AlignmentSurfaceDeformations.h:32
GeometryAligner
Class to update a given geometry with a set of alignments.
Definition: GeometryAligner.h:33
SurfaceDeformationFactory::create
SurfaceDeformation * create(int type, const std::vector< double > &params)
Definition: SurfaceDeformationFactory.cc:40