lheutils 0.0.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
lheutils/__init__.py ADDED
@@ -0,0 +1 @@
1
+ from lheutils._version import version as __version__
lheutils/_version.py ADDED
@@ -0,0 +1,34 @@
1
+ # file generated by setuptools-scm
2
+ # don't change, don't track in version control
3
+
4
+ __all__ = [
5
+ "__version__",
6
+ "__version_tuple__",
7
+ "version",
8
+ "version_tuple",
9
+ "__commit_id__",
10
+ "commit_id",
11
+ ]
12
+
13
+ TYPE_CHECKING = False
14
+ if TYPE_CHECKING:
15
+ from typing import Tuple
16
+ from typing import Union
17
+
18
+ VERSION_TUPLE = Tuple[Union[int, str], ...]
19
+ COMMIT_ID = Union[str, None]
20
+ else:
21
+ VERSION_TUPLE = object
22
+ COMMIT_ID = object
23
+
24
+ version: str
25
+ __version__: str
26
+ __version_tuple__: VERSION_TUPLE
27
+ version_tuple: VERSION_TUPLE
28
+ commit_id: COMMIT_ID
29
+ __commit_id__: COMMIT_ID
30
+
31
+ __version__ = version = '0.0.2'
32
+ __version_tuple__ = version_tuple = (0, 0, 2)
33
+
34
+ __commit_id__ = commit_id = None
@@ -0,0 +1,159 @@
1
+ #!/usr/bin/env python3
2
+ """
3
+ CLI tool to convert LHE files with different compression and weight format options.
4
+
5
+ This tool allows you to convert Les Houches Event (LHE) files from one format
6
+ to another, with options to change compression and weight format.
7
+ """
8
+
9
+ import argparse
10
+ import signal
11
+ import sys
12
+ from pathlib import Path
13
+ from typing import Optional
14
+
15
+ import pylhe
16
+
17
+ import lheutils
18
+ from lheutils.cli.util import create_base_parser
19
+
20
+ # We do not want a Python Exception on broken pipe, which happens when piping to 'head' or 'less'
21
+ signal.signal(signal.SIGPIPE, signal.SIG_DFL)
22
+
23
+
24
+ def convert_lhe_file(
25
+ input_file: str,
26
+ output_file: Optional[str] = None,
27
+ compress: bool = False,
28
+ weight_format: str = "rwgt",
29
+ ) -> tuple[int, str]:
30
+ """Convert an LHE file with specified options.
31
+
32
+ Args:
33
+ input_file: Path to the input LHE file
34
+ output_file: Path to the output LHE file (None for stdout)
35
+ compress: Whether to compress the output file
36
+ weight_format: Weight format to use ('rwgt', 'init-rwgt', or 'none')
37
+ """
38
+ try:
39
+ # Read the input file
40
+ if input_file == "-":
41
+ lhefile = pylhe.LHEFile.frombuffer(sys.stdin)
42
+ else:
43
+ lhefile = pylhe.LHEFile.fromfile(input_file)
44
+
45
+ # Determine weight options based on format
46
+ if weight_format == "rwgt":
47
+ rwgt = True
48
+ weights = False
49
+ elif weight_format == "init-rwgt":
50
+ rwgt = True
51
+ weights = True
52
+ elif weight_format == "none":
53
+ rwgt = False
54
+ weights = False
55
+ else:
56
+ return 1, f"Error: Invalid weight format: {weight_format}"
57
+
58
+ # Write the output file
59
+ if output_file is None:
60
+ if compress:
61
+ return (
62
+ 1,
63
+ f"Error: Compression option ignored when writing to stdout (use `lhe2lhe {input_file} | gzip`)",
64
+ )
65
+ lhefile.write(
66
+ sys.stdout,
67
+ rwgt=rwgt,
68
+ weights=weights,
69
+ )
70
+ else:
71
+ lhefile.tofile(
72
+ output_file,
73
+ gz=compress,
74
+ rwgt=rwgt,
75
+ weights=weights,
76
+ )
77
+
78
+ except FileNotFoundError:
79
+ if input_file == "-":
80
+ return 1, "Error: Unable to read from stdin"
81
+ return 1, f"Error: Input file '{input_file}' not found"
82
+ except Exception as e:
83
+ source = "stdin" if input_file == "-" else f"input file '{input_file}'"
84
+ return 1, f"Error during conversion from {source}: {e}"
85
+ return 0, "Conversion successful"
86
+
87
+
88
+ def main() -> None:
89
+ """Main CLI function."""
90
+ parser = create_base_parser(
91
+ description="Convert LHE files with different compression and weight format options",
92
+ formatter_class=argparse.RawDescriptionHelpFormatter,
93
+ epilog="""
94
+ Examples:
95
+ lhe2lhe input.lhe # Convert to stdout
96
+ lhe2lhe input.lhe output.lhe # Basic conversion
97
+ lhe2lhe input.lhe output.lhe.gz --compress # Compress output
98
+ lhe2lhe input.lhe output.lhe --weight-format init-rwgt # Use init-rwgt format
99
+ lhe2lhe input.lhe.gz output.lhe --weight-format none # Remove weights
100
+ lhe2lhe input.lhe output.lhe.gz -c -w rwgt # Short options
101
+ lhe2lhe input.lhe | gzip > output.lhe.gz # Pipe to compress
102
+ cat input.lhe | lhe2lhe # Convert from stdin to stdout
103
+ lhe2lhe < input.lhe > output.lhe # Redirect stdin/stdout
104
+
105
+ Weight formats:
106
+ rwgt - Include weights in 'rwgt' format (default)
107
+ init-rwgt - Include weights in 'init-rwgt' format (both rwgt and weights)
108
+ none - Exclude all weights
109
+ """,
110
+ )
111
+
112
+ parser.add_argument(
113
+ "input", nargs="?", default="-", help="Input LHE file (default: stdin)"
114
+ )
115
+ parser.add_argument("output", nargs="?", help="Output LHE file (default: stdout)")
116
+
117
+ parser.add_argument(
118
+ "--compress",
119
+ "-c",
120
+ action="store_true",
121
+ help="Compress the output file (ignored if output filename ends with .gz/.gzip)",
122
+ )
123
+
124
+ parser.add_argument(
125
+ "--weight-format",
126
+ "-w",
127
+ choices=["rwgt", "init-rwgt", "none"],
128
+ default="rwgt",
129
+ help="Weight format to use in output (default: rwgt)",
130
+ )
131
+
132
+ args = parser.parse_args()
133
+
134
+ # Validate input file exists (skip validation for stdin)
135
+ if args.input != "-":
136
+ input_path = Path(args.input)
137
+ if not input_path.exists():
138
+ print(f"Error: Input file '{args.input}' does not exist", file=sys.stderr)
139
+ sys.exit(1)
140
+
141
+ # Check if output directory exists and create it if needed
142
+ if args.output is not None:
143
+ output_path = Path(args.output)
144
+ output_path.parent.mkdir(parents=True, exist_ok=True)
145
+
146
+ # Perform the conversion
147
+ retcode, message = convert_lhe_file(
148
+ args.input,
149
+ args.output,
150
+ args.compress,
151
+ args.weight_format,
152
+ )
153
+ if retcode != 0:
154
+ print(message, file=sys.stderr)
155
+ sys.exit(retcode)
156
+
157
+
158
+ if __name__ == "__main__":
159
+ main()