|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# hddm_cull_events - python implementation of the old C++ utility |
| 4 | +# of the same name and options. |
| 5 | +# |
| 6 | +# author: richard.t.jones at uconn.edu |
| 7 | +# versoin: april 22, 2025 |
| 8 | + |
| 9 | +import os |
| 10 | +import sys |
| 11 | +import hddm_r |
| 12 | +import hddm_s |
| 13 | + |
| 14 | +def usage(): |
| 15 | + print("Usage:") |
| 16 | + print(" hddm_cull_events [-oOutputfile] [-sNeventsToSkip]", |
| 17 | + "[-kNeventsToKeep] file1.hddm file2.hddm ...") |
| 18 | + print() |
| 19 | + print(" options:") |
| 20 | + print(" -o<Outputfile> Set output filename [culled.hddm]") |
| 21 | + print(" -s<NeventsToSkip> Set number of events to skip [0]") |
| 22 | + print(" -k<NeventsToKeep> Set number of events to keep [1]") |
| 23 | + print(" -e<EventPosition> Keep only the single, specified file position") |
| 24 | + print(" -E<EventNumber> Keep only the single, specified event number") |
| 25 | + print(" -r Input file is in REST format [sim format]") |
| 26 | + print(" -I Enable data integrity checks on output HDDM stream") |
| 27 | + print(" -C Enable compression on output HDDM stream") |
| 28 | + print(""" |
| 29 | + This will copy a continguous set of events from the combined event streams |
| 30 | + into a seperate output file. The primary use for this would be to copy |
| 31 | + a single, problematic event into a seperate file for easier debugging. |
| 32 | +
|
| 33 | + If the -eNNN option is used then only a single event is extracted |
| 34 | + (file position NNN) and written to a file with the name evtNNN.hddm. |
| 35 | + |
| 36 | + If the -ENNN option is used then only a single event is extracted |
| 37 | + (the specified event number) and written to a file with the name evtNNN.hddm. |
| 38 | + Note that the event is counted from the begining of the file, starting |
| 39 | + with "1". This does NOT look at the event number stored in the event itself. |
| 40 | + """) |
| 41 | + |
| 42 | +if len(sys.argv) < 2 or sys.argv[1][:2] == "-h" or sys.argv[1][:2] == "-?" or sys.argv[1][:2] == "--": |
| 43 | + usage() |
| 44 | + sys.exit(1) |
| 45 | + |
| 46 | +outfile = "culled.hddm" |
| 47 | +skipevents = 0 |
| 48 | +keepevents = -1 |
| 49 | +startpos = 0 |
| 50 | +startevno = 0 |
| 51 | +hddm_ = hddm_s |
| 52 | +integrity = 0 |
| 53 | +compression = 0 |
| 54 | + |
| 55 | +argc = 1 |
| 56 | +while argc < len(sys.argv): |
| 57 | + arg = sys.argv[argc] |
| 58 | + argc += 1 |
| 59 | + try: |
| 60 | + if len(arg) > 1: |
| 61 | + if arg[:2] == "-o": |
| 62 | + if len(arg) > 2: |
| 63 | + outfile = arg[2:] |
| 64 | + elif len(sys.argv) > argc + 1: |
| 65 | + outfile = sys.argv[argc] |
| 66 | + argc += 1 |
| 67 | + elif arg[:2] == '-s': |
| 68 | + if len(arg) > 2: |
| 69 | + skipevents = int(arg[2:]) |
| 70 | + elif len(sys.argv) > argc + 1: |
| 71 | + skipevents = int(sys.argv[argc]) |
| 72 | + argc += 1 |
| 73 | + elif arg[:2] == '-k': |
| 74 | + if len(arg) > 2: |
| 75 | + keepevents = int(arg[2:]) |
| 76 | + elif len(sys.argv) > argc + 1: |
| 77 | + keepevents = int(sys.argv[argc]) |
| 78 | + argc += 1 |
| 79 | + elif arg[:2] == '-e': |
| 80 | + if len(arg) > 2: |
| 81 | + startpos = [int(a) for a in arg[2:].split(',')] |
| 82 | + elif len(sys.argv) > argc + 1: |
| 83 | + startpos = [int(a) for a in sys.argv[argc].split(',')] |
| 84 | + argc += 1 |
| 85 | + elif arg[:2] == '-E': |
| 86 | + if len(arg) > 2: |
| 87 | + startevno = int(arg[2:]) |
| 88 | + elif len(sys.argv) > argc + 1: |
| 89 | + startevno = int(sys.argv[argc]) |
| 90 | + argc += 1 |
| 91 | + elif arg[:2] == '-r': |
| 92 | + hddm_ = hddm_r |
| 93 | + elif arg[:2] == '-I': |
| 94 | + integrity = hddm_.k_crc32_integrity |
| 95 | + elif arg[:2] == '-C': |
| 96 | + integrity = hddm_.k_bz2_compression |
| 97 | + elif os.path.isfile(arg): |
| 98 | + argc -= 1 |
| 99 | + break |
| 100 | + else: |
| 101 | + print("hddm_cull_events error: invalid argument", arg) |
| 102 | + usage() |
| 103 | + sys.exit(2) |
| 104 | + except: |
| 105 | + print("hddm_cull_events error: invalid argument", arg) |
| 106 | + usage() |
| 107 | + sys.exit(3) |
| 108 | + |
| 109 | +hddmout = hddm_.ostream(outfile) |
| 110 | +hddmout.integrityChecks = integrity |
| 111 | +hddmout.compression = compression |
| 112 | + |
| 113 | +while argc < len(sys.argv): |
| 114 | + hddmin = hddm_.istream(sys.argv[argc]) |
| 115 | + argc += 1 |
| 116 | + if skipevents: |
| 117 | + hddmin.skip(skipevents) |
| 118 | + skipevents = 0 |
| 119 | + if startpos: |
| 120 | + hddmin.position = hddm_.streamposition(*startpos) |
| 121 | + startpos = 0 |
| 122 | + for rec in hddmin: |
| 123 | + if keepevents == 0: |
| 124 | + break |
| 125 | + evno = 9e9 |
| 126 | + for pev in rec.getPhysicsEvents(): |
| 127 | + evno = pev.eventNo |
| 128 | + if evno < startevno: |
| 129 | + continue |
| 130 | + startevno = 0 |
| 131 | + hddmout.write(rec) |
| 132 | + keepevents -= 1 |
| 133 | + print("wrote event", evno, " with file position", hddmin.position) |
0 commit comments