CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
MuonAlignmentOutputXML.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: MuonAlignment
4 // Class : MuonAlignmentOutputXML
5 //
6 // Implementation:
7 // <Notes on implementation>
8 //
9 // Original Author:
10 // Created: Fri Mar 14 18:02:33 CDT 2008
11 // $Id: MuonAlignmentOutputXML.cc,v 1.9 2011/06/07 19:38:24 khotilov Exp $
12 //
13 
14 // system include files
17 
18 // user include files
32 
33 //
34 // constants, enums and typedefs
35 //
36 
37 //
38 // static data member definitions
39 //
40 
41 //
42 // constructors and destructor
43 //
45  : m_fileName(iConfig.getParameter<std::string>("fileName"))
46  , m_survey(iConfig.getParameter<bool>("survey"))
47  , m_rawIds(iConfig.getParameter<bool>("rawIds"))
48  , m_eulerAngles(iConfig.getParameter<bool>("eulerAngles"))
49  , m_precision(iConfig.getParameter<int>("precision"))
50  , m_suppressDTBarrel(iConfig.getUntrackedParameter<bool>("suppressDTBarrel", false))
51  , m_suppressDTWheels(iConfig.getUntrackedParameter<bool>("suppressDTWheels", false))
52  , m_suppressDTStations(iConfig.getUntrackedParameter<bool>("suppressDTStations", false))
53  , m_suppressDTChambers(iConfig.getUntrackedParameter<bool>("suppressDTChambers", false))
54  , m_suppressDTSuperLayers(iConfig.getUntrackedParameter<bool>("suppressDTSuperLayers", false))
55  , m_suppressDTLayers(iConfig.getUntrackedParameter<bool>("suppressDTLayers", false))
56  , m_suppressCSCEndcaps(iConfig.getUntrackedParameter<bool>("suppressCSCEndcaps", false))
57  , m_suppressCSCStations(iConfig.getUntrackedParameter<bool>("suppressCSCStations", false))
58  , m_suppressCSCRings(iConfig.getUntrackedParameter<bool>("suppressCSCRings", false))
59  , m_suppressCSCChambers(iConfig.getUntrackedParameter<bool>("suppressCSCChambers", false))
60  , m_suppressCSCLayers(iConfig.getUntrackedParameter<bool>("suppressCSCLayers", false))
61 {
62  std::string str_relativeto = iConfig.getParameter<std::string>("relativeto");
63 
64  if (str_relativeto == std::string("none")) {
65  m_relativeto = 0;
66  }
67  else if (str_relativeto == std::string("ideal")) {
68  m_relativeto = 1;
69  }
70  else if (str_relativeto == std::string("container")) {
71  m_relativeto = 2;
72  }
73  else {
74  throw cms::Exception("BadConfig") << "relativeto must be \"none\", \"ideal\", or \"container\"" << std::endl;
75  }
76 }
77 
78 // MuonAlignmentOutputXML::MuonAlignmentOutputXML(const MuonAlignmentOutputXML& rhs)
79 // {
80 // // do actual copying here;
81 // }
82 
84 {
85 }
86 
87 //
88 // assignment operators
89 //
90 // const MuonAlignmentOutputXML& MuonAlignmentOutputXML::operator=(const MuonAlignmentOutputXML& rhs)
91 // {
92 // //An exception safe implementation is
93 // MuonAlignmentOutputXML temp(rhs);
94 // swap(rhs);
95 //
96 // return *this;
97 // }
98 
99 //
100 // member functions
101 //
102 
103 void MuonAlignmentOutputXML::write(AlignableMuon *alignableMuon, const edm::EventSetup &iSetup) const {
104  std::ofstream outputFile(m_fileName.c_str());
105  outputFile << std::setprecision(m_precision) << std::fixed;
106 
107  outputFile << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << std::endl;
108  outputFile << "<?xml-stylesheet type=\"text/xml\" href=\"MuonAlignment.xsl\"?>" << std::endl;
109  outputFile << "<MuonAlignment>" << std::endl << std::endl;
110 
111  std::map<align::ID, CLHEP::HepSymMatrix> errors;
112  AlignmentErrorsExtended *dtErrors = alignableMuon->dtAlignmentErrorsExtended();
113  AlignmentErrorsExtended *cscErrors = alignableMuon->cscAlignmentErrorsExtended();
114  for (std::vector<AlignTransformErrorExtended>::const_iterator dtError = dtErrors->m_alignError.begin(); dtError != dtErrors->m_alignError.end(); ++dtError) {
115  errors[dtError->rawId()] = dtError->matrix();
116  }
117  for (std::vector<AlignTransformErrorExtended>::const_iterator cscError = cscErrors->m_alignError.begin(); cscError != cscErrors->m_alignError.end(); ++cscError) {
118  errors[cscError->rawId()] = cscError->matrix();
119  }
120 
121  align::Alignables barrels = alignableMuon->DTBarrel();
122  align::Alignables endcaps = alignableMuon->CSCEndcaps();
123 
124  if (m_relativeto == 1) {
126  iSetup.get<IdealGeometryRecord>().get(cpv);
127 
129  iSetup.get<MuonNumberingRecord>().get(mdc);
130  DTGeometryBuilderFromDDD DTGeometryBuilder;
132 
133  boost::shared_ptr<DTGeometry> dtGeometry(new DTGeometry );
134  DTGeometryBuilder.build(dtGeometry, &(*cpv), *mdc);
135 
136  boost::shared_ptr<CSCGeometry> boost_cscGeometry(new CSCGeometry);
137  CSCGeometryBuilder.build(boost_cscGeometry, &(*cpv), *mdc);
138 
139  AlignableMuon ideal_alignableMuon(&(*dtGeometry), &(*boost_cscGeometry));
140 
141  align::Alignables ideal_barrels = ideal_alignableMuon.DTBarrel();
142  align::Alignables ideal_endcaps = ideal_alignableMuon.CSCEndcaps();
143 
144  writeComponents(barrels, ideal_barrels, errors, outputFile, true);
145  writeComponents(endcaps, ideal_endcaps, errors, outputFile, false);
146  }
147  else {
148  align::Alignables empty1, empty2;
149 
150  writeComponents(barrels, empty1, errors, outputFile, true);
151  writeComponents(endcaps, empty2, errors, outputFile, false);
152  }
153 
154  outputFile << "</MuonAlignment>" << std::endl;
155 }
156 
158  std::map<align::ID, CLHEP::HepSymMatrix>& errors, std::ofstream &outputFile, bool DT) const {
159  align::Alignables::const_iterator ideal = ideals.begin();
160  for (align::Alignables::const_iterator alignable = alignables.begin(); alignable != alignables.end(); ++alignable) {
161  if (m_survey && (*alignable)->survey() == NULL) {
162  throw cms::Exception("Alignment") << "SurveyDets must all be defined when writing to XML" << std::endl;
163  } // now I can assume it's okay everywhere
164 
165  align::StructureType alignableObjectId = (*alignable)->alignableObjectId();
166 
167  if ((alignableObjectId == align::AlignableDTBarrel && !m_suppressDTBarrel) ||
168  (alignableObjectId == align::AlignableDTWheel && !m_suppressDTWheels) ||
169  (alignableObjectId == align::AlignableDTStation && !m_suppressDTStations) ||
170  (alignableObjectId == align::AlignableDTChamber && !m_suppressDTChambers) ||
171  (DT && alignableObjectId == align::AlignableDTSuperLayer && !m_suppressDTSuperLayers) ||
172  (DT && alignableObjectId == align::AlignableDetUnit && !m_suppressDTLayers) ||
173  (alignableObjectId == align::AlignableCSCEndcap && !m_suppressCSCEndcaps) ||
174  (alignableObjectId == align::AlignableCSCStation && !m_suppressCSCStations) ||
175  (alignableObjectId == align::AlignableCSCRing && !m_suppressCSCRings) ||
176  (alignableObjectId == align::AlignableCSCChamber && !m_suppressCSCChambers) ||
177  (!DT && alignableObjectId == align::AlignableDetUnit && !m_suppressCSCLayers)) {
178 
179  unsigned int rawId = (*alignable)->geomDetId().rawId();
180  outputFile << "<operation>" << std::endl;
181 
182  if (DT) {
183  if (m_rawIds && rawId != 0) {
184  std::string typeName = AlignableObjectId::idToString(alignableObjectId);
185  if (alignableObjectId == align::AlignableDTSuperLayer) typeName = std::string("DTSuperLayer");
186  if (alignableObjectId == align::AlignableDetUnit) typeName = std::string("DTLayer");
187  outputFile << " <" << typeName << " rawId=\"" << rawId << "\" />" << std::endl;
188  }
189  else {
190  if (alignableObjectId == align::AlignableDetUnit) {
191  DTLayerId id(rawId);
192  outputFile << " <DTLayer wheel=\"" << id.wheel() << "\" station=\"" << id.station() << "\" sector=\"" << id.sector() << "\" superlayer=\"" << id.superlayer() << "\" layer=\"" << id.layer() << "\" />" << std::endl;
193  }
194  else if (alignableObjectId == align::AlignableDTSuperLayer) {
195  DTSuperLayerId id(rawId);
196  outputFile << " <DTSuperLayer wheel=\"" << id.wheel() << "\" station=\"" << id.station() << "\" sector=\"" << id.sector() << "\" superlayer=\"" << id.superlayer() << "\" />" << std::endl;
197  }
198  else if (alignableObjectId == align::AlignableDTChamber) {
199  DTChamberId id(rawId);
200  outputFile << " <DTChamber wheel=\"" << id.wheel() << "\" station=\"" << id.station() << "\" sector=\"" << id.sector() << "\" />" << std::endl;
201  }
202 
203  else {
204  DTChamberId id((*alignable)->id());
205  if (alignableObjectId == align::AlignableDTStation) {
206  outputFile << " <DTStation wheel=\"" << id.wheel() << "\" station=\"" << id.station() << "\" />" << std::endl;
207  }
208  else if (alignableObjectId == align::AlignableDTWheel) {
209  outputFile << " <DTWheel wheel=\"" << id.wheel() << "\" />" << std::endl;
210  }
211  else if (alignableObjectId == align::AlignableDTBarrel) {
212  outputFile << " <DTBarrel />" << std::endl;
213  }
214  else throw cms::Exception("Alignment") << "Unknown DT Alignable StructureType" << std::endl;
215  }
216 
217  } // end if not rawId
218  } // end if DT
219 
220  else { // CSC
221  if (m_rawIds && rawId != 0) {
222  std::string typeName = AlignableObjectId::idToString(alignableObjectId);
223  if (alignableObjectId == align::AlignableDetUnit) typeName = std::string("CSCLayer");
224  outputFile << " <" << typeName << " rawId=\"" << rawId << "\" />" << std::endl;
225  }
226  else {
227  if (alignableObjectId == align::AlignableDetUnit) {
228  CSCDetId id(rawId);
229  outputFile << " <CSCLayer endcap=\"" << id.endcap() << "\" station=\"" << id.station() << "\" ring=\"" << id.ring() << "\" chamber=\"" << id.chamber() << "\" layer=\"" << id.layer() << "\" />" << std::endl;
230  }
231  else if (alignableObjectId == align::AlignableCSCChamber) {
232  CSCDetId id(rawId);
233  outputFile << " <CSCChamber endcap=\"" << id.endcap() << "\" station=\"" << id.station() << "\" ring=\"" << id.ring() << "\" chamber=\"" << id.chamber() << "\" />" << std::endl;
234  }
235  else {
236  CSCDetId id((*alignable)->id());
237  if (alignableObjectId == align::AlignableCSCRing) {
238  outputFile << " <CSCRing endcap=\"" << id.endcap() << "\" station=\"" << id.station() << "\" ring=\"" << id.ring() << "\" />" << std::endl;
239  }
240  else if (alignableObjectId == align::AlignableCSCStation) {
241  outputFile << " <CSCStation endcap=\"" << id.endcap() << "\" station=\"" << id.station() << "\" />" << std::endl;
242  }
243  else if (alignableObjectId == align::AlignableCSCEndcap) {
244  outputFile << " <CSCEndcap endcap=\"" << id.endcap() << "\" />" << std::endl;
245  }
246  else throw cms::Exception("Alignment") << "Unknown CSC Alignable StructureType" << std::endl;
247 
248  }
249 
250  } // end if not rawId
251  } // end if CSC
252 
253  align::PositionType pos = (*alignable)->globalPosition();
254  align::RotationType rot = (*alignable)->globalRotation();
255 
256  if (m_survey) {
257  pos = (*alignable)->survey()->position();
258  rot = (*alignable)->survey()->rotation();
259  }
260 
261  std::string str_relativeto;
262  if (m_relativeto == 0) {
263  str_relativeto = std::string("none");
264  }
265 
266  else if (m_relativeto == 1) {
267  if (ideal == ideals.end() || (*ideal)->alignableObjectId() != alignableObjectId || (*ideal)->id() != (*alignable)->id()) {
268  throw cms::Exception("Alignment") << "AlignableMuon and ideal_AlignableMuon are out of sync!" << std::endl;
269  }
270 
271  align::PositionType idealPosition = (*ideal)->globalPosition();
272  align::RotationType idealRotation = (*ideal)->globalRotation();
273 
274  pos = align::PositionType(idealRotation * (pos.basicVector() - idealPosition.basicVector()));
275  rot = rot * idealRotation.transposed();
276 
277  str_relativeto = std::string("ideal");
278 
279  bool csc_debug=0;
280  if (csc_debug && !DT) {
281  CSCDetId id(rawId);
282  if(id.endcap()==1 && id.station()==1 && id.ring()==1 && id.chamber()==33 ){
283  std::cout<<" investigating "<<id<<std::endl<<(*alignable)->globalRotation()<<std::endl<<std::endl
284  <<idealRotation.transposed()<<std::endl<<std::endl<<rot<<std::endl<<std::endl;
285  double phix = atan2(rot.yz(), rot.zz());
286  double phiy = asin(-rot.xz());
287  double phiz = atan2(rot.xy(), rot.xx());
288 
289  std::cout << "phix=\"" << phix << "\" phiy=\"" << phiy << "\" phiz=\"" << phiz << std::endl;
290 
291  align::EulerAngles eulerAngles = align::toAngles((*alignable)->globalRotation());
292  std::cout << "alpha=\"" << eulerAngles(1) << "\" beta=\"" << eulerAngles(2) << "\" gamma=\"" << eulerAngles(3) << std::endl;
293  eulerAngles = align::toAngles(idealRotation);
294  std::cout << "alpha=\"" << eulerAngles(1) << "\" beta=\"" << eulerAngles(2) << "\" gamma=\"" << eulerAngles(3) << std::endl;
295  eulerAngles = align::toAngles(rot);
296  std::cout << "alpha=\"" << eulerAngles(1) << "\" beta=\"" << eulerAngles(2) << "\" gamma=\"" << eulerAngles(3) << std::endl;
297  }
298  }
299  }
300 
301  else if (m_relativeto == 2 && (*alignable)->mother() != NULL) {
302  align::PositionType globalPosition = (*alignable)->mother()->globalPosition();
303  align::RotationType globalRotation = (*alignable)->mother()->globalRotation();
304 
305  pos = align::PositionType(globalRotation * (pos.basicVector() - globalPosition.basicVector()));
306  rot = rot * globalRotation.transposed();
307 
308  str_relativeto = std::string("container");
309  }
310 
311  else assert(false); // can't happen: see constructor
312 
313  outputFile << " <setposition relativeto=\"" << str_relativeto << "\" "
314  << "x=\"" << pos.x() << "\" y=\"" << pos.y() << "\" z=\"" << pos.z() << "\" ";
315 
316  if (m_eulerAngles) {
317  align::EulerAngles eulerAngles = align::toAngles(rot);
318  outputFile << "alpha=\"" << eulerAngles(1) << "\" beta=\"" << eulerAngles(2) << "\" gamma=\"" << eulerAngles(3) << "\" />" << std::endl;
319  }
320 
321  else {
322  // the angle convention originally used in alignment, also known as "non-standard Euler angles with a Z-Y-X convention"
323  // this also gets the sign convention right
324  double phix = atan2(rot.yz(), rot.zz());
325  double phiy = asin(-rot.xz());
326  double phiz = atan2(rot.xy(), rot.xx());
327 
328  outputFile << "phix=\"" << phix << "\" phiy=\"" << phiy << "\" phiz=\"" << phiz << "\" />" << std::endl;
329  }
330 
331  if (m_survey) {
332  align::ErrorMatrix err = (*alignable)->survey()->errors();
333 
334  outputFile << " <setsurveyerr"
335  << " xx=\"" << err(0,0) << "\" xy=\"" << err(0,1) << "\" xz=\"" << err(0,2) << "\" xa=\"" << err(0,3) << "\" xb=\"" << err(0,4) << "\" xc=\"" << err(0,5)
336  << "\" yy=\"" << err(1,1) << "\" yz=\"" << err(1,2) << "\" ya=\"" << err(1,3) << "\" yb=\"" << err(1,4) << "\" yc=\"" << err(1,5)
337  << "\" zz=\"" << err(2,2) << "\" za=\"" << err(2,3) << "\" zb=\"" << err(2,4) << "\" zc=\"" << err(2,5)
338  << "\" aa=\"" << err(3,3) << "\" ab=\"" << err(3,4) << "\" ac=\"" << err(3,5)
339  << "\" bb=\"" << err(4,4) << "\" bc=\"" << err(4,5)
340  << "\" cc=\"" << err(5,5) << "\" />" << std::endl;
341  }
342 
343  else if (rawId != 0) {
344 
345  CLHEP::HepSymMatrix err = errors[(*alignable)->id()];
346 
347  outputFile <<" <setape xx=\"" << err(0,0) << "\" xy=\"" << err(0,1) << "\" xz=\"" << err(0,2) << "\" xa=\"" << err(0,3) << "\" xb=\"" << err(0,4) << "\" xc=\"" << err(0,5)
348  << "\" yy=\"" << err(1,1) << "\" yz=\"" << err(1,2) << "\" ya=\"" << err(1,3) << "\" yb=\"" << err(1,4) << "\" yc=\"" << err(1,5)
349  << "\" zz=\"" << err(2,2) << "\" za=\"" << err(2,3) << "\" zb=\"" << err(2,4) << "\" zc=\"" << err(2,5)
350  << "\" aa=\"" << err(3,3) << "\" ab=\"" << err(3,4) << "\" ac=\"" << err(3,5)
351  << "\" bb=\"" << err(4,4) << "\" bc=\"" << err(4,5)
352  << "\" cc=\"" << err(5,5) << "\" />" << std::endl;
353  }
354 
355  outputFile << "</operation>" << std::endl << std::endl;
356 
357  } // end if not suppressed
358 
359  // write superstructures before substructures: this is important because <setape> overwrites all substructures' APEs
360  if (ideal != ideals.end()) {
361  align::Alignables components = (*alignable)->components();
362  align::Alignables ideal_components = (*ideal)->components();
363  writeComponents(components, ideal_components, errors, outputFile, DT);
364  ++ideal; // important for synchronization in the "for" loop!
365  }
366  else {
367  align::Alignables components = (*alignable)->components();
368  align::Alignables dummy;
369  writeComponents(components, dummy, errors, outputFile, DT);
370  }
371 
372  } // end loop over alignables
373 }
374 
375 //
376 // const member functions
377 //
378 
379 //
380 // static member functions
381 //
T xx() const
T getParameter(std::string const &) const
void write(AlignableMuon *alignableMuon, const edm::EventSetup &iSetup) const
assert(m_qm.get())
align::Alignables DTBarrel()
T y() const
Definition: PV3DBase.h:63
#define NULL
Definition: scimark2.h:8
void build(boost::shared_ptr< CSCGeometry > geom, const DDCompactView *fv, const MuonDDDConstants &muonConstants)
Build the geometry.
int endcap() const
Definition: CSCDetId.h:106
Point3DBase< Scalar, GlobalTag > PositionType
Definition: Definitions.h:30
T xy() const
T zz() const
align::Alignables CSCEndcaps()
T z() const
Definition: PV3DBase.h:64
EulerAngles toAngles(const RotationType &)
Convert rotation matrix to angles about x-, y-, z-axes (frame rotation).
Definition: Utilities.cc:7
AlignmentErrorsExtended * cscAlignmentErrorsExtended()
MuonAlignmentOutputXML(const edm::ParameterSet &iConfig)
AlgebraicVector EulerAngles
Definition: Definitions.h:36
void build(boost::shared_ptr< DTGeometry > theGeometry, const DDCompactView *cview, const MuonDDDConstants &muonConstants)
std::vector< AlignTransformErrorExtended > m_alignError
const T & get() const
Definition: EventSetup.h:55
AlignmentErrorsExtended * dtAlignmentErrorsExtended()
std::vector< Alignable * > Alignables
Definition: Utilities.h:28
T xz() const
TkRotation transposed() const
tuple cout
Definition: gather_cfg.py:121
math::Error< 6 >::type ErrorMatrix
Definition: Definitions.h:39
volatile std::atomic< bool > shutdown_flag false
static const char * idToString(align::StructureType type)
int wheel() const
Return the wheel number.
Definition: DTChamberId.h:45
Constructor of the full muon geometry.
Definition: AlignableMuon.h:36
T x() const
Definition: PV3DBase.h:62
const BasicVectorType & basicVector() const
Definition: PV3DBase.h:56
void writeComponents(align::Alignables &alignables, align::Alignables &ideals, std::map< align::ID, CLHEP::HepSymMatrix > &errors, std::ofstream &outputFile, bool DT) const
T yz() const