CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/Alignment/MuonAlignmentAlgorithms/scripts/createTree.py

Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 
00003 import re,os,sys
00004 import optparse
00005 
00006 # python 2.6 has json modue; <2.6 could use simplejson
00007 try:
00008   import json
00009 except ImportError:
00010   import simplejson as json
00011 
00012 from mutypes import * 
00013 
00014 import pprint
00015 pp = pprint.PrettyPrinter(indent=2)
00016 
00017 
00018 NAME_TO_TITLE = {
00019 "map_DTvsphi_dxdz.png" : "map of dxdz residual vs phi",
00020 "map_DTvsphi_dydz.png" : "map of dydz residual vs phi",
00021 "map_DTvsphi_x.png" : "map of x residual vs phi",
00022 "map_DTvsphi_y.png" : "map of y residual vs phi",
00023 "map_DTvsz_dxdz.png" : "map of dxdz residual vs z",
00024 "map_DTvsz_dydz.png" : "map of dydz residual vs z",
00025 "map_DTvsz_x.png" : "map of x residual vs z",
00026 "map_DTvsz_y.png" : "map of y residual vs z",
00027 "map_CSCvsphi_dxdz.png" : "map of d(rphi)/dz residual vs phi",
00028 "map_CSCvsphi_x.png" : "map of rphi residual vs phi",
00029 "map_CSCvsr_dxdz.png" : "map of d(rphi)/dz residual vs r",
00030 "map_CSCvsr_x.png" : "map of rphi residual vs r",
00031 "segdifphi_dt13_resid.png" : "segdiff in x residuals vs phi",
00032 "segdifphi_dt13_slope.png" : "segdiff in dxdz residuals vs phi",
00033 "segdifphi_dt2_resid.png" : "segdiff in y residuals vs phi",
00034 "segdifphi_dt2_slope.png" : "segdiff in dydz residuals vs phi",
00035 "segdif_dt13_resid.png" : "segdiff in x residuals",
00036 "segdif_dt13_slope.png" : "segdiff in dxdz residuals",
00037 "segdif_dt2_resid.png" : "segdiff in y residuals",
00038 "segdif_dt2_slope.png" : "segdiff in dydz residuals",
00039 "segdif_csc_resid.png" : "segdiff in rphi residuals",
00040 "segdif_csc_slope.png" : "segdiff in d(rphi)/dz residuals",
00041 "dt_bellcurves.png" : "residuals distributions",
00042 "dt_polynomials.png" : "residuals relations to misalignments",
00043 "csc_bellcurves.png" : "residuals distributions",
00044 "csc_polynomials.png" : "residuals relations to misalignments",
00045 'dt_curvature_deltax.png' : 'Delta x residuals vs. curvature',
00046 'dt_curvature_deltadxdz.png' : 'Delta dxdz residuals vs. curvature',
00047 "medians.png" : "medians distribution"
00048 }
00049 ######################################################
00050 # functions definitions
00051 
00052 ######################################################
00053 # To parse commandline args
00054 
00055 usage='%prog [options]\n'+\
00056   'Creates a tree_items.js data file for a browsable JavaScript tree using results produced '+\
00057   'by running alignment_validation_plots.py.'
00058 
00059 parser=optparse.OptionParser(usage)
00060 
00061 parser.add_option("-i", "--inputDir",
00062   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",
00063   type="string",
00064   default='',
00065   dest="inputDir")
00066 
00067 parser.add_option("-v", "--verbose",
00068   help="Degree of debug info verbosity",
00069   type="int",
00070   default=0,
00071   dest="verbose")
00072 
00073 options,args=parser.parse_args()
00074 
00075 if options.inputDir=='':
00076   print "\nOne or more of REQUIRED options is missing!\n"
00077   parser.print_help()
00078   # See \n"+sys.argv[0]+" --help"
00079   sys.exit()
00080 
00081 ######################################################
00082 
00083 
00084 
00085 ############################################################################################################
00086 ############################################################################################################
00087 # main script
00088 
00089 # create directory structure
00090 
00091 #basedir='/disks/sdb5/home_reloc/khotilov/db/cms/alignment'
00092 #os.chdir(basedir)
00093 os.chdir(options.inputDir)
00094 
00095 #iteration1 = "iteration_01"
00096 #iteration3 = "iteration_03"
00097 #iteration1 = "NOV4DT_PASS3noweight_TkHIP_01"
00098 #iteration3 = "NOV4DT_PASS3noweight_TkHIP_05"
00099 iteration1 = "iter1"
00100 iterationN = "iterN"
00101 comdir = "common/"
00102 
00103 ######################################################
00104 # open root and py result files
00105 
00106 iteration_directory = iterationN
00107 
00108 
00109 def parseDir(dir,label,it1="",itN=""):
00110   """it1 and itN   are the first and the last iterations' directory names
00111      dir           is some directory with the results from for the LAST 
00112                    iteration, so it must contain a itN substring 
00113      label         is a label for tree's folder for this directory"""
00114   if len(itN)>0 and dir.find(itN)==-1:
00115     print "directory ", dir, "has no ", itN, " in it!!"
00116     return ["problem!!!",""]
00117   res = [label,dir]
00118   files = os.listdir(dir)
00119   files.sort()
00120   for f in files:
00121     if re.match(".+\.png", f):
00122       if len(it1)>0 and len(itN)>0:
00123         lnN = [itN,dir+'/'+f]
00124         dir1 = dir.replace(itN,it1)
00125         if not os.access(dir1+'/'+f,os.F_OK):
00126           print "WARNING: no ",dir1+'/'+f," file found!!!"
00127         ln1 = [it1,dir1+'/'+f]
00128         ln = [NAME_TO_TITLE[f],dir+'/'+f,ln1,lnN]
00129         res.append(ln)
00130       else:
00131         ln = [NAME_TO_TITLE[f],dir+'/'+f]
00132         #print ln
00133         res.append(ln)
00134   #pp.pprint(res)
00135   return res
00136 
00137 
00138 mytree = []
00139 tree_level1 = ['test','']
00140 
00141 # DT
00142 dt_basedir = iteration_directory+'/MB/'
00143 tree_level2 = parseDir(dt_basedir,"MB",iteration1,iterationN)
00144 for wheel in DT_TYPES:
00145   dd = dt_basedir + wheel[0]
00146   print dd
00147   tree_level3 = parseDir(dd,wheel[0],iteration1,iterationN)
00148   for station in wheel[2]:
00149     dd = dt_basedir + wheel[0]+'/'+station[1]
00150     print dd 
00151     tree_level4 = parseDir(dd,station[0],iteration1,iterationN)
00152     for sector in range(1,station[2]+1):
00153       ssector = "%02d" % sector
00154       dd = dt_basedir+wheel[0]+'/'+station[1]+'/'+ssector
00155       #print dd
00156       tree_level5 = parseDir(dd,"%s/%d" % (station[0],sector),iteration1,iterationN)
00157       if len(tree_level5) == 2: tree_level5.append(['none',''])
00158       tree_level4.append(tree_level5)
00159     if len(tree_level4) == 2: tree_level4.append(['none',''])
00160     tree_level3.append(tree_level4)
00161   if len(tree_level3) == 2: tree_level3.append(['none',''])
00162   tree_level2.append(tree_level3)
00163 if len(tree_level2) == 2: tree_level2.append(['none',''])
00164 tree_level1.append(tree_level2)
00165 
00166 # CSC
00167 csc_basedir = iteration_directory+'/'
00168 for endcap in CSC_TYPES:
00169   dd = csc_basedir+endcap[0]
00170   print dd
00171   tree_level2 = parseDir(dd,endcap[0],iteration1,iterationN)
00172   for station in endcap[2]:
00173     dd = csc_basedir+endcap[0]+'/'+station[1]
00174     print dd
00175     tree_level3 = parseDir(dd,station[0],iteration1,iterationN)
00176     for ring in station[2]:
00177       dd = csc_basedir+endcap[0]+'/'+station[1]+'/'+ring[1]
00178       print dd
00179       tree_level4 = parseDir(dd,"%s/%s" % (station[0],ring[1]),iteration1,iterationN)
00180       for chamber in range(1,ring[2]+1):
00181         schamber = "%02d" % chamber
00182         dd = csc_basedir+endcap[0]+'/'+station[1]+'/'+ring[1]+'/'+schamber
00183         #print dd
00184         tree_level5 = parseDir(dd,"%s/%s/%d" % (station[0],ring[1],chamber),iteration1,iterationN)
00185         tree_level4.append(tree_level5)
00186       if len(tree_level4) == 2: tree_level4.append(['none',''])
00187       tree_level3.append(tree_level4)
00188     if len(tree_level3) == 2: tree_level3.append(['none',''])
00189     tree_level2.append(tree_level3)
00190   if len(tree_level2) == 2: tree_level2.append(['none',''])
00191   tree_level1.append(tree_level2)
00192 
00193 # Common plots
00194 common_basedir = comdir
00195 tree_level2 = parseDir(common_basedir,"All")
00196 tree_level1.append(tree_level2)
00197 
00198 
00199 mytree.append(tree_level1)
00200 print " "
00201 #pp.pprint(mytree)
00202 print
00203 
00204 ff = open("tree_items.js",mode="w")
00205 print >>ff, "var TREE_ITEMS = "
00206 json.dump(mytree,ff)
00207 ff.close()