CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/Alignment/MuonAlignment/python/geometryXMLtoCSV.py

Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 # XML must come from MuonGeometryDBConverter; it must not be hand-made
00004 # Example configuration that will work
00005 # 
00006 # PSet outputXML = {
00007 #     string fileName = "tmp.xml"
00008 #     string relativeto = "container"   # keep in mind which relativeto you used when interpreting positions and angles!
00009 #     bool survey = false               # important: survey must be false
00010 #     bool rawIds = false               # important: rawIds must be false
00011 #     bool eulerAngles = false
00012 # 
00013 #     untracked bool suppressDTBarrel = true
00014 #     untracked bool suppressDTWheels = true
00015 #     untracked bool suppressDTStations = true
00016 #     untracked bool suppressDTChambers = true
00017 #     untracked bool suppressDTSuperLayers = true
00018 #     untracked bool suppressDTLayers = true
00019 #     untracked bool suppressCSCEndcaps = true
00020 #     untracked bool suppressCSCStations = true
00021 #     untracked bool suppressCSCRings = true
00022 #     untracked bool suppressCSCChambers = true
00023 #     untracked bool suppressCSCLayers = false
00024 # }
00025 
00026 # External libraries (standard in Python >= 2.4, at least)
00027 from xml.sax import handler, make_parser
00028 from sys import stdin
00029 
00030 # Headers for the CSV file
00031 print "Alignable, wheel, station, sector, superlayer, layer, relativeto, x, y, z, angletype, phix, phiy, phiz, xx, xy, xz, yy, yz, zz"
00032 print ", endcap, station, ring, chamber, layer, , , , , , alpha, beta, gamma, , , , , , "
00033 
00034 # This class is a subclass of something which knows how to parse XML
00035 class ContentHandler(handler.ContentHandler):
00036     # what to do when you get to a <startelement>
00037     def startElement(self, tag, attrib):
00038         attrib = dict(attrib.items())
00039         if "rawId" in attrib: raise Exception, "Please use \"rawIds = false\""
00040         if "aa" in attrib: raise Exception, "Please use \"survey = false\""
00041 
00042         # <DT...>: print wheel/station/sector/superlayer/layer
00043         if tag[0:2] == "DT":
00044             print tag,  # ending with a comma means "don't print end-of-line character"
00045             for a in "wheel", "station", "sector", "superlayer", "layer":
00046                 if a in attrib:
00047                     print (", %s" % attrib[a]),
00048                 else:
00049                     print ", ",
00050 
00051         # <CSC...>: print endcap/station/ring/chamber/layer
00052         elif tag[0:3] == "CSC":
00053             print tag,
00054             for a in "endcap", "station", "ring", "chamber", "layer":
00055                 if a in attrib:
00056                     print (", %s" % attrib[a]),
00057                 else:
00058                     print ", ",
00059 
00060         # <setposition>: print x, y, z and phix, phiy, phiz or alpha, beta, gamma
00061         elif tag == "setposition":
00062             print (", %(relativeto)s, %(x)s, %(y)s, %(z)s" % attrib),
00063             if "phix" in attrib:
00064                 print (", phixyz, %(phix)s, %(phiy)s, %(phiz)s" % attrib),
00065             else:
00066                 print (", Euler, %(alpha)s, %(beta)s, %(gamma)s" % attrib),
00067 
00068         # <setape>: print xx, xy, xz, yy, yz, zz
00069         elif tag == "setape":
00070             print (", %(xx)s, %(xy)s, %(xz)s, %(yy)s, %(yz)s, %(zz)s" % attrib),
00071 
00072     # what to do when you get to an </endelement>
00073     def endElement(self, tag):
00074         if tag == "operation":
00075             print ""  # end current line (note: no comma)
00076 
00077 # Actually make it and use it on "stdin" (a file object)
00078 parser = make_parser()
00079 parser.setContentHandler(ContentHandler())
00080 parser.parse(stdin)