A package for handling Lorentz vectors with NumPy and Awkward array backends
Project description
LVec
⚠️ This project is a work in progress
A Python package for seamless handling of Lorentz vectors, 2D vectors, and 3D vectors in HEP analysis, bridging the gap between Scikit-HEP and ROOT ecosystems.
Motivation
LVec aims to simplify HEP analysis by providing a unified interface for working with various vector types across different frameworks. It seamlessly integrates with both the Scikit-HEP ecosystem (uproot, vector, awkward) and ROOT/PyROOT, enabling physicists to write more maintainable and efficient analysis code.
Installation
pip install lvec
For development installation:
git clone https://github.com/MohamedElashri/lvec
cd lvec
pip install -e ".[dev]"
Quick Start
Lorentz Vectors
from lvec import LVec
import numpy as np
# Create a single Lorentz vector
v = LVec(px=1.0, py=2.0, pz=3.0, E=4.0)
# Access properties
print(f"Mass: {v.mass}")
print(f"pt: {v.pt}")
# Create from pt, eta, phi, mass
v2 = LVec.from_ptepm(pt=5.0, eta=0.0, phi=0.0, m=1.0)
# Vector operations
v3 = v1 + v2
v4 = v1 * 2.0
2D Vectors
from lvec import Vector2D
# Create a 2D vector
vec2d = Vector2D(x=3.0, y=4.0)
# Access properties
print(f"Magnitude: {vec2d.r}")
print(f"Angle (phi): {vec2d.phi}")
# Rotate vector
rotated = vec2d.rotate(angle=np.pi/4) # 45 degrees rotation
3D Vectors
from lvec import Vector3D
# Create a 3D vector
vec3d = Vector3D(x=1.0, y=2.0, z=3.0)
# Access properties
print(f"Magnitude: {vec3d.r}")
print(f"Theta: {vec3d.theta}")
print(f"Phi: {vec3d.phi}")
# Rotate around axis
rotated = vec3d.rotate(theta=np.pi/2, axis=[0, 1, 0]) # 90 degrees around y-axis
Array Operations
# Works with numpy arrays
px = np.array([1.0, 2.0, 3.0])
py = np.array([2.0, 3.0, 4.0])
pz = np.array([3.0, 4.0, 5.0])
E = np.array([4.0, 5.0, 6.0])
vectors = LVec(px, py, pz, E)
# Works with awkward arrays
import awkward as ak
vectors_ak = LVec(ak.Array(px), ak.Array(py), ak.Array(pz), ak.Array(E))
Available Methods
Lorentz Vector (LVec) Methods
| Method | Description | Parameters | Returns |
|---|---|---|---|
__init__ |
Create a Lorentz vector | px, py, pz, E |
LVec |
from_ptepm |
Create from pt, eta, phi, mass | pt, eta, phi, m |
LVec |
from_p4 |
Create from px, py, pz, E | px, py, pz, E |
LVec |
from_ary |
Create from dictionary | ary_dict with px, py, pz, E keys |
LVec |
from_vec |
Create from vector-like object | vobj with px, py, pz, E attributes |
LVec |
boost |
Boost vector to new frame | bx, by, bz |
LVec |
mass |
Get invariant mass | - | float |
pt |
Get transverse momentum | - | float |
eta |
Get pseudorapidity | - | float |
phi |
Get azimuthal angle | - | float |
E |
Get energy | - | float |
p |
Get total momentum | - | float |
2D Vector (Vector2D) Methods
| Method | Description | Parameters | Returns |
|---|---|---|---|
__init__ |
Create a 2D vector | x, y |
Vector2D |
x |
Get x component | - | float |
y |
Get y component | - | float |
r |
Get vector magnitude | - | float |
phi |
Get azimuthal angle | - | float |
dot |
Compute dot product | other |
float |
3D Vector (Vector3D) Methods
| Method | Description | Parameters | Returns |
|---|---|---|---|
__init__ |
Create a 3D vector | x, y, z |
Vector3D |
x |
Get x component | - | float |
y |
Get y component | - | float |
z |
Get z component | - | float |
r |
Get vector magnitude | - | float |
rho |
Get cylindrical radius | - | float |
phi |
Get azimuthal angle | - | float |
theta |
Get polar angle | - | float |
dot |
Compute dot product | other |
float |
cross |
Compute cross product | other |
Vector3D |
Requirements
- Python >= 3.10
- NumPy >= 1.20.0
- Awkward >= 2.0.0
For development:
- pytest >= 7.0.0
- uproot >= 5.0.0
Citation
If you use LVec in your research, please cite:
@software{lvec2024,
author = {Mohamed Elashri},
title = {LVec: A Python package for handling Lorentz vectors},
year = {2024},
publisher = {GitHub},
url = {https://github.com/MohamedElashri/lvec}
}
Documentation
For detailed documentation and examples, visit our documentation page.
Examples
Check out our examples directory for comprehensive examples including:
- Basic vector operations
- Decay reconstructions
- Frame transformations
- 2D vector manipulations
- 3D spatial analysis
- Advanced selections
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file lvec-0.1.4.tar.gz.
File metadata
- Download URL: lvec-0.1.4.tar.gz
- Upload date:
- Size: 114.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
acffd2fb064e2c73b818aa7fa1f2b4e6dbc867ed3144c18df19c96b9b9c63e7b
|
|
| MD5 |
7f753cfd2652778d9ea52726fb9e9fc5
|
|
| BLAKE2b-256 |
d3b4326e7ad966cbbdcd78c48017f431cc6225eef747747633419f19b52d5504
|
Provenance
The following attestation bundles were made for lvec-0.1.4.tar.gz:
Publisher:
python-publish.yml on MohamedElashri/lvec
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
lvec-0.1.4.tar.gz -
Subject digest:
acffd2fb064e2c73b818aa7fa1f2b4e6dbc867ed3144c18df19c96b9b9c63e7b - Sigstore transparency entry: 192079476
- Sigstore integration time:
-
Permalink:
MohamedElashri/lvec@1a48f16e9102550698ed87adb84fb83e4efc775d -
Branch / Tag:
refs/tags/v0.1.4 - Owner: https://github.com/MohamedElashri
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@1a48f16e9102550698ed87adb84fb83e4efc775d -
Trigger Event:
release
-
Statement type:
File details
Details for the file lvec-0.1.4-py3-none-any.whl.
File metadata
- Download URL: lvec-0.1.4-py3-none-any.whl
- Upload date:
- Size: 48.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c45a7c1c0dcc4e0c7786f399e5cc5d37e87eb3a6bc5d4ded4ca8679c20d28693
|
|
| MD5 |
40bdcd08fb186733393aacdca8972d46
|
|
| BLAKE2b-256 |
61f2967b976315d93bb2f45d335052ae0ee9910a86c12fdf8c28219c249aee60
|
Provenance
The following attestation bundles were made for lvec-0.1.4-py3-none-any.whl:
Publisher:
python-publish.yml on MohamedElashri/lvec
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
lvec-0.1.4-py3-none-any.whl -
Subject digest:
c45a7c1c0dcc4e0c7786f399e5cc5d37e87eb3a6bc5d4ded4ca8679c20d28693 - Sigstore transparency entry: 192079478
- Sigstore integration time:
-
Permalink:
MohamedElashri/lvec@1a48f16e9102550698ed87adb84fb83e4efc775d -
Branch / Tag:
refs/tags/v0.1.4 - Owner: https://github.com/MohamedElashri
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@1a48f16e9102550698ed87adb84fb83e4efc775d -
Trigger Event:
release
-
Statement type: