test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
geometryDiff.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 
3 from Alignment.MuonAlignment.geometryXMLparser import MuonGeometry, dtorder, cscorder
4 import sys, getopt
5 
6 usage = "Usage: geometryDiff.py [-h|--help] [-e|--epsilon epsilon] geometry1.xml geometry2.xml"
7 
8 try:
9  opts, args = getopt.getopt(sys.argv[1:], "he:", ["help", "epsilon="])
10 except getopt.GetoptError as msg:
11  print >>sys.stderr, usage
12  sys.exit(2)
13 
14 if len(args) != 2:
15  print >>sys.stderr, usage
16  sys.exit(2)
17 
18 opts = dict(opts)
19 
20 if "-h" in opts or "--help" in opts:
21  print usage
22  sys.exit(0)
23 
24 epsilon = 1e-6
25 if "-e" in opts: epsilon = float(opts["-e"])
26 if "--epsilon" in opts: epsilon = float(opts["--epsilon"])
27 
28 geom1 = MuonGeometry(file(args[0]))
29 geom2 = MuonGeometry(file(args[1]))
30 
31 from math import sin, cos, sqrt
32 sqrtepsilon = sqrt(epsilon)
33 
34 def matrixmult(a, b):
35  return [[sum([i*j for i, j in zip(row, col)]) for col in zip(*b)] for row in a]
36 
37 def transpose(a):
38  return [[a[j][i] for j in range(len(a[i]))] for i in range(len(a))]
39 
40 def rotFromPhi(g):
41  phix, phiy, phiz = g.phix, g.phiy, g.phiz
42  rotX = [[1., 0., 0., ],
43  [0., cos(phix), sin(phix),],
44  [0., -sin(phix), cos(phix),]]
45  rotY = [[cos(phiy), 0., -sin(phiy),],
46  [0., 1., 0., ],
47  [sin(phiy), 0., cos(phiy),]]
48  rotZ = [[cos(phiz), sin(phiz), 0., ],
49  [-sin(phiz), cos(phiz), 0., ],
50  [0., 0., 1., ]]
51  return matrixmult(rotX, matrixmult(rotY, rotZ))
52 
53 def rotFromEuler(g):
54  s1, s2, s3 = sin(g.alpha), sin(g.beta), sin(g.gamma)
55  c1, c2, c3 = cos(g.alpha), cos(g.beta), cos(g.gamma)
56  return [[c2 * c3, c1 * s3 + s1 * s2 * c3, s1 * s3 - c1 * s2 * c3,],
57  [-c2 * s3, c1 * c3 - s1 * s2 * s3, s1 * c3 + c1 * s2 * s3,],
58  [s2, -s1 * c2, c1 * c2, ]]
59 
60 def loopover(which):
61  if which == "DT":
62  keys = geom1.dt.keys()
63  keys.sort(dtorder)
64 
65  elif which == "CSC":
66  keys = geom1.csc.keys()
67  keys.sort(cscorder)
68 
69  else: raise Exception
70 
71  for key in keys:
72  if which == "DT":
73  g1 = geom1.dt[key]
74  g2 = geom2.dt[key]
75  else:
76  g1 = geom1.csc[key]
77  g2 = geom2.csc[key]
78 
79  if g1.relativeto != g2.relativeto:
80  print "%s %s relativeto=\"%s\" versus relativeto=\"%s\"" % (which, str(key), g1.relativeto, g2.relativeto)
81 
82  if abs(g1.x - g2.x) > epsilon or abs(g1.y - g2.y) > epsilon or abs(g1.z - g2.z) > epsilon:
83  print "%s %s position difference: (%g, %g, %g) - (%g, %g, %g) = (%g, %g, %g)" % \
84  (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)
85 
86  if "phix" in g1.__dict__:
87  g1type = "phi"
88  g1a, g1b, g1c = g1.phix, g1.phiy, g1.phiz
89  g1rot = rotFromPhi(g1)
90  else:
91  g1type = "euler"
92  g1a, g1b, g1c = g1.alpha, g1.beta, g1.gamma
93  g1rot = rotFromEuler(g1)
94 
95  if "phix" in g2.__dict__:
96  g2type = "phi"
97  g2a, g2b, g2c = g2.phix, g2.phiy, g2.phiz
98  g2rot = rotFromPhi(g2)
99  else:
100  g2type = "euler"
101  g2a, g2b, g2c = g2.alpha, g2.beta, g2.gamma
102  g2rot = rotFromEuler(g2)
103 
104  diff = matrixmult(g1rot, transpose(g2rot))
105  if abs(diff[0][0] - 1.) > sqrtepsilon or abs(diff[1][1] - 1.) > sqrtepsilon or abs(diff[2][2] - 1.) > sqrtepsilon or \
106  abs(diff[0][1]) > epsilon or abs(diff[0][2]) > epsilon or abs(diff[1][2]) > epsilon:
107  print "%s %s rotation difference: %s(%g, %g, %g) - %s(%g, %g, %g) = %s" % \
108  (which, str(key), g1type, g1a, g1b, g1c, g2type, g2a, g2b, g2c, str(diff))
109 
110 loopover("DT")
111 loopover("CSC")
112 
113 
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
T sqrt(T t)
Definition: SSEVec.h:18
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
Abs< T >::type abs(const T &t)
Definition: Abs.h:22