CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_8_patch3/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.9 2011/06/07 19:38:24 khotilov 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                static AlignableObjectId converter;
00185                std::string typeName = converter.typeToName(alignableObjectId);
00186                if (alignableObjectId == align::AlignableDTSuperLayer) typeName = std::string("DTSuperLayer");
00187                if (alignableObjectId == align::AlignableDetUnit) typeName = std::string("DTLayer");
00188                outputFile << "  <" << typeName << " rawId=\"" << rawId << "\" />" << std::endl;
00189             }
00190             else {
00191                if (alignableObjectId == align::AlignableDetUnit) {
00192                   DTLayerId id(rawId);
00193                   outputFile << "  <DTLayer wheel=\"" << id.wheel() << "\" station=\"" << id.station() << "\" sector=\"" << id.sector() << "\" superlayer=\"" << id.superlayer() << "\" layer=\"" << id.layer() << "\" />" << std::endl;
00194                }
00195                else if (alignableObjectId == align::AlignableDTSuperLayer) {
00196                   DTSuperLayerId id(rawId);
00197                   outputFile << "  <DTSuperLayer wheel=\"" << id.wheel() << "\" station=\"" << id.station() << "\" sector=\"" << id.sector() << "\" superlayer=\"" << id.superlayer() << "\" />" << std::endl;
00198                }
00199                else if (alignableObjectId == align::AlignableDTChamber) {
00200                   DTChamberId id(rawId);
00201                   outputFile << "  <DTChamber wheel=\"" << id.wheel() << "\" station=\"" << id.station() << "\" sector=\"" << id.sector() << "\" />" << std::endl;
00202                }
00203 
00204                else {
00205                   DTChamberId id((*alignable)->id());
00206                   if (alignableObjectId == align::AlignableDTStation) {
00207                      outputFile << "  <DTStation wheel=\"" << id.wheel() << "\" station=\"" << id.station() << "\" />" << std::endl;
00208                   }
00209                   else if (alignableObjectId == align::AlignableDTWheel) {
00210                      outputFile << "  <DTWheel wheel=\"" << id.wheel() << "\" />" << std::endl;
00211                   }
00212                   else if (alignableObjectId == align::AlignableDTBarrel) {
00213                      outputFile << "  <DTBarrel />" << std::endl;
00214                   }
00215                   else throw cms::Exception("Alignment") << "Unknown DT Alignable StructureType" << std::endl;
00216                }
00217 
00218             } // end if not rawId
00219          } // end if DT
00220 
00221          else { // CSC
00222             if (m_rawIds  &&  rawId != 0) {
00223                static AlignableObjectId converter;
00224                std::string typeName = converter.typeToName(alignableObjectId);
00225                if (alignableObjectId == align::AlignableDetUnit) typeName = std::string("CSCLayer");
00226                outputFile << "  <" << typeName << " rawId=\"" << rawId << "\" />" << std::endl;
00227             }
00228             else {
00229                if (alignableObjectId == align::AlignableDetUnit) {
00230                   CSCDetId id(rawId);
00231                   outputFile << "  <CSCLayer endcap=\"" << id.endcap() << "\" station=\"" << id.station() << "\" ring=\"" << id.ring() << "\" chamber=\"" << id.chamber() << "\" layer=\"" << id.layer() << "\" />" << std::endl;
00232                }
00233                else if (alignableObjectId == align::AlignableCSCChamber) {
00234                   CSCDetId id(rawId);
00235                   outputFile << "  <CSCChamber endcap=\"" << id.endcap() << "\" station=\"" << id.station() << "\" ring=\"" << id.ring() << "\" chamber=\"" << id.chamber() << "\" />" << std::endl;
00236                }
00237                else {
00238                   CSCDetId id((*alignable)->id());
00239                   if (alignableObjectId == align::AlignableCSCRing) {
00240                      outputFile << "  <CSCRing endcap=\"" << id.endcap() << "\" station=\"" << id.station() << "\" ring=\"" << id.ring() << "\" />" << std::endl;
00241                   }
00242                   else if (alignableObjectId == align::AlignableCSCStation) {
00243                      outputFile << "  <CSCStation endcap=\"" << id.endcap() << "\" station=\"" << id.station() << "\" />" << std::endl;
00244                   }
00245                   else if (alignableObjectId == align::AlignableCSCEndcap) {
00246                      outputFile << "  <CSCEndcap endcap=\"" << id.endcap() << "\" />" << std::endl;
00247                   }
00248                   else throw cms::Exception("Alignment") << "Unknown CSC Alignable StructureType" << std::endl;
00249                
00250                }
00251 
00252             } // end if not rawId
00253          } // end if CSC
00254 
00255          align::PositionType pos = (*alignable)->globalPosition();
00256          align::RotationType rot = (*alignable)->globalRotation();
00257 
00258          if (m_survey) {
00259             pos = (*alignable)->survey()->position();
00260             rot = (*alignable)->survey()->rotation();
00261          }
00262 
00263          std::string str_relativeto;
00264          if (m_relativeto == 0) {
00265             str_relativeto = std::string("none");
00266          }
00267 
00268          else if (m_relativeto == 1) {
00269             if (ideal == ideals.end()  ||  (*ideal)->alignableObjectId() != alignableObjectId  ||  (*ideal)->id() != (*alignable)->id()) {
00270                throw cms::Exception("Alignment") << "AlignableMuon and ideal_AlignableMuon are out of sync!" << std::endl;
00271             }
00272 
00273             align::PositionType idealPosition = (*ideal)->globalPosition();
00274             align::RotationType idealRotation = (*ideal)->globalRotation();
00275 
00276             pos = align::PositionType(idealRotation * (pos.basicVector() - idealPosition.basicVector()));
00277             rot = rot * idealRotation.transposed();
00278 
00279             str_relativeto = std::string("ideal");
00280 
00281             bool csc_debug=0;
00282             if (csc_debug && !DT) {
00283               CSCDetId id(rawId);
00284               if(id.endcap()==1 && id.station()==1 && id.ring()==1 && id.chamber()==33 ){
00285                 std::cout<<" investigating "<<id<<std::endl<<(*alignable)->globalRotation()<<std::endl<<std::endl
00286                          <<idealRotation.transposed()<<std::endl<<std::endl<<rot<<std::endl<<std::endl;
00287                 double phix = atan2(rot.yz(), rot.zz());
00288                 double phiy = asin(-rot.xz());
00289                 double phiz = atan2(rot.xy(), rot.xx());
00290 
00291                 std::cout << "phix=\"" << phix << "\" phiy=\"" << phiy << "\" phiz=\"" << phiz << std::endl;
00292 
00293                 align::EulerAngles eulerAngles = align::toAngles((*alignable)->globalRotation());
00294                 std::cout << "alpha=\"" << eulerAngles(1) << "\" beta=\"" << eulerAngles(2) << "\" gamma=\"" << eulerAngles(3) << std::endl;
00295                 eulerAngles = align::toAngles(idealRotation);
00296                 std::cout << "alpha=\"" << eulerAngles(1) << "\" beta=\"" << eulerAngles(2) << "\" gamma=\"" << eulerAngles(3) << std::endl;
00297                 eulerAngles = align::toAngles(rot);
00298                 std::cout << "alpha=\"" << eulerAngles(1) << "\" beta=\"" << eulerAngles(2) << "\" gamma=\"" << eulerAngles(3) << std::endl;
00299               }
00300             }
00301          }
00302 
00303          else if (m_relativeto == 2  &&  (*alignable)->mother() != NULL) {
00304             align::PositionType globalPosition = (*alignable)->mother()->globalPosition();
00305             align::RotationType globalRotation = (*alignable)->mother()->globalRotation();
00306 
00307             pos = align::PositionType(globalRotation * (pos.basicVector() - globalPosition.basicVector()));
00308             rot = rot * globalRotation.transposed();
00309 
00310             str_relativeto = std::string("container");
00311          }
00312 
00313          else assert(false);  // can't happen: see constructor
00314 
00315          outputFile << "  <setposition relativeto=\"" << str_relativeto << "\" "
00316                     << "x=\"" << pos.x() << "\" y=\"" << pos.y() << "\" z=\"" << pos.z() << "\" ";
00317 
00318          if (m_eulerAngles) {
00319             align::EulerAngles eulerAngles = align::toAngles(rot);
00320             outputFile << "alpha=\"" << eulerAngles(1) << "\" beta=\"" << eulerAngles(2) << "\" gamma=\"" << eulerAngles(3) << "\" />" << std::endl;
00321          }
00322          
00323          else {
00324             // the angle convention originally used in alignment, also known as "non-standard Euler angles with a Z-Y-X convention"
00325             // this also gets the sign convention right
00326             double phix = atan2(rot.yz(), rot.zz());
00327             double phiy = asin(-rot.xz());
00328             double phiz = atan2(rot.xy(), rot.xx());
00329             
00330             outputFile << "phix=\"" << phix << "\" phiy=\"" << phiy << "\" phiz=\"" << phiz << "\" />" << std::endl;
00331          }
00332 
00333          if (m_survey) {
00334             align::ErrorMatrix err = (*alignable)->survey()->errors();
00335 
00336             outputFile << "  <setsurveyerr"
00337                        <<   " xx=\"" << err(0,0) << "\" xy=\"" << err(0,1) << "\" xz=\"" << err(0,2) << "\" xa=\"" << err(0,3) << "\" xb=\"" << err(0,4) << "\" xc=\"" << err(0,5)
00338                        << "\" yy=\"" << err(1,1) << "\" yz=\"" << err(1,2) << "\" ya=\"" << err(1,3) << "\" yb=\"" << err(1,4) << "\" yc=\"" << err(1,5)
00339                        << "\" zz=\"" << err(2,2) << "\" za=\"" << err(2,3) << "\" zb=\"" << err(2,4) << "\" zc=\"" << err(2,5)
00340                        << "\" aa=\"" << err(3,3) << "\" ab=\"" << err(3,4) << "\" ac=\"" << err(3,5)
00341                        << "\" bb=\"" << err(4,4) << "\" bc=\"" << err(4,5)
00342                        << "\" cc=\"" << err(5,5) << "\" />" << std::endl;
00343          }
00344 
00345          else if (rawId != 0) {
00346             CLHEP::HepSymMatrix err = errors[(*alignable)->id()];
00347 
00348             outputFile << "  <setape xx=\"" << err(1,1) << "\" xy=\"" << err(1,2) << "\" xz=\"" << err(1,3)
00349                        << "\" yy=\"" << err(2,2) << "\" yz=\"" << err(2,3) << "\" zz=\"" << err(3,3) << "\" />" << std::endl;
00350          }
00351 
00352          outputFile << "</operation>" << std::endl << std::endl;
00353 
00354       } // end if not suppressed
00355 
00356       // write superstructures before substructures: this is important because <setape> overwrites all substructures' APEs
00357       if (ideal != ideals.end()) {
00358          align::Alignables components = (*alignable)->components();
00359          align::Alignables ideal_components = (*ideal)->components();
00360          writeComponents(components, ideal_components, errors, outputFile, DT);
00361          ++ideal; // important for synchronization in the "for" loop!
00362       }
00363       else {
00364          align::Alignables components = (*alignable)->components();
00365          align::Alignables dummy;
00366          writeComponents(components, dummy, errors, outputFile, DT);
00367       }
00368 
00369    } // end loop over alignables
00370 }
00371 
00372 //
00373 // const member functions
00374 //
00375 
00376 //
00377 // static member functions
00378 //