Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
#!/usr/bin/env python3
"""
_RunExpressProcessing_

Test wrapper to generate an express processing config and actually push
it into cmsRun for testing with a few input files etc from the command line

"""

import sys
import getopt
import traceback
import pickle

from Configuration.DataProcessing.GetScenario import getScenario



class RunExpressProcessing:

    def __init__(self):
        self.scenario = None
        self.writeRAW = False
        self.writeRECO = False
        self.writeFEVT = False
        self.writeDQM = False
        self.writeDQMIO = False
        self.noOutput = False
        self.globalTag = None
        self.inputLFN = None
        self.alcaRecos = None
        self.nThreads = None
        self.dat = False

    def __call__(self):
        if self.scenario == None:
            msg = "No --scenario specified"
            raise RuntimeError(msg)
        if self.globalTag == None:
            msg = "No --global-tag specified"
            raise RuntimeError(msg)
        if self.inputLFN == None:
            msg = "No --lfn specified"
            raise RuntimeError(msg)
        
        try:
            scenario = getScenario(self.scenario)
        except Exception as ex:
            msg = "Error getting Scenario implementation for %s\n" % (
                self.scenario,)
            msg += str(ex)
            raise RuntimeError(msg)

        print("Retrieved Scenario: %s" % self.scenario)
        print("Using Global Tag: %s" % self.globalTag)

        dataTiers = []
        if self.writeRAW:
            dataTiers.append("RAW")
            print("Configuring to Write out RAW")
        if self.writeRECO:
            dataTiers.append("RECO")
            print("Configuring to Write out RECO")
        if self.writeFEVT:
            dataTiers.append("FEVT")
            print("Configuring to Write out FEVT")
        if self.writeDQM:
            dataTiers.append("DQM")
            print("Configuring to Write out DQM")
        if self.writeDQMIO:
            dataTiers.append("DQMIO")
            print("Configuring to Write out DQMIO")
        if self.alcaRecos:
            dataTiers.append("ALCARECO")
            print("Configuring to Write out ALCARECO")

        try:
            kwds = {}

            if self.noOutput:
                kwds['outputs'] = []
            else:
                outputs = []
                for dataTier in dataTiers:
                    outputs.append({ 'dataTier' : dataTier,
                                     'eventContent' : dataTier,
                                     'moduleLabel' : "write_%s" % dataTier })
                kwds['outputs'] = outputs

                if self.alcaRecos:
                    kwds['skims'] = self.alcaRecos

            if self.nThreads:
                kwds['nThreads'] = int(self.nThreads)

            if self.dat:
                kwds['inputSource'] = 'DAT'

            process = scenario.expressProcessing(self.globalTag, **kwds)

        except NotImplementedError as ex:
            print("This scenario does not support Express Processing:\n")
            return
        except Exception as ex:
            msg = "Error creating Express Processing config:\n"
            msg += traceback.format_exc()
            raise RuntimeError(msg)

        process.source.fileNames = [self.inputLFN]

        import FWCore.ParameterSet.Config as cms

        process.maxEvents = cms.untracked.PSet( input = cms.untracked.int32(10) )

        pklFile = open("RunExpressProcessingCfg.pkl", "wb")
        psetFile = open("RunExpressProcessingCfg.py", "w")
        try:
            pickle.dump(process, pklFile, protocol=0)
            psetFile.write("import FWCore.ParameterSet.Config as cms\n")
            psetFile.write("import pickle\n")
            psetFile.write("handle = open('RunExpressProcessingCfg.pkl','rb')\n")
            psetFile.write("process = pickle.load(handle)\n")
            psetFile.write("handle.close()\n")
            psetFile.close()
        except Exception as ex:
            print("Error writing out PSet:")
            print(traceback.format_exc())
            raise ex
        finally:
            psetFile.close()
            pklFile.close()

        cmsRun = "cmsRun -e RunExpressProcessingCfg.py"
        print("Now do:\n%s" % cmsRun)



if __name__ == '__main__':
    valid = ["scenario=", "raw", "reco", "fevt", "dqm", "dqmio", "no-output",
             "global-tag=", "lfn=", "dat", 'alcarecos=', "nThreads="]
    usage = \
"""
RunExpressProcessing.py <options>

Where options are:
 --scenario=ScenarioName
 --raw (to enable RAW output)
 --reco (to enable RECO output)
 --fevt (to enable FEVT output)
 --dqm (to enable DQM output)
 --no-output (create config with no output, overrides other settings)
 --global-tag=GlobalTag
 --lfn=/store/input/lfn
 --dat (to enable streamer files as input)
 --alcarecos=plus_seprated_list
 --nThreads=Number_of_cores_or_Threads_used

Examples:

python RunExpressProcessing.py --scenario cosmics --global-tag GLOBALTAG --lfn /store/whatever --fevt --dqmio --alcarecos=TkAlCosmics0T+SiStripCalZeroBias

python RunExpressProcessing.py --scenario pp --global-tag GLOBALTAG --lfn /store/whatever --dat --fevt --dqmio --alcarecos=TkAlMinBias+SiStripCalZeroBias

"""
    try:
        opts, args = getopt.getopt(sys.argv[1:], "", valid)
    except getopt.GetoptError as ex:
        print(usage)
        print(str(ex))
        sys.exit(1)


    expressinator = RunExpressProcessing()

    for opt, arg in opts:
        if opt == "--scenario":
            expressinator.scenario = arg
        if opt == "--raw":
            expressinator.writeRAW = True
        if opt == "--reco":
            expressinator.writeRECO = True
        if opt == "--fevt":
            expressinator.writeFEVT = True
        if opt == "--dqm":
            expressinator.writeDQM = True
        if opt == "--dqmio":
            expressinator.writeDQMIO = True
        if opt == "--no-output":
            expressinator.noOutput = True
        if opt == "--global-tag":
            expressinator.globalTag = arg
        if opt == "--lfn" :
            expressinator.inputLFN = arg
        if opt == "--alcarecos":
            expressinator.alcaRecos = [ x for x in arg.split('+') if len(x) > 0 ]
        if opt == "--nThreads":
            expressinator.nThreads = arg
        if opt == "--dat":
            expressinator.dat = True

    expressinator()