CMS 3D CMS Logo

relativeConstraints.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import sys, optparse, re, math
4 from Alignment.MuonAlignment.geometryXMLparser import MuonGeometry
5 
6 usage = """./%prog GeomGlobal.xml AbsConstrints.[phiy|phipos|phiz] frameName
7 
8 Calculates constraints from AbsConstraints relative to GeomGlobal for
9 use in CSCOverlapsAlignmentAlgorithm. Assumes that constraints (including
10 chamber names) are properly formatted.
11 
12 GeomGlobal CSC geometry represented as a relativeto="none" XML file
13  (standard format--- must be MuonGeometryDBConverter output)
14 AbsConstrints text file listing absolute phiy, phipos, or phiz constraints
15  as "chambername measuredvalue uncertainty"
16 frameName name of coordinate system frame (all constraints are
17  relative to this coordinate system"""
18 
19 parser = optparse.OptionParser(usage)
20 parser.add_option("--scaleErrors",
21  help="factor to scale errors: 1 is default, 10 *weakens* constraint by a factor of 10, etc.",
22  type="string",
23  default=1,
24  dest="scaleErrors")
25 parser.add_option("--disks",
26  help="align whole disks, rather than individual rings",
27  action="store_true",
28  dest="disks")
29 
30 if len(sys.argv) < 4:
31  raise SystemError("Too few arguments.\n\n"+parser.format_help())
32 
33 if sys.argv[2][-5:] == ".phiy": mode = "phiy"
34 elif sys.argv[2][-7:] == ".phipos": mode = "phipos"
35 elif sys.argv[2][-5:] == ".phiz": mode = "phiz"
36 else: raise Exception
37 
38 geometry = MuonGeometry(sys.argv[1])
39 constraints = file(sys.argv[2])
40 frameName = sys.argv[3]
41 
42 options, args = parser.parse_args(sys.argv[4:])
43 options.scaleErrors = float(options.scaleErrors)
44 
45 empty = True
46 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": []}
47 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": []}
48 
49 for line in constraints.readlines():
50  match = re.match(r"(ME[\+\-/0-9]+)\s+([\+\-\.eE0-9]+)\s+([\+\-\.eE0-9]+)", line)
51  if match is not None:
52  chamber, value, error = match.groups()
53  ringName = chamber[0:6]
54  value = float(value)
55  error = float(error) * options.scaleErrors
56 
57  if options.disks:
58  if ringName in ("ME+1/2", "ME+1/3"): ringName = "YE+1"
59  elif ringName in ("ME+2/1", "ME+2/2", "ME+3/1", "ME+3/2"): ringName = "YE+2"
60  elif ringName in ("ME-1/2", "ME-1/3"): ringName = "YE-1"
61  elif ringName in ("ME-2/1", "ME-2/2", "ME-3/1", "ME-3/2"): ringName = "YE-2"
62 
63  if chamber[2] == "+": endcap = 1
64  elif chamber[2] == "-": endcap = 2
65  else: raise Exception
66  station = int(chamber[3])
67  ring = int(chamber[5])
68  cham = int(chamber[7:9])
69 
70  if mode == "phiy":
71  geom = geometry.csc[endcap, station, ring, cham].phiy
72  elif mode == "phipos":
73  geom = math.atan2(geometry.csc[endcap, station, ring, cham].y, geometry.csc[endcap, station, ring, cham].x)
74  elif mode == "phiz":
75  geom = geometry.csc[endcap, station, ring, cham].phiz
76 
77  relative = value - geom
78 
79  if mode in ("phiy", "phipos", "phiz"):
80  while relative > math.pi: relative -= 2.*math.pi
81  while relative <= -math.pi: relative += 2.*math.pi
82 
83  if ringName in byRing:
84  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())
85  empty = False
86 
87 if not empty:
88  keys = byRing.keys()
89  keys.sort()
90  print "for fitter in process.looper.algoConfig.fitters:"
91  for ringName in keys:
92  if len(byRing[ringName]) > 0:
93  print " if fitter.name.value() == \"%(ringName)s\":" % vars()
94  print " fitter.alignables.append(\"%(frameName)s\")" % vars()
95  for line in byRing[ringName]:
96  print " fitter.constraints.append(%(line)s)" % vars()