00001
00002 #include <memory>
00003
00004
00005 #include "FWCore/Framework/interface/EventSetup.h"
00006 #include "FWCore/Framework/interface/ESHandle.h"
00007
00008
00009 #include "FWCore/ServiceRegistry/interface/Service.h"
00010 #include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
00011
00012
00013 #include "Geometry/Records/interface/MuonGeometryRecord.h"
00014
00015
00016 #include "CondFormats/Alignment/interface/Alignments.h"
00017 #include "CondFormats/Alignment/interface/AlignmentErrors.h"
00018 #include "CondFormats/Alignment/interface/SurveyErrors.h"
00019 #include "Alignment/CommonAlignment/interface/SurveyDet.h"
00020 #include "Alignment/MuonAlignment/interface/MuonAlignment.h"
00021
00022 #include "Alignment/MuonAlignment/interface/MuonAlignmentOutputXML.h"
00023
00024 #include "Alignment/CommonAlignment/interface/Alignable.h"
00025 #include "Alignment/CommonAlignment/interface/SurveyDet.h"
00026 #include "DataFormats/TrackingRecHit/interface/AlignmentPositionError.h"
00027 #include "DataFormats/GeometryCommonDetAlgo/interface/GlobalError.h"
00028
00029
00030
00031 void MuonAlignment::init()
00032 {
00033 theDTAlignRecordName = "DTAlignmentRcd";
00034 theDTErrorRecordName = "DTAlignmentErrorRcd";
00035 theCSCAlignRecordName = "CSCAlignmentRcd";
00036 theCSCErrorRecordName = "CSCAlignmentErrorRcd";
00037 theDTSurveyRecordName = "DTSurveyRcd";
00038 theDTSurveyErrorRecordName = "DTSurveyErrorRcd";
00039 theCSCSurveyRecordName = "CSCSurveyRcd";
00040 theCSCSurveyErrorRecordName = "CSCSurveyErrorRcd";
00041 theAlignableMuon = NULL;
00042 theAlignableNavigator = NULL;
00043 }
00044
00045 MuonAlignment::MuonAlignment( const edm::EventSetup& iSetup )
00046 {
00047 init();
00048
00049 edm::ESHandle<DTGeometry> dtGeometry;
00050 edm::ESHandle<CSCGeometry> cscGeometry;
00051 iSetup.get<MuonGeometryRecord>().get( dtGeometry );
00052 iSetup.get<MuonGeometryRecord>().get( cscGeometry );
00053
00054 theAlignableMuon = new AlignableMuon( &(*dtGeometry) , &(*cscGeometry) );
00055 theAlignableNavigator = new AlignableNavigator( theAlignableMuon );
00056 }
00057
00058 MuonAlignment::MuonAlignment( const edm::EventSetup& iSetup, const MuonAlignmentInputMethod& input )
00059 {
00060 init();
00061
00062 theAlignableMuon = input.newAlignableMuon( iSetup );
00063 theAlignableNavigator = new AlignableNavigator( theAlignableMuon );
00064 }
00065
00066
00067
00068 void MuonAlignment::moveAlignableLocalCoord( DetId& detid, std::vector<float>& displacements, std::vector<float>& rotations ){
00069
00070
00071 Alignable* theAlignable = theAlignableNavigator->alignableFromDetId( detid );
00072
00073
00074 LocalVector lvector( displacements.at(0), displacements.at(1), displacements.at(2));
00075 GlobalVector gvector = ( theAlignable->surface()).toGlobal( lvector );
00076
00077
00078 theAlignable->move( gvector );
00079
00080
00081 theAlignable->rotateAroundLocalX( rotations.at(0) );
00082 theAlignable->rotateAroundLocalY( rotations.at(1) );
00083 theAlignable->rotateAroundLocalZ( rotations.at(2) );
00084
00085 }
00086
00087
00088
00089 void MuonAlignment::moveAlignableGlobalCoord( DetId& detid, std::vector<float>& displacements, std::vector<float>& rotations ){
00090
00091
00092 Alignable* theAlignable = theAlignableNavigator->alignableFromDetId( detid );
00093
00094
00095 GlobalVector gvector( displacements.at(0), displacements.at(1), displacements.at(2));
00096
00097
00098 theAlignable->move( gvector );
00099
00100
00101 theAlignable->rotateAroundGlobalX( rotations.at(0) );
00102 theAlignable->rotateAroundGlobalY( rotations.at(1) );
00103 theAlignable->rotateAroundGlobalZ( rotations.at(2) );
00104
00105 }
00106
00107
00108
00109 void MuonAlignment::recursiveList(std::vector<Alignable*> alignables, std::vector<Alignable*> &theList) {
00110 for (std::vector<Alignable*>::const_iterator alignable = alignables.begin(); alignable != alignables.end(); ++alignable) {
00111 recursiveList((*alignable)->components(), theList);
00112 theList.push_back(*alignable);
00113 }
00114 }
00115
00116
00117
00118 void MuonAlignment::recursiveMap(std::vector<Alignable*> alignables, std::map<align::ID, Alignable*> &theMap) {
00119 for (std::vector<Alignable*>::const_iterator alignable = alignables.begin(); alignable != alignables.end(); ++alignable) {
00120 unsigned int rawId = (*alignable)->geomDetId().rawId();
00121 if (rawId != 0) {
00122 theMap[rawId] = *alignable;
00123 }
00124 recursiveMap((*alignable)->components(), theMap);
00125 }
00126 }
00127
00128
00129
00130 void MuonAlignment::recursiveStructureMap(std::vector<Alignable*> alignables, std::map<std::pair<align::StructureType, align::ID>, Alignable*> &theMap) {
00131 for (std::vector<Alignable*>::const_iterator alignable = alignables.begin(); alignable != alignables.end(); ++alignable) {
00132 theMap[std::pair<align::StructureType, align::ID>((*alignable)->alignableObjectId(), (*alignable)->id())] = *alignable;
00133 recursiveStructureMap((*alignable)->components(), theMap);
00134 }
00135 }
00136
00137
00138
00139 void MuonAlignment::copyAlignmentToSurvey(double shiftErr, double angleErr) {
00140 std::map<align::ID, Alignable*> alignableMap;
00141 recursiveMap(theAlignableMuon->DTBarrel(), alignableMap);
00142 recursiveMap(theAlignableMuon->CSCEndcaps(), alignableMap);
00143
00144
00145 AlignmentErrors* dtAlignmentErrors = theAlignableMuon->dtAlignmentErrors();
00146 AlignmentErrors* cscAlignmentErrors = theAlignableMuon->cscAlignmentErrors();
00147 std::vector<AlignTransformError> alignmentErrors;
00148 std::copy(dtAlignmentErrors->m_alignError.begin(), dtAlignmentErrors->m_alignError.end(), std::back_inserter(alignmentErrors));
00149 std::copy(cscAlignmentErrors->m_alignError.begin(), cscAlignmentErrors->m_alignError.end(), std::back_inserter(alignmentErrors));
00150
00151 for (std::vector<AlignTransformError>::const_iterator alignmentError = alignmentErrors.begin();
00152 alignmentError != alignmentErrors.end();
00153 ++alignmentError) {
00154 align::ErrorMatrix matrix6x6 = ROOT::Math::SMatrixIdentity();
00155 CLHEP::HepSymMatrix matrix3x3 = alignmentError->matrix();
00156
00157 for (int i = 0; i < 3; i++) {
00158 for (int j = 0; j < 3; j++) {
00159 matrix6x6(i, j) = matrix3x3(i+1, j+1);
00160 }
00161 }
00162 matrix6x6(3,3) = angleErr;
00163 matrix6x6(4,4) = angleErr;
00164 matrix6x6(5,5) = angleErr;
00165
00166 Alignable *alignable = alignableMap[alignmentError->rawId()];
00167 alignable->setSurvey(new SurveyDet(alignable->surface(), matrix6x6));
00168 }
00169
00170 fillGapsInSurvey(shiftErr, angleErr);
00171 }
00172
00173
00174
00175
00176 void MuonAlignment::fillGapsInSurvey(double shiftErr, double angleErr) {
00177
00178 std::map<std::pair<align::StructureType, align::ID>, Alignable*> alignableStructureMap;
00179 recursiveStructureMap(theAlignableMuon->DTBarrel(), alignableStructureMap);
00180 recursiveStructureMap(theAlignableMuon->CSCEndcaps(), alignableStructureMap);
00181
00182 for (std::map<std::pair<align::StructureType, align::ID>, Alignable*>::const_iterator iter = alignableStructureMap.begin();
00183 iter != alignableStructureMap.end();
00184 ++iter) {
00185 if (iter->second->survey() == NULL) {
00186 align::ErrorMatrix matrix6x6 = ROOT::Math::SMatrixIdentity();
00187 matrix6x6(0,0) = shiftErr;
00188 matrix6x6(1,1) = shiftErr;
00189 matrix6x6(2,2) = shiftErr;
00190 matrix6x6(3,3) = angleErr;
00191 matrix6x6(4,4) = angleErr;
00192 matrix6x6(5,5) = angleErr;
00193 iter->second->setSurvey(new SurveyDet(iter->second->surface(), matrix6x6));
00194 }
00195 }
00196 }
00197
00198
00199
00200 void MuonAlignment::recursiveCopySurveyToAlignment(Alignable *alignable) {
00201 if (alignable->survey() != NULL) {
00202 const SurveyDet *survey = alignable->survey();
00203
00204 align::PositionType pos = survey->position();
00205 align::RotationType rot = survey->rotation();
00206
00207 align::PositionType oldpos = alignable->globalPosition();
00208 align::RotationType oldrot = alignable->globalRotation();
00209 alignable->move(GlobalVector(-oldpos.x(), -oldpos.y(), -oldpos.z()));
00210 alignable->rotateInGlobalFrame(oldrot.transposed());
00211 alignable->rotateInGlobalFrame(rot);
00212 alignable->move(GlobalVector(pos.x(), pos.y(), pos.z()));
00213
00214 align::ErrorMatrix matrix6x6 = survey->errors();
00215 CLHEP::HepSymMatrix matrix3x3(3);
00216 for (int i = 0; i < 3; i++) {
00217 for (int j = 0; j < 3; j++) {
00218 matrix3x3(i+1, j+1) = matrix6x6(i, j);
00219 }
00220 }
00221
00222
00223 alignable->setAlignmentPositionError(AlignmentPositionError(GlobalError(matrix3x3)));
00224 }
00225
00226
00227 std::vector<Alignable*> components = alignable->components();
00228 for (std::vector<Alignable*>::const_iterator comp = components.begin(); comp != components.end(); ++comp) {
00229 recursiveCopySurveyToAlignment(*comp);
00230 }
00231 }
00232
00233 void MuonAlignment::copySurveyToAlignment() {
00234 recursiveCopySurveyToAlignment(theAlignableMuon);
00235 }
00236
00237
00238
00239
00240 void MuonAlignment::writeXML(const edm::ParameterSet iConfig, const edm::EventSetup &iSetup) {
00241 MuonAlignmentOutputXML(iConfig).write(theAlignableMuon, iSetup);
00242 }
00243
00244 void MuonAlignment::saveDTSurveyToDB(void) {
00245
00246 edm::Service<cond::service::PoolDBOutputService> poolDbService;
00247 if( !poolDbService.isAvailable() )
00248 throw cms::Exception("NotAvailable") << "PoolDBOutputService not available";
00249
00250
00251 Alignments *dtAlignments = new Alignments();
00252 SurveyErrors *dtSurveyErrors = new SurveyErrors();
00253
00254 std::vector<Alignable*> alignableList;
00255 recursiveList(theAlignableMuon->DTBarrel(), alignableList);
00256
00257 for (std::vector<Alignable*>::const_iterator alignable = alignableList.begin(); alignable != alignableList.end(); ++alignable) {
00258 const align::PositionType &pos = (*alignable)->survey()->position();
00259 const align::RotationType &rot = (*alignable)->survey()->rotation();
00260
00261 AlignTransform value(CLHEP::Hep3Vector(pos.x(), pos.y(), pos.z()),
00262 CLHEP::HepRotation(CLHEP::HepRep3x3(rot.xx(), rot.xy(), rot.xz(),
00263 rot.yx(), rot.yy(), rot.yz(),
00264 rot.zx(), rot.zy(), rot.zz())),
00265 (*alignable)->id());
00266 SurveyError error((*alignable)->alignableObjectId(), (*alignable)->id(), (*alignable)->survey()->errors());
00267
00268 dtAlignments->m_align.push_back(value);
00269 dtSurveyErrors->m_surveyErrors.push_back(error);
00270 }
00271
00272
00273 poolDbService->writeOne<Alignments>( &(*dtAlignments), poolDbService->currentTime(), theDTSurveyRecordName);
00274 poolDbService->writeOne<SurveyErrors>( &(*dtSurveyErrors), poolDbService->currentTime(), theDTSurveyErrorRecordName);
00275 }
00276
00277 void MuonAlignment::saveCSCSurveyToDB(void) {
00278
00279 edm::Service<cond::service::PoolDBOutputService> poolDbService;
00280 if( !poolDbService.isAvailable() )
00281 throw cms::Exception("NotAvailable") << "PoolDBOutputService not available";
00282
00283
00284 Alignments *cscAlignments = new Alignments();
00285 SurveyErrors *cscSurveyErrors = new SurveyErrors();
00286
00287 std::vector<Alignable*> alignableList;
00288 recursiveList(theAlignableMuon->CSCEndcaps(), alignableList);
00289
00290 for (std::vector<Alignable*>::const_iterator alignable = alignableList.begin(); alignable != alignableList.end(); ++alignable) {
00291 const align::PositionType &pos = (*alignable)->survey()->position();
00292 const align::RotationType &rot = (*alignable)->survey()->rotation();
00293
00294 AlignTransform value(CLHEP::Hep3Vector(pos.x(), pos.y(), pos.z()),
00295 CLHEP::HepRotation(CLHEP::HepRep3x3(rot.xx(), rot.xy(), rot.xz(),
00296 rot.yx(), rot.yy(), rot.yz(),
00297 rot.zx(), rot.zy(), rot.zz())),
00298 (*alignable)->id());
00299 SurveyError error((*alignable)->alignableObjectId(), (*alignable)->id(), (*alignable)->survey()->errors());
00300
00301 cscAlignments->m_align.push_back(value);
00302 cscSurveyErrors->m_surveyErrors.push_back(error);
00303 }
00304
00305
00306 poolDbService->writeOne<Alignments>( &(*cscAlignments), poolDbService->currentTime(), theCSCSurveyRecordName);
00307 poolDbService->writeOne<SurveyErrors>( &(*cscSurveyErrors), poolDbService->currentTime(), theCSCSurveyErrorRecordName);
00308 }
00309
00310 void MuonAlignment::saveSurveyToDB(void) {
00311 saveDTSurveyToDB();
00312 saveCSCSurveyToDB();
00313 }
00314
00315 void MuonAlignment::saveDTtoDB(void) {
00316
00317 edm::Service<cond::service::PoolDBOutputService> poolDbService;
00318 if( !poolDbService.isAvailable() )
00319 throw cms::Exception("NotAvailable") << "PoolDBOutputService not available";
00320
00321
00322 Alignments* dt_Alignments = theAlignableMuon->dtAlignments() ;
00323 AlignmentErrors* dt_AlignmentErrors = theAlignableMuon->dtAlignmentErrors();
00324
00325
00326 poolDbService->writeOne<Alignments>( &(*dt_Alignments), poolDbService->currentTime(), theDTAlignRecordName);
00327 poolDbService->writeOne<AlignmentErrors>( &(*dt_AlignmentErrors), poolDbService->currentTime(), theDTErrorRecordName);
00328 }
00329
00330 void MuonAlignment::saveCSCtoDB(void) {
00331
00332 edm::Service<cond::service::PoolDBOutputService> poolDbService;
00333 if( !poolDbService.isAvailable() )
00334 throw cms::Exception("NotAvailable") << "PoolDBOutputService not available";
00335
00336
00337 Alignments* csc_Alignments = theAlignableMuon->cscAlignments();
00338 AlignmentErrors* csc_AlignmentErrors = theAlignableMuon->cscAlignmentErrors();
00339
00340
00341 poolDbService->writeOne<Alignments>( &(*csc_Alignments), poolDbService->currentTime(), theCSCAlignRecordName);
00342 poolDbService->writeOne<AlignmentErrors>( &(*csc_AlignmentErrors), poolDbService->currentTime(), theCSCErrorRecordName);
00343 }
00344
00345 void MuonAlignment::saveToDB(void) {
00346 saveDTtoDB();
00347 saveCSCtoDB();
00348 }