CMS 3D CMS Logo

motionPolicyChamber.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 from __future__ import print_function
4 import os, sys, optparse, math, copy
5 from Alignment.MuonAlignment.geometryXMLparser import MuonGeometry, dtorder, cscorder
6 
7 copyargs = sys.argv[:]
8 for i in range(len(copyargs)):
9  if copyargs[i] == "":
10  copyargs[i] = "\"\""
11 commandline = " ".join(copyargs)
12 print(commandline)
13 
14 prog = sys.argv[0]
15 
16 usage = """%(prog)s INPUT_XML_0 INPUT_XML_N INPUT_REPORT_N OUTPUT_XML [--nsigma NSIGMA] [--dt] [--csc]
17 
18 Required arguments:
19 
20 INPUT_XML_0 is xml file name with chamber positions from the initial (iteration 0) geometry
21 INPUT_XML_N is xml file name with chamber positions from the last iteration's geometry
22 INPUT_REPORT_N is a _report.py file from the last iteration
23 OUTPUT_XML is the resulting .xml file
24 
25 Options:
26 
27 --nsigma NSIGMA optional minimum position displacement (measured in nsigma of deltax) in order to move a chamber
28  default NSIGMA=3
29 --dt if present, DT chambers will be included
30 --csc if present, CSC chambers will be included
31  NOTE: By default, having no --dt or --csc specified is equivalent to "--dt --csc"
32 """ % vars()
33 
34 if len(sys.argv) < 5:
35  print("Too few arguments.\n\n"+usage)
36  sys.exit()
37 
38 parser=optparse.OptionParser(usage)
39 
40 parser.add_option("-s","--nsigma",
41  help="optional minimum nsigma(deltax) position displacement in order to move a chamber; default NSIGMA=3",
42  type="int",
43  default=2,
44  dest="nsigma")
45 
46 parser.add_option("--dt",
47  help="If it is present, but not --csc, DT chambers only will be considered",
48  action="store_true",
49  default=False,
50  dest="dt")
51 
52 parser.add_option("--csc",
53  help="If this is present, but not --dt, CSC chambers only will be considered",
54  action="store_true",
55  default=False,
56  dest="csc")
57 
58 options, args = parser.parse_args(sys.argv[5:])
59 
60 theInXML0 = sys.argv[1]
61 theInXMLN = sys.argv[2]
62 theInREPN = sys.argv[3]
63 theOutXML = sys.argv[4]
64 
65 theNSigma = options.nsigma
66 
67 DO_DT = False
68 DO_CSC = False
69 if options.dt or not ( options.dt or options.csc):
70  DO_DT = True
71 if options.csc or not ( options.dt or options.csc):
72  DO_CSC = True
73 
74 
75 if not os.access(theInXML0,os.F_OK): print(theInXML0,"not found!\n")
76 if not os.access(theInXMLN,os.F_OK): print(theInXMLN,"not found!\n")
77 if not os.access(theInREPN,os.F_OK): print(theInREPN,"not found!\n")
78 if not (os.access(theInXML0,os.F_OK) and os.access(theInXMLN,os.F_OK) and os.access(theInREPN,os.F_OK)):
79  print(usage)
80  sys.exit()
81 
82 geom0 = MuonGeometry(file(theInXML0))
83 geomN = MuonGeometry(file(theInXMLN))
84 execfile(theInREPN)
85 
86 
87 def loopover(muSystem):
88 
89  movedChamberKeys = []
90 
91  if muSystem == "DT":
92  keys = geom0.dt.keys()
93  keys.sort(dtorder)
94  elif muSystem == "CSC":
95  keys = geom0.csc.keys()
96  keys.sort(cscorder)
97  else: raise Exception
98 
99  nkeys, nkeysr, nkeyspass, nmoved, nnotmoved = 0,0,0,0,0
100  nfail_toideal, nfail_deltafinal, nfail_lowstat, nfail_nsigma = 0,0,0,0
101  nok_toideal, nok_deltafinal, nok_lowstat, nok_nsigma = 0,0,0,0
102 
103  for key in keys:
104  is_ch = True
105  if muSystem == "DT":
106  if len(key) != 3: is_ch = False
107  ch_key = key[:3]
108  g1 = geom0.dt[key]
109  g2 = geomN.dt[key]
110  ch_g1 = geom0.dt[ch_key]
111  ch_g2 = geomN.dt[ch_key]
112  else:
113  if len(key) != 4: is_ch = False
114  ch_key = key[:4]
115  g1 = geom0.csc[key]
116  g2 = geomN.csc[key]
117  ch_g1 = geom0.csc[ch_key]
118  ch_g2 = geomN.csc[ch_key]
119  if is_ch: nkeys+=1
120 
121  chWasntMoved = True
122  if ch_key in movedChamberKeys:
123  chWasntMoved = False
124 
125  if g1.relativeto != g2.relativeto:
126  print("%s %s relativeto=\"%s\" versus relativeto=\"%s\"" % (muSystem, str(key), g1.relativeto, g2.relativeto))
127 
128  found = False
129  #r = reports[0]
130  for r in reports:
131  if r.postal_address[1:] == ch_key:
132  found = True
133  rep = r
134  break
135  if not found:
136  if is_ch: print(muSystem, str(key), "not found in the report. Continue...")
137  continue
138  if is_ch: nkeysr+=1
139 
140  if rep.status != "PASS":
141  if is_ch: print(muSystem, str(key), "status is not PASS: %s Continue..." % rep.status)
142  continue
143  #print muSystem, str(key), str(ch_key)
144  if is_ch: nkeyspass+=1
145 
146  ############################################################
147  # CHECKS
148 
149  ok = True
150 
151  if muSystem == "DT" and chWasntMoved:
152 
153  # check that chamber's movement respective to ideal geometry is in reasonable range of 20 mm or 20 mrad:
154  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:
155  ok = False
156  if is_ch:
157  nfail_toideal += 1
158  print("Warning!!!", muSystem, str(key), \
159  "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))
160  if is_ch and ok: nok_toideal +=1
161 
162  # check that movements during the final iteration were negligible
163  # separately for station 4
164  if key[1] != 4 :
165  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:
166  ok = False
167  if is_ch:
168  nfail_deltafinal += 1
169  print("Warning!!!", muSystem, str(key), \
170  "moved too much at final iteration: dx=%.2f mm dy=%.2f mm dphiy=%.2f mrad dphiz=%.2f mrad skipping..." % \
171  (rep.deltax.value*10, rep.deltay.value*10, rep.deltaphiy.value*1000, rep.deltaphiz.value*1000))
172  else:
173  if abs(rep.deltax.value) > 0.03 or abs(rep.deltaphiy.value) > 0.0003 or abs(rep.deltaphiz.value) > 0.0006:
174  ok = False
175  if is_ch:
176  nfail_deltafinal += 1
177  print("Warning!!!", muSystem, str(key), \
178  "moved too much at final iteration: dx=%.2f mm dphiy=%.2f mrad dphiz=%.2f mrad skipping..." % \
179  (rep.deltax.value*10, rep.deltaphiy.value*1000, rep.deltaphiz.value*1000))
180  if is_ch and ok: nok_deltafinal +=1
181 
182  # low statictics check:
183  if rep.deltax.error > 0.5:
184  ok = False
185  if is_ch:
186  nfail_lowstat +=1
187  print("Warning!!!", muSystem, str(key), "low statistics chamber with too big dx.error = %.2f mm skipping..." % (rep.deltax.error*10.))
188  if is_ch and ok: nok_lowstat +=1
189 
190  # N-SIGMA MOTION POLICY CHECK
191  #print "%s %s position difference: %g - %g = %g --> %g, nsigma: %g)" % (
192  # muSystem, str(key), g1.x, g2.x, g1.x - g2.x, abs(g1.x - g2.x)/rep.deltax.error, theNSigma * rep.deltax.error)
193  if abs(ch_g1.x - ch_g2.x) < theNSigma * math.sqrt ( rep.deltax.error*rep.deltax.error + 0.02*0.02 ) :
194  ok = False
195  if is_ch:
196  nfail_nsigma += 1
197  print(muSystem, str(key), "not moved: xN-x0 = %.3f - %.3f = %.3f < %.3f mm" % \
198  ( 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.))
199 
200  if ok: chWasntMoved = False
201 
202  if not ok or chWasntMoved: continue
203 
204  ############################################################
205  # MOTION
206  if is_ch:
207  movedChamberKeys.append(ch_key)
208  print(muSystem, str(key), "moved!")
209  nmoved+=1
210  #move this chamber/superlayer/layer:
211  if muSystem == "DT":
212  geom0.dt[key] = copy.copy(geomN.dt[key])
213  else:
214  geom0.csc[key] = copy.copy(geomN.csc[key])
215 
216  nnotmoved = nkeys - nmoved
217  nsig = int(theNSigma)
218  print("""
219 %(muSystem)s REPORT for %(nsig)d sigma policy:
220 Cumulative counters:
221  %(nkeys)d\t chambers in geometry
222  %(nkeysr)d\t chambers in report
223  %(nkeyspass)d\t have PASS status
224  %(nok_toideal)d\t pass big shift with respect to ideal check
225  %(nok_deltafinal)d\t pass big deltas during final iteration
226  %(nok_lowstat)d\t pass low statistics (or big deltax.error) check
227  %(nmoved)d\t moved
228 Not moved counters:
229  %(nnotmoved)d\t chambers not moved
230  Numbers of chambers were not moved due to:
231  %(nfail_toideal)d\t big shift with respect to ideal
232  %(nfail_deltafinal)d\t big deltas during final iteration
233  %(nfail_lowstat)d\t low statistics (or big deltax.error)
234  %(nfail_nsigma)d\t |x_final - x_initial| < nsigma
235 """ % vars())
236 
237 if DO_DT: loopover("DT")
238 if DO_CSC: loopover("CSC")
239 
240 geom0.xml(file(theOutXML, "w"))
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:65
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
#define str(s)