1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
#!/usr/bin/env python
# XML must come from MuonGeometryDBConverter; it must not be hand-made
# Example configuration that will work
#
# PSet outputXML = {
# string fileName = "tmp.xml"
# string relativeto = "container" # keep in mind which relativeto you used when interpreting positions and angles!
# bool survey = false # important: survey must be false
# bool rawIds = false # important: rawIds must be false
# bool eulerAngles = false
#
# untracked bool suppressDTBarrel = true
# untracked bool suppressDTWheels = true
# untracked bool suppressDTStations = true
# untracked bool suppressDTChambers = true
# untracked bool suppressDTSuperLayers = true
# untracked bool suppressDTLayers = true
# untracked bool suppressCSCEndcaps = true
# untracked bool suppressCSCStations = true
# untracked bool suppressCSCRings = true
# untracked bool suppressCSCChambers = true
# untracked bool suppressCSCLayers = false
# }
# External libraries (standard in Python >= 2.4, at least)
from xml.sax import handler, make_parser
from sys import stdin
# Headers for the CSV file
print("Alignable, wheel, station, sector, superlayer, layer, relativeto, x, y, z, angletype, phix, phiy, phiz, xx, xy, xz, yy, yz, zz")
print(", endcap, station, ring, chamber, layer, , , , , , alpha, beta, gamma, , , , , , ")
# This class is a subclass of something which knows how to parse XML
class ContentHandler(handler.ContentHandler):
# what to do when you get to a <startelement>
def startElement(self, tag, attrib):
attrib = dict(attrib.items())
if "rawId" in attrib: raise Exception("Please use \"rawIds = false\"")
if "aa" in attrib: raise Exception("Please use \"survey = false\"")
# <DT...>: print wheel/station/sector/superlayer/layer
if tag[0:2] == "DT":
print(tag, end=' ') # ending with a comma means "don't print end-of-line character"
for a in "wheel", "station", "sector", "superlayer", "layer":
if a in attrib:
print((", %s" % attrib[a]), end=' ')
else:
print(", ", end=' ')
# <CSC...>: print endcap/station/ring/chamber/layer
elif tag[0:3] == "CSC":
print(tag, end=' ')
for a in "endcap", "station", "ring", "chamber", "layer":
if a in attrib:
print((", %s" % attrib[a]), end=' ')
else:
print(", ", end=' ')
# <setposition>: print x, y, z and phix, phiy, phiz or alpha, beta, gamma
elif tag == "setposition":
print((", %(relativeto)s, %(x)s, %(y)s, %(z)s" % attrib), end=' ')
if "phix" in attrib:
print((", phixyz, %(phix)s, %(phiy)s, %(phiz)s" % attrib), end=' ')
else:
print((", Euler, %(alpha)s, %(beta)s, %(gamma)s" % attrib), end=' ')
# <setape>: print xx, xy, xz, yy, yz, zz
elif tag == "setape":
print((", %(xx)s, %(xy)s, %(xz)s, %(yy)s, %(yz)s, %(zz)s" % attrib), end=' ')
# what to do when you get to an </endelement>
def endElement(self, tag):
if tag == "operation":
print("") # end current line (note: no comma)
# Actually make it and use it on "stdin" (a file object)
parser = make_parser()
parser.setContentHandler(ContentHandler())
parser.parse(stdin)
|