CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_7_hltpatch2/src/Alignment/MuonAlignmentAlgorithms/scripts/alignCSCRings.py

Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 
00003 import re,os,sys,shutil
00004 import optparse
00005 
00006 from mutypes import *
00007 
00008 execfile("plotscripts.py")
00009 
00010 ROOT.gROOT.SetBatch(1);
00011 
00012 ######################################################
00013 # To parse commandline args
00014 
00015 usage='%prog [options]\n'+\
00016   'a script to run CSC ring alignment procedure'+\
00017   'It is designed to run right after alignmentValidation.py script that was run with --map option. '
00018 
00019 parser=optparse.OptionParser(usage)
00020 
00021 parser.add_option("-l", "--runLabel",
00022   help="[REQUIRED] label to use for a run",
00023   type="string",
00024   default='',
00025   dest="runLabel")
00026 
00027 parser.add_option("-d", "--dir",
00028   help="[REQUIRED] directory where tmp_test_results_map__{runLabel}.pkl fit results file is located",
00029   type="string",
00030   default='',
00031   dest="dir")
00032 
00033 parser.add_option("-x", "--xml",
00034   help="[REQUIRED] xml file with input geometry",
00035   type="string",
00036   default='',
00037   dest="xml")
00038 
00039 parser.add_option("--ring2only",
00040   help="if invoked, use only ring 2 results to align all rings in corresponding disks",
00041   action="store_true",
00042   dest="ring2only")
00043 
00044 options,args=parser.parse_args()
00045 
00046 
00047 if options.runLabel=='' or options.dir=='' or options.xml=='':
00048   print "\nOne or more of REQUIRED options is missing!\n"
00049   parser.print_help()
00050   sys.exit()
00051 
00052 allOptions = "-l "+options.runLabel+" -i "+options.dir+" -x "+options.xml
00053 #if options.diagnostic: allOptions += " --diagnostic"
00054 print sys.argv[0]+" "+allOptions
00055 
00056 pwd = str(os.getcwdu())
00057 
00058 if not os.access(options.dir,os.F_OK):
00059   print "Directory " + options.dir + " does not exist. Exiting..."
00060   sys.exit()
00061 os.chdir(options.dir)
00062 
00063 if not loadTestResultsMap(options.runLabel):
00064   print "Cant open pickle file with mapplots fit results. Exiting..."
00065   sys.exit()
00066 
00067 
00068 xml_corr = {}
00069 
00070 print "       \tdX(mm)   \t dY(mm)   \tdPhiZ(mrad)"
00071 for endcap in CSC_TYPES:
00072   for station in endcap[2]:
00073     for ring in station[2]:
00074       if ring[1]=="ALL": continue
00075       # skip ME4/2 for now
00076       if station[1]=="4" and ring[1]=="2": continue
00077       
00078       ring_id = "%s%s/%s" % (endcap[0], station[1],ring[1])
00079       
00080       if MAP_RESULTS_FITSIN.has_key(ring_id):
00081         postal_address = idToPostalAddress(ring_id+'/01')
00082         
00083         fits = MAP_RESULTS_FITSIN[ring_id]
00084         d_x, de_x = fits['sin'][0]/10., fits['sin'][1]/10.
00085         d_y, de_y = -fits['cos'][0]/10., fits['cos'][1]/10.
00086         d_phiz, de_phiz = -fits['a'][0]/10./signConventions[postal_address][3], fits['a'][1]/10./signConventions[postal_address][3]
00087         
00088         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)
00089         
00090         e = endcap[3]
00091         s = station[1]
00092         r = ring[1]
00093         xml_corr[ring_id] = "<setposition relativeto=\"none\" x=\"%(d_x)s\" y=\"%(d_y)s\" phiz=\"%(d_phiz)s\" />" % vars()
00094 
00095 xml_str = """
00096 """
00097 for endcap in CSC_TYPES:
00098   for station in endcap[2]:
00099     for ring in station[2]:
00100       if ring[1]=="ALL": continue
00101       # skip ME4/2 for now
00102       #if station[1]=="4" and ring[1]=="2": continue
00103       
00104       r_with_corr = ring[1]
00105       s_with_corr = station[1]
00106       # use ME4/1 for aligning ME4/2
00107       if station[1]=="4" and ring[1]=="2": r_with_corr = "1"
00108       # ring 2 only option
00109       if options.ring2only : r_with_corr = "2"
00110       if options.ring2only and station[1]=="3": s_with_corr = "2"
00111       # no matter what, always use ME11 to correct ME11
00112       if station[1]=="1" and ring[1]=="1": r_with_corr = "1"
00113 
00114       # for jim's BH cross-check
00115       #if station[1]=="1" and ring[1]=="3": r_with_corr = "2"
00116       #if station[1]=="2" or  station[1]=="3": 
00117       #   r_with_corr = "1"
00118       #   s_with_corr = "2"
00119       
00120       ring_id = "%s%s/%s" % (endcap[0], s_with_corr, r_with_corr)
00121 
00122       if xml_corr.has_key(ring_id):
00123         corr = xml_corr[ring_id]
00124         e = endcap[3]
00125         s = station[1]
00126         r = ring[1]
00127         xml_str += """<operation>
00128   <CSCRing endcap=\"%(e)s\" station=\"%(s)s\" ring=\"%(r)s\" />
00129   %(corr)s
00130 </operation>
00131 
00132 """ % vars()
00133         # if it's ME1/1, move ME1/4 the same amount as well
00134         if s=="1" and r=="1":
00135           xml_str += """<operation>
00136   <CSCRing endcap=\"%(e)s\" station=\"%(s)s\" ring=\"4\" />
00137   %(corr)s
00138 </operation>
00139 
00140 """ % vars()
00141 
00142 print xml_str
00143 xml_str += "</MuonAlignment>"
00144 
00145 os.chdir(pwd)
00146 
00147 ff = open(options.xml+".ring",mode="w")
00148 print >>ff, xml_str
00149 ff.close()
00150 
00151 os.system('grep -v "</MuonAlignment>" %s > %s' % (options.xml, options.xml+".tmp"))
00152 os.system('cat %s %s > %s' % (options.xml+".tmp", options.xml+".ring", options.xml+".ring.xml") )
00153 os.system('rm %s %s' % (options.xml+".tmp", options.xml+".ring") )