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