CMS 3D CMS Logo

createTree.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 import re,os,sys
4 import optparse
5 
6 # python 2.6 has json modue; <2.6 could use simplejson
7 try:
8  import json
9 except ImportError:
10  import simplejson as json
11 
12 from mutypes import *
13 
14 import pprint
15 pp = pprint.PrettyPrinter(indent=2)
16 
17 
18 NAME_TO_TITLE = {
19 "map_DTvsphi_dxdz.png" : "map of dxdz residual vs phi",
20 "map_DTvsphi_dydz.png" : "map of dydz residual vs phi",
21 "map_DTvsphi_x.png" : "map of x residual vs phi",
22 "map_DTvsphi_y.png" : "map of y residual vs phi",
23 "map_DTvsz_dxdz.png" : "map of dxdz residual vs z",
24 "map_DTvsz_dydz.png" : "map of dydz residual vs z",
25 "map_DTvsz_x.png" : "map of x residual vs z",
26 "map_DTvsz_y.png" : "map of y residual vs z",
27 "map_DTvsz_all_dxdz.png" : "map of dxdz residual vs z",
28 "map_DTvsz_all_dydz.png" : "map of dydz residual vs z",
29 "map_DTvsz_all_x.png" : "map of x residual vs z",
30 "map_DTvsz_all_y.png" : "map of y residual vs z",
31 "map_CSCvsphi_dxdz.png" : "map of d(rphi)/dz residual vs phi",
32 "map_CSCvsphi_x.png" : "map of rphi residual vs phi",
33 "map_CSCvsr_dxdz.png" : "map of d(rphi)/dz residual vs r",
34 "map_CSCvsr_x.png" : "map of rphi residual vs r",
35 "segdifphi_x_dt_csc_resid.png" : "segdiff DT-CSC in x residuals vs phi",
36 "segdifphi_dt13_resid.png" : "segdiff in x residuals vs phi",
37 "segdifphi_dt13_slope.png" : "segdiff in dxdz residuals vs phi",
38 "segdifphi_dt2_resid.png" : "segdiff in y residuals vs phi",
39 "segdifphi_dt2_slope.png" : "segdiff in dydz residuals vs phi",
40 "segdif_x_dt_csc_resid.png" : "segdiff DT-CSC in x residuals",
41 "segdif_dt13_resid.png" : "segdiff in x residuals",
42 "segdif_dt13_slope.png" : "segdiff in dxdz residuals",
43 "segdif_dt2_resid.png" : "segdiff in y residuals",
44 "segdif_dt2_slope.png" : "segdiff in dydz residuals",
45 "segdifphi_csc_resid.png" : "segdiff in rphi residuals vs phi",
46 "segdifphi_csc_slope.png" : "segdiff in d(rphi)/dz residuals vs phi",
47 "segdif_csc_resid.png" : "segdiff in rphi residuals",
48 "segdif_csc_slope.png" : "segdiff in d(rphi)/dz residuals",
49 "dt_bellcurves.png" : "residuals distributions",
50 "dt_polynomials.png" : "residuals relations to misalignments",
51 "csc_bellcurves.png" : "residuals distributions",
52 "csc_polynomials.png" : "residuals relations to misalignments",
53 'dt_curvature_deltax.png' : 'Delta x residuals vs. curvature',
54 'dt_curvature_deltadxdz.png' : 'Delta dxdz residuals vs. curvature',
55 "medians.png" : "medians distribution"
56 }
57 ######################################################
58 # functions definitions
59 
60 ######################################################
61 # To parse commandline args
62 
63 usage='%prog [options]\n'+\
64  'Creates a tree_items.js data file for a browsable JavaScript tree using results produced '+\
65  'by running alignment_validation_plots.py.'
66 
67 parser=optparse.OptionParser(usage)
68 
69 parser.add_option("-i", "--inputDir",
70  help="[REQUIRED] input directory: should contain 'iter1', 'iterN' and 'common' directories filled with alignment_validation_plots.py. The resulting tree_items.js is also dumped into this directory",
71  type="string",
72  default='',
73  dest="inputDir")
74 
75 parser.add_option("-v", "--verbose",
76  help="Degree of debug info verbosity",
77  type="int",
78  default=0,
79  dest="verbose")
80 
81 options,args=parser.parse_args()
82 
83 if options.inputDir=='':
84  print "\nOne or more of REQUIRED options is missing!\n"
85  parser.print_help()
86  # See \n"+sys.argv[0]+" --help"
87  sys.exit()
88 
89 ######################################################
90 
91 
92 
93 ############################################################################################################
94 ############################################################################################################
95 # main script
96 
97 # create directory structure
98 
99 #basedir='/disks/sdb5/home_reloc/khotilov/db/cms/alignment'
100 #os.chdir(basedir)
101 os.chdir(options.inputDir)
102 
103 #iteration1 = "iteration_01"
104 #iteration3 = "iteration_03"
105 #iteration1 = "NOV4DT_PASS3noweight_TkHIP_01"
106 #iteration3 = "NOV4DT_PASS3noweight_TkHIP_05"
107 iteration1 = "iter1"
108 iterationN = "iterN"
109 comdir = "common/"
110 
111 ######################################################
112 # open root and py result files
113 
114 iteration_directory = iterationN
115 
116 
117 def parseDir(dir,label,it1="",itN=""):
118  """it1 and itN are the first and the last iterations' directory names
119  dir is some directory with the results from for the LAST
120  iteration, so it must contain a itN substring
121  label is a label for tree's folder for this directory"""
122  if len(itN)>0 and dir.find(itN)==-1:
123  print "directory ", dir, "has no ", itN, " in it!!"
124  return ["problem!!!",""]
125  res = [label,dir]
126  files = sorted(os.listdir(dir))
127  for f in files:
128  if re.match(".+\.png", f):
129  if len(it1)>0 and len(itN)>0:
130  lnN = [itN,dir+'/'+f]
131  dir1 = dir.replace(itN,it1)
132  if not os.access(dir1+'/'+f,os.F_OK):
133  print "WARNING: no ",dir1+'/'+f," file found!!!"
134  ln1 = [it1,dir1+'/'+f]
135  ln = [NAME_TO_TITLE[f],dir+'/'+f,ln1,lnN]
136  res.append(ln)
137  else:
138  ln = [NAME_TO_TITLE[f],dir+'/'+f]
139  #print ln
140  res.append(ln)
141  #pp.pprint(res)
142  return res
143 
144 
145 mytree = []
146 tree_level1 = ['test','']
147 
148 # DT
149 dt_basedir = iteration_directory+'/MB/'
150 tree_level2 = parseDir(dt_basedir,"MB",iteration1,iterationN)
151 for wheel in DT_TYPES:
152  dd = dt_basedir + wheel[0]
153  print dd
154  tree_level3 = parseDir(dd,wheel[0],iteration1,iterationN)
155  for station in wheel[2]:
156  dd = dt_basedir + wheel[0]+'/'+station[1]
157  print dd
158  tree_level4 = parseDir(dd,station[0],iteration1,iterationN)
159  for sector in range(1,station[2]+1):
160  ssector = "%02d" % sector
161  dd = dt_basedir+wheel[0]+'/'+station[1]+'/'+ssector
162  #print dd
163  tree_level5 = parseDir(dd,"%s/%d" % (station[0],sector),iteration1,iterationN)
164  if len(tree_level5) == 2: tree_level5.append(['none',''])
165  tree_level4.append(tree_level5)
166  if len(tree_level4) == 2: tree_level4.append(['none',''])
167  tree_level3.append(tree_level4)
168  if len(tree_level3) == 2: tree_level3.append(['none',''])
169  tree_level2.append(tree_level3)
170 if len(tree_level2) == 2: tree_level2.append(['none',''])
171 tree_level1.append(tree_level2)
172 
173 # CSC
174 csc_basedir = iteration_directory+'/'
175 for endcap in CSC_TYPES:
176  dd = csc_basedir+endcap[0]
177  print dd
178  tree_level2 = parseDir(dd,endcap[0],iteration1,iterationN)
179  for station in endcap[2]:
180  dd = csc_basedir+endcap[0]+'/'+station[1]
181  print dd
182  tree_level3 = parseDir(dd,station[0],iteration1,iterationN)
183  for ring in station[2]:
184  dd = csc_basedir+endcap[0]+'/'+station[1]+'/'+ring[1]
185  print dd
186  tree_level4 = parseDir(dd,"%s/%s" % (station[0],ring[1]),iteration1,iterationN)
187  for chamber in range(1,ring[2]+1):
188  schamber = "%02d" % chamber
189  dd = csc_basedir+endcap[0]+'/'+station[1]+'/'+ring[1]+'/'+schamber
190  #print dd
191  tree_level5 = parseDir(dd,"%s/%s/%d" % (station[0],ring[1],chamber),iteration1,iterationN)
192  tree_level4.append(tree_level5)
193  if len(tree_level4) == 2: tree_level4.append(['none',''])
194  tree_level3.append(tree_level4)
195  if len(tree_level3) == 2: tree_level3.append(['none',''])
196  tree_level2.append(tree_level3)
197  if len(tree_level2) == 2: tree_level2.append(['none',''])
198  tree_level1.append(tree_level2)
199 
200 # Common plots
201 common_basedir = comdir
202 tree_level2 = parseDir(common_basedir,"All")
203 tree_level1.append(tree_level2)
204 
205 
206 mytree.append(tree_level1)
207 print " "
208 #pp.pprint(mytree)
209 print
210 
211 ff = open("tree_items.js",mode="w")
212 print >>ff, "var TREE_ITEMS = "
213 json.dump(mytree,ff)
214 ff.close()
def parseDir(dir, label, it1="", itN="")
Definition: createTree.py:117