CMS 3D CMS Logo

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