Skip to content

Make the magnetic field evaluation reentrant - #16057

Open
sawenzel wants to merge 1 commit into
AliceO2Group:devfrom
sawenzel:swenzel/g4mt-pr3-reentrant-field
Open

sawenzel wants to merge 1 commit into
AliceO2Group:devfrom
sawenzel:swenzel/g4mt-pr3-reentrant-field

Conversation

@sawenzel

@sawenzel sawenzel commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator

This makes the evaluation of the magnetic field safe to call from several threads on one shared MagneticField object, which is what multithreaded Geant4 workers will do.

So far, Chebyshev3D and Chebyshev3DCalc evaluated through scratch arrays held as class members, and the TPC integrals in MagneticWrapperChebyshev used static coordinates. Concurrent calls therefore overwrote each other's intermediate results and returned wrong field values. The scratch now lives on the stack. It is sized for 64 rows and columns, while the ALICE field maps use at most 14. Larger parameterisations fall back to a thread-local buffer.

Note that the arithmetic, including the narrowing of the mapped coordinates to float, is unchanged, so the field values are bit-identical. A seeded o2-sim run (10 Pythia8 pp events, all detectors, Geant4, -j 8) gives identical kinematics and hits compared with dev. In a standalone benchmark of the evaluation the difference is within 2 %.

A new unit test evaluates the field, getBz and the two TPC integrals from 8 threads and compares them with a sequential pass. On dev this test fails, on this branch it passes.

Relates to https://its.cern.ch/jira/browse/O2-7305

Assisted by Claude Code.

This removes the shared scratch state from the Chebyshev field evaluation, so that one field can be evaluated from several threads.

- Chebyshev3D keeps the mapped coordinates in a local array instead of a member.
- Chebyshev3DCalc evaluates into scratch on the stack, or into a thread-local buffer for parameterisations larger than 64 rows or columns. The ALICE maps use at most 14.
- MagneticWrapperChebyshev no longer uses static coordinates in the TPC integrals.
- The arithmetic is unchanged, so the field values are bit-identical.
- A unit test evaluates the field from 8 threads and compares with a sequential pass.

This prepares the simulation for multithreaded Geant4, where all workers share one field.

https://its.cern.ch/jira/browse/O2-7305

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sawenzel
sawenzel requested a review from shahor02 as a code owner November 5, 2026 21:23

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant