00001 import re
00002 from math import *
00003 from svgfig import rgb, SVG, pathtoPath, load as load_svg
00004 from geometryXMLparser import *
00005 from signConventions import *
00006
00007 def dt_colors(wheel, station, sector):
00008 return rgb(0.1, 0.9, 0.)
00009
00010 def csc_colors(endcap, station, ring, chamber):
00011 return rgb(0.1, 0.9, 0.)
00012
00013 def draw_station(geom1, geom2, station, filename, length_factor=100., angle_factor=100., colors=dt_colors):
00014 if station == 4: station_template = load_svg("station4_template.svg")
00015 else: station_template = load_svg("station_template.svg")
00016
00017 if station == 1: x_scale_factor = 1/6.
00018 if station == 2: x_scale_factor = 1/7.
00019 if station == 3: x_scale_factor = 1/8.5
00020 if station == 4: x_scale_factor = 1/10.
00021 y_scale_factor = 1/7.
00022
00023
00024 new_boxes = SVG("g")
00025
00026
00027 for treeindex, svgitem in station_template:
00028 if isinstance(svgitem, SVG) and "id" in svgitem.attr and svgitem["id"][:3] == "MB_":
00029 m = re.match("MB_([0-9mpz]+)_([0-9]+)", svgitem["id"])
00030 if m is None: raise Exception
00031
00032 wheel = m.group(1)
00033 if wheel == "m2": wheel = -2
00034 elif wheel == "m1": wheel = -1
00035 elif wheel == "z": wheel = 0
00036 elif wheel == "p1": wheel = 1
00037 elif wheel == "p2": wheel = 2
00038 sector = int(m.group(2))
00039
00040 xdiff = x_scale_factor * length_factor * (geom1.dt[wheel, station, sector].x - geom2.dt[wheel, station, sector].x) * signConventions["DT", wheel, station, sector][0]
00041 ydiff = -y_scale_factor * length_factor * (geom1.dt[wheel, station, sector].y - geom2.dt[wheel, station, sector].y) * signConventions["DT", wheel, station, sector][1]
00042 phizdiff = -angle_factor * (geom1.dt[wheel, station, sector].phiz - geom2.dt[wheel, station, sector].phiz) * signConventions["DT", wheel, station, sector][2]
00043
00044 sx = float(svgitem["x"]) + float(svgitem["width"])/2.
00045 sy = float(svgitem["y"]) + float(svgitem["height"])/2.
00046
00047 svgitem["transform"] = "translate(%g,%g)" % (sx, sy)
00048 svgitem["x"] = -float(svgitem["width"])/2.
00049 svgitem["y"] = -float(svgitem["height"])/2.
00050
00051 svgitem["style"] = "fill:#e1e1e1;fill-opacity:1;stroke:#000000;stroke-width:1.0;stroke-dasharray:1, 1;stroke-dashoffset:0"
00052
00053 newBox = svgitem.clone()
00054 newBox["transform"] = "translate(%g,%g) rotate(%g) " % (sx + xdiff, sy + ydiff, phizdiff * 180./pi)
00055 newBox["style"] = "fill:%s;fill-opacity:0.5;stroke:#000000;stroke-width:1.0;stroke-opacity:1;stroke-dasharray:none" % colors(wheel, station, sector)
00056
00057 new_boxes.append(newBox)
00058
00059 for treeindex, svgitem in station_template:
00060 if isinstance(svgitem, SVG) and svgitem.t == "g" and "id" in svgitem.attr and svgitem["id"] == "chambers":
00061 svgitem.append(new_boxes)
00062
00063 elif isinstance(svgitem, SVG) and "id" in svgitem.attr and svgitem["id"] == "stationx":
00064 svgitem[0] = "Station %d" % station
00065 svgitem[0] += " (length x%g, angle x%g)" % (length_factor, angle_factor)
00066
00067 station_template.save(filename)
00068
00069 def draw_wheel(geom1, geom2, wheel, filename, length_factor=100., angle_factor=100., colors=dt_colors):
00070 wheel_template = load_svg("wheel_template.svg")
00071
00072
00073 new_boxes = SVG("g")
00074
00075
00076 for treeindex, svgitem in wheel_template:
00077 if isinstance(svgitem, SVG) and "id" in svgitem.attr and svgitem["id"][:3] == "MB_":
00078 m = re.match("MB_([0-9]+)_([0-9]+)", svgitem["id"])
00079 if m is None: raise Exception
00080
00081 station, sector = int(m.group(1)), int(m.group(2))
00082 xdiff = -length_factor * (geom1.dt[wheel, station, sector].x - geom2.dt[wheel, station, sector].x) * signConventions["DT", wheel, station, sector][0]
00083 zdiff = length_factor * (geom1.dt[wheel, station, sector].z - geom2.dt[wheel, station, sector].z) * signConventions["DT", wheel, station, sector][2]
00084 phiydiff = -angle_factor * (geom1.dt[wheel, station, sector].phiy - geom2.dt[wheel, station, sector].phiy) * signConventions["DT", wheel, station, sector][1]
00085
00086 m = re.search("translate\(([0-9\.\-\+eE]+),\s([0-9\.\-\+eE]+)\)\srotate\(([0-9\.\-\+eE]+)\)",svgitem["transform"])
00087
00088 tx = float(m.group(1))
00089 ty = float(m.group(2))
00090 tr = float(m.group(3))
00091
00092 newBox = svgitem.clone()
00093
00094 svgitem["style"] = "fill:#e1e1e1;fill-opacity:1;stroke:#000000;stroke-width:5.0;stroke-dasharray:1, 1;stroke-dashoffset:0"
00095 newBox["style"] = "fill:%s;fill-opacity:0.5;stroke:#000000;stroke-width:5.0;stroke-opacity:1;stroke-dasharray:none" % colors(wheel, station, sector)
00096 newBox["id"] = newBox["id"] + "_moved"
00097
00098 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)
00099
00100 new_boxes.append(newBox)
00101
00102 for treeindex, svgitem in wheel_template:
00103 if isinstance(svgitem, SVG) and svgitem.t == "g" and "id" in svgitem.attr and svgitem["id"] == "chambers":
00104 svgitem.append(new_boxes)
00105
00106 elif isinstance(svgitem, SVG) and "id" in svgitem.attr and svgitem["id"] == "wheelx":
00107 if wheel == 0: svgitem[0] = "Wheel 0"
00108 else: svgitem[0] = "Wheel %+d" % wheel
00109 svgitem[0] += " (length x%g, angle x%g)" % (length_factor, angle_factor)
00110
00111 wheel_template.save(filename)
00112
00113 def draw_disk(geom1, geom2, endcap, station, filename, length_factor=1., angle_factor=100., colors=csc_colors):
00114 if station == 1: disk_template = load_svg("disk1_template.svg")
00115 if station in (2, 3): disk_template = load_svg("disk23_template.svg")
00116 if endcap == 1 and station == 4: disk_template = load_svg("diskp4_template.svg")
00117 if endcap == 2 and station == 4: disk_template = load_svg("diskm4_template.svg")
00118
00119 scale_factor = 0.233
00120
00121 new_boxes = SVG("g")
00122
00123 for treeindex, svgitem in disk_template:
00124 if isinstance(svgitem, SVG) and "id" in svgitem.attr and svgitem["id"] == "fakecenter":
00125 fakecenter = pathtoPath(pathtoPath(svgitem).SVG())
00126 sumx = 0.
00127 sumy = 0.
00128 sum1 = 0.
00129 for i, di in enumerate(fakecenter.d):
00130 if di[0] in ("M", "L"):
00131 sumx += di[1]
00132 sumy += di[2]
00133 sum1 += 1.
00134 originx = sumx/sum1
00135 originy = sumy/sum1
00136
00137 for treeindex, svgitem in disk_template:
00138 if isinstance(svgitem, SVG) and "id" in svgitem.attr and svgitem["id"][:3] == "ME_":
00139 m = re.match("ME_([0-9]+)_([0-9]+)", svgitem["id"])
00140 if m is None: raise Exception
00141
00142 ring, chamber = int(m.group(1)), int(m.group(2))
00143 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]
00144 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]
00145 phizdiff = -angle_factor * (geom1.csc[endcap, station, ring, chamber].phiz - geom2.csc[endcap, station, ring, chamber].phiz) * signConventions["CSC", endcap, station, ring, chamber][2]
00146
00147 svgitem["style"] = "fill:#e1e1e1;fill-opacity:1;stroke:#000000;stroke-width:1.0;stroke-dasharray:1, 1;stroke-dashoffset:0"
00148
00149 newBox = pathtoPath(svgitem)
00150
00151
00152 first = True
00153 for i, di in enumerate(newBox.d):
00154 if not first and di[0] == "m":
00155 di = list(di)
00156 di[0] = "l"
00157 newBox.d[i] = tuple(di)
00158 first = False
00159
00160
00161 newBox = pathtoPath(newBox.SVG())
00162
00163
00164 sumx = 0.
00165 sumy = 0.
00166 sum1 = 0.
00167 for i, di in enumerate(newBox.d):
00168 if di[0] == "L":
00169 sumx += di[1]
00170 sumy += di[2]
00171 sum1 += 1.
00172 centerx = sumx/sum1
00173 centery = sumy/sum1
00174
00175 phipos = atan2(centery - originy, centerx - originx) - pi/2.
00176 for i, di in enumerate(newBox.d):
00177 if di[0] in ("M", "L"):
00178 di = list(di)
00179 di[1] += cos(phipos)*xdiff - sin(phipos)*ydiff
00180 di[2] += sin(phipos)*xdiff + cos(phipos)*ydiff
00181 newBox.d[i] = tuple(di)
00182
00183 centerx += cos(phipos)*xdiff - sin(phipos)*ydiff
00184 centery += sin(phipos)*xdiff + cos(phipos)*ydiff
00185
00186 for i, di in enumerate(newBox.d):
00187 if di[0] in ("M", "L"):
00188 di = list(di)
00189 dispx = cos(phizdiff) * (di[1] - centerx) - sin(phizdiff) * (di[2] - centery)
00190 dispy = sin(phizdiff) * (di[1] - centerx) + cos(phizdiff) * (di[2] - centery)
00191 di[1] = dispx + centerx
00192 di[2] = dispy + centery
00193 newBox.d[i] = tuple(di)
00194
00195 newBox = newBox.SVG()
00196 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)
00197 newBox["id"] = newBox["id"] + "_moved"
00198
00199 new_boxes.append(newBox)
00200
00201 for treeindex, svgitem in disk_template:
00202 if isinstance(svgitem, SVG) and svgitem.t == "g" and "id" in svgitem.attr and svgitem["id"] == "chambers":
00203 svgitem.append(new_boxes)
00204
00205 elif isinstance(svgitem, SVG) and "id" in svgitem.attr and svgitem["id"] == "diskx":
00206 if endcap == 1: svgitem[0] = "Disk %+d" % station
00207 else: svgitem[0] = "Disk %+d" % (-station)
00208 svgitem[0] += " (length x%g, angle x%g)" % (length_factor, angle_factor)
00209
00210 disk_template.save(filename)