HEP Units and Constants
hepunits is a Python library that provides a comprehensive set of units and physical constants specifically tailored for the High Energy Physics (HEP) system of units. It simplifies working with quantities in HEP research by offering well-defined units like MeV and constants such as the speed of light. The library is actively maintained, with regular updates for Python version compatibility and minor feature enhancements, currently at version 2.4.4.
Warnings
- breaking Python version support is frequently updated. For example, Python 3.8 support was removed in v2.4.3, and Python 3.7 support was removed in v2.3.4. Always check `requires_python` in `pyproject.toml` or `setup.py` for your target version.
- gotcha The `Pint` integration for unit-aware quantity objects is optional and requires `pip install hepunits[pint]`. If you attempt to use `hepunits.pint` functionality without installing the extra dependency, you will encounter an `ImportError`.
- gotcha hepunits is based on the High Energy Physics (HEP) system of units, where common units like energy are in MeV and length in mm, and constants like the speed of light are defined accordingly (e.g., `c_light` is in mm/ns). This differs from standard SI, which can lead to unexpected results if not accounted for.
- gotcha Unit and constant objects (e.g., `units.GeV`, `constants.c_light`) are not themselves `Pint` Quantities unless you explicitly use the `hepunits.pint` integration and its unit registry. Basic unit access provides bare numeric values when multiplied or divided.
Install
-
pip install hepunits -
pip install hepunits[pint]
Imports
- units
from hepunits import units
- constants
from hepunits import constants
- get_unit_registry
import hepunits.pint; ureg = hepunits.pint.get_unit_registry()
Quickstart
from hepunits import units, constants
# Accessing units
energy = 100 * units.GeV
print(f"Energy: {energy}")
# Accessing constants
speed_of_light = constants.c_light
print(f"Speed of light: {speed_of_light} in mm/ns")
# Using Pint integration (requires pip install hepunits[pint])
try:
import hepunits.pint
ureg = hepunits.pint.get_unit_registry()
mass = 938.27 * ureg.MeV / ureg.c**2
print(f"Proton mass (Pint): {mass}")
except ImportError:
print("Pint integration not installed. Skipping Pint example.")