CMS 3D CMS Logo

MCScenario_CRAFT1_22X.py
Go to the documentation of this file.
1 # To use this script:
2 # it's an ordinary Python script, not a CMSSW configuration file (though it writes and runs CMSSW configuration files)
3 # * you MUST have an inertGlobalPositionRcd.db in your working directory
4 # * you MUST NOT have an MCScenario_CRAFT1_22X.db
5 #
6 # to make the inertGlobalPositionRcd:
7 # cmsRun Alignment/MuonAlignment/python/makeGlobalPositionRcd_cfg.py
8 #
9 # to get rid of the MCScenario_CRAFT1_22X.db:
10 # rm MCScenario_CRAFT1_22X.db (naturally)
11 #
12 # to run this script:
13 # python MCScenario_CRAFT1_22X.py
14 #
15 # it will create
16 # * MCScenario_CRAFT1_22X.xml the XML file with randomly-distributed values, created directly by define_scenario()
17 # * convert_cfg.py the conversion configuration file
18 # * MCScenario_CRAFT1_22X.db the SQLite database created from the XML
19 # * check_cfg.py configuration file that converts the SQLite file back into XML
20 # * MCScenario_CRAFT1_22X_CHECKME.xml converted back, so that we can check the values that were saved to the database
21 #
22 # to check the output in Excel, do this
23 # ./Alignment/MuonAlignment/python/geometryXMLtoCSV.py < MCScenario_CRAFT1_22X_CHECKME.xml > MCScenario_CRAFT1_22X_CHECKME.csv
24 # and then open MCScenario_CRAFT1_22X_CHECKME.csv in Excel
25 
26 import random, os
27 from math import *
28 
29 # set the initial seed for reproducibility!
30 random.seed(123456)
31 
32 #### called once at the end of this script
34  scenario = define_scenario()
35  write_xml(scenario, "MCScenario_CRAFT1_22X.xml")
36  write_conversion_cfg("convert_cfg.py", "MCScenario_CRAFT1_22X.xml", "MCScenario_CRAFT1_22X.db")
37  cmsRun("convert_cfg.py")
38  write_check_cfg("check_cfg.py", "MCScenario_CRAFT1_22X.db", "MCScenario_CRAFT1_22X_CHECKME.xml")
39  cmsRun("check_cfg.py")
40 #### that's it! everything this uses is defined below
41 
42 def write_conversion_cfg(fileName, xmlFileName, dbFileName):
43  outfile = file(fileName, "w")
44  outfile.write("""
45 from Alignment.MuonAlignment.convertXMLtoSQLite_cfg import *
46 process.MuonGeometryDBConverter.fileName = "%(xmlFileName)s"
47 process.PoolDBOutputService.connect = "sqlite_file:%(dbFileName)s"
48 """ % vars())
49 
50 def write_check_cfg(fileName, dbFileName, xmlFileName):
51  outfile = file(fileName, "w")
52  outfile.write("""
53 from Alignment.MuonAlignment.convertSQLitetoXML_cfg import *
54 process.PoolDBESSource.connect = "sqlite_file:%(dbFileName)s"
55 process.MuonGeometryDBConverter.outputXML.fileName = "%(xmlFileName)s"
56 process.MuonGeometryDBConverter.outputXML.relativeto = "ideal"
57 process.MuonGeometryDBConverter.outputXML.suppressDTChambers = False
58 process.MuonGeometryDBConverter.outputXML.suppressDTSuperLayers = False
59 process.MuonGeometryDBConverter.outputXML.suppressDTLayers = True
60 process.MuonGeometryDBConverter.outputXML.suppressCSCChambers = False
61 process.MuonGeometryDBConverter.outputXML.suppressCSCLayers = False
62 """ % vars())
63 
64 def cmsRun(fileName):
65  os.system("cmsRun %(fileName)s" % vars())
66 
67 ########### writing a scenario in XML ##############################################################
68 
69 # only needed to make the output XML readable
70 DTpreferred_order = {"wheel":1, "station":2, "sector":3, "superlayer":4, "layer":5}
71 CSCpreferred_order = {"endcap":1, "station":2, "ring":3, "chamber":4, "layer":5}
72 def DTsorter(a, b): return cmp(DTpreferred_order[a], DTpreferred_order[b])
73 def CSCsorter(a, b): return cmp(CSCpreferred_order[a], CSCpreferred_order[b])
74 
75 # an instance of this class corresponds to one <DTChamber ... /> or <CSCStation ... />, etc.
76 class Alignable:
77  def __init__(self, alignabletype, **location):
78  self.alignabletype = alignabletype
79  self.location = location
80 
81  def writeXML(self):
82  parameters = self.location.keys()
83  if self.alignabletype[0:2] == "DT":
84  parameters.sort(DTsorter)
85  else:
86  parameters.sort(CSCsorter)
87 
88  output = ["<", self.alignabletype, " "]
89  for parameter in parameters:
90  output.extend([parameter, "=\"", str(self.location[parameter]), "\" "])
91  output.append("/>")
92 
93  return "".join(output)
94 
95 preferred_order = {"x":1, "y":2, "z":3, "phix":4, "phiy":5, "phiz":6}
96 def sorter(a, b): return cmp(preferred_order[a], preferred_order[b])
97 
98 # an instance of this class corresponds to one <setposition ... />
99 class Position:
100  def __init__(self, **location):
101  self.location = location
102 
103  def writeXML(self):
104  parameters = self.location.keys()
105  parameters.sort(sorter)
106 
107  output = ["<setposition relativeto=\"ideal\" "]
108  for parameter in parameters:
109  output.extend([parameter, "=\"", str(self.location[parameter]), "\" "])
110  output.append("/>")
111 
112  return "".join(output)
113 
114 # an instance of this class corresponds to one <operation> ... </operation> in the XML file
115 class Operation:
116  def __init__(self, alignable, position):
117  self.alignable = alignable
118  self.position = position
119 
120  def writeXML(self):
121  output = ["<operation> ", self.alignable.writeXML(), " ", self.position.writeXML(), " </operation>\n"]
122  return "".join(output)
123 
124 def write_xml(scenario, fileName):
125  # a scenario is an ordered list of Operations
126  XMLlist = ["<MuonAlignment>\n"]
127  for operation in scenario:
128  XMLlist.append(operation.writeXML())
129  XMLlist.append("</MuonAlignment>\n")
130  XMLstring = "".join(XMLlist)
131 
132  outfile = file(fileName, "w")
133  outfile.write(XMLstring)
134 
135 class DTChamber:
136  def __init__(self, **location):
137  self.__dict__.update(location)
138 
140  def __init__(self, **location):
141  self.__dict__.update(location)
142 
143 ########### defining the actual scenario ##############################################################
144 
145 # this is the interesting part: where we define a scenario for CRAFT1 MC
147  # this will be a list of operations to write to an XML file
148  scenario = []
149 
150  # Uncertainty in DT chamber positions comes in two parts:
151  # 1. positions within sectors
152  # 2. positions of the sector-groups
153 
154  # Aligned chambers (wheels -1, 0, +1 except sectors 1 and 7)
155  # uncertainty within sectors:
156  # x: 0.08 cm (from segment-matching) phix: 0.0007 rad (from MC)
157  # y: 0.10 cm (from MC) phiy: 0.0007 rad (from segment-matching)
158  # z: 0.10 cm (from MC) phiz: 0.0003 rad (from MC)
159  # uncertainty of sector-groups (depends on choice of pT cut, not well understood):
160  # x: 0.05 cm
161 
162  # Unaligned chambers uncertainty within sectors:
163  # x: 0.08 cm (same as above) phix: 0.0016 rad
164  # y: 0.24 cm phiy: 0.0021 rad
165  # z: 0.42 cm with a -0.35 cm bias phiz: 0.0010 rad
166  # uncertainty of sector-groups:
167  # x: 0.65 cm
168  # These come from actual alignments measured in the aligned
169  # chambers (we assume that the unaligned chambers have
170  # misalignments on the same scale)
171 
172  # Also, superlayer z uncertainty is 0.054 cm
173 
174  # Before starting, let's build a list of chambers
175  DTchambers = []
176  for wheel in -2, -1, 0, 1, 2:
177  for station in 1, 2, 3, 4:
178  if station == 4: nsectors = 14
179  else: nsectors = 12
180  for sector in range(1, nsectors+1):
181  DTchambers.append(DTChamber(wheel = wheel, station = station, sector = sector))
182 
183  # the superlayers
184  for dtchamber in DTchambers:
185  for superlayer in 1, 2, 3:
186  if superlayer == 2 and dtchamber.station == 4: continue
187 
188  alignable = Alignable("DTSuperLayer", wheel = dtchamber.wheel, station = dtchamber.station, sector = dtchamber.sector, superlayer = superlayer)
189  position = Position(x = 0, y = 0, z = random.gauss(0, 0.054), phix = 0, phiy = 0, phiz = 0)
190  scenario.append(Operation(alignable, position))
191 
192  sector_errx = {}
193 
194  # sector-groups for aligned chambers:
195  for wheel in -1, 0, 1:
196  for sector in 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14:
197  sector_errx[wheel, sector] = random.gauss(0., 0.05)
198 
199  # sector-groups for unaligned chambers:
200  for wheel in -1, 0, 1:
201  for sector in 1, 7:
202  sector_errx[wheel, sector] = random.gauss(0., 0.65)
203  for wheel in -2, 2:
204  for sector in 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14:
205  sector_errx[wheel, sector] = random.gauss(0., 0.65)
206 
207  for dtchamber in DTchambers:
208  # within sectors for aligned chambers:
209  if dtchamber.wheel in (-1, 0, 1) and dtchamber.sector in (2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14):
210  errx = random.gauss(0, 0.08)
211  erry = random.gauss(0, 0.10)
212  errz = random.gauss(0, 0.10)
213  errphix = random.gauss(0, 0.0007)
214  errphiy = random.gauss(0, 0.0007)
215  errphiz = random.gauss(0, 0.0003)
216 
217  # within sectors for unaligned chambers:
218  else:
219  errx = random.gauss(0, 0.08)
220  erry = random.gauss(0, 0.24)
221  errz = random.gauss(-0.35, 0.42)
222  errphix = random.gauss(0, 0.0016)
223  errphiy = random.gauss(0, 0.0021)
224  errphiz = random.gauss(0, 0.0010)
225 
226  errx += sector_errx[dtchamber.wheel, dtchamber.sector]
227 
228  # now turn this into an operation
229  alignable = Alignable("DTChamber", wheel = dtchamber.wheel, station = dtchamber.station, sector = dtchamber.sector)
230  position = Position(x = errx, y = erry, z = errz, phix = errphix, phiy = errphiy, phiz = errphiz)
231  scenario.append(Operation(alignable, position))
232 
233  # Uncertainty in CSC chamber positions comes in 5 parts:
234  # 1. 0.0092 cm layer x misalignments observed with beam-halo tracks
235  # 2. isotropic photogrammetry uncertainty of 0.03 cm (x, y, z) and 0.00015 rad in phiz
236  # 3. 0.0023 rad phiy misalignment observed with beam-halo tracks
237  # 4. 0.1438 cm z and 0.00057 rad phix uncertainty between rings from SLM (from comparison in 0T data with PG)
238  # 5. 0.05 cm (x, y, z) disk misalignments and 0.0001 rad rotation around beamline
239 
240  # Before starting, let's build a list of chambers
241  CSCchambers = []
242  for endcap in 1, 2:
243  for station, ring in (1, 1), (1, 2), (1, 3), (1, 4), (2, 1), (2, 2), (3, 1), (3, 2), (4, 1):
244  if station > 1 and ring == 1:
245  nchambers = 18
246  else:
247  nchambers = 36
248 
249  for chamber in range(1, nchambers+1):
250  CSCchambers.append(CSCChamber(endcap = endcap, station = station, ring = ring, chamber = chamber))
251 
252  # First, the layer uncertainties: x only for simplicity, observed 0.0092 cm in overlaps alignment test
253  for chamber in CSCchambers:
254  for layer in 1, 2, 3, 4, 5, 6:
255  alignable = Alignable("CSCLayer", endcap = chamber.endcap, station = chamber.station, ring = chamber.ring, chamber = chamber.chamber, layer = layer)
256  position = Position(x = random.gauss(0, 0.0092), y = 0, z = 0, phix = 0, phiy = 0, phiz = 0)
257  scenario.append(Operation(alignable, position))
258 
259  # Next, the ring errors from DCOPS (derived from comparison with photogrammetry)
260  CSCrings = []
261  for endcap in 1, 2:
262  for station, ring in (1, 1), (1, 2), (1, 3), (1, 4), (2, 1), (2, 2), (3, 1), (3, 2), (4, 1):
263  CSCrings.append(CSCChamber(endcap = endcap, station = station, ring = ring, z = random.gauss(0, 0.1438), phix = random.gauss(0, 0.00057)))
264 
265  # Next, the chamber errors
266  for chamber in CSCchambers:
267  errx = random.gauss(0, 0.03)
268  erry = random.gauss(0, 0.03)
269  errz = random.gauss(0, 0.03)
270  errphix = random.gauss(0, 0.00057)
271  errphiy = random.gauss(0, 0.0023)
272  errphiz = random.gauss(0, 0.00015)
273 
274  for ring in CSCrings:
275  if ring.endcap == chamber.endcap and ring.station == chamber.station and ring.ring == chamber.ring:
276  errz += ring.z
277  errphix += ring.phix
278  break
279 
280  alignable = Alignable("CSCChamber", endcap = chamber.endcap, station = chamber.station, ring = chamber.ring, chamber = chamber.chamber)
281  position = Position(x = errx, y = erry, z = errz, phix = errphix, phiy = errphiy, phiz = errphiz)
282  scenario.append(Operation(alignable, position))
283 
284  # Finally, the disk errors
285  for endcap in 1, 2:
286  for station in 1, 2, 3, 4:
287  alignable = Alignable("CSCStation", endcap = endcap, station = station)
288  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))
289  scenario.append(Operation(alignable, position))
290 
291  return scenario
292 
293 # run it all!
def __init__(self, alignabletype, location)
def make_scenario_sqlite()
called once at the end of this script
def write_conversion_cfg(fileName, xmlFileName, dbFileName)
that&#39;s it! everything this uses is defined below
def __init__(self, alignable, position)
def write_xml(scenario, fileName)
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
def write_check_cfg(fileName, dbFileName, xmlFileName)