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 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
// system include files
#include <memory>

// user include files
#include "FWCore/Framework/interface/EventSetup.h"
#include "FWCore/Framework/interface/ESHandle.h"

// Conditions database
#include "FWCore/ServiceRegistry/interface/Service.h"
#include "CondCore/DBOutputService/interface/PoolDBOutputService.h"

// Alignment
#include "CondFormats/Alignment/interface/Alignments.h"
#include "CondFormats/Alignment/interface/AlignmentErrorsExtended.h"
#include "CondFormats/Alignment/interface/SurveyErrors.h"
#include "Alignment/CommonAlignment/interface/SurveyDet.h"
#include "Alignment/MuonAlignment/interface/MuonAlignment.h"

#include "Alignment/MuonAlignment/interface/MuonAlignmentOutputXML.h"

#include "Alignment/CommonAlignment/interface/Alignable.h"
#include "Alignment/CommonAlignment/interface/SurveyDet.h"
#include "DataFormats/GeometryCommonDetAlgo/interface/AlignmentPositionError.h"
#include "DataFormats/GeometryCommonDetAlgo/interface/GlobalError.h"

//____________________________________________________________________________________
//
void MuonAlignment::init() {
  theDTAlignRecordName = "DTAlignmentRcd";
  theDTErrorRecordName = "DTAlignmentErrorExtendedRcd";
  theCSCAlignRecordName = "CSCAlignmentRcd";
  theCSCErrorRecordName = "CSCAlignmentErrorExtendedRcd";
  theGEMAlignRecordName = "GEMAlignmentRcd";
  theGEMErrorRecordName = "GEMAlignmentErrorExtendedRcd";
  theDTSurveyRecordName = "DTSurveyRcd";
  theDTSurveyErrorRecordName = "DTSurveyErrorExtendedRcd";
  theCSCSurveyRecordName = "CSCSurveyRcd";
  theCSCSurveyErrorRecordName = "CSCSurveyErrorExtendedRcd";
  theAlignableMuon = nullptr;
  theAlignableNavigator = nullptr;
}

MuonAlignment::MuonAlignment(const DTGeometry* dtGeometry,
                             const CSCGeometry* cscGeometry,
                             const GEMGeometry* gemGeometry)
    : dtGeometry_(dtGeometry), cscGeometry_(cscGeometry), gemGeometry_(gemGeometry) {
  init();

  theAlignableMuon = new AlignableMuon(&*dtGeometry_, &*cscGeometry_, &*gemGeometry_);
  theAlignableNavigator = new AlignableNavigator(theAlignableMuon);
}

MuonAlignment::MuonAlignment(const edm::EventSetup& iSetup, const MuonAlignmentInputMethod& input) {
  init();

  theAlignableMuon = input.newAlignableMuon();
  theAlignableNavigator = new AlignableNavigator(theAlignableMuon);
}

//____________________________________________________________________________________
//
void MuonAlignment::moveAlignableLocalCoord(DetId& detid, align::Scalars& displacements, align::Scalars& rotations) {
  // Displace and rotate DT an Alignable associated to a GeomDet or GeomDetUnit
  Alignable* theAlignable = theAlignableNavigator->alignableFromDetId(detid);

  // Convert local to global diplacements
  align::LocalVector lvector(displacements.at(0), displacements.at(1), displacements.at(2));
  align::GlobalVector gvector = (theAlignable->surface()).toGlobal(lvector);

  // global displacement of the chamber
  theAlignable->move(gvector);

  // local rotation of the chamber
  theAlignable->rotateAroundLocalX(rotations.at(0));  // Local X axis rotation
  theAlignable->rotateAroundLocalY(rotations.at(1));  // Local Y axis rotation
  theAlignable->rotateAroundLocalZ(rotations.at(2));  // Local Z axis rotation
}

//____________________________________________________________________________________
//
void MuonAlignment::moveAlignableGlobalCoord(DetId& detid, align::Scalars& displacements, align::Scalars& rotations) {
  // Displace and rotate DT an Alignable associated to a GeomDet or GeomDetUnit
  Alignable* theAlignable = theAlignableNavigator->alignableFromDetId(detid);

  // Convert std::vector to GlobalVector
  align::GlobalVector gvector(displacements.at(0), displacements.at(1), displacements.at(2));

  // global displacement of the chamber
  theAlignable->move(gvector);

  // local rotation of the chamber
  theAlignable->rotateAroundGlobalX(rotations.at(0));  // Global X axis rotation
  theAlignable->rotateAroundGlobalY(rotations.at(1));  // Global Y axis rotation
  theAlignable->rotateAroundGlobalZ(rotations.at(2));  // Global Z axis rotation
}

//____________________________________________________________________________________
//
void MuonAlignment::recursiveList(const align::Alignables& alignables, align::Alignables& theList) {
  for (align::Alignables::const_iterator alignable = alignables.begin(); alignable != alignables.end(); ++alignable) {
    recursiveList((*alignable)->components(), theList);
    theList.push_back(*alignable);
  }
}

//____________________________________________________________________________________
//
void MuonAlignment::recursiveMap(const align::Alignables& alignables, std::map<align::ID, Alignable*>& theMap) {
  for (align::Alignables::const_iterator alignable = alignables.begin(); alignable != alignables.end(); ++alignable) {
    unsigned int rawId = (*alignable)->geomDetId().rawId();
    if (rawId != 0) {
      theMap[rawId] = *alignable;
    }
    recursiveMap((*alignable)->components(), theMap);
  }
}

//____________________________________________________________________________________
//
void MuonAlignment::recursiveStructureMap(const align::Alignables& alignables,
                                          std::map<std::pair<align::StructureType, align::ID>, Alignable*>& theMap) {
  for (align::Alignables::const_iterator alignable = alignables.begin(); alignable != alignables.end(); ++alignable) {
    theMap[std::pair<align::StructureType, align::ID>((*alignable)->alignableObjectId(), (*alignable)->id())] =
        *alignable;
    recursiveStructureMap((*alignable)->components(), theMap);
  }
}

//____________________________________________________________________________________
//
void MuonAlignment::copyAlignmentToSurvey(double shiftErr, double angleErr) {
  std::map<align::ID, Alignable*> alignableMap;
  recursiveMap(theAlignableMuon->DTBarrel(), alignableMap);
  recursiveMap(theAlignableMuon->CSCEndcaps(), alignableMap);
  recursiveMap(theAlignableMuon->GEMEndcaps(), alignableMap);

  // Set the survey error to the alignable error, expanding the matrix as needed
  AlignmentErrorsExtended* dtAlignmentErrorsExtended = theAlignableMuon->dtAlignmentErrorsExtended();
  AlignmentErrorsExtended* cscAlignmentErrorsExtended = theAlignableMuon->cscAlignmentErrorsExtended();
  AlignmentErrorsExtended* gemAlignmentErrorsExtended = theAlignableMuon->gemAlignmentErrorsExtended();
  std::vector<AlignTransformErrorExtended> alignmentErrors;
  std::copy(dtAlignmentErrorsExtended->m_alignError.begin(),
            dtAlignmentErrorsExtended->m_alignError.end(),
            std::back_inserter(alignmentErrors));
  std::copy(cscAlignmentErrorsExtended->m_alignError.begin(),
            cscAlignmentErrorsExtended->m_alignError.end(),
            std::back_inserter(alignmentErrors));
  std::copy(gemAlignmentErrorsExtended->m_alignError.begin(),
            gemAlignmentErrorsExtended->m_alignError.end(),
            std::back_inserter(alignmentErrors));

  for (std::vector<AlignTransformErrorExtended>::const_iterator alignmentError = alignmentErrors.begin();
       alignmentError != alignmentErrors.end();
       ++alignmentError) {
    align::ErrorMatrix matrix6x6 = ROOT::Math::SMatrixIdentity();  // start from (0, 0)
    CLHEP::HepSymMatrix matrix6x6new = alignmentError->matrix();   // start from (1, 1)

    for (int i = 0; i < 6; i++) {
      for (int j = 0; j < 6; j++) {
        matrix6x6(i, j) = matrix6x6new(i + 1, j + 1);
      }
    }
    //matrix6x6(3,3) = angleErr;
    //matrix6x6(4,4) = angleErr;
    //matrix6x6(5,5) = angleErr;

    Alignable* alignable = alignableMap[alignmentError->rawId()];
    alignable->setSurvey(new SurveyDet(alignable->surface(), matrix6x6));
  }

  fillGapsInSurvey(shiftErr, angleErr);
}

//____________________________________________________________________________________
//

