Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-03-03 02:23:12

0001 #!/usr/bin/env python3
0002 
0003 from __future__ import print_function
0004 import sys, optparse, re, math
0005 from Alignment.MuonAlignment.geometryXMLparser import MuonGeometry
0006 
0007 usage = """./%prog GeomGlobal.xml AbsConstrints.[phiy|phipos|phiz] frameName
0008 
0009 Calculates constraints from AbsConstraints relative to GeomGlobal for
0010 use in CSCOverlapsAlignmentAlgorithm.  Assumes that constraints (including
0011 chamber names) are properly formatted.
0012 
0013 GeomGlobal     CSC geometry represented as a relativeto="none" XML file
0014                (standard format--- must be MuonGeometryDBConverter output)
0015 AbsConstrints  text file listing absolute phiy, phipos, or phiz constraints
0016                as "chambername measuredvalue uncertainty"
0017 frameName      name of coordinate system frame (all constraints are
0018                relative to this coordinate system"""
0019 
0020 parser = optparse.OptionParser(usage)
0021 parser.add_option("--scaleErrors",
0022                   help="factor to scale errors: 1 is default, 10 *weakens* constraint by a factor of 10, etc.",
0023                   type="string",
0024                   default=1,
0025                   dest="scaleErrors")
0026 parser.add_option("--disks",
0027                   help="align whole disks, rather than individual rings",
0028                   action="store_true",
0029                   dest="disks")
0030 
0031 if len(sys.argv) < 4:
0032     raise SystemError("Too few arguments.\n\n"+parser.format_help())
0033 
0034 if sys.argv[2][-5:] == ".phiy": mode = "phiy"
0035 elif sys.argv[2][-7:] == ".phipos": mode = "phipos"
0036 elif sys.argv[2][-5:] == ".phiz": mode = "phiz"
0037 else: raise Exception
0038 
0039 geometry = MuonGeometry(sys.argv[1])
0040 constraints = file(sys.argv[2])
0041 frameName = sys.argv[3]
0042 
0043 options, args = parser.parse_args(sys.argv[4:])
0044 options.scaleErrors = float(options.scaleErrors)
0045 
0046 empty = True
0047 byRing = {"ME+1/1": [], "ME+1/2": [], "ME+2/1": [], "ME+2/2": [], "ME+3/1": [], "ME+3/2": [], "ME+4/1": [], "ME+4/2": [], "ME-1/1": [], "ME-1/2": [], "ME-2/1": [], "ME-2/2": [], "ME-3/1": [], "ME-3/2": [], "ME-4/1": [], "ME-4/2": []}
0048 if options.disks: byRing = {"ME+1/1": [], "YE+1": [], "YE+2": [], "ME+4/1": [], "ME+4/2": [], "ME-1/1": [], "YE-1": [], "YE-2": [], "ME-4/1": [], "ME-4/2": []}
0049 
0050 for line in constraints.readlines():
0051     match = re.match(r"(ME[\+\-/0-9]+)\s+([\+\-\.eE0-9]+)\s+([\+\-\.eE0-9]+)", line)
0052     if match is not None:
0053         chamber, value, error = match.groups()
0054         ringName = chamber[0:6]
0055         value = float(value)
0056         error = float(error) * options.scaleErrors
0057 
0058         if options.disks:
0059             if ringName in ("ME+1/2", "ME+1/3"): ringName = "YE+1"
0060             elif ringName in ("ME+2/1", "ME+2/2", "ME+3/1", "ME+3/2"): ringName = "YE+2"
0061             elif ringName in ("ME-1/2", "ME-1/3"): ringName = "YE-1"
0062             elif ringName in ("ME-2/1", "ME-2/2", "ME-3/1", "ME-3/2"): ringName = "YE-2"
0063 
0064         if chamber[2] == "+": endcap = 1
0065         elif chamber[2] == "-": endcap = 2
0066         else: raise Exception
0067         station = int(chamber[3])
0068         ring = int(chamber[5])
0069         cham = int(chamber[7:9])
0070 
0071         if mode == "phiy":
0072             geom = geometry.csc[endcap, station, ring, cham].phiy
0073         elif mode == "phipos":
0074             geom = math.atan2(geometry.csc[endcap, station, ring, cham].y, geometry.csc[endcap, station, ring, cham].x)
0075         elif mode == "phiz":
0076             geom = geometry.csc[endcap, station, ring, cham].phiz
0077 
0078         relative = value - geom
0079 
0080         if mode in ("phiy", "phipos", "phiz"):
0081             while relative > math.pi: relative -= 2.*math.pi
0082             while relative <= -math.pi: relative += 2.*math.pi
0083 
0084         if ringName in byRing:
0085             byRing[ringName].append("""cms.PSet(i = cms.string("%(frameName)s"), j = cms.string("%(chamber)s"), value = cms.double(%(relative)g), error = cms.double(%(error)g))""" % vars())
0086             empty = False
0087 
0088 if not empty:
0089     keys = sorted(byRing.keys())
0090     print("for fitter in process.looper.algoConfig.fitters:")
0091     for ringName in keys:
0092         if len(byRing[ringName]) > 0:
0093             print("    if fitter.name.value() == \"%(ringName)s\":" % vars())
0094             print("        fitter.alignables.append(\"%(frameName)s\")" % vars())
0095             for line in byRing[ringName]:
0096                 print("        fitter.constraints.append(%(line)s)" % vars())