1 from optparse
import OptionParser
2 from random
import gauss
5 execfile(
"Alignment/MuonAlignment/data/idealTransformation.py")
9 parser = OptionParser(usage=
"Usage: python %prog outputName [options] (default is unit matrix times 1e-15)")
11 parser.add_option(
"--xx", dest=
"xx", help=
"variance of x (cm*cm)", default=
"1e-15")
12 parser.add_option(
"--xy", dest=
"xy", help=
"covariance of x and y (cm*cm)", default=
"0")
13 parser.add_option(
"--xz", dest=
"xz", help=
"covariance of x and z (cm*cm)", default=
"0")
14 parser.add_option(
"--xphix", dest=
"xphix", help=
"covariance of x and phix (cm*rad)", default=
"0")
15 parser.add_option(
"--xphiy", dest=
"xphiy", help=
"covariance of x and phiy (cm*rad)", default=
"0")
16 parser.add_option(
"--xphiz", dest=
"xphiz", help=
"covariance of x and phiz (cm*rad)", default=
"0")
18 parser.add_option(
"--yy", dest=
"yy", help=
"variance of y (cm*cm)", default=
"1e-15")
19 parser.add_option(
"--yz", dest=
"yz", help=
"covariance of y and z (cm*cm)", default=
"0")
20 parser.add_option(
"--yphix", dest=
"yphix", help=
"covariance of y and phix (cm*rad)", default=
"0")
21 parser.add_option(
"--yphiy", dest=
"yphiy", help=
"covariance of y and phiy (cm*rad)", default=
"0")
22 parser.add_option(
"--yphiz", dest=
"yphiz", help=
"covariance of y and phiz (cm*rad)", default=
"0")
24 parser.add_option(
"--zz", dest=
"zz", help=
"variance of z (cm*cm)", default=
"1e-15")
25 parser.add_option(
"--zphix", dest=
"zphix", help=
"covariance of z and phix (cm*rad)", default=
"0")
26 parser.add_option(
"--zphiy", dest=
"zphiy", help=
"covariance of z and phiy (cm*rad)", default=
"0")
27 parser.add_option(
"--zphiz", dest=
"zphiz", help=
"covariance of z and phiz (cm*rad)", default=
"0")
29 parser.add_option(
"--phixphix", dest=
"phixphix", help=
"variance of phix (rad*rad)", default=
"1e-15")
30 parser.add_option(
"--phixphiy", dest=
"phixphiy", help=
"covariance of phix and phiy (rad*rad)", default=
"0")
31 parser.add_option(
"--phixphiz", dest=
"phixphiz", help=
"covariance of phix and phiz (rad*rad)", default=
"0")
33 parser.add_option(
"--phiyphiy", dest=
"phiyphiy", help=
"variance of phiy (rad*rad)", default=
"1e-15")
34 parser.add_option(
"--phiyphiz", dest=
"phiyphiz", help=
"covariance of phiy and phiz (rad*rad)", default=
"0")
36 parser.add_option(
"--phizphiz", dest=
"phizphiz", help=
"variance of phiz (rad*rad)", default=
"1e-15")
38 parser.add_option(
"-f", dest=
"force", help=
"force overwrite of output files", action=
"store_true")
40 options, args = parser.parse_args()
41 if args
is None or len(args) != 1:
47 if os.path.exists(outputName +
".xml"):
48 raise Exception, (outputName +
".xml exists!")
49 if os.path.exists(outputName +
"_convert_cfg.py"):
50 raise Exception, (outputName +
"_convert_cfg.py exists!")
51 if os.path.exists(outputName +
".db"):
52 raise Exception, (outputName +
".db exists!")
53 if os.path.exists(outputName +
"_correlations.txt"):
54 raise Exception, (outputName +
"_correlations.txt exists!")
56 components =
"xx",
"xy",
"xz",
"xphix",
"xphiy",
"xphiz",
"yy",
"yz",
"yphix",
"yphiy",
"yphiz",
"zz",
"zphix",
"zphiy",
"zphiz",
"phixphix",
"phixphiy",
"phixphiz",
"phiyphiy",
"phiyphiz",
"phizphiz"
57 for component
in components:
58 exec(
"%s = float(options.%s)" % (component, component))
62 print "Spread in each parameter: x %g mm" % (
sqrt(xx)*10.)
63 print " y %g mm" % (
sqrt(yy)*10.)
64 print " z %g mm" % (
sqrt(zz)*10.)
65 print " phix %g mrad" % (
sqrt(phixphix)*1000.)
66 print " phiy %g mrad" % (
sqrt(phiyphiy)*1000.)
67 print " phiz %g mrad" % (
sqrt(phizphiz)*1000.)
70 print "Covariance matrix (x, y, z, phix, phiy, phiz):"
71 print "%11.8f %11.8f %11.8f %11.8f %11.8f %11.8f" % (xx, xy, xz, xphix, xphiy, xphiz)
72 print "%11.8f %11.8f %11.8f %11.8f %11.8f %11.8f" % (xy, yy, yz, yphix, yphiy, yphiz)
73 print "%11.8f %11.8f %11.8f %11.8f %11.8f %11.8f" % (xz, yz, zz, zphix, zphiy, zphiz)
74 print "%11.8f %11.8f %11.8f %11.8f %11.8f %11.8f" % (xphix, yphix, zphix, phixphix, phixphiy, phixphiz)
75 print "%11.8f %11.8f %11.8f %11.8f %11.8f %11.8f" % (xphiy, yphiy, zphiy, phixphiy, phiyphiy, phiyphiz)
76 print "%11.8f %11.8f %11.8f %11.8f %11.8f %11.8f" % (xphiz, yphiz, zphiz, phixphiz, phiyphiz, phizphiz)
79 print "Correlation (x, y, z, phix, phiy, phiz):"
83 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))
84 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))
85 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))
93 if correlation_coefficient > 1.:
94 raise Exception,
"Correlations must not be larger than one!"
99 """Matrix multiplication: mmult([[11, 12], [21, 22]], [[-1, 0], [0, 1]]) returns [[-11, 12], [-21, 22]]"""
100 return [[sum([i*j
for i, j
in zip(row, col)])
for col
in zip(*b)]
for row
in a]
103 """Applies matrix m to vector v: mvdot([[-1, 0], [0, 1]], [12, 55]) returns [-12, 55]"""
104 return [i[0]
for i
in mmult(m, [[vi]
for vi
in v])]
107 """Matrix transposition: mtrans([[11, 12], [21, 22]]) returns [[11, 21], [12, 22]]"""
108 return [[a[j][i]
for j
in range(len(a[i]))]
for i
in range(len(a))]
111 """Cholesky decomposition of the correlation matrix to properly normalize the transformed random deviates"""
117 for j
in range(len(A[0])):
118 D[j] = A[j][j] - sum([L[j,k]**2 * D[k]
for k
in range(j)])
119 for i
in range(len(A)):
121 L[i,j] = (A[i][j] - sum([L[i,k] * L[j,k] * D[k]
for k
in range(j)])) / D[j]
123 L = [[ 1., 0., 0., 0., 0., 0.],
124 [L[1,0], 1., 0., 0., 0., 0.],
125 [L[2,0], L[2,1], 1., 0., 0., 0.],
126 [L[3,0], L[3,1], L[3,2], 1., 0., 0.],
127 [L[4,0], L[4,1], L[4,2], L[4,1], 1., 0.],
128 [L[5,0], L[5,1], L[5,2], L[5,1], L[5,0], 1.]]
130 Dsqrt = [[
sqrt(D[0]), 0., 0., 0., 0., 0.],
131 [ 0.,
sqrt(D[1]), 0., 0., 0., 0.],
132 [ 0., 0.,
sqrt(D[2]), 0., 0., 0.],
133 [ 0., 0., 0.,
sqrt(D[3]), 0., 0.],
134 [ 0., 0., 0., 0.,
sqrt(D[4]), 0.],
135 [ 0., 0., 0., 0., 0.,
sqrt(D[5])]]
137 return mmult(L, Dsqrt)
139 matrix = [[ xx, xy, xz, xphix, xphiy, xphiz],
140 [ xy, yy, yz, yphix, yphiy, yphiz],
141 [ xz, yz, zz, zphix, zphiy, zphiz],
142 [xphix, yphix, zphix, phixphix, phixphiy, phixphiz],
143 [xphiy, yphiy, zphiy, phixphiy, phiyphiy, phiyphiz],
144 [xphiz, yphiz, zphiz, phixphiz, phiyphiz, phizphiz]]
151 randomunit = [gauss(0., 1.), gauss(0., 1.), gauss(0., 1.), gauss(0., 1.), gauss(0., 1.), gauss(0., 1.)]
152 return mvdot(chomat, randomunit)
156 for wheel
in -2, -1, 0, 1, 2:
157 for station
in 1, 2, 3, 4:
158 for sector
in range(1, 14+1):
159 if station != 4
and sector > 12:
continue
161 misal[
"DT", wheel, station, 0, sector] =
random6dof()
164 for station
in 1, 2, 3, 4:
166 if station > 1
and ring == 3:
continue
167 for sector
in range(1, 36+1):
168 if station > 1
and ring == 1
and sector > 18:
continue
170 misal[
"CSC", endcap, station, ring, sector] =
random6dof()
203 for xi, yi, zi, phixi, phiyi, phizi
in misal.values():
214 sum_xphix += xi*phixi
215 sum_xphiy += xi*phiyi
216 sum_xphiz += xi*phizi
219 sum_yphix += yi*phixi
220 sum_yphiy += yi*phiyi
221 sum_yphiz += yi*phizi
223 sum_zphix += zi*phixi
224 sum_zphiy += zi*phiyi
225 sum_zphiz += zi*phizi
226 sum_phixphix += phixi*phixi
227 sum_phixphiy += phixi*phiyi
228 sum_phixphiz += phixi*phizi
229 sum_phiyphiy += phiyi*phiyi
230 sum_phiyphiz += phiyi*phizi
231 sum_phizphiz += phizi*phizi
233 ave_x = sum_x/float(len(misal))
234 ave_y = sum_y/float(len(misal))
235 ave_z = sum_z/float(len(misal))
236 ave_phix = sum_phix/float(len(misal))
237 ave_phiy = sum_phiy/float(len(misal))
238 ave_phiz = sum_phiz/float(len(misal))
240 ave_xx = sum_xx/float(len(misal))
241 ave_xy = sum_xy/float(len(misal))
242 ave_xz = sum_xz/float(len(misal))
243 ave_xphix = sum_xphix/float(len(misal))
244 ave_xphiy = sum_xphiy/float(len(misal))
245 ave_xphiz = sum_xphiz/float(len(misal))
246 ave_yy = sum_yy/float(len(misal))
247 ave_yz = sum_yz/float(len(misal))
248 ave_yphix = sum_yphix/float(len(misal))
249 ave_yphiy = sum_yphiy/float(len(misal))
250 ave_yphiz = sum_yphiz/float(len(misal))
251 ave_zz = sum_zz/float(len(misal))
252 ave_zphix = sum_zphix/float(len(misal))
253 ave_zphiy = sum_zphiy/float(len(misal))
254 ave_zphiz = sum_zphiz/float(len(misal))
255 ave_phixphix = sum_phixphix/float(len(misal))
256 ave_phixphiy = sum_phixphiy/float(len(misal))
257 ave_phixphiz = sum_phixphiz/float(len(misal))
258 ave_phiyphiy = sum_phiyphiy/float(len(misal))
259 ave_phiyphiz = sum_phiyphiz/float(len(misal))
260 ave_phizphiz = sum_phizphiz/float(len(misal))
262 print "Estimated covariance matrix from %d chambers (x, y, z, phix, phiy, phiz):" % len(misal)
263 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)
264 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)
265 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)
266 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)
267 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)
268 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)
271 print "Estimated correlation from %d chambers (x, y, z, phix, phiy, phiz):" % len(misal)
272 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))
273 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))
274 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))
275 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))
276 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))
277 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))
283 if os.path.exists(outputName +
".xml"):
284 os.unlink(outputName +
".xml")
285 if os.path.exists(outputName +
"_convert_cfg.py"):
286 os.unlink(outputName +
"_convert_cfg.py")
287 if os.path.exists(outputName +
".db"):
288 os.unlink(outputName +
".db")
289 if os.path.exists(outputName +
"_correlations.txt"):
290 os.unlink(outputName +
"_correlations.txt")
294 txtfile =
file(outputName +
"_correlations.txt",
"w")
295 for wheel
in -2, -1, 0, 1, 2:
296 for station
in 1, 2, 3, 4:
297 for sector
in range(1, 14+1):
298 if station != 4
and sector > 12:
continue
299 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())
302 for station
in 1, 2, 3, 4:
304 if station > 1
and ring == 3:
continue
305 for sector
in range(1, 36+1):
306 if station > 1
and ring == 1
and sector > 18:
continue
307 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())
311 xmlfile =
file(outputName +
".xml",
"w")
312 xmlfile.write(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
313 xmlfile.write(
"<?xml-stylesheet type=\"text/xml\" href=\"MuonAlignment.xsl\"?>\n")
314 xmlfile.write(
"<MuonAlignment>\n\n")
316 for (system, whendcap, station, ring, sector), (xi, yi, zi, phixi, phiyi, phizi)
in misal.items():
317 if system ==
"DT": wheel = whendcap
318 if system ==
"CSC": endcap = whendcap
320 rot = rotation[system, whendcap, station, ring, sector]
321 localape = [[xx, xy, xz], [xy, yy, yz], [xz, yz, zz]]
323 globalxx = globalape[0][0]
324 globalxy = globalape[0][1]
325 globalxz = globalape[0][2]
326 globalyy = globalape[1][1]
327 globalyz = globalape[1][2]
328 globalzz = globalape[2][2]
330 xmlfile.write(
"<operation>\n")
333 xmlfile.write(
" <DTChamber wheel=\"%(wheel)d\" station=\"%(station)d\" sector=\"%(sector)d\" />\n" % vars())
335 xmlfile.write(
" <CSCChamber endcap=\"%(endcap)d\" station=\"%(station)d\" ring=\"%(ring)d\" chamber=\"%(sector)d\" />\n" % vars())
338 if (station, ring) == (1, 1):
339 xmlfile.write(
" <CSCChamber endcap=\"%(endcap)d\" station=\"%(station)d\" ring=\"4\" chamber=\"%(sector)d\" />\n" % vars())
341 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())
342 xmlfile.write(
" <setape xx=\"%(globalxx)g\" xy=\"%(globalxy)g\" xz=\"%(globalxz)g\" yy=\"%(globalyy)g\" yz=\"%(globalyz)g\" zz=\"%(globalzz)g\" />\n" % vars())
343 xmlfile.write(
"</operation>\n\n")
345 xmlfile.write(
"</MuonAlignment>\n")
350 cfgfile =
file(outputName +
"_convert_cfg.py",
"w")
352 cfgfile.write(
"""import FWCore.ParameterSet.Config as cms
354 process = cms.Process("CONVERT")
355 process.source = cms.Source("EmptySource")
356 process.maxEvents = cms.untracked.PSet(input = cms.untracked.int32(1))
358 process.load("Geometry.CMSCommonData.cmsIdealGeometryXML_cfi")
359 process.load("Geometry.MuonNumbering.muonNumberingInitialization_cfi")
361 process.MuonGeometryDBConverter = cms.EDAnalyzer("MuonGeometryDBConverter",
362 input = cms.string("xml"),
363 fileName = cms.string("%(outputName)s.xml"),
364 shiftErr = cms.double(1000.),
365 angleErr = cms.double(6.28),
367 output = cms.string("db")
370 process.load("CondCore.DBCommon.CondDBSetup_cfi")
371 process.PoolDBOutputService = cms.Service("PoolDBOutputService",
373 connect = cms.string("sqlite_file:%(outputName)s.db"),
374 toPut = cms.VPSet(cms.PSet(record = cms.string("DTAlignmentRcd"), tag = cms.string("DTAlignmentRcd")),
375 cms.PSet(record = cms.string("DTAlignmentErrorRcd"), tag = cms.string("DTAlignmentErrorRcd")),
376 cms.PSet(record = cms.string("CSCAlignmentRcd"), tag = cms.string("CSCAlignmentRcd")),
377 cms.PSet(record = cms.string("CSCAlignmentErrorRcd"), tag = cms.string("CSCAlignmentErrorRcd"))))
379 process.Path = cms.Path(process.MuonGeometryDBConverter)
382 print "To create an SQLite file for this geometry (%(outputName)s.db), run the following:" % vars()
384 os.system(
"echo cmsRun %s_convert_cfg.py" % outputName)
def mmult
Print out user's choices as diagnostics.
def random6dof
Generate correlated random misalignments for all chambers.
Abs< T >::type abs(const T &t)