File indexing completed on 2023-03-17 10:39:47
0001
0002
0003 from __future__ import print_function
0004 import re,os,sys,shutil
0005 import optparse
0006
0007 from mutypes import *
0008
0009 execfile("plotscripts.py")
0010
0011 ROOT.gROOT.SetBatch(1);
0012
0013
0014
0015
0016 usage='%prog [options]\n'+\
0017 'a script to run CSC ring alignment procedure'+\
0018 'It is designed to run right after alignmentValidation.py script that was run with --map option. '
0019
0020 parser=optparse.OptionParser(usage)
0021
0022 parser.add_option("-l", "--runLabel",
0023 help="[REQUIRED] label to use for a run",
0024 type="string",
0025 default='',
0026 dest="runLabel")
0027
0028 parser.add_option("-d", "--dir",
0029 help="[REQUIRED] directory where tmp_test_results_map__{runLabel}.pkl fit results file is located",
0030 type="string",
0031 default='',
0032 dest="dir")
0033
0034 parser.add_option("-x", "--xml",
0035 help="[REQUIRED] xml file with input geometry",
0036 type="string",
0037 default='',
0038 dest="xml")
0039
0040 parser.add_option("--ring2only",
0041 help="if invoked, use only ring 2 results to align all rings in corresponding disks",
0042 action="store_true",
0043 dest="ring2only")
0044
0045 options,args=parser.parse_args()
0046
0047
0048 if options.runLabel=='' or options.dir=='' or options.xml=='':
0049 print("\nOne or more of REQUIRED options is missing!\n")
0050 parser.print_help()
0051 sys.exit()
0052
0053 allOptions = "-l "+options.runLabel+" -i "+options.dir+" -x "+options.xml
0054
0055 print(sys.argv[0]+" "+allOptions)
0056
0057 pwd = str(os.getcwd())
0058
0059 if not os.access(options.dir,os.F_OK):
0060 print("Directory " + options.dir + " does not exist. Exiting...")
0061 sys.exit()
0062 os.chdir(options.dir)
0063
0064 if not loadTestResultsMap(options.runLabel):
0065 print("Cant open pickle file with mapplots fit results. Exiting...")
0066 sys.exit()
0067
0068
0069 xml_corr = {}
0070
0071 print(" \tdX(mm) \t dY(mm) \tdPhiZ(mrad)")
0072 for endcap in CSC_TYPES:
0073 for station in endcap[2]:
0074 for ring in station[2]:
0075 if ring[1]=="ALL": continue
0076
0077 if station[1]=="4" and ring[1]=="2": continue
0078
0079 ring_id = "%s%s/%s" % (endcap[0], station[1],ring[1])
0080
0081 if ring_id in MAP_RESULTS_FITSIN:
0082 postal_address = idToPostalAddress(ring_id+'/01')
0083
0084 fits = MAP_RESULTS_FITSIN[ring_id]
0085 d_x, de_x = fits['sin'][0]/10., fits['sin'][1]/10.
0086 d_y, de_y = -fits['cos'][0]/10., fits['cos'][1]/10.
0087 d_phiz, de_phiz = -fits['a'][0]/10./signConventions[postal_address][3], fits['a'][1]/10./signConventions[postal_address][3]
0088
0089 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))
0090
0091 e = endcap[3]
0092 s = station[1]
0093 r = ring[1]
0094 xml_corr[ring_id] = "<setposition relativeto=\"none\" x=\"%(d_x)s\" y=\"%(d_y)s\" phiz=\"%(d_phiz)s\" />" % vars()
0095
0096 xml_str = """
0097 """
0098 for endcap in CSC_TYPES:
0099 for station in endcap[2]:
0100 for ring in station[2]:
0101 if ring[1]=="ALL": continue
0102
0103
0104
0105 r_with_corr = ring[1]
0106 s_with_corr = station[1]
0107
0108 if station[1]=="4" and ring[1]=="2": r_with_corr = "1"
0109
0110 if options.ring2only : r_with_corr = "2"
0111 if options.ring2only and station[1]=="3": s_with_corr = "2"
0112
0113 if station[1]=="1" and ring[1]=="1": r_with_corr = "1"
0114
0115
0116
0117
0118
0119
0120
0121 ring_id = "%s%s/%s" % (endcap[0], s_with_corr, r_with_corr)
0122
0123 if ring_id in xml_corr:
0124 corr = xml_corr[ring_id]
0125 e = endcap[3]
0126 s = station[1]
0127 r = ring[1]
0128 xml_str += """<operation>
0129 <CSCRing endcap=\"%(e)s\" station=\"%(s)s\" ring=\"%(r)s\" />
0130 %(corr)s
0131 </operation>
0132
0133 """ % vars()
0134
0135 if s=="1" and r=="1":
0136 xml_str += """<operation>
0137 <CSCRing endcap=\"%(e)s\" station=\"%(s)s\" ring=\"4\" />
0138 %(corr)s
0139 </operation>
0140
0141 """ % vars()
0142
0143 print(xml_str)
0144 xml_str += "</MuonAlignment>"
0145
0146 os.chdir(pwd)
0147
0148 ff = open(options.xml+".ring",mode="w")
0149 print(xml_str, file=ff)
0150 ff.close()
0151
0152 os.system('grep -v "</MuonAlignment>" %s > %s' % (options.xml, options.xml+".tmp"))
0153 os.system('cat %s %s > %s' % (options.xml+".tmp", options.xml+".ring", options.xml+".ring.xml") )
0154 os.system('rm %s %s' % (options.xml+".tmp", options.xml+".ring") )