Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-25 02:29:03

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