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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
#include "Alignment/CommonAlignment/interface/AlignableDetUnit.h"

#include "Geometry/CommonDetUnit/interface/GeomDet.h"

#include "CondFormats/Alignment/interface/Alignments.h"
#include "CondFormats/Alignment/interface/AlignmentErrorsExtended.h"
#include "CLHEP/Vector/RotationInterfaces.h"
#include "DataFormats/GeometryCommonDetAlgo/interface/AlignmentPositionError.h"
#include "Geometry/CommonTopologies/interface/SurfaceDeformation.h"

#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "FWCore/Utilities/interface/Exception.h"

//__________________________________________________________________________________________________
AlignableDetUnit::AlignableDetUnit(const GeomDetUnit* geomDetUnit)
    :  // rely on non-NULL pointer!
      Alignable(geomDetUnit->geographicalId().rawId(), geomDetUnit->surface()),
      theAlignmentPositionError(nullptr),
      theSurfaceDeformation(nullptr),
      theCachedSurfaceDeformation(nullptr) {
  if (geomDetUnit->alignmentPositionError()) {  // take over APE from geometry
    // 2nd argument w/o effect:
    this->setAlignmentPositionError(*(geomDetUnit->alignmentPositionError()), false);
  }

  if (geomDetUnit->surfaceDeformation()) {  // take over surface modification
    // 2nd argument w/o effect:
    this->setSurfaceDeformation(geomDetUnit->surfaceDeformation(), false);
  }

  theDeepComponents.push_back(this);
}

//__________________________________________________________________________________________________
AlignableDetUnit::~AlignableDetUnit() {
  delete theAlignmentPositionError;
  delete theSurfaceDeformation;
  delete theCachedSurfaceDeformation;
  for (auto surface : surfaceDeformationsCache_)
    delete surface.second;
}

//__________________________________________________________________________________________________
void AlignableDetUnit::update(const GeomDetUnit* geomDetUnit) {
  if (!geomDetUnit) {
    throw cms::Exception("Alignment") << "@SUB=AlignableDetUnit::update\n"
                                      << "Trying to update with GeomDetUnit* pointing to 'nullptr'.";
  }

  Alignable::update(geomDetUnit->geographicalId().rawId(), geomDetUnit->surface());

  if (geomDetUnit->alignmentPositionError()) {  // take over APE from geometry
    // 2nd argument w/o effect:
    this->setAlignmentPositionError(*(geomDetUnit->alignmentPositionError()), false);
  }

  if (geomDetUnit->surfaceDeformation()) {  // take over surface modification
    // 2nd argument w/o effect:
    this->setSurfaceDeformation(geomDetUnit->surfaceDeformation(), false);
  }
}

//__________________________________________________________________________________________________
void AlignableDetUnit::addComponent(Alignable* /*unused*/) {
  throw cms::Exception("LogicError") << "AlignableDetUnit cannot have components, but try to add one!";
}

//__________________________________________________________________________________________________
void AlignableDetUnit::move(const GlobalVector& displacement) {
  theSurface.move(displacement);
  this->addDisplacement(displacement);
}

//__________________________________________________________________________________________________
void AlignableDetUnit::rotateInGlobalFrame(const RotationType& rotation) {
  theSurface.rotate(rotation);
  this->addRotation(rotation);
}

//__________________________________________________________________________________________________
void AlignableDetUnit::setAlignmentPositionError(const AlignmentPositionError& ape, bool /*propagateDown*/) {
  if (!theAlignmentPositionError)
    theAlignmentPositionError = new AlignmentPositionError(ape);
  else
    *theAlignmentPositionError = ape;
}

//__________________________________________________________________________________________________
void AlignableDetUnit::addAlignmentPositionError(const AlignmentPositionError& ape, bool propagateDown) {
  if (!theAlignmentPositionError)
    this->setAlignmentPositionError(ape, propagateDown);  // 2nd argument w/o effect
  else
    *theAlignmentPositionError += ape;
}

//__________________________________________________________________________________________________
void AlignableDetUnit::addAlignmentPositionErrorFromRotation(const RotationType& rot, bool propagateDown) {
  // average error calculated by movement of a local point at
  // (xWidth/2,yLength/2,0) caused by the rotation rot
  GlobalVector localPositionVector =
      surface().toGlobal(LocalVector(.5 * surface().width(), .5 * surface().length(), 0.));

  const LocalVector::BasicVectorType& lpvgf = localPositionVector.basicVector();
  GlobalVector gv(rot.multiplyInverse(lpvgf) - lpvgf);

  AlignmentPositionError ape(gv.x(), gv.y(), gv.z());
  this->addAlignmentPositionError(ape, propagateDown);  // 2nd argument w/o effect
}

//__________________________________________________________________________________________________
void AlignableDetUnit::addAlignmentPositionErrorFromLocalRotation(const RotationType& rot, bool propagateDown) {
  RotationType globalRot = globalRotation().multiplyInverse(rot * globalRotation());
  this->addAlignmentPositionErrorFromRotation(globalRot, propagateDown);  // 2nd argument w/o effect
}

//__________________________________________________________________________________________________
void AlignableDetUnit::setSurfaceDeformation(const SurfaceDeformation* deformation, bool /* propagateDown */) {
  delete theSurfaceDeformation;  // OK for zero pointers
  if (deformation) {
    theSurfaceDeformation = deformation->clone();
  } else {
    theSurfaceDeformation = nullptr;
  }
}

