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