CMS 3D CMS Logo

alignCSCRings.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 import re,os,sys,shutil
4 import optparse
5 
6 from mutypes import *
7 
8 execfile("plotscripts.py")
9 
10 ROOT.gROOT.SetBatch(1);
11 
12 ######################################################
13 # To parse commandline args
14 
15 usage='%prog [options]\n'+\
16  'a script to run CSC ring alignment procedure'+\
17  'It is designed to run right after alignmentValidation.py script that was run with --map option. '
18 
19 parser=optparse.OptionParser(usage)
20 
21 parser.add_option("-l", "--runLabel",
22  help="[REQUIRED] label to use for a run",
23  type="string",
24  default='',
25  dest="runLabel")
26 
27 parser.add_option("-d", "--dir",
28  help="[REQUIRED] directory where tmp_test_results_map__{runLabel}.pkl fit results file is located",
29  type="string",
30  default='',
31  dest="dir")
32 
33 parser.add_option("-x", "--xml",
34  help="[REQUIRED] xml file with input geometry",
35  type="string",
36  default='',
37  dest="xml")
38 
39 parser.add_option("--ring2only",
40  help="if invoked, use only ring 2 results to align all rings in corresponding disks",
41  action="store_true",
42  dest="ring2only")
43 
44 options,args=parser.parse_args()
45 
46 
47 if options.runLabel=='' or options.dir=='' or options.xml=='':
48  print "\nOne or more of REQUIRED options is missing!\n"
49  parser.print_help()
50  sys.exit()
51 
52 allOptions = "-l "+options.runLabel+" -i "+options.dir+" -x "+options.xml
53 #if options.diagnostic: allOptions += " --diagnostic"
54 print sys.argv[0]+" "+allOptions
55 
56 pwd = str(os.getcwdu())
57 
58 if not os.access(options.dir,os.F_OK):
59  print "Directory " + options.dir + " does not exist. Exiting..."
60  sys.exit()
61 os.chdir(options.dir)
62 
63 if not loadTestResultsMap(options.runLabel):
64  print "Cant open pickle file with mapplots fit results. Exiting..."
65  sys.exit()
66 
67 
68 xml_corr = {}
69 
70 print " \tdX(mm) \t dY(mm) \tdPhiZ(mrad)"
71 for endcap in CSC_TYPES:
72  for station in endcap[2]:
73  for ring in station[2]:
74  if ring[1]=="ALL": continue
75  # skip ME4/2 for now
76  if station[1]=="4" and ring[1]=="2": continue
77 
78  ring_id = "%s%s/%s" % (endcap[0], station[1],ring[1])
79 
80  if ring_id in MAP_RESULTS_FITSIN:
81  postal_address = idToPostalAddress(ring_id+'/01')
82 
83  fits = MAP_RESULTS_FITSIN[ring_id]
84  d_x, de_x = fits['sin'][0]/10., fits['sin'][1]/10.
85  d_y, de_y = -fits['cos'][0]/10., fits['cos'][1]/10.
86  d_phiz, de_phiz = -fits['a'][0]/10./signConventions[postal_address][3], fits['a'][1]/10./signConventions[postal_address][3]
87 
88  print "%s \t%+.2f+-%.2f \t%+.2f+-%.2f \t%+.2f+-%.2f" % (ring_id, d_x*10 , de_x*10, d_y*10 , de_y*10 , d_phiz*1000, de_phiz*1000)
89 
90  e = endcap[3]
91  s = station[1]
92  r = ring[1]
93  xml_corr[ring_id] = "<setposition relativeto=\"none\" x=\"%(d_x)s\" y=\"%(d_y)s\" phiz=\"%(d_phiz)s\" />" % vars()
94 
95 xml_str = """
96 """
97 for endcap in CSC_TYPES:
98  for station in endcap[2]:
99  for ring in station[2]:
100  if ring[1]=="ALL": continue
101  # skip ME4/2 for now
102  #if station[1]=="4" and ring[1]=="2": continue
103 
104  r_with_corr = ring[1]
105  s_with_corr = station[1]
106  # use ME4/1 for aligning ME4/2
107  if station[1]=="4" and ring[1]=="2": r_with_corr = "1"
108  # ring 2 only option
109  if options.ring2only : r_with_corr = "2"
110  if options.ring2only and station[1]=="3": s_with_corr = "2"
111  # no matter what, always use ME11 to correct ME11
112  if station[1]=="1" and ring[1]=="1": r_with_corr = "1"
113 
114  # for jim's BH cross-check
115  #if station[1]=="1" and ring[1]=="3": r_with_corr = "2"
116  #if station[1]=="2" or station[1]=="3":
117  # r_with_corr = "1"
118  # s_with_corr = "2"
119 
120  ring_id = "%s%s/%s" % (endcap[0], s_with_corr, r_with_corr)
121 
122  if ring_id in xml_corr:
123  corr = xml_corr[ring_id]
124  e = endcap[3]
125  s = station[1]
126  r = ring[1]
127  xml_str += """<operation>
128  <CSCRing endcap=\"%(e)s\" station=\"%(s)s\" ring=\"%(r)s\" />
129  %(corr)s
130 </operation>
131 
132 """ % vars()
133  # if it's ME1/1, move ME1/4 the same amount as well
134  if s=="1" and r=="1":
135  xml_str += """<operation>
136  <CSCRing endcap=\"%(e)s\" station=\"%(s)s\" ring=\"4\" />
137  %(corr)s
138 </operation>
139 
140 """ % vars()
141 
142 print xml_str
143 xml_str += "</MuonAlignment>"
144 
145 os.chdir(pwd)
146 
147 ff = open(options.xml+".ring",mode="w")
148 print >>ff, xml_str
149 ff.close()
150 
151 os.system('grep -v "</MuonAlignment>" %s > %s' % (options.xml, options.xml+".tmp"))
152 os.system('cat %s %s > %s' % (options.xml+".tmp", options.xml+".ring", options.xml+".ring.xml") )
153 os.system('rm %s %s' % (options.xml+".tmp", options.xml+".ring") )
def loadTestResultsMap(run_name)
def idToPostalAddress(id)
Definition: plotscripts.py:861