CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/Alignment/MuonAlignmentAlgorithms/scripts/createBeamHaloJobs.py

Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 import os, sys, re, optparse, math
00004 
00005 copyargs = sys.argv[:]
00006 for i in range(len(copyargs)):
00007     if copyargs[i] == "":
00008         copyargs[i] = "\"\""
00009     if copyargs[i].find(" ") != -1:
00010         copyargs[i] = "\"%s\"" % copyargs[i]
00011 commandline = " ".join(copyargs)
00012 
00013 usage = """./%prog DIRNAME PATTERN INITIALGEOM INPUTFILES [options]
00014 
00015 Creates (overwrites) a directory for each word in PATTERN and creates
00016 (overwrites) submitJobs.sh with the submission sequence and
00017 dependencies.
00018 
00019 DIRNAME        directories will be named DIRNAME01, DIRNAME02, etc.
00020 PATTERN        a quoted combination of "phiy", "phipos", "phiz"
00021 INITIALGEOM    SQLite file containing muon geometry with tag names
00022                CSCAlignmentRcd, CSCAlignmentErrorRcd
00023 INPUTFILES     Python file defining 'fileNames', a list of input files as
00024                strings"""
00025 
00026 parser = optparse.OptionParser(usage)
00027 parser.add_option("-j", "--jobs",
00028                   help="approximate number of \"gather\" subjobs",
00029                   type="int",
00030                   default=50,
00031                   dest="subjobs")
00032 parser.add_option("-s", "--submitJobs",
00033                   help="alternate name of submitJobs.sh script (please include .sh extension); a file with this name will be OVERWRITTEN",
00034                   type="string",
00035                   default="submitJobs.sh",
00036                   dest="submitJobs")
00037 parser.add_option("-b", "--big",
00038                   help="if invoked, subjobs will also be run on cmscaf1nd",
00039                   action="store_true",
00040                   dest="big")
00041 parser.add_option("--globalTag",
00042                   help="GlobalTag for calibration conditions",
00043                   type="string",
00044                   default="GR_R_35X_V8A::All",
00045                   dest="globaltag")
00046 parser.add_option("--photogrammetry",
00047                   help="if invoked, alignment will be constrained to photogrammetry",
00048                   action="store_true",
00049                   dest="photogrammetry")
00050 parser.add_option("--photogrammetryScale",
00051                   help="scale factor for photogrammetry constraint: 1 is default and 10 *weakens* the constraint by a factor of 10",
00052                   type="string",
00053                   default="1.",
00054                   dest="photogrammetryScale")
00055 parser.add_option("--disks",
00056                   help="align whole disks, rather than individual rings",
00057                   action="store_true",
00058                   dest="disks")
00059 
00060 parser.add_option("--minP",
00061                   help="minimum track momentum (measured via radial component of fringe fields)",
00062                   type="string",
00063                   default="5",
00064                   dest="minP")
00065 parser.add_option("--minHitsPerChamber",
00066                   help="minimum number of hits per chamber",
00067                   type="string",
00068                   default="5",
00069                   dest="minHitsPerChamber")
00070 parser.add_option("--maxdrdz",
00071                   help="maximum dr/dz of tracklets (anti-cosmic cut)",
00072                   type="string",
00073                   default="0.2",
00074                   dest="maxdrdz")
00075 parser.add_option("--maxRedChi2",
00076                   help="maximum reduced chi^2 of tracks",
00077                   type="string",
00078                   default="10",
00079                   dest="maxRedChi2")
00080 parser.add_option("--fiducial",
00081                   help="if invoked, select only segments within the good region of the chamber (for all 6 layers)",
00082                   action="store_true",
00083                   dest="fiducial")
00084 parser.add_option("--useHitWeights",
00085                   help="if invoked, use hit weights in tracklet fits",
00086                   action="store_true",
00087                   dest="useHitWeights")
00088 parser.add_option("--truncateSlopeResid",
00089                   help="maximum allowed slope residual in mrad (like the histograms in a phiy job)",
00090                   type="string",
00091                   default="30.",
00092                   dest="truncateSlopeResid")
00093 parser.add_option("--truncateOffsetResid",
00094                   help="maximum allowed offset residual in mm (like the histograms in a phipos or phiz job)",
00095                   type="string",
00096                   default="15.",
00097                   dest="truncateOffsetResid")
00098 parser.add_option("--combineME11",
00099                   help="if invoked, combine ME1/1a and ME1/1b chambers",
00100                   action="store_true",
00101                   dest="combineME11")
00102 parser.add_option("--useTrackWeights",
00103                   help="if invoked, weight residuals by track uncertainties",
00104                   action="store_true",
00105                   dest="useTrackWeights")
00106 parser.add_option("--errorFromRMS",
00107                   help="if invoked, determine residuals uncertainties from the RMS of the residuals distribution",
00108                   action="store_true",
00109                   dest="errorFromRMS")
00110 parser.add_option("--minTracksPerOverlap",
00111                   help="minimum number of tracks needed for an overlap constraint to be valid",
00112                   type="string",
00113                   default="10",
00114                   dest="minTracksPerOverlap")
00115 parser.add_option("--slopeFromTrackRefit",
00116                   help="if invoked, determine direction of tracklets by refitting track to all other stations",
00117                   action="store_true",
00118                   dest="slopeFromTrackRefit")
00119 parser.add_option("--minStationsInTrackRefits",
00120                   help="minimum number of stations in a full track refit (slopeFromTrackRefit)",
00121                   type="string",
00122                   default="2",
00123                   dest="minStationsInTrackRefits")
00124 
00125 if len(sys.argv) < 5:
00126     raise SystemError, "Too few arguments.\n\n"+parser.format_help()
00127 
00128 DIRNAME = sys.argv[1]
00129 PATTERN = re.split("\s+", sys.argv[2])
00130 INITIALGEOM = sys.argv[3]
00131 INPUTFILES = sys.argv[4]
00132 
00133 options, args = parser.parse_args(sys.argv[5:])
00134 globaltag = options.globaltag
00135 photogrammetry = options.photogrammetry
00136 photogrammetryScale = options.photogrammetryScale
00137 disks = options.disks
00138 minP = options.minP
00139 minHitsPerChamber = options.minHitsPerChamber
00140 maxdrdz = options.maxdrdz
00141 maxRedChi2 = options.maxRedChi2
00142 fiducial = options.fiducial
00143 useHitWeights = options.useHitWeights
00144 truncateSlopeResid = options.truncateSlopeResid
00145 truncateOffsetResid = options.truncateOffsetResid
00146 combineME11 = options.combineME11
00147 useTrackWeights = options.useTrackWeights
00148 errorFromRMS = options.errorFromRMS
00149 minTracksPerOverlap = options.minTracksPerOverlap
00150 slopeFromTrackRefit = options.slopeFromTrackRefit
00151 minStationsInTrackRefits = options.minStationsInTrackRefits
00152 
00153 execfile(INPUTFILES)
00154 stepsize = int(math.ceil(1.*len(fileNames)/options.subjobs))
00155 pwd = str(os.getcwdu())
00156 
00157 bsubfile = ["#!/bin/sh", ""]
00158 bsubnames = []
00159 last_align = None
00160 
00161 directory = ""
00162 for i, mode in enumerate(PATTERN):
00163     iteration = i+1
00164     if iteration == 1:
00165         inputdb = INITIALGEOM
00166         inputdbdir = directory[:]
00167     else:
00168         inputdb = director + ".db"
00169         inputdbdir = directory[:]
00170 
00171     directory = "%s%02d/" % (DIRNAME, iteration)
00172     director = directory[:-1]
00173     os.system("rm -rf %s; mkdir %s" % (directory, directory))
00174     os.system("cp gatherBH_cfg.py %s" % directory)
00175     os.system("cp alignBH_cfg.py %s" % directory)
00176 
00177     bsubfile.append("cd %s" % directory)
00178 
00179     constraints = ""
00180     if photogrammetry and (mode == "phipos" or mode == "phiz"):
00181         diskswitch = ""
00182         if disks: diskswitch = "--disks "
00183 
00184         constraints += """export ALIGNMENT_CONVERTXML=%(inputdb)s
00185 cmsRun $ALIGNMENT_AFSDIR/Alignment/MuonAlignmentAlgorithms/python/convertToXML_global_cfg.py 
00186 python $ALIGNMENT_AFSDIR/Alignment/MuonAlignmentAlgorithms/scripts/relativeConstraints.py %(inputdb)s_global.xml $ALIGNMENT_AFSDIR/Alignment/MuonAlignmentAlgorithms/data/Photogrammetry2007.%(mode)s PGFrame --scaleErrors %(photogrammetryScale)s %(diskswitch)s> constraints_cff.py""" % vars()
00187     else:
00188         constraints += """echo \"\" > constraints_cff.py"""
00189 
00190     for jobnumber in range(options.subjobs):
00191         gather_fileName = "%sgather%03d.sh" % (directory, jobnumber)
00192         inputfiles = " ".join(fileNames[jobnumber*stepsize:(jobnumber+1)*stepsize])
00193 
00194         if len(inputfiles) > 0:
00195             file(gather_fileName, "w").write("""#/bin/sh
00196 # %(commandline)s
00197 
00198 export ALIGNMENT_CAFDIR=`pwd`
00199 
00200 cd %(pwd)s
00201 eval `scramv1 run -sh`
00202 export ALIGNMENT_AFSDIR=`pwd`
00203 
00204 export ALIGNMENT_INPUTFILES='%(inputfiles)s'
00205 export ALIGNMENT_ITERATION=%(iteration)d
00206 export ALIGNMENT_MODE=%(mode)s
00207 export ALIGNMENT_JOBNUMBER=%(jobnumber)d
00208 export ALIGNMENT_INPUTDB=%(inputdb)s
00209 export ALIGNMENT_GLOBALTAG=%(globaltag)s
00210 export ALIGNMENT_PHOTOGRAMMETRY=%(photogrammetry)s
00211 export ALIGNMENT_DISKS=%(disks)s
00212 export ALIGNMENT_minP=%(minP)s
00213 export ALIGNMENT_minHitsPerChamber=%(minHitsPerChamber)s
00214 export ALIGNMENT_maxdrdz=%(maxdrdz)s
00215 export ALIGNMENT_maxRedChi2=%(maxRedChi2)s
00216 export ALIGNMENT_fiducial=%(fiducial)s
00217 export ALIGNMENT_useHitWeights=%(useHitWeights)s
00218 export ALIGNMENT_truncateSlopeResid=%(truncateSlopeResid)s
00219 export ALIGNMENT_truncateOffsetResid=%(truncateOffsetResid)s
00220 export ALIGNMENT_combineME11=%(combineME11)s
00221 export ALIGNMENT_useTrackWeights=%(useTrackWeights)s
00222 export ALIGNMENT_errorFromRMS=%(errorFromRMS)s
00223 export ALIGNMENT_minTracksPerOverlap=%(minTracksPerOverlap)s
00224 export ALIGNMENT_slopeFromTrackRefit=%(slopeFromTrackRefit)s
00225 export ALIGNMENT_minStationsInTrackRefits=%(minStationsInTrackRefits)s
00226 
00227 cp -f %(directory)sgatherBH_cfg.py %(inputdbdir)s%(inputdb)s inertGlobalPositionRcd.db $ALIGNMENT_CAFDIR/
00228 cd $ALIGNMENT_CAFDIR/
00229 
00230 %(constraints)s
00231 
00232 ls -l
00233 cmsRun gatherBH_cfg.py
00234 ls -l
00235 cp -f *.tmp *.root $ALIGNMENT_AFSDIR/%(directory)s
00236 """ % vars())
00237             os.system("chmod +x %s" % gather_fileName)
00238             bsubfile.append("echo %sgather%03d.sh" % (directory, jobnumber))
00239 
00240             if last_align is None: waiter = ""
00241             else: waiter = "-w \"ended(%s)\"" % last_align
00242             if options.big: queue = "cmscaf1nd"
00243             else: queue = "cmscaf1nh"
00244             
00245             bsubfile.append("bsub -R \"type==SLC5_64\" -q %s -J \"%s_gather%03d\" %s gather%03d.sh" % (queue, director, jobnumber, waiter, jobnumber))
00246 
00247             bsubnames.append("ended(%s_gather%03d)" % (director, jobnumber))
00248 
00249     file("%sconvert-db-to-xml_cfg.py" % directory, "w").write("""from Alignment.MuonAlignment.convertSQLitetoXML_cfg import *
00250 process.PoolDBESSource.connect = \"sqlite_file:%(directory)s%(director)s.db\"
00251 process.MuonGeometryDBConverter.outputXML.fileName = \"%(directory)s%(director)s.xml\"
00252 process.MuonGeometryDBConverter.outputXML.relativeto = \"ideal\"
00253 process.MuonGeometryDBConverter.outputXML.suppressDTChambers = True
00254 process.MuonGeometryDBConverter.outputXML.suppressDTSuperLayers = True
00255 process.MuonGeometryDBConverter.outputXML.suppressDTLayers = True
00256 process.MuonGeometryDBConverter.outputXML.suppressCSCChambers = False
00257 process.MuonGeometryDBConverter.outputXML.suppressCSCLayers = True
00258 
00259 process.MuonGeometryDBConverter.getAPEs = True
00260 process.PoolDBESSource.toGet = cms.VPSet(
00261     cms.PSet(record = cms.string(\"DTAlignmentRcd\"), tag = cms.string(\"DTAlignmentRcd\")),
00262     cms.PSet(record = cms.string(\"DTAlignmentErrorRcd\"), tag = cms.string(\"DTAlignmentErrorRcd\")),
00263     cms.PSet(record = cms.string(\"CSCAlignmentRcd\"), tag = cms.string(\"CSCAlignmentRcd\")),
00264     cms.PSet(record = cms.string(\"CSCAlignmentErrorRcd\"), tag = cms.string(\"CSCAlignmentErrorRcd\")),
00265       )
00266 """ % vars())
00267 
00268     constraints += """\ncp -f constraints_cff.py $ALIGNMENT_AFSDIR/%(directory)sconstraints_cff.py""" % vars()
00269 
00270     file("%salign.sh" % directory, "w").write("""#!/bin/sh
00271 # %(commandline)s
00272 
00273 export ALIGNMENT_CAFDIR=`pwd`
00274 
00275 cd %(pwd)s
00276 eval `scramv1 run -sh`
00277 export ALIGNMENT_AFSDIR=`pwd`
00278 
00279 export ALIGNMENT_ITERATION=%(iteration)d
00280 export ALIGNMENT_MODE=%(mode)s
00281 export ALIGNMENT_INPUTDB=%(inputdb)s
00282 export ALIGNMENT_GLOBALTAG=%(globaltag)s
00283 export ALIGNMENT_PHOTOGRAMMETRY=%(photogrammetry)s
00284 export ALIGNMENT_DISKS=%(disks)s
00285 export ALIGNMENT_minP=%(minP)s
00286 export ALIGNMENT_minHitsPerChamber=%(minHitsPerChamber)s
00287 export ALIGNMENT_maxdrdz=%(maxdrdz)s
00288 export ALIGNMENT_maxRedChi2=%(maxRedChi2)s
00289 export ALIGNMENT_fiducial=%(fiducial)s
00290 export ALIGNMENT_useHitWeights=%(useHitWeights)s
00291 export ALIGNMENT_truncateSlopeResid=%(truncateSlopeResid)s
00292 export ALIGNMENT_truncateOffsetResid=%(truncateOffsetResid)s
00293 export ALIGNMENT_combineME11=%(combineME11)s
00294 export ALIGNMENT_useTrackWeights=%(useTrackWeights)s
00295 export ALIGNMENT_errorFromRMS=%(errorFromRMS)s
00296 export ALIGNMENT_minTracksPerOverlap=%(minTracksPerOverlap)s
00297 export ALIGNMENT_slopeFromTrackRefit=%(slopeFromTrackRefit)s
00298 export ALIGNMENT_minStationsInTrackRefits=%(minStationsInTrackRefits)s
00299 
00300 cp -f %(directory)salignBH_cfg.py %(directory)sconvert-db-to-xml_cfg.py %(inputdbdir)s%(inputdb)s %(directory)s*.tmp inertGlobalPositionRcd.db $ALIGNMENT_CAFDIR/
00301 cd $ALIGNMENT_CAFDIR/
00302 export ALIGNMENT_ALIGNMENTTMP=`ls alignment*.tmp`
00303 
00304 %(constraints)s
00305 
00306 ls -l
00307 cmsRun alignBH_cfg.py
00308 cp -f report.py $ALIGNMENT_AFSDIR/%(directory)s%(director)s_report.py
00309 cp -f outputdb.db $ALIGNMENT_AFSDIR/%(directory)s%(director)s.db
00310 cp -f plotting.root $ALIGNMENT_AFSDIR/%(directory)s%(director)s.root
00311 
00312 cd $ALIGNMENT_AFSDIR
00313 cmsRun %(directory)sconvert-db-to-xml_cfg.py
00314 """ % vars())
00315     os.system("chmod +x %salign.sh" % directory)
00316 
00317     bsubfile.append("echo %salign.sh" % directory)
00318     bsubfile.append("bsub -R \"type==SLC5_64\" -q cmscaf1nd -J \"%s_align\" -w \"%s\" align.sh" % (director, " && ".join(bsubnames)))
00319     bsubfile.append("cd ..")
00320     bsubnames = []
00321     last_align = "%s_align" % director
00322 
00323 bsubfile.append("")
00324 file(options.submitJobs, "w").write("\n".join(bsubfile))
00325 os.system("chmod +x %s" % options.submitJobs)