void MuonAlignment::fillGapsInSurvey(double shiftErr, double angleErr) {
  // get all the ones we missed
  std::map<std::pair<align::StructureType, align::ID>, Alignable*> alignableStructureMap;
  recursiveStructureMap(theAlignableMuon->DTBarrel(), alignableStructureMap);
  recursiveStructureMap(theAlignableMuon->CSCEndcaps(), alignableStructureMap);

  for (std::map<std::pair<align::StructureType, align::ID>, Alignable*>::const_iterator iter =
           alignableStructureMap.begin();
       iter != alignableStructureMap.end();
       ++iter) {
    if (iter->second->survey() == nullptr) {
      align::ErrorMatrix matrix6x6 = ROOT::Math::SMatrixIdentity();
      matrix6x6(0, 0) = shiftErr;
      matrix6x6(1, 1) = shiftErr;
      matrix6x6(2, 2) = shiftErr;
      matrix6x6(3, 3) = angleErr;
      matrix6x6(4, 4) = angleErr;
      matrix6x6(5, 5) = angleErr;
      iter->second->setSurvey(new SurveyDet(iter->second->surface(), matrix6x6));
    }
  }
}

//____________________________________________________________________________________
//
void MuonAlignment::recursiveCopySurveyToAlignment(Alignable* alignable) {
  if (alignable->survey() != nullptr) {
    const SurveyDet* survey = alignable->survey();

    const align::PositionType& pos = survey->position();
    align::RotationType rot = survey->rotation();

    align::PositionType oldpos = alignable->globalPosition();
    align::RotationType oldrot = alignable->globalRotation();
    alignable->move(align::GlobalVector(-oldpos.x(), -oldpos.y(), -oldpos.z()));
    alignable->rotateInGlobalFrame(oldrot.transposed());
    alignable->rotateInGlobalFrame(rot);
    alignable->move(align::GlobalVector(pos.x(), pos.y(), pos.z()));

    align::ErrorMatrix matrix6x6 = survey->errors();  // start from 0,0
    AlgebraicSymMatrix66 matrix6x6new;                // start from 0,0
    for (int i = 0; i < 6; i++) {
      for (int j = 0; j <= i; j++) {
        matrix6x6new(i, j) = matrix6x6(i, j);
      }
    }

    // this sets APEs at this level and (since 2nd argument is true) all lower levels
    alignable->setAlignmentPositionError(AlignmentPositionError(GlobalErrorExtended(matrix6x6new)), true);
  }

  // do lower levels afterward to thwart the cumulative setting of APEs
  align::Alignables components = alignable->components();
  for (align::Alignables::const_iterator comp = components.begin(); comp != components.end(); ++comp) {
    recursiveCopySurveyToAlignment(*comp);
  }
}

void MuonAlignment::copySurveyToAlignment() { recursiveCopySurveyToAlignment(theAlignableMuon); }

//____________________________________________________________________________________
// Code needed to store alignments to DB

void MuonAlignment::writeXML(const edm::ParameterSet& iConfig,
                             const DTGeometry* dtGeometryXML,
                             const CSCGeometry* cscGeometryXML,
                             const GEMGeometry* gemGeometryXML) {
  MuonAlignmentOutputXML(iConfig, dtGeometryXML, cscGeometryXML, gemGeometryXML).write(theAlignableMuon);
}

void MuonAlignment::saveDTSurveyToDB(void) {
  // Call service
  edm::Service<cond::service::PoolDBOutputService> poolDbService;
  if (!poolDbService.isAvailable())  // Die if not available
    throw cms::Exception("NotAvailable") << "PoolDBOutputService not available";

  // Get alignments and errors
  Alignments dtAlignments{};
  SurveyErrors dtSurveyErrors{};

  align::Alignables alignableList;
  recursiveList(theAlignableMuon->DTBarrel(), alignableList);

  for (align::Alignables::const_iterator alignable = alignableList.begin(); alignable != alignableList.end();
       ++alignable) {
    const align::PositionType& pos = (*alignable)->survey()->position();
    const align::RotationType& rot = (*alignable)->survey()->rotation();

    AlignTransform value(CLHEP::Hep3Vector(pos.x(), pos.y(), pos.z()),
                         CLHEP::HepRotation(CLHEP::HepRep3x3(
                             rot.xx(), rot.xy(), rot.xz(), rot.yx(), rot.yy(), rot.yz(), rot.zx(), rot.zy(), rot.zz())),
                         (*alignable)->id());
    SurveyError error((*alignable)->alignableObjectId(), (*alignable)->id(), (*alignable)->survey()->errors());

    dtAlignments.m_align.push_back(value);
    dtSurveyErrors.m_surveyErrors.push_back(error);
  }

  // Store DT alignments and errors
  poolDbService->writeOneIOV<Alignments>(dtAlignments, poolDbService->currentTime(), theDTSurveyRecordName);
  poolDbService->writeOneIOV<SurveyErrors>(dtSurveyErrors, poolDbService->currentTime(), theDTSurveyErrorRecordName);
}

