Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:56:47

0001 #! /usr/bin/env python3
0002 
0003 from __future__ import print_function
0004 from builtins import range
0005 import os, sys, optparse, math, copy
0006 from Alignment.MuonAlignment.geometryXMLparser import MuonGeometry, dtorder, cscorder
0007 
0008 copyargs = sys.argv[:]
0009 for i in range(len(copyargs)):
0010     if copyargs[i] == "":
0011         copyargs[i] = "\"\""
0012 commandline = " ".join(copyargs)
0013 print(commandline)
0014 
0015 prog = sys.argv[0]
0016 
0017 usage = """%(prog)s INPUT_XML_0  INPUT_XML_N  INPUT_REPORT_N  OUTPUT_XML  [--nsigma NSIGMA] [--dt] [--csc]
0018 
0019 Required arguments:
0020 
0021 INPUT_XML_0      is xml file name with chamber positions from the initial (iteration 0) geometry
0022 INPUT_XML_N      is xml file name with chamber positions from the last iteration's geometry
0023 INPUT_REPORT_N   is a _report.py file from the last iteration
0024 OUTPUT_XML       is the resulting .xml file
0025 
0026 Options:
0027 
0028 --nsigma NSIGMA  optional minimum position displacement (measured in nsigma of deltax) in order to move a chamber
0029                  default NSIGMA=3
0030 --dt             if present, DT chambers will be included
0031 --csc            if present, CSC chambers will be included
0032                  NOTE: By default, having no --dt or --csc specified is equivalent to "--dt --csc"
0033 """ % vars()
0034 
0035 if len(sys.argv) < 5:
0036   print("Too few arguments.\n\n"+usage)
0037   sys.exit()
0038 
0039 parser=optparse.OptionParser(usage)
0040 
0041 parser.add_option("-s","--nsigma",
0042   help="optional minimum nsigma(deltax) position displacement in order to move a chamber; default NSIGMA=3",
0043   type="int",
0044   default=2,
0045   dest="nsigma")
0046 
0047 parser.add_option("--dt",
0048   help="If it is present, but not --csc, DT chambers only will be considered",
0049   action="store_true",
0050   default=False,
0051   dest="dt")
0052 
0053 parser.add_option("--csc",
0054   help="If this is present, but not --dt, CSC chambers only will be considered",
0055   action="store_true",
0056   default=False,
0057   dest="csc")
0058 
0059 options, args = parser.parse_args(sys.argv[5:])
0060 
0061 theInXML0 = sys.argv[1]
0062 theInXMLN = sys.argv[2]
0063 theInREPN = sys.argv[3]
0064 theOutXML = sys.argv[4]
0065 
0066 theNSigma = options.nsigma
0067 
0068 DO_DT  = False
0069 DO_CSC = False
0070 if options.dt or not ( options.dt or options.csc):
0071   DO_DT = True
0072 if options.csc or not ( options.dt or options.csc):
0073   DO_CSC = True
0074 
0075 
0076 if not os.access(theInXML0,os.F_OK): print(theInXML0,"not found!\n")
0077 if not os.access(theInXMLN,os.F_OK): print(theInXMLN,"not found!\n")
0078 if not os.access(theInREPN,os.F_OK): print(theInREPN,"not found!\n")
0079 if not (os.access(theInXML0,os.F_OK) and os.access(theInXMLN,os.F_OK) and os.access(theInREPN,os.F_OK)):
0080   print(usage)
0081   sys.exit()
0082 
0083 geom0 = MuonGeometry(file(theInXML0))
0084 geomN = MuonGeometry(file(theInXMLN))
0085 execfile(theInREPN)
0086 
0087 
0088 def loopover(muSystem):
0089 
0090   movedChamberKeys = []
0091   
0092   if muSystem == "DT":
0093     keys = geom0.dt.keys()
0094     keys.sort(dtorder)
0095   elif muSystem == "CSC":
0096     keys = geom0.csc.keys()
0097     keys.sort(cscorder)
0098   else: raise Exception
0099   
0100   nkeys, nkeysr, nkeyspass, nmoved, nnotmoved = 0,0,0,0,0
0101   nfail_toideal, nfail_deltafinal, nfail_lowstat, nfail_nsigma = 0,0,0,0
0102   nok_toideal, nok_deltafinal, nok_lowstat, nok_nsigma = 0,0,0,0
0103   
0104   for key in keys:
0105     is_ch = True
0106     if muSystem == "DT":
0107       if len(key) != 3: is_ch = False
0108       ch_key = key[:3]
0109       g1 = geom0.dt[key]
0110       g2 = geomN.dt[key]
0111       ch_g1 = geom0.dt[ch_key]
0112       ch_g2 = geomN.dt[ch_key]
0113     else:
0114       if len(key) != 4: is_ch = False
0115       ch_key = key[:4]
0116       g1 = geom0.csc[key]
0117       g2 = geomN.csc[key]
0118       ch_g1 = geom0.csc[ch_key]
0119       ch_g2 = geomN.csc[ch_key]
0120     if is_ch: nkeys+=1
0121 
0122     chWasntMoved = True
0123     if ch_key in movedChamberKeys:
0124       chWasntMoved = False
0125     
0126     if g1.relativeto != g2.relativeto:
0127       print("%s %s relativeto=\"%s\" versus relativeto=\"%s\"" % (muSystem, str(key), g1.relativeto, g2.relativeto))
0128 
0129     found = False
0130     #r = reports[0]
0131     for r in reports:
0132       if r.postal_address[1:] == ch_key:
0133         found = True
0134         rep = r
0135         break
0136     if not found:
0137       if is_ch: print(muSystem, str(key), "not found in the report. Continue...")
0138       continue
0139     if is_ch: nkeysr+=1
0140 
0141     if rep.status != "PASS":
0142       if is_ch: print(muSystem, str(key), "status is not PASS: %s   Continue..." % rep.status)
0143       continue
0144     #print muSystem, str(key), str(ch_key)
0145     if is_ch: nkeyspass+=1
0146     
0147     ############################################################
0148     # CHECKS
0149     
0150     ok = True
0151     
0152     if muSystem == "DT" and chWasntMoved:
0153       
0154       # check that chamber's movement respective to ideal geometry is in reasonable range of 20 mm or 20 mrad:
0155       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:
0156         ok = False
0157         if is_ch:
0158           nfail_toideal += 1
0159           print("Warning!!!", muSystem, str(key), \
0160             "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))
0161       if is_ch and ok: nok_toideal +=1
0162       
0163       # check that movements during the final iteration were negligible
0164       # separately for station 4
0165       if key[1] != 4 :
0166         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:
0167           ok = False
0168           if is_ch:
0169             nfail_deltafinal += 1
0170             print("Warning!!!", muSystem, str(key), \
0171               "moved too much at final iteration: dx=%.2f mm  dy=%.2f mm  dphiy=%.2f mrad  dphiz=%.2f mrad   skipping..." % \
0172                 (rep.deltax.value*10, rep.deltay.value*10, rep.deltaphiy.value*1000, rep.deltaphiz.value*1000))
0173       else:
0174         if abs(rep.deltax.value) > 0.03 or abs(rep.deltaphiy.value) > 0.0003 or abs(rep.deltaphiz.value) > 0.0006:
0175           ok = False
0176           if is_ch:
0177             nfail_deltafinal += 1
0178             print("Warning!!!", muSystem, str(key), \
0179               "moved too much at final iteration: dx=%.2f mm  dphiy=%.2f mrad  dphiz=%.2f mrad   skipping..." % \
0180                 (rep.deltax.value*10, rep.deltaphiy.value*1000, rep.deltaphiz.value*1000))
0181       if is_ch and ok: nok_deltafinal +=1
0182 
0183       # low statictics check:
0184       if rep.deltax.error > 0.5:
0185         ok = False
0186         if is_ch:
0187           nfail_lowstat +=1
0188           print("Warning!!!", muSystem, str(key), "low statistics chamber with too big dx.error = %.2f mm   skipping..." % (rep.deltax.error*10.))
0189       if is_ch and ok: nok_lowstat +=1
0190     
0191       # N-SIGMA MOTION POLICY CHECK
0192       #print "%s %s position difference: %g - %g = %g --> %g, nsigma: %g)" % (
0193       #        muSystem, str(key), g1.x, g2.x, g1.x - g2.x, abs(g1.x - g2.x)/rep.deltax.error, theNSigma * rep.deltax.error)
0194       if abs(ch_g1.x - ch_g2.x) < theNSigma * math.sqrt ( rep.deltax.error*rep.deltax.error + 0.02*0.02 ) :
0195         ok = False
0196         if is_ch:
0197           nfail_nsigma += 1
0198           print(muSystem, str(key), "not moved: xN-x0 = %.3f - %.3f = %.3f < %.3f mm" % \
0199             ( 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.))
0200 
0201       if ok: chWasntMoved = False
0202     
0203     if not ok or chWasntMoved: continue
0204     
0205     ############################################################
0206     # MOTION
0207     if is_ch:
0208       movedChamberKeys.append(ch_key)
0209       print(muSystem, str(key), "moved!")
0210       nmoved+=1
0211     #move this chamber/superlayer/layer:
0212     if muSystem == "DT":
0213       geom0.dt[key] = copy.copy(geomN.dt[key])
0214     else:
0215       geom0.csc[key] = copy.copy(geomN.csc[key])
0216     
0217   nnotmoved = nkeys - nmoved
0218   nsig = int(theNSigma)
0219   print("""
0220 %(muSystem)s REPORT for  %(nsig)d sigma policy:
0221 Cumulative counters:
0222   %(nkeys)d\t chambers in geometry
0223   %(nkeysr)d\t chambers in report
0224   %(nkeyspass)d\t have PASS status
0225   %(nok_toideal)d\t pass big shift with respect to ideal check
0226   %(nok_deltafinal)d\t pass big deltas during final iteration
0227   %(nok_lowstat)d\t pass low statistics (or big deltax.error) check
0228   %(nmoved)d\t moved
0229 Not moved counters:
0230   %(nnotmoved)d\t chambers not moved
0231   Numbers of chambers were not moved due to:
0232     %(nfail_toideal)d\t big shift with respect to ideal
0233     %(nfail_deltafinal)d\t big deltas during final iteration
0234     %(nfail_lowstat)d\t low statistics (or big deltax.error)
0235     %(nfail_nsigma)d\t |x_final - x_initial| < nsigma
0236 """ % vars())
0237 
0238 if DO_DT: loopover("DT")
0239 if DO_CSC: loopover("CSC")
0240 
0241 geom0.xml(file(theOutXML, "w"))