CMS 3D CMS Logo

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