Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:44:35

0001 #include "Alignment/CommonAlignment/interface/AlignableDetUnit.h"
0002 
0003 #include "Geometry/CommonDetUnit/interface/GeomDet.h"
0004 
0005 #include "CondFormats/Alignment/interface/Alignments.h"
0006 #include "CondFormats/Alignment/interface/AlignmentErrorsExtended.h"
0007 #include "CLHEP/Vector/RotationInterfaces.h"
0008 #include "DataFormats/GeometryCommonDetAlgo/interface/AlignmentPositionError.h"
0009 #include "Geometry/CommonTopologies/interface/SurfaceDeformation.h"
0010 
0011 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0012 #include "FWCore/Utilities/interface/Exception.h"
0013 
0014 //__________________________________________________________________________________________________
0015 AlignableDetUnit::AlignableDetUnit(const GeomDetUnit* geomDetUnit)
0016     :  // rely on non-NULL pointer!
0017       Alignable(geomDetUnit->geographicalId().rawId(), geomDetUnit->surface()),
0018       theAlignmentPositionError(nullptr),
0019       theSurfaceDeformation(nullptr),
0020       theCachedSurfaceDeformation(nullptr) {
0021   if (geomDetUnit->alignmentPositionError()) {  // take over APE from geometry
0022     // 2nd argument w/o effect:
0023     this->setAlignmentPositionError(*(geomDetUnit->alignmentPositionError()), false);
0024   }
0025 
0026   if (geomDetUnit->surfaceDeformation()) {  // take over surface modification
0027     // 2nd argument w/o effect:
0028     this->setSurfaceDeformation(geomDetUnit->surfaceDeformation(), false);
0029   }
0030 
0031   theDeepComponents.push_back(this);
0032 }
0033 
0034 //__________________________________________________________________________________________________
0035 AlignableDetUnit::~AlignableDetUnit() {
0036   delete theAlignmentPositionError;
0037   delete theSurfaceDeformation;
0038   delete theCachedSurfaceDeformation;
0039   for (auto surface : surfaceDeformationsCache_)
0040     delete surface.second;
0041 }
0042 
0043 //__________________________________________________________________________________________________
0044 void AlignableDetUnit::update(const GeomDetUnit* geomDetUnit) {
0045   if (!geomDetUnit) {
0046     throw cms::Exception("Alignment") << "@SUB=AlignableDetUnit::update\n"
0047                                       << "Trying to update with GeomDetUnit* pointing to 'nullptr'.";
0048   }
0049 
0050   Alignable::update(geomDetUnit->geographicalId().rawId(), geomDetUnit->surface());
0051 
0052   if (geomDetUnit->alignmentPositionError()) {  // take over APE from geometry
0053     // 2nd argument w/o effect:
0054     this->setAlignmentPositionError(*(geomDetUnit->alignmentPositionError()), false);
0055   }
0056 
0057   if (geomDetUnit->surfaceDeformation()) {  // take over surface modification
0058     // 2nd argument w/o effect:
0059     this->setSurfaceDeformation(geomDetUnit->surfaceDeformation(), false);
0060   }
0061 }
0062 
0063 //__________________________________________________________________________________________________
0064 void AlignableDetUnit::addComponent(Alignable* /*unused*/) {
0065   throw cms::Exception("LogicError") << "AlignableDetUnit cannot have components, but try to add one!";
0066 }
0067 
0068 //__________________________________________________________________________________________________
0069 void AlignableDetUnit::move(const GlobalVector& displacement) {
0070   theSurface.move(displacement);
0071   this->addDisplacement(displacement);
0072 }
0073 
0074 //__________________________________________________________________________________________________
0075 void AlignableDetUnit::rotateInGlobalFrame(const RotationType& rotation) {
0076   theSurface.rotate(rotation);
0077   this->addRotation(rotation);
0078 }
0079 
0080 //__________________________________________________________________________________________________
0081 void AlignableDetUnit::setAlignmentPositionError(const AlignmentPositionError& ape, bool /*propagateDown*/) {
0082   if (!theAlignmentPositionError)
0083     theAlignmentPositionError = new AlignmentPositionError(ape);
0084   else
0085     *theAlignmentPositionError = ape;
0086 }
0087 
0088 //__________________________________________________________________________________________________
0089 void AlignableDetUnit::addAlignmentPositionError(const AlignmentPositionError& ape, bool propagateDown) {
0090   if (!theAlignmentPositionError)
0091     this->setAlignmentPositionError(ape, propagateDown);  // 2nd argument w/o effect
0092   else
0093     *theAlignmentPositionError += ape;
0094 }
0095 
0096 //__________________________________________________________________________________________________
0097 void AlignableDetUnit::addAlignmentPositionErrorFromRotation(const RotationType& rot, bool propagateDown) {
0098   // average error calculated by movement of a local point at
0099   // (xWidth/2,yLength/2,0) caused by the rotation rot
0100   GlobalVector localPositionVector =
0101       surface().toGlobal(LocalVector(.5 * surface().width(), .5 * surface().length(), 0.));
0102 
0103   const LocalVector::BasicVectorType& lpvgf = localPositionVector.basicVector();
0104   GlobalVector gv(rot.multiplyInverse(lpvgf) - lpvgf);
0105 
0106   AlignmentPositionError ape(gv.x(), gv.y(), gv.z());
0107   this->addAlignmentPositionError(ape, propagateDown);  // 2nd argument w/o effect
0108 }
0109 
0110 //__________________________________________________________________________________________________
0111 void AlignableDetUnit::addAlignmentPositionErrorFromLocalRotation(const RotationType& rot, bool propagateDown) {
0112   RotationType globalRot = globalRotation().multiplyInverse(rot * globalRotation());
0113   this->addAlignmentPositionErrorFromRotation(globalRot, propagateDown);  // 2nd argument w/o effect
0114 }
0115 
0116 //__________________________________________________________________________________________________
0117 void AlignableDetUnit::setSurfaceDeformation(const SurfaceDeformation* deformation, bool /* propagateDown */) {
0118   delete theSurfaceDeformation;  // OK for zero pointers
0119   if (deformation) {
0120     theSurfaceDeformation = deformation->clone();
0121   } else {
0122     theSurfaceDeformation = nullptr;
0123   }
0124 }
0125 
0126 //__________________________________________________________________________________________________
0127 void AlignableDetUnit::addSurfaceDeformation(const SurfaceDeformation* deformation, bool propagateDown) {
0128   if (!deformation) {
0129     // nothing to do
0130   } else if (!theSurfaceDeformation) {
0131     this->setSurfaceDeformation(deformation, propagateDown);  // fine since no components
0132   } else if (!theSurfaceDeformation->add(*deformation)) {
0133     edm::LogError("Alignment") << "@SUB=AlignableDetUnit::addSurfaceDeformation"
0134                                << "Cannot add deformation type " << deformation->type() << " to type "
0135                                << theSurfaceDeformation->type() << ", so erase deformation information.";
0136     delete theSurfaceDeformation;
0137     theSurfaceDeformation = nullptr;
0138   }
0139 }
0140 
0141 //__________________________________________________________________________________________________
0142 void AlignableDetUnit::dump() const {
0143   std::ostringstream parameters;
0144   if (theSurfaceDeformation) {
0145     parameters << "    surface deformation parameters:";
0146     for (const auto& param : theSurfaceDeformation->parameters()) {
0147       parameters << " " << param;
0148     }
0149   } else {
0150     parameters << "    no surface deformation parameters";
0151   }
0152 
0153   edm::LogInfo("AlignableDump") << " AlignableDetUnit has position = " << this->globalPosition()
0154                                 << ", orientation:" << std::endl
0155                                 << this->globalRotation() << std::endl
0156                                 << " total displacement and rotation: " << this->displacement() << std::endl
0157                                 << this->rotation() << "\n"
0158                                 << parameters.str();
0159 }
0160 
0161 //__________________________________________________________________________________________________
0162 Alignments* AlignableDetUnit::alignments() const {
0163   Alignments* m_alignments = new Alignments();
0164   RotationType rot(this->globalRotation());
0165 
0166   // Get alignments (position, rotation, detId)
0167   CLHEP::Hep3Vector clhepVector(globalPosition().x(), globalPosition().y(), globalPosition().z());
0168   CLHEP::HepRotation clhepRotation(
0169       CLHEP::HepRep3x3(rot.xx(), rot.xy(), rot.xz(), rot.yx(), rot.yy(), rot.yz(), rot.zx(), rot.zy(), rot.zz()));
0170   uint32_t detId = this->geomDetId().rawId();
0171 
0172   AlignTransform transform(clhepVector, clhepRotation, detId);
0173 
0174   // Add to alignments container
0175   m_alignments->m_align.push_back(transform);
0176 
0177   return m_alignments;
0178 }
0179 
0180 //__________________________________________________________________________________________________
0181 AlignmentErrorsExtended* AlignableDetUnit::alignmentErrors() const {
0182   AlignmentErrorsExtended* m_alignmentErrors = new AlignmentErrorsExtended();
0183 
0184   uint32_t detId = this->geomDetId().rawId();
0185 
0186   CLHEP::HepSymMatrix clhepSymMatrix(6, 0);
0187   if (theAlignmentPositionError)  // Might not be set
0188     clhepSymMatrix = asHepMatrix(theAlignmentPositionError->globalError().matrix());
0189 
0190   AlignTransformErrorExtended transformError(clhepSymMatrix, detId);
0191 
0192   m_alignmentErrors->m_alignError.push_back(transformError);
0193 
0194   return m_alignmentErrors;
0195 }
0196 
0197 //__________________________________________________________________________________________________
0198 int AlignableDetUnit::surfaceDeformationIdPairs(std::vector<std::pair<int, SurfaceDeformation*> >& result) const {
0199   if (theSurfaceDeformation) {
0200     result.push_back(std::pair<int, SurfaceDeformation*>(this->geomDetId().rawId(), theSurfaceDeformation));
0201     return 1;
0202   }
0203 
0204   return 0;
0205 }
0206 
0207 //__________________________________________________________________________________________________
0208 void AlignableDetUnit::cacheTransformation() {
0209   theCachedSurface = theSurface;
0210   theCachedDisplacement = theDisplacement;
0211   theCachedRotation = theRotation;
0212 
0213   if (theCachedSurfaceDeformation) {
0214     delete theCachedSurfaceDeformation;
0215     theCachedSurfaceDeformation = nullptr;
0216   }
0217 
0218   if (theSurfaceDeformation)
0219     theCachedSurfaceDeformation = theSurfaceDeformation->clone();
0220 }
0221 
0222 //__________________________________________________________________________________________________
0223 void AlignableDetUnit::cacheTransformation(const align::RunNumber& run) {
0224   surfacesCache_[run] = theSurface;
0225   displacementsCache_[run] = theDisplacement;
0226   rotationsCache_[run] = theRotation;
0227 
0228   auto existingCache = surfaceDeformationsCache_.find(run);
0229   if (existingCache != surfaceDeformationsCache_.end()) {
0230     delete existingCache->second;
0231     existingCache->second = nullptr;
0232   }
0233 
0234   if (theSurfaceDeformation) {
0235     surfaceDeformationsCache_[run] = theSurfaceDeformation->clone();
0236   }
0237 }
0238 
0239 //__________________________________________________________________________________________________
0240 void AlignableDetUnit::restoreCachedTransformation() {
0241   theSurface = theCachedSurface;
0242   theDisplacement = theCachedDisplacement;
0243   theRotation = theCachedRotation;
0244 
0245   if (theSurfaceDeformation) {
0246     delete theSurfaceDeformation;
0247     theSurfaceDeformation = nullptr;
0248   }
0249 
0250   if (theCachedSurfaceDeformation) {
0251     this->setSurfaceDeformation(theCachedSurfaceDeformation, false);
0252   }
0253 }
0254 
0255 //__________________________________________________________________________________________________
0256 void AlignableDetUnit::restoreCachedTransformation(const align::RunNumber& run) {
0257   if (surfacesCache_.find(run) == surfacesCache_.end()) {
0258     throw cms::Exception("Alignment") << "@SUB=Alignable::restoreCachedTransformation\n"
0259                                       << "Trying to restore cached transformation for a run (" << run
0260                                       << ") that has not been cached.";
0261   } else {
0262     theSurface = surfacesCache_[run];
0263     theDisplacement = displacementsCache_[run];
0264     theRotation = rotationsCache_[run];
0265 
0266     if (theSurfaceDeformation) {
0267       delete theSurfaceDeformation;
0268       theSurfaceDeformation = nullptr;
0269     }
0270 
0271     if (surfaceDeformationsCache_[run]) {
0272       this->setSurfaceDeformation(surfaceDeformationsCache_[run], false);
0273     }
0274   }
0275 }
0276 
0277 //______________________________________________________________________________
0278 const align::Alignables AlignableDetUnit::emptyComponents_{};