Skip to main content

Python API for GEMC geometry definition and output analysis

Project description

pygemc

Tests Python PyPI License: GEMC GEMC documentation

pygemc is the Python API used by GEMC to define detector geometry, materials, optical properties, mirrors, geometry variations, and lightweight output-analysis workflows. It lets users build GEMC databases with Python scripts, preview geometry with PyVista, and inspect GEMC CSV or ROOT output without writing C++.

The package is installed as part of the GEMC source build and can also be developed as a standalone Python project.

Features

  • Python classes for GEMC geometry and material databases
  • GVolume helpers for common Geant4 solids such as boxes, tubes, cones, and trapezoids
  • GMaterial helpers for chemical formulas, fractional-mass mixtures, and optical/scintillation properties
  • GConfiguration run, variation, factory, SQLite, ASCII, and PyVista configuration handling
  • autogeometry() convenience setup for detector scripts
  • SQLite and ASCII database output
  • PyVista rendering, interactive Qt display, and VTK.js .vtksz export for geometry inspection and documentation
  • gemc-system-template CLI for generating ready-to-run detector systems
  • Python code snippets for supported Geant4 solid constructors
  • gemc-analyzer CLI for summarizing and plotting GEMC CSV or ROOT output
  • Unit conversion helpers for length, angle, time, and energy strings
  • Pytest suite that does not require a compiled gemc binary

Installation

Use a Python virtual environment for direct pip installs:

python3 -m venv ~/venv/pygemc
source ~/venv/pygemc/bin/activate
python -m pip install --upgrade pip

Stable PyPI Install

Install pygemc with:

python -m pip install pygemc

Optional ROOT-file analysis dependencies:

python -m pip install "pygemc[root]"

Development Snapshot

Install the moving GitHub dev prerelease with:

python -m pip install "pygemc @ git+https://github.com/gemc/pygemc.git@dev"

Use this when you need the latest development version before the next stable PyPI release.

Local Clone Development Install

Use this when you are editing pygemc from a local clone:

git clone https://github.com/gemc/pygemc.git
cd pygemc
python -m pip install -e ".[dev]"

Optional ROOT-file analysis dependencies for local development:

python -m pip install -e ".[dev,root]"

The package requires Python 3.10 or newer and depends on NumPy, VTK, PyVista, PyVistaQt, PyQt6, pandas, and matplotlib.

Installed with GEMC

When GEMC is built from source, the parent Meson project installs pygemc into the GEMC Python environment. After adding the GEMC Python environment to PATH, the commands below are available:

gemc-system-template --help
gemc-analyzer --help

Quickstart

Create a detector template:

gemc-system-template -s counter
cd counter
./counter.py

The generated system contains:

File Purpose
counter.py Main geometry-builder script
geometry.py Example volumes, including a flux detector
materials.py Example methane-gas material
counter.yaml GEMC steering card
README.md Placeholder notes for the generated detector

Run with PyVista visualization:

./counter.py -pv

Export a VTK.js scene:

./counter.py -pvvtk counter -pvz 0.25

Run the generated simulation with GEMC when the compiled gemc executable is available:

gemc counter.yaml

Analyze output:

gemc-analyzer counter_t0_digitized.csv totEdep --kind csv --bins 50

Geometry API

Typical geometry scripts create a configuration and publish volumes/materials to it:

from pygemc import GMaterial, GVolume, autogeometry

cfg = autogeometry("examples", "counter")

gas = GMaterial("methaneGas")
gas.description = "methane gas CH4 0.000667 g/cm3"
gas.density = 0.000667
gas.addNAtoms("C", 1)
gas.addNAtoms("H", 4)
gas.publish(cfg)

flux = GVolume("flux_box")
flux.description = "air flux box"
flux.make_box(40.0, 40.0, 2.0)
flux.set_position(0, 0, 100)
flux.material = "G4_AIR"
flux.color = "3399FF"
flux.style = 1
flux.digitization = "flux"
flux.set_identifier("box", 2)
flux.publish(cfg)

Common command-line options accepted by geometry scripts:

