From 2bdd8778e5061492b0362dab3e1c244badc0f756 Mon Sep 17 00:00:00 2001 From: Sandro Wenzel Date: Fri, 20 Nov 2026 14:16:31 +0100 Subject: [PATCH] Make the lean crossing locate the default This makes the lean crossing locate the default of the relocating VecGeom navigator and moves its navigator choice into the geometry setup. - G4.vecgeomLeanLocate now defaults to true; false keeps the previous locate as a fallback for one release. - GeometryManager::setVecGeomBvhForSmallMothers gives volumes with one or two daughters BVHNavigatorV, replacing the per-step choice in the navigator. - Those volumes also get BVHSafetyEstimator, the estimator BVHNavigatorV uses in the step, so ComputeSafety and the step agree. - Their level locators are unchanged; kPropagated and the previous locate keep the simple navigator and estimator. - The BVH navigator is installed under the same O2_VECGEOM_HAS_BVH_VNAVIGATOR guard as in buildVecGeomGeometry. - The lean navigator checks this setting at every new track, and every caller of its constructor states the locate. - The unit test sets the geometry up for each navigator as o2-sim does. Co-Authored-By: Claude Opus 5.5 --- Common/SimConfig/include/SimConfig/G4Params.h | 7 ++-- .../include/DetectorsBase/GeometryManager.h | 4 +++ Detectors/Base/src/GeometryManager.cxx | 36 +++++++++++++++++-- Detectors/gconfig/src/VecGeomG4Navigator.cxx | 13 ++++--- Detectors/gconfig/src/VecGeomG4Navigator.h | 21 ++++++----- Detectors/gconfig/src/VecGeomNavigation.cxx | 9 ++--- .../gconfig/test/testVecGeomG4Navigator.cxx | 35 ++++++++++++------ 7 files changed, 91 insertions(+), 34 deletions(-) diff --git a/Common/SimConfig/include/SimConfig/G4Params.h b/Common/SimConfig/include/SimConfig/G4Params.h index a2e82ab61845e..ad88ea44622b7 100644 --- a/Common/SimConfig/include/SimConfig/G4Params.h +++ b/Common/SimConfig/include/SimConfig/G4Params.h @@ -61,9 +61,10 @@ struct G4Params : public o2::conf::ConfigurableParamHelper { // which of the two VecGeom navigators EVecGeomNav vecgeomNavigator = EVecGeomNav::kRelocating; double vecgeomPushDepth = 1.e-9; // cm; how far past a face, measured across it, a boundary - // point is pushed before it is located - bool vecgeomLeanLocate = false; // kRelocating: locate a crossing 10 nm along the step, as VecGeom - // relocates, instead of across the face with the flush-volume loop + // point is pushed before it is located (previous locate only) + bool vecgeomLeanLocate = true; // kRelocating: locate a crossing 10 nm along the step, as VecGeom + // relocates; false is the previous locate across the face with the + // flush-volume loop, kept as a fallback bool vecgeomZeroSafety = false; // answer zero to every safety query; conservative, but it // shortens steps and so changes the random history bool vecgeomFlattenAssemblies = true; // dissolve TGeo assemblies into their content when converting diff --git a/Detectors/Base/include/DetectorsBase/GeometryManager.h b/Detectors/Base/include/DetectorsBase/GeometryManager.h index 70aa47c126ec0..ad8fc44b50e25 100644 --- a/Detectors/Base/include/DetectorsBase/GeometryManager.h +++ b/Detectors/Base/include/DetectorsBase/GeometryManager.h @@ -176,6 +176,10 @@ class GeometryManager : public TObject /// assemblies into their content. \param booleanThreshold lets VecGeom turn Boolean solids with at /// least this many components into MultiUnions; 0 keeps them as converted. static void buildVecGeomGeometry(bool flattenAssemblies, int booleanThreshold = 0); + /// BVH navigator and safety estimator (true) or the simple ones (false, as built) for volumes with one or two daughters. + static void setVecGeomBvhForSmallMothers(bool bvh); + /// Whether volumes with one or two daughters currently have the BVH pair. + static bool vecGeomSmallMothersUseBvh(); #else static constexpr bool isVecGeomAvailable() { return false; } #endif diff --git a/Detectors/Base/src/GeometryManager.cxx b/Detectors/Base/src/GeometryManager.cxx index 5b870d36e420a..ed067b16c8fbd 100644 --- a/Detectors/Base/src/GeometryManager.cxx +++ b/Detectors/Base/src/GeometryManager.cxx @@ -737,6 +737,23 @@ bool usesBvhAcceleration(vecgeom::LogicalVolume const* vol) return vol->GetDaughtersp()->size() > 2; } +/// The current choice of setVecGeomBvhForSmallMothers. +bool sSmallMothersUseBvh = false; + +/// Navigator and safety estimator of a volume below the BVH threshold, always as a matched pair. +void setSmallVolumeNavigation(vecgeom::LogicalVolume* vol, bool bvh) +{ +#ifdef O2_VECGEOM_HAS_BVH_VNAVIGATOR + vol->SetNavigator(bvh ? vecgeom::BVHNavigatorV<>::Instance() : vecgeom::NewSimpleNavigator<>::Instance()); + vol->SetSafetyEstimator(bvh ? vecgeom::BVHSafetyEstimator::Instance() : vecgeom::SimpleSafetyEstimator::Instance()); +#else + if (bvh) { + LOG(fatal) << "The BVH navigator for small volumes needs a VecGeom with BVHNavigatorV"; + } + vol->SetNavigator(vecgeom::NewSimpleNavigator<>::Instance()); +#endif +} + /// Converts the currently loaded TGeo geometry to VecGeom and sets up navigators, once per /// process, the first time the VecGeom backend is requested. Not part of loadGeometry(), /// which every job calls regardless of whether it ever uses the VecGeom backend. @@ -783,11 +800,10 @@ void GeometryManager::buildVecGeomGeometry(bool flattenAssemblies, int booleanTh for (auto& lvol : vecgeom::GeoManager::Instance().GetLogicalVolumesMap()) { auto* vol = lvol.second; if (!usesBvhAcceleration(vol)) { - vol->SetNavigator(vecgeom::NewSimpleNavigator<>::Instance()); + setSmallVolumeNavigation(vol, false); #ifdef O2_VECGEOM_HAS_BVH_VNAVIGATOR vol->SetLevelLocator(vol->ContainsAssembly() ? vecgeom::SimpleAssemblyLevelLocator::GetInstance() : vecgeom::SimpleLevelLocator::GetInstance()); - vol->SetSafetyEstimator(vecgeom::SimpleSafetyEstimator::Instance()); #else vol->SetLevelLocator(vecgeom::SimpleLevelLocator::GetInstance()); #endif @@ -824,6 +840,21 @@ void GeometryManager::buildVecGeomGeometry(bool flattenAssemblies, int booleanTh }); } +//_____________________________________________________________________________________ +void GeometryManager::setVecGeomBvhForSmallMothers(bool bvh) +{ + sSmallMothersUseBvh = bvh; + for (auto& lvol : vecgeom::GeoManager::Instance().GetLogicalVolumesMap()) { + auto* vol = lvol.second; + if (!usesBvhAcceleration(vol) && vol->GetDaughtersp()->size() > 0) { + setSmallVolumeNavigation(vol, bvh); + } + } +} + +//_____________________________________________________________________________________ +bool GeometryManager::vecGeomSmallMothersUseBvh() { return sSmallMothersUseBvh; } + //_____________________________________________________________________________________ o2::base::MatBudget GeometryManager::vecGeomMaterialBudget(float x0, float y0, float z0, float x1, float y1, float z1) { @@ -904,6 +935,7 @@ o2::base::MatBudget GeometryManager::vecGeomMaterialBudget(float x0, float y0, f ? static_cast(vecgeom::BVHNavigator::ComputeStepAndPropagatedState(currPoint, dirr, remainingDist, *currnavstate, *newnavstate)) : static_cast(lvol->GetNavigator()->ComputeStepAndPropagatedState(currPoint, dirr, remainingDist, *currnavstate, *newnavstate)); #else + // After setVecGeomBvhForSmallMothers(true) small mothers step with BVHNavigatorV here too: same answer. const double step = static_cast(lvol->GetNavigator()->ComputeStepAndPropagatedState(currPoint, dirr, remainingDist, *currnavstate, *newnavstate)); #endif if (step < 2.E-10) { diff --git a/Detectors/gconfig/src/VecGeomG4Navigator.cxx b/Detectors/gconfig/src/VecGeomG4Navigator.cxx index fa883a34718cd..50dac99347630 100644 --- a/Detectors/gconfig/src/VecGeomG4Navigator.cxx +++ b/Detectors/gconfig/src/VecGeomG4Navigator.cxx @@ -9,6 +9,9 @@ // granted to it by virtue of its status as an Intergovernmental Organization // or submit itself to any jurisdiction. +// before the VecGeom headers, which define a VECGEOM macro +#include "DetectorsBase/GeometryManager.h" + #include "VecGeomG4Navigator.h" #include "VecGeomParallelWorld.h" @@ -19,7 +22,6 @@ #include #include -#include #include #include #include @@ -124,10 +126,8 @@ G4double VecGeomG4Navigator::ComputeStep(const G4ThreeVector& globalPoint, const if (top == nullptr) { // the track is outside the world return kInfinity; } - auto const* lvol = top->GetLogicalVolume(); - // The lean locate relies on the BVH navigator refusing a daughter the ray leaves, also for small mothers. - auto const* navigator = mLeanLocate && lvol->GetDaughtersp()->size() > 0 ? vecgeom::BVHNavigatorV<>::Instance() - : lvol->GetNavigator(); + // With the lean locate, the geometry setup gives every mother with daughters the BVH navigator. + auto const* navigator = top->GetLogicalVolume()->GetNavigator(); // On the point a boundary locate left the track on, the safety is zero; a point seen before // reuses its safety. Otherwise the navigator computes it with the step. @@ -384,6 +384,9 @@ G4VPhysicalVolume* VecGeomG4Navigator::LocateGlobalPointAndSetup(const G4ThreeVe if (!relativeSearch) { resetZeroSteps(); // a new track: nothing of the previous one applies + if (mLeanLocate && !o2::base::GeometryManager::vecGeomSmallMothersUseBvh()) { + LOG(fatal) << "The lean locate needs GeometryManager::setVecGeomBvhForSmallMothers(true)"; + } locateFromWorld(p); } else if (topOf(mCurState) == nullptr) { locateFromWorld(p); diff --git a/Detectors/gconfig/src/VecGeomG4Navigator.h b/Detectors/gconfig/src/VecGeomG4Navigator.h index 8a1eeb6785c2c..5193db80b48f9 100644 --- a/Detectors/gconfig/src/VecGeomG4Navigator.h +++ b/Detectors/gconfig/src/VecGeomG4Navigator.h @@ -27,13 +27,14 @@ class VecGeomParallelWorld; /// - ComputeStep leaves the current volume alone. It records whether the step ends on a boundary /// and, for a daughter hit, the state that enters it. /// - The locate on a boundary relocates: into the recorded daughter, or out of the current volume. -/// The point is first pushed across the face by a small depth, and afterwards leaves every volume -/// it is flush with and heading out of. As in Geant4, the volume left is blocked only in the first -/// ComputeStep after the exit, and only while the direction points away from it. -/// - With the lean locate (G4.vecgeomLeanLocate) the point is pushed 10 nm along the step instead, -/// which takes it past every coincident face, so no flush-volume loop is needed. Every mother with -/// daughters is then stepped with the BVH navigator, whose daughter query refuses at zero distance a -/// daughter the ray is leaving; that is the block of the volume just left, without a normal. +/// - With the lean locate (G4.vecgeomLeanLocate, the default) the point is pushed 10 nm along the step, +/// which takes it past every coincident face. The geometry setup then steps every mother with daughters +/// with the BVH navigator (GeometryManager::setVecGeomBvhForSmallMothers), whose daughter query refuses +/// at zero distance a daughter the ray is leaving; that is the block of the volume just left. +/// - With the previous locate, kept as a fallback, the point is pushed across the face by a small depth +/// and afterwards leaves every volume it is flush with and heading out of. As in Geant4, the volume +/// left is blocked only in the first ComputeStep after the exit, and only while the direction points +/// away from it. /// - Safety is zero only at the boundary point itself. A bounded query is answered from the last safety /// computed in the same volume when that covers the bound. /// @@ -46,11 +47,13 @@ class VecGeomG4Navigator : public VecGeomG4NavigatorBase /// it is located. \param zeroSafety answers zero to every safety query. /// \param parallelWorld the prioritised placements, or null if there are none. /// \param leanLocate locates a crossing as VecGeom relocates: 10 nm along the step, no face normal, no - /// flush-volume loop; the volume left is refused by the BVH navigator's rule for a daughter the ray leaves. + /// flush-volume loop; it needs GeometryManager::setVecGeomBvhForSmallMothers(true). VecGeomG4Navigator(VecGeomG4Map const& map, double pushDepth, bool zeroSafety, - VecGeomParallelWorld const* parallelWorld = nullptr, bool leanLocate = false); + VecGeomParallelWorld const* parallelWorld, bool leanLocate); ~VecGeomG4Navigator() override; + bool leanLocate() const { return mLeanLocate; } + G4double ComputeStep(const G4ThreeVector& globalPoint, const G4ThreeVector& direction, const G4double proposedStepLength, G4double& newSafety) override; diff --git a/Detectors/gconfig/src/VecGeomNavigation.cxx b/Detectors/gconfig/src/VecGeomNavigation.cxx index 5210efaf650eb..3072e7e186fd5 100644 --- a/Detectors/gconfig/src/VecGeomNavigation.cxx +++ b/Detectors/gconfig/src/VecGeomNavigation.cxx @@ -85,6 +85,9 @@ void installVecGeomNavigator() o2::base::GeometryManager::buildVecGeomGeometry(g4Params.vecgeomFlattenAssemblies, g4Params.vecgeomBooleanThreshold); timer.Stop(); LOG(info) << "VecGeom geometry built in " << timer.RealTime() << " s"; + // The lean locate needs every mother with daughters stepped with the BVH navigator; kPropagated keeps the build's. + const bool leanLocate = g4Params.vecgeomLeanLocate && g4Params.vecgeomNavigator == o2::conf::EVecGeomNav::kRelocating; + o2::base::GeometryManager::setVecGeomBvhForSmallMothers(leanLocate); timer.Start(); // Owned here for the lifetime of the process; the navigator keeps a reference to it. @@ -123,11 +126,9 @@ void installVecGeomNavigator() LOG(info) << "VecGeom navigation: propagating navigator"; navigator = new VecGeomG4PropagatingNavigator(map, g4Params.vecgeomZeroSafety); } else { - if (g4Params.vecgeomLeanLocate) { - LOG(info) << "VecGeom relocating navigator with the lean locate"; - } + LOG(info) << "VecGeom relocating navigator with the " << (leanLocate ? "lean" : "previous") << " locate"; navigator = new VecGeomG4Navigator(map, g4Params.vecgeomPushDepth, g4Params.vecgeomZeroSafety, parallelWorldPtr, - g4Params.vecgeomLeanLocate); + leanLocate); } navigator->SetWorldVolume(detConstruction->GetTopPV()); diff --git a/Detectors/gconfig/test/testVecGeomG4Navigator.cxx b/Detectors/gconfig/test/testVecGeomG4Navigator.cxx index a6c41e7348027..6efbbf1d5963d 100644 --- a/Detectors/gconfig/test/testVecGeomG4Navigator.cxx +++ b/Detectors/gconfig/test/testVecGeomG4Navigator.cxx @@ -93,7 +93,7 @@ struct Fixture { BOOST_REQUIRE(world != nullptr); o2::base::GeometryManager::buildVecGeomGeometry(true); map.build(*construction, true); - navigator = std::make_unique(map, 1.e-9, false); + navigator = std::make_unique(map, 1.e-9, false, nullptr, false); navigator->SetWorldVolume(world); lean = std::make_unique(map, 1.e-9, false, nullptr, true); lean->SetWorldVolume(world); @@ -108,10 +108,23 @@ Fixture& fixture() return f; } -/// Both locates: the default one and the lean one. +/// Both locates: the previous one and the lean one. std::vector> navigators() { - return {{"default locate", fixture().navigator.get()}, {"lean locate", fixture().lean.get()}}; + return {{"previous locate", fixture().navigator.get()}, {"lean locate", fixture().lean.get()}}; +} + +/// The navigator, with the small mothers set up for its locate as installVecGeomNavigator does. +VecGeomG4Navigator& withSetup(VecGeomG4Navigator& nav) +{ + o2::base::GeometryManager::setVecGeomBvhForSmallMothers(nav.leanLocate()); + return nav; +} + +VecGeomG4PropagatingNavigator& withSetup(VecGeomG4PropagatingNavigator& nav) +{ + o2::base::GeometryManager::setVecGeomBvhForSmallMothers(false); + return nav; } /// The path of the navigator's touchable, in TGeo's notation. @@ -269,7 +282,7 @@ double chordsUntilPush(G4Navigator& nav, const G4ThreeVector& p, int& zeroSteps, BOOST_AUTO_TEST_CASE(rays_visit_the_volumes_tgeo_visits) { for (auto& [name, navp] : navigators()) { - auto& nav = *navp; + auto& nav = withSetup(*navp); BOOST_TEST_CONTEXT(name) { std::mt19937_64 gen(1); @@ -321,7 +334,7 @@ BOOST_AUTO_TEST_CASE(rays_visit_the_volumes_tgeo_visits) BOOST_AUTO_TEST_CASE(a_turn_at_the_face_does_not_change_the_volume_entered) { for (auto& [name, navp] : navigators()) { - auto& nav = *navp; + auto& nav = withSetup(*navp); BOOST_TEST_CONTEXT(name) { std::mt19937_64 gen(2); @@ -359,7 +372,7 @@ BOOST_AUTO_TEST_CASE(a_turn_at_the_face_does_not_change_the_volume_entered) BOOST_AUTO_TEST_CASE(a_track_tangent_to_a_field_boundary_is_freed_along_its_momentum) { for (auto& [name, navp] : navigators()) { - auto& nav = *navp; + auto& nav = withSetup(*navp); BOOST_TEST_CONTEXT(name) { // A track on the top face of a lead tile, as under a field: the chord points into the tile, the momentum out of it. @@ -413,7 +426,7 @@ BOOST_AUTO_TEST_CASE(a_track_tangent_to_a_field_boundary_is_freed_along_its_mome BOOST_AUTO_TEST_CASE(a_push_restarts_the_run_of_zero_steps_only_in_another_volume_it_reached) { for (auto& [name, navp] : navigators()) { - auto& nav = *navp; + auto& nav = withSetup(*navp); BOOST_TEST_CONTEXT(name) { const G4ThreeVector momentum(0., 1., 0.); @@ -452,7 +465,7 @@ BOOST_AUTO_TEST_CASE(a_push_restarts_the_run_of_zero_steps_only_in_another_volum BOOST_AUTO_TEST_CASE(a_track_pushed_from_place_to_place_is_aborted_after_its_fourth_run) { for (auto& [name, navp] : navigators()) { - auto& nav = *navp; + auto& nav = withSetup(*navp); BOOST_TEST_CONTEXT(name) { static AbortCounter handler; // registered with Geant4 for the rest of the process @@ -485,7 +498,7 @@ BOOST_AUTO_TEST_CASE(a_track_pushed_from_place_to_place_is_aborted_after_its_fou BOOST_AUTO_TEST_CASE(the_propagating_navigator_has_a_zero_safety_only_on_the_face_it_crossed) { - auto& nav = *fixture().propagating; + auto& nav = withSetup(*fixture().propagating); const G4ThreeVector down(0., 0., -1.); const G4ThreeVector face = enterLead(nav); BOOST_CHECK_EQUAL(nav.ComputeSafety(face), 0.); @@ -497,7 +510,7 @@ BOOST_AUTO_TEST_CASE(the_propagating_navigator_has_a_zero_safety_only_on_the_fac BOOST_AUTO_TEST_CASE(a_new_track_after_a_crossing_is_not_on_a_boundary) { - auto& nav = *fixture().propagating; + auto& nav = withSetup(*fixture().propagating); enterLead(nav); const G4ThreeVector up(0., 0., 1.); nav.LocateGlobalPointAndSetup(inPaper, &up, false, false); @@ -508,7 +521,7 @@ BOOST_AUTO_TEST_CASE(a_new_track_after_a_crossing_is_not_on_a_boundary) BOOST_AUTO_TEST_CASE(a_resumed_track_after_a_crossing_is_not_on_a_boundary) { - auto& nav = *fixture().propagating; + auto& nav = withSetup(*fixture().propagating); const G4ThreeVector up(0., 0., 1.); nav.LocateGlobalPointAndSetup(inPaper, &up, false, false); std::unique_ptr touchable(nav.CreateTouchableHistory());