forked from acts-project/acts
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy path__main__.py
More file actions
199 lines (169 loc) · 5.22 KB
/
__main__.py
File metadata and controls
199 lines (169 loc) · 5.22 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env python3
"""Command-line interface for HepMC3 file normalization.
This module can be invoked as:
python -m acts.examples.hepmc3 [options]
"""
import argparse
import json
import sys
from pathlib import Path
from . import (
Compression,
Format,
availableCompressionModes,
availableFormats,
normalizeFiles,
)
def main(prog: str):
parser = argparse.ArgumentParser(
prog=prog,
description="HepMC3 File Normalizer - Normalize and chunk HepMC files",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
# Multi-file mode with chunking
%(prog)s -i input1.hepmc3.gz input2.hepmc3 -n 1000 -c zstd -l 9
%(prog)s -i file.root -o normalized/ -p events -n 5000
# Single output mode (format/compression auto-detected)
%(prog)s -i input1.hepmc3.gz input2.hepmc3 -S combined.hepmc3.zst
%(prog)s -i input.hepmc3.gz -S output/events.hepmc3.gz
""",
)
parser.add_argument(
"-i",
"--input",
nargs="+",
required=True,
metavar="FILE",
help="Input HepMC files",
)
parser.add_argument(
"-S",
"--single-output",
metavar="FILE",
help="Write all events to a single output file. Format and compression are detected from filename.",
)
parser.add_argument(
"-o",
"--output-dir",
default=".",
metavar="DIR",
help="Output directory (ignored with --single-output) [default: .]",
)
parser.add_argument(
"-p",
"--output-prefix",
default="events",
metavar="PREFIX",
help="Output file prefix [default: events]",
)
parser.add_argument(
"-n",
"--events-per-file",
type=int,
default=10000,
metavar="N",
help="Number of events per output file (ignored with --single-output) [default: 10000]",
)
parser.add_argument(
"-m",
"--max-events",
type=int,
default=0,
metavar="N",
help="Maximum number of events to read (0 = all events) [default: 0]",
)
parser.add_argument(
"-c",
"--compression",
choices=["none", "zlib", "lzma", "bzip2", "zstd"],
default="none",
help="Compression type (ignored with --single-output) [default: none]",
)
parser.add_argument(
"-l",
"--compression-level",
type=int,
default=6,
metavar="LEVEL",
help="Compression level (0-19, higher = more compression) [default: 6]",
)
parser.add_argument(
"-f",
"--format",
choices=["ascii", "root"],
default="ascii",
help="Output format (ignored with --single-output) [default: ascii]",
)
parser.add_argument(
"-j",
"--json",
action="store_true",
help="Write JSON output with list of created files to stdout",
)
parser.add_argument("-v", "--verbose", action="store_true", help="Verbose output")
parser.add_argument(
"--list-compressions",
action="store_true",
help="List available compression modes and exit",
)
args = parser.parse_args()
# Handle --list-compressions
if args.list_compressions:
print("Available compression modes:")
for comp in availableCompressionModes():
print(f" {comp}")
print("\nAvailable formats:")
for fmt in availableFormats():
print(f" {fmt}")
return 0
# Convert string compression to enum
compression_map = {
"none": Compression.none,
"zlib": Compression.zlib,
"lzma": Compression.lzma,
"bzip2": Compression.bzip2,
"zstd": Compression.zstd,
}
# Convert string format to enum
format_map = {
"ascii": Format.ascii,
"root": Format.root,
}
try:
# Convert inputs to Path objects
input_files = [Path(f) for f in args.input]
single_output = Path(args.single_output) if args.single_output else None
# Run normalization
result = normalizeFiles(
inputFiles=input_files,
singleOutputPath=single_output,
outputDir=Path(args.output_dir),
outputPrefix=args.output_prefix,
eventsPerFile=args.events_per_file,
maxEvents=args.max_events,
format=format_map.get(args.format),
compression=compression_map.get(args.compression),
compressionLevel=args.compression_level,
verbose=args.verbose,
)
# Write JSON output if requested
if args.json:
output = {
"num_events": result.numEvents,
"num_files": len(result.outputFiles),
"files": [
{
"path": str(Path(f).absolute()),
"size": Path(f).stat().st_size if Path(f).exists() else None,
}
for f in result.outputFiles
],
}
print(json.dumps(output, indent=2))
return 0
except Exception as e:
print(f"ERROR: {e}", file=sys.stderr)
return 1
if __name__ == "__main__":
sys.exit(main(prog="acts.examples.hepmc3"))