Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-25 02:29:04

0001 #! /usr/bin/env python3
0002 
0003 from builtins import range
0004 import os, sys, optparse, math
0005 
0006 copyargs = sys.argv[:]
0007 for i in range(len(copyargs)):
0008     if copyargs[i] == "":
0009         copyargs[i] = "\"\""
0010     if copyargs[i].find(" ") != -1:
0011         copyargs[i] = "\"%s\"" % copyargs[i]
0012 commandline = " ".join(copyargs)
0013 
0014 prog = sys.argv[0]
0015 
0016 usage = """./%(prog)s DIRNAME ITERATIONS INITIALGEOM INPUTFILES [options]
0017 
0018 Creates (overwrites) a directory for each of the iterations and creates (overwrites)
0019 submitJobs.sh with the submission sequence and dependencies.
0020 
0021 DIRNAME        directories will be named DIRNAME01, DIRNAME02, etc.
0022 ITERATIONS     number of iterations
0023 INITIALGEOM    SQLite file containing muon geometry with tag names
0024                DTAlignmentRcd, DTAlignmentErrorExtendedRcd, CSCAlignmentRcd, CSCAlignmentErrorExtendedRcd
0025 INPUTFILES     Python file defining 'fileNames', a list of input files as
0026                strings (create with findQualityFiles.py)""" % vars()
0027 
0028 parser = optparse.OptionParser(usage)
0029 parser.add_option("-j", "--jobs",
0030                    help="approximate number of \"gather\" subjobs",
0031                    type="int",
0032                    default=50,
0033                    dest="subjobs")
0034 parser.add_option("-s", "--submitJobs",
0035                    help="alternate name of submitJobs.sh script (please include .sh extension); a file with this name will be OVERWRITTEN",
0036                    type="string",
0037                    default="submitJobs.sh",
0038                    dest="submitJobs")
0039 parser.add_option("-b", "--big",
0040                   help="if invoked, subjobs will also be run on cmscaf1nd",
0041                   action="store_true",
0042                   dest="big")
0043 parser.add_option("-u", "--user_mail",
0044                   help="if invoked, send mail to a specified email destination. If \"-u\" is not present, the default destination LSB_MAILTO in lsf.conf will be used",
0045                   type="string",
0046                   dest="user_mail")
0047 parser.add_option("--mapplots",
0048                   help="if invoked, draw \"map plots\"",
0049                   action="store_true",
0050                   dest="mapplots")
0051 parser.add_option("--segdiffplots",
0052                   help="if invoked, draw \"segment-difference plots\"",
0053                   action="store_true",
0054                   dest="segdiffplots")
0055 parser.add_option("--curvatureplots",
0056                   help="if invoked, draw \"curvature plots\"",
0057                   action="store_true",
0058                   dest="curvatureplots")
0059 parser.add_option("--globalTag",
0060                   help="GlobalTag for alignment/calibration conditions (typically all conditions except muon and tracker alignment)",
0061                   type="string",
0062                   default="CRAFT0831X_V1::All",
0063                   dest="globaltag")
0064 parser.add_option("--trackerconnect",
0065                   help="connect string for tracker alignment (frontier://FrontierProd/CMS_COND_310X_ALIGN or sqlite_file:...)",
0066                   type="string",
0067                   default="",
0068                   dest="trackerconnect")
0069 parser.add_option("--trackeralignment",
0070                   help="name of TrackerAlignmentRcd tag",
0071                   type="string",
0072                   default="Alignments",
0073                   dest="trackeralignment")
0074 parser.add_option("--trackerAPEconnect",
0075                   help="connect string for tracker APEs (frontier://... or sqlite_file:...)",
0076                   type="string",
0077                   default="",
0078                   dest="trackerAPEconnect")
0079 parser.add_option("--trackerAPE",
0080                   help="name of TrackerAlignmentErrorExtendedRcd tag (tracker APEs)",
0081                   type="string",
0082                   default="AlignmentErrorsExtended",
0083                   dest="trackerAPE")
0084 parser.add_option("--trackerBowsconnect",
0085                   help="connect string for tracker Surface Deformations (frontier://... or sqlite_file:...)",
0086                   type="string",
0087                   default="",
0088                   dest="trackerBowsconnect")
0089 parser.add_option("--trackerBows",
0090                   help="name of TrackerSurfaceDeformationRcd tag",
0091                   type="string",
0092                   default="TrackerSurfaceDeformations",
0093                   dest="trackerBows")
0094 parser.add_option("--gprcdconnect",
0095                   help="connect string for GlobalPositionRcd (frontier://... or sqlite_file:...)",
0096                   type="string",
0097                   default="",
0098                   dest="gprcdconnect")
0099 parser.add_option("--gprcd",
0100                   help="name of GlobalPositionRcd tag",
0101                   type="string",
0102                   default="GlobalPosition",
0103                   dest="gprcd")
0104 parser.add_option("--iscosmics",
0105                   help="if invoked, use cosmic track refitter instead of the standard one",
0106                   action="store_true",
0107                   dest="iscosmics")
0108 parser.add_option("--station123params",
0109                   help="alignable parameters for DT stations 1, 2, 3 (see SWGuideAlignmentAlgorithms#Selection_of_what_to_align)",
0110                   type="string",
0111                   default="111111",
0112                   dest="station123params")
0113 parser.add_option("--station4params",
0114                   help="alignable parameters for DT station 4",
0115                   type="string",
0116                   default="100011",
0117                   dest="station4params")
0118 parser.add_option("--cscparams",
0119                   help="alignable parameters for CSC chambers",
0120                   type="string",
0121                   default="100011",
0122                   dest="cscparams")
0123 parser.add_option("--minTrackPt",
0124                   help="minimum allowed track transverse momentum (in GeV)",
0125                   type="string",
0126                   default="0",
0127                   dest="minTrackPt")
0128 parser.add_option("--maxTrackPt",
0129                   help="maximum allowed track transverse momentum (in GeV)",
0130                   type="string",
0131                   default="1000",
0132                   dest="maxTrackPt")
0133 parser.add_option("--minTrackP",
0134                   help="minimum allowed track momentum (in GeV)",
0135                   type="string",
0136                   default="0",
0137                   dest="minTrackP")
0138 parser.add_option("--maxTrackP",
0139                   help="maximum allowed track momentum (in GeV)",
0140                   type="string",
0141                   default="10000",
0142                   dest="maxTrackP")
0143 parser.add_option("--minTrackerHits",
0144                   help="minimum number of tracker hits",
0145                   type="int",
0146                   default=15,
0147                   dest="minTrackerHits")
0148 parser.add_option("--maxTrackerRedChi2",
0149                   help="maximum tracker chi^2 per degrees of freedom",
0150                   type="string",
0151                   default="10",
0152                   dest="maxTrackerRedChi2")
0153 parser.add_option("--notAllowTIDTEC",
0154                   help="if invoked, do not allow tracks that pass through the tracker's TID||TEC region (not recommended)",
0155                   action="store_true",
0156                   dest="notAllowTIDTEC")
0157 parser.add_option("--twoBin",
0158                   help="if invoked, apply the \"two-bin method\" to control charge-antisymmetric errors",
0159                   action="store_true",
0160                   dest="twoBin")
0161 parser.add_option("--weightAlignment",
0162                   help="if invoked, segments will be weighted by ndf/chi^2 in the alignment",
0163                   action="store_true",
0164                   dest="weightAlignment")
0165 parser.add_option("--minAlignmentSegments",
0166                   help="minimum number of segments required to align a chamber",
0167                   type="int",
0168                   default=5,
0169                   dest="minAlignmentHits")
0170 parser.add_option("--notCombineME11",
0171                   help="if invoced, treat ME1/1a and ME1/1b as separate objects",
0172                   action="store_true",
0173                   dest="notCombineME11")
0174 parser.add_option("--maxEvents",
0175                   help="maximum number of events",
0176                   type="string",
0177                   default="-1",
0178                   dest="maxEvents")
0179 parser.add_option("--skipEvents",
0180                   help="number of events to be skipped",
0181                   type="string",
0182                   default="0",
0183                   dest="skipEvents")
0184 parser.add_option("--validationLabel",
0185                   help="if given nonempty string RUNLABEL, diagnostics and creation of plots will be run in the end of the last iteration; the RUNLABEL will be used to mark a run; the results will be put into a RUNLABEL_DATESTAMP.tgz tarball",
0186                   type="string",
0187                   default="",
0188                   dest="validationLabel")
0189 parser.add_option("--maxResSlopeY",
0190                   help="maximum residual slope y component",
0191                   type="string",
0192                   default="10",
0193                   dest="maxResSlopeY")
0194 parser.add_option("--motionPolicyNSigma",
0195                   help="minimum nsigma(deltax) position displacement in order to move a chamber for the final alignment result; default NSIGMA=3",
0196                   type="int",
0197                   default=3,
0198                   dest="motionPolicyNSigma")
0199 parser.add_option("--noCleanUp",
0200                   help="if invoked, temporary plotting???.root and *.tmp files would not be removed at the end of each align job",
0201                   action="store_true",
0202                   dest="noCleanUp")
0203 parser.add_option("--noCSC",
0204                   help="if invoked, CSC endcap chambers would not be processed",
0205                   action="store_true",
0206                   dest="noCSC")
0207 parser.add_option("--noDT",
0208                   help="if invoked, DT barrel chambers would not be processed",
0209                   action="store_true",
0210                   dest="noDT")
0211 parser.add_option("--createMapNtuple",
0212                   help="if invoked while mapplots are switched on, a special ntuple would be created",
0213                   action="store_true",
0214                   dest="createMapNtuple")
0215 parser.add_option("--inputInBlocks",
0216                   help="if invoked, assume that INPUTFILES provides a list of files already groupped into job blocks, -j has no effect in that case",
0217                   action="store_true",
0218                   dest="inputInBlocks")
0219 parser.add_option("--json",
0220                   help="If present with JSON file as argument, use JSON file for good lumi mask. "+\
0221                   "The latest JSON file is available at /afs/cern.ch/cms/CAF/CMSCOMM/COMM_DQM/certification/Collisions11/7TeV/Prompt/",
0222                   type="string",
0223                   default="",
0224                   dest="json")
0225 parser.add_option("--createAlignNtuple",
0226                   help="if invoked, debug ntuples with residuals would be created during gather jobs",
0227                   action="store_true",
0228                   dest="createAlignNtuple")
0229 parser.add_option("--residualsModel",
0230                   help="functional residuals model. Possible vaslues: pureGaussian2D (default), pureGaussian, GaussPowerTails, ROOTVoigt, powerLawTails",
0231                   type="string",
0232                   default="pureGaussian2D",
0233                   dest="residualsModel")
0234 parser.add_option("--useResiduals",
0235                   help="select residuals to use, possible values: 1111, 1110, 1100, 1010, 0010 that correspond to x y dxdz dydz residuals",
0236                   type="string",
0237                   default="1110",
0238                   dest="useResiduals")
0239 parser.add_option("--peakNSigma",
0240                   help="if >0, only residuals peaks within n-sigma multidimentional ellipsoid would be considered in the alignment fit",
0241                   type="string",
0242                   default="-1.",
0243                   dest="peakNSigma")
0244 parser.add_option("--preFilter",
0245                   help="if invoked, MuonAlignmentPreFilter module would be invoked in the Path's beginning. Can significantly speed up gather jobs.",
0246                   action="store_true",
0247                   dest="preFilter")
0248 parser.add_option("--muonCollectionTag",
0249                   help="If empty, use trajectories. If not empty, it's InputTag of muons collection to use in tracker muons based approach, e.g., 'newmuons' or 'muons'",
0250                   type="string",
0251                   default="",
0252                   dest="muonCollectionTag")
0253 parser.add_option("--maxDxy",
0254                   help="maximum track impact parameter with relation to beamline",
0255                   type="string",
0256                   default="1000.",
0257                   dest="maxDxy")
0258 parser.add_option("--minNCrossedChambers",
0259                   help="minimum number of muon chambers that a track is required to cross",
0260                   type="string",
0261                   default="3",
0262                   dest="minNCrossedChambers")
0263 parser.add_option("--extraPlots",
0264                   help="produce additional plots with geometry, reports differences, and corrections visulizations",
0265                   action="store_true",
0266                   dest="extraPlots")
0267 
0268 if len(sys.argv) < 5:
0269     raise SystemError("Too few arguments.\n\n"+parser.format_help())
0270 
0271 DIRNAME = sys.argv[1]
0272 ITERATIONS = int(sys.argv[2])
0273 INITIALGEOM = sys.argv[3]
0274 INPUTFILES = sys.argv[4]
0275 
0276 options, args = parser.parse_args(sys.argv[5:])
0277 user_mail = options.user_mail
0278 mapplots_ingeneral = options.mapplots
0279 segdiffplots_ingeneral = options.segdiffplots
0280 curvatureplots_ingeneral = options.curvatureplots
0281 globaltag = options.globaltag
0282 trackerconnect = options.trackerconnect
0283 trackeralignment = options.trackeralignment
0284 trackerAPEconnect = options.trackerAPEconnect
0285 trackerAPE = options.trackerAPE
0286 trackerBowsconnect = options.trackerBowsconnect
0287 trackerBows = options.trackerBows
0288 gprcdconnect = options.gprcdconnect
0289 gprcd = options.gprcd
0290 iscosmics = str(options.iscosmics)
0291 station123params = options.station123params
0292 station4params = options.station4params
0293 cscparams = options.cscparams
0294 muonCollectionTag = options.muonCollectionTag
0295 minTrackPt = options.minTrackPt
0296 maxTrackPt = options.maxTrackPt
0297 minTrackP = options.minTrackP
0298 maxTrackP = options.maxTrackP
0299 maxDxy = options.maxDxy
0300 minTrackerHits = str(options.minTrackerHits)
0301 maxTrackerRedChi2 = options.maxTrackerRedChi2
0302 minNCrossedChambers = options.minNCrossedChambers
0303 allowTIDTEC = str(not options.notAllowTIDTEC)
0304 twoBin = str(options.twoBin)
0305 weightAlignment = str(options.weightAlignment)
0306 minAlignmentHits = str(options.minAlignmentHits)
0307 combineME11 = str(not options.notCombineME11)
0308 maxEvents = options.maxEvents
0309 skipEvents = options.skipEvents
0310 validationLabel = options.validationLabel
0311 maxResSlopeY = options.maxResSlopeY
0312 theNSigma = options.motionPolicyNSigma
0313 residualsModel = options.residualsModel
0314 peakNSigma = options.peakNSigma
0315 preFilter = not not options.preFilter
0316 extraPlots = options.extraPlots
0317 useResiduals = options.useResiduals
0318 
0319 
0320 #print "check: ", allowTIDTEC, combineME11, preFilter
0321 
0322 doCleanUp = not options.noCleanUp
0323 createMapNtuple = not not options.createMapNtuple
0324 createAlignNtuple = not not options.createAlignNtuple
0325 
0326 doCSC = True
0327 if options.noCSC: doCSC = False
0328 doDT = True
0329 if options.noDT: doDT = False
0330 if options.noCSC and options.noDT:
0331     print("cannot do --noCSC and --noDT at the same time!")
0332     sys.exit()
0333 
0334 json_file = options.json
0335 
0336 fileNames=[]
0337 fileNamesBlocks=[]
0338 execfile(INPUTFILES)
0339 njobs = options.subjobs
0340 if (options.inputInBlocks):
0341     njobs = len(fileNamesBlocks)
0342     if njobs==0:
0343         print("while --inputInBlocks is specified, the INPUTFILES has no blocks!")
0344         sys.exit()
0345 
0346 stepsize = int(math.ceil(1.*len(fileNames)/options.subjobs))
0347 
0348 pwd = str(os.getcwd())
0349 
0350 copytrackerdb = ""
0351 if trackerconnect[0:12] == "sqlite_file:": copytrackerdb += "%s " % trackerconnect[12:]
0352 if trackerAPEconnect[0:12] == "sqlite_file:": copytrackerdb += "%s " % trackerAPEconnect[12:]
0353 if trackerBowsconnect[0:12] == "sqlite_file:": copytrackerdb += "%s " % trackerBowsconnect[12:]
0354 if gprcdconnect[0:12] == "sqlite_file:": copytrackerdb += "%s " % gprcdconnect[12:]
0355 
0356 
0357 #####################################################################
0358 # step 0: convert initial geometry to xml
0359 INITIALXML = INITIALGEOM + '.xml'
0360 if INITIALGEOM[-3:]=='.db':
0361     INITIALXML = INITIALGEOM[:-3] + '.xml'
0362 print("Converting",INITIALGEOM,"to",INITIALXML," ...will be done in several seconds...")
0363 print("./Alignment/MuonAlignmentAlgorithms/scripts/convertSQLiteXML.py  %s %s --gprcdconnect %s --gprcd %s" % (INITIALGEOM,INITIALXML,gprcdconnect,gprcd))
0364 exit_code = os.system("./Alignment/MuonAlignmentAlgorithms/scripts/convertSQLiteXML.py  %s %s --gprcdconnect %s --gprcd %s" % (INITIALGEOM,INITIALXML,gprcdconnect,gprcd))
0365 if exit_code>0:
0366     print("problem: conversion exited with code:", exit_code)
0367     sys.exit()
0368 
0369 #####################################################################
0370 
0371 def writeGatherCfg(fname, my_vars):
0372     file(fname, "w").write("""#/bin/sh
0373 # %(commandline)s
0374 
0375 export ALIGNMENT_CAFDIR=`pwd`
0376 
0377 cd %(pwd)s
0378 eval `scramv1 run -sh`
0379 export ALIGNMENT_AFSDIR=`pwd`
0380 
0381 export ALIGNMENT_INPUTFILES='%(inputfiles)s'
0382 export ALIGNMENT_ITERATION=%(iteration)d
0383 export ALIGNMENT_JOBNUMBER=%(jobnumber)d
0384 export ALIGNMENT_MAPPLOTS=%(mapplots)s
0385 export ALIGNMENT_SEGDIFFPLOTS=%(segdiffplots)s
0386 export ALIGNMENT_CURVATUREPLOTS=%(curvatureplots)s
0387 export ALIGNMENT_GLOBALTAG=%(globaltag)s
0388 export ALIGNMENT_INPUTDB=%(inputdb)s
0389 export ALIGNMENT_TRACKERCONNECT=%(trackerconnect)s
0390 export ALIGNMENT_TRACKERALIGNMENT=%(trackeralignment)s
0391 export ALIGNMENT_TRACKERAPECONNECT=%(trackerAPEconnect)s
0392 export ALIGNMENT_TRACKERAPE=%(trackerAPE)s
0393 export ALIGNMENT_TRACKERBOWSCONNECT=%(trackerBowsconnect)s
0394 export ALIGNMENT_TRACKERBOWS=%(trackerBows)s
0395 export ALIGNMENT_GPRCDCONNECT=%(gprcdconnect)s
0396 export ALIGNMENT_GPRCD=%(gprcd)s
0397 export ALIGNMENT_ISCOSMICS=%(iscosmics)s
0398 export ALIGNMENT_STATION123PARAMS=%(station123params)s
0399 export ALIGNMENT_STATION4PARAMS=%(station4params)s
0400 export ALIGNMENT_CSCPARAMS=%(cscparams)s
0401 export ALIGNMENT_MUONCOLLECTIONTAG=%(muonCollectionTag)s
0402 export ALIGNMENT_MINTRACKPT=%(minTrackPt)s
0403 export ALIGNMENT_MAXTRACKPT=%(maxTrackPt)s
0404 export ALIGNMENT_MINTRACKP=%(minTrackP)s
0405 export ALIGNMENT_MAXTRACKP=%(maxTrackP)s
0406 export ALIGNMENT_MAXDXY=%(maxDxy)s
0407 export ALIGNMENT_MINTRACKERHITS=%(minTrackerHits)s
0408 export ALIGNMENT_MAXTRACKERREDCHI2=%(maxTrackerRedChi2)s
0409 export ALIGNMENT_MINNCROSSEDCHAMBERS=%(minNCrossedChambers)s
0410 export ALIGNMENT_ALLOWTIDTEC=%(allowTIDTEC)s
0411 export ALIGNMENT_TWOBIN=%(twoBin)s
0412 export ALIGNMENT_WEIGHTALIGNMENT=%(weightAlignment)s
0413 export ALIGNMENT_MINALIGNMENTHITS=%(minAlignmentHits)s
0414 export ALIGNMENT_COMBINEME11=%(combineME11)s
0415 export ALIGNMENT_MAXEVENTS=%(maxEvents)s
0416 export ALIGNMENT_SKIPEVENTS=%(skipEvents)s
0417 export ALIGNMENT_MAXRESSLOPEY=%(maxResSlopeY)s
0418 export ALIGNMENT_DO_DT=%(doDT)s
0419 export ALIGNMENT_DO_CSC=%(doCSC)s
0420 export ALIGNMENT_JSON=%(json_file)s
0421 export ALIGNMENT_CREATEMAPNTUPLE=%(createMapNtuple)s
0422 #export ALIGNMENT_CREATEALIGNNTUPLE=%(createAlignNtuple)s
0423 export ALIGNMENT_PREFILTER=%(preFilter)s
0424 
0425 
0426 if [ \"zzz$ALIGNMENT_JSON\" != \"zzz\" ]; then
0427   cp -f $ALIGNMENT_JSON $ALIGNMENT_CAFDIR/
0428 fi
0429 
0430 cp -f %(directory)sgather_cfg.py %(inputdbdir)s%(inputdb)s %(copytrackerdb)s $ALIGNMENT_CAFDIR/
0431 cd $ALIGNMENT_CAFDIR/
0432 ls -l
0433 cmsRun gather_cfg.py
0434 ls -l
0435 cp -f *.tmp %(copyplots)s $ALIGNMENT_AFSDIR/%(directory)s
0436 """ % my_vars)
0437 
0438 #####################################################################
0439 
0440 def writeAlignCfg(fname, my_vars):
0441     file("%salign.sh" % directory, "w").write("""#!/bin/sh
0442 # %(commandline)s
0443 
0444 export ALIGNMENT_CAFDIR=`pwd`
0445 
0446 cd %(pwd)s
0447 eval `scramv1 run -sh`
0448 export ALIGNMENT_AFSDIR=`pwd`
0449 export ALIGNMENT_INPUTDB=%(inputdb)s
0450 export ALIGNMENT_ITERATION=%(iteration)d
0451 export ALIGNMENT_GLOBALTAG=%(globaltag)s
0452 export ALIGNMENT_TRACKERCONNECT=%(trackerconnect)s
0453 export ALIGNMENT_TRACKERALIGNMENT=%(trackeralignment)s
0454 export ALIGNMENT_TRACKERAPECONNECT=%(trackerAPEconnect)s
0455 export ALIGNMENT_TRACKERAPE=%(trackerAPE)s
0456 export ALIGNMENT_TRACKERBOWSCONNECT=%(trackerBowsconnect)s
0457 export ALIGNMENT_TRACKERBOWS=%(trackerBows)s
0458 export ALIGNMENT_GPRCDCONNECT=%(gprcdconnect)s
0459 export ALIGNMENT_GPRCD=%(gprcd)s
0460 export ALIGNMENT_ISCOSMICS=%(iscosmics)s
0461 export ALIGNMENT_STATION123PARAMS=%(station123params)s
0462 export ALIGNMENT_STATION4PARAMS=%(station4params)s
0463 export ALIGNMENT_CSCPARAMS=%(cscparams)s
0464 export ALIGNMENT_MINTRACKPT=%(minTrackPt)s
0465 export ALIGNMENT_MAXTRACKPT=%(maxTrackPt)s
0466 export ALIGNMENT_MINTRACKP=%(minTrackP)s
0467 export ALIGNMENT_MAXTRACKP=%(maxTrackP)s
0468 export ALIGNMENT_MINTRACKERHITS=%(minTrackerHits)s
0469 export ALIGNMENT_MAXTRACKERREDCHI2=%(maxTrackerRedChi2)s
0470 export ALIGNMENT_ALLOWTIDTEC=%(allowTIDTEC)s
0471 export ALIGNMENT_TWOBIN=%(twoBin)s
0472 export ALIGNMENT_WEIGHTALIGNMENT=%(weightAlignment)s
0473 export ALIGNMENT_MINALIGNMENTHITS=%(minAlignmentHits)s
0474 export ALIGNMENT_COMBINEME11=%(combineME11)s
0475 export ALIGNMENT_MAXRESSLOPEY=%(maxResSlopeY)s
0476 export ALIGNMENT_CLEANUP=%(doCleanUp)s
0477 export ALIGNMENT_CREATEALIGNNTUPLE=%(createAlignNtuple)s
0478 export ALIGNMENT_RESIDUALSMODEL=%(residualsModel)s
0479 export ALIGNMENT_PEAKNSIGMA=%(peakNSigma)s
0480 export ALIGNMENT_USERESIDUALS=%(useResiduals)s
0481 
0482 cp -f %(directory)salign_cfg.py %(inputdbdir)s%(inputdb)s %(directory)s*.tmp  %(copytrackerdb)s $ALIGNMENT_CAFDIR/
0483 
0484 export ALIGNMENT_PLOTTINGTMP=`find %(directory)splotting0*.root -maxdepth 1 -size +0 -print 2> /dev/null`
0485 
0486 # if it's 1st or last iteration, combine _plotting.root files into one:
0487 if [ \"$ALIGNMENT_ITERATION\" != \"111\" ] || [ \"$ALIGNMENT_ITERATION\" == \"%(ITERATIONS)s\" ]; then
0488   #nfiles=$(ls %(directory)splotting0*.root 2> /dev/null | wc -l)
0489   if [ \"zzz$ALIGNMENT_PLOTTINGTMP\" != \"zzz\" ]; then
0490     hadd -f1 %(directory)s%(director)s_plotting.root %(directory)splotting0*.root
0491     #if [ $? == 0 ] && [ \"$ALIGNMENT_CLEANUP\" == \"True\" ]; then rm %(directory)splotting0*.root; fi
0492   fi
0493 fi
0494 
0495 if [ \"$ALIGNMENT_CLEANUP\" == \"True\" ] && [ \"zzz$ALIGNMENT_PLOTTINGTMP\" != \"zzz\" ]; then
0496   rm $ALIGNMENT_PLOTTINGTMP
0497 fi
0498 
0499 cd $ALIGNMENT_CAFDIR/
0500 export ALIGNMENT_ALIGNMENTTMP=`find alignment*.tmp -maxdepth 1 -size +1k -print 2> /dev/null`
0501 ls -l
0502 
0503 cmsRun align_cfg.py
0504 cp -f MuonAlignmentFromReference_report.py $ALIGNMENT_AFSDIR/%(directory)s%(director)s_report.py
0505 cp -f MuonAlignmentFromReference_outputdb.db $ALIGNMENT_AFSDIR/%(directory)s%(director)s.db
0506 cp -f MuonAlignmentFromReference_plotting.root $ALIGNMENT_AFSDIR/%(directory)s%(director)s.root
0507 
0508 cd $ALIGNMENT_AFSDIR
0509 ./Alignment/MuonAlignmentAlgorithms/scripts/convertSQLiteXML.py %(directory)s%(director)s.db %(directory)s%(director)s.xml --noLayers --gprcdconnect $ALIGNMENT_GPRCDCONNECT --gprcd $ALIGNMENT_GPRCD
0510 
0511 export ALIGNMENT_ALIGNMENTTMP=`find %(directory)salignment*.tmp -maxdepth 1 -size +1k -print 2> /dev/null`
0512 if [ \"$ALIGNMENT_CLEANUP\" == \"True\" ] && [ \"zzz$ALIGNMENT_ALIGNMENTTMP\" != \"zzz\" ]; then
0513   rm $ALIGNMENT_ALIGNMENTTMP
0514   echo " "
0515 fi
0516 
0517 # if it's not 1st or last iteration, do some clean up:
0518 if [ \"$ALIGNMENT_ITERATION\" != \"1\" ] && [ \"$ALIGNMENT_ITERATION\" != \"%(ITERATIONS)s\" ]; then
0519   if [ \"$ALIGNMENT_CLEANUP\" == \"True\" ] && [ -e %(directory)s%(director)s.root ]; then
0520     rm %(directory)s%(director)s.root
0521   fi
0522 fi
0523 
0524 # if it's last iteration, apply chamber motion policy
0525 if [ \"$ALIGNMENT_ITERATION\" == \"%(ITERATIONS)s\" ]; then
0526   # convert this iteration's geometry into detailed xml
0527   ./Alignment/MuonAlignmentAlgorithms/scripts/convertSQLiteXML.py %(directory)s%(director)s.db %(directory)s%(director)s_extra.xml --gprcdconnect $ALIGNMENT_GPRCDCONNECT --gprcd $ALIGNMENT_GPRCD
0528   # perform motion policy 
0529   ./Alignment/MuonAlignmentAlgorithms/scripts/motionPolicyChamber.py \
0530       %(INITIALXML)s  %(directory)s%(director)s_extra.xml \
0531       %(directory)s%(director)s_report.py \
0532       %(directory)s%(director)s_final.xml \
0533       --nsigma %(theNSigma)s
0534   # convert the resulting xml into the final sqlite geometry
0535   ./Alignment/MuonAlignmentAlgorithms/scripts/convertSQLiteXML.py %(directory)s%(director)s_final.xml %(directory)s%(director)s_final.db --gprcdconnect $ALIGNMENT_GPRCDCONNECT --gprcd $ALIGNMENT_GPRCD
0536 fi
0537 
0538 """ % my_vars)
0539 
0540 #####################################################################
0541 
0542 def writeValidationCfg(fname, my_vars):
0543     file(fname, "w").write("""#!/bin/sh
0544 # %(commandline)s
0545 
0546 export ALIGNMENT_CAFDIR=`pwd`
0547 mkdir files
0548 mkdir out
0549 
0550 cd %(pwd)s
0551 eval `scramv1 run -sh`
0552 ALIGNMENT_AFSDIR=`pwd`
0553 ALIGNMENT_ITERATION=%(iteration)d
0554 ALIGNMENT_MAPPLOTS=None
0555 ALIGNMENT_SEGDIFFPLOTS=None
0556 ALIGNMENT_CURVATUREPLOTS=None
0557 ALIGNMENT_EXTRAPLOTS=%(extraPlots)s
0558 export ALIGNMENT_GPRCDCONNECT=%(gprcdconnect)s
0559 export ALIGNMENT_GPRCD=%(gprcd)s
0560 export ALIGNMENT_DO_DT=%(doDT)s
0561 export ALIGNMENT_DO_CSC=%(doCSC)s
0562 
0563 
0564 # copy the scripts to CAFDIR
0565 cd Alignment/MuonAlignmentAlgorithms/scripts/
0566 cp -f plotscripts.py $ALIGNMENT_CAFDIR/
0567 cp -f mutypes.py $ALIGNMENT_CAFDIR/
0568 cp -f alignmentValidation.py $ALIGNMENT_CAFDIR/
0569 cp -f phiedges_fitfunctions.C $ALIGNMENT_CAFDIR/
0570 cp -f createTree.py $ALIGNMENT_CAFDIR/
0571 cp -f signConventions.py $ALIGNMENT_CAFDIR/
0572 cp -f convertSQLiteXML.py $ALIGNMENT_CAFDIR/
0573 cp -f wrapperExtraPlots.sh $ALIGNMENT_CAFDIR/
0574 cd -
0575 cp Alignment/MuonAlignmentAlgorithms/test/browser/tree* $ALIGNMENT_CAFDIR/out/
0576 
0577 # copy the results to CAFDIR
0578 cp -f %(directory1)s%(director1)s_report.py $ALIGNMENT_CAFDIR/files/
0579 cp -f %(directory)s%(director)s_report.py $ALIGNMENT_CAFDIR/files/
0580 cp -f %(directory1)s%(director1)s.root $ALIGNMENT_CAFDIR/files/
0581 cp -f %(directory)s%(director)s.root $ALIGNMENT_CAFDIR/files/
0582 if [ -e %(directory1)s%(director1)s_plotting.root ] && [ -e %(directory)s%(director)s_plotting.root ]; then
0583   cp -f %(directory1)s%(director1)s_plotting.root $ALIGNMENT_CAFDIR/files/
0584   cp -f %(directory)s%(director)s_plotting.root $ALIGNMENT_CAFDIR/files/
0585   ALIGNMENT_MAPPLOTS=%(mapplots)s
0586   ALIGNMENT_SEGDIFFPLOTS=%(segdiffplots)s
0587   ALIGNMENT_CURVATUREPLOTS=%(curvatureplots)s
0588 fi
0589 
0590 dtcsc=""
0591 if [ $ALIGNMENT_DO_DT == \"True\" ]; then
0592   dtcsc="--dt"
0593 fi
0594 if [ $ALIGNMENT_DO_CSC == \"True\" ]; then
0595   dtcsc="${dtcsc} --csc"
0596 fi
0597 
0598 
0599 cd $ALIGNMENT_CAFDIR/
0600 echo \" ### Start running ###\"
0601 date
0602 
0603 # do fits and median plots first 
0604 ./alignmentValidation.py -l %(validationLabel)s -i $ALIGNMENT_CAFDIR --i1 files --iN files --i1prefix %(director1)s --iNprefix %(director)s -o $ALIGNMENT_CAFDIR/out  --createDirSructure --dt --csc --fit --median
0605 
0606 if [ $ALIGNMENT_MAPPLOTS == \"True\" ]; then
0607   ./alignmentValidation.py -l %(validationLabel)s -i $ALIGNMENT_CAFDIR --i1 files --iN files --i1prefix %(director1)s --iNprefix %(director)s -o $ALIGNMENT_CAFDIR/out  $dtcsc --map
0608 fi
0609 
0610 if [ $ALIGNMENT_SEGDIFFPLOTS == \"True\" ]; then
0611   ./alignmentValidation.py -l %(validationLabel)s -i $ALIGNMENT_CAFDIR --i1 files --iN files --i1prefix %(director1)s --iNprefix %(director)s -o $ALIGNMENT_CAFDIR/out  $dtcsc --segdiff
0612 fi                   
0613 
0614 if [ $ALIGNMENT_CURVATUREPLOTS == \"True\" ]; then
0615   ./alignmentValidation.py -l %(validationLabel)s -i $ALIGNMENT_CAFDIR --i1 files --iN files --i1prefix %(director1)s --iNprefix %(director)s -o $ALIGNMENT_CAFDIR/out  $dtcsc --curvature
0616 fi
0617 
0618 if [ $ALIGNMENT_EXTRAPLOTS == \"True\" ]; then
0619   if [ \"zzz%(copytrackerdb)s\" != \"zzz\" ]; then
0620     cp -f $ALIGNMENT_AFSDIR/%(copytrackerdb)s $ALIGNMENT_CAFDIR/
0621   fi
0622   cp $ALIGNMENT_AFSDIR/inertGlobalPositionRcd.db .
0623   ./convertSQLiteXML.py $ALIGNMENT_AFSDIR/%(INITIALGEOM)s g0.xml --noLayers  --gprcdconnect $ALIGNMENT_GPRCDCONNECT --gprcd $ALIGNMENT_GPRCD
0624   ./wrapperExtraPlots.sh -n $ALIGNMENT_ITERATION -i $ALIGNMENT_AFSDIR -0 g0.xml -z -w %(station123params)s %(dir_no_)s
0625   mkdir out/extra
0626   cd %(dir_no_)s
0627   mv MB ../out/extra/
0628   mv ME ../out/extra/
0629   cd -
0630 fi
0631 
0632 # run simple diagnostic
0633 ./alignmentValidation.py -l %(validationLabel)s -i $ALIGNMENT_CAFDIR --i1 files --iN files --i1prefix %(director1)s --iNprefix %(director)s -o $ALIGNMENT_CAFDIR/out --dt --csc --diagnostic
0634 
0635 # fill the tree browser structure: 
0636 ./createTree.py -i $ALIGNMENT_CAFDIR/out
0637 
0638 timestamp=`date \"+%%y-%%m-%%d %%H:%%M:%%S\"`
0639 echo \"%(validationLabel)s.plots (${timestamp})\" > out/label.txt
0640 
0641 ls -l out/
0642 timestamp=`date +%%Y%%m%%d%%H%%M%%S`
0643 tar czf %(validationLabel)s_${timestamp}.tgz out
0644 cp -f %(validationLabel)s_${timestamp}.tgz $ALIGNMENT_AFSDIR/
0645 
0646 """ % my_vars)
0647 
0648 
0649 #####################################################################
0650 
0651 #SUPER_SPECIAL_XY_AND_DXDZ_ITERATIONS = True
0652 SUPER_SPECIAL_XY_AND_DXDZ_ITERATIONS = False
0653 
0654 bsubfile = ["#!/bin/sh", ""]
0655 bsubnames = []
0656 last_align = None
0657 directory = ""
0658 
0659 for iteration in range(1, ITERATIONS+1):
0660     if iteration == 1:
0661         inputdb = INITIALGEOM
0662         inputdbdir = directory[:]
0663     else:
0664         inputdb = director + ".db"
0665         inputdbdir = directory[:]
0666 
0667     directory = "%s%02d/" % (DIRNAME, iteration)
0668     director = directory[:-1]
0669 
0670     dir_no_ = DIRNAME
0671     if DIRNAME[-1]=='_': dir_no_ = DIRNAME[:-1]
0672 
0673     os.system("rm -rf %s; mkdir %s" % (directory, directory))
0674     os.system("cp Alignment/MuonAlignmentAlgorithms/python/gather_cfg.py %s" % directory)
0675     os.system("cp Alignment/MuonAlignmentAlgorithms/python/align_cfg.py %s" % directory)
0676 
0677     bsubfile.append("cd %s" % directory)
0678 
0679     mapplots = False
0680     if mapplots_ingeneral and (iteration == 1 or iteration == 3 or iteration == 5 or iteration == 7 or iteration == 9 or iteration == ITERATIONS): mapplots = True
0681     segdiffplots = False
0682     if segdiffplots_ingeneral and (iteration == 1 or iteration == ITERATIONS): segdiffplots = True
0683     curvatureplots = False
0684     if curvatureplots_ingeneral and (iteration == 1 or iteration == ITERATIONS): curvatureplots = True
0685 
0686     ### gather.sh runners for njobs
0687     for jobnumber in range(njobs):
0688         if not options.inputInBlocks:
0689             inputfiles = " ".join(fileNames[jobnumber*stepsize:(jobnumber+1)*stepsize])
0690         else:
0691             inputfiles = " ".join(fileNamesBlocks[jobnumber])
0692 
0693         if mapplots or segdiffplots or curvatureplots: copyplots = "plotting*.root"
0694         else: copyplots = ""
0695 
0696         if len(inputfiles) > 0:
0697             gather_fileName = "%sgather%03d.sh" % (directory, jobnumber)
0698             writeGatherCfg(gather_fileName, vars())
0699             os.system("chmod +x %s" % gather_fileName)
0700             bsubfile.append("echo %sgather%03d.sh" % (directory, jobnumber))
0701 
0702             if last_align is None: waiter = ""
0703             else: waiter = "-w \"ended(%s)\"" % last_align            
0704             if options.big: queue = "cmscaf1nd"
0705             else: queue = "cmscaf1nh"
0706 
0707             bsubfile.append("bsub -R \"type==SLC6_64\" -q %s -J \"%s_gather%03d\" -u youremail.tamu.edu %s gather%03d.sh" % (queue, director, jobnumber, waiter, jobnumber))
0708 
0709             bsubnames.append("ended(%s_gather%03d)" % (director, jobnumber))
0710 
0711 
0712     ### align.sh
0713     if SUPER_SPECIAL_XY_AND_DXDZ_ITERATIONS:
0714         if ( iteration == 1 or iteration == 3 or iteration == 5 or iteration == 7 or iteration == 9):
0715             tmp = station123params, station123params, useResiduals 
0716             station123params, station123params, useResiduals = "000010", "000010", "0010"
0717             writeAlignCfg("%salign.sh" % directory, vars())
0718             station123params, station123params, useResiduals = tmp
0719         elif ( iteration == 2 or iteration == 4 or iteration == 6 or iteration == 8 or iteration == 10):
0720             tmp = station123params, station123params, useResiduals 
0721             station123params, station123params, useResiduals = "110001", "100001", "1100"
0722             writeAlignCfg("%salign.sh" % directory, vars())
0723             station123params, station123params, useResiduals = tmp
0724     else:
0725         writeAlignCfg("%salign.sh" % directory, vars())
0726 
0727     os.system("chmod +x %salign.sh" % directory)
0728 
0729     bsubfile.append("echo %salign.sh" % directory)
0730     if user_mail: bsubfile.append("bsub -R \"type==SLC6_64\" -q cmscaf1nd -J \"%s_align\" -u %s -w \"%s\" align.sh" % (director, user_mail, " && ".join(bsubnames)))
0731     else: bsubfile.append("bsub -R \"type==SLC6_64\" -q cmscaf1nd -J \"%s_align\" -w \"%s\" align.sh" % (director, " && ".join(bsubnames)))
0732 
0733     #bsubfile.append("cd ..")
0734     bsubnames = []
0735     last_align = "%s_align" % director
0736 
0737 
0738     ### after the last iteration (optionally) do diagnostics run
0739     if len(validationLabel) and iteration == ITERATIONS:
0740         # do we have plotting files created?
0741         directory1 = "%s01/" % DIRNAME
0742         director1 = directory1[:-1]
0743 
0744         writeValidationCfg("%svalidation.sh" % directory, vars())
0745         os.system("chmod +x %svalidation.sh" % directory)
0746 
0747         bsubfile.append("echo %svalidation.sh" % directory)
0748         if user_mail: bsubfile.append("bsub -R \"type==SLC6_64\" -q cmscaf1nd -J \"%s_validation\" -u %s -w \"ended(%s)\" validation.sh" % (director, user_mail, last_align))
0749         else: bsubfile.append("bsub -R \"type==SLC6_64\" -q cmscaf1nd -J \"%s_validation\" -w \"ended(%s)\" validation.sh" % (director, last_align))
0750 
0751     bsubfile.append("cd ..")
0752     bsubfile.append("")
0753 
0754 
0755 file(options.submitJobs, "w").write("\n".join(bsubfile))
0756 os.system("chmod +x %s" % options.submitJobs)