12 """Runs the splitting routines"""
14 import eformat, logging
15 import EventApps.myopt
as myopt
16 from libpyevent_storage
import CompressionType
17 from libpyeformat_helper
import SourceIdentifier, SubDetector
22 option[
'start-event'] = {
'short':
'a',
'arg':
True,
25 'description':
'Number of events which should be skipped from the begin'}
27 option[
'max-events'] = {
'short':
'n',
'arg':
True,
30 'description':
'Maximum number of events in the output file. 0 means, all useful events from the input.'}
32 option[
'verbosity'] = {
'short':
'v',
'arg':
True,
33 'default': logging.INFO,
35 'description':
'Log verbosity'}
37 option[
'progress-bar'] = {
'short':
'P',
'arg':
False,
40 'description':
'Show progress bar when running interactively'}
42 option[
'output-dir'] = {
'short':
'd',
'arg':
True,
45 'description':
'Directory in which the output file should be written'}
47 option[
'uncompressed'] = {
'short':
'u',
'arg':
False,
50 'description':
'Write out uncompressed data (default without this option is compressed)'}
53 option[
'stream-name'] = {
'short':
's',
'arg':
True,
55 'group':
'Stream Tag',
56 'description':
'Name(s) of stream(s) which should be written out, e.g. "stream1,stream2,stream3"'}
58 option[
'project-tag'] = {
'short':
'p',
'arg':
True,
60 'group':
'Stream Tag',
61 'description':
'Project tag which should be used for the output file'}
63 option[
'lumi-block'] = {
'short':
'l',
'arg':
True,
65 'group':
'Stream Tag',
66 'description':
'Lumiblock number used for the output file. Use 0 if multiple LB in file.'}
69 option[
'hlt-only'] = {
'short':
'm',
'arg':
True,
71 'group':
'HLT Result',
72 'description':
'Drop all detector data and write out only HLT data for the given module ID.' + \
73 ' Module ID <0 is a wildcard for all HLT module IDs.'}
75 parser = myopt.Parser(extra_args=
True)
76 for (k,v)
in option.items():
77 parser.add_option(k, v[
'short'], v[
'description'], v[
'arg'], v[
'default'],v[
'group'])
79 if len(sys.argv) == 1:
80 print (parser.usage(
'global "%s" options:' % sys.argv[0]))
84 (kwargs, extra) = parser.parse(sys.argv[1:], prefix=
'global "%s" options:' % sys.argv[0])
87 logging.getLogger(
'').name = os.path.splitext(os.path.basename(sys.argv[0]))[0]
88 logging.getLogger(
'').
setLevel(kwargs[
'verbosity'])
91 stream = eformat.istream(extra)
96 dr = eformat.EventStorage.pickDataReader(extra[0])
99 df = eformat.EventStorage.RawFileName(extra[0])
102 projectTag = dr.projectTag()
103 lumiBlockNumber = dr.lumiblockNumber()
104 applicationName =
'athenaHLT'
105 streamType =
'unknown'
106 if df.hasValidCore() :
107 productionStep = df.productionStep()
109 productionStep =
'unknown'
112 runNumber = dr.runNumber()
113 outputDirectory = kwargs[
'output-dir']
114 streamName = kwargs[
'stream-name']
116 streamNames_out = streamName.split(
',')
117 if len(streamNames_out) > 1:
118 streamName =
'accepted'
120 if kwargs[
'project-tag']
is not None:
121 projectTag = kwargs[
'project-tag']
122 if kwargs[
'lumi-block'] != -1:
123 lumiBlockNumber = kwargs[
'lumi-block']
125 if (lumiBlockNumber==0):
126 productionStep =
'merge'
129 if (
not os.path.exists(outputDirectory))
or (
not os.path.isdir(outputDirectory)):
130 logging.fatal(
' Output directory %s does not exist ' % outputDirectory)
137 totalEvents_skipped = 0
144 if kwargs[
'start-event'] > 0:
145 kwargs[
'start-event'] -= 1
146 totalEvents_skipped += 1
149 if kwargs[
'max-events'] > 0
and totalEvents_in >= kwargs[
'max-events']:
150 logging.info(
' Maximum number of events reached : %d', kwargs[
'max-events'])
154 streamTags = e.stream_tag()
155 logging.debug(
' === New Event nr = %s (Run,Global ID) = (%d,%d) === ', totalEvents_in,e.run_no(),e.global_id())
158 for tag
in streamTags:
159 if tag.name
in streamNames_out:
161 if streamAccepted :
continue
165 logging.debug(
' Matching event found for stream tag = %s', tag)
166 logging.debug(
' Stream Tag:Robs = %s', [hex(r)
for r
in tag.robs])
167 logging.debug(
' Stream Tag:Dets = %s', [hex(d)
for d
in tag.dets])
171 if lumiBlockNumber > 0:
172 if e.lumi_block() != lumiBlockNumber:
173 logging.error(
' Event (Run,Global ID) = (%d,%d) has a lumi block number %d,'
174 ' which is different from LB = %d for the output file. Event skipped.',
175 e.run_no(),e.global_id(),e.lumi_block(),lumiBlockNumber)
179 if e.run_no() != runNumber:
180 logging.error(
' Event (Run,Global ID) = (%d,%d) has a run number,'
181 ' which is different from the run number = %d for the output file. Event skipped.',
182 e.run_no(),e.global_id(),runNumber)
186 if streamType != tag.type:
187 streamType = tag.type
188 logging.debug(
' streamType set to = %s', streamType)
190 outRawFile = eformat.EventStorage.RawFileName(projectTag,
197 logging.debug(
' set output file name = %s', outRawFile.fileNameCore())
200 compressionTypeES = CompressionType.NONE
if kwargs[
'uncompressed']
else CompressionType.ZLIB
201 compressionType = eformat.helper.Compression.UNCOMPRESSED
if kwargs[
'uncompressed'] \
202 else eformat.helper.Compression.ZLIB
203 compressionLevel = 0
if kwargs[
'uncompressed']
else 1
206 ostream = eformat.ostream(directory=outputDirectory,
207 core_name=outRawFile.fileNameCore(),
208 run_number=dr.runNumber(),
209 trigger_type=dr.triggerType(),
210 detector_mask=dr.detectorMask(),
211 beam_type=dr.beamType(),
212 beam_energy=dr.beamEnergy(),
213 compression=compressionTypeES,
214 complevel=compressionLevel)
217 is_feb_tag = (len(tag.robs)==0
and len(tag.dets)==0)
218 if is_feb_tag
and not kwargs[
'hlt-only']:
220 pbev = eformat.write.FullEventFragment(e)
221 logging.debug(
' Write full event fragment ')
226 if kwargs[
'hlt-only']:
227 if int(kwargs[
'hlt-only']) < 0:
228 dets = [SubDetector.TDAQ_HLT]
if SubDetector.TDAQ_HLT
in tag.dets
or is_feb_tag
else []
229 robs = [robid
for robid
in tag.robs
if SourceIdentifier(robid).subdetector_id()==SubDetector.TDAQ_HLT]
231 requested_rob_id =
int(SourceIdentifier(SubDetector.TDAQ_HLT,
int(kwargs[
'hlt-only'])))
232 if SubDetector.TDAQ_HLT
in tag.dets
or requested_rob_id
in tag.robs
or is_feb_tag:
233 robs = [requested_rob_id]
235 dets =
list(tag.dets)
236 robs =
list(tag.robs)
240 logging.debug(
' Write partial event fragment ')
242 if rob.source_id().
code()
in robs:
243 rob_output_list.append(rob)
244 if rob.source_id().subdetector_id()
in dets:
245 rob_output_list.append(rob)
247 pbev = eformat.write.FullEventFragment()
249 for out_rob
in rob_output_list:
250 pbev.append_unchecked(out_rob)
253 pbev.compression_type(compressionType)
254 pbev.compression_level(compressionLevel)
256 if (logging.getLogger(
'').getEffectiveLevel() > logging.DEBUG)
and kwargs[
'progress-bar']:
257 sys.stdout.write(
'.')
264 logging.info(
'Total number of events processed = %d ', totalEvents_in)
265 logging.info(
'Number of events skipped at the beginning = %d ', totalEvents_skipped)
266 logging.info(
'Number of events written to output file = %d ', totalEvents_out)
267 if totalEvents_out > 0:
268 logging.info(
'Output file = %s ', ostream.last_filename())
270 logging.error(
'No events selected so no output file created')
275 if __name__ ==
"__main__":