//__________________________________________________________________________________________________
void AlignableDetUnit::addSurfaceDeformation(const SurfaceDeformation* deformation, bool propagateDown) {
  if (!deformation) {
    // nothing to do
  } else if (!theSurfaceDeformation) {
    this->setSurfaceDeformation(deformation, propagateDown);  // fine since no components
  } else if (!theSurfaceDeformation->add(*deformation)) {
    edm::LogError("Alignment") << "@SUB=AlignableDetUnit::addSurfaceDeformation"
                               << "Cannot add deformation type " << deformation->type() << " to type "
                               << theSurfaceDeformation->type() << ", so erase deformation information.";
    delete theSurfaceDeformation;
    theSurfaceDeformation = nullptr;
  }
}

//__________________________________________________________________________________________________
void AlignableDetUnit::dump() const {
  std::ostringstream parameters;
  if (theSurfaceDeformation) {
    parameters << "    surface deformation parameters:";
    for (const auto& param : theSurfaceDeformation->parameters()) {
      parameters << " " << param;
    }
  } else {
    parameters << "    no surface deformation parameters";
  }

  edm::LogInfo("AlignableDump") << " AlignableDetUnit has position = " << this->globalPosition()
                                << ", orientation:" << std::endl
                                << this->globalRotation() << std::endl
                                << " total displacement and rotation: " << this->displacement() << std::endl
                                << this->rotation() << "\n"
                                << parameters.str();
}

//__________________________________________________________________________________________________
Alignments* AlignableDetUnit::alignments() const {
  Alignments* m_alignments = new Alignments();
  RotationType rot(this->globalRotation());

  // Get alignments (position, rotation, detId)
  CLHEP::Hep3Vector clhepVector(globalPosition().x(), globalPosition().y(), globalPosition().z());
  CLHEP::HepRotation clhepRotation(
      CLHEP::HepRep3x3(rot.xx(), rot.xy(), rot.xz(), rot.yx(), rot.yy(), rot.yz(), rot.zx(), rot.zy(), rot.zz()));
  uint32_t detId = this->geomDetId().rawId();

  AlignTransform transform(clhepVector, clhepRotation, detId);

  // Add to alignments container
  m_alignments->m_align.push_back(transform);

  return m_alignments;
}

//__________________________________________________________________________________________________
AlignmentErrorsExtended* AlignableDetUnit::alignmentErrors() const {
  AlignmentErrorsExtended* m_alignmentErrors = new AlignmentErrorsExtended();

  uint32_t detId = this->geomDetId().rawId();

  CLHEP::HepSymMatrix clhepSymMatrix(6, 0);
  if (theAlignmentPositionError)  // Might not be set
    clhepSymMatrix = asHepMatrix(theAlignmentPositionError->globalError().matrix());

  AlignTransformErrorExtended transformError(clhepSymMatrix, detId);

  m_alignmentErrors->m_alignError.push_back(transformError);

  return m_alignmentErrors;
}

//__________________________________________________________________________________________________
int AlignableDetUnit::surfaceDeformationIdPairs(std::vector<std::pair<int, SurfaceDeformation*> >& result) const {
  if (theSurfaceDeformation) {
    result.push_back(std::pair<int, SurfaceDeformation*>(this->geomDetId().rawId(), theSurfaceDeformation));
    return 1;
  }

  return 0;
}

//__________________________________________________________________________________________________
void AlignableDetUnit::cacheTransformation() {
  theCachedSurface = theSurface;
  theCachedDisplacement = theDisplacement;
  theCachedRotation = theRotation;

  if (theCachedSurfaceDeformation) {
    delete theCachedSurfaceDeformation;
    theCachedSurfaceDeformation = nullptr;
  }

  if (theSurfaceDeformation)
    theCachedSurfaceDeformation = theSurfaceDeformation->clone();
}

//__________________________________________________________________________________________________
void AlignableDetUnit::cacheTransformation(const align::RunNumber& run) {
  surfacesCache_[run] = theSurface;
  displacementsCache_[run] = theDisplacement;
  rotationsCache_[run] = theRotation;

  auto existingCache = surfaceDeformationsCache_.find(run);
  if (existingCache != surfaceDeformationsCache_.end()) {
    delete existingCache->second;
    existingCache->second = nullptr;
  }

  if (theSurfaceDeformation) {
    surfaceDeformationsCache_[run] = theSurfaceDeformation->clone();
  }
}

//__________________________________________________________________________________________________
void AlignableDetUnit::restoreCachedTransformation() {
  theSurface = theCachedSurface;
  theDisplacement = theCachedDisplacement;
  theRotation = theCachedRotation;

  if (theSurfaceDeformation) {
    delete theSurfaceDeformation;
    theSurfaceDeformation = nullptr;
  }

  if (theCachedSurfaceDeformation) {
    this->setSurfaceDeformation(theCachedSurfaceDeformation, false);
  }
}

//__________________________________________________________________________________________________
void AlignableDetUnit::restoreCachedTransformation(const align::RunNumber& run) {
  if (surfacesCache_.find(run) == surfacesCache_.end()) {
    throw cms::Exception("Alignment") << "@SUB=Alignable::restoreCachedTransformation\n"
                                      << "Trying to restore cached transformation for a run (" << run
                                      << ") that has not been cached.";
  } else {
    theSurface = surfacesCache_[run];
    theDisplacement = displacementsCache_[run];
    theRotation = rotationsCache_[run];

    if (theSurfaceDeformation) {
      delete theSurfaceDeformation;
      theSurfaceDeformation = nullptr;
    }

    if (surfaceDeformationsCache_[run]) {
      this->setSurfaceDeformation(surfaceDeformationsCache_[run], false);
    }
  }
}

//______________________________________________________________________________
const align::Alignables AlignableDetUnit::emptyComponents_{};