Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-07-07 22:32:56

0001 #!/usr/bin/env python3
0002 """
0003 _RunAlcaSkimming_
0004 
0005 Test wrapper to generate an alca skimming config and push it into cmsRun for
0006 testing with a few input files etc from the command line
0007 
0008 """
0009 from __future__ import print_function
0010 
0011 import sys
0012 import getopt
0013 import pickle
0014 
0015 from Configuration.DataProcessing.GetScenario import getScenario
0016 
0017 
0018 
0019 class RunAlcaSkimming:
0020 
0021     def __init__(self):
0022         self.scenario = None
0023         self.skims = []
0024         self.inputLFN = None
0025 
0026     def __call__(self):
0027         if self.scenario == None:
0028             msg = "No --scenario specified"
0029             raise RuntimeError(msg)
0030         if self.inputLFN == None:
0031             msg = "No --lfn specified"
0032             raise RuntimeError(msg)
0033 
0034         if len(self.skims) == 0:
0035             msg = "No --skims provided, need at least one"
0036             raise RuntimeError(msg)
0037 
0038         if self.globalTag == None:
0039             msg = "No --global-tag specified"
0040             raise RuntimeError(msg)
0041 
0042         try:
0043             scenario = getScenario(self.scenario)
0044         except Exception as ex:
0045             msg = "Error getting Scenario implementation for %s\n" % (
0046                 self.scenario,)
0047             msg += str(ex)
0048             raise RuntimeError(msg)
0049 
0050         print("Retrieved Scenario: %s" % self.scenario)
0051         print("Creating ALCA skimming config with skims:")
0052         for skim in self.skims:
0053             print(" => %s" % skim)
0054             
0055         try:
0056             process = scenario.alcaSkim(self.skims, globaltag = self.globalTag)
0057         except NotImplementedError as ex:
0058             print("This scenario does not support Alca Skimming:\n")
0059             return
0060         except Exception as ex:
0061             msg = "Error creating Alca Skimming config:\n"
0062             msg += str(ex)
0063             raise RuntimeError(msg)
0064 
0065         process.source.fileNames.append(self.inputLFN)
0066 
0067         import FWCore.ParameterSet.Config as cms
0068 
0069         process.maxEvents = cms.untracked.PSet( input = cms.untracked.int32(10) )
0070 
0071         pklFile = open("RunAlcaSkimmingCfg.pkl", "wb")
0072         psetFile = open("RunAlcaSkimmingCfg.py", "w")
0073         try:
0074             pickle.dump(process, pklFile, protocol=0)
0075             psetFile.write("import FWCore.ParameterSet.Config as cms\n")
0076             psetFile.write("import pickle\n")
0077             psetFile.write("handle = open('RunAlcaSkimmingCfg.pkl','rb')\n")
0078             psetFile.write("process = pickle.load(handle)\n")
0079             psetFile.write("handle.close()\n")
0080             psetFile.close()
0081         except Exception as ex:
0082             print("Error writing out PSet:")
0083             print(traceback.format_exc())
0084             raise ex
0085         finally:
0086             psetFile.close()
0087             pklFile.close()
0088 
0089         cmsRun = "cmsRun -e RunAlcaSkimmingCfg.py"
0090         print("Now do:\n%s" % cmsRun)
0091 
0092 
0093 
0094 if __name__ == '__main__':
0095     valid = ["scenario=", "skims=", "lfn=","global-tag="]
0096 
0097     usage = \
0098 """
0099 RunAlcaSkimming.py <options>
0100 
0101 Where options are:
0102  --scenario=ScenarioName
0103  --lfn=/store/input/lfn
0104  --skims=comma,separated,list
0105  --global-tag=GlobalTag
0106 
0107 Example:
0108 python2.4 RunAlcaSkimming.py --scenario=Cosmics --lfn=/store/whatever --skims=MuAlStandAloneCosmics
0109 
0110 """
0111     try:
0112         opts, args = getopt.getopt(sys.argv[1:], "", valid)
0113     except getopt.GetoptError as ex:
0114         print(usage)
0115         print(str(ex))
0116         sys.exit(1)
0117 
0118 
0119     skimmer = RunAlcaSkimming()
0120 
0121     for opt, arg in opts:
0122         if opt == "--scenario":
0123             skimmer.scenario = arg
0124         if opt == "--lfn" :
0125             skimmer.inputLFN = arg
0126         if opt == "--skims":
0127             skimmer.skims = [ x for x in arg.split(',') if len(x) > 0 ]
0128         if opt == "--global-tag":
0129             skimmer.globalTag = arg
0130 
0131     skimmer()