Skip to content

Commit 09cd96e

Browse files
sawenzelclaude
andcommitted
Set up the VecGeom BVH navigator and safety estimators for every volume
This lets GeometryManager set up VecGeom for Geant4 navigation as well as for the material budget. - buildVecGeomGeometry() converts once and takes the assembly flattening as a parameter; the material budget keeps flattening. - With a VecGeom that has BVHNavigatorV, volumes with more than two daughters get the BVH navigator and level locator, and every volume gets an explicit safety estimator. - Without it the VecGeom v2 setup is unchanged. https://gitlab.cern.ch/VecGeom/VecGeom/-/merge_requests/1547 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent fced507 commit 09cd96e

2 files changed

Lines changed: 39 additions & 6 deletions

File tree

‎Detectors/Base/include/DetectorsBase/GeometryManager.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,12 @@ class GeometryManager : public TObject
135135
/// Mean material budget between two points, using the VecGeom backend. On first call,
136136
/// lazily converts the currently loaded TGeo geometry to VecGeom (once per process).
137137
static o2::base::MatBudget vecGeomMaterialBudget(float x0, float y0, float z0, float x1, float y1, float z1);
138+
/// Converts the currently loaded TGeo geometry to VecGeom and assigns a navigator, a level
139+
/// locator and a safety estimator to every logical volume. Does the work once per process; later
140+
/// calls, whatever they ask for, return the geometry already built, so a caller that needs a
141+
/// particular assembly treatment must come first. \param flattenAssemblies dissolves TGeo
142+
/// assemblies into their content.
143+
static void buildVecGeomGeometry(bool flattenAssemblies);
138144
#else
139145
static constexpr bool isVecGeomAvailable() { return false; }
140146
#endif

‎Detectors/Base/src/GeometryManager.cxx‎

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@
4949
#include <VecGeom/navigation/VNavigator.h>
5050
#include <VecGeom/volumes/LogicalVolume.h>
5151
#include <mutex>
52+
// The BVH navigator of the VNavigator family, which Geant4 navigation needs on every volume.
53+
#if __has_include(<VecGeom/navigation/BVHNavigatorV.h>)
54+
#define O2_VECGEOM_HAS_BVH_VNAVIGATOR
55+
#include <VecGeom/navigation/BVHNavigatorV.h>
56+
#include <VecGeom/navigation/BVHLevelLocator.h>
57+
#include <VecGeom/navigation/BVHSafetyEstimator.h>
58+
#include <VecGeom/navigation/SimpleSafetyEstimator.h>
59+
#endif
5260
#endif
5361

5462
using namespace o2::detectors;
@@ -574,15 +582,22 @@ bool usesBvhAcceleration(vecgeom::LogicalVolume const* vol)
574582
/// process, the first time the VecGeom backend is requested. Not part of loadGeometry(),
575583
/// which every job calls regardless of whether it ever uses the VecGeom backend.
576584
void ensureVecGeomWorldBuilt()
585+
{
586+
GeometryManager::buildVecGeomGeometry(true);
587+
}
588+
} // namespace
589+
590+
void GeometryManager::buildVecGeomGeometry(bool flattenAssemblies)
577591
{
578592
static std::once_flag onceFlag;
579-
std::call_once(onceFlag, []() {
593+
std::call_once(onceFlag, [flattenAssemblies]() {
580594
if (!gGeoManager) {
581595
LOG(fatal) << "Cannot build VecGeom geometry: no TGeo geometry loaded (call GeometryManager::loadGeometry() first)";
582596
}
583597
// Translate geometry and material pointers, then build acceleration structures.
584598
tgeo2vecgeom::RootGeoManager::Instance().SetMaterialConversionHook([](TGeoMaterial const* m) { return (void*)m; });
585-
tgeo2vecgeom::RootGeoManager::Instance().SetFlattenAssemblies(true);
599+
LOG(info) << "VecGeom conversion: flattenAssemblies=" << flattenAssemblies;
600+
tgeo2vecgeom::RootGeoManager::Instance().SetFlattenAssemblies(flattenAssemblies);
586601
tgeo2vecgeom::RootGeoManager::Instance().LoadRootGeometry();
587602

588603
// Acceleration structures must be built before the navigators/locators reference them.
@@ -593,15 +608,28 @@ void ensureVecGeomWorldBuilt()
593608
// Builds a BVH per logical volume.
594609
vecgeom::BVHManager::Init();
595610

596-
// For each logical volume, set both a navigator (used for ComputeStep) and a matched
597-
// level locator (used for point relocation after a boundary crossing via GlobalLocator).
611+
// For each logical volume, set a navigator (used for ComputeStep), a matched level locator
612+
// (used for point relocation after a boundary crossing via GlobalLocator) and, where the
613+
// VNavigator family is complete, the safety estimator LogicalVolume::GetSafetyEstimator()
614+
// hands out, which is separate from the one a navigator uses internally.
598615
for (auto& lvol : vecgeom::GeoManager::Instance().GetLogicalVolumesMap()) {
599616
auto* vol = lvol.second;
600617
if (!usesBvhAcceleration(vol)) {
601618
vol->SetNavigator(vecgeom::NewSimpleNavigator<>::Instance());
619+
#ifdef O2_VECGEOM_HAS_BVH_VNAVIGATOR
620+
vol->SetLevelLocator(vol->ContainsAssembly() ? vecgeom::SimpleAssemblyLevelLocator::GetInstance()
621+
: vecgeom::SimpleLevelLocator::GetInstance());
622+
vol->SetSafetyEstimator(vecgeom::SimpleSafetyEstimator::Instance());
623+
#else
602624
vol->SetLevelLocator(vecgeom::SimpleLevelLocator::GetInstance());
625+
#endif
603626
} else {
604-
#if VECGEOM_VERSION >= 0x020000
627+
#if defined(O2_VECGEOM_HAS_BVH_VNAVIGATOR)
628+
vol->SetNavigator(vecgeom::BVHNavigatorV<>::Instance());
629+
vol->SetLevelLocator(vol->ContainsAssembly() ? vecgeom::BVHAssemblyAwareLevelLocator::GetInstance()
630+
: vecgeom::BVHLevelLocator::GetInstance());
631+
vol->SetSafetyEstimator(vecgeom::BVHSafetyEstimator::Instance());
632+
#elif VECGEOM_VERSION >= 0x020000
605633
// VecGeom 2 turned BVHNavigator into a plain class with static entry points instead of a
606634
// VNavigator singleton, so there is nothing to attach: vecGeomMaterialBudget() calls it
607635
// directly.
@@ -627,7 +655,6 @@ void ensureVecGeomWorldBuilt()
627655
}
628656
});
629657
}
630-
} // namespace
631658

632659
//_____________________________________________________________________________________
633660
o2::base::MatBudget GeometryManager::vecGeomMaterialBudget(float x0, float y0, float z0, float x1, float y1, float z1)

0 commit comments

Comments
 (0)