Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:45:20

0001 from __future__ import absolute_import
0002 import re
0003 from math import *
0004 from .svgfig import rgb, SVG, pathtoPath, load as load_svg
0005 from .geometryXMLparser import *
0006 from signConventions import *
0007 
0008 def dt_colors(wheel, station, sector):
0009     return rgb(0.1, 0.9, 0.)
0010 
0011 def csc_colors(endcap, station, ring, chamber):
0012     return rgb(0.1, 0.9, 0.)
0013 
0014 def draw_station(geom1, geom2, station, filename, length_factor=100., angle_factor=100., colors=dt_colors, template_dir='./'):
0015     if station == 4: 
0016         station_template = load_svg(template_dir + "station4_template.svg")
0017     else: 
0018         station_template = load_svg(template_dir + "station_template.svg")
0019 
0020     if station == 1: x_scale_factor = 1/6.
0021     if station == 2: x_scale_factor = 1/7.
0022     if station == 3: x_scale_factor = 1/8.5
0023     if station == 4: x_scale_factor = 1/10.
0024     y_scale_factor = 1/7.
0025 
0026     # make a new group to put the moved chambers into
0027     new_boxes = SVG("g")
0028 
0029     # loop over the SVG tree, looking for our chambers (by id)
0030     for treeindex, svgitem in station_template:
0031         if isinstance(svgitem, SVG) and "id" in svgitem.attr and svgitem["id"][:3] == "MB_":
0032             m = re.match("MB_([0-9mpz]+)_([0-9]+)", svgitem["id"])
0033             if m is None: raise Exception
0034 
0035             wheel = m.group(1)
0036             if wheel == "m2": wheel = -2
0037             elif wheel == "m1": wheel = -1
0038             elif wheel == "z": wheel = 0
0039             elif wheel == "p1": wheel = 1
0040             elif wheel == "p2": wheel = 2
0041             sector = int(m.group(2))
0042 
0043             xdiff = x_scale_factor * length_factor * (geom1.dt[wheel, station, sector].x - geom2.dt[wheel, station, sector].x) * signConventions["DT", wheel, station, sector][0]
0044             ydiff = -y_scale_factor * length_factor * (geom1.dt[wheel, station, sector].y - geom2.dt[wheel, station, sector].y) * signConventions["DT", wheel, station, sector][1]
0045             phizdiff = -angle_factor * (geom1.dt[wheel, station, sector].phiz - geom2.dt[wheel, station, sector].phiz) * signConventions["DT", wheel, station, sector][2]
0046 
0047             sx = float(svgitem["x"]) + float(svgitem["width"])/2.
0048             sy = float(svgitem["y"]) + float(svgitem["height"])/2.
0049 
0050             svgitem["transform"] = "translate(%g,%g)" % (sx, sy)
0051             svgitem["x"] = -float(svgitem["width"])/2.
0052             svgitem["y"] = -float(svgitem["height"])/2.
0053 
0054             svgitem["style"] = "fill:#e1e1e1;fill-opacity:1;stroke:#000000;stroke-width:1.0;stroke-dasharray:1, 1;stroke-dashoffset:0"
0055 
0056             newBox = svgitem.clone()
0057             newBox["transform"] = "translate(%g,%g) rotate(%g) " % (sx + xdiff, sy + ydiff, phizdiff * 180./pi)
0058             newBox["style"] = "fill:%s;fill-opacity:0.5;stroke:#000000;stroke-width:1.0;stroke-opacity:1;stroke-dasharray:none" % colors(wheel, station, sector)
0059 
0060             new_boxes.append(newBox)
0061 
0062     for treeindex, svgitem in station_template:
0063         if isinstance(svgitem, SVG) and svgitem.t == "g" and "id" in svgitem.attr and svgitem["id"] == "chambers":
0064             svgitem.append(new_boxes)
0065 
0066         elif isinstance(svgitem, SVG) and "id" in svgitem.attr and svgitem["id"] == "stationx":
0067             svgitem[0] = "Station %d" % station
0068             svgitem[0] += " (length x%g, angle x%g)" % (length_factor, angle_factor)
0069 
0070     station_template.save(filename)
0071 
0072 def draw_wheel(geom1, geom2, wheel, filename, length_factor=100., angle_factor=100., colors=dt_colors, template_dir='./'):
0073     wheel_template = load_svg(template_dir + "wheel_template.svg")
0074 
0075     # make a new group to put the moved chambers into
0076     new_boxes = SVG("g")
0077 
0078     # loop over the SVG tree, looking for our chambers (by id)
0079     for treeindex, svgitem in wheel_template:
0080         if isinstance(svgitem, SVG) and "id" in svgitem.attr and svgitem["id"][:3] == "MB_":
0081             m = re.match("MB_([0-9]+)_([0-9]+)", svgitem["id"])
0082             if m is None: raise Exception
0083 
0084             station, sector = int(m.group(1)), int(m.group(2))
0085             xdiff = -length_factor * (geom1.dt[wheel, station, sector].x - geom2.dt[wheel, station, sector].x) * signConventions["DT", wheel, station, sector][0]
0086             zdiff = length_factor * (geom1.dt[wheel, station, sector].z - geom2.dt[wheel, station, sector].z) * signConventions["DT", wheel, station, sector][2]
0087             phiydiff = -angle_factor * (geom1.dt[wheel, station, sector].phiy - geom2.dt[wheel, station, sector].phiy) * signConventions["DT", wheel, station, sector][1]
0088 
0089             m = re.search("translate\(([0-9\.\-\+eE]+),\s([0-9\.\-\+eE]+)\)\srotate\(([0-9\.\-\+eE]+)\)",svgitem["transform"])
0090 
0091             tx = float(m.group(1))
0092             ty = float(m.group(2))
0093             tr = float(m.group(3))
0094 
0095             newBox = svgitem.clone()
0096 
0097             svgitem["style"] = "fill:#e1e1e1;fill-opacity:1;stroke:#000000;stroke-width:5.0;stroke-dasharray:1, 1;stroke-dashoffset:0"
0098             newBox["style"] = "fill:%s;fill-opacity:0.5;stroke:#000000;stroke-width:5.0;stroke-opacity:1;stroke-dasharray:none" % colors(wheel, station, sector)
0099             newBox["id"] = newBox["id"] + "_moved"
0100 
0101             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) 
0102 
0103             new_boxes.append(newBox)
0104 
0105     for treeindex, svgitem in wheel_template:
0106         if isinstance(svgitem, SVG) and svgitem.t == "g" and "id" in svgitem.attr and svgitem["id"] == "chambers":
0107             svgitem.append(new_boxes)
0108 
0109         elif isinstance(svgitem, SVG) and "id" in svgitem.attr and svgitem["id"] == "wheelx":
0110             if wheel == 0: svgitem[0] = "Wheel 0"
0111             else: svgitem[0] = "Wheel %+d" % wheel
0112             svgitem[0] += " (length x%g, angle x%g)" % (length_factor, angle_factor)
0113 
0114     wheel_template.save(filename)
0115 
0116 def draw_disk(geom1, geom2, endcap, station, filename, length_factor=1., angle_factor=100., colors=csc_colors, template_dir='./'):
0117     if station == 1: 
0118         disk_template = load_svg(template_dir + "disk1_template.svg")
0119     if station in (2, 3): 
0120         disk_template = load_svg(template_dir + "disk23_template.svg")
0121     if endcap == 1 and station == 4: 
0122         disk_template = load_svg(template_dir + "diskp4_template.svg")
0123     if endcap == 2 and station == 4: 
0124         disk_template = load_svg(template_dir + "diskm4_template.svg")
0125 
0126     scale_factor = 0.233
0127 
0128     new_boxes = SVG("g")
0129 
0130     for treeindex, svgitem in disk_template:
0131         if isinstance(svgitem, SVG) and "id" in svgitem.attr and svgitem["id"] == "fakecenter":
0132             fakecenter = pathtoPath(pathtoPath(svgitem).SVG())
0133             sumx = 0.
0134             sumy = 0.
0135             sum1 = 0.
0136             for i, di in enumerate(fakecenter.d):
0137                 if di[0] in ("M", "L"):
0138                     sumx += di[1]
0139                     sumy += di[2]
0140                     sum1 += 1.
0141             originx = sumx/sum1
0142             originy = sumy/sum1
0143 
0144     for treeindex, svgitem in disk_template:
0145         if isinstance(svgitem, SVG) and "id" in svgitem.attr and svgitem["id"][:3] == "ME_":
0146             m = re.match("ME_([0-9]+)_([0-9]+)", svgitem["id"])
0147             if m is None: raise Exception
0148 
0149             ring, chamber = int(m.group(1)), int(m.group(2))
0150             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]
0151             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]
0152             phizdiff = -angle_factor * (geom1.csc[endcap, station, ring, chamber].phiz - geom2.csc[endcap, station, ring, chamber].phiz) * signConventions["CSC", endcap, station, ring, chamber][2]
0153 
0154             svgitem["style"] = "fill:#e1e1e1;fill-opacity:1;stroke:#000000;stroke-width:1.0;stroke-dasharray:1, 1;stroke-dashoffset:0"
0155 
0156             newBox = pathtoPath(svgitem)
0157 
0158             # Inkscape outputs wrong SVG: paths are filled with movetos, rather than linetos; this fixes that
0159             first = True
0160             for i, di in enumerate(newBox.d):
0161                 if not first and di[0] == "m":
0162                     di = list(di)
0163                     di[0] = "l"
0164                     newBox.d[i] = tuple(di)
0165                 first = False
0166 
0167             # convert to absolute coordinates
0168             newBox = pathtoPath(newBox.SVG())
0169 
0170             # find the center of the object
0171             sumx = 0.
0172             sumy = 0.
0173             sum1 = 0.
0174             for i, di in enumerate(newBox.d):
0175                 if di[0] == "L":
0176                     sumx += di[1]
0177                     sumy += di[2]
0178                     sum1 += 1.
0179             centerx = sumx/sum1
0180             centery = sumy/sum1
0181 
0182             phipos = atan2(centery - originy, centerx - originx) - pi/2.
0183             for i, di in enumerate(newBox.d):
0184                 if di[0] in ("M", "L"):
0185                     di = list(di)
0186                     di[1] += cos(phipos)*xdiff - sin(phipos)*ydiff
0187                     di[2] += sin(phipos)*xdiff + cos(phipos)*ydiff
0188                     newBox.d[i] = tuple(di)
0189 
0190             centerx += cos(phipos)*xdiff - sin(phipos)*ydiff
0191             centery += sin(phipos)*xdiff + cos(phipos)*ydiff
0192 
0193             for i, di in enumerate(newBox.d):
0194                 if di[0] in ("M", "L"):
0195                     di = list(di)
0196                     dispx = cos(phizdiff) * (di[1] - centerx) - sin(phizdiff) * (di[2] - centery)
0197                     dispy = sin(phizdiff) * (di[1] - centerx) + cos(phizdiff) * (di[2] - centery)
0198                     di[1] = dispx + centerx
0199                     di[2] = dispy + centery
0200                     newBox.d[i] = tuple(di)
0201 
0202             newBox = newBox.SVG()
0203             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)
0204             newBox["id"] = newBox["id"] + "_moved"
0205 
0206             new_boxes.append(newBox)
0207 
0208     for treeindex, svgitem in disk_template:
0209         if isinstance(svgitem, SVG) and svgitem.t == "g" and "id" in svgitem.attr and svgitem["id"] == "chambers":
0210             svgitem.append(new_boxes)
0211 
0212         elif isinstance(svgitem, SVG) and "id" in svgitem.attr and svgitem["id"] == "diskx":
0213             if endcap == 1: svgitem[0] = "Disk %+d" % station
0214             else: svgitem[0] = "Disk %+d" % (-station)
0215             svgitem[0] += " (length x%g, angle x%g)" % (length_factor, angle_factor)
0216 
0217     disk_template.save(filename)