00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 import random, os
00027 from math import *
00028
00029
00030 random.seed(123456)
00031
00032
00033 def make_scenario_sqlite():
00034 scenario = define_scenario()
00035 write_xml(scenario, "MCScenario_CRAFT1_22X.xml")
00036 write_conversion_cfg("convert_cfg.py", "MCScenario_CRAFT1_22X.xml", "MCScenario_CRAFT1_22X.db")
00037 cmsRun("convert_cfg.py")
00038 write_check_cfg("check_cfg.py", "MCScenario_CRAFT1_22X.db", "MCScenario_CRAFT1_22X_CHECKME.xml")
00039 cmsRun("check_cfg.py")
00040
00041
00042 def write_conversion_cfg(fileName, xmlFileName, dbFileName):
00043 outfile = file(fileName, "w")
00044 outfile.write("""
00045 from Alignment.MuonAlignment.convertXMLtoSQLite_cfg import *
00046 process.MuonGeometryDBConverter.fileName = "%(xmlFileName)s"
00047 process.PoolDBOutputService.connect = "sqlite_file:%(dbFileName)s"
00048 """ % vars())
00049
00050 def write_check_cfg(fileName, dbFileName, xmlFileName):
00051 outfile = file(fileName, "w")
00052 outfile.write("""
00053 from Alignment.MuonAlignment.convertSQLitetoXML_cfg import *
00054 process.PoolDBESSource.connect = "sqlite_file:%(dbFileName)s"
00055 process.MuonGeometryDBConverter.outputXML.fileName = "%(xmlFileName)s"
00056 process.MuonGeometryDBConverter.outputXML.relativeto = "ideal"
00057 process.MuonGeometryDBConverter.outputXML.suppressDTChambers = False
00058 process.MuonGeometryDBConverter.outputXML.suppressDTSuperLayers = False
00059 process.MuonGeometryDBConverter.outputXML.suppressDTLayers = True
00060 process.MuonGeometryDBConverter.outputXML.suppressCSCChambers = False
00061 process.MuonGeometryDBConverter.outputXML.suppressCSCLayers = False
00062 """ % vars())
00063
00064 def cmsRun(fileName):
00065 os.system("cmsRun %(fileName)s" % vars())
00066
00067
00068
00069
00070 DTpreferred_order = {"wheel":1, "station":2, "sector":3, "superlayer":4, "layer":5}
00071 CSCpreferred_order = {"endcap":1, "station":2, "ring":3, "chamber":4, "layer":5}
00072 def DTsorter(a, b): return cmp(DTpreferred_order[a], DTpreferred_order[b])
00073 def CSCsorter(a, b): return cmp(CSCpreferred_order[a], CSCpreferred_order[b])
00074
00075
00076 class Alignable:
00077 def __init__(self, alignabletype, **location):
00078 self.alignabletype = alignabletype
00079 self.location = location
00080
00081 def writeXML(self):
00082 parameters = self.location.keys()
00083 if self.alignabletype[0:2] == "DT":
00084 parameters.sort(DTsorter)
00085 else:
00086 parameters.sort(CSCsorter)
00087
00088 output = ["<", self.alignabletype, " "]
00089 for parameter in parameters:
00090 output.extend([parameter, "=\"", str(self.location[parameter]), "\" "])
00091 output.append("/>")
00092
00093 return "".join(output)
00094
00095 preferred_order = {"x":1, "y":2, "z":3, "phix":4, "phiy":5, "phiz":6}
00096 def sorter(a, b): return cmp(preferred_order[a], preferred_order[b])
00097
00098
00099 class Position:
00100 def __init__(self, **location):
00101 self.location = location
00102
00103 def writeXML(self):
00104 parameters = self.location.keys()
00105 parameters.sort(sorter)
00106
00107 output = ["<setposition relativeto=\"ideal\" "]
00108 for parameter in parameters:
00109 output.extend([parameter, "=\"", str(self.location[parameter]), "\" "])
00110 output.append("/>")
00111
00112 return "".join(output)
00113
00114
00115 class Operation:
00116 def __init__(self, alignable, position):
00117 self.alignable = alignable
00118 self.position = position
00119
00120 def writeXML(self):
00121 output = ["<operation> ", self.alignable.writeXML(), " ", self.position.writeXML(), " </operation>\n"]
00122 return "".join(output)
00123
00124 def write_xml(scenario, fileName):
00125
00126 XMLlist = ["<MuonAlignment>\n"]
00127 for operation in scenario:
00128 XMLlist.append(operation.writeXML())
00129 XMLlist.append("</MuonAlignment>\n")
00130 XMLstring = "".join(XMLlist)
00131
00132 outfile = file(fileName, "w")
00133 outfile.write(XMLstring)
00134
00135 class DTChamber:
00136 def __init__(self, **location):
00137 self.__dict__.update(location)
00138
00139 class CSCChamber:
00140 def __init__(self, **location):
00141 self.__dict__.update(location)
00142
00143
00144
00145
00146 def define_scenario():
00147
00148 scenario = []
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175 DTchambers = []
00176 for wheel in -2, -1, 0, 1, 2:
00177 for station in 1, 2, 3, 4:
00178 if station == 4: nsectors = 14
00179 else: nsectors = 12
00180 for sector in range(1, nsectors+1):
00181 DTchambers.append(DTChamber(wheel = wheel, station = station, sector = sector))
00182
00183
00184 for dtchamber in DTchambers:
00185 for superlayer in 1, 2, 3:
00186 if superlayer == 2 and dtchamber.station == 4: continue
00187
00188 alignable = Alignable("DTSuperLayer", wheel = dtchamber.wheel, station = dtchamber.station, sector = dtchamber.sector, superlayer = superlayer)
00189 position = Position(x = 0, y = 0, z = random.gauss(0, 0.054), phix = 0, phiy = 0, phiz = 0)
00190 scenario.append(Operation(alignable, position))
00191
00192 sector_errx = {}
00193
00194
00195 for wheel in -1, 0, 1:
00196 for sector in 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14:
00197 sector_errx[wheel, sector] = random.gauss(0., 0.05)
00198
00199
00200 for wheel in -1, 0, 1:
00201 for sector in 1, 7:
00202 sector_errx[wheel, sector] = random.gauss(0., 0.65)
00203 for wheel in -2, 2:
00204 for sector in 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14:
00205 sector_errx[wheel, sector] = random.gauss(0., 0.65)
00206
00207 for dtchamber in DTchambers:
00208
00209 if dtchamber.wheel in (-1, 0, 1) and dtchamber.sector in (2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14):
00210 errx = random.gauss(0, 0.08)
00211 erry = random.gauss(0, 0.10)
00212 errz = random.gauss(0, 0.10)
00213 errphix = random.gauss(0, 0.0007)
00214 errphiy = random.gauss(0, 0.0007)
00215 errphiz = random.gauss(0, 0.0003)
00216
00217
00218 else:
00219 errx = random.gauss(0, 0.08)
00220 erry = random.gauss(0, 0.24)
00221 errz = random.gauss(-0.35, 0.42)
00222 errphix = random.gauss(0, 0.0016)
00223 errphiy = random.gauss(0, 0.0021)
00224 errphiz = random.gauss(0, 0.0010)
00225
00226 errx += sector_errx[dtchamber.wheel, dtchamber.sector]
00227
00228
00229 alignable = Alignable("DTChamber", wheel = dtchamber.wheel, station = dtchamber.station, sector = dtchamber.sector)
00230 position = Position(x = errx, y = erry, z = errz, phix = errphix, phiy = errphiy, phiz = errphiz)
00231 scenario.append(Operation(alignable, position))
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241 CSCchambers = []
00242 for endcap in 1, 2:
00243 for station, ring in (1, 1), (1, 2), (1, 3), (1, 4), (2, 1), (2, 2), (3, 1), (3, 2), (4, 1):
00244 if station > 1 and ring == 1:
00245 nchambers = 18
00246 else:
00247 nchambers = 36
00248
00249 for chamber in range(1, nchambers+1):
00250 CSCchambers.append(CSCChamber(endcap = endcap, station = station, ring = ring, chamber = chamber))
00251
00252
00253 for chamber in CSCchambers:
00254 for layer in 1, 2, 3, 4, 5, 6:
00255 alignable = Alignable("CSCLayer", endcap = chamber.endcap, station = chamber.station, ring = chamber.ring, chamber = chamber.chamber, layer = layer)
00256 position = Position(x = random.gauss(0, 0.0092), y = 0, z = 0, phix = 0, phiy = 0, phiz = 0)
00257 scenario.append(Operation(alignable, position))
00258
00259
00260 CSCrings = []
00261 for endcap in 1, 2:
00262 for station, ring in (1, 1), (1, 2), (1, 3), (1, 4), (2, 1), (2, 2), (3, 1), (3, 2), (4, 1):
00263 CSCrings.append(CSCChamber(endcap = endcap, station = station, ring = ring, z = random.gauss(0, 0.1438), phix = random.gauss(0, 0.00057)))
00264
00265
00266 for chamber in CSCchambers:
00267 errx = random.gauss(0, 0.03)
00268 erry = random.gauss(0, 0.03)
00269 errz = random.gauss(0, 0.03)
00270 errphix = random.gauss(0, 0.00057)
00271 errphiy = random.gauss(0, 0.0023)
00272 errphiz = random.gauss(0, 0.00015)
00273
00274 for ring in CSCrings:
00275 if ring.endcap == chamber.endcap and ring.station == chamber.station and ring.ring == chamber.ring:
00276 errz += ring.z
00277 errphix += ring.phix
00278 break
00279
00280 alignable = Alignable("CSCChamber", endcap = chamber.endcap, station = chamber.station, ring = chamber.ring, chamber = chamber.chamber)
00281 position = Position(x = errx, y = erry, z = errz, phix = errphix, phiy = errphiy, phiz = errphiz)
00282 scenario.append(Operation(alignable, position))
00283
00284
00285 for endcap in 1, 2:
00286 for station in 1, 2, 3, 4:
00287 alignable = Alignable("CSCStation", endcap = endcap, station = station)
00288 position = Position(x = random.gauss(0, 0.05), y = random.gauss(0, 0.05), z = random.gauss(0, 0.05), phix = 0., phiy = 0., phiz = random.gauss(0, 0.0001))
00289 scenario.append(Operation(alignable, position))
00290
00291 return scenario
00292
00293
00294 make_scenario_sqlite()