Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "Alignment/CommonAlignment/interface/AlignableDetUnit.h"
0002 #include "CondFormats/Alignment/interface/Alignments.h"
0003 #include "CondFormats/Alignment/interface/AlignmentErrorsExtended.h"
0004 #include "CLHEP/Vector/RotationInterfaces.h"
0005 #include "DataFormats/GeometryCommonDetAlgo/interface/AlignmentPositionError.h"
0006 #include "Geometry/CommonDetUnit/interface/GeomDet.h"
0007 
0008 #include "Alignment/CommonAlignment/interface/AlignableDet.h"
0009 
0010 #include "FWCore/Utilities/interface/Exception.h"
0011 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0012 
0013 //__________________________________________________________________________________________________
0014 AlignableDet::AlignableDet(const GeomDet* geomDet, bool addComponents)
0015     : AlignableComposite(geomDet), theAlignmentPositionError(nullptr) {
0016   // ensure that the surface is not constrained to the average position of the
0017   // components:
0018   compConstraintType_ = Alignable::CompConstraintType::NONE;
0019 
0020   if (geomDet->alignmentPositionError()) {
0021     // false: do not propagate APE to (anyway not yet existing) daughters
0022     this->setAlignmentPositionError(*(geomDet->alignmentPositionError()), false);
0023   }
0024 
0025   if (addComponents) {
0026     if (geomDet->components().empty()) {  // Is a DetUnit
0027       throw cms::Exception("BadHierarchy") << "[AlignableDet] GeomDet with DetId " << geomDet->geographicalId().rawId()
0028                                            << " has no components, use AlignableDetUnit.\n";
0029     } else {  // Push back all components
0030       const std::vector<const GeomDet*>& geomDets = geomDet->components();
0031       for (std::vector<const GeomDet*>::const_iterator idet = geomDets.begin(); idet != geomDets.end(); ++idet) {
0032         const GeomDetUnit* unit = dynamic_cast<const GeomDetUnit*>(*idet);
0033         if (!unit) {
0034           throw cms::Exception("BadHierarchy")
0035               << "[AlignableDet] component not GeomDetUnit, call with addComponents==false"
0036               << " and build hierarchy yourself.\n";  // e.g. AlignableDTChamber
0037         }
0038         this->addComponent(new AlignableDetUnit(unit));
0039       }
0040     }
0041     // Ensure that the surface is not screwed up by addComponent, it must stay the GeomDet's one:
0042     theSurface = AlignableSurface(geomDet->surface());
0043   }  // end addComponents
0044 }
0045 
0046 //__________________________________________________________________________________________________
0047 AlignableDet::~AlignableDet() { delete theAlignmentPositionError; }
0048 
0049 //______________________________________________________________________________
0050 void AlignableDet::update(const GeomDet* geomDet, bool updateComponents) {
0051   AlignableComposite::update(geomDet);
0052 
0053   if (geomDet->alignmentPositionError()) {
0054     // false: do not propagate APE to daughters (done by their update functions)
0055     this->setAlignmentPositionError(*(geomDet->alignmentPositionError()), false);
0056   }
0057 
0058   if (updateComponents) {
0059     if (geomDet->components().empty()) {  // Is a DetUnit
0060       throw cms::Exception("BadHierarchy") << "[AlignableDet] GeomDet with DetId " << geomDet->geographicalId().rawId()
0061                                            << " has no components, use AlignableDetUnit.\n";
0062     } else {  // Push back all components
0063       const auto& geomDets = geomDet->components();
0064       for (const auto& idet : geomDets) {
0065         auto unit = dynamic_cast<const GeomDetUnit*>(idet);
0066         if (!unit) {
0067           throw cms::Exception("BadHierarchy") << "[AlignableDet] component not GeomDetUnit, call with "
0068                                                << "updateComponents==false and build hierarchy yourself.\n";
0069           // -> e.g. AlignableDTChamber
0070         }
0071 
0072         const auto components = this->components();
0073         auto comp = std::find_if(components.begin(), components.end(), [&unit](const auto& c) {
0074           return c->id() == unit->geographicalId().rawId();
0075         });
0076 
0077         if (comp != components.end()) {
0078           auto aliDetUnit = dynamic_cast<AlignableDetUnit*>(*comp);
0079           if (aliDetUnit) {
0080             aliDetUnit->update(unit);
0081           } else {
0082             throw cms::Exception("LogicError") << "[AlignableDet::update] cast to 'AlignableDetUnit*' failed "
0083                                                << "while it should not\n";
0084           }
0085         } else {
0086           throw cms::Exception("GeometryMismatch")
0087               << "[AlignableDet::update] GeomDet with DetId " << unit->geographicalId().rawId()
0088               << " not found in current geometry.\n";
0089         }
0090       }
0091     }
0092     // Ensure that the surface is not screwed up by update of components, it must stay the GeomDet's one:
0093     theSurface = AlignableSurface(geomDet->surface());
0094   }  // end updateComponents
0095 }
0096 
0097 //__________________________________________________________________________________________________
0098 void AlignableDet::setAlignmentPositionError(const AlignmentPositionError& ape, bool propagateDown) {
0099   if (!theAlignmentPositionError)
0100     theAlignmentPositionError = new AlignmentPositionError(ape);
0101   else
0102     *theAlignmentPositionError = ape;
0103 
0104   this->AlignableComposite::setAlignmentPositionError(ape, propagateDown);
0105 }
0106 
0107 //__________________________________________________________________________________________________
0108 void AlignableDet::addAlignmentPositionError(const AlignmentPositionError& ape, bool propagateDown) {
0109   if (!theAlignmentPositionError) {
0110     theAlignmentPositionError = new AlignmentPositionError(ape);
0111   } else {
0112     *theAlignmentPositionError += ape;
0113   }
0114 
0115   this->AlignableComposite::addAlignmentPositionError(ape, propagateDown);
0116 }
0117 
0118 //__________________________________________________________________________________________________
0119 void AlignableDet::addAlignmentPositionErrorFromRotation(const RotationType& rot, bool propagateDown) {
0120   // average error calculated by movement of a local point at
0121   // (xWidth/2,yLength/2,0) caused by the rotation rot
0122   GlobalVector localPositionVector =
0123       surface().toGlobal(LocalVector(.5 * surface().width(), .5 * surface().length(), 0.));
0124 
0125   const LocalVector::BasicVectorType& lpvgf = localPositionVector.basicVector();
0126   GlobalVector gv(rot.multiplyInverse(lpvgf) - lpvgf);
0127 
0128   AlignmentPositionError ape(gv.x(), gv.y(), gv.z());
0129   this->addAlignmentPositionError(ape, propagateDown);
0130 
0131   this->AlignableComposite::addAlignmentPositionErrorFromRotation(rot, propagateDown);
0132 }
0133 
0134 //__________________________________________________________________________________________________
0135 Alignments* AlignableDet::alignments() const {
0136   Alignments* m_alignments = new Alignments();
0137   RotationType rot(this->globalRotation());
0138 
0139   // Get position, rotation, detId
0140   CLHEP::Hep3Vector clhepVector(globalPosition().x(), globalPosition().y(), globalPosition().z());
0141   CLHEP::HepRotation clhepRotation(
0142       CLHEP::HepRep3x3(rot.xx(), rot.xy(), rot.xz(), rot.yx(), rot.yy(), rot.yz(), rot.zx(), rot.zy(), rot.zz()));
0143   uint32_t detId = this->geomDetId().rawId();
0144 
0145   AlignTransform transform(clhepVector, clhepRotation, detId);
0146 
0147   // Add to alignments container
0148   m_alignments->m_align.push_back(transform);
0149 
0150   // Add those from components
0151   Alignments* compAlignments = this->AlignableComposite::alignments();
0152   std::copy(compAlignments->m_align.begin(), compAlignments->m_align.end(), std::back_inserter(m_alignments->m_align));
0153   delete compAlignments;
0154 
0155   return m_alignments;
0156 }
0157 
0158 //__________________________________________________________________________________________________
0159 AlignmentErrorsExtended* AlignableDet::alignmentErrors(void) const {
0160   AlignmentErrorsExtended* m_alignmentErrors = new AlignmentErrorsExtended();
0161 
0162   // Add associated alignment position error
0163   uint32_t detId = this->geomDetId().rawId();
0164   CLHEP::HepSymMatrix clhepSymMatrix(6, 0);
0165   if (theAlignmentPositionError)  // Might not be set
0166     clhepSymMatrix = asHepMatrix(theAlignmentPositionError->globalError().matrix());
0167   AlignTransformErrorExtended transformError(clhepSymMatrix, detId);
0168   m_alignmentErrors->m_alignError.push_back(transformError);
0169 
0170   // Add those from components
0171   AlignmentErrorsExtended* compAlignmentErrs = this->AlignableComposite::alignmentErrors();
0172   std::copy(compAlignmentErrs->m_alignError.begin(),
0173             compAlignmentErrs->m_alignError.end(),
0174             std::back_inserter(m_alignmentErrors->m_alignError));
0175   delete compAlignmentErrs;
0176 
0177   return m_alignmentErrors;
0178 }