Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:33:19

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