CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_7_hltpatch1/src/Alignment/MuonAlignment/python/makeMuonMisalignmentScenario.py

Go to the documentation of this file.
00001 from optparse import OptionParser
00002 from random import gauss
00003 from math import sqrt
00004 import os
00005 execfile("Alignment/MuonAlignment/data/idealTransformation.py")
00006 
00007 ### Get variances and covariances from the commandline
00008 
00009 parser = OptionParser(usage="Usage: python %prog outputName [options] (default is unit matrix times 1e-15)")
00010 
00011 parser.add_option("--xx", dest="xx", help="variance of x (cm*cm)", default="1e-15")
00012 parser.add_option("--xy", dest="xy", help="covariance of x and y (cm*cm)", default="0")
00013 parser.add_option("--xz", dest="xz", help="covariance of x and z (cm*cm)", default="0")
00014 parser.add_option("--xphix", dest="xphix", help="covariance of x and phix (cm*rad)", default="0")
00015 parser.add_option("--xphiy", dest="xphiy", help="covariance of x and phiy (cm*rad)", default="0")
00016 parser.add_option("--xphiz", dest="xphiz", help="covariance of x and phiz (cm*rad)", default="0")
00017 
00018 parser.add_option("--yy", dest="yy", help="variance of y (cm*cm)", default="1e-15")
00019 parser.add_option("--yz", dest="yz", help="covariance of y and z (cm*cm)", default="0")
00020 parser.add_option("--yphix", dest="yphix", help="covariance of y and phix (cm*rad)", default="0")
00021 parser.add_option("--yphiy", dest="yphiy", help="covariance of y and phiy (cm*rad)", default="0")
00022 parser.add_option("--yphiz", dest="yphiz", help="covariance of y and phiz (cm*rad)", default="0")
00023 
00024 parser.add_option("--zz", dest="zz", help="variance of z (cm*cm)", default="1e-15")
00025 parser.add_option("--zphix", dest="zphix", help="covariance of z and phix (cm*rad)", default="0")
00026 parser.add_option("--zphiy", dest="zphiy", help="covariance of z and phiy (cm*rad)", default="0")
00027 parser.add_option("--zphiz", dest="zphiz", help="covariance of z and phiz (cm*rad)", default="0")
00028 
00029 parser.add_option("--phixphix", dest="phixphix", help="variance of phix (rad*rad)", default="1e-15")
00030 parser.add_option("--phixphiy", dest="phixphiy", help="covariance of phix and phiy (rad*rad)", default="0")
00031 parser.add_option("--phixphiz", dest="phixphiz", help="covariance of phix and phiz (rad*rad)", default="0")
00032 
00033 parser.add_option("--phiyphiy", dest="phiyphiy", help="variance of phiy (rad*rad)", default="1e-15")
00034 parser.add_option("--phiyphiz", dest="phiyphiz", help="covariance of phiy and phiz (rad*rad)", default="0")
00035 
00036 parser.add_option("--phizphiz", dest="phizphiz", help="variance of phiz (rad*rad)", default="1e-15")
00037 
00038 parser.add_option("-f", dest="force", help="force overwrite of output files", action="store_true")
00039 
00040 options, args = parser.parse_args()
00041 if args is None or len(args) != 1:
00042     parser.print_help()
00043     exit(-1)
00044 outputName = args[0]
00045 
00046 if not options.force:
00047     if os.path.exists(outputName + ".xml"):
00048         raise Exception, (outputName + ".xml exists!")
00049     if os.path.exists(outputName + "_convert_cfg.py"):
00050         raise Exception, (outputName + "_convert_cfg.py exists!")
00051     if os.path.exists(outputName + ".db"):
00052         raise Exception, (outputName + ".db exists!")
00053     if os.path.exists(outputName + "_correlations.txt"):
00054         raise Exception, (outputName + "_correlations.txt exists!")
00055 
00056 components = "xx", "xy", "xz", "xphix", "xphiy", "xphiz", "yy", "yz", "yphix", "yphiy", "yphiz", "zz", "zphix", "zphiy", "zphiz", "phixphix", "phixphiy", "phixphiz", "phiyphiy", "phiyphiz", "phizphiz"
00057 for component in components:
00058     exec("%s = float(options.%s)" % (component, component))
00059 
00060 ### Print out user's choices as diagnostics
00061 
00062 print "Spread in each parameter: x %g mm" % (sqrt(xx)*10.)
00063 print "                          y %g mm" % (sqrt(yy)*10.)
00064 print "                          z %g mm" % (sqrt(zz)*10.)
00065 print "                          phix %g mrad" % (sqrt(phixphix)*1000.)
00066 print "                          phiy %g mrad" % (sqrt(phiyphiy)*1000.)
00067 print "                          phiz %g mrad" % (sqrt(phizphiz)*1000.)
00068 print
00069 
00070 print "Covariance matrix (x, y, z, phix, phiy, phiz):"
00071 print "%11.8f %11.8f %11.8f %11.8f %11.8f %11.8f" % (xx, xy, xz, xphix, xphiy, xphiz)
00072 print "%11.8f %11.8f %11.8f %11.8f %11.8f %11.8f" % (xy, yy, yz, yphix, yphiy, yphiz)
00073 print "%11.8f %11.8f %11.8f %11.8f %11.8f %11.8f" % (xz, yz, zz, zphix, zphiy, zphiz)
00074 print "%11.8f %11.8f %11.8f %11.8f %11.8f %11.8f" % (xphix, yphix, zphix, phixphix, phixphiy, phixphiz)
00075 print "%11.8f %11.8f %11.8f %11.8f %11.8f %11.8f" % (xphiy, yphiy, zphiy, phixphiy, phiyphiy, phiyphiz)
00076 print "%11.8f %11.8f %11.8f %11.8f %11.8f %11.8f" % (xphiz, yphiz, zphiz, phixphiz, phiyphiz, phizphiz)
00077 print
00078 
00079 print "Correlation (x, y, z, phix, phiy, phiz):"
00080 print "%11.8f %11.8f %11.8f %11.8f %11.8f %11.8f" % (xx/sqrt(xx)/sqrt(xx), xy/sqrt(xx)/sqrt(yy), xz/sqrt(xx)/sqrt(zz), xphix/sqrt(xx)/sqrt(phixphix), xphiy/sqrt(xx)/sqrt(phiyphiy), xphiz/sqrt(xx)/sqrt(phizphiz))
00081 print "%11.8f %11.8f %11.8f %11.8f %11.8f %11.8f" % (xy/sqrt(yy)/sqrt(xx), yy/sqrt(yy)/sqrt(yy), yz/sqrt(yy)/sqrt(zz), yphix/sqrt(yy)/sqrt(phixphix), yphiy/sqrt(yy)/sqrt(phiyphiy), yphiz/sqrt(yy)/sqrt(phizphiz))
00082 print "%11.8f %11.8f %11.8f %11.8f %11.8f %11.8f" % (xz/sqrt(zz)/sqrt(xx), yz/sqrt(zz)/sqrt(yy), zz/sqrt(zz)/sqrt(zz), zphix/sqrt(zz)/sqrt(phixphix), zphiy/sqrt(zz)/sqrt(phiyphiy), zphiz/sqrt(zz)/sqrt(phizphiz))
00083 print "%11.8f %11.8f %11.8f %11.8f %11.8f %11.8f" % (xphix/sqrt(phixphix)/sqrt(xx), yphix/sqrt(phixphix)/sqrt(yy), zphix/sqrt(phixphix)/sqrt(zz), phixphix/sqrt(phixphix)/sqrt(phixphix), phixphiy/sqrt(phixphix)/sqrt(phiyphiy), phixphiz/sqrt(phixphix)/sqrt(phizphiz))
00084 print "%11.8f %11.8f %11.8f %11.8f %11.8f %11.8f" % (xphiy/sqrt(phiyphiy)/sqrt(xx), yphiy/sqrt(phiyphiy)/sqrt(yy), zphiy/sqrt(phiyphiy)/sqrt(zz), phixphiy/sqrt(phiyphiy)/sqrt(phixphix), phiyphiy/sqrt(phiyphiy)/sqrt(phiyphiy), phiyphiz/sqrt(phiyphiy)/sqrt(phizphiz))
00085 print "%11.8f %11.8f %11.8f %11.8f %11.8f %11.8f" % (xphiz/sqrt(phizphiz)/sqrt(xx), yphiz/sqrt(phizphiz)/sqrt(yy), zphiz/sqrt(phizphiz)/sqrt(zz), phixphiz/sqrt(phizphiz)/sqrt(phixphix), phiyphiz/sqrt(phizphiz)/sqrt(phiyphiy), phizphiz/sqrt(phizphiz)/sqrt(phizphiz))
00086 print
00087 
00088 for correlation_coefficient in [abs(xy/sqrt(xx)/sqrt(yy)), abs(xz/sqrt(xx)/sqrt(zz)), abs(xphix/sqrt(xx)/sqrt(phixphix)), abs(xphiy/sqrt(xx)/sqrt(phiyphiy)), abs(xphiz/sqrt(xx)/sqrt(phizphiz)), \
00089                                 abs(yz/sqrt(yy)/sqrt(zz)), abs(yphix/sqrt(yy)/sqrt(phixphix)), abs(yphiy/sqrt(yy)/sqrt(phiyphiy)), abs(yphiz/sqrt(yy)/sqrt(phizphiz)),
00090                                 abs(zphix/sqrt(zz)/sqrt(phixphix)), abs(zphiy/sqrt(zz)/sqrt(phiyphiy)), abs(zphiz/sqrt(zz)/sqrt(phizphiz)),
00091                                 abs(phixphiy/sqrt(phixphix)/sqrt(phiyphiy)), abs(phixphiz/sqrt(phixphix)/sqrt(phizphiz)),
00092                                 abs(phiyphiz/sqrt(phiyphiy)/sqrt(phizphiz))]:
00093     if correlation_coefficient > 1.:
00094         raise Exception, "Correlations must not be larger than one!"
00095 
00096 ### Some useful mathematical transformations (why don't we have access to numpy?)
00097 
00098 def mmult(a, b):
00099     """Matrix multiplication: mmult([[11, 12], [21, 22]], [[-1, 0], [0, 1]]) returns [[-11, 12], [-21, 22]]"""
00100     return [[sum([i*j for i, j in zip(row, col)]) for col in zip(*b)] for row in a]
00101 
00102 def mvdot(m, v):
00103     """Applies matrix m to vector v: mvdot([[-1, 0], [0, 1]], [12, 55]) returns [-12, 55]"""
00104     return [i[0] for i in mmult(m, [[vi] for vi in v])]
00105 
00106 def mtrans(a):
00107     """Matrix transposition: mtrans([[11, 12], [21, 22]]) returns [[11, 21], [12, 22]]"""
00108     return [[a[j][i] for j in range(len(a[i]))] for i in range(len(a))]
00109 
00110 def cholesky(A):
00111     """Cholesky decomposition of the correlation matrix to properly normalize the transformed random deviates"""
00112 
00113     # A = L * D * L^T = (L * D^0.5) * (L * D^0.5)^T where we want (L * D^0.5), the "square root" of A
00114     # find L and D from A using recurrence relations
00115     L = {}
00116     D = {}
00117     for j in range(len(A[0])):
00118         D[j] = A[j][j] - sum([L[j,k]**2 * D[k] for k in range(j)])
00119         for i in range(len(A)):
00120             if i > j:
00121                 L[i,j] = (A[i][j] - sum([L[i,k] * L[j,k] * D[k] for k in range(j)])) / D[j]
00122 
00123     L = [[    1.,     0.,     0.,     0.,     0., 0.],
00124          [L[1,0],     1.,     0.,     0.,     0., 0.],
00125          [L[2,0], L[2,1],     1.,     0.,     0., 0.],
00126          [L[3,0], L[3,1], L[3,2],     1.,     0., 0.],
00127          [L[4,0], L[4,1], L[4,2], L[4,1],     1., 0.],
00128          [L[5,0], L[5,1], L[5,2], L[5,1], L[5,0], 1.]]
00129 
00130     Dsqrt = [[sqrt(D[0]),         0.,          0.,         0.,         0.,         0.],
00131              [        0., sqrt(D[1]),          0.,         0.,         0.,         0.],
00132              [        0.,         0., sqrt(D[2]),          0.,         0.,         0.],
00133              [        0.,         0.,          0., sqrt(D[3]),         0.,         0.],
00134              [        0.,         0.,          0.,         0., sqrt(D[4]),         0.],
00135              [        0.,         0.,          0.,         0.,         0., sqrt(D[5])]]
00136 
00137     return mmult(L, Dsqrt)
00138 
00139 matrix = [[   xx,    xy,    xz,    xphix,    xphiy,    xphiz],
00140           [   xy,    yy,    yz,    yphix,    yphiy,    yphiz],
00141           [   xz,    yz,    zz,    zphix,    zphiy,    zphiz],
00142           [xphix, yphix, zphix, phixphix, phixphiy, phixphiz],
00143           [xphiy, yphiy, zphiy, phixphiy, phiyphiy, phiyphiz],
00144           [xphiz, yphiz, zphiz, phixphiz, phiyphiz, phizphiz]]
00145 
00146 chomat = cholesky(matrix)
00147 
00148 ### Generate correlated random misalignments for all chambers
00149 
00150 def random6dof():
00151     randomunit = [gauss(0., 1.), gauss(0., 1.), gauss(0., 1.), gauss(0., 1.), gauss(0., 1.), gauss(0., 1.)]
00152     return mvdot(chomat, randomunit)
00153 
00154 misal = {}
00155 
00156 for wheel in -2, -1, 0, 1, 2:
00157     for station in 1, 2, 3, 4:
00158         for sector in range(1, 14+1):
00159             if station != 4 and sector > 12: continue
00160 
00161             misal["DT", wheel, station, 0, sector] = random6dof()
00162 
00163 for endcap in 1, 2:
00164     for station in 1, 2, 3, 4:
00165         for ring in 1, 2, 3:
00166             if station > 1 and ring == 3: continue
00167             for sector in range(1, 36+1):
00168                 if station > 1 and ring == 1 and sector > 18: continue
00169 
00170                 misal["CSC", endcap, station, ring, sector] = random6dof()
00171 
00172 ### More diagnostics
00173 
00174 sum_x = 0.
00175 sum_y = 0.
00176 sum_z = 0.
00177 sum_phix = 0.
00178 sum_phiy = 0.
00179 sum_phiz = 0.
00180 
00181 sum_xx = 0.
00182 sum_xy = 0.
00183 sum_xz = 0.
00184 sum_xphix = 0.
00185 sum_xphiy = 0.
00186 sum_xphiz = 0.
00187 sum_yy = 0.
00188 sum_yz = 0.
00189 sum_yphix = 0.
00190 sum_yphiy = 0.
00191 sum_yphiz = 0.
00192 sum_zz = 0.
00193 sum_zphix = 0.
00194 sum_zphiy = 0.
00195 sum_zphiz = 0.
00196 sum_phixphix = 0.
00197 sum_phixphiy = 0.
00198 sum_phixphiz = 0.
00199 sum_phiyphiy = 0.
00200 sum_phiyphiz = 0.
00201 sum_phizphiz = 0.
00202 
00203 for xi, yi, zi, phixi, phiyi, phizi in misal.values():
00204     sum_x += xi
00205     sum_y += yi
00206     sum_z += zi
00207     sum_phix += phixi
00208     sum_phiy += phiyi
00209     sum_phiz += phizi
00210     
00211     sum_xx += xi*xi
00212     sum_xy += xi*yi
00213     sum_xz += xi*zi
00214     sum_xphix += xi*phixi
00215     sum_xphiy += xi*phiyi
00216     sum_xphiz += xi*phizi
00217     sum_yy += yi*yi
00218     sum_yz += yi*zi
00219     sum_yphix += yi*phixi
00220     sum_yphiy += yi*phiyi
00221     sum_yphiz += yi*phizi
00222     sum_zz += zi*zi
00223     sum_zphix += zi*phixi
00224     sum_zphiy += zi*phiyi
00225     sum_zphiz += zi*phizi
00226     sum_phixphix += phixi*phixi
00227     sum_phixphiy += phixi*phiyi
00228     sum_phixphiz += phixi*phizi
00229     sum_phiyphiy += phiyi*phiyi
00230     sum_phiyphiz += phiyi*phizi
00231     sum_phizphiz += phizi*phizi
00232 
00233 ave_x = sum_x/float(len(misal))
00234 ave_y = sum_y/float(len(misal))
00235 ave_z = sum_z/float(len(misal))
00236 ave_phix = sum_phix/float(len(misal))
00237 ave_phiy = sum_phiy/float(len(misal))
00238 ave_phiz = sum_phiz/float(len(misal))
00239 
00240 ave_xx = sum_xx/float(len(misal))
00241 ave_xy = sum_xy/float(len(misal))
00242 ave_xz = sum_xz/float(len(misal))
00243 ave_xphix = sum_xphix/float(len(misal))
00244 ave_xphiy = sum_xphiy/float(len(misal))
00245 ave_xphiz = sum_xphiz/float(len(misal))
00246 ave_yy = sum_yy/float(len(misal))
00247 ave_yz = sum_yz/float(len(misal))
00248 ave_yphix = sum_yphix/float(len(misal))
00249 ave_yphiy = sum_yphiy/float(len(misal))
00250 ave_yphiz = sum_yphiz/float(len(misal))
00251 ave_zz = sum_zz/float(len(misal))
00252 ave_zphix = sum_zphix/float(len(misal))
00253 ave_zphiy = sum_zphiy/float(len(misal))
00254 ave_zphiz = sum_zphiz/float(len(misal))
00255 ave_phixphix = sum_phixphix/float(len(misal))
00256 ave_phixphiy = sum_phixphiy/float(len(misal))
00257 ave_phixphiz = sum_phixphiz/float(len(misal))
00258 ave_phiyphiy = sum_phiyphiy/float(len(misal))
00259 ave_phiyphiz = sum_phiyphiz/float(len(misal))
00260 ave_phizphiz = sum_phizphiz/float(len(misal))
00261 
00262 print "Estimated covariance matrix from %d chambers (x, y, z, phix, phiy, phiz):" % len(misal)
00263 print "%11.8f %11.8f %11.8f %11.8f %11.8f %11.8f" % (ave_xx, ave_xy, ave_xz, ave_xphix, ave_xphiy, ave_xphiz)
00264 print "%11.8f %11.8f %11.8f %11.8f %11.8f %11.8f" % (ave_xy, ave_yy, ave_yz, ave_yphix, ave_yphiy, ave_yphiz)
00265 print "%11.8f %11.8f %11.8f %11.8f %11.8f %11.8f" % (ave_xz, ave_yz, ave_zz, ave_zphix, ave_zphiy, ave_zphiz)
00266 print "%11.8f %11.8f %11.8f %11.8f %11.8f %11.8f" % (ave_xphix, ave_yphix, ave_zphix, ave_phixphix, ave_phixphiy, ave_phixphiz)
00267 print "%11.8f %11.8f %11.8f %11.8f %11.8f %11.8f" % (ave_xphiy, ave_yphiy, ave_zphiy, ave_phixphiy, ave_phiyphiy, ave_phiyphiz)
00268 print "%11.8f %11.8f %11.8f %11.8f %11.8f %11.8f" % (ave_xphiz, ave_yphiz, ave_zphiz, ave_phixphiz, ave_phiyphiz, ave_phizphiz)
00269 print
00270 
00271 print "Estimated correlation from %d chambers (x, y, z, phix, phiy, phiz):" % len(misal)
00272 print "%11.8f %11.8f %11.8f %11.8f %11.8f %11.8f" % (ave_xx/sqrt(ave_xx)/sqrt(ave_xx), ave_xy/sqrt(ave_xx)/sqrt(ave_yy), ave_xz/sqrt(ave_xx)/sqrt(ave_zz), ave_xphix/sqrt(ave_xx)/sqrt(ave_phixphix), ave_xphiy/sqrt(ave_xx)/sqrt(ave_phiyphiy), ave_xphiz/sqrt(ave_xx)/sqrt(ave_phizphiz))
00273 print "%11.8f %11.8f %11.8f %11.8f %11.8f %11.8f" % (ave_xy/sqrt(ave_yy)/sqrt(ave_xx), ave_yy/sqrt(ave_yy)/sqrt(ave_yy), ave_yz/sqrt(ave_yy)/sqrt(ave_zz), ave_yphix/sqrt(ave_yy)/sqrt(ave_phixphix), ave_yphiy/sqrt(ave_yy)/sqrt(ave_phiyphiy), ave_yphiz/sqrt(ave_yy)/sqrt(ave_phizphiz))
00274 print "%11.8f %11.8f %11.8f %11.8f %11.8f %11.8f" % (ave_xz/sqrt(ave_zz)/sqrt(ave_xx), ave_yz/sqrt(ave_zz)/sqrt(ave_yy), ave_zz/sqrt(ave_zz)/sqrt(ave_zz), ave_zphix/sqrt(ave_zz)/sqrt(ave_phixphix), ave_zphiy/sqrt(ave_zz)/sqrt(ave_phiyphiy), ave_zphiz/sqrt(ave_zz)/sqrt(ave_phizphiz))
00275 print "%11.8f %11.8f %11.8f %11.8f %11.8f %11.8f" % (ave_xphix/sqrt(ave_phixphix)/sqrt(ave_xx), ave_yphix/sqrt(ave_phixphix)/sqrt(ave_yy), ave_zphix/sqrt(ave_phixphix)/sqrt(ave_zz), ave_phixphix/sqrt(ave_phixphix)/sqrt(ave_phixphix), ave_phixphiy/sqrt(ave_phixphix)/sqrt(ave_phiyphiy), ave_phixphiz/sqrt(ave_phixphix)/sqrt(ave_phizphiz))
00276 print "%11.8f %11.8f %11.8f %11.8f %11.8f %11.8f" % (ave_xphiy/sqrt(ave_phiyphiy)/sqrt(ave_xx), ave_yphiy/sqrt(ave_phiyphiy)/sqrt(ave_yy), ave_zphiy/sqrt(ave_phiyphiy)/sqrt(ave_zz), ave_phixphiy/sqrt(ave_phiyphiy)/sqrt(ave_phixphix), ave_phiyphiy/sqrt(ave_phiyphiy)/sqrt(ave_phiyphiy), ave_phiyphiz/sqrt(ave_phiyphiy)/sqrt(ave_phizphiz))
00277 print "%11.8f %11.8f %11.8f %11.8f %11.8f %11.8f" % (ave_xphiz/sqrt(ave_phizphiz)/sqrt(ave_xx), ave_yphiz/sqrt(ave_phizphiz)/sqrt(ave_yy), ave_zphiz/sqrt(ave_phizphiz)/sqrt(ave_zz), ave_phixphiz/sqrt(ave_phizphiz)/sqrt(ave_phixphix), ave_phiyphiz/sqrt(ave_phizphiz)/sqrt(ave_phiyphiy), ave_phizphiz/sqrt(ave_phizphiz)/sqrt(ave_phizphiz))
00278 print
00279 
00280 ### Delete all three files at once to make sure the user never sees
00281 ### stale data (e.g. from a stopped process due to failed conversion)
00282 
00283 if os.path.exists(outputName + ".xml"):
00284     os.unlink(outputName + ".xml")
00285 if os.path.exists(outputName + "_convert_cfg.py"):
00286     os.unlink(outputName + "_convert_cfg.py")
00287 if os.path.exists(outputName + ".db"):
00288     os.unlink(outputName + ".db")
00289 if os.path.exists(outputName + "_correlations.txt"):
00290     os.unlink(outputName + "_correlations.txt")
00291 
00292 ### Print out the list of correlations
00293 
00294 txtfile = file(outputName + "_correlations.txt", "w")
00295 for wheel in -2, -1, 0, 1, 2:
00296     for station in 1, 2, 3, 4:
00297         for sector in range(1, 14+1):
00298             if station != 4 and sector > 12: continue
00299             txtfile.write("DT %(wheel)d %(station)d %(sector)d %(xx)g %(xy)g %(xz)g %(xphix)g %(xphiy)g %(xphiz)g %(yy)g %(yz)g %(yphix)g %(yphiy)g %(yphiz)g %(zz)g %(zphix)g %(zphiy)g %(zphiz)g %(phixphix)g %(phixphiy)g %(phixphiz)g %(phiyphiy)g %(phiyphiz)g %(phizphiz)g\n" % vars())
00300 
00301 for endcap in 1, 2:
00302     for station in 1, 2, 3, 4:
00303         for ring in 1, 2, 3:
00304             if station > 1 and ring == 3: continue
00305             for sector in range(1, 36+1):
00306                 if station > 1 and ring == 1 and sector > 18: continue
00307                 txtfile.write("CSC %(endcap)d %(station)d %(ring)d %(sector)d %(xx)g %(xy)g %(xz)g %(xphix)g %(xphiy)g %(xphiz)g %(yy)g %(yz)g %(yphix)g %(yphiy)g %(yphiz)g %(zz)g %(zphix)g %(zphiy)g %(zphiz)g %(phixphix)g %(phixphiy)g %(phixphiz)g %(phiyphiy)g %(phiyphiz)g %(phizphiz)g\n" % vars())
00308 
00309 ### Make an XML representation of the misalignment
00310 
00311 xmlfile = file(outputName + ".xml", "w")
00312 xmlfile.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
00313 xmlfile.write("<?xml-stylesheet type=\"text/xml\" href=\"MuonAlignment.xsl\"?>\n")
00314 xmlfile.write("<MuonAlignment>\n\n")
00315 
00316 for (system, whendcap, station, ring, sector), (xi, yi, zi, phixi, phiyi, phizi) in misal.items():
00317     if system == "DT": wheel = whendcap
00318     if system == "CSC": endcap = whendcap
00319 
00320     rot = rotation[system, whendcap, station, ring, sector]
00321     localape = [[xx, xy, xz], [xy, yy, yz], [xz, yz, zz]]
00322     globalape = mmult(rot, mmult(localape, mtrans(rot)))
00323     globalxx = globalape[0][0]
00324     globalxy = globalape[0][1]
00325     globalxz = globalape[0][2]
00326     globalyy = globalape[1][1]
00327     globalyz = globalape[1][2]
00328     globalzz = globalape[2][2]
00329 
00330     xmlfile.write("<operation>\n")
00331 
00332     if system == "DT":
00333         xmlfile.write("    <DTChamber wheel=\"%(wheel)d\" station=\"%(station)d\" sector=\"%(sector)d\" />\n" % vars())
00334     if system == "CSC":
00335         xmlfile.write("    <CSCChamber endcap=\"%(endcap)d\" station=\"%(station)d\" ring=\"%(ring)d\" chamber=\"%(sector)d\" />\n" % vars())
00336 
00337         # ME1/1a is called "ring 4", but it should always get exactly the same alignment constants as the corresponding ME1/1b ("ring 1")
00338         if (station, ring) == (1, 1):
00339             xmlfile.write("    <CSCChamber endcap=\"%(endcap)d\" station=\"%(station)d\" ring=\"4\" chamber=\"%(sector)d\" />\n" % vars())
00340 
00341     xmlfile.write("    <setposition relativeto=\"ideal\" x=\"%(xi)g\" y=\"%(yi)g\" z=\"%(zi)g\" phix=\"%(phixi)g\" phiy=\"%(phiyi)g\" phiz=\"%(phizi)g\" />\n" % vars())
00342     xmlfile.write("    <setape xx=\"%(globalxx)g\" xy=\"%(globalxy)g\" xz=\"%(globalxz)g\" yy=\"%(globalyy)g\" yz=\"%(globalyz)g\" zz=\"%(globalzz)g\" />\n" % vars())
00343     xmlfile.write("</operation>\n\n")
00344 
00345 xmlfile.write("</MuonAlignment>\n")
00346 xmlfile.close()
00347 
00348 ### Convert it to an SQLite file with CMSSW
00349 
00350 cfgfile = file(outputName + "_convert_cfg.py", "w")
00351 
00352 cfgfile.write("""import FWCore.ParameterSet.Config as cms
00353 
00354 process = cms.Process("CONVERT")
00355 process.source = cms.Source("EmptySource")
00356 process.maxEvents = cms.untracked.PSet(input = cms.untracked.int32(1))
00357 
00358 process.load("Geometry.CMSCommonData.cmsIdealGeometryXML_cfi")
00359 process.load("Geometry.MuonNumbering.muonNumberingInitialization_cfi")
00360 
00361 process.MuonGeometryDBConverter = cms.EDAnalyzer("MuonGeometryDBConverter",
00362                                                  input = cms.string("xml"),
00363                                                  fileName = cms.string("%(outputName)s.xml"),
00364                                                  shiftErr = cms.double(1000.),
00365                                                  angleErr = cms.double(6.28),
00366 
00367                                                  output = cms.string("db")
00368                                                  )
00369 
00370 process.load("CondCore.DBCommon.CondDBSetup_cfi")
00371 process.PoolDBOutputService = cms.Service("PoolDBOutputService",
00372                                           process.CondDBSetup,
00373                                           connect = cms.string("sqlite_file:%(outputName)s.db"),
00374                                           toPut = cms.VPSet(cms.PSet(record = cms.string("DTAlignmentRcd"), tag = cms.string("DTAlignmentRcd")),
00375                                                             cms.PSet(record = cms.string("DTAlignmentErrorRcd"), tag = cms.string("DTAlignmentErrorRcd")),
00376                                                             cms.PSet(record = cms.string("CSCAlignmentRcd"), tag = cms.string("CSCAlignmentRcd")),
00377                                                             cms.PSet(record = cms.string("CSCAlignmentErrorRcd"), tag = cms.string("CSCAlignmentErrorRcd"))))
00378 
00379 process.Path = cms.Path(process.MuonGeometryDBConverter)
00380 """ % vars())
00381 
00382 print "To create an SQLite file for this geometry (%(outputName)s.db), run the following:" % vars()
00383 print
00384 os.system("echo cmsRun %s_convert_cfg.py" % outputName)
00385