CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_1/src/Alignment/MuonAlignment/python/geometryDiffVisualization.py

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