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