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
# Convert the RAW data from EDM .root files into DAQ .raw format
#
# usage: cmsRun $CMSSW_RELEASE_BASE/HLTrigger/Tools/python/convertToRaw.py \
#           inputFiles=/store/path/file.root[,/store/path/file.root,...] \
#           runNumber=NNNNNN \
#           [lumiNumber=NNNN] \
#           [eventsPerFile=50] \
#           [eventsPerLumi=11650] \
#           [rawDataCollection=rawDataCollector] \
#           [outputPath=output_directory]
#
# The output files will appear as output_directory/runNNNNNN/runNNNNNN_lumiNNNN_indexNNNNNN.raw .

import sys
import os
import FWCore.ParameterSet.Config as cms
import FWCore.ParameterSet.VarParsing as VarParsing

process = cms.Process("FAKE")

process.maxEvents = cms.untracked.PSet(
    input = cms.untracked.int32(-1)                                 # to be overwritten after parsing the command line options
)

process.source = cms.Source("PoolSource",
    fileNames = cms.untracked.vstring()                             # to be overwritten after parsing the command line options
)

from EventFilter.Utilities.EvFDaqDirector_cfi import EvFDaqDirector as _EvFDaqDirector
process.EvFDaqDirector = _EvFDaqDirector.clone(
    baseDir = "",                                                   # to be overwritten after parsing the command line options
    buBaseDir = "",                                                 # to be overwritten after parsing the command line options
    runNumber = 0                                                   # to be overwritten after parsing the command line options
)

process.writer = cms.OutputModule("RawStreamFileWriterForBU",
    source = cms.InputTag('rawDataCollector'),                      # to be overwritten after parsing the command line options
    numEventsPerFile = cms.uint32(0)                                # to be overwritten after parsing the command line options
)

process.endpath = cms.EndPath(process.writer)

process.load('FWCore.MessageService.MessageLogger_cfi')
process.MessageLogger.cerr.FwkReport.reportEvery = 0                # to be overwritten after parsing the command line options

# parse command line options
options = VarParsing.VarParsing ('python')
for name in 'filePrepend', 'maxEvents', 'outputFile', 'secondaryOutputFile', 'section', 'tag', 'storePrepend', 'totalSections':
    del options._register[name]
    del options._beenSet[name]
    del options._info[name]
    del options._types[name]
    if name in options._singletons:
        del options._singletons[name]
    if name in options._lists:
        del options._lists[name]
    if name in options._noCommaSplit:
        del options._noCommaSplit[name]
    if name in options._noDefaultClear:
        del options._noDefaultClear[name]


options.register('runNumber',
                 0,
                 VarParsing.VarParsing.multiplicity.singleton,
                 VarParsing.VarParsing.varType.int,
                 "Run number to use")

options.register('lumiNumber',
                 None,
                 VarParsing.VarParsing.multiplicity.singleton,
                 VarParsing.VarParsing.varType.int,
                 "Luminosity section number to use")

options.register('eventsPerLumi',
                 11650,
                 VarParsing.VarParsing.multiplicity.singleton,
                 VarParsing.VarParsing.varType.int,
                 "Number of events in the given luminosity section to process")

options.register('eventsPerFile',
                 50,
                 VarParsing.VarParsing.multiplicity.singleton,
                 VarParsing.VarParsing.varType.int,
                 "Split the output into files with at most this number of events")

options.register('rawDataCollection',
                 'rawDataCollector',
                 VarParsing.VarParsing.multiplicity.singleton,
                 VarParsing.VarParsing.varType.string,
                 "FEDRawDataCollection to be repacked into RAW format")

options.register('outputPath',
                 os.getcwd(),
                 VarParsing.VarParsing.multiplicity.singleton,
                 VarParsing.VarParsing.varType.string,
                 "Output directory for the FED RAW data files")

options.parseArguments()

# check that the option values are valide
if options.runNumber <= 0:
    sys.stderr.write('Invalid run number\n')
    sys.exit(1)

if options.lumiNumber is not None and options.lumiNumber <= 0:
    sys.stderr.write('Invalid luminosity section number\n')
    sys.exit(1)

if options.eventsPerLumi == 0 or options.eventsPerLumi < -1:
    sys.stderr.write('Invalid number of events per luminosity section\n')
    sys.exit(1)

if options.eventsPerFile <= 0:
    sys.stderr.write('Invalid number of events per output file\n')
    sys.exit(1)

# configure the job based on the command line options
process.source.fileNames = options.inputFiles
if options.lumiNumber is not None:
    # process only one lumisection
    process.source.lumisToProcess = cms.untracked.VLuminosityBlockRange('%d:%d' % (options.runNumber, options.lumiNumber))
    process.maxEvents.input = options.eventsPerLumi
process.EvFDaqDirector.runNumber = options.runNumber
process.EvFDaqDirector.baseDir = options.outputPath
process.EvFDaqDirector.buBaseDir = options.outputPath
process.writer.source = options.rawDataCollection
process.writer.numEventsPerFile = options.eventsPerFile
process.MessageLogger.cerr.FwkReport.reportEvery = options.eventsPerFile

# create the output directory, if it does not exist
outputRunPath = f'{options.outputPath}/run{options.runNumber:06d}'
os.makedirs(outputRunPath, exist_ok=True)
open(f'{outputRunPath}/fu.lock', 'w').close()