CMS 3D CMS Logo

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

Generated on Tue Jun 9 17:24:12 2009 for CMSSW by  doxygen 1.5.4