CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10/src/Alignment/MuonAlignmentAlgorithms/scripts/relativeConstraints.py

Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 import sys, optparse, re, math
00004 from Alignment.MuonAlignment.geometryXMLparser import MuonGeometry
00005 
00006 usage = """./%prog GeomGlobal.xml AbsConstrints.[phiy|phipos|phiz] frameName
00007 
00008 Calculates constraints from AbsConstraints relative to GeomGlobal for
00009 use in CSCOverlapsAlignmentAlgorithm.  Assumes that constraints (including
00010 chamber names) are properly formatted.
00011 
00012 GeomGlobal     CSC geometry represented as a relativeto="none" XML file
00013                (standard format--- must be MuonGeometryDBConverter output)
00014 AbsConstrints  text file listing absolute phiy, phipos, or phiz constraints
00015                as "chambername measuredvalue uncertainty"
00016 frameName      name of coordinate system frame (all constraints are
00017                relative to this coordinate system"""
00018 
00019 parser = optparse.OptionParser(usage)
00020 parser.add_option("--scaleErrors",
00021                   help="factor to scale errors: 1 is default, 10 *weakens* constraint by a factor of 10, etc.",
00022                   type="string",
00023                   default=1,
00024                   dest="scaleErrors")
00025 parser.add_option("--disks",
00026                   help="align whole disks, rather than individual rings",
00027                   action="store_true",
00028                   dest="disks")
00029 
00030 if len(sys.argv) < 4:
00031     raise SystemError, "Too few arguments.\n\n"+parser.format_help()
00032 
00033 if sys.argv[2][-5:] == ".phiy": mode = "phiy"
00034 elif sys.argv[2][-7:] == ".phipos": mode = "phipos"
00035 elif sys.argv[2][-5:] == ".phiz": mode = "phiz"
00036 else: raise Exception
00037 
00038 geometry = MuonGeometry(sys.argv[1])
00039 constraints = file(sys.argv[2])
00040 frameName = sys.argv[3]
00041 
00042 options, args = parser.parse_args(sys.argv[4:])
00043 options.scaleErrors = float(options.scaleErrors)
00044 
00045 empty = True
00046 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": []}
00047 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": []}
00048 
00049 for line in constraints.readlines():
00050     match = re.match(r"(ME[\+\-/0-9]+)\s+([\+\-\.eE0-9]+)\s+([\+\-\.eE0-9]+)", line)
00051     if match is not None:
00052         chamber, value, error = match.groups()
00053         ringName = chamber[0:6]
00054         value = float(value)
00055         error = float(error) * options.scaleErrors
00056 
00057         if options.disks:
00058             if ringName in ("ME+1/2", "ME+1/3"): ringName = "YE+1"
00059             elif ringName in ("ME+2/1", "ME+2/2", "ME+3/1", "ME+3/2"): ringName = "YE+2"
00060             elif ringName in ("ME-1/2", "ME-1/3"): ringName = "YE-1"
00061             elif ringName in ("ME-2/1", "ME-2/2", "ME-3/1", "ME-3/2"): ringName = "YE-2"
00062 
00063         if chamber[2] == "+": endcap = 1
00064         elif chamber[2] == "-": endcap = 2
00065         else: raise Exception
00066         station = int(chamber[3])
00067         ring = int(chamber[5])
00068         cham = int(chamber[7:9])
00069 
00070         if mode == "phiy":
00071             geom = geometry.csc[endcap, station, ring, cham].phiy
00072         elif mode == "phipos":
00073             geom = math.atan2(geometry.csc[endcap, station, ring, cham].y, geometry.csc[endcap, station, ring, cham].x)
00074         elif mode == "phiz":
00075             geom = geometry.csc[endcap, station, ring, cham].phiz
00076 
00077         relative = value - geom
00078 
00079         if mode in ("phiy", "phipos", "phiz"):
00080             while relative > math.pi: relative -= 2.*math.pi
00081             while relative <= -math.pi: relative += 2.*math.pi
00082 
00083         if ringName in byRing:
00084             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())
00085             empty = False
00086 
00087 if not empty:
00088     keys = byRing.keys()
00089     keys.sort()
00090     print "for fitter in process.looper.algoConfig.fitters:"
00091     for ringName in keys:
00092         if len(byRing[ringName]) > 0:
00093             print "    if fitter.name.value() == \"%(ringName)s\":" % vars()
00094             print "        fitter.alignables.append(\"%(frameName)s\")" % vars()
00095             for line in byRing[ringName]:
00096                 print "        fitter.constraints.append(%(line)s)" % vars()