CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_9/src/Alignment/MuonAlignmentAlgorithms/scripts/motionPolicyChamber.py

Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 
00003 import os, sys, optparse, math, copy
00004 from Alignment.MuonAlignment.geometryXMLparser import MuonGeometry, dtorder, cscorder
00005 
00006 copyargs = sys.argv[:]
00007 for i in range(len(copyargs)):
00008     if copyargs[i] == "":
00009         copyargs[i] = "\"\""
00010 commandline = " ".join(copyargs)
00011 print commandline
00012 
00013 prog = sys.argv[0]
00014 
00015 usage = """%(prog)s INPUT_XML_0  INPUT_XML_N  INPUT_REPORT_N  OUTPUT_XML  [--nsigma NSIGMA] [--dt] [--csc]
00016 
00017 Required arguments:
00018 
00019 INPUT_XML_0      is xml file name with chamber positions from the initial (iteration 0) geometry
00020 INPUT_XML_N      is xml file name with chamber positions from the last iteration's geometry
00021 INPUT_REPORT_N   is a _report.py file from the last iteration
00022 OUTPUT_XML       is the resulting .xml file
00023 
00024 Options:
00025 
00026 --nsigma NSIGMA  optional minimum position displacement (measured in nsigma of deltax) in order to move a chamber
00027                  default NSIGMA=3
00028 --dt             if present, DT chambers will be included
00029 --csc            if present, CSC chambers will be included
00030                  NOTE: By default, having no --dt or --csc specified is equivalent to "--dt --csc"
00031 """ % vars()
00032 
00033 if len(sys.argv) < 5:
00034   print "Too few arguments.\n\n"+usage
00035   sys.exit()
00036 
00037 parser=optparse.OptionParser(usage)
00038 
00039 parser.add_option("-s","--nsigma",
00040   help="optional minimum nsigma(deltax) position displacement in order to move a chamber; default NSIGMA=3",
00041   type="int",
00042   default=2,
00043   dest="nsigma")
00044 
00045 parser.add_option("--dt",
00046   help="If it is present, but not --csc, DT chambers only will be considered",
00047   action="store_true",
00048   default=False,
00049   dest="dt")
00050 
00051 parser.add_option("--csc",
00052   help="If this is present, but not --dt, CSC chambers only will be considered",
00053   action="store_true",
00054   default=False,
00055   dest="csc")
00056 
00057 options, args = parser.parse_args(sys.argv[5:])
00058 
00059 theInXML0 = sys.argv[1]
00060 theInXMLN = sys.argv[2]
00061 theInREPN = sys.argv[3]
00062 theOutXML = sys.argv[4]
00063 
00064 theNSigma = options.nsigma
00065 
00066 DO_DT  = False
00067 DO_CSC = False
00068 if options.dt or not ( options.dt or options.csc):
00069   DO_DT = True
00070 if options.csc or not ( options.dt or options.csc):
00071   DO_CSC = True
00072 
00073 
00074 if not os.access(theInXML0,os.F_OK): print theInXML0,"not found!\n"
00075 if not os.access(theInXMLN,os.F_OK): print theInXMLN,"not found!\n"
00076 if not os.access(theInREPN,os.F_OK): print theInREPN,"not found!\n"
00077 if not (os.access(theInXML0,os.F_OK) and os.access(theInXMLN,os.F_OK) and os.access(theInREPN,os.F_OK)):
00078   print usage
00079   sys.exit()
00080 
00081 geom0 = MuonGeometry(file(theInXML0))
00082 geomN = MuonGeometry(file(theInXMLN))
00083 execfile(theInREPN)
00084 
00085 
00086 def loopover(muSystem):
00087 
00088   movedChamberKeys = []
00089   
00090   if muSystem == "DT":
00091     keys = geom0.dt.keys()
00092     keys.sort(dtorder)
00093   elif muSystem == "CSC":
00094     keys = geom0.csc.keys()
00095     keys.sort(cscorder)
00096   else: raise Exception
00097   
00098   nkeys, nkeysr, nkeyspass, nmoved, nnotmoved = 0,0,0,0,0
00099   nfail_toideal, nfail_deltafinal, nfail_lowstat, nfail_nsigma = 0,0,0,0
00100   nok_toideal, nok_deltafinal, nok_lowstat, nok_nsigma = 0,0,0,0
00101   
00102   for key in keys:
00103     is_ch = True
00104     if muSystem == "DT":
00105       if len(key) != 3: is_ch = False
00106       ch_key = key[:3]
00107       g1 = geom0.dt[key]
00108       g2 = geomN.dt[key]
00109       ch_g1 = geom0.dt[ch_key]
00110       ch_g2 = geomN.dt[ch_key]
00111     else:
00112       if len(key) != 4: is_ch = False
00113       ch_key = key[:4]
00114       g1 = geom0.csc[key]
00115       g2 = geomN.csc[key]
00116       ch_g1 = geom0.csc[ch_key]
00117       ch_g2 = geomN.csc[ch_key]
00118     if is_ch: nkeys+=1
00119 
00120     chWasntMoved = True
00121     if ch_key in movedChamberKeys:
00122       chWasntMoved = False
00123     
00124     if g1.relativeto != g2.relativeto:
00125       print "%s %s relativeto=\"%s\" versus relativeto=\"%s\"" % (muSystem, str(key), g1.relativeto, g2.relativeto)
00126 
00127     found = False
00128     #r = reports[0]
00129     for r in reports:
00130       if r.postal_address[1:] == ch_key:
00131         found = True
00132         rep = r
00133         break
00134     if not found:
00135       if is_ch: print muSystem, str(key), "not found in the report. Continue..."
00136       continue
00137     if is_ch: nkeysr+=1
00138 
00139     if rep.status != "PASS":
00140       if is_ch: print muSystem, str(key), "status is not PASS: %s   Continue..." % rep.status
00141       continue
00142     #print muSystem, str(key), str(ch_key)
00143     if is_ch: nkeyspass+=1
00144     
00145     ############################################################
00146     # CHECKS
00147     
00148     ok = True
00149     
00150     if muSystem == "DT" and chWasntMoved:
00151       
00152       # check that chamber's movement respective to ideal geometry is in reasonable range of 20 mm or 20 mrad:
00153       if abs(ch_g2.x) > 2. or abs(ch_g2.y) > 2. or abs(ch_g2.phiy) > 0.02 or abs(ch_g2.phiz) > 0.02:
00154         ok = False
00155         if is_ch:
00156           nfail_toideal += 1
00157           print "Warning!!!", muSystem, str(key), \
00158             "moved too much with respect to ideal: dx=%.2f mm  dy=%.2f mm  dphiy=%.2f mrad  dphiz=%.2f mrad  skipping..." % (ch_g2.x*10, ch_g2.y*10, ch_g2.phiy*1000, ch_g2.phiz*1000)
00159       if is_ch and ok: nok_toideal +=1
00160       
00161       # check that movements during the final iteration were negligible
00162       # separately for station 4
00163       if key[1] != 4 :
00164         if abs(rep.deltax.value) > 0.03 or abs(rep.deltay.value) > 0.03 or abs(rep.deltaphiy.value) > 0.0003 or abs(rep.deltaphiz.value) > 0.0006:
00165           ok = False
00166           if is_ch:
00167             nfail_deltafinal += 1
00168             print "Warning!!!", muSystem, str(key), \
00169               "moved too much at final iteration: dx=%.2f mm  dy=%.2f mm  dphiy=%.2f mrad  dphiz=%.2f mrad   skipping..." % \
00170                 (rep.deltax.value*10, rep.deltay.value*10, rep.deltaphiy.value*1000, rep.deltaphiz.value*1000)
00171       else:
00172         if abs(rep.deltax.value) > 0.03 or abs(rep.deltaphiy.value) > 0.0003 or abs(rep.deltaphiz.value) > 0.0006:
00173           ok = False
00174           if is_ch:
00175             nfail_deltafinal += 1
00176             print "Warning!!!", muSystem, str(key), \
00177               "moved too much at final iteration: dx=%.2f mm  dphiy=%.2f mrad  dphiz=%.2f mrad   skipping..." % \
00178                 (rep.deltax.value*10, rep.deltaphiy.value*1000, rep.deltaphiz.value*1000)
00179       if is_ch and ok: nok_deltafinal +=1
00180 
00181       # low statictics check:
00182       if rep.deltax.error > 0.5:
00183         ok = False
00184         if is_ch:
00185           nfail_lowstat +=1
00186           print "Warning!!!", muSystem, str(key), "low statistics chamber with too big dx.error = %.2f mm   skipping..." % (rep.deltax.error*10.)
00187       if is_ch and ok: nok_lowstat +=1
00188     
00189       # N-SIGMA MOTION POLICY CHECK
00190       #print "%s %s position difference: %g - %g = %g --> %g, nsigma: %g)" % (
00191       #        muSystem, str(key), g1.x, g2.x, g1.x - g2.x, abs(g1.x - g2.x)/rep.deltax.error, theNSigma * rep.deltax.error)
00192       if abs(ch_g1.x - ch_g2.x) < theNSigma * math.sqrt ( rep.deltax.error*rep.deltax.error + 0.02*0.02 ) :
00193         ok = False
00194         if is_ch:
00195           nfail_nsigma += 1
00196           print muSystem, str(key), "not moved: xN-x0 = %.3f - %.3f = %.3f < %.3f mm" % \
00197             ( ch_g2.x*10., ch_g1.x*10., (ch_g2.x-ch_g1.x)*10., theNSigma * math.sqrt ( rep.deltax.error*rep.deltax.error + 0.02*0.02 )*10.)
00198 
00199       if ok: chWasntMoved = False
00200     
00201     if not ok or chWasntMoved: continue
00202     
00203     ############################################################
00204     # MOTION
00205     if is_ch:
00206       movedChamberKeys.append(ch_key)
00207       print muSystem, str(key), "moved!"
00208       nmoved+=1
00209     #move this chamber/superlayer/layer:
00210     if muSystem == "DT":
00211       geom0.dt[key] = copy.copy(geomN.dt[key])
00212     else:
00213       geom0.csc[key] = copy.copy(geomN.csc[key])
00214     
00215   nnotmoved = nkeys - nmoved
00216   nsig = int(theNSigma)
00217   print """
00218 %(muSystem)s REPORT for  %(nsig)d sigma policy:
00219 Cumulative counters:
00220   %(nkeys)d\t chambers in geometry
00221   %(nkeysr)d\t chambers in report
00222   %(nkeyspass)d\t have PASS status
00223   %(nok_toideal)d\t pass big shift with respect to ideal check
00224   %(nok_deltafinal)d\t pass big deltas during final iteration
00225   %(nok_lowstat)d\t pass low statistics (or big deltax.error) check
00226   %(nmoved)d\t moved
00227 Not moved counters:
00228   %(nnotmoved)d\t chambers not moved
00229   Numbers of chambers were not moved due to:
00230     %(nfail_toideal)d\t big shift with respect to ideal
00231     %(nfail_deltafinal)d\t big deltas during final iteration
00232     %(nfail_lowstat)d\t low statistics (or big deltax.error)
00233     %(nfail_nsigma)d\t |x_final - x_initial| < nsigma
00234 """ % vars()
00235 
00236 if DO_DT: loopover("DT")
00237 if DO_CSC: loopover("CSC")
00238 
00239 geom0.xml(file(theOutXML, "w"))