CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC4_patch1/src/Alignment/MuonAlignment/src/MuonAlignmentOutputXML.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:     MuonAlignment
00004 // Class  :     MuonAlignmentOutputXML
00005 // 
00006 // Implementation:
00007 //     <Notes on implementation>
00008 //
00009 // Original Author:  
00010 //         Created:  Fri Mar 14 18:02:33 CDT 2008
00011 // $Id: MuonAlignmentOutputXML.cc,v 1.10 2012/06/30 09:13:41 eulisse Exp $
00012 //
00013 
00014 // system include files
00015 #include "FWCore/Framework/interface/ESHandle.h"
00016 #include "FWCore/Framework/interface/ESTransientHandle.h"
00017 
00018 // user include files
00019 #include "Alignment/MuonAlignment/interface/MuonAlignmentOutputXML.h"
00020 #include "Alignment/CommonAlignment/interface/AlignableObjectId.h"
00021 #include "DataFormats/MuonDetId/interface/DTChamberId.h"
00022 #include "DataFormats/MuonDetId/interface/DTSuperLayerId.h"
00023 #include "DataFormats/MuonDetId/interface/DTLayerId.h"
00024 #include "DataFormats/MuonDetId/interface/CSCDetId.h"
00025 #include "Geometry/Records/interface/MuonNumberingRecord.h"
00026 #include "Geometry/DTGeometryBuilder/src/DTGeometryBuilderFromDDD.h"
00027 #include "Geometry/CSCGeometryBuilder/src/CSCGeometryBuilderFromDDD.h"
00028 #include "Geometry/DTGeometry/interface/DTGeometry.h"
00029 #include "Geometry/CSCGeometry/interface/CSCGeometry.h"
00030 #include "Alignment/CommonAlignment/interface/SurveyDet.h"
00031 #include "CondFormats/Alignment/interface/AlignmentErrors.h"
00032 
00033 //
00034 // constants, enums and typedefs
00035 //
00036 
00037 //
00038 // static data member definitions
00039 //
00040 
00041 //
00042 // constructors and destructor
00043 //
00044 MuonAlignmentOutputXML::MuonAlignmentOutputXML(const edm::ParameterSet &iConfig)
00045    : m_fileName(iConfig.getParameter<std::string>("fileName"))
00046    , m_survey(iConfig.getParameter<bool>("survey"))
00047    , m_rawIds(iConfig.getParameter<bool>("rawIds"))
00048    , m_eulerAngles(iConfig.getParameter<bool>("eulerAngles"))
00049    , m_precision(iConfig.getParameter<int>("precision"))
00050    , m_suppressDTBarrel(iConfig.getUntrackedParameter<bool>("suppressDTBarrel", false))
00051    , m_suppressDTWheels(iConfig.getUntrackedParameter<bool>("suppressDTWheels", false))
00052    , m_suppressDTStations(iConfig.getUntrackedParameter<bool>("suppressDTStations", false))
00053    , m_suppressDTChambers(iConfig.getUntrackedParameter<bool>("suppressDTChambers", false))
00054    , m_suppressDTSuperLayers(iConfig.getUntrackedParameter<bool>("suppressDTSuperLayers", false))
00055    , m_suppressDTLayers(iConfig.getUntrackedParameter<bool>("suppressDTLayers", false))
00056    , m_suppressCSCEndcaps(iConfig.getUntrackedParameter<bool>("suppressCSCEndcaps", false))
00057    , m_suppressCSCStations(iConfig.getUntrackedParameter<bool>("suppressCSCStations", false))
00058    , m_suppressCSCRings(iConfig.getUntrackedParameter<bool>("suppressCSCRings", false))
00059    , m_suppressCSCChambers(iConfig.getUntrackedParameter<bool>("suppressCSCChambers", false))
00060    , m_suppressCSCLayers(iConfig.getUntrackedParameter<bool>("suppressCSCLayers", false))
00061 {
00062    std::string str_relativeto = iConfig.getParameter<std::string>("relativeto");
00063 
00064    if (str_relativeto == std::string("none")) {
00065       m_relativeto = 0;
00066    }
00067    else if (str_relativeto == std::string("ideal")) {
00068       m_relativeto = 1;
00069    }
00070    else if (str_relativeto == std::string("container")) {
00071       m_relativeto = 2;
00072    }
00073    else {
00074       throw cms::Exception("BadConfig") << "relativeto must be \"none\", \"ideal\", or \"container\"" << std::endl;
00075    }
00076 }
00077 
00078 // MuonAlignmentOutputXML::MuonAlignmentOutputXML(const MuonAlignmentOutputXML& rhs)
00079 // {
00080 //    // do actual copying here;
00081 // }
00082 
00083 MuonAlignmentOutputXML::~MuonAlignmentOutputXML()
00084 {
00085 }
00086 
00087 //
00088 // assignment operators
00089 //
00090 // const MuonAlignmentOutputXML& MuonAlignmentOutputXML::operator=(const MuonAlignmentOutputXML& rhs)
00091 // {
00092 //   //An exception safe implementation is
00093 //   MuonAlignmentOutputXML temp(rhs);
00094 //   swap(rhs);
00095 //
00096 //   return *this;
00097 // }
00098 
00099 //
00100 // member functions
00101 //
00102 
00103 void MuonAlignmentOutputXML::write(AlignableMuon *alignableMuon, const edm::EventSetup &iSetup) const {
00104    std::ofstream outputFile(m_fileName.c_str());
00105    outputFile << std::setprecision(m_precision) << std::fixed;
00106 
00107    outputFile << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << std::endl;
00108    outputFile << "<?xml-stylesheet type=\"text/xml\" href=\"MuonAlignment.xsl\"?>" << std::endl;
00109    outputFile << "<MuonAlignment>" << std::endl << std::endl;
00110    
00111    std::map<align::ID, CLHEP::HepSymMatrix> errors;
00112    AlignmentErrors *dtErrors = alignableMuon->dtAlignmentErrors();
00113    AlignmentErrors *cscErrors = alignableMuon->cscAlignmentErrors();
00114    for (std::vector<AlignTransformError>::const_iterator dtError = dtErrors->m_alignError.begin();  dtError != dtErrors->m_alignError.end();  ++dtError) {
00115       errors[dtError->rawId()] = dtError->matrix();
00116    }
00117    for (std::vector<AlignTransformError>::const_iterator cscError = cscErrors->m_alignError.begin();  cscError != cscErrors->m_alignError.end();  ++cscError) {
00118       errors[cscError->rawId()] = cscError->matrix();
00119    }
00120 
00121    align::Alignables barrels = alignableMuon->DTBarrel();
00122    align::Alignables endcaps = alignableMuon->CSCEndcaps();
00123 
00124    if (m_relativeto == 1) {
00125       edm::ESTransientHandle<DDCompactView> cpv;
00126       iSetup.get<IdealGeometryRecord>().get(cpv);
00127 
00128       edm::ESHandle<MuonDDDConstants> mdc;
00129       iSetup.get<MuonNumberingRecord>().get(mdc);
00130       DTGeometryBuilderFromDDD DTGeometryBuilder;
00131       CSCGeometryBuilderFromDDD CSCGeometryBuilder;
00132  
00133       boost::shared_ptr<DTGeometry> dtGeometry(new DTGeometry );
00134       DTGeometryBuilder.build(dtGeometry, &(*cpv), *mdc);
00135 
00136       boost::shared_ptr<CSCGeometry> boost_cscGeometry(new CSCGeometry);
00137       CSCGeometryBuilder.build(boost_cscGeometry, &(*cpv), *mdc);
00138 
00139       AlignableMuon ideal_alignableMuon(&(*dtGeometry), &(*boost_cscGeometry));
00140 
00141       align::Alignables ideal_barrels = ideal_alignableMuon.DTBarrel();
00142       align::Alignables ideal_endcaps = ideal_alignableMuon.CSCEndcaps();
00143 
00144       writeComponents(barrels, ideal_barrels, errors, outputFile, true);
00145       writeComponents(endcaps, ideal_endcaps, errors, outputFile, false);
00146    }
00147    else {
00148       align::Alignables empty1, empty2;
00149 
00150       writeComponents(barrels, empty1, errors, outputFile, true);
00151       writeComponents(endcaps, empty2, errors, outputFile, false);
00152    }
00153 
00154    outputFile << "</MuonAlignment>" << std::endl;
00155 }
00156 
00157 void MuonAlignmentOutputXML::writeComponents(align::Alignables &alignables, align::Alignables &ideals,
00158                                              std::map<align::ID, CLHEP::HepSymMatrix>& errors, std::ofstream &outputFile, bool DT) const {
00159    align::Alignables::const_iterator ideal = ideals.begin();
00160    for (align::Alignables::const_iterator alignable = alignables.begin();  alignable != alignables.end();  ++alignable) {
00161       if (m_survey  &&  (*alignable)->survey() == NULL) {
00162          throw cms::Exception("Alignment") << "SurveyDets must all be defined when writing to XML" << std::endl;
00163       } // now I can assume it's okay everywhere
00164 
00165       align::StructureType alignableObjectId = (*alignable)->alignableObjectId();
00166 
00167       if ((alignableObjectId == align::AlignableDTBarrel  &&  !m_suppressDTBarrel)  ||
00168           (alignableObjectId == align::AlignableDTWheel  &&  !m_suppressDTWheels)  ||
00169           (alignableObjectId == align::AlignableDTStation  &&  !m_suppressDTStations)  ||
00170           (alignableObjectId == align::AlignableDTChamber  &&  !m_suppressDTChambers)  ||
00171           (DT  &&  alignableObjectId == align::AlignableDTSuperLayer  &&  !m_suppressDTSuperLayers)  ||
00172           (DT  &&  alignableObjectId == align::AlignableDetUnit  &&  !m_suppressDTLayers)  ||
00173           (alignableObjectId == align::AlignableCSCEndcap  &&  !m_suppressCSCEndcaps)  ||
00174           (alignableObjectId == align::AlignableCSCStation  &&  !m_suppressCSCStations)  ||
00175           (alignableObjectId == align::AlignableCSCRing  &&  !m_suppressCSCRings)  ||
00176           (alignableObjectId == align::AlignableCSCChamber  &&  !m_suppressCSCChambers)  ||
00177           (!DT  &&  alignableObjectId == align::AlignableDetUnit  &&  !m_suppressCSCLayers)) {
00178 
00179          unsigned int rawId = (*alignable)->geomDetId().rawId();
00180          outputFile << "<operation>" << std::endl;
00181 
00182          if (DT) {
00183             if (m_rawIds  &&  rawId != 0) {
00184                std::string typeName = AlignableObjectId::idToString(alignableObjectId);
00185                if (alignableObjectId == align::AlignableDTSuperLayer) typeName = std::string("DTSuperLayer");
00186                if (alignableObjectId == align::AlignableDetUnit) typeName = std::string("DTLayer");
00187                outputFile << "  <" << typeName << " rawId=\"" << rawId << "\" />" << std::endl;
00188             }
00189             else {
00190                if (alignableObjectId == align::AlignableDetUnit) {
00191                   DTLayerId id(rawId);
00192                   outputFile << "  <DTLayer wheel=\"" << id.wheel() << "\" station=\"" << id.station() << "\" sector=\"" << id.sector() << "\" superlayer=\"" << id.superlayer() << "\" layer=\"" << id.layer() << "\" />" << std::endl;
00193                }
00194                else if (alignableObjectId == align::AlignableDTSuperLayer) {
00195                   DTSuperLayerId id(rawId);
00196                   outputFile << "  <DTSuperLayer wheel=\"" << id.wheel() << "\" station=\"" << id.station() << "\" sector=\"" << id.sector() << "\" superlayer=\"" << id.superlayer() << "\" />" << std::endl;
00197                }
00198                else if (alignableObjectId == align::AlignableDTChamber) {
00199                   DTChamberId id(rawId);
00200                   outputFile << "  <DTChamber wheel=\"" << id.wheel() << "\" station=\"" << id.station() << "\" sector=\"" << id.sector() << "\" />" << std::endl;
00201                }
00202 
00203                else {
00204                   DTChamberId id((*alignable)->id());
00205                   if (alignableObjectId == align::AlignableDTStation) {
00206                      outputFile << "  <DTStation wheel=\"" << id.wheel() << "\" station=\"" << id.station() << "\" />" << std::endl;
00207                   }
00208                   else if (alignableObjectId == align::AlignableDTWheel) {
00209                      outputFile << "  <DTWheel wheel=\"" << id.wheel() << "\" />" << std::endl;
00210                   }
00211                   else if (alignableObjectId == align::AlignableDTBarrel) {
00212                      outputFile << "  <DTBarrel />" << std::endl;
00213                   }
00214                   else throw cms::Exception("Alignment") << "Unknown DT Alignable StructureType" << std::endl;
00215                }
00216 
00217             } // end if not rawId
00218          } // end if DT
00219 
00220          else { // CSC
00221             if (m_rawIds  &&  rawId != 0) {
00222                std::string typeName = AlignableObjectId::idToString(alignableObjectId);
00223                if (alignableObjectId == align::AlignableDetUnit) typeName = std::string("CSCLayer");
00224                outputFile << "  <" << typeName << " rawId=\"" << rawId << "\" />" << std::endl;
00225             }
00226             else {
00227                if (alignableObjectId == align::AlignableDetUnit) {
00228                   CSCDetId id(rawId);
00229                   outputFile << "  <CSCLayer endcap=\"" << id.endcap() << "\" station=\"" << id.station() << "\" ring=\"" << id.ring() << "\" chamber=\"" << id.chamber() << "\" layer=\"" << id.layer() << "\" />" << std::endl;
00230                }
00231                else if (alignableObjectId == align::AlignableCSCChamber) {
00232                   CSCDetId id(rawId);
00233                   outputFile << "  <CSCChamber endcap=\"" << id.endcap() << "\" station=\"" << id.station() << "\" ring=\"" << id.ring() << "\" chamber=\"" << id.chamber() << "\" />" << std::endl;
00234                }
00235                else {
00236                   CSCDetId id((*alignable)->id());
00237                   if (alignableObjectId == align::AlignableCSCRing) {
00238                      outputFile << "  <CSCRing endcap=\"" << id.endcap() << "\" station=\"" << id.station() << "\" ring=\"" << id.ring() << "\" />" << std::endl;
00239                   }
00240                   else if (alignableObjectId == align::AlignableCSCStation) {
00241                      outputFile << "  <CSCStation endcap=\"" << id.endcap() << "\" station=\"" << id.station() << "\" />" << std::endl;
00242                   }
00243                   else if (alignableObjectId == align::AlignableCSCEndcap) {
00244                      outputFile << "  <CSCEndcap endcap=\"" << id.endcap() << "\" />" << std::endl;
00245                   }
00246                   else throw cms::Exception("Alignment") << "Unknown CSC Alignable StructureType" << std::endl;
00247                
00248                }
00249 
00250             } // end if not rawId
00251          } // end if CSC
00252 
00253          align::PositionType pos = (*alignable)->globalPosition();
00254          align::RotationType rot = (*alignable)->globalRotation();
00255 
00256          if (m_survey) {
00257             pos = (*alignable)->survey()->position();
00258             rot = (*alignable)->survey()->rotation();
00259          }
00260 
00261          std::string str_relativeto;
00262          if (m_relativeto == 0) {
00263             str_relativeto = std::string("none");
00264          }
00265 
00266          else if (m_relativeto == 1) {
00267             if (ideal == ideals.end()  ||  (*ideal)->alignableObjectId() != alignableObjectId  ||  (*ideal)->id() != (*alignable)->id()) {
00268                throw cms::Exception("Alignment") << "AlignableMuon and ideal_AlignableMuon are out of sync!" << std::endl;
00269             }
00270 
00271             align::PositionType idealPosition = (*ideal)->globalPosition();
00272             align::RotationType idealRotation = (*ideal)->globalRotation();
00273 
00274             pos = align::PositionType(idealRotation * (pos.basicVector() - idealPosition.basicVector()));
00275             rot = rot * idealRotation.transposed();
00276 
00277             str_relativeto = std::string("ideal");
00278 
00279             bool csc_debug=0;
00280             if (csc_debug && !DT) {
00281               CSCDetId id(rawId);
00282               if(id.endcap()==1 && id.station()==1 && id.ring()==1 && id.chamber()==33 ){
00283                 std::cout<<" investigating "<<id<<std::endl<<(*alignable)->globalRotation()<<std::endl<<std::endl
00284                          <<idealRotation.transposed()<<std::endl<<std::endl<<rot<<std::endl<<std::endl;
00285                 double phix = atan2(rot.yz(), rot.zz());
00286                 double phiy = asin(-rot.xz());
00287                 double phiz = atan2(rot.xy(), rot.xx());
00288 
00289                 std::cout << "phix=\"" << phix << "\" phiy=\"" << phiy << "\" phiz=\"" << phiz << std::endl;
00290 
00291                 align::EulerAngles eulerAngles = align::toAngles((*alignable)->globalRotation());
00292                 std::cout << "alpha=\"" << eulerAngles(1) << "\" beta=\"" << eulerAngles(2) << "\" gamma=\"" << eulerAngles(3) << std::endl;
00293                 eulerAngles = align::toAngles(idealRotation);
00294                 std::cout << "alpha=\"" << eulerAngles(1) << "\" beta=\"" << eulerAngles(2) << "\" gamma=\"" << eulerAngles(3) << std::endl;
00295                 eulerAngles = align::toAngles(rot);
00296                 std::cout << "alpha=\"" << eulerAngles(1) << "\" beta=\"" << eulerAngles(2) << "\" gamma=\"" << eulerAngles(3) << std::endl;
00297               }
00298             }
00299          }
00300 
00301          else if (m_relativeto == 2  &&  (*alignable)->mother() != NULL) {
00302             align::PositionType globalPosition = (*alignable)->mother()->globalPosition();
00303             align::RotationType globalRotation = (*alignable)->mother()->globalRotation();
00304 
00305             pos = align::PositionType(globalRotation * (pos.basicVector() - globalPosition.basicVector()));
00306             rot = rot * globalRotation.transposed();
00307 
00308             str_relativeto = std::string("container");
00309          }
00310 
00311          else assert(false);  // can't happen: see constructor
00312 
00313          outputFile << "  <setposition relativeto=\"" << str_relativeto << "\" "
00314                     << "x=\"" << pos.x() << "\" y=\"" << pos.y() << "\" z=\"" << pos.z() << "\" ";
00315 
00316          if (m_eulerAngles) {
00317             align::EulerAngles eulerAngles = align::toAngles(rot);
00318             outputFile << "alpha=\"" << eulerAngles(1) << "\" beta=\"" << eulerAngles(2) << "\" gamma=\"" << eulerAngles(3) << "\" />" << std::endl;
00319          }
00320          
00321          else {
00322             // the angle convention originally used in alignment, also known as "non-standard Euler angles with a Z-Y-X convention"
00323             // this also gets the sign convention right
00324             double phix = atan2(rot.yz(), rot.zz());
00325             double phiy = asin(-rot.xz());
00326             double phiz = atan2(rot.xy(), rot.xx());
00327             
00328             outputFile << "phix=\"" << phix << "\" phiy=\"" << phiy << "\" phiz=\"" << phiz << "\" />" << std::endl;
00329          }
00330 
00331          if (m_survey) {
00332             align::ErrorMatrix err = (*alignable)->survey()->errors();
00333 
00334             outputFile << "  <setsurveyerr"
00335                        <<   " xx=\"" << err(0,0) << "\" xy=\"" << err(0,1) << "\" xz=\"" << err(0,2) << "\" xa=\"" << err(0,3) << "\" xb=\"" << err(0,4) << "\" xc=\"" << err(0,5)
00336                        << "\" yy=\"" << err(1,1) << "\" yz=\"" << err(1,2) << "\" ya=\"" << err(1,3) << "\" yb=\"" << err(1,4) << "\" yc=\"" << err(1,5)
00337                        << "\" zz=\"" << err(2,2) << "\" za=\"" << err(2,3) << "\" zb=\"" << err(2,4) << "\" zc=\"" << err(2,5)
00338                        << "\" aa=\"" << err(3,3) << "\" ab=\"" << err(3,4) << "\" ac=\"" << err(3,5)
00339                        << "\" bb=\"" << err(4,4) << "\" bc=\"" << err(4,5)
00340                        << "\" cc=\"" << err(5,5) << "\" />" << std::endl;
00341          }
00342 
00343          else if (rawId != 0) {
00344             CLHEP::HepSymMatrix err = errors[(*alignable)->id()];
00345 
00346             outputFile << "  <setape xx=\"" << err(1,1) << "\" xy=\"" << err(1,2) << "\" xz=\"" << err(1,3)
00347                        << "\" yy=\"" << err(2,2) << "\" yz=\"" << err(2,3) << "\" zz=\"" << err(3,3) << "\" />" << std::endl;
00348          }
00349 
00350          outputFile << "</operation>" << std::endl << std::endl;
00351 
00352       } // end if not suppressed
00353 
00354       // write superstructures before substructures: this is important because <setape> overwrites all substructures' APEs
00355       if (ideal != ideals.end()) {
00356          align::Alignables components = (*alignable)->components();
00357          align::Alignables ideal_components = (*ideal)->components();
00358          writeComponents(components, ideal_components, errors, outputFile, DT);
00359          ++ideal; // important for synchronization in the "for" loop!
00360       }
00361       else {
00362          align::Alignables components = (*alignable)->components();
00363          align::Alignables dummy;
00364          writeComponents(components, dummy, errors, outputFile, DT);
00365       }
00366 
00367    } // end loop over alignables
00368 }
00369 
00370 //
00371 // const member functions
00372 //
00373 
00374 //
00375 // static member functions
00376 //