CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/Alignment/MuonAlignment/python/geometryDiff.py

Go to the documentation of this file.
00001 #!/usr/bin/python
00002 
00003 from Alignment.MuonAlignment.geometryXMLparser import MuonGeometry, dtorder, cscorder
00004 import sys, getopt
00005 
00006 usage = "Usage: geometryDiff.py [-h|--help] [-e|--epsilon epsilon] geometry1.xml geometry2.xml"
00007 
00008 try:
00009   opts, args = getopt.getopt(sys.argv[1:], "he:", ["help", "epsilon="])
00010 except getopt.GetoptError, msg:
00011   print >>sys.stderr, usage
00012   sys.exit(2)
00013 
00014 if len(args) != 2:
00015   print >>sys.stderr, usage
00016   sys.exit(2)
00017 
00018 opts = dict(opts)
00019 
00020 if "-h" in opts or "--help" in opts:
00021   print usage
00022   sys.exit(0)
00023 
00024 epsilon = 1e-6
00025 if "-e" in opts: epsilon = float(opts["-e"])
00026 if "--epsilon" in opts: epsilon = float(opts["--epsilon"])
00027 
00028 geom1 = MuonGeometry(file(args[0]))
00029 geom2 = MuonGeometry(file(args[1]))
00030 
00031 from math import sin, cos, sqrt
00032 sqrtepsilon = sqrt(epsilon)
00033 
00034 def matrixmult(a, b):
00035   return [[sum([i*j for i, j in zip(row, col)]) for col in zip(*b)] for row in a]
00036 
00037 def transpose(a):
00038   return [[a[j][i] for j in range(len(a[i]))] for i in range(len(a))]
00039 
00040 def rotFromPhi(g):
00041   phix, phiy, phiz = g.phix, g.phiy, g.phiz
00042   rotX = [[1.,         0.,         0.,       ],
00043           [0.,         cos(phix),  sin(phix),],
00044           [0.,        -sin(phix),  cos(phix),]]
00045   rotY = [[cos(phiy),  0.,        -sin(phiy),],
00046           [0.,         1.,         0.,       ],
00047           [sin(phiy),  0.,         cos(phiy),]]
00048   rotZ = [[cos(phiz),  sin(phiz),  0.,       ],
00049           [-sin(phiz), cos(phiz),  0.,       ],
00050           [0.,         0.,         1.,       ]]
00051   return matrixmult(rotX, matrixmult(rotY, rotZ))
00052 
00053 def rotFromEuler(g):
00054   s1, s2, s3 = sin(g.alpha), sin(g.beta), sin(g.gamma)
00055   c1, c2, c3 = cos(g.alpha), cos(g.beta), cos(g.gamma)
00056   return [[c2 * c3,    c1 * s3 + s1 * s2 * c3,   s1 * s3 - c1 * s2 * c3,],
00057           [-c2 * s3,   c1 * c3 - s1 * s2 * s3,   s1 * c3 + c1 * s2 * s3,],
00058           [s2,        -s1 * c2,                  c1 * c2,               ]]
00059 
00060 def loopover(which):
00061   if which == "DT":
00062     keys = geom1.dt.keys()
00063     keys.sort(dtorder)
00064 
00065   elif which == "CSC":
00066     keys = geom1.csc.keys()
00067     keys.sort(cscorder)
00068 
00069   else: raise Exception
00070 
00071   for key in keys:
00072     if which == "DT":
00073       g1 = geom1.dt[key]
00074       g2 = geom2.dt[key]
00075     else:
00076       g1 = geom1.csc[key]
00077       g2 = geom2.csc[key]
00078 
00079     if g1.relativeto != g2.relativeto:
00080       print "%s %s relativeto=\"%s\" versus relativeto=\"%s\"" % (which, str(key), g1.relativeto, g2.relativeto)
00081 
00082     if abs(g1.x - g2.x) > epsilon or abs(g1.y - g2.y) > epsilon or abs(g1.z - g2.z) > epsilon:
00083       print "%s %s position difference: (%g, %g, %g) - (%g, %g, %g) = (%g, %g, %g)" % \
00084             (which, str(key), g1.x, g1.y, g1.z, g2.x, g2.y, g2.z, g1.x - g2.x, g1.y - g2.y, g1.z - g2.z)
00085 
00086     if "phix" in g1.__dict__:
00087       g1type = "phi"
00088       g1a, g1b, g1c = g1.phix, g1.phiy, g1.phiz
00089       g1rot = rotFromPhi(g1)
00090     else:
00091       g1type = "euler"
00092       g1a, g1b, g1c = g1.alpha, g1.beta, g1.gamma
00093       g1rot = rotFromEuler(g1)
00094 
00095     if "phix" in g2.__dict__:
00096       g2type = "phi"
00097       g2a, g2b, g2c = g2.phix, g2.phiy, g2.phiz
00098       g2rot = rotFromPhi(g2)
00099     else:
00100       g2type = "euler"
00101       g2a, g2b, g2c = g2.alpha, g2.beta, g2.gamma
00102       g2rot = rotFromEuler(g2)
00103     
00104     diff = matrixmult(g1rot, transpose(g2rot))
00105     if abs(diff[0][0] - 1.) > sqrtepsilon or abs(diff[1][1] - 1.) > sqrtepsilon or abs(diff[2][2] - 1.) > sqrtepsilon or \
00106        abs(diff[0][1]) > epsilon or abs(diff[0][2]) > epsilon or abs(diff[1][2]) > epsilon:
00107       print "%s %s rotation difference: %s(%g, %g, %g) - %s(%g, %g, %g) = %s" % \
00108             (which, str(key), g1type, g1a, g1b, g1c, g2type, g2a, g2b, g2c, str(diff))
00109 
00110 loopover("DT")
00111 loopover("CSC")
00112 
00113