Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0002 #include "Alignment/CommonAlignment/interface/Alignable.h"
0003 #include "Alignment/CommonAlignment/interface/SurveyDet.h"
0004 #include "Alignment/CommonAlignment/interface/AlignTools.h"
0005 
0006 #include <iostream>
0007 #include <fstream>
0008 
0009 //Finds the TR between two alignables - first alignable is reference
0010 AlgebraicVector align::diffAlignables(Alignable* refAli,
0011                                       Alignable* curAli,
0012                                       const std::string& weightBy,
0013                                       bool weightById,
0014                                       const std::vector<unsigned int>& weightByIdVector) {
0015   //check they are the same
0016   if (refAli->alignableObjectId() != curAli->alignableObjectId()) {
0017     if (refAli->id() != curAli->id()) {
0018       throw cms::Exception("Geometry Error") << "[AlignTools] Error, Alignables do not match";
0019     }
0020   }
0021 
0022   //create points
0023   align::GlobalVectors refVs;
0024   align::GlobalVectors curVs;
0025   align::createPoints(&refVs, refAli, weightBy, weightById, weightByIdVector);
0026   align::createPoints(&curVs, curAli, weightBy, weightById, weightByIdVector);
0027 
0028   //redefine the set of points
0029   //find the translational difference
0030   align::GlobalVector theR = align::diffR(curVs, refVs);
0031 
0032   //CM difference (needed below in rotational transformation)
0033   align::GlobalVector pointsCM = align::centerOfMass(curVs);
0034   align::PositionType alignableCM = curAli->globalPosition();
0035   align::GlobalVector cmdiff(
0036       alignableCM.x() - pointsCM.x(), alignableCM.y() - pointsCM.y(), alignableCM.z() - pointsCM.z());
0037 
0038   //readjust points before finding rotation
0039   align::GlobalVector CMref = align::centerOfMass(refVs);
0040   align::GlobalVector CMcur = align::centerOfMass(curVs);
0041   for (unsigned int k = 0; k < refVs.size(); ++k) {
0042     refVs[k] -= CMref;
0043     curVs[k] -= CMcur;
0044   }
0045 
0046   //find rotational difference (global)
0047   align::RotationType rot = align::diffRot(curVs, refVs);
0048   align::EulerAngles theW = align::toAngles(rot);
0049   //convert to local rotation
0050   align::RotationType localrot = refAli->surface().toLocal(rot);
0051   align::EulerAngles theLocalW = align::toAngles(localrot);
0052 
0053   //adjust translational difference factoring in different rotational CM
0054   //needed because rotateInGlobalFrame is about CM of alignable, not points
0055   const align::GlobalVector::BasicVectorType& lpvgf = cmdiff.basicVector();
0056   align::GlobalVector moveV(rot.multiplyInverse(lpvgf) - lpvgf);
0057   align::GlobalVector theRprime(theR + moveV);
0058   //convert to local movement
0059   align::LocalVector theLocalRprime = refAli->surface().toLocal(theRprime);
0060 
0061   AlgebraicVector deltaRW(12);
0062   // global values
0063   deltaRW(1) = theRprime.x();
0064   deltaRW(2) = theRprime.y();
0065   deltaRW(3) = theRprime.z();
0066   deltaRW(4) = theW(1);
0067   deltaRW(5) = theW(2);
0068   deltaRW(6) = theW(3);
0069   // local values
0070   deltaRW(7) = theLocalRprime.x();
0071   deltaRW(8) = theLocalRprime.y();
0072   deltaRW(9) = theLocalRprime.z();
0073   deltaRW(10) = theLocalW(1);
0074   deltaRW(11) = theLocalW(2);
0075   deltaRW(12) = theLocalW(3);
0076 
0077   refVs.clear();
0078   curVs.clear();
0079 
0080   return deltaRW;
0081 }
0082 
0083 //Moves the alignable by the AlgebraicVector
0084 void align::moveAlignable(Alignable* ali, AlgebraicVector diff) {
0085   GlobalVector dR(diff[0], diff[1], diff[2]);
0086   align::EulerAngles dOmega(3);
0087   dOmega[0] = diff[3];
0088   dOmega[1] = diff[4];
0089   dOmega[2] = diff[5];
0090   align::RotationType dRot = align::toMatrix(dOmega);
0091   ali->move(dR);
0092   ali->rotateInGlobalFrame(dRot);
0093 }
0094 
0095 //Creates the points which are used in diffAlignables
0096 void align::createPoints(align::GlobalVectors* Vs,
0097                          Alignable* ali,
0098                          const std::string& weightBy,
0099                          bool weightById,
0100                          const std::vector<unsigned int>& weightByIdVector) {
0101   std::string copy = weightBy;
0102   std::transform(copy.begin(), copy.end(), copy.begin(), (int (*)(int))toupper);
0103   if (copy != "SELF") {
0104     const auto& comp = ali->components();
0105     unsigned int nComp = comp.size();
0106     for (unsigned int i = 0; i < nComp; ++i)
0107       align::createPoints(Vs, comp[i], weightBy, weightById, weightByIdVector);
0108     // double the weight for SS modules if weight by Det
0109     if ((ali->alignableObjectId() == align::AlignableDet) && (weightBy == "Det")) {
0110       for (unsigned int i = 0; i < nComp; ++i)
0111         align::createPoints(Vs, comp[i], weightBy, weightById, weightByIdVector);
0112     }
0113 
0114     //only create points for lowest hiearchical level
0115     if (ali->alignableObjectId() == align::AlignableDetUnit) {
0116       //check if the raw id or the mother's raw id is on the list
0117       bool createPointsForDetUnit = true;
0118       if (weightById)
0119         createPointsForDetUnit = align::readModuleList(ali->id(), ali->mother()->id(), weightByIdVector);
0120       if (createPointsForDetUnit) {
0121         //if no survey information, create local points
0122         if (!(ali->survey())) {
0123           align::ErrorMatrix error;
0124           ali->setSurvey(new SurveyDet(ali->surface(), error * 1e-6));
0125         }
0126         const align::GlobalPoints& points = ali->surface().toGlobal(ali->survey()->localPoints());
0127         for (unsigned int j = 0; j < points.size(); ++j) {
0128           align::GlobalVector dummy(points[j].x(), points[j].y(), points[j].z());
0129           Vs->push_back(dummy);
0130         }
0131       }
0132     }
0133   } else {
0134     bool createPointsForDetUnit = true;
0135     if (weightById)
0136       createPointsForDetUnit = align::readModuleList(ali->id(), ali->mother()->id(), weightByIdVector);
0137     if (createPointsForDetUnit) {
0138       //if no survey information, create local points
0139       if (!(ali->survey())) {
0140         align::ErrorMatrix error;
0141         ali->setSurvey(new SurveyDet(ali->surface(), error * 1e-6));
0142       }
0143       const align::GlobalPoints& points = ali->surface().toGlobal(ali->survey()->localPoints());
0144       for (unsigned int j = 0; j < points.size(); ++j) {
0145         align::GlobalVector dummy(points[j].x(), points[j].y(), points[j].z());
0146         Vs->push_back(dummy);
0147       }
0148     }
0149   }
0150 }
0151 
0152 bool align::readModuleList(unsigned int aliId,
0153                            unsigned int motherId,
0154                            const std::vector<unsigned int>& weightByIdVector) {
0155   bool foundId = false;
0156 
0157   unsigned int sizeVector = weightByIdVector.size();
0158 
0159   for (unsigned int i = 0; i < sizeVector; ++i) {
0160     unsigned int listId = weightByIdVector[i];
0161 
0162     if (listId == aliId) {
0163       foundId = true;
0164       break;
0165     }
0166     if (listId == motherId) {
0167       foundId = true;
0168       break;
0169     }
0170   }
0171 
0172   return foundId;
0173 }