void MuonAlignment::saveCSCSurveyToDB(void) {
  // Call service
  edm::Service<cond::service::PoolDBOutputService> poolDbService;
  if (!poolDbService.isAvailable())  // Die if not available
    throw cms::Exception("NotAvailable") << "PoolDBOutputService not available";

  // Get alignments and errors
  Alignments cscAlignments{};
  SurveyErrors cscSurveyErrors{};

  align::Alignables alignableList;
  recursiveList(theAlignableMuon->CSCEndcaps(), alignableList);

  for (align::Alignables::const_iterator alignable = alignableList.begin(); alignable != alignableList.end();
       ++alignable) {
    const align::PositionType& pos = (*alignable)->survey()->position();
    const align::RotationType& rot = (*alignable)->survey()->rotation();

    AlignTransform value(CLHEP::Hep3Vector(pos.x(), pos.y(), pos.z()),
                         CLHEP::HepRotation(CLHEP::HepRep3x3(
                             rot.xx(), rot.xy(), rot.xz(), rot.yx(), rot.yy(), rot.yz(), rot.zx(), rot.zy(), rot.zz())),
                         (*alignable)->id());
    SurveyError error((*alignable)->alignableObjectId(), (*alignable)->id(), (*alignable)->survey()->errors());

    cscAlignments.m_align.push_back(value);
    cscSurveyErrors.m_surveyErrors.push_back(error);
  }

  // Store CSC alignments and errors
  poolDbService->writeOneIOV<Alignments>(cscAlignments, poolDbService->currentTime(), theCSCSurveyRecordName);
  poolDbService->writeOneIOV<SurveyErrors>(cscSurveyErrors, poolDbService->currentTime(), theCSCSurveyErrorRecordName);
}

void MuonAlignment::saveSurveyToDB(void) {
  saveDTSurveyToDB();
  saveCSCSurveyToDB();
}

void MuonAlignment::saveDTtoDB(void) {
  // Call service
  edm::Service<cond::service::PoolDBOutputService> poolDbService;
  if (!poolDbService.isAvailable())  // Die if not available
    throw cms::Exception("NotAvailable") << "PoolDBOutputService not available";

  // Get alignments and errors
  Alignments dt_Alignments = *(theAlignableMuon->dtAlignments());
  AlignmentErrorsExtended dt_AlignmentErrorsExtended = *(theAlignableMuon->dtAlignmentErrorsExtended());

  // Store DT alignments and errors
  poolDbService->writeOneIOV<Alignments>(dt_Alignments, poolDbService->currentTime(), theDTAlignRecordName);
  poolDbService->writeOneIOV<AlignmentErrorsExtended>(
      dt_AlignmentErrorsExtended, poolDbService->currentTime(), theDTErrorRecordName);
}

void MuonAlignment::saveCSCtoDB(void) {
  // Call service
  edm::Service<cond::service::PoolDBOutputService> poolDbService;
  if (!poolDbService.isAvailable())  // Die if not available
    throw cms::Exception("NotAvailable") << "PoolDBOutputService not available";

  // Get alignments and errors
  Alignments csc_Alignments = *(theAlignableMuon->cscAlignments());
  AlignmentErrorsExtended csc_AlignmentErrorsExtended = *(theAlignableMuon->cscAlignmentErrorsExtended());

  // Store CSC alignments and errors
  poolDbService->writeOneIOV<Alignments>(csc_Alignments, poolDbService->currentTime(), theCSCAlignRecordName);
  poolDbService->writeOneIOV<AlignmentErrorsExtended>(
      csc_AlignmentErrorsExtended, poolDbService->currentTime(), theCSCErrorRecordName);
}

void MuonAlignment::saveGEMtoDB(void) {
  // Call service
  edm::Service<cond::service::PoolDBOutputService> poolDbService;
  if (!poolDbService.isAvailable())  // Die if not available
    throw cms::Exception("NotAvailable") << "PoolDBOutputService not available";

  // Get alignments and errors
  Alignments gem_Alignments = *(theAlignableMuon->gemAlignments());
  AlignmentErrorsExtended gem_AlignmentErrorsExtended = *(theAlignableMuon->gemAlignmentErrorsExtended());

  // Store CSC alignments and errors
  poolDbService->writeOneIOV<Alignments>(gem_Alignments, poolDbService->currentTime(), theGEMAlignRecordName);
  poolDbService->writeOneIOV<AlignmentErrorsExtended>(
      gem_AlignmentErrorsExtended, poolDbService->currentTime(), theGEMErrorRecordName);
}

void MuonAlignment::saveToDB(void) {
  saveDTtoDB();
  saveCSCtoDB();
  saveGEMtoDB();
}