Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-26 02:34:38

0001 #!/usr/bin/env cmsRun
0002 
0003 import shutil
0004 import sys
0005 
0006 from Validation.RecoTau.ValidationOptions_cff import *
0007 import Validation.RecoTau.RecoTauValidation_cfi as validation
0008 
0009 process = cms.Process("TEST")
0010 
0011 # command options defined in Validation/RecoTau/python/ValidationOptions_cfi
0012 options.parseArguments()
0013 
0014 checkOptionsForBadInput()
0015 
0016 if not calledBycmsRun() and not options.gridJob:
0017    print("Run 'cmsRun RunTauValidation_cfg.py help' for options.")
0018    # quit here so we dont' create a bunch of directories
0019    #  if the user only wants the help
0020    #sys.exit()
0021 
0022 # Make sure we dont' clobber another directory! Skip in batch mode (runs from an LSF machine)
0023 if not CMSSWEnvironmentIsCurrent() and options.batchNumber == -1 and not options.gridJob:
0024    print("CMSSW_BASE points to a different directory, please rerun cmsenv!")
0025    sys.exit()
0026 
0027 
0028 # DQM store, PDT sources etc
0029 process.load("Configuration.StandardSequences.Services_cff")
0030 
0031 ######################################
0032 #                                    #
0033 #       Output Info Store            #
0034 #                                    #
0035 ######################################
0036 
0037 """
0038    Data is stored in
0039 
0040    TauID/[EventType]_[DataSource]_[Conditions][label]
0041 
0042 """
0043 
0044 #outputDirName = "Validation_%s" % ReleaseVersion
0045 outputDirName = "TauID"
0046 
0047 
0048 outputDir = os.path.join(os.getcwd(), outputDirName) 
0049 # This is the directory where we store the stuff about our current configuration
0050 outputBaseDir = outputDir
0051 
0052 subDirName = ""
0053 
0054 subDirName += "%s_%s" % (options.eventType, options.dataSource)
0055 
0056 if options.conditions != "whatever":
0057    subDirName += "_%s" % options.conditions.replace('::', '_')
0058 
0059 if (options.label != "none"):
0060    subDirName += "_" + options.label
0061 
0062 outputDir = os.path.join(outputDir, subDirName)
0063 
0064 # Store configuration, showtags, etc in a sub directory
0065 configDir = os.path.join(outputDir, "Config")
0066 
0067 if os.path.exists(outputDir) and options.batchNumber < 0:# and not options.gridJob:
0068    print("Output directory %s already exists!  OK to overwrite?" % outputDir)
0069    while True:
0070       input = raw_input("Please enter [y/n] ")
0071       if (input == 'y'):
0072          break
0073       elif (input == 'n'):
0074          print(" ...exiting.")
0075          sys.exit()
0076 
0077 if not os.path.exists(outputDir):
0078    os.makedirs(outputDir)
0079 
0080 if not os.path.exists(configDir):
0081    os.makedirs(configDir)
0082 
0083 ######################################
0084 #                                    #
0085 #       Data Source Setup            #
0086 #                                    #
0087 ######################################
0088 
0089 def LoadDataCffFile(theFile):
0090    outputFileName = os.path.join(configDir, "DataSource_cff.py")
0091    process.load(theFile)
0092    outputFile = open(outputFileName,'w')
0093    outputFile.write('import FWCore.ParameterSet.Config as cms\n')
0094    outputFile.write('source = %s\n'%process.source)
0095 
0096 process.schedule = cms.Schedule()
0097 
0098 # Check if we are simulating events - if so we need to define our generator
0099 if options.dataSource.find('sim') != -1:
0100    if options.eventType == "ZTT":
0101       process.load("Configuration.Generator.ZTT_Tauola_All_hadronic_cfi")
0102    elif options.eventType == "QCD":
0103       process.load("Configuration.Generator.QCDForPF_cfi")
0104 
0105 # Run on a RECO (eg RelVal)
0106 if options.dataSource.find('recoFiles') != -1:
0107    myFile = options.sourceFile
0108    if myFile == 'none':
0109       myFile = "Validation.RecoTau.sources.EventSource_%s_RECO_cff" % options.eventType
0110       #myFile = os.path.join(ReleaseBase, "Validation/RecoTau/test", "EventSource_%s_RECO_cff.py" % options.eventType)
0111    LoadDataCffFile(myFile)
0112    if len(process.source.fileNames) == 0 and not options.gridJob:
0113       import Validation.RecoTau.DBSApi_cff as mydbs
0114       if os.path.isfile('SourcesDatabase.xml'):
0115          print("Trying to retrieve the input files from SourcesDatabase.xml...")
0116          xml = open('SourcesDatabase.xml','r')
0117          mydbs.loadXML(xml,options.eventType,process.source)
0118       if len(process.source.fileNames) == 0:
0119          print("Accessing DBS to retrieve the input files...")
0120          mydbs.FillSource(options.eventType,process.source)
0121       if len(process.source.fileNames) == 0:
0122          sys.exit(0)
0123       print(process.source)
0124    # check if we want to rerun PFTau
0125    if options.dataSource.find('PFTau') != -1:
0126       process.load("Configuration.StandardSequences.GeometryRecoDB_cff")
0127       process.load("Configuration.StandardSequences.FrontierConditions_GlobalTag_cff")
0128       process.load("Configuration.StandardSequences.MagneticField_cff")
0129       process.load("RecoTauTag.Configuration.RecoPFTauTag_cff")
0130       process.runPFTau = cms.Path(process.PFTau)
0131       process.schedule.append(process.runPFTau)
0132 
0133 # Run on DIGI files and re-RECO
0134 elif options.dataSource == 'digiFiles':
0135    myFile = options.sourceFile
0136    if myFile == 'none':
0137       myFile = "Validation.RecoTau.EventSource_%s_DIGI_cff" % options.eventType
0138       #myFile = os.path.join(ReleaseBase, "Validation/RecoTau/test", "EventSource_%s_DIGI_cff.py" % options.eventType)
0139    LoadDataCffFile(myFile)
0140    # get the sequences need to redo RECO
0141    process.load("Validation.RecoTau.ProduceTausFromDigis_cff")
0142    process.makeTausFromDigiFiles = cms.Path(proces.makeTausFromDigis)
0143    process.schedule.append(process.makeTausFromDigiFiles)
0144 
0145 # Generate FASTSIM DATA
0146 elif options.dataSource == 'fastsim':
0147    process.load("Validation.RecoTau.ProduceTausWithFastSim_cff")
0148    process.fastSimTaus = cms.Path(process.makeTausWithFastSim)
0149    process.schedule.append(process.fastSimTaus)
0150 
0151 # Generate FULLSIM DATA
0152 elif options.dataSource == 'fullsim':
0153    process.load("Validation.RecoTau.ProduceFullSimAndDigisForTaus_cff")
0154    process.load("Validation.RecoTau.ProduceTausFromDigis_cff")
0155    process.fullSimTaus = cms.Path(process.simAndDigitizeForTaus*process.makeTausFromDigis)
0156    process.schedule.append(process.fullSimTaus)
0157 
0158 # Specify conditions if desired
0159 if options.conditions != "whatever":
0160    process.load("Configuration.StandardSequences.FrontierConditions_GlobalTag_cff")
0161    process.GlobalTag.globaltag = options.conditions
0162 
0163 # have to set max events here, since it may get written by the 
0164 # dataSource cffs
0165 process.maxEvents = cms.untracked.PSet(
0166     input = cms.untracked.int32(options.maxEvents)
0167 )
0168 
0169 # Skip events, if we are running in batch mode on files
0170 if options.batchNumber >= 0 and options.dataSource.find('Files') != -1:
0171    process.source.skipEvents = cms.untracked.uint32(options.batchNumber*options.maxEvents)
0172 
0173 ######################################
0174 #                                    #
0175 #       Validation Setup             #
0176 #                                    #
0177 ######################################
0178 
0179 # Store the tags and CVS diff to the tags, and the current release
0180 #  only do this once in a batch job.  The additional tar file is a fail safe - 
0181 #  the parameters shouldn't change in outputDir.
0182 if (options.batchNumber <= 0 ):#and not options.gridJob):
0183    os.system("cd $CMSSW_BASE/src; \
0184               showtags -t -r > showtags.txt; \
0185               cvs -q diff >& diffToTags.patch;\
0186               cvs -q diff -r %s >& diffToVanillaRelease.patch; \
0187               tar -cvzf TagsAndDiff.tar.gz showtags.txt *.patch; \
0188               mv showtags.txt *.patch %s; \
0189               mv TagsAndDiff.tar.gz %s" % (ReleaseVersion, configDir, configDir))
0190 
0191 if options.batchNumber >= 0:
0192    # store the batch produced root files in a sub directory
0193    outputDir = os.path.join(outputDir, "BatchJobs")
0194    if not os.path.exists(outputDir):
0195       os.mkdir(outputDir)
0196 
0197 #Validation output file
0198 outputFileNameBase = "TauVal_%s" % ReleaseVersion
0199 if options.label != "none":
0200    outputFileNameBase += "_%s" % options.label
0201 outputFileNameBase += "_"
0202 outputFileNameBase += options.eventType
0203 
0204 if options.batchNumber >= 0:
0205    outputFileNameBase += "_%i" % options.batchNumber
0206    options.writeEDMFile = options.writeEDMFile.replace(".root", "_%i.root" % options.batchNumber)
0207 outputFileNameBase += ".root"
0208 
0209 if validation.StandardMatchingParameters.recoCuts.value() != "" and validation.StandardMatchingParameters.genCuts.value() != "":
0210   print('Matching: cut(s) set to: reco "%s", gen "%s".' % (validation.StandardMatchingParameters.recoCuts.value(), validation.StandardMatchingParameters.genCuts.value()))
0211 else:
0212   if validation.StandardMatchingParameters.recoCuts.value() != "":
0213     print('Matching: reco cut(s) set to: "%s".' % validation.StandardMatchingParameters.recoCuts.value())
0214   if validation.StandardMatchingParameters.genCuts.value() != "":
0215     print('Matching: gen cut(s) set to: "%s".' % validation.StandardMatchingParameters.genCuts.value())
0216 
0217 if options.gridJob:
0218    outputFileName = 'TauVal_GridJob.root'
0219 else:
0220    outputFileName = os.path.join(outputDir, outputFileNameBase)
0221 
0222 print('The output file will be: '+outputFileName)
0223 if options.gridJob:
0224    cfg=open('./crab.cfg', 'r')
0225    cfgContent=cfg.read()
0226    if cfgContent.find(outputFileName) == -1:
0227       print("ERROR: CRAB output file not matching the grid one!\nexiting...")
0228       sys.exit()
0229 
0230 process.saveTauEff = cms.EDAnalyzer("TauDQMSimpleFileSaver",
0231   outputFileName = cms.string(outputFileName)
0232 )
0233 
0234 process.load("Validation.RecoTau.dataTypes.ValidateTausOn%s_cff" % options.eventType)
0235 process.validation = cms.Path( process.produceDenominator )#getattr(process,'produceDenominator'+options.eventType) )
0236 
0237 if options.batchNumber >= 0 or options.gridJob:
0238    process.validation *= process.runTauValidationBatchMode #in batch mode, the efficiencies are not computed - only the num/denom
0239 else:
0240    process.validation *= process.runTauValidation
0241 
0242 process.validation *= process.saveTauEff #save the output
0243 
0244 process.schedule.append(process.validation)
0245 
0246 if options.batchNumber >= 0:
0247    newSeed = process.RandomNumberGeneratorService.theSource.initialSeed.value() + options.batchNumber 
0248    process.RandomNumberGeneratorService.theSource.initialSeed = cms.untracked.uint32(newSeed)
0249    process.RandomNumberGeneratorService.generator.initialSeed = cms.untracked.uint32(newSeed)
0250    print("I'm setting the random seed to ", newSeed)
0251 
0252 
0253 process.load("RecoTauTag.Configuration.RecoTauTag_EventContent_cff")
0254 
0255 TauTagValOutputCommands = cms.PSet(
0256       outputCommands = cms.untracked.vstring('drop *',
0257          'keep recoPFCandidates_*_*_*',
0258          'keep *_genParticles*_*_*',
0259          'keep *_ak5GenJets_*_*',
0260          'keep *_tauGenJets*_*_*',
0261          'keep *_selectedGenTauDecays*_*_*'
0262          )
0263       )
0264 
0265 TauTagValOutputCommands.outputCommands.extend(process.RecoTauTagRECO.outputCommands)
0266 
0267 # talk to output module
0268 if options.writeEDMFile != "":
0269    # Find where the EDM file should be written.  This is set by the 
0270    #  to the working directory when running jobs on lxbatch
0271    try:
0272       edmOutputDir = os.environ['edmOutputDir']
0273       options.writeEDMFile = os.path.join(edmOutputDir, options.writeEDMFile)
0274    except KeyError:
0275       pass
0276 
0277    process.out = cms.OutputModule("PoolOutputModule",
0278          TauTagValOutputCommands,
0279          verbose = cms.untracked.bool(False),
0280          fileName = cms.untracked.string (options.writeEDMFile)
0281    )
0282    myOutpath = cms.EndPath(process.out)
0283    process.schedule.append(myOutpath)
0284 
0285 if options.myModifications != ['none']:
0286    for aModifier in options.myModifications:
0287       process.load(aModifier.replace('.py',''))
0288 
0289 
0290 ######################################
0291 #                                    #
0292 #       CFG dump                     #
0293 #                                    #
0294 ######################################
0295 #process.Timing = cms.Service("Timing",
0296 #         useJobReport = cms.untracked.bool(True)
0297 #    )
0298 #process.SimpleMemoryCheck = cms.Service("SimpleMemoryCheck",
0299 #         useJobReport = cms.untracked.bool(True)
0300 #    )
0301 
0302 #if grid job end here
0303 if not options.gridJob:
0304 
0305    dumpFileName = "cfgDump"
0306    if options.batchNumber >= 0:
0307       dumpFileName += "_"
0308       dumpFileName += str(options.batchNumber)
0309       
0310    dumpFileName += ".py"
0311    
0312    processDumpFile = open('%s/%s' % (configDir, dumpFileName), 'w')
0313    
0314    print(process.dumpPython(), file=processDumpFile)
0315