7 Write a copy of a ByteStream file with updated metadata
14 import libpyevent_storage
as EventStorage
27 parser = argparse.ArgumentParser(usage=
'%(prog)s [options] FILE',
29 formatter_class=
lambda prog: argparse.HelpFormatter(prog, max_help_position=36, width=100))
30 parser.add_argument(
'file',
32 help=
'input ByteStream file')
33 parser.add_argument(
'--outputName',
'-o',
34 metavar=
'NAME', action=
'store',
35 help=
'Output file name base. If not specified, created automatically from metadata')
36 parser.add_argument(
'--copyFrom',
'-c',
37 metavar=
'FILE', action=
'store',
38 help=
'Copy metadata from other ByteStream file')
39 parser.add_argument(
'--numEvents',
'-n',
40 metavar=
'NUM', action=
'store', type=int,
41 help=
'Copy only first NUM events from input to output')
42 parser.add_argument(
'--runNumber',
'-r',
43 metavar=
'NUM', action=
'store', type=int,
44 help=
'Change run number')
45 parser.add_argument(
'--stream',
'-s',
46 metavar=
'NAME', action=
'store',
47 help=
'Change stream name')
48 parser.add_argument(
'--projectTag',
'-p',
49 metavar=
'TAG', action=
'store',
50 help=
'Change project tag')
51 parser.add_argument(
'--beamType',
'-b',
52 metavar=
'TYPE', action=
'store',
53 choices=beam_type_dict.keys(),
54 help=
'Change beam type, possible values are: %(choices)s')
55 parser.add_argument(
'--beamEnergy',
'-e',
56 metavar=
'ENERGY', action=
'store', type=int,
57 help=
'Change beam energy')
58 parser.add_argument(
'--lumiBlock',
'-l',
59 metavar=
'LB', action=
'store', type=int,
60 help=
'Change lumi block number')
61 parser.add_argument(
'--triggerType',
'-t',
62 metavar=
'TT', action=
'store', type=int,
63 help=
'Change trigger type')
64 parser.add_argument(
'--detectorMask',
'-m',
65 metavar=
'MASK', action=
'store', type=int,
66 help=
'Change detector mask')
67 parser.add_argument(
'--verbose',
'-v',
69 help=
'Increase output verbosity')
75 logging.basicConfig(stream=sys.stdout,
76 format=
'%(levelname)-8s %(message)s',
77 level=logging.DEBUG
if args.verbose
else logging.INFO)
80 logging.info(
'Reading events from %s and metadata from %s', args.file, args.copyFrom)
82 logging.info(
'Reading events and metadata from %s', args.file)
83 meta_input = args.copyFrom
if args.copyFrom
else args.file
84 reader = EventStorage.pickDataReader(meta_input)
85 input_stream = eformat.istream(args.file)
90 metadata_basic[
'runNumber'] = reader.runNumber()
91 metadata_basic[
'triggerType'] = reader.triggerType()
92 metadata_basic[
'detectorMask'] = reader.detectorMask()
93 metadata_basic[
'beamType'] = reader.beamType()
94 metadata_basic[
'beamEnergy'] = reader.beamEnergy()
95 metadata_extra[
'Stream'] = reader.stream()
96 metadata_extra[
'Project'] = reader.projectTag()
97 metadata_extra[
'LumiBlock'] = reader.lumiblockNumber()
99 logging.debug(
'Input metadata_basic = %s', metadata_basic)
100 logging.debug(
'Input metadata_extra = %s', metadata_extra)
104 metadata_basic[
'runNumber'] = args.runNumber
106 metadata_basic[
'triggerType'] = args.triggerType
107 if args.detectorMask:
108 metadata_basic[
'detectorMask'] = args.detectorMask
110 metadata_basic[
'beamType'] = beam_type_dict[args.beamType]
112 metadata_basic[
'beamEnergy'] = args.beamEnergy
114 metadata_extra[
'Stream'] = args.stream
116 metadata_extra[
'Project'] = args.projectTag
118 metadata_extra[
'LumiBlock'] = args.lumiBlock
120 logging.debug(
'Updated metadata_basic = %s', metadata_basic)
121 logging.debug(
'Updated metadata_extra = %s', metadata_extra)
124 file_name_base = args.outputName
125 if not file_name_base:
127 ptag = metadata_extra[
'Project']
128 runno = metadata_basic[
'runNumber']
129 stream = metadata_extra[
'Stream']
130 lbn = metadata_extra[
'LumiBlock']
133 file_name_list.append(ptag
if ptag
else 'data')
134 file_name_list.append(
'{:08d}'.
format(runno
if runno
else 0))
135 file_name_list.append(stream
if stream
else 'unknown_stream')
136 file_name_list.append(
'lb{:04d}'.
format(lbn
if lbn
else 0))
137 file_name_base =
'.'.
join(file_name_list)
140 metadata_extra_strings = [
'{:s}={:s}'.
format(k,
str(v))
for k, v
in six.iteritems(metadata_extra)]
141 output_stream = eformat.ostream(
142 core_name = file_name_base,
143 run_number = metadata_basic[
'runNumber'],
144 trigger_type = metadata_basic[
'triggerType'],
145 detector_mask = metadata_basic[
'detectorMask'],
146 beam_type = metadata_basic[
'beamType'],
147 beam_energy = metadata_basic[
'beamEnergy'],
148 meta_data_strings = metadata_extra_strings)
150 logging.info(
'Writing file %s', output_stream.current_filename().
replace(
'.writing',
'.data'))
153 nmax = args.numEvents
or -1
154 for event
in input_stream:
156 if nmax >= 0
and ievt > nmax:
158 logging.debug(
'Writing event %d', ievt)
159 output_stream.write(event)
162 if '__main__' in __name__: