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  auto dtGeometry = std::shared_ptr<DTGeometry>();
134  DTGeometryBuilder.build(dtGeometry, &(*cpv), *mdc);
135 
136  auto boost_cscGeometry = std::make_shared<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, alignableMuon->objectIdProvider());
145  writeComponents(endcaps, ideal_endcaps, errors, outputFile, false, alignableMuon->objectIdProvider());
146  }
147  else {
148  align::Alignables empty1, empty2;
149 
150  writeComponents(barrels, empty1, errors, outputFile, true, alignableMuon->objectIdProvider());
151  writeComponents(endcaps, empty2, errors, outputFile, false, alignableMuon->objectIdProvider());
152  }
153 
154  outputFile << "</MuonAlignment>" << std::endl;
155 }
156 
158  align::Alignables &ideals,
159  std::map<align::ID, CLHEP::HepSymMatrix>& errors,
160  std::ofstream &outputFile,
161  bool DT,
162  const AlignableObjectId& objectIdProvider) const {
163  align::Alignables::const_iterator ideal = ideals.begin();
164  for (align::Alignables::const_iterator alignable = alignables.begin(); alignable != alignables.end(); ++alignable) {
165  if (m_survey && (*alignable)->survey() == NULL) {
166  throw cms::Exception("Alignment") << "SurveyDets must all be defined when writing to XML" << std::endl;
167  } // now I can assume it's okay everywhere
168 
169  align::StructureType alignableObjectId = (*alignable)->alignableObjectId();
170 
171  if ((alignableObjectId == align::AlignableDTBarrel && !m_suppressDTBarrel) ||
172  (alignableObjectId == align::AlignableDTWheel && !m_suppressDTWheels) ||
173  (alignableObjectId == align::AlignableDTStation && !m_suppressDTStations) ||
174  (alignableObjectId == align::AlignableDTChamber && !m_suppressDTChambers) ||
175  (DT && alignableObjectId == align::AlignableDTSuperLayer && !m_suppressDTSuperLayers) ||
176  (DT && alignableObjectId == align::AlignableDetUnit && !m_suppressDTLayers) ||
177  (alignableObjectId == align::AlignableCSCEndcap && !m_suppressCSCEndcaps) ||
178  (alignableObjectId == align::AlignableCSCStation && !m_suppressCSCStations) ||
179  (alignableObjectId == align::AlignableCSCRing && !m_suppressCSCRings) ||
180  (alignableObjectId == align::AlignableCSCChamber && !m_suppressCSCChambers) ||
181  (!DT && alignableObjectId == align::AlignableDetUnit && !m_suppressCSCLayers)) {
182 
183  unsigned int rawId = (*alignable)->geomDetId().rawId();
184  outputFile << "<operation>" << std::endl;
185 
186  if (DT) {
187  if (m_rawIds && rawId != 0) {
188  std::string typeName = objectIdProvider.idToString(alignableObjectId);
189  if (alignableObjectId == align::AlignableDTSuperLayer) typeName = std::string("DTSuperLayer");
190  if (alignableObjectId == align::AlignableDetUnit) typeName = std::string("DTLayer");
191  outputFile << " <" << typeName << " rawId=\"" << rawId << "\" />" << std::endl;
192  }
193  else {
194  if (alignableObjectId == align::AlignableDetUnit) {
195  DTLayerId id(rawId);
196  outputFile << " <DTLayer wheel=\"" << id.wheel() << "\" station=\"" << id.station() << "\" sector=\"" << id.sector() << "\" superlayer=\"" << id.superlayer() << "\" layer=\"" << id.layer() << "\" />" << std::endl;
197  }
198  else if (alignableObjectId == align::AlignableDTSuperLayer) {
199  DTSuperLayerId id(rawId);
200  outputFile << " <DTSuperLayer wheel=\"" << id.wheel() << "\" station=\"" << id.station() << "\" sector=\"" << id.sector() << "\" superlayer=\"" << id.superlayer() << "\" />" << std::endl;
201  }
202  else if (alignableObjectId == align::AlignableDTChamber) {
203  DTChamberId id(rawId);
204  outputFile << " <DTChamber wheel=\"" << id.wheel() << "\" station=\"" << id.station() << "\" sector=\"" << id.sector() << "\" />" << std::endl;
205  }
206 
207  else {
208  DTChamberId id((*alignable)->id());
209  if (alignableObjectId == align::AlignableDTStation) {
210  outputFile << " <DTStation wheel=\"" << id.wheel() << "\" station=\"" << id.station() << "\" />" << std::endl;
211  }
212  else if (alignableObjectId == align::AlignableDTWheel) {
213  outputFile << " <DTWheel wheel=\"" << id.wheel() << "\" />" << std::endl;
214  }
215  else if (alignableObjectId == align::AlignableDTBarrel) {
216  outputFile << " <DTBarrel />" << std::endl;
217  }
218  else throw cms::Exception("Alignment") << "Unknown DT Alignable StructureType" << std::endl;
219  }
220 
221  } // end if not rawId
222  } // end if DT
223 
224  else { // CSC
225  if (m_rawIds && rawId != 0) {
226  std::string typeName = objectIdProvider.idToString(alignableObjectId);
227  if (alignableObjectId == align::AlignableDetUnit) typeName = std::string("CSCLayer");
228  outputFile << " <" << typeName << " rawId=\"" << rawId << "\" />" << std::endl;
229  }
230  else {
231  if (alignableObjectId == align::AlignableDetUnit) {
232  CSCDetId id(rawId);
233  outputFile << " <CSCLayer endcap=\"" << id.endcap() << "\" station=\"" << id.station() << "\" ring=\"" << id.ring() << "\" chamber=\"" << id.chamber() << "\" layer=\"" << id.layer() << "\" />" << std::endl;
234  }
235  else if (alignableObjectId == align::AlignableCSCChamber) {
236  CSCDetId id(rawId);
237  outputFile << " <CSCChamber endcap=\"" << id.endcap() << "\" station=\"" << id.station() << "\" ring=\"" << id.ring() << "\" chamber=\"" << id.chamber() << "\" />" << std::endl;
238  }
239  else {
240  CSCDetId id((*alignable)->id());
241  if (alignableObjectId == align::AlignableCSCRing) {
242  outputFile << " <CSCRing endcap=\"" << id.endcap() << "\" station=\"" << id.station() << "\" ring=\"" << id.ring() << "\" />" << std::endl;
243  }
244  else if (alignableObjectId == align::AlignableCSCStation) {
245  outputFile << " <CSCStation endcap=\"" << id.endcap() << "\" station=\"" << id.station() << "\" />" << std::endl;
246  }
247  else if (alignableObjectId == align::AlignableCSCEndcap) {
248  outputFile << " <CSCEndcap endcap=\"" << id.endcap() << "\" />" << std::endl;
249  }
250  else throw cms::Exception("Alignment") << "Unknown CSC Alignable StructureType" << std::endl;
251 
252  }
253 
254  } // end if not rawId
255  } // end if CSC
256 
257  align::PositionType pos = (*alignable)->globalPosition();
258  align::RotationType rot = (*alignable)->globalRotation();
259 
260  if (m_survey) {
261  pos = (*alignable)->survey()->position();
262  rot = (*alignable)->survey()->rotation();
263  }
264 
265  std::string str_relativeto;
266  if (m_relativeto == 0) {
267  str_relativeto = std::string("none");
268  }
269 
270  else if (m_relativeto == 1) {
271  if (ideal == ideals.end() || (*ideal)->alignableObjectId() != alignableObjectId || (*ideal)->id() != (*alignable)->id()) {
272  throw cms::Exception("Alignment") << "AlignableMuon and ideal_AlignableMuon are out of sync!" << std::endl;
273  }
274 
275  align::PositionType idealPosition = (*ideal)->globalPosition();
276  align::RotationType idealRotation = (*ideal)->globalRotation();
277 
278  pos = align::PositionType(idealRotation * (pos.basicVector() - idealPosition.basicVector()));
279  rot = rot * idealRotation.transposed();
280 
281  str_relativeto = std::string("ideal");
282 
283  bool csc_debug=0;
284  if (csc_debug && !DT) {
285  CSCDetId id(rawId);
286  if(id.endcap()==1 && id.station()==1 && id.ring()==1 && id.chamber()==33 ){
287  std::cout<<" investigating "<<id<<std::endl<<(*alignable)->globalRotation()<<std::endl<<std::endl
288  <<idealRotation.transposed()<<std::endl<<std::endl<<rot<<std::endl<<std::endl;
289  double phix = atan2(rot.yz(), rot.zz());
290  double phiy = asin(-rot.xz());
291  double phiz = atan2(rot.xy(), rot.xx());
292 
293  std::cout << "phix=\"" << phix << "\" phiy=\"" << phiy << "\" phiz=\"" << phiz << std::endl;
294 
295  align::EulerAngles eulerAngles = align::toAngles((*alignable)->globalRotation());
296  std::cout << "alpha=\"" << eulerAngles(1) << "\" beta=\"" << eulerAngles(2) << "\" gamma=\"" << eulerAngles(3) << std::endl;
297  eulerAngles = align::toAngles(idealRotation);
298  std::cout << "alpha=\"" << eulerAngles(1) << "\" beta=\"" << eulerAngles(2) << "\" gamma=\"" << eulerAngles(3) << std::endl;
299  eulerAngles = align::toAngles(rot);
300  std::cout << "alpha=\"" << eulerAngles(1) << "\" beta=\"" << eulerAngles(2) << "\" gamma=\"" << eulerAngles(3) << std::endl;
301  }
302  }
303  }
304 
305  else if (m_relativeto == 2 && (*alignable)->mother() != NULL) {
306  align::PositionType globalPosition = (*alignable)->mother()->globalPosition();
307  align::RotationType globalRotation = (*alignable)->mother()->globalRotation();
308 
309  pos = align::PositionType(globalRotation * (pos.basicVector() - globalPosition.basicVector()));
310  rot = rot * globalRotation.transposed();
311 
312  str_relativeto = std::string("container");
313  }
314 
315  else assert(false); // can't happen: see constructor
316 
317  outputFile << " <setposition relativeto=\"" << str_relativeto << "\" "
318  << "x=\"" << pos.x() << "\" y=\"" << pos.y() << "\" z=\"" << pos.z() << "\" ";
319 
320  if (m_eulerAngles) {
321  align::EulerAngles eulerAngles = align::toAngles(rot);
322  outputFile << "alpha=\"" << eulerAngles(1) << "\" beta=\"" << eulerAngles(2) << "\" gamma=\"" << eulerAngles(3) << "\" />" << std::endl;
323  }
324 
325  else {
326  // the angle convention originally used in alignment, also known as "non-standard Euler angles with a Z-Y-X convention"
327  // this also gets the sign convention right
328  double phix = atan2(rot.yz(), rot.zz());
329  double phiy = asin(-rot.xz());
330  double phiz = atan2(rot.xy(), rot.xx());
331 
332  outputFile << "phix=\"" << phix << "\" phiy=\"" << phiy << "\" phiz=\"" << phiz << "\" />" << std::endl;
333  }
334 
335  if (m_survey) {
336  align::ErrorMatrix err = (*alignable)->survey()->errors();
337 
338  outputFile << " <setsurveyerr"
339  << " xx=\"" << err(0,0) << "\" xy=\"" << err(0,1) << "\" xz=\"" << err(0,2) << "\" xa=\"" << err(0,3) << "\" xb=\"" << err(0,4) << "\" xc=\"" << err(0,5)
340  << "\" yy=\"" << err(1,1) << "\" yz=\"" << err(1,2) << "\" ya=\"" << err(1,3) << "\" yb=\"" << err(1,4) << "\" yc=\"" << err(1,5)
341  << "\" zz=\"" << err(2,2) << "\" za=\"" << err(2,3) << "\" zb=\"" << err(2,4) << "\" zc=\"" << err(2,5)
342  << "\" aa=\"" << err(3,3) << "\" ab=\"" << err(3,4) << "\" ac=\"" << err(3,5)
343  << "\" bb=\"" << err(4,4) << "\" bc=\"" << err(4,5)
344  << "\" cc=\"" << err(5,5) << "\" />" << std::endl;
345  }
346 
347  else if (rawId != 0) {
348 
349  CLHEP::HepSymMatrix err = errors[(*alignable)->id()];
350 
351  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)
352  << "\" yy=\"" << err(1,1) << "\" yz=\"" << err(1,2) << "\" ya=\"" << err(1,3) << "\" yb=\"" << err(1,4) << "\" yc=\"" << err(1,5)
353  << "\" zz=\"" << err(2,2) << "\" za=\"" << err(2,3) << "\" zb=\"" << err(2,4) << "\" zc=\"" << err(2,5)
354  << "\" aa=\"" << err(3,3) << "\" ab=\"" << err(3,4) << "\" ac=\"" << err(3,5)
355  << "\" bb=\"" << err(4,4) << "\" bc=\"" << err(4,5)
356  << "\" cc=\"" << err(5,5) << "\" />" << std::endl;
357  }
358 
359  outputFile << "</operation>" << std::endl << std::endl;
360 
361  } // end if not suppressed
362 
363  // write superstructures before substructures: this is important because <setape> overwrites all substructures' APEs
364  if (ideal != ideals.end()) {
365  align::Alignables components = (*alignable)->components();
366  align::Alignables ideal_components = (*ideal)->components();
367  writeComponents(components, ideal_components, errors, outputFile, DT, objectIdProvider);
368  ++ideal; // important for synchronization in the "for" loop!
369  }
370  else {
371  align::Alignables components = (*alignable)->components();
372  align::Alignables dummy;
373  writeComponents(components, dummy, errors, outputFile, DT, objectIdProvider);
374  }
375 
376  } // end loop over alignables
377 }
378 
379 //
380 // const member functions
381 //
382 
383 //
384 // static member functions
385 //
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
const AlignableObjectId & objectIdProvider() const
Return muon alignable object ID provider derived from the muon system geometry.
Definition: AlignableMuon.h:82
int endcap() const
Definition: CSCDetId.h:93
Point3DBase< Scalar, GlobalTag > PositionType
Definition: Definitions.h:30
T xy() const
T zz() const
align::Alignables CSCEndcaps()
T z() const
Definition: PV3DBase.h:64
Allows conversion between type and name, and vice-versa.
EulerAngles toAngles(const RotationType &)
Convert rotation matrix to angles about x-, y-, z-axes (frame rotation).
Definition: Utilities.cc:10
AlignmentErrorsExtended * cscAlignmentErrorsExtended()
MuonAlignmentOutputXML(const edm::ParameterSet &iConfig)
AlgebraicVector EulerAngles
Definition: Definitions.h:36
const char * idToString(align::StructureType type) const
std::vector< AlignTransformErrorExtended > m_alignError
void writeComponents(align::Alignables &alignables, align::Alignables &ideals, std::map< align::ID, CLHEP::HepSymMatrix > &errors, std::ofstream &outputFile, bool DT, const AlignableObjectId &) const
const T & get() const
Definition: EventSetup.h:56
void build(std::shared_ptr< CSCGeometry > geom, const DDCompactView *fv, const MuonDDDConstants &muonConstants)
Build the geometry.
AlignmentErrorsExtended * dtAlignmentErrorsExtended()
std::vector< Alignable * > Alignables
Definition: Utilities.h:30
void build(std::shared_ptr< DTGeometry > theGeometry, const DDCompactView *cview, const MuonDDDConstants &muonConstants)
T xz() const
TkRotation transposed() const
tuple cout
Definition: gather_cfg.py:145
math::Error< 6 >::type ErrorMatrix
Definition: Definitions.h:39
volatile std::atomic< bool > shutdown_flag false
int wheel() const
Return the wheel number.
Definition: DTChamberId.h:45
Constructor of the full muon geometry.
Definition: AlignableMuon.h:37
T x() const
Definition: PV3DBase.h:62
const BasicVectorType & basicVector() const
Definition: PV3DBase.h:56
T yz() const