Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "Alignment/CommonAlignment/interface/Alignable.h"
#include "Alignment/CommonAlignment/interface/SurveyDet.h"
#include "Alignment/CommonAlignment/interface/AlignTools.h"

#include <iostream>
#include <fstream>

//Finds the TR between two alignables - first alignable is reference
AlgebraicVector align::diffAlignables(Alignable* refAli,
                                      Alignable* curAli,
                                      const std::string& weightBy,
                                      bool weightById,
                                      const std::vector<unsigned int>& weightByIdVector) {
  //check they are the same
  if (refAli->alignableObjectId() != curAli->alignableObjectId()) {
    if (refAli->id() != curAli->id()) {
      throw cms::Exception("Geometry Error") << "[AlignTools] Error, Alignables do not match";
    }
  }

  //create points
  align::GlobalVectors refVs;
  align::GlobalVectors curVs;
  align::createPoints(&refVs, refAli, weightBy, weightById, weightByIdVector);
  align::createPoints(&curVs, curAli, weightBy, weightById, weightByIdVector);

  //redefine the set of points
  //find the translational difference
  align::GlobalVector theR = align::diffR(curVs, refVs);

  //CM difference (needed below in rotational transformation)
  align::GlobalVector pointsCM = align::centerOfMass(curVs);
  align::PositionType alignableCM = curAli->globalPosition();
  align::GlobalVector cmdiff(
      alignableCM.x() - pointsCM.x(), alignableCM.y() - pointsCM.y(), alignableCM.z() - pointsCM.z());

  //readjust points before finding rotation
  align::GlobalVector CMref = align::centerOfMass(refVs);
  align::GlobalVector CMcur = align::centerOfMass(curVs);
  for (unsigned int k = 0; k < refVs.size(); ++k) {
    refVs[k] -= CMref;
    curVs[k] -= CMcur;
  }

  //find rotational difference (global)
  align::RotationType rot = align::diffRot(curVs, refVs);
  align::EulerAngles theW = align::toAngles(rot);
  //convert to local rotation
  align::RotationType localrot = refAli->surface().toLocal(rot);
  align::EulerAngles theLocalW = align::toAngles(localrot);

  //adjust translational difference factoring in different rotational CM
  //needed because rotateInGlobalFrame is about CM of alignable, not points
  const align::GlobalVector::BasicVectorType& lpvgf = cmdiff.basicVector();
  align::GlobalVector moveV(rot.multiplyInverse(lpvgf) - lpvgf);
  align::GlobalVector theRprime(theR + moveV);
  //convert to local movement
  align::LocalVector theLocalRprime = refAli->surface().toLocal(theRprime);

  AlgebraicVector deltaRW(12);
  // global values
  deltaRW(1) = theRprime.x();
  deltaRW(2) = theRprime.y();
  deltaRW(3) = theRprime.z();
  deltaRW(4) = theW(1);
  deltaRW(5) = theW(2);
  deltaRW(6) = theW(3);
  // local values
  deltaRW(7) = theLocalRprime.x();
  deltaRW(8) = theLocalRprime.y();
  deltaRW(9) = theLocalRprime.z();
  deltaRW(10) = theLocalW(1);
  deltaRW(11) = theLocalW(2);
  deltaRW(12) = theLocalW(3);

  refVs.clear();
  curVs.clear();

  return deltaRW;
}

//Moves the alignable by the AlgebraicVector
void align::moveAlignable(Alignable* ali, AlgebraicVector diff) {
  GlobalVector dR(diff[0], diff[1], diff[2]);
  align::EulerAngles dOmega(3);
  dOmega[0] = diff[3];
  dOmega[1] = diff[4];
  dOmega[2] = diff[5];
  align::RotationType dRot = align::toMatrix(dOmega);
  ali->move(dR);
  ali->rotateInGlobalFrame(dRot);
}

//Creates the points which are used in diffAlignables
void align::createPoints(align::GlobalVectors* Vs,
                         Alignable* ali,
                         const std::string& weightBy,
                         bool weightById,
                         const std::vector<unsigned int>& weightByIdVector) {
  std::string copy = weightBy;
  std::transform(copy.begin(), copy.end(), copy.begin(), (int (*)(int))toupper);
  if (copy != "SELF") {
    const auto& comp = ali->components();
    unsigned int nComp = comp.size();
    for (unsigned int i = 0; i < nComp; ++i)
      align::createPoints(Vs, comp[i], weightBy, weightById, weightByIdVector);
    // double the weight for SS modules if weight by Det
    if ((ali->alignableObjectId() == align::AlignableDet) && (weightBy == "Det")) {
      for (unsigned int i = 0; i < nComp; ++i)
        align::createPoints(Vs, comp[i], weightBy, weightById, weightByIdVector);
    }

    //only create points for lowest hiearchical level
    if (ali->alignableObjectId() == align::AlignableDetUnit) {
      //check if the raw id or the mother's raw id is on the list
      bool createPointsForDetUnit = true;
      if (weightById)
        createPointsForDetUnit = align::readModuleList(ali->id(), ali->mother()->id(), weightByIdVector);
      if (createPointsForDetUnit) {
        //if no survey information, create local points
        if (!(ali->survey())) {
          align::ErrorMatrix error;
          ali->setSurvey(new SurveyDet(ali->surface(), error * 1e-6));
        }
        const align::GlobalPoints& points = ali->surface().toGlobal(ali->survey()->localPoints());
        for (unsigned int j = 0; j < points.size(); ++j) {
          align::GlobalVector dummy(points[j].x(), points[j].y(), points[j].z());
          Vs->push_back(dummy);
        }
      }
    }
  } else {
    bool createPointsForDetUnit = true;
    if (weightById)
      createPointsForDetUnit = align::readModuleList(ali->id(), ali->mother()->id(), weightByIdVector);
    if (createPointsForDetUnit) {
      //if no survey information, create local points
      if (!(ali->survey())) {
        align::ErrorMatrix error;
        ali->setSurvey(new SurveyDet(ali->surface(), error * 1e-6));
      }
      const align::GlobalPoints& points = ali->surface().toGlobal(ali->survey()->localPoints());
      for (unsigned int j = 0; j < points.size(); ++j) {
        align::GlobalVector dummy(points[j].x(), points[j].y(), points[j].z());
        Vs->push_back(dummy);
      }
    }
  }
}

bool align::readModuleList(unsigned int aliId,
                           unsigned int motherId,
                           const std::vector<unsigned int>& weightByIdVector) {
  bool foundId = false;

  unsigned int sizeVector = weightByIdVector.size();

  for (unsigned int i = 0; i < sizeVector; ++i) {
    unsigned int listId = weightByIdVector[i];

    if (listId == aliId) {
      foundId = true;
      break;
    }
    if (listId == motherId) {
      foundId = true;
      break;
    }
  }

  return foundId;
}