Option Purpose
-f, --factory Select sqlite or ascii output
-v, --variation Select the geometry variation
-r, --run Select the run number
-sql, --dbhost Select the SQLite file path
-pv Show a PyVista window
-pvb Show a PyVistaQt background plotter
-pvvtk Export a VTK.js .vtksz scene
-pvz Set the VTK.js export zoom

PyVista Visualization

PyVista support is central to pygemc: geometry scripts can display the detector as they build it, open an interactive Qt viewer, or export a .vtksz scene that can be published in documentation.

B1 B2
B1 PyVista geometry B2 PyVista geometry
Materials Simple Flux
Materials PyVista geometry Simple Flux PyVista geometry

Open the linked interactive PyVista scenes generated from the GEMC examples.

GitHub README pages cannot embed .vtksz files directly, so the preview image links to the hosted VTK.js viewer.

Command-Line Tools

gemc-system-template

Generate a detector skeleton:

gemc-system-template -s counter

List supported solid snippets:

gemc-system-template -sl

Print a volume-construction snippet:

gemc-system-template -gv G4Box

Write a snippet to a file:

gemc-system-template -gv G4Tubs -write_to geometry.py -geo_sub build_tube

gemc-analyzer

Summarize an output file:

gemc-analyzer counter_t0_digitized.csv --kind csv

Plot a variable:

gemc-analyzer counter_t0_digitized.csv totEdep --kind csv --bins 50

Save a figure without opening a GUI:

gemc-analyzer out.root E --kind root --detector flux --save energy.png

Analyzer inputs:

  • CSV output files or CSV root names
  • ROOT files when pygemc[root] dependencies are installed
  • Digitized and true-information data streams

Tests

Run the standalone Python tests:

pytest
pytest tests/test_cli.py
pytest tests/test_geometry.py
pytest -v
pytest -k "sqlite"

The tests cover CLI behavior and geometry database generation. They intentionally do not require Geant4 or a compiled gemc executable; full simulation tests live in the parent GEMC Meson build.

Project Layout

Path Purpose
src/pygemc/api/ Geometry, materials, units, SQLite output, PyVista support, and templates
src/pygemc/analyzer/ CSV/ROOT readers, plotting, and analyzer CLI
tests/ Standalone pytest suite
releases/ Release notes
pyproject.toml Python packaging metadata and console scripts
meson.build Meson subproject integration used by GEMC

Documentation

Contributing

Keep patches focused and run the relevant pytest targets before opening a pull request. If a change affects the integrated GEMC build, also run the parent repository Meson tests for the affected examples or modules.

License

pygemc is distributed under the GEMC Software License, the same license used by the main GEMC source repository. See LICENSE.md.

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

pygemc-0.2.0.tar.gz (52.2 kB view details)

Uploaded Source

Built Distribution

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

pygemc-0.2.0-py3-none-any.whl (63.9 kB view details)

Uploaded Python 3

File details

Details for the file pygemc-0.2.0.tar.gz.

File metadata

  • Download URL: pygemc-0.2.0.tar.gz
  • Upload date:
  • Size: 52.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pygemc-0.2.0.tar.gz
Algorithm Hash digest
SHA256 588769a8d26cf7bb5674d6299066f8d4ad864746ff91f58f9326607abaaf571b
MD5 d85d2f434c6b6fb728f3cf47529e829e
BLAKE2b-256 262874334410cd1f392759e4f091ef300fb32d76feb6dee9fb71879c985440e2

See more details on using hashes here.

Provenance

The following attestation bundles were made for pygemc-0.2.0.tar.gz:

Publisher: publish_pypi.yml on gemc/pygemc

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

File details

Details for the file pygemc-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: pygemc-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 63.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pygemc-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9ed326e3919cfeadfa7b4cdff4a7a7647dd266b60b949f9faf971e984fec7df2
MD5 690befa255598a88af667a1cab9a0712
BLAKE2b-256 41f9bb7490acb1a2fa0d8bb2330bb8ebeb0d56f4f6a1a3678e6e373d501ab675

See more details on using hashes here.

Provenance

The following attestation bundles were made for pygemc-0.2.0-py3-none-any.whl:

Publisher: publish_pypi.yml on gemc/pygemc

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