Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-04-08 00:34:53

0001 // system include files
0002 #include <memory>
0003 
0004 // user include files
0005 #include "FWCore/Framework/interface/EventSetup.h"
0006 #include "FWCore/Framework/interface/ESHandle.h"
0007 
0008 // Conditions database
0009 #include "FWCore/ServiceRegistry/interface/Service.h"
0010 #include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
0011 
0012 // Alignment
0013 #include "CondFormats/Alignment/interface/Alignments.h"
0014 #include "CondFormats/Alignment/interface/AlignmentErrorsExtended.h"
0015 #include "CondFormats/Alignment/interface/SurveyErrors.h"
0016 #include "Alignment/CommonAlignment/interface/SurveyDet.h"
0017 #include "Alignment/MuonAlignment/interface/MuonAlignment.h"
0018 
0019 #include "Alignment/MuonAlignment/interface/MuonAlignmentOutputXML.h"
0020 
0021 #include "Alignment/CommonAlignment/interface/Alignable.h"
0022 #include "Alignment/CommonAlignment/interface/SurveyDet.h"
0023 #include "DataFormats/GeometryCommonDetAlgo/interface/AlignmentPositionError.h"
0024 #include "DataFormats/GeometryCommonDetAlgo/interface/GlobalError.h"
0025 
0026 //____________________________________________________________________________________
0027 //
0028 void MuonAlignment::init() {
0029   theDTAlignRecordName = "DTAlignmentRcd";
0030   theDTErrorRecordName = "DTAlignmentErrorExtendedRcd";
0031   theCSCAlignRecordName = "CSCAlignmentRcd";
0032   theCSCErrorRecordName = "CSCAlignmentErrorExtendedRcd";
0033   theGEMAlignRecordName = "GEMAlignmentRcd";
0034   theGEMErrorRecordName = "GEMAlignmentErrorExtendedRcd";
0035   theDTSurveyRecordName = "DTSurveyRcd";
0036   theDTSurveyErrorRecordName = "DTSurveyErrorExtendedRcd";
0037   theCSCSurveyRecordName = "CSCSurveyRcd";
0038   theCSCSurveyErrorRecordName = "CSCSurveyErrorExtendedRcd";
0039   theAlignableMuon = nullptr;
0040   theAlignableNavigator = nullptr;
0041 }
0042 
0043 MuonAlignment::MuonAlignment(const DTGeometry* dtGeometry,
0044                              const CSCGeometry* cscGeometry,
0045                              const GEMGeometry* gemGeometry)
0046     : dtGeometry_(dtGeometry), cscGeometry_(cscGeometry), gemGeometry_(gemGeometry) {
0047   init();
0048 
0049   theAlignableMuon = new AlignableMuon(&*dtGeometry_, &*cscGeometry_, &*gemGeometry_);
0050   theAlignableNavigator = new AlignableNavigator(theAlignableMuon);
0051 }
0052 
0053 MuonAlignment::MuonAlignment(const edm::EventSetup& iSetup, const MuonAlignmentInputMethod& input) {
0054   init();
0055 
0056   theAlignableMuon = input.newAlignableMuon();
0057   theAlignableNavigator = new AlignableNavigator(theAlignableMuon);
0058 }
0059 
0060 //____________________________________________________________________________________
0061 //
0062 void MuonAlignment::moveAlignableLocalCoord(DetId& detid, align::Scalars& displacements, align::Scalars& rotations) {
0063   // Displace and rotate DT an Alignable associated to a GeomDet or GeomDetUnit
0064   Alignable* theAlignable = theAlignableNavigator->alignableFromDetId(detid);
0065 
0066   // Convert local to global diplacements
0067   align::LocalVector lvector(displacements.at(0), displacements.at(1), displacements.at(2));
0068   align::GlobalVector gvector = (theAlignable->surface()).toGlobal(lvector);
0069 
0070   // global displacement of the chamber
0071   theAlignable->move(gvector);
0072 
0073   // local rotation of the chamber
0074   theAlignable->rotateAroundLocalX(rotations.at(0));  // Local X axis rotation
0075   theAlignable->rotateAroundLocalY(rotations.at(1));  // Local Y axis rotation
0076   theAlignable->rotateAroundLocalZ(rotations.at(2));  // Local Z axis rotation
0077 }
0078 
0079 //____________________________________________________________________________________
0080 //
0081 void MuonAlignment::moveAlignableGlobalCoord(DetId& detid, align::Scalars& displacements, align::Scalars& rotations) {
0082   // Displace and rotate DT an Alignable associated to a GeomDet or GeomDetUnit
0083   Alignable* theAlignable = theAlignableNavigator->alignableFromDetId(detid);
0084 
0085   // Convert std::vector to GlobalVector
0086   align::GlobalVector gvector(displacements.at(0), displacements.at(1), displacements.at(2));
0087 
0088   // global displacement of the chamber
0089   theAlignable->move(gvector);
0090 
0091   // local rotation of the chamber
0092   theAlignable->rotateAroundGlobalX(rotations.at(0));  // Global X axis rotation
0093   theAlignable->rotateAroundGlobalY(rotations.at(1));  // Global Y axis rotation
0094   theAlignable->rotateAroundGlobalZ(rotations.at(2));  // Global Z axis rotation
0095 }
0096 
0097 //____________________________________________________________________________________
0098 //
0099 void MuonAlignment::recursiveList(const align::Alignables& alignables, align::Alignables& theList) {
0100   for (align::Alignables::const_iterator alignable = alignables.begin(); alignable != alignables.end(); ++alignable) {
0101     recursiveList((*alignable)->components(), theList);
0102     theList.push_back(*alignable);
0103   }
0104 }
0105 
0106 //____________________________________________________________________________________
0107 //
0108 void MuonAlignment::recursiveMap(const align::Alignables& alignables, std::map<align::ID, Alignable*>& theMap) {
0109   for (align::Alignables::const_iterator alignable = alignables.begin(); alignable != alignables.end(); ++alignable) {
0110     unsigned int rawId = (*alignable)->geomDetId().rawId();
0111     if (rawId != 0) {
0112       theMap[rawId] = *alignable;
0113     }
0114     recursiveMap((*alignable)->components(), theMap);
0115   }
0116 }
0117 
0118 //____________________________________________________________________________________
0119 //
0120 void MuonAlignment::recursiveStructureMap(const align::Alignables& alignables,
0121                                           std::map<std::pair<align::StructureType, align::ID>, Alignable*>& theMap) {
0122   for (align::Alignables::const_iterator alignable = alignables.begin(); alignable != alignables.end(); ++alignable) {
0123     theMap[std::pair<align::StructureType, align::ID>((*alignable)->alignableObjectId(), (*alignable)->id())] =
0124         *alignable;
0125     recursiveStructureMap((*alignable)->components(), theMap);
0126   }
0127 }
0128 
0129 //____________________________________________________________________________________
0130 //
0131 void MuonAlignment::copyAlignmentToSurvey(double shiftErr, double angleErr) {
0132   std::map<align::ID, Alignable*> alignableMap;
0133   recursiveMap(theAlignableMuon->DTBarrel(), alignableMap);
0134   recursiveMap(theAlignableMuon->CSCEndcaps(), alignableMap);
0135   recursiveMap(theAlignableMuon->GEMEndcaps(), alignableMap);
0136 
0137   // Set the survey error to the alignable error, expanding the matrix as needed
0138   AlignmentErrorsExtended* dtAlignmentErrorsExtended = theAlignableMuon->dtAlignmentErrorsExtended();
0139   AlignmentErrorsExtended* cscAlignmentErrorsExtended = theAlignableMuon->cscAlignmentErrorsExtended();
0140   AlignmentErrorsExtended* gemAlignmentErrorsExtended = theAlignableMuon->gemAlignmentErrorsExtended();
0141   std::vector<AlignTransformErrorExtended> alignmentErrors;
0142   std::copy(dtAlignmentErrorsExtended->m_alignError.begin(),
0143             dtAlignmentErrorsExtended->m_alignError.end(),
0144             std::back_inserter(alignmentErrors));
0145   std::copy(cscAlignmentErrorsExtended->m_alignError.begin(),
0146             cscAlignmentErrorsExtended->m_alignError.end(),
0147             std::back_inserter(alignmentErrors));
0148   std::copy(gemAlignmentErrorsExtended->m_alignError.begin(),
0149             gemAlignmentErrorsExtended->m_alignError.end(),
0150             std::back_inserter(alignmentErrors));
0151 
0152   for (std::vector<AlignTransformErrorExtended>::const_iterator alignmentError = alignmentErrors.begin();
0153        alignmentError != alignmentErrors.end();
0154        ++alignmentError) {
0155     align::ErrorMatrix matrix6x6 = ROOT::Math::SMatrixIdentity();  // start from (0, 0)
0156     CLHEP::HepSymMatrix matrix6x6new = alignmentError->matrix();   // start from (1, 1)
0157 
0158     for (int i = 0; i < 6; i++) {
0159       for (int j = 0; j < 6; j++) {
0160         matrix6x6(i, j) = matrix6x6new(i + 1, j + 1);
0161       }
0162     }
0163     //matrix6x6(3,3) = angleErr;
0164     //matrix6x6(4,4) = angleErr;
0165     //matrix6x6(5,5) = angleErr;
0166 
0167     Alignable* alignable = alignableMap[alignmentError->rawId()];
0168     alignable->setSurvey(new SurveyDet(alignable->surface(), matrix6x6));
0169   }
0170 
0171   fillGapsInSurvey(shiftErr, angleErr);
0172 }
0173 
0174 //____________________________________________________________________________________
0175 //
0176 
0177 void MuonAlignment::fillGapsInSurvey(double shiftErr, double angleErr) {
0178   // get all the ones we missed
0179   std::map<std::pair<align::StructureType, align::ID>, Alignable*> alignableStructureMap;
0180   recursiveStructureMap(theAlignableMuon->DTBarrel(), alignableStructureMap);
0181   recursiveStructureMap(theAlignableMuon->CSCEndcaps(), alignableStructureMap);
0182 
0183   for (std::map<std::pair<align::StructureType, align::ID>, Alignable*>::const_iterator iter =
0184            alignableStructureMap.begin();
0185        iter != alignableStructureMap.end();
0186        ++iter) {
0187     if (iter->second->survey() == nullptr) {
0188       align::ErrorMatrix matrix6x6 = ROOT::Math::SMatrixIdentity();
0189       matrix6x6(0, 0) = shiftErr;
0190       matrix6x6(1, 1) = shiftErr;
0191       matrix6x6(2, 2) = shiftErr;
0192       matrix6x6(3, 3) = angleErr;
0193       matrix6x6(4, 4) = angleErr;
0194       matrix6x6(5, 5) = angleErr;
0195       iter->second->setSurvey(new SurveyDet(iter->second->surface(), matrix6x6));
0196     }
0197   }
0198 }
0199 
0200 //____________________________________________________________________________________
0201 //
0202 void MuonAlignment::recursiveCopySurveyToAlignment(Alignable* alignable) {
0203   if (alignable->survey() != nullptr) {
0204     const SurveyDet* survey = alignable->survey();
0205 
0206     const align::PositionType& pos = survey->position();
0207     align::RotationType rot = survey->rotation();
0208 
0209     align::PositionType oldpos = alignable->globalPosition();
0210     align::RotationType oldrot = alignable->globalRotation();
0211     alignable->move(align::GlobalVector(-oldpos.x(), -oldpos.y(), -oldpos.z()));
0212     alignable->rotateInGlobalFrame(oldrot.transposed());
0213     alignable->rotateInGlobalFrame(rot);
0214     alignable->move(align::GlobalVector(pos.x(), pos.y(), pos.z()));
0215 
0216     align::ErrorMatrix matrix6x6 = survey->errors();  // start from 0,0
0217     AlgebraicSymMatrix66 matrix6x6new;                // start from 0,0
0218     for (int i = 0; i < 6; i++) {
0219       for (int j = 0; j <= i; j++) {
0220         matrix6x6new(i, j) = matrix6x6(i, j);
0221       }
0222     }
0223 
0224     // this sets APEs at this level and (since 2nd argument is true) all lower levels
0225     alignable->setAlignmentPositionError(AlignmentPositionError(GlobalErrorExtended(matrix6x6new)), true);
0226   }
0227 
0228   // do lower levels afterward to thwart the cumulative setting of APEs
0229   align::Alignables components = alignable->components();
0230   for (align::Alignables::const_iterator comp = components.begin(); comp != components.end(); ++comp) {
0231     recursiveCopySurveyToAlignment(*comp);
0232   }
0233 }
0234 
0235 void MuonAlignment::copySurveyToAlignment() { recursiveCopySurveyToAlignment(theAlignableMuon); }
0236 
0237 //____________________________________________________________________________________
0238 // Code needed to store alignments to DB
0239 
0240 void MuonAlignment::writeXML(const edm::ParameterSet& iConfig,
0241                              const DTGeometry* dtGeometryXML,
0242                              const CSCGeometry* cscGeometryXML,
0243                              const GEMGeometry* gemGeometryXML) {
0244   MuonAlignmentOutputXML(iConfig, dtGeometryXML, cscGeometryXML, gemGeometryXML).write(theAlignableMuon);
0245 }
0246 
0247 void MuonAlignment::saveDTSurveyToDB(void) {
0248   // Call service
0249   edm::Service<cond::service::PoolDBOutputService> poolDbService;
0250   if (!poolDbService.isAvailable())  // Die if not available
0251     throw cms::Exception("NotAvailable") << "PoolDBOutputService not available";
0252 
0253   // Get alignments and errors
0254   Alignments dtAlignments{};
0255   SurveyErrors dtSurveyErrors{};
0256 
0257   align::Alignables alignableList;
0258   recursiveList(theAlignableMuon->DTBarrel(), alignableList);
0259 
0260   for (align::Alignables::const_iterator alignable = alignableList.begin(); alignable != alignableList.end();
0261        ++alignable) {
0262     const align::PositionType& pos = (*alignable)->survey()->position();
0263     const align::RotationType& rot = (*alignable)->survey()->rotation();
0264 
0265     AlignTransform value(CLHEP::Hep3Vector(pos.x(), pos.y(), pos.z()),
0266                          CLHEP::HepRotation(CLHEP::HepRep3x3(
0267                              rot.xx(), rot.xy(), rot.xz(), rot.yx(), rot.yy(), rot.yz(), rot.zx(), rot.zy(), rot.zz())),
0268                          (*alignable)->id());
0269     SurveyError error((*alignable)->alignableObjectId(), (*alignable)->id(), (*alignable)->survey()->errors());
0270 
0271     dtAlignments.m_align.push_back(value);
0272     dtSurveyErrors.m_surveyErrors.push_back(error);
0273   }
0274 
0275   // Store DT alignments and errors
0276   poolDbService->writeOneIOV<Alignments>(dtAlignments, poolDbService->currentTime(), theDTSurveyRecordName);
0277   poolDbService->writeOneIOV<SurveyErrors>(dtSurveyErrors, poolDbService->currentTime(), theDTSurveyErrorRecordName);
0278 }
0279 
0280 void MuonAlignment::saveCSCSurveyToDB(void) {
0281   // Call service
0282   edm::Service<cond::service::PoolDBOutputService> poolDbService;
0283   if (!poolDbService.isAvailable())  // Die if not available
0284     throw cms::Exception("NotAvailable") << "PoolDBOutputService not available";
0285 
0286   // Get alignments and errors
0287   Alignments cscAlignments{};
0288   SurveyErrors cscSurveyErrors{};
0289 
0290   align::Alignables alignableList;
0291   recursiveList(theAlignableMuon->CSCEndcaps(), alignableList);
0292 
0293   for (align::Alignables::const_iterator alignable = alignableList.begin(); alignable != alignableList.end();
0294        ++alignable) {
0295     const align::PositionType& pos = (*alignable)->survey()->position();
0296     const align::RotationType& rot = (*alignable)->survey()->rotation();
0297 
0298     AlignTransform value(CLHEP::Hep3Vector(pos.x(), pos.y(), pos.z()),
0299                          CLHEP::HepRotation(CLHEP::HepRep3x3(
0300                              rot.xx(), rot.xy(), rot.xz(), rot.yx(), rot.yy(), rot.yz(), rot.zx(), rot.zy(), rot.zz())),
0301                          (*alignable)->id());
0302     SurveyError error((*alignable)->alignableObjectId(), (*alignable)->id(), (*alignable)->survey()->errors());
0303 
0304     cscAlignments.m_align.push_back(value);
0305     cscSurveyErrors.m_surveyErrors.push_back(error);
0306   }
0307 
0308   // Store CSC alignments and errors
0309   poolDbService->writeOneIOV<Alignments>(cscAlignments, poolDbService->currentTime(), theCSCSurveyRecordName);
0310   poolDbService->writeOneIOV<SurveyErrors>(cscSurveyErrors, poolDbService->currentTime(), theCSCSurveyErrorRecordName);
0311 }
0312 
0313 void MuonAlignment::saveSurveyToDB(void) {
0314   saveDTSurveyToDB();
0315   saveCSCSurveyToDB();
0316 }
0317 
0318 void MuonAlignment::saveDTtoDB(void) {
0319   // Call service
0320   edm::Service<cond::service::PoolDBOutputService> poolDbService;
0321   if (!poolDbService.isAvailable())  // Die if not available
0322     throw cms::Exception("NotAvailable") << "PoolDBOutputService not available";
0323 
0324   // Get alignments and errors
0325   Alignments dt_Alignments = *(theAlignableMuon->dtAlignments());
0326   AlignmentErrorsExtended dt_AlignmentErrorsExtended = *(theAlignableMuon->dtAlignmentErrorsExtended());
0327 
0328   // Store DT alignments and errors
0329   poolDbService->writeOneIOV<Alignments>(dt_Alignments, poolDbService->currentTime(), theDTAlignRecordName);
0330   poolDbService->writeOneIOV<AlignmentErrorsExtended>(
0331       dt_AlignmentErrorsExtended, poolDbService->currentTime(), theDTErrorRecordName);
0332 }
0333 
0334 void MuonAlignment::saveCSCtoDB(void) {
0335   // Call service
0336   edm::Service<cond::service::PoolDBOutputService> poolDbService;
0337   if (!poolDbService.isAvailable())  // Die if not available
0338     throw cms::Exception("NotAvailable") << "PoolDBOutputService not available";
0339 
0340   // Get alignments and errors
0341   Alignments csc_Alignments = *(theAlignableMuon->cscAlignments());
0342   AlignmentErrorsExtended csc_AlignmentErrorsExtended = *(theAlignableMuon->cscAlignmentErrorsExtended());
0343 
0344   // Store CSC alignments and errors
0345   poolDbService->writeOneIOV<Alignments>(csc_Alignments, poolDbService->currentTime(), theCSCAlignRecordName);
0346   poolDbService->writeOneIOV<AlignmentErrorsExtended>(
0347       csc_AlignmentErrorsExtended, poolDbService->currentTime(), theCSCErrorRecordName);
0348 }
0349 
0350 void MuonAlignment::saveGEMtoDB(void) {
0351   // Call service
0352   edm::Service<cond::service::PoolDBOutputService> poolDbService;
0353   if (!poolDbService.isAvailable())  // Die if not available
0354     throw cms::Exception("NotAvailable") << "PoolDBOutputService not available";
0355 
0356   // Get alignments and errors
0357   Alignments gem_Alignments = *(theAlignableMuon->gemAlignments());
0358   AlignmentErrorsExtended gem_AlignmentErrorsExtended = *(theAlignableMuon->gemAlignmentErrorsExtended());
0359 
0360   // Store CSC alignments and errors
0361   poolDbService->writeOneIOV<Alignments>(gem_Alignments, poolDbService->currentTime(), theGEMAlignRecordName);
0362   poolDbService->writeOneIOV<AlignmentErrorsExtended>(
0363       gem_AlignmentErrorsExtended, poolDbService->currentTime(), theGEMErrorRecordName);
0364 }
0365 
0366 void MuonAlignment::saveToDB(void) {
0367   saveDTtoDB();
0368   saveCSCtoDB();
0369   saveGEMtoDB();
0370 }