6 Merges events from multiple raw files from one or more directories.
7 This is mainly for use on small samples
11 import libpyevent_storage
as EventStorage
19 parser = argparse.ArgumentParser(usage=
'%(prog)s [options] outBaseName dir1 [dir2 ...]',
21 formatter_class=
lambda prog: argparse.HelpFormatter(prog, max_help_position=36, width=100))
22 parser.add_argument(
'outBaseName',
23 help=
'Output file name base')
24 parser.add_argument(
'dirs',
27 help=
'Input directory (can be multiple)')
28 parser.add_argument(
'--copyFrom',
'-c',
29 metavar=
'FILE', action=
'store',
30 help=
'Copy metadata from other ByteStream file')
31 parser.add_argument(
'--nepfOutput',
'-n',
32 metavar=
'NUM', action=
'store', type=int, default=500,
33 help=
'Number of events per output file (default=%(default)s)')
34 parser.add_argument(
'--nepfInput',
'-N',
35 metavar=
'NUM', action=
'store', type=int, default=0,
36 help=
'Number of events per input file, 0 means all (default=%(default)s)')
37 parser.add_argument(
'--renumber',
'-r',
39 help=
'Renumber event IDs from 1')
40 parser.add_argument(
'--verbose',
'-v',
42 help=
'Increase output verbosity')
48 for dirname
in sorted(dirs):
49 files = os.listdir(dirname)
50 logging.debug(
'Found %d files in %s', len(files), dirname)
52 filenames.append(dirname +
'/' + f)
58 logging.basicConfig(stream=sys.stdout,
59 format=
'%(levelname)-8s %(message)s',
60 level=logging.DEBUG
if args.verbose
else logging.INFO)
63 logging.info(
'Merging %d files', len(filenames))
64 logging.info(
'filenames: %s', filenames)
67 dr = EventStorage.pickDataReader(filenames[0])
68 output = eformat.ostream(core_name=args.outBaseName,
69 run_number=dr.runNumber(),
70 trigger_type=dr.triggerType(),
71 detector_mask=dr.detectorMask(),
72 beam_type=dr.beamType(),
73 beam_energy=dr.beamEnergy())
74 output.max_events_per_file(args.nepfOutput)
76 for ifile, input_file
in enumerate(filenames):
77 logging.info(
"Opening file %d: %s", ifile+1, input_file)
78 inputFile = eformat.istream([input_file])
80 for ievt_in_file, event
in enumerate(inputFile):
81 if args.nepfInput > 0
and ievt_in_file >= args.nepfInput:
83 logging.debug(
"Reading event with Lvl1 Id = %ld", event.lvl1_id())
84 new_event = eformat.write.FullEventFragment(event)
86 logging.debug(
"Rewriting the event with Lvl1 Id = %ld", ievt)
87 new_event.lvl1_id(ievt)
88 new_event.global_id(ievt)
89 for rob
in new_event.children():
92 rob.rod_lvl1_trigger_type(event.lvl1_trigger_type())
94 logging.debug(
"Rewriting the event with unchanged Lvl1 Id")
96 output.write(new_event)
99 logging.info(
'Wrote %d events to %d files', ievt-1, 1+((ievt-2)//args.nepfOutput))
102 if "__main__" in __name__: