CMS 3D CMS Logo

geometryDiffVisualization.py
Go to the documentation of this file.
1 from __future__ import absolute_import
2 import re
3 from math import *
4 from .svgfig import rgb, SVG, pathtoPath, load as load_svg
5 from .geometryXMLparser import *
6 from signConventions import *
7 
8 def dt_colors(wheel, station, sector):
9  return rgb(0.1, 0.9, 0.)
10 
11 def csc_colors(endcap, station, ring, chamber):
12  return rgb(0.1, 0.9, 0.)
13 
14 def draw_station(geom1, geom2, station, filename, length_factor=100., angle_factor=100., colors=dt_colors, template_dir='./'):
15  if station == 4:
16  station_template = load_svg(template_dir + "station4_template.svg")
17  else:
18  station_template = load_svg(template_dir + "station_template.svg")
19 
20  if station == 1: x_scale_factor = 1/6.
21  if station == 2: x_scale_factor = 1/7.
22  if station == 3: x_scale_factor = 1/8.5
23  if station == 4: x_scale_factor = 1/10.
24  y_scale_factor = 1/7.
25 
26  # make a new group to put the moved chambers into
27  new_boxes = SVG("g")
28 
29  # loop over the SVG tree, looking for our chambers (by id)
30  for treeindex, svgitem in station_template:
31  if isinstance(svgitem, SVG) and "id" in svgitem.attr and svgitem["id"][:3] == "MB_":
32  m = re.match("MB_([0-9mpz]+)_([0-9]+)", svgitem["id"])
33  if m is None: raise Exception
34 
35  wheel = m.group(1)
36  if wheel == "m2": wheel = -2
37  elif wheel == "m1": wheel = -1
38  elif wheel == "z": wheel = 0
39  elif wheel == "p1": wheel = 1
40  elif wheel == "p2": wheel = 2
41  sector = int(m.group(2))
42 
43  xdiff = x_scale_factor * length_factor * (geom1.dt[wheel, station, sector].x - geom2.dt[wheel, station, sector].x) * signConventions["DT", wheel, station, sector][0]
44  ydiff = -y_scale_factor * length_factor * (geom1.dt[wheel, station, sector].y - geom2.dt[wheel, station, sector].y) * signConventions["DT", wheel, station, sector][1]
45  phizdiff = -angle_factor * (geom1.dt[wheel, station, sector].phiz - geom2.dt[wheel, station, sector].phiz) * signConventions["DT", wheel, station, sector][2]
46 
47  sx = float(svgitem["x"]) + float(svgitem["width"])/2.
48  sy = float(svgitem["y"]) + float(svgitem["height"])/2.
49 
50  svgitem["transform"] = "translate(%g,%g)" % (sx, sy)
51  svgitem["x"] = -float(svgitem["width"])/2.
52  svgitem["y"] = -float(svgitem["height"])/2.
53 
54  svgitem["style"] = "fill:#e1e1e1;fill-opacity:1;stroke:#000000;stroke-width:1.0;stroke-dasharray:1, 1;stroke-dashoffset:0"
55 
56  newBox = svgitem.clone()
57  newBox["transform"] = "translate(%g,%g) rotate(%g) " % (sx + xdiff, sy + ydiff, phizdiff * 180./pi)
58  newBox["style"] = "fill:%s;fill-opacity:0.5;stroke:#000000;stroke-width:1.0;stroke-opacity:1;stroke-dasharray:none" % colors(wheel, station, sector)
59 
60  new_boxes.append(newBox)
61 
62  for treeindex, svgitem in station_template:
63  if isinstance(svgitem, SVG) and svgitem.t == "g" and "id" in svgitem.attr and svgitem["id"] == "chambers":
64  svgitem.append(new_boxes)
65 
66  elif isinstance(svgitem, SVG) and "id" in svgitem.attr and svgitem["id"] == "stationx":
67  svgitem[0] = "Station %d" % station
68  svgitem[0] += " (length x%g, angle x%g)" % (length_factor, angle_factor)
69 
70  station_template.save(filename)
71 
72 def draw_wheel(geom1, geom2, wheel, filename, length_factor=100., angle_factor=100., colors=dt_colors, template_dir='./'):
73  wheel_template = load_svg(template_dir + "wheel_template.svg")
74 
75  # make a new group to put the moved chambers into
76  new_boxes = SVG("g")
77 
78  # loop over the SVG tree, looking for our chambers (by id)
79  for treeindex, svgitem in wheel_template:
80  if isinstance(svgitem, SVG) and "id" in svgitem.attr and svgitem["id"][:3] == "MB_":
81  m = re.match("MB_([0-9]+)_([0-9]+)", svgitem["id"])
82  if m is None: raise Exception
83 
84  station, sector = int(m.group(1)), int(m.group(2))
85  xdiff = -length_factor * (geom1.dt[wheel, station, sector].x - geom2.dt[wheel, station, sector].x) * signConventions["DT", wheel, station, sector][0]
86  zdiff = length_factor * (geom1.dt[wheel, station, sector].z - geom2.dt[wheel, station, sector].z) * signConventions["DT", wheel, station, sector][2]
87  phiydiff = -angle_factor * (geom1.dt[wheel, station, sector].phiy - geom2.dt[wheel, station, sector].phiy) * signConventions["DT", wheel, station, sector][1]
88 
89  m = re.search("translate\(([0-9\.\-\+eE]+),\s([0-9\.\-\+eE]+)\)\srotate\(([0-9\.\-\+eE]+)\)",svgitem["transform"])
90 
91  tx = float(m.group(1))
92  ty = float(m.group(2))
93  tr = float(m.group(3))
94 
95  newBox = svgitem.clone()
96 
97  svgitem["style"] = "fill:#e1e1e1;fill-opacity:1;stroke:#000000;stroke-width:5.0;stroke-dasharray:1, 1;stroke-dashoffset:0"
98  newBox["style"] = "fill:%s;fill-opacity:0.5;stroke:#000000;stroke-width:5.0;stroke-opacity:1;stroke-dasharray:none" % colors(wheel, station, sector)
99  newBox["id"] = newBox["id"] + "_moved"
100 
101  newBox["transform"] = "translate(%g,%g) rotate(%g)" % (tx - xdiff*cos(tr*pi/180.) + zdiff*sin(tr*pi/180.), ty - xdiff*sin(tr*pi/180.) - zdiff*cos(tr*pi/180.), tr - phiydiff*180./pi)
102 
103  new_boxes.append(newBox)
104 
105  for treeindex, svgitem in wheel_template:
106  if isinstance(svgitem, SVG) and svgitem.t == "g" and "id" in svgitem.attr and svgitem["id"] == "chambers":
107  svgitem.append(new_boxes)
108 
109  elif isinstance(svgitem, SVG) and "id" in svgitem.attr and svgitem["id"] == "wheelx":
110  if wheel == 0: svgitem[0] = "Wheel 0"
111  else: svgitem[0] = "Wheel %+d" % wheel
112  svgitem[0] += " (length x%g, angle x%g)" % (length_factor, angle_factor)
113 
114  wheel_template.save(filename)
115 
116 def draw_disk(geom1, geom2, endcap, station, filename, length_factor=1., angle_factor=100., colors=csc_colors, template_dir='./'):
117  if station == 1:
118  disk_template = load_svg(template_dir + "disk1_template.svg")
119  if station in (2, 3):
120  disk_template = load_svg(template_dir + "disk23_template.svg")
121  if endcap == 1 and station == 4:
122  disk_template = load_svg(template_dir + "diskp4_template.svg")
123  if endcap == 2 and station == 4:
124  disk_template = load_svg(template_dir + "diskm4_template.svg")
125 
126  scale_factor = 0.233
127 
128  new_boxes = SVG("g")
129 
130  for treeindex, svgitem in disk_template:
131  if isinstance(svgitem, SVG) and "id" in svgitem.attr and svgitem["id"] == "fakecenter":
132  fakecenter = pathtoPath(pathtoPath(svgitem).SVG())
133  sumx = 0.
134  sumy = 0.
135  sum1 = 0.
136  for i, di in enumerate(fakecenter.d):
137  if di[0] in ("M", "L"):
138  sumx += di[1]
139  sumy += di[2]
140  sum1 += 1.
141  originx = sumx/sum1
142  originy = sumy/sum1
143 
144  for treeindex, svgitem in disk_template:
145  if isinstance(svgitem, SVG) and "id" in svgitem.attr and svgitem["id"][:3] == "ME_":
146  m = re.match("ME_([0-9]+)_([0-9]+)", svgitem["id"])
147  if m is None: raise Exception
148 
149  ring, chamber = int(m.group(1)), int(m.group(2))
150  xdiff = scale_factor * length_factor * (geom1.csc[endcap, station, ring, chamber].x - geom2.csc[endcap, station, ring, chamber].x) * signConventions["CSC", endcap, station, ring, chamber][0]
151  ydiff = -scale_factor * length_factor * (geom1.csc[endcap, station, ring, chamber].y - geom2.csc[endcap, station, ring, chamber].y) * signConventions["CSC", endcap, station, ring, chamber][1]
152  phizdiff = -angle_factor * (geom1.csc[endcap, station, ring, chamber].phiz - geom2.csc[endcap, station, ring, chamber].phiz) * signConventions["CSC", endcap, station, ring, chamber][2]
153 
154  svgitem["style"] = "fill:#e1e1e1;fill-opacity:1;stroke:#000000;stroke-width:1.0;stroke-dasharray:1, 1;stroke-dashoffset:0"
155 
156  newBox = pathtoPath(svgitem)
157 
158  # Inkscape outputs wrong SVG: paths are filled with movetos, rather than linetos; this fixes that
159  first = True
160  for i, di in enumerate(newBox.d):
161  if not first and di[0] == "m":
162  di = list(di)
163  di[0] = "l"
164  newBox.d[i] = tuple(di)
165  first = False
166 
167  # convert to absolute coordinates
168  newBox = pathtoPath(newBox.SVG())
169 
170  # find the center of the object
171  sumx = 0.
172  sumy = 0.
173  sum1 = 0.
174  for i, di in enumerate(newBox.d):
175  if di[0] == "L":
176  sumx += di[1]
177  sumy += di[2]
178  sum1 += 1.
179  centerx = sumx/sum1
180  centery = sumy/sum1
181 
182  phipos = atan2(centery - originy, centerx - originx) - pi/2.
183  for i, di in enumerate(newBox.d):
184  if di[0] in ("M", "L"):
185  di = list(di)
186  di[1] += cos(phipos)*xdiff - sin(phipos)*ydiff
187  di[2] += sin(phipos)*xdiff + cos(phipos)*ydiff
188  newBox.d[i] = tuple(di)
189 
190  centerx += cos(phipos)*xdiff - sin(phipos)*ydiff
191  centery += sin(phipos)*xdiff + cos(phipos)*ydiff
192 
193  for i, di in enumerate(newBox.d):
194  if di[0] in ("M", "L"):
195  di = list(di)
196  dispx = cos(phizdiff) * (di[1] - centerx) - sin(phizdiff) * (di[2] - centery)
197  dispy = sin(phizdiff) * (di[1] - centerx) + cos(phizdiff) * (di[2] - centery)
198  di[1] = dispx + centerx
199  di[2] = dispy + centery
200  newBox.d[i] = tuple(di)
201 
202  newBox = newBox.SVG()
203  newBox["style"] = "fill:%s;fill-opacity:0.5;stroke:#000000;stroke-width:1.0;stroke-opacity:1;stroke-dasharray:none" % colors(endcap, station, ring, chamber)
204  newBox["id"] = newBox["id"] + "_moved"
205 
206  new_boxes.append(newBox)
207 
208  for treeindex, svgitem in disk_template:
209  if isinstance(svgitem, SVG) and svgitem.t == "g" and "id" in svgitem.attr and svgitem["id"] == "chambers":
210  svgitem.append(new_boxes)
211 
212  elif isinstance(svgitem, SVG) and "id" in svgitem.attr and svgitem["id"] == "diskx":
213  if endcap == 1: svgitem[0] = "Disk %+d" % station
214  else: svgitem[0] = "Disk %+d" % (-station)
215  svgitem[0] += " (length x%g, angle x%g)" % (length_factor, angle_factor)
216 
217  disk_template.save(filename)
def draw_disk(geom1, geom2, endcap, station, filename, length_factor=1., angle_factor=100., colors=csc_colors, template_dir='./')
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
def csc_colors(endcap, station, ring, chamber)
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
def pathtoPath(svg)
Definition: svgfig.py:1007
def draw_wheel(geom1, geom2, wheel, filename, length_factor=100., angle_factor=100., colors=dt_colors, template_dir='./')
Definition: colors.py:1
def dt_colors(wheel, station, sector)
def draw_station(geom1, geom2, station, filename, length_factor=100., angle_factor=100., colors=dt_colors, template_dir='./')
def rgb(r, g, b, maximum=1.)
Definition: svgfig.py:40