Skip to content

feat: convert FieldMapB to using covfie#934

Open
wdconinc wants to merge 11 commits intomainfrom
covfie
Open

feat: convert FieldMapB to using covfie#934
wdconinc wants to merge 11 commits intomainfrom
covfie

Conversation

@wdconinc
Copy link
Copy Markdown
Contributor

@wdconinc wdconinc commented Sep 4, 2025

Briefly, what does this PR introduce?

Needs:

This PR converts the FieldMapB handling to using covfie for all interpolation etc. This will allow us to pass-through covfie to ACTS and others(*), while continuing to use the DD4hep field accessors where necessary.

(*) No demo of this. Not sure all necessary parts are there. You'd need to access the bare cvf file.

After profiling with this change, the cost of fieldComponent calls went from 12% to 7% (and due to the way covfie works with templated code, all calls could be inlined so there is no remaining branching).

What kind of change does this PR introduce?

  • Bug fix (issue #__)
  • New feature (issue #__)
  • Documentation update
  • Other: __

Please check if this PR fulfills the following:

  • Tests for the changes have been added
  • Documentation has been added / updated
  • Changes have been communicated to collaborators

Does this PR introduce breaking changes? What changes might users need to make to their code?

No.

Does this PR change default behavior?

No.

@github-actions github-actions Bot added the topic: infrastructure Regarding build system, CI, CD label Sep 4, 2025
Copilot AI review requested due to automatic review settings February 24, 2026 18:10
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR migrates FieldMapB magnetic field-map sampling/interpolation to use the covfie field infrastructure, with the goal of enabling covfie pass-through to downstream consumers while keeping the DD4hep field interface.

Changes:

  • Added covfie-based field types and loading for BrBz and BxByBz map formats.
  • Replaced manual (bi/tri)-linear interpolation with covfie field_view::at(...) sampling.
  • Added covfie as a CMake dependency.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
src/FieldMapB.cpp Switches field-map storage/sampling to covfie-backed fields and removes the custom interpolation/indexing logic.
CMakeLists.txt Adds covfie discovery via find_package(covfie REQUIRED).
Comments suppressed due to low confidence (1)

CMakeLists.txt:67

  • find_package(covfie REQUIRED) is added, but the plugin target is not linked against any covfie imported target (nor are covfie include dirs applied). Unless covfie headers are in a global include path, this will break builds; please attach covfie to the ${a_lib_name} target (e.g., via target_link_libraries with the package’s exported target) so include paths/compile options propagate correctly.
# Dependencies
find_package(DD4hep 1.27 REQUIRED COMPONENTS DDCore DDRec)
find_package(fmt REQUIRED)
find_package(covfie REQUIRED)
find_package(IRT2 2.1.1)

#-----------------------------------------------------------------------------------
set(a_lib_name ${PROJECT_NAME})

set(DD4HEP_SET_RPATH TRUE)
dd4hep_configure_output(INSTALL ${CMAKE_INSTALL_PREFIX})
dd4hep_set_compiler_flags()

file(GLOB sources CONFIGURE_DEPENDS src/*.cpp)

dd4hep_add_plugin(${a_lib_name}
  SOURCES ${sources}
  USES ROOT::Core ROOT::Gdml
  )
target_link_libraries(${a_lib_name}
  PUBLIC DD4hep::DDCore DD4hep::DDRec fmt::fmt
  )

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/FieldMapB.cpp Outdated
Comment thread src/FieldMapB.cpp
Comment thread src/FieldMapB.cpp
Comment thread src/FieldMapB.cpp Outdated
@wdconinc
Copy link
Copy Markdown
Contributor Author

wdconinc commented Apr 23, 2026

The **** in the capybara comparison appear to be caused by the different rounding errors throughout. A direct comparison is here (for SiBarrelHits, in npsim-gun with electrons, e.g. https://github.com/eic/epic/actions/runs/24809319094/job/72610712305?pr=934#step:8:557, reflowed for clarity per eic/epic-capybara#34):

SiBarrelHits.cellID p = 0.134
[[89753421954969915, 144679662776263228], [...], ..., [158559519591375163, ...]]
[[89753421954969915, 144679662776263228], ..., [213691313252491579, ...]]
SiBarrelHits.eDep p = 0.828
[[9.58e-05, 9.72e-06], [1.62e-05, ...], ..., [], [9.43e-06, 1.01e-05, 1.08e-08]]
[[9.58e-05, 9.72e-06], [7.03e-06], ..., [1.61e-05, ...], [1.11e-05, 2.14e-05]]
SiBarrelHits.momentum.x p = 0.041
[[12.8, 12.8], [2, 2, 1.92], [], [...], ..., [], [], [9.92, 9.93, -0.000318]]
[[12.8, 12.8], [0.366], [-5.02, ..., 8.08e-05], ..., [3.61, 3.61], [7.61, 7.66]]
SiBarrelHits.momentum.y p = 0.319
[[13.5, 13.4], [-15.3, -15.3, -15.3], [], ..., [], [0.732, 0.653, -0.000114]]
[[13.5, 13.4], [-1.03], [17.6, ..., -0.00038], ..., [0.207, 0.127], [6.8, 6.74]]
SiBarrelHits.momentum.z p = 0.009
[[-7.2, -7.2], [12.4, 12.4, 12.4], [], ..., [], [], [-6.66, -6.66, -6.92e-05]]
[[-7.2, -7.2], [-0.36], [-5.03, ...], ..., [...], [-1.58, -1.58], [-9.2, -9.2]]
SiBarrelHits.pathLength p = 0.282
[[0.0429, 0.0429], [0.0514, ..., 0.0512], ..., [], [0.0482, 0.0481, 0.000263]]
[[0.0429, 0.0429], [0.0422], [...], ..., [0.0438, 0.0437], [0.0539, 0.0539]]
SiBarrelHits.position.x p = 0.023
[[180, 291], [35.2, 36, 54.8], [], [202, ...], ..., [], [], [269, 423, -376]]
[[180, 291], [181], [-73.4, -117, ..., -116, -115], ..., [269, 423], [200, 311]]
SiBarrelHits.position.y p = 0.382
[[192, 308], [-261, -267, -414], [], [...], ..., [], [], [], [21.7, 32.4, -183]]
[[192, 308], [-384], [253, 408, ..., 408, 409], ..., [20.6, 27.8], [181, 280]]
SiBarrelHits.position.z p = 0.117
[[-102, -164], [211, 216, 335], [], [...], ..., [], [], [], [-180, -284, -177]]
[[-102, -164], [-140], [-72.6, ..., -128], ..., [-118, -186], [-243, -377]]
SiBarrelHits.quality p = 1.000
[[0, 0], [0, 0, 0], [], [0, 0], ..., [], [], [], [0, 0, 1073741824]]
[[0, 0], [0], [0, 0, ..., 1073741824, 1073741824], ..., [...], [0, 0], [0, 0]]
SiBarrelHits.time p = 0.000
[[0.941, 1.52], [1.13, 1.15, 1.79], [], [...], ..., [], [], [1.08, 1.7, 9.03]]
[[0.941, 1.52], [1.49], [0.912, ..., 1.64], ..., [0.982, 1.55], [1.21, 1.88]]

For the first event we find the exact same values, so there is no major field shift that leads to different hits. It is only after this first event that we start to see divergence, indicative of small differences leading to divergence.

@wdconinc
Copy link
Copy Markdown
Contributor Author

wdconinc commented Apr 28, 2026

Hmm... https://eicweb.phy.anl.gov/EIC/benchmarks/detector_benchmarks/-/jobs/7695818#L1894

-------- EEEE ------- G4Exception-START -------- EEEE -------
*** G4Exception : GeomField0003
      issued by : G4RKIntegrationDriver::ComputeNewStepSize
Error is negative!
*** Fatal Exception *** core dump ***
G4Track (0x55c78d659bc0) - track ID = 1, parent ID = 0
 Particle type : e- - creator process : not available
 Kinetic energy : 2.73654 MeV - Momentum direction : (0.300279,-0.921401,-0.246684)
 Step length : 18.5933 cm  - total energy deposit : 0 eV 
 Pre-step point : (48.7466,437.917,-57055.1) - Physical volume : world_volume_1 (Air)
 - defined by : eBrem - step status : 4
 Post-step point : (48.7466,437.917,-57055.1) - Physical volume : world_volume_1 (Air)
 - defined by : eIoni - step status : 7
 *** Note: Step information might not be properly updated.
-------- EEEE -------- G4Exception-END --------- EEEE -------

Also https://eicweb.phy.anl.gov/EIC/benchmarks/detector_benchmarks/-/jobs/7695821#L1400.

The covfie linear interpolation stencil always accesses both floor(index)
and floor(index)+1 in each dimension. When a physical coordinate maps to
the last valid array index (sizes[i]-1), the stencil requests index
sizes[i] which is one past the end of the allocation. In release builds
the strided/morton backends have no bounds checking (the assert is inside
#ifndef NDEBUG), so this silently reads garbage or NaN.

A NaN field value propagates through the Runge-Kutta error estimator:
  if (errMaxNorm > 1) ...   // NaN > 1  -> false
  if (errMaxNorm >= 0) ...  // NaN >= 0 -> false

Fix: insert covfie::backend::clamp between linear and the storage backend
(strided or morton) for all four field type aliases. The clamp transformer
auto-configures min=0 / max=sizes[i]-1 from the wrapped backend and
implements clamp-to-edge semantics, so queries at the exact boundary return
the boundary value rather than reading out of bounds.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@eic eic deleted a comment from github-actions Bot Apr 29, 2026
Add clamp<strided> layer to field map binary format so boundaries are
clamped during linear interpolation, preventing out-of-bounds reads that
caused NaN field values and the Geant4 GeomField0003 fatal exception.

The converter now uses affine<nearest_neighbour<clamp<strided>>> which
produces binary output matching the reader's affine<linear<clamp<strided>>>
type, since nearest_neighbour and linear are both transparent on disk.

Field map files regenerated and pushed to epic-data at 4d6becf.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions
Copy link
Copy Markdown
Contributor

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants