Skip to main content

A package for handling Lorentz vectors with NumPy and Awkward array backends

Project description

lvec CI

LVec

PyPI version License: GPL v3

⚠️ 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

lvec-0.1.4.tar.gz (114.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lvec-0.1.4-py3-none-any.whl (48.6 kB view details)

Uploaded Python 3

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

Hashes for lvec-0.1.4.tar.gz
Algorithm Hash digest
SHA256 acffd2fb064e2c73b818aa7fa1f2b4e6dbc867ed3144c18df19c96b9b9c63e7b
MD5 7f753cfd2652778d9ea52726fb9e9fc5
BLAKE2b-256 d3b4326e7ad966cbbdcd78c48017f431cc6225eef747747633419f19b52d5504

See more details on using hashes here.

Provenance

The following attestation bundles were made for lvec-0.1.4.tar.gz:

Publisher: python-publish.yml on MohamedElashri/lvec

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for lvec-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 c45a7c1c0dcc4e0c7786f399e5cc5d37e87eb3a6bc5d4ded4ca8679c20d28693
MD5 40bdcd08fb186733393aacdca8972d46
BLAKE2b-256 61f2967b976315d93bb2f45d335052ae0ee9910a86c12fdf8c28219c249aee60

See more details on using hashes here.

Provenance

The following attestation bundles were made for lvec-0.1.4-py3-none-any.whl:

Publisher: python-publish.yml on MohamedElashri/lvec

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page