CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_9/src/Alignment/MuonAlignment/python/MCScenario_CRAFT1_22X.py

Go to the documentation of this file.
00001 # To use this script:
00002 #     it's an ordinary Python script, not a CMSSW configuration file (though it writes and runs CMSSW configuration files)
00003 #         * you MUST have an inertGlobalPositionRcd.db in your working directory
00004 #         * you MUST NOT have an MCScenario_CRAFT1_22X.db
00005 #
00006 #     to make the inertGlobalPositionRcd:
00007 #         cmsRun Alignment/MuonAlignment/python/makeGlobalPositionRcd_cfg.py
00008 #
00009 #     to get rid of the MCScenario_CRAFT1_22X.db:
00010 #         rm MCScenario_CRAFT1_22X.db    (naturally)
00011 #
00012 #     to run this script:
00013 #         python MCScenario_CRAFT1_22X.py
00014 #
00015 #     it will create
00016 #         * MCScenario_CRAFT1_22X.xml            the XML file with randomly-distributed values, created directly by define_scenario()
00017 #         * convert_cfg.py                       the conversion configuration file
00018 #         * MCScenario_CRAFT1_22X.db             the SQLite database created from the XML
00019 #         * check_cfg.py                         configuration file that converts the SQLite file back into XML
00020 #         * MCScenario_CRAFT1_22X_CHECKME.xml    converted back, so that we can check the values that were saved to the database
00021 #
00022 #     to check the output in Excel, do this
00023 #         ./Alignment/MuonAlignment/python/geometryXMLtoCSV.py < MCScenario_CRAFT1_22X_CHECKME.xml > MCScenario_CRAFT1_22X_CHECKME.csv
00024 #         and then open MCScenario_CRAFT1_22X_CHECKME.csv in Excel
00025 
00026 import random, os
00027 from math import *
00028 
00029 # set the initial seed for reproducibility!
00030 random.seed(123456)
00031 
00032 #### called once at the end of this script
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 #### that's it!  everything this uses is defined below
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 ########### writing a scenario in XML ##############################################################
00068 
00069 # only needed to make the output XML readable
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 # an instance of this class corresponds to one <DTChamber ... /> or <CSCStation ... />, etc.
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 # an instance of this class corresponds to one <setposition ... />
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 # an instance of this class corresponds to one <operation> ... </operation> in the XML file
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     # a scenario is an ordered list of Operations
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 ########### defining the actual scenario ##############################################################
00144 
00145 # this is the interesting part: where we define a scenario for CRAFT1 MC
00146 def define_scenario():
00147     # this will be a list of operations to write to an XML file
00148     scenario = []
00149 
00150     # Uncertainty in DT chamber positions comes in two parts:
00151     #    1. positions within sectors
00152     #    2. positions of the sector-groups
00153 
00154     # Aligned chambers (wheels -1, 0, +1 except sectors 1 and 7)
00155     # uncertainty within sectors:
00156     #    x: 0.08 cm (from segment-matching)  phix: 0.0007 rad (from MC)
00157     #    y: 0.10 cm (from MC)                phiy: 0.0007 rad (from segment-matching)
00158     #    z: 0.10 cm (from MC)                phiz: 0.0003 rad (from MC)
00159     # uncertainty of sector-groups (depends on choice of pT cut, not well understood):
00160     #    x: 0.05 cm
00161 
00162     # Unaligned chambers uncertainty within sectors:
00163     #    x: 0.08 cm (same as above)          phix: 0.0016 rad
00164     #    y: 0.24 cm                          phiy: 0.0021 rad
00165     #    z: 0.42 cm with a -0.35 cm bias     phiz: 0.0010 rad
00166     # uncertainty of sector-groups:
00167     #    x: 0.65 cm
00168     # These come from actual alignments measured in the aligned
00169     # chambers (we assume that the unaligned chambers have
00170     # misalignments on the same scale)
00171 
00172     # Also, superlayer z uncertainty is 0.054 cm
00173 
00174     # Before starting, let's build a list of chambers
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     # the superlayers
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     # sector-groups for aligned chambers:
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     # sector-groups for unaligned chambers:
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         # within sectors for aligned chambers:
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         # within sectors for unaligned chambers:
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         # now turn this into an operation
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     # Uncertainty in CSC chamber positions comes in 5 parts:
00234     #    1. 0.0092 cm layer x misalignments observed with beam-halo tracks
00235     #    2. isotropic photogrammetry uncertainty of 0.03 cm (x, y, z) and 0.00015 rad in phiz
00236     #    3. 0.0023 rad phiy misalignment observed with beam-halo tracks
00237     #    4. 0.1438 cm z and 0.00057 rad phix uncertainty between rings from SLM (from comparison in 0T data with PG)
00238     #    5. 0.05 cm (x, y, z) disk misalignments and 0.0001 rad rotation around beamline
00239 
00240     # Before starting, let's build a list of chambers
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     # First, the layer uncertainties: x only for simplicity, observed 0.0092 cm in overlaps alignment test
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     # Next, the ring errors from DCOPS (derived from comparison with photogrammetry)
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     # Next, the chamber errors
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     # Finally, the disk errors
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 # run it all!
00294 make_scenario_sqlite()