CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_2_9_HLT1_bphpatch4/src/Alignment/MuonAlignmentAlgorithms/scripts/alignmentValidation.py

Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 
00003 import re,os,sys,shutil
00004 import optparse
00005 
00006 from mutypes import *
00007 
00008 execfile("plotscripts.py")
00009 
00010 ROOT.gROOT.SetBatch(1);
00011 
00012 ######################################################
00013 
00014 ######################################################
00015 # To parse commandline args
00016 
00017 usage='%prog [options]\n'+\
00018   'This script dumps muon alignment validation plots '+\
00019   '(see https://twiki.cern.ch/twiki/bin/viewauth/CMS/SWGuideMuonAlignValidationPlots) '+\
00020   'in web-friendly png format into a predefined directory structure '+\
00021   'and also (to be implemented) does some quality checks for these plots.\n'+\
00022   'Script uses output of the first and last iterations of muon alignment runs '+\
00023   'that should be located in inputDir+i1 and inputDir+iN directories (see options descriptions). '+\
00024   'Each of these directories should contain\ni#prefix+"_plotting.root"\ni#prefix+".root"\ni#prefix+"_report.py"\n'+\
00025   'files, where # = 1 or N.\n'+\
00026   'Output will be located in outputDir, which must exist. Plots from the first and last iterations '+\
00027   'will be in outputDir+"iter1" and outputDir+"iterN" respectively, and plots relevant for all iterations will '+\
00028   'reside in outputDir+"common/".\n'+\
00029   'For a first run a --createDirSructure option must be present. If this option is present for subsequent runs '+\
00030   'with the same outputDir, all previous results in "iter1", "iterN" and and "common" would be deleted!\n'+\
00031   'If neither --dt or --csc is present, plots for both systems will be created.\n'+\
00032   'Options must include either -a or any of the following: --map, --segdiff, --fit, --median'
00033 
00034 parser=optparse.OptionParser(usage)
00035 
00036 parser.add_option("-l", "--runLabel",
00037   help="[REQUIRED] label to use for a run",
00038   type="string",
00039   default='',
00040   dest="runLabel")
00041 
00042 parser.add_option("-i", "--inputDir",
00043   help="[REQUIRED] input directory: should contain directories with the first and the final iterations' results",
00044   type="string",
00045   default='',
00046   dest="inputDir")
00047 
00048 parser.add_option("--i1",
00049   help="[REQUIRED] directory with the alignment 1st iteration's results relative to inputDir",
00050   type="string",
00051   default='',
00052   dest="i1")
00053 
00054 parser.add_option("--iN",
00055   help="[REQUIRED] directory with the alignment last iteration's results relative to inputDir",
00056   type="string",
00057   default='',
00058   dest="iN")
00059 
00060 parser.add_option("--i1prefix",
00061   help="filename prefix for the alignment 1st iteration's results. If not provided, i1prefix = i1",
00062   type="string",
00063   default='',
00064   dest="i1prefix")
00065 
00066 parser.add_option("--iNprefix",
00067   help="filename prefix for the alignment last iteration's results. If not provided, iNprefix = iN",
00068   type="string",
00069   default='',
00070   dest="iNprefix")
00071 
00072 parser.add_option("-o", "--outputDir",
00073   help="output directory: all plots will be saved with relation to outputDir. If not provided, consider outputDir = inputDir",
00074   type="string",
00075   default='',
00076   dest="outputDir")
00077 
00078 parser.add_option("--createDirSructure",
00079   help="If present, new directory structure for storing plots will be first created for each iteration at outputDir+i1 and outputDir+iN. WARNING: this will delete any existing results!",
00080   action="store_true",
00081   default=False,
00082   dest="createDirSructure")
00083 
00084 parser.add_option("--dt",
00085   help="If it is present, but not --csc, DT only plots will be created",
00086   action="store_true",
00087   default=False,
00088   dest="dt")
00089 
00090 parser.add_option("--csc",
00091   help="If this is present, but not --dt, CSC only plots will be created",
00092   action="store_true",
00093   default=False,
00094   dest="csc")
00095 
00096 parser.add_option("-a","--all",
00097   help="If present, all types of plots will be created",
00098   action="store_true",
00099   default=False,
00100   dest="all")
00101 
00102 parser.add_option("--map",
00103   help="If present, map plots will be created",
00104   action="store_true",
00105   default=False,
00106   dest="map")
00107 
00108 parser.add_option("--segdiff",
00109   help="If present, segdiff plots will be created",
00110   action="store_true",
00111   default=False,
00112   dest="segdiff")
00113 
00114 parser.add_option("--curvature",
00115   help="If present, curvature plots will be created",
00116   action="store_true",
00117   default=False,
00118   dest="curvature")
00119 
00120 parser.add_option("--fit",
00121   help="If present, fit functions plots will be created",
00122   action="store_true",
00123   default=False,
00124   dest="fit")
00125 
00126 parser.add_option("--median",
00127   help="If present, median plots will be created",
00128   action="store_true",
00129   default=False,
00130   dest="median")
00131 
00132 parser.add_option("--diagnostic",
00133   help="If present, will run diagnostic checks",
00134   action="store_true",
00135   default=False,
00136   dest="diagnostic")
00137 
00138 parser.add_option("-v", "--verbose",
00139   help="Degree of debug info verbosity",
00140   type="int",
00141   default=0,
00142   dest="verbose")
00143 
00144 options,args=parser.parse_args()
00145 
00146 if options.runLabel=='' or options.inputDir=='' or options.i1=='' or options.iN=='':
00147   print "\nOne or more of REQUIRED options is missing!\n"
00148   parser.print_help()
00149   # See \n"+sys.argv[0]+" --help"
00150   sys.exit()
00151 
00152 outdir = options.outputDir
00153 if outdir=='': outdir = options.inputDir
00154 
00155 i1prefix = options.i1prefix
00156 if i1prefix=='' : i1prefix = options.i1
00157 
00158 iNprefix = options.iNprefix
00159 if iNprefix=='' : iNprefix = options.iN
00160 
00161 if not os.access(outdir,os.F_OK):
00162   print "\noutDir = "+outdir+"\ndoes not exist! Exiting..."
00163   sys.exit()
00164 
00165 # If neither --dt or --csc is present, plots for both systems will be created
00166 DO_DT  = False
00167 DO_CSC = False
00168 if options.dt or not ( options.dt or options.csc):
00169   DO_DT = True
00170 if options.csc or not ( options.dt or options.csc):
00171   DO_CSC = True
00172 
00173 if not (options.all or options.map or options.curvature or options.segdiff or options.fit or options.median or options.diagnostic):
00174   print "\nOptions must include either -a or any of the following: --map, --segdiff, --fit, --median, --diagnostic. Exiting..."
00175   sys.exit()
00176 
00177 SINGLE_ITERATION = False
00178 if i1prefix == iNprefix: SINGLE_ITERATION = True
00179 
00180 DO_MAP = False
00181 DO_SEGDIFF = False
00182 DO_CURVATURE = False
00183 DO_FIT = False
00184 DO_MEDIAN = False
00185 if options.map or options.all:
00186   DO_MAP = True
00187 if options.segdiff or options.all:
00188   DO_SEGDIFF = True
00189 if options.curvature or options.all:
00190   DO_CURVATURE = True
00191 if options.fit or options.all:
00192   DO_FIT = True
00193 if options.median or options.all:
00194   DO_MEDIAN = True
00195 
00196 DO_DIAGNOSTIC = options.diagnostic
00197 
00198 allOptions = "-l "+options.runLabel+" -i "+options.inputDir+" --i1 "+options.i1+" --iN "+options.iN
00199 if options.i1prefix !='': allOptions += " --i1prefix " + options.i1prefix
00200 if options.iNprefix !='': allOptions += " --iNprefix " + options.iNprefix
00201 allOptions += " -o "+options.outputDir
00202 if options.createDirSructure: allOptions += " --createDirSructure"
00203 if DO_DT: allOptions += " --dt"
00204 if DO_CSC: allOptions += " --csc"
00205 if options.all: allOptions += " -a"
00206 if options.map: allOptions += " --map"
00207 if options.segdiff: allOptions += " --segdiff"
00208 if options.curvature: allOptions += " --curvature"
00209 if options.fit: allOptions += " --fit"
00210 if options.median: allOptions += " --median"
00211 if options.diagnostic: allOptions += " --diagnostic"
00212 print sys.argv[0]+" "+allOptions
00213 
00214 
00215 QUICKTESTN=10000
00216 
00217 
00218 
00219 ######################################################
00220 
00221 # template for canvases list
00222 CANVASES_LIST_TEMPLATE = [
00223 ['Common',' ',
00224  ['medians distribution','medians.png']
00225 ],
00226 ['DT',' ',
00227  ['Wheel&Station: map of dxdz residual vs phi','map_DTvsphi_dxdz.png'],
00228  ['Wheel&Station: map of dydz residual vs phi','map_DTvsphi_dydz.png'],
00229  ['Wheel&Station: map of x residual vs phi','map_DTvsphi_x.png'],
00230  ['Wheel&Station: map of y residual vs phi','map_DTvsphi_y.png'],
00231  ['Station&Sector: map of dxdz residual vs z','map_DTvsz_dxdz.png'],
00232  ['Station&Sector: map of dydz residual vs z','map_DTvsz_dydz.png'],
00233  ['Station&Sector: map of x residual vs z','map_DTvsz_x.png'],
00234  ['Station&Sector: map of y residual vs z','map_DTvsz_y.png'],
00235  ['Wheel: segdiff in x residuals vs phi','segdifphi_dt13_resid.png'],
00236  ['Wheel: segdiff in dxdz residuals vs phi','segdifphi_dt13_slope.png'],
00237  ['Wheel: segdiff in y residuals vs phi','segdifphi_dt2_resid.png'],
00238  ['Wheel: segdiff in dydz residuals vs phi','segdifphi_dt2_slope.png'],
00239  ['Chamber: segdiff in x residuals','segdif_dt13_resid.png'],
00240  ['Chamber: segdiff in dxdz residuals','segdif_dt13_slope.png'],
00241  ['Chamber: segdiff in y residuals','segdif_dt2_resid.png'],
00242  ['Chamber: segdiff in dydz residuals','segdif_dt2_slope.png'],
00243  ['Chamber: residuals distributions','dt_bellcurves.png'],
00244  ['Chamber: residuals relations to misalignments','dt_polynomials.png'],
00245  ['Chamber: Delta x residuals vs. curvature','dt_curvature_deltax.png'],
00246  ['Chamber: Delta dxdz residuals vs. curvature','dt_curvature_deltadxdz.png']
00247 ],
00248 ['CSC',' ',
00249  ['Station&Ring: map of d(rphi)/dz residual vs phi','map_CSCvsphi_dxdz.png'],
00250  ['Station&Ring: map of rphi residual vs phi','map_CSCvsphi_x.png'],
00251  ['Station&Chamber: map of d(rphi)/dz residual vs r','map_CSCvsr_dxdz.png'],
00252  ['Station&Chamber: map of rphi residual vs r','map_CSCvsr_x.png'],
00253  ['Chamber: segdiff in rphi residuals','segdif_csc_resid.png'],
00254  ['Chamber: segdiff in d(rphi)/dz residuals','segdif_csc_slope.png'],
00255  ['Chamber: residuals distributions','csc_bellcurves.png'],
00256  ['Chamber: residuals relations to misalignments','csc_polynomials.png']
00257 ]
00258 ]
00259 
00260 
00261 ######################################################
00262 # functions definitions
00263 
00264 
00265 def isFileUnderDir(dir_name, file_name):
00266   '''Recursively looks for file named file_name under dir_name directory
00267   '''
00268   if not DO_DT and dir_name.find("MB")>-1:
00269     return False
00270   if not DO_CSC and dir_name.find("ME")>-1:
00271     return False
00272   
00273   for f in os.listdir(dir_name):
00274     dirfile = os.path.join(dir_name, f)
00275     if os.path.isfile(dirfile) and f==file_name:
00276       return True
00277     # recursively access file names in subdirectories
00278     elif os.path.isdir(dirfile):
00279       #print "Accessing directory:", dirfile
00280       if isFileUnderDir(dirfile, file_name): return True
00281   return False
00282 
00283 
00284 # to time saving of plots
00285 def saveAs(nm): 
00286   t1 = time.time()
00287   ddt[15] += 1
00288   c1.SaveAs(nm)
00289   tn = time.time()
00290   ddt[16] = 1./ddt[15]*((ddt[15]-1)*ddt[16] + tn-t1)
00291 
00292 
00293 def createDirectoryStructure(iteration_name):
00294   
00295   if not os.access(iteration_name,os.F_OK):
00296     os.mkdir(iteration_name)
00297 
00298   csc_basedir = iteration_name+'/'
00299   for endcap in CSC_TYPES:
00300     #print csc_basedir+endcap[0]
00301     shutil.rmtree(csc_basedir+endcap[0],True)
00302     os.mkdir(csc_basedir+endcap[0])
00303     for station in endcap[2]:
00304       #print csc_basedir+endcap[0]+'/'+station[1]
00305       os.mkdir(csc_basedir+endcap[0]+'/'+station[1])
00306       for ring in station[2]:
00307         #print csc_basedir+endcap[0]+'/'+station[1]+'/'+ring[1]
00308         os.mkdir(csc_basedir+endcap[0]+'/'+station[1]+'/'+ring[1])
00309         for chamber in range(1,ring[2]+1):
00310           schamber = "%02d" % chamber
00311           #print csc_basedir+endcap[0]+'/'+station[1]+'/'+ring[1]+'/'+schamber
00312           os.mkdir(csc_basedir+endcap[0]+'/'+station[1]+'/'+ring[1]+'/'+schamber)
00313 
00314   dt_basedir = iteration_name+'/MB/'
00315   #print dt_basedir
00316   shutil.rmtree(dt_basedir,True)
00317   os.mkdir(dt_basedir)
00318   for wheel in DT_TYPES:
00319     #print dt_basedir+wheel[0]
00320     os.mkdir(dt_basedir+wheel[0])
00321     for station in wheel[2]:
00322       #print dt_basedir+wheel[0]+'/'+station[1]
00323       os.mkdir(dt_basedir+wheel[0]+'/'+station[1])
00324       for sector in range(1,station[2]+1):
00325         ssector = "%02d" % sector
00326         #print dt_basedir+wheel[0]+'/'+station[1]+'/'+ssector
00327         os.mkdir(dt_basedir+wheel[0]+'/'+station[1]+'/'+ssector)
00328 
00329   print os.getcwd()
00330 
00331 ######################################################
00332 
00333 def doMapPlotsDT(dt_basedir, tfiles_plotting):
00334   """write DT map plots
00335 
00336  "DTvsphi_st%dwh%s" % (station, wheelletter):
00337 
00338   plots "integrated" over ALL SECTORS:
00339   of x, y, dxdz, dydz vs. phi (y and dydz only for stations 1-3)
00340   made for all (station,wheel) combinations
00341 
00342   Access interface may be arranged into station(1 .. 4) vs. wheel(-2 .. +2) map.
00343   It could be incorporated into a general DT chambers map (column1: wheel, column2: station,
00344   columns3-16 correspond to sector #) by making station numbers in column 2 clickable.
00345   
00346 
00347  "DTvsz_st%dsec%02d" % (station, sector)
00348 
00349   plots "integrated" over ALL WHEELS:
00350   of x, y, dxdz, dydz vs. z (y and dydz only for stations 1-3)
00351   made for all (station,sector) combinations
00352 
00353   Interface: may be arranged into station(1 .. 4) vs. sector(1 .. 14) map with sector range
00354   (1 .. 12) for stations 1-3.
00355   It could be incorporated into an EXTENDED general DT chambers map (extended by adding an
00356   identifier "ALL" in column1 for wheel number)."""
00357 
00358   for wheel in DT_TYPES:
00359     if wheel[1]=="ALL": continue
00360     for station in wheel[2]:
00361       pdir = dt_basedir+'/'+wheel[0]+'/'+station[1]+'/'
00362       label = "DTvsphi_st%dwh%s" % (int(station[1]), wheelLetter(int(wheel[1])))
00363       htitle = "wheel %+d, station %s" % (int(wheel[1]), station[1])
00364       mapplot(tfiles_plotting, label, "x", window=15., title=htitle, fitsawteeth=True,fitsine=True)
00365       c1.SaveAs(pdir+'map_DTvsphi_x.png')
00366       #mapplot(tfiles_plotting, label, "dxdz", window=15., title=htitle, fitsawteeth=True,fitsine=True)
00367       mapplot(tfiles_plotting, label, "dxdz", window=15., title=htitle)
00368       c1.SaveAs(pdir+'map_DTvsphi_dxdz.png')
00369 
00370       if station[1]=='4': continue
00371       #mapplot(tfiles_plotting, label, "y", window=15., title=htitle, fitsawteeth=True,fitsine=True)
00372       mapplot(tfiles_plotting, label, "y", window=15., title=htitle)
00373       c1.SaveAs(pdir+'map_DTvsphi_y.png')
00374       #mapplot(tfiles_plotting, label, "dydz", window=15., title=htitle, fitsawteeth=True,fitsine=True)
00375       mapplot(tfiles_plotting, label, "dydz", window=15., title=htitle)
00376       c1.SaveAs(pdir+'map_DTvsphi_dydz.png')
00377 
00378   qcount=0
00379   for wheel in DT_TYPES:
00380     if wheel[1]!="ALL": continue
00381     for station in wheel[2]:
00382       for sector in range(1,station[2]+1):
00383         if qcount>QUICKTESTN: break
00384         qcount += 1
00385         ssector = "%02d" % sector
00386         pdir = dt_basedir+'/'+wheel[0]+'/'+station[1]+'/'+ssector+'/'
00387         label = "DTvsz_st%ssec%s" % (station[1], ssector)
00388         htitle = "station %s, sector %d" % (station[1], sector)
00389         mapplot(tfiles_plotting, label, "x", window=15., title=htitle)
00390         c1.SaveAs(pdir+'map_DTvsz_x.png')
00391         mapplot(tfiles_plotting, label, "dxdz", window=15., title=htitle)
00392         c1.SaveAs(pdir+'map_DTvsz_dxdz.png')
00393         
00394         if station[1]=='4': continue
00395         mapplot(tfiles_plotting, label, "y", window=15., title=htitle)
00396         c1.SaveAs(pdir+'map_DTvsz_y.png')
00397         mapplot(tfiles_plotting, label, "dydz", window=15., title=htitle)
00398         c1.SaveAs(pdir+'map_DTvsz_dydz.png')
00399   
00400   saveTestResultsMap(options.runLabel)
00401 
00402 
00403 def doMapPlotsCSC(csc_basedir, tfiles_plotting):
00404   """write CSC map plots
00405 
00406  "CSCvsphi_me%s%d%d" % (endcap, station, ring)
00407 
00408   plots "integrated" over ALL SECTORS:
00409   of rphi, drphi/dz vs. phi
00410   made for all (endcap,station,ring) combinations
00411 
00412   Interface: may be arranged into two station(1 .. 4) vs. R(1 .. 4) maps for both endcaps
00413   with R range (1 .. 4) for stations 2-4
00414  It could be incorporated into a general CSC chambers map (column1: endcap, column2: station,
00415   column3: ring, columns4-40 correspond to chamber #) by making ring numbers in column 3
00416   clickable.
00417 
00418 
00419  "CSCvsr_me%s%dch%02d" % (endcap, station, chamberNumber)
00420 
00421   plots "integrated" over ALL RINGS:
00422   of rphi, drphi/dz vs. z
00423   made for all (endcap,station,chamber) combinations
00424 
00425   Interface: may be arranged into two station(1 .. 4) vs. chamber(1 .. 36) maps for both endcaps
00426   It could be incorporated into an EXTENDED general CSC chambers map (extended by adding an
00427   identifier "ALL" in column3 for ring number)."""
00428 
00429   for endcap in CSC_TYPES:
00430     for station in endcap[2]:
00431       for ring in station[2]:
00432         if ring[1]=="ALL": continue
00433         pdir = csc_basedir+'/'+endcap[0]+'/'+station[1]+'/'+ring[1]+'/'
00434         label = "CSCvsphi_me%s%s%s" % (endcap[1], station[1], ring[1])
00435         htitle = "%s%s/%s" % (endcap[0], station[1],ring[1])
00436         mapplot(tfiles_plotting, label, "x", window=15., title=htitle, fitsine=True)
00437         c1.SaveAs(pdir+'map_CSCvsphi_x.png')
00438         mapplot(tfiles_plotting, label, "dxdz", window=15., title=htitle)
00439         c1.SaveAs(pdir+'map_CSCvsphi_dxdz.png')
00440 
00441   saveTestResultsMap(options.runLabel)
00442 
00443   qcount = 0
00444   for endcap in CSC_TYPES:
00445     for station in endcap[2]:
00446       for ring in station[2]:
00447         if ring[1]!="ALL": continue
00448         for chamber in range(1,ring[2]+1):
00449           if qcount>QUICKTESTN: break
00450           qcount += 1
00451           schamber = "%02d" % chamber
00452           pdir = csc_basedir+'/'+endcap[0]+'/'+station[1]+'/'+ring[1]+'/'+schamber+'/'
00453           label = "CSCvsr_me%s%sch%s" % (endcap[1], station[1], schamber)
00454           htitle = "%s%s/ALL/%d" % (endcap[0], station[1],chamber)
00455           mapplot(tfiles_plotting, label, "x", window=15., title=htitle)
00456           c1.SaveAs(pdir+'map_CSCvsr_x.png')
00457           mapplot(tfiles_plotting, label, "dxdz", window=15., title=htitle)
00458           c1.SaveAs(pdir+'map_CSCvsr_dxdz.png')
00459 
00460 
00461 def doCurvaturePlotsDT(dt_basedir, tfiles_plotting):
00462   """write DT curvature plots
00463 
00464   "wheel%s_sector%s" % (wheel, sector)
00465 
00466   wheel in "m2", "m1", "z", "p1", "p2"
00467   station 1 only!
00468   sector in "01", ..., "12"
00469 
00470   "param" may be one of 
00471     "deltax" (Delta x position residuals),
00472     "deltadxdz" (Delta (dx/dz) angular residuals),
00473     "curverr" (Delta x * d(Delta q/pT)/d(Delta x) = Delta q/pT in the absence of misalignment) - not necessary
00474 
00475   made for all (wheel,station=1,sector) combinations
00476 
00477   Interface: could be accesses through a general DT chambers map for station=1 chambers."""
00478   
00479   w_dict = {'-2':'m2', '-1':'m1', '0':'z', '1':'p1', '2':'p2'}
00480   qcount = 0
00481   for wheel in DT_TYPES:
00482     if wheel[1]=="ALL": continue
00483     #station = 1
00484     station = wheel[2][0]
00485     print wheel[0]+'/'+station[1]
00486     for sector in range(1,station[2]+1):
00487       if sector>12: break
00488       if qcount>QUICKTESTN: break
00489       qcount += 1
00490       ssector = "%02d" % sector
00491       pdir = dt_basedir+'/'+wheel[0]+'/'+station[1]+'/'+ssector+'/'
00492       label = "wheel%s_sector%s" % (w_dict[wheel[1]], ssector)
00493       thetitle ="Wheel %s, sector %s" % (wheel[1], ssector)
00494       curvatureplot(tfiles_plotting, label, "deltax", title=thetitle, window=15., fitline=True)
00495       saveAs(pdir+'dt_curvature_deltax.png')
00496       curvatureplot(tfiles_plotting, label, "deltadxdz", title=thetitle, window=15., fitline=True)
00497       saveAs(pdir+'dt_curvature_deltadxdz.png')
00498 
00499 
00500 def doSegDiffPlotsDT(dt_basedir, tfiles_plotting, iter_reports):
00501   """write segment-difference plots for DT
00502 
00503  segdiff "dt13_resid" and "dt13_slope"
00504 
00505   set of plots of
00506   x vs qpt, x for positive, x for negative ("dt13_resid")
00507   dxdz vs qpt, dxdz for positive, dxdz for negative ("dt13_slope")
00508   done for MB1-MB2, MB2-MB3, and MB3-MB4 stations combinations with all possible (wheel, sector)
00509 
00510   Interface: could be accessed through a general DT chambers map, but only for chambers in
00511   stations 2-4 (e.g., station 2 would provide MB1-MB2 plots).
00512 
00513  segdiff "dt2_resid" and "dt2_slope"
00514 
00515   set of plots of
00516   y vs q/pt, y for positive, y for negative ("dt2_resid")
00517   dydz vs q/pt, dydz for positive, dydz for negative ("dt2_slope")
00518   done for MB1-MB2, MB2-MB3 stations combinations with all possible (wheel, sector)
00519 
00520   Interface: then the interface would still be a general DT map,
00521   but the info only available from station 2 & 3 chambers."""
00522 
00523   qcount = 0
00524   for iwheel in DT_TYPES:
00525     if iwheel[1]=="ALL": continue
00526     for istation in iwheel[2]:
00527       if istation[1]=="1": continue
00528       dstations = (int(istation[1])-1)*10 + int(istation[1])
00529       #print dstations
00530       for isector in range(1, istation[2] + 1):
00531         if isector > 12: continue
00532         if qcount>QUICKTESTN: break
00533         qcount += 1
00534         ssector = "%02d" % isector
00535         pdir = dt_basedir + '/' + iwheel[0] + '/' + istation[1] + '/' + ssector + '/'
00536         
00537         segdiff(tfiles_plotting, "dt13_resid", dstations, wheel=int(iwheel[1]), sector=isector, window=10.)
00538         c1.SaveAs(pdir + 'segdif_dt13_resid.png')
00539         segdiff(tfiles_plotting, "dt13_slope", dstations, wheel=int(iwheel[1]), sector=isector, window=10.)
00540         c1.SaveAs(pdir + 'segdif_dt13_slope.png')
00541         
00542         if istation[1] == '4': continue
00543         segdiff(tfiles_plotting, "dt2_resid", dstations, wheel=int(iwheel[1]), sector=isector, window=10.)
00544         c1.SaveAs(pdir + 'segdif_dt2_resid.png')
00545         segdiff(tfiles_plotting, "dt2_slope", dstations, wheel=int(iwheel[1]), sector=isector, window=10.)
00546         c1.SaveAs(pdir + 'segdif_dt2_slope.png')
00547 
00548   """segdiffvsphi "dt13_resid" and "dt13_slope"
00549 
00550   plot for a specific wheel #:
00551   x vs phi of pair ("dt13_resid")
00552   dxdz vs phi of pair ("dt13_slope")
00553   contains all three combinations of neighboring stations
00554   made for all possible wheel values
00555 
00556   Interface: could be accessed by clicking on wheel number under the "wheel" column
00557   in a general DT map
00558 
00559  segdiffvsphi "dt2_resid" and "dt2_slope"
00560 
00561   plot for a specific wheel #:
00562   y vs phi of pair ("dt2_resid")
00563   dydz vs phi of pair ("dt2_slope")
00564   contains both MB1-MB2 and MB2-MB3 combinations
00565   made for all possible wheel values
00566 
00567   Interface: could be accessed by clicking on wheel number under the "wheel" column
00568   in a general DT map"""
00569 
00570   if len(iter_reports)==0: return
00571   
00572   for iwheel in DT_TYPES:
00573     if iwheel[1]=="ALL": continue
00574     pdir = dt_basedir + '/' + iwheel[0] + '/'
00575     segdiffvsphi(tfiles_plotting, iter_reports, "dt13_resid", int(iwheel[1]), window=15., excludesectors=(1,7))
00576     c1.SaveAs(pdir + 'segdifphi_dt13_resid.png')
00577     segdiffvsphi(tfiles_plotting, iter_reports, "dt13_slope", int(iwheel[1]), window=15., excludesectors=(1,7))
00578     c1.SaveAs(pdir + 'segdifphi_dt13_slope.png')
00579     segdiffvsphi(tfiles_plotting, iter_reports, "dt2_resid", int(iwheel[1]), window=15., excludesectors=(1,7))
00580     c1.SaveAs(pdir + 'segdifphi_dt2_resid.png')
00581     segdiffvsphi(tfiles_plotting, iter_reports, "dt2_slope", int(iwheel[1]), window=15., excludesectors=(1,7))
00582     c1.SaveAs(pdir + 'segdifphi_dt2_slope.png')
00583 
00584 
00585 def doSegDiffPlotsCSC(csc_basedir, tfiles_plotting, iter_reports):
00586   """write segment-difference plots for CSC
00587  
00588  segdiff "csc_resid" and "csc_slope"
00589 
00590   set of plots of
00591   rphi vs qpt, rphi for positive, rphi for negative ("csc_resid")
00592   drphidz vs qpt, drphidz for positive, drphidz for negative ("csc_slope")
00593   done for ME1-ME2, ME2-ME3, and ME3-ME4 stations combinations with
00594     endcap "m" or "p" 
00595     ring 1 or 2
00596     chamber 1-18 (r1) or 1-36 (r2)
00597   note: there's no ME3-ME4 plots for R2
00598   
00599   Interface: could be accessed through a general CSC chambers map, but only for chambers in
00600   stations 2-4 (e.g., station 2 would provide ME1-ME2 plots)."""
00601 
00602   qcount = 0
00603   for iendcap in CSC_TYPES:
00604     for istation in iendcap[2]:
00605       if istation[1]=="1": continue
00606       dstations = (int(istation[1])-1)*10 + int(istation[1])
00607       for iring in istation[2]:
00608         if iring[1]=="ALL": continue
00609         if istation[1]=="4" and iring[1]=="2": continue
00610         for ichamber in range(1,iring[2]+1):
00611           if qcount>QUICKTESTN: break
00612           qcount += 1
00613           schamber = "%02d" % ichamber
00614           pdir = csc_basedir+'/'+iendcap[0]+'/'+istation[1]+'/'+iring[1]+'/'+schamber+'/'
00615           segdiff(tfiles_plotting, "csc_resid", dstations, 
00616                   endcap=iendcap[1], ring=int(iring[1]), chamber=ichamber, window=10.)
00617           c1.SaveAs(pdir + 'segdif_csc_resid.png')
00618           segdiff(tfiles_plotting, "csc_slope", dstations, 
00619                   endcap=iendcap[1], ring=int(iring[1]), chamber=ichamber, window=10.)
00620           c1.SaveAs(pdir + 'segdif_csc_slope.png')
00621 
00622 
00623 def doFitFunctionsPlotsDT(dt_basedir, iter_tfile, iter_reports):
00624   """write fit functions plots for DT
00625 
00626  DT bellcurves and polynomials
00627 
00628   set of plots of bellcurves
00629     x, dxdz, x vs. dxdz (for all 4 stations)
00630     y, dydz, x vs. dxdz (only for stations 1-3?)
00631 
00632   set of plots of polynomials -- for stations 1-3 only??
00633     x vs. xpos,    x vs ypos,    x vs dxdz angle,    x vs dydz angle
00634     y vs. xpos,    y vs ypos,    y vs dxdz angle,    y vs dydz angle
00635     dxdz vs. xpos, dxdz vs ypos, dxdz vs dxdz angle, dxdz vs dydz angle
00636     dydz vs. xpos, dydz vs ypos, dydz vs dxdz angle, dydz vs dydz angle
00637 
00638   set of plots of polynomials -- for station 4 only??
00639     x vs. xpos,    x vs dxdz angle
00640     dxdz vs. xpos, dxdz vs dxdz angle
00641 
00642   made for all (wheel,station,sector) combinations
00643 
00644   Interface: could be accesses through a general DT chambers map."""
00645 
00646   qcount = 0
00647   clearDDT()
00648   for wheel in DT_TYPES:
00649     if wheel[1]=="ALL": continue
00650     for station in wheel[2]:
00651       print wheel[0]+'/'+station[1]
00652       for sector in range(1,station[2]+1):
00653         if qcount>QUICKTESTN: break
00654         qcount += 1
00655         ssector = "%02d" % sector
00656         pdir = dt_basedir+'/'+wheel[0]+'/'+station[1]+'/'+ssector+'/'
00657         label = "MBwh%sst%ssec%s" % (wheelLetter(int(wheel[1])),station[1],ssector)
00658         bellcurves(iter_tfile, iter_reports, label, False)
00659         #c1.SaveAs(pdir+'dt_bellcurves.png')
00660         saveAs(pdir+'dt_bellcurves.png')
00661         polynomials(iter_tfile, iter_reports, label, False)
00662         #c1.SaveAs(pdir+'dt_polynomials.png')
00663         saveAs(pdir+'dt_polynomials.png')
00664   #printDeltaTs()
00665 
00666 
00667 def doFitFunctionsPlotsCSC(csc_basedir, iter_tfile, iter_reports):
00668   """write fit functions plots for CSC
00669 
00670  CSC bellcurves and polynomials
00671 
00672   set of plots of bellcurves
00673     rphi, drphidz, rphi vs. drphidz
00674 
00675   set of plots of polynomials
00676     rphi vs. rphi pos,    rphi vs drphidz angle
00677     drphidz vs. rphi pos, drphidz vs drphidz angle
00678 
00679   made for all (endcap,station,ring,chamber) combinations
00680 
00681   Interface: could be accesses through a general CSC chambers map."""
00682 
00683   qcount = 0
00684   clearDDT()
00685   for endcap in CSC_TYPES:
00686     for station in endcap[2]:
00687       for ring in station[2]:
00688         if ring[1]=="ALL": continue
00689         print endcap[0]+'/'+station[1]+'/'+ring[1]
00690         for chamber in range(1,ring[2]+1):
00691           if qcount>QUICKTESTN: break
00692           qcount += 1
00693           schamber = "%02d" % chamber
00694           pdir = csc_basedir+'/'+endcap[0]+'/'+station[1]+'/'+ring[1]+'/'+schamber+'/'
00695           label = "ME%s%s%s_%s" % (endcap[1], station[1], ring[1], schamber)
00696           bellcurves(iter_tfile, iter_reports, label, False)
00697           #c1.SaveAs(pdir+'csc_bellcurves.png')
00698           saveAs(pdir+'csc_bellcurves.png')
00699           polynomials(iter_tfile, iter_reports, label, False)
00700           #c1.SaveAs(pdir+'csc_polynomials.png')
00701           saveAs(pdir+'csc_polynomials.png')
00702   #printDeltaTs()
00703 
00704 
00705 
00706 def doIterationPlots(iteration_directory, tfiles_plotting, iter_tfile, iter_reports):
00707   dt_basedir = iteration_directory+'/'+'MB'
00708   csc_basedir = iteration_directory+'/'
00709 
00710   if DO_DT and DO_MAP:
00711     doMapPlotsDT(dt_basedir, tfiles_plotting)
00712   if DO_CSC and DO_MAP:
00713     doMapPlotsCSC(csc_basedir, tfiles_plotting)
00714 
00715   if DO_DT and DO_CURVATURE:
00716     doCurvaturePlotsDT(dt_basedir, tfiles_plotting)
00717   #if DO_CSC and DO_CURVATURE:
00718   #  doCurvaturePlotsCSC(csc_basedir, tfiles_plotting)
00719 
00720   if DO_DT and DO_SEGDIFF:
00721     doSegDiffPlotsDT(dt_basedir, tfiles_plotting, iter_reports)
00722   if DO_CSC and DO_SEGDIFF:
00723     doSegDiffPlotsCSC(csc_basedir, tfiles_plotting, iter_reports)
00724 
00725   if DO_DT and DO_FIT:
00726     doFitFunctionsPlotsDT(dt_basedir, iter_tfile, iter_reports)
00727   if DO_CSC and DO_FIT:
00728     doFitFunctionsPlotsCSC(csc_basedir, iter_tfile, iter_reports)
00729 
00730 
00731 def createCanvasesList(fname="canvases_list.js"):
00732   '''Use CANVASES_LIST_TEMPLATE as a template to create a canvases list include for the browser.
00733      Write out only those canvases which have existing filename.png plots.
00734   '''
00735   CANVASES_LIST = []
00736   for scope in CANVASES_LIST_TEMPLATE:
00737     scope_entry = []
00738     if len(scope)>2:
00739       scope_entry = [scope[0],scope[1]]
00740       for canvas_entry in scope[2:]:
00741         if isFileUnderDir("./",canvas_entry[1]):
00742           scope_entry.append(canvas_entry)
00743     CANVASES_LIST.append(scope_entry)
00744 
00745   ff = open(fname,mode="w")
00746   print >>ff, "var CANVASES_LIST = "
00747   json.dump(CANVASES_LIST,ff)
00748   ff.close()
00749 
00750 
00751 def createCanvasToIDList(fname="canvas2id_list.js"):
00752   '''Create a canvas-to-ids list include for the browser.
00753      Write out only those canvases which have existing filename.png plots.
00754   '''
00755   CANVAS2ID_LIST = []
00756   for scope in CANVASES_LIST_TEMPLATE:
00757     if len(scope)>2:
00758       for canvas_entry in scope[2:]:
00759         ids = idsForFile("./",canvas_entry[1])
00760         #  scope_entry.append(canvas_entry)
00761         # uniquify:
00762         set_ids = set(ids)
00763         uids = list(set_ids)
00764         print canvas_entry, ":", len(uids), "ids"
00765         if (len(uids)>0):
00766           CANVAS2ID_LIST.append( (canvas_entry[1],uids) )
00767   #print CANVAS2ID_LIST
00768   CANVAS2ID_LIST_DICT = dict(CANVAS2ID_LIST)
00769   #print CANVAS2ID_LIST_DICT
00770   ff = open(fname,mode="w")
00771   print >>ff, "var CANVAS2ID_LIST = "
00772   json.dump(CANVAS2ID_LIST_DICT,ff)
00773   ff.close()
00774 
00775 def idsForFile(dir_name, file_name):
00776   '''Recursively looks for file named file_name under dir_name directory
00777   and fill the list with dir names converted to IDs 
00778   '''
00779   id_list = []
00780   for f in os.listdir(dir_name):
00781     dirfile = os.path.join(dir_name, f)
00782     if os.path.isfile(dirfile) and f==file_name:
00783       id_list.append(dirToID(dir_name))
00784     # recursively access file names in subdirectories
00785     elif os.path.isdir(dirfile):
00786       #print "Accessing directory:", dirfile
00787       ids = idsForFile(dirfile, file_name)
00788       if (len(ids)>0): 
00789         id_list.extend(ids)
00790   return id_list
00791 
00792 
00793 def dirToID(d):
00794   if d[-1]!='/': d += '/'
00795   dtn = d.find("/MB/")
00796   if dtn!=-1:
00797     return d[dtn+4:-1]
00798   cscn = d.find("/ME-/")
00799   if cscn!=-1:
00800     return 'ME-'+d[cscn+4:-1]
00801   cscn = d.find("/ME+/")
00802   if cscn!=-1:
00803     return 'ME+'+d[cscn+4:-1]
00804   return ''
00805 
00806 
00807 ############################################################################################################
00808 ############################################################################################################
00809 # main script
00810 
00811 # open input files:
00812 
00813 #basedir='/disks/sdb5/home_reloc/khotilov/db/cms/alignment'
00814 #iteration1 = "iteration_01"
00815 #iteration3 = "iteration_03"
00816 #iteration1 = "NOV4DT_PASS3noweight_TkHIP_01"
00817 #iteration3 = "NOV4DT_PASS3noweight_TkHIP_05"
00818 #os.chdir(options.inputDir)
00819 #iteration1 = options.i1
00820 #iteration3 = options.iN
00821 
00822 fname = options.inputDir+'/'+options.i1+'/'+i1prefix
00823 tfiles1_plotting = []
00824 iter1_tfile = None
00825 iter1_reports = []
00826 if not SINGLE_ITERATION:
00827   if (DO_MAP or DO_SEGDIFF or DO_CURVATURE) and not os.access(fname+"_plotting.root",os.F_OK):
00828     print "no file "+fname+"_plotting.root"
00829     sys.exit()
00830   if DO_FIT and not os.access(fname+".root",os.F_OK):
00831     print "no file "+fname+".root"
00832     sys.exit()
00833   if DO_MEDIAN and not os.access(fname+"_report.py",os.F_OK):
00834     print "no file "+fname+"_report.py"
00835     sys.exit()
00836   if DO_MAP or DO_SEGDIFF or DO_CURVATURE: tfiles1_plotting.append(ROOT.TFile(fname+"_plotting.root"))
00837   if os.access(fname+".root",os.F_OK):
00838     iter1_tfile = ROOT.TFile(fname+".root")
00839   if os.access(fname+"_report.py",os.F_OK):
00840     execfile(fname+"_report.py")
00841     iter1_reports = reports
00842 
00843 fname = options.inputDir+'/'+options.iN+'/'+iNprefix
00844 tfilesN_plotting = []
00845 iterN_tfile = None
00846 iterN_reports = []
00847 if (DO_MAP or DO_SEGDIFF or DO_CURVATURE) and not os.access(fname+"_plotting.root",os.F_OK):
00848   print "no file "+fname+"_plotting.root"
00849   sys.exit()
00850 if DO_FIT and not os.access(fname+".root",os.F_OK):
00851   print "no file "+fname+".root"
00852   sys.exit()
00853 if DO_MEDIAN and not os.access(fname+"_report.py",os.F_OK):
00854   print "no file "+fname+"_report.py"
00855   sys.exit()
00856 if DO_MAP or DO_SEGDIFF or DO_CURVATURE: tfilesN_plotting.append(ROOT.TFile(fname+"_plotting.root"))
00857 if os.access(fname+".root",os.F_OK):
00858   iterN_tfile = ROOT.TFile(fname+".root")
00859 if os.access(fname+"_report.py",os.F_OK):
00860   execfile(fname+"_report.py")
00861   iterN_reports = reports
00862 
00863 if DO_MAP:
00864   os.chdir(options.inputDir)
00865   phiedges2c()
00866 
00867 ######################################################
00868 # setup output:
00869 
00870 # cd to outputDIr
00871 os.chdir(options.outputDir)
00872 
00873 comdir = "common/"
00874 iteration1 = "iter1"
00875 iterationN = "iterN"
00876 
00877 # create directory structure
00878 if options.createDirSructure:
00879   print "WARNING: all existing results in "+options.outputDir+" will be deleted!"
00880   if not SINGLE_ITERATION: createDirectoryStructure(iteration1)
00881   createDirectoryStructure(iterationN)
00882   if not os.access(comdir,os.F_OK):
00883     os.mkdir(comdir)
00884 
00885 ######################################################
00886 # do drawing
00887 
00888 c1 = ROOT.TCanvas("c1","c1",800,600)
00889 
00890 set_palette("blues")
00891 
00892 if not SINGLE_ITERATION: doIterationPlots(iteration1, tfiles1_plotting, iter1_tfile, iter1_reports)
00893 doIterationPlots(iterationN, tfilesN_plotting, iterN_tfile, iterN_reports)
00894 
00895 if CPP_LOADED: ROOT.cleanUpHeap()
00896 
00897 # write distributions of medians plots
00898 
00899 if DO_MEDIAN:
00900   plotmedians(iter1_reports, iterN_reports)
00901   c1.SaveAs(comdir+'medians.png')
00902 
00903 # perform diagnostic
00904 if DO_DIAGNOSTIC:
00905   #if not SINGLE_ITERATION: doTests(iter1_reports,"mu_list_1.js","dqm_report_1.js",options.runLabel)
00906   doTests(iterN_reports,"mu_list.js","dqm_report.js",options.runLabel)
00907   createCanvasesList("canvases_list.js")
00908   createCanvasToIDList("canvas2id_list.js")