Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:56:06

0001 /** \file Alignable.cc
0002  *
0003  *  $Date: 2012/12/11 19:11:02 $
0004  *  $Revision: 1.23 $
0005  *  (last update by $Author: flucke $)
0006  */
0007 
0008 #include "Alignment/CommonAlignment/interface/AlignmentParameters.h"
0009 #include "Alignment/CommonAlignment/interface/SurveyDet.h"
0010 
0011 #include "Alignment/CommonAlignment/interface/Alignable.h"
0012 
0013 #include "CondFormats/Alignment/interface/AlignmentSurfaceDeformations.h"
0014 
0015 #include "Geometry/CommonTopologies/interface/SurfaceDeformation.h"
0016 #include "FWCore/Utilities/interface/Exception.h"
0017 
0018 //__________________________________________________________________________________________________
0019 Alignable::Alignable(align::ID id, const AlignableSurface& surf)
0020     : theDetId(id),  // FIXME: inconsistent with other ctr., but needed for AlignableNavigator
0021       theId(id),     // (finally get rid of one of the IDs!)
0022       theSurface(surf),
0023       theCachedSurface(surf),
0024       theAlignmentParameters(nullptr),
0025       theMother(nullptr),
0026       theSurvey(nullptr) {}
0027 
0028 //__________________________________________________________________________________________________
0029 Alignable::Alignable(align::ID id, const RotationType& rot)
0030     : theDetId(),  // FIXME: inconsistent with other ctr., cf. above
0031       theId(id),
0032       theSurface(PositionType(), rot),
0033       theCachedSurface(PositionType(), rot),
0034       theAlignmentParameters(nullptr),
0035       theMother(nullptr),
0036       theSurvey(nullptr) {}
0037 
0038 //__________________________________________________________________________________________________
0039 Alignable::~Alignable() {
0040   delete theAlignmentParameters;
0041   delete theSurvey;
0042 }
0043 
0044 //__________________________________________________________________________________________________
0045 void Alignable::update(align::ID id, const AlignableSurface& surf) {
0046   if (theId != id) {
0047     throw cms::Exception("Alignment") << "@SUB=Alignable::update\n"
0048                                       << "Current alignable ID does not match ID of the update.";
0049   }
0050   const auto shift = surf.position() - theSurface.position();
0051   theSurface = surf;
0052 
0053   // reset displacement and rotations after update
0054   theDisplacement = GlobalVector();
0055   theRotation = RotationType();
0056 
0057   // recalculate containing composite's position
0058   updateMother(shift);
0059 }
0060 
0061 //__________________________________________________________________________________________________
0062 bool Alignable::firstCompsWithParams(Alignables& paramComps) const {
0063   bool isConsistent = true;
0064   bool hasAliComp = false;  // whether there are any (grand-) daughters with parameters
0065   bool first = true;
0066   const auto& comps = this->components();
0067   for (const auto& iComp : comps) {
0068     if (iComp->alignmentParameters()) {  // component has parameters itself
0069       paramComps.push_back(iComp);
0070       if (!first && !hasAliComp)
0071         isConsistent = false;
0072       hasAliComp = true;
0073     } else {
0074       const unsigned int nCompBefore = paramComps.size();
0075       if (!(iComp->firstCompsWithParams(paramComps))) {
0076         isConsistent = false;  // problem down in hierarchy
0077       }
0078       if (paramComps.size() != nCompBefore) {
0079         if (!first && !hasAliComp)
0080           isConsistent = false;
0081         hasAliComp = true;
0082       } else if (hasAliComp) {  // no components with params, but previous component did have comps.
0083         isConsistent = false;
0084       }
0085     }
0086     first = false;
0087   }
0088 
0089   return isConsistent;
0090 }
0091 
0092 //__________________________________________________________________________________________________
0093 bool Alignable::lastCompsWithParams(Alignables& paramComps) const {
0094   bool isConsistent = true;
0095   bool hasAliComp = false;
0096   bool first = true;
0097   const auto& comps = this->components();
0098   for (const auto& iComp : comps) {
0099     const auto nCompsBefore = paramComps.size();
0100     isConsistent = iComp->lastCompsWithParams(paramComps);
0101     if (paramComps.size() == nCompsBefore) {
0102       if (iComp->alignmentParameters()) {
0103         paramComps.push_back(iComp);
0104         if (!first && !hasAliComp)
0105           isConsistent = false;
0106         hasAliComp = true;
0107       }
0108     } else {
0109       if (hasAliComp) {
0110         isConsistent = false;
0111       }
0112       if (!first && !hasAliComp)
0113         isConsistent = false;
0114       hasAliComp = true;
0115     }
0116     first = false;
0117   }
0118 
0119   return isConsistent;
0120 }
0121 
0122 //__________________________________________________________________________________________________
0123 void Alignable::setAlignmentParameters(AlignmentParameters* dap) {
0124   delete theAlignmentParameters;
0125   theAlignmentParameters = dap;
0126 }
0127 
0128 //__________________________________________________________________________________________________
0129 void Alignable::rotateInLocalFrame(const RotationType& rotation) {
0130   // This is done by simply transforming the rotation from
0131   // the local system O to the global one  O^-1 * Rot * O
0132   // and then applying the global rotation  O * Rot
0133 
0134   rotateInGlobalFrame(surface().toGlobal(rotation));
0135 }
0136 
0137 //__________________________________________________________________________________________________
0138 void Alignable::rotateAroundGlobalAxis(const GlobalVector& axis, Scalar radians) {
0139   rotateInGlobalFrame(RotationType(axis.basicVector(), radians));
0140 }
0141 
0142 //__________________________________________________________________________________________________
0143 void Alignable::rotateAroundLocalAxis(const LocalVector& axis, Scalar radians) {
0144   rotateInLocalFrame(RotationType(axis.basicVector(), radians));
0145 }
0146 
0147 //__________________________________________________________________________________________________
0148 void Alignable::rotateAroundGlobalX(Scalar radians) {
0149   RotationType rot(1., 0., 0., 0., std::cos(radians), std::sin(radians), 0., -std::sin(radians), std::cos(radians));
0150 
0151   rotateInGlobalFrame(rot);
0152 }
0153 
0154 //__________________________________________________________________________________________________
0155 void Alignable::rotateAroundLocalX(Scalar radians) {
0156   RotationType rot(1., 0., 0., 0., std::cos(radians), std::sin(radians), 0., -std::sin(radians), std::cos(radians));
0157 
0158   rotateInLocalFrame(rot);
0159 }
0160 
0161 //__________________________________________________________________________________________________
0162 void Alignable::rotateAroundGlobalY(Scalar radians) {
0163   RotationType rot(std::cos(radians), 0., -std::sin(radians), 0., 1., 0., std::sin(radians), 0., std::cos(radians));
0164 
0165   rotateInGlobalFrame(rot);
0166 }
0167 
0168 //__________________________________________________________________________________________________
0169 void Alignable::rotateAroundLocalY(Scalar radians) {
0170   RotationType rot(std::cos(radians), 0., -std::sin(radians), 0., 1., 0., std::sin(radians), 0., std::cos(radians));
0171 
0172   rotateInLocalFrame(rot);
0173 }
0174 
0175 //__________________________________________________________________________________________________
0176 void Alignable::rotateAroundGlobalZ(Scalar radians) {
0177   RotationType rot(std::cos(radians), std::sin(radians), 0., -std::sin(radians), std::cos(radians), 0., 0., 0., 1.);
0178 
0179   rotateInGlobalFrame(rot);
0180 }
0181 
0182 //__________________________________________________________________________________________________
0183 void Alignable::rotateAroundLocalZ(Scalar radians) {
0184   RotationType rot(std::cos(radians), std::sin(radians), 0., -std::sin(radians), std::cos(radians), 0., 0., 0., 1.);
0185 
0186   rotateInLocalFrame(rot);
0187 }
0188 
0189 //__________________________________________________________________________________________________
0190 void Alignable::addDisplacement(const GlobalVector& displacement) { theDisplacement += displacement; }
0191 
0192 //__________________________________________________________________________________________________
0193 void Alignable::addRotation(const RotationType& rotation) { theRotation *= rotation; }
0194 
0195 //__________________________________________________________________________________________________
0196 AlignmentSurfaceDeformations* Alignable::surfaceDeformations(void) const {
0197   typedef std::pair<int, SurfaceDeformation*> IdSurfaceDeformationPtrPair;
0198 
0199   std::vector<IdSurfaceDeformationPtrPair> result;
0200   surfaceDeformationIdPairs(result);
0201   std::sort(result.begin(), result.end(), [](auto& a, auto& b) { return a.first < b.first; });
0202 
0203   AlignmentSurfaceDeformations* allSurfaceDeformations = new AlignmentSurfaceDeformations();
0204 
0205   for (std::vector<IdSurfaceDeformationPtrPair>::const_iterator iPair = result.begin(); iPair != result.end();
0206        ++iPair) {
0207     // should we check for 'empty' parameters here (all zeros) and skip ?
0208     // may be add 'empty' method to SurfaceDeformation
0209     allSurfaceDeformations->add((*iPair).first, (*iPair).second->type(), (*iPair).second->parameters());
0210   }
0211 
0212   return allSurfaceDeformations;
0213 }
0214 
0215 void Alignable::cacheTransformation() {
0216   // first treat itself
0217   theCachedSurface = theSurface;
0218   theCachedDisplacement = theDisplacement;
0219   theCachedRotation = theRotation;
0220 
0221   // now treat components (a clean design would move that to AlignableComposite...)
0222   for (const auto& it : this->components())
0223     it->cacheTransformation();
0224 }
0225 
0226 void Alignable::cacheTransformation(const align::RunNumber& run) {
0227   // first treat itself
0228   surfacesCache_[run] = theSurface;
0229   displacementsCache_[run] = theDisplacement;
0230   rotationsCache_[run] = theRotation;
0231 
0232   // now treat components (a clean design would move that to AlignableComposite...)
0233   for (const auto& it : this->components())
0234     it->cacheTransformation(run);
0235 }
0236 
0237 void Alignable::restoreCachedTransformation() {
0238   // first treat itself
0239   theSurface = theCachedSurface;
0240   theDisplacement = theCachedDisplacement;
0241   theRotation = theCachedRotation;
0242 
0243   // now treat components (a clean design would move that to AlignableComposite...)
0244   for (const auto& it : this->components())
0245     it->restoreCachedTransformation();
0246 }
0247 
0248 void Alignable::restoreCachedTransformation(const align::RunNumber& run) {
0249   if (surfacesCache_.find(run) == surfacesCache_.end()) {
0250     throw cms::Exception("Alignment") << "@SUB=Alignable::restoreCachedTransformation\n"
0251                                       << "Trying to restore cached transformation for a run (" << run
0252                                       << ") that has not been cached.";
0253   } else {
0254     // first treat itself
0255     theSurface = surfacesCache_[run];
0256     theDisplacement = displacementsCache_[run];
0257     theRotation = rotationsCache_[run];
0258 
0259     // now treat components (a clean design would move that to AlignableComposite...)
0260     for (const auto& it : this->components())
0261       it->restoreCachedTransformation();
0262   }
0263 }
0264 
0265 //__________________________________________________________________________________________________
0266 void Alignable::setSurvey(const SurveyDet* survey) {
0267   delete theSurvey;
0268   theSurvey = survey;
0269 }
0270 
0271 //______________________________________________________________________________
0272 void Alignable::updateMother(const GlobalVector& shift) {
0273   if (!theMother)
0274     return;
0275 
0276   const auto thisComps = this->deepComponents().size();
0277   const auto motherComps = theMother->deepComponents().size();
0278   const auto motherShift = shift * static_cast<Scalar>(thisComps) / motherComps;
0279 
0280   switch (theMother->compConstraintType()) {
0281     case CompConstraintType::NONE:
0282       break;
0283     case CompConstraintType::POSITION_Z:
0284       theMother->theSurface.move(GlobalVector(0, 0, motherShift.z()));
0285       theMother->updateMother(GlobalVector(0, 0, motherShift.z()));
0286       break;
0287     case CompConstraintType::POSITION:
0288       theMother->theSurface.move(motherShift);
0289       theMother->updateMother(motherShift);
0290       break;
0291   }
0292 }
0293 
0294 //______________________________________________________________________________
0295 void Alignable::recenterSurface() {
0296   const auto& currentPosition = this->globalPosition();
0297   theSurface.move(align::GlobalVector{-currentPosition.x(), -currentPosition.y(), -currentPosition.z()});
0298 }