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