Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:56:40

0001 #!/usr/bin/env python
0002 
0003 # XML must come from MuonGeometryDBConverter; not hand-made
0004 # Example configuration that will work
0005 # 
0006 # PSet outputXML = {
0007 #     string fileName = "tmp.xml"
0008 #     string relativeto = "container"   # keep in mind which relativeto you used when interpreting positions and angles!
0009 #     bool survey = false               # important: survey must be false
0010 #     bool rawIds = false               # important: rawIds must be false
0011 #     bool eulerAngles = false
0012 #     int32 precision = 10
0013 # }
0014 
0015 def dtorder(a, b):
0016   for ai, bi, name in zip(list(a) + [0]*(5 - len(a)), \
0017                           list(b) + [0]*(5 - len(b)), \
0018                           ("wheel", "station", "sector", "superlayer", "layer")):
0019     exec("a%s = %d" % (name, ai))
0020     exec("b%s = %d" % (name, bi))
0021 
0022   if awheel == bwheel and astation == bstation:
0023 
0024     if asector != bsector:
0025       if astation == 4: sectororder = [0, 1, 2, 3, 4, 13, 5, 6, 7, 8, 9, 10, 14, 11, 12]
0026       elif awheel == 0: sectororder = [0, 1, 5, 9, 2, 6, 10, 3, 7, 11, 4, 8, 12]
0027       else: sectororder = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
0028       return cmp(sectororder.index(asector), sectororder.index(bsector))
0029 
0030     elif asuperlayer != bsuperlayer:
0031       superlayerorder = [0, 1, 3, 2]
0032       return cmp(superlayerorder.index(asuperlayer), superlayerorder.index(bsuperlayer))
0033 
0034   return cmp(a, b)
0035 
0036 def cscorder(a, b):
0037   for ai, bi, name in zip(list(a) + [0]*(5 - len(a)), \
0038                           list(b) + [0]*(5 - len(b)), \
0039                           ("endcap", "station", "ring", "chamber", "layer")):
0040     exec("a%s = %d" % (name, ai))
0041     exec("b%s = %d" % (name, bi))
0042 
0043   if astation == 1 and aring == 3: return cmp(a, b)
0044 
0045   elif aendcap == bendcap and astation == bstation and aring == bring and achamber != bchamber:
0046     if achamber == 0: return -1 # upper hierarchy comes first
0047     if bchamber == 0: return  1 # upper hierarchy comes first
0048     if achamber % 2 == 1 and bchamber % 2 == 0: return -1  # odds come first
0049     elif achamber % 2 == 0 and bchamber % 2 == 1: return 1 # evens come after
0050 
0051   return cmp(a, b)
0052 
0053 # External libraries (standard in Python >= 2.4, at least)
0054 import xml.sax
0055 
0056 class Alignable:
0057     def pos(self):
0058         return self.x, self.y, self.z
0059     def covariance(self):
0060         return (self.xx, self.xy, self.xz, self.xa, self.xb, self.xc), (self.xy, self.yy, self.yz, self.ya, self.yb, self.yc), (self.xz, self.yz, self.zz, self.za, self.zb, self.zc), (self.xa, self.ya, self.za, self.aa, self.ab, self.ac), (self.xb, self.yb, self.zb, self.ab, self.bb, self.bc), (self.xc, self.yc, self.zc, self.ac, self.ac, self.cc)
0061 
0062 class DTAlignable:
0063     def index(self):
0064         i = []
0065         try: i.append(self.wheel)
0066         except AttributeError: pass
0067         try: i.append(self.station)
0068         except AttributeError: pass
0069         try: i.append(self.sector)
0070         except AttributeError: pass
0071         try: i.append(self.superlayer)
0072         except AttributeError: pass
0073         try: i.append(self.layer)
0074         except AttributeError: pass
0075         return tuple(i)
0076 
0077 class CSCAlignable:
0078     def index(self):
0079         i = []
0080         try: i.append(self.endcap)
0081         except AttributeError: pass
0082         try: i.append(self.station)
0083         except AttributeError: pass
0084         try: i.append(self.ring)
0085         except AttributeError: pass
0086         try: i.append(self.chamber)
0087         except AttributeError: pass
0088         try: i.append(self.layer)
0089         except AttributeError: pass
0090         return tuple(i)
0091 
0092 class Operation:
0093     def __init__(self):
0094         self.chambers = []
0095         self.setposition = {}
0096         self.setape = {}
0097 
0098 # This class is a subclass of something which knows how to parse XML
0099 class MuonGeometry(xml.sax.handler.ContentHandler):
0100     def __init__(self, stream=None):
0101         self.dt = {}
0102         self.csc = {}
0103         self._operation = None
0104 
0105         if stream is not None:
0106           parser = xml.sax.make_parser()
0107           parser.setContentHandler(self)
0108           parser.parse(stream)
0109 
0110     # what to do when you get to a <startelement>
0111     def startElement(self, tag, attrib):
0112         attrib = dict(attrib.items())
0113         if "rawId" in attrib: raise Exception("Please use \"rawIds = false\"")
0114         if "aa" in attrib: raise Exception("Please use \"survey = false\"")
0115 
0116         if tag == "MuonAlignment": pass
0117 
0118         elif tag == "collection": raise NotImplementedError("<collection /> and <collection> blocks aren't implemented yet")
0119 
0120         elif tag == "operation":
0121             self._operation = Operation()
0122 
0123         elif self._operation is None: raise Exception("All chambers and positions must be enclosed in <operation> blocks")
0124 
0125         elif tag == "setposition":
0126             self._operation.setposition["relativeto"] = str(attrib["relativeto"])
0127 
0128             for name in "x", "y", "z":
0129                 self._operation.setposition[name] = float(attrib[name])
0130             try:
0131                 for name in "phix", "phiy", "phiz":
0132                     self._operation.setposition[name] = float(attrib[name])
0133             except KeyError:
0134                 for name in "alpha", "beta", "gamma":
0135                     self._operation.setposition[name] = float(attrib[name])
0136 
0137         elif tag == "setape":
0138             for name in "xx", "xy", "xz", "xa", "xb", "xc", "yy", "yz", "ya", "yb", "yc", "zz", "za", "zb", "zc", "aa", "ab", "ac", "bb", "bc", "cc":
0139                 self._operation.setposition[name] = float(attrib[name])
0140 
0141         elif tag[0:2] == "DT":
0142             alignable = DTAlignable()
0143             for name in "wheel", "station", "sector", "superlayer", "layer":
0144                 if name in attrib:
0145                     alignable.__dict__[name] = int(attrib[name])
0146             self._operation.chambers.append(alignable)
0147 
0148         # <CSC...>: print endcap/station/ring/chamber/layer
0149         elif tag[0:3] == "CSC":
0150             alignable = CSCAlignable()
0151             for name in "endcap", "station", "ring", "chamber", "layer":
0152                 if name in attrib:
0153                     alignable.__dict__[name] = int(attrib[name])
0154             self._operation.chambers.append(alignable)
0155 
0156     # what to do when you get to an </endelement>
0157     def endElement(self, tag):
0158         if tag == "operation":
0159             if self._operation is None: raise Exception("Unbalanced <operation></operation>")
0160             for c in self._operation.chambers:
0161                 c.__dict__.update(self._operation.setposition)
0162                 c.__dict__.update(self._operation.setape)
0163                 if isinstance(c, DTAlignable): self.dt[c.index()] = c
0164                 elif isinstance(c, CSCAlignable): self.csc[c.index()] = c
0165 
0166     # writing back to xml
0167     def xml(self, stream=None, precision=10):
0168       if precision == None: format = "%g"
0169       else: format = "%." + str(precision) + "f"
0170 
0171       if stream == None:
0172         output = []
0173         writeline = lambda x: output.append(x)
0174       else:
0175         writeline = lambda x: stream.write(x)
0176 
0177       writeline("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
0178       writeline("<?xml-stylesheet type=\"text/xml\" href=\"MuonAlignment.xsl\"?>\n")
0179       writeline("<MuonAlignment>\n\n")
0180 
0181       dtkeys = self.dt.keys()
0182       dtkeys.sort(dtorder)
0183       csckeys = self.csc.keys()
0184       csckeys.sort(cscorder)
0185 
0186       def f(number): return format % number
0187 
0188       def position_ape(ali, attributes):
0189         writeline("  <%s%s />\n" % (level, attributes))
0190         writeline("  <setposition relativeto=\"%s\" x=\"%s\" y=\"%s\" z=\"%s\" phix=\"%s\" phiy=\"%s\" phiz=\"%s\" />\n" % \
0191                   (ali.relativeto, f(ali.x), f(ali.y), f(ali.z), f(ali.phix), f(ali.phiy), f(ali.phiz)))
0192 
0193         if "xx" in ali.__dict__:
0194           writeline("  <setape xx=\"%s\" xy=\"%s\" xz=\"%s\" xa=\"%s\" xb=\"%s\" xc=\"%s\" yy=\"%s\" yz=\"%s\" ya=\"%s\" yb=\"%s\" yc=\"%s\" zz=\"%s\" za=\"%s\" zb=\"%s\" zc=\"%s\" aa=\"%s\" ab=\"%s\" ac=\"%s\" bb=\"%s\" bc=\"%s\" cc=\"%s\" />\n" % \
0195                     (f(ali.xx), f(ali.xy), f(ali.xz), f(ali.xa), f(ali.xb), f(ali.xc), f(ali.yy), f(ali.yz), f(ali.ya), f(ali.yb), f(ali.yc), f(ali.zz), f(ali.za), f(ali.zb), f(ali.zc), f(ali.aa), f(ali.ab), f(ali.ac), f(ali.bb), f(ali.bc), f(ali.cc)))
0196 
0197       for key in dtkeys:
0198         writeline("<operation>\n")
0199 
0200         if len(key) == 0: level = "DTBarrel"
0201         elif len(key) == 1: level = "DTWheel "
0202         elif len(key) == 2: level = "DTStation "
0203         elif len(key) == 3: level = "DTChamber "
0204         elif len(key) == 4: level = "DTSuperLayer "
0205         elif len(key) == 5: level = "DTLayer "
0206 
0207         ali = self.dt[key]
0208         attributes = " ".join(["%s=\"%d\"" % (name, value) for name, value in zip(("wheel", "station", "sector", "superlayer", "layer"), key)])
0209         position_ape(ali, attributes)
0210 
0211         writeline("</operation>\n\n")
0212 
0213       for key in csckeys:
0214         writeline("<operation>\n")
0215 
0216         if len(key) == 1: level = "CSCEndcap "
0217         elif len(key) == 2: level = "CSCStation "
0218         elif len(key) == 3: level = "CSCRing "
0219         elif len(key) == 4: level = "CSCChamber "
0220         elif len(key) == 5: level = "CSCLayer "
0221 
0222         ali = self.csc[key]
0223         attributes = " ".join(["%s=\"%d\"" % (name, value) for name, value in zip(("endcap", "station", "ring", "chamber", "layer"), key)])
0224         position_ape(ali, attributes)
0225 
0226         writeline("</operation>\n\n")
0227 
0228       writeline("</MuonAlignment>\n")
0229       if stream == None: return "".join(output)