Skip to content

Commit 9e6edb5

Browse files
sawenzelclaude
andcommitted
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 <noreply@anthropic.com>
1 parent a9362c4 commit 9e6edb5

7 files changed

Lines changed: 91 additions & 34 deletions

File tree

‎Common/SimConfig/include/SimConfig/G4Params.h‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,10 @@ struct G4Params : public o2::conf::ConfigurableParamHelper<G4Params> {
6161
// which of the two VecGeom navigators
6262
EVecGeomNav vecgeomNavigator = EVecGeomNav::kRelocating;
6363
double vecgeomPushDepth = 1.e-9; // cm; how far past a face, measured across it, a boundary
64-
// point is pushed before it is located
65-
bool vecgeomLeanLocate = false; // kRelocating: locate a crossing 10 nm along the step, as VecGeom
66-
// relocates, instead of across the face with the flush-volume loop
64+
// point is pushed before it is located (previous locate only)
65+
bool vecgeomLeanLocate = true; // kRelocating: locate a crossing 10 nm along the step, as VecGeom
66+
// relocates; false is the previous locate across the face with the
67+
// flush-volume loop, kept as a fallback
6768
bool vecgeomZeroSafety = false; // answer zero to every safety query; conservative, but it
6869
// shortens steps and so changes the random history
6970
bool vecgeomFlattenAssemblies = true; // dissolve TGeo assemblies into their content when converting

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ class GeometryManager : public TObject
176176
/// assemblies into their content. \param booleanThreshold lets VecGeom turn Boolean solids with at
177177
/// least this many components into MultiUnions; 0 keeps them as converted.
178178
static void buildVecGeomGeometry(bool flattenAssemblies, int booleanThreshold = 0);
179+
/// BVH navigator and safety estimator (true) or the simple ones (false, as built) for volumes with one or two daughters.
180+
static void setVecGeomBvhForSmallMothers(bool bvh);
181+
/// Whether volumes with one or two daughters currently have the BVH pair.
182+
static bool vecGeomSmallMothersUseBvh();
179183
#else
180184
static constexpr bool isVecGeomAvailable() { return false; }
181185
#endif

‎Detectors/Base/src/GeometryManager.cxx‎

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,23 @@ bool usesBvhAcceleration(vecgeom::LogicalVolume const* vol)
737737
return vol->GetDaughtersp()->size() > 2;
738738
}
739739

740+
/// The current choice of setVecGeomBvhForSmallMothers.
741+
bool sSmallMothersUseBvh = false;
742+
743+
/// Navigator and safety estimator of a volume below the BVH threshold, always as a matched pair.
744+
void setSmallVolumeNavigation(vecgeom::LogicalVolume* vol, bool bvh)
745+
{
746+
#ifdef O2_VECGEOM_HAS_BVH_VNAVIGATOR
747+
vol->SetNavigator(bvh ? vecgeom::BVHNavigatorV<>::Instance() : vecgeom::NewSimpleNavigator<>::Instance());
748+
vol->SetSafetyEstimator(bvh ? vecgeom::BVHSafetyEstimator::Instance() : vecgeom::SimpleSafetyEstimator::Instance());
749+
#else
750+
if (bvh) {
751+
LOG(fatal) << "The BVH navigator for small volumes needs a VecGeom with BVHNavigatorV";
752+
}
753+
vol->SetNavigator(vecgeom::NewSimpleNavigator<>::Instance());
754+
#endif
755+
}
756+
740757
/// Converts the currently loaded TGeo geometry to VecGeom and sets up navigators, once per
741758
/// process, the first time the VecGeom backend is requested. Not part of loadGeometry(),
742759
/// which every job calls regardless of whether it ever uses the VecGeom backend.
@@ -783,11 +800,10 @@ void GeometryManager::buildVecGeomGeometry(bool flattenAssemblies, int booleanTh
783800
for (auto& lvol : vecgeom::GeoManager::Instance().GetLogicalVolumesMap()) {
784801
auto* vol = lvol.second;
785802
if (!usesBvhAcceleration(vol)) {
786-
vol->SetNavigator(vecgeom::NewSimpleNavigator<>::Instance());
803+
setSmallVolumeNavigation(vol, false);
787804
#ifdef O2_VECGEOM_HAS_BVH_VNAVIGATOR
788805
vol->SetLevelLocator(vol->ContainsAssembly() ? vecgeom::SimpleAssemblyLevelLocator::GetInstance()
789806
: vecgeom::SimpleLevelLocator::GetInstance());
790-
vol->SetSafetyEstimator(vecgeom::SimpleSafetyEstimator::Instance());
791807
#else
792808
vol->SetLevelLocator(vecgeom::SimpleLevelLocator::GetInstance());
793809
#endif
@@ -824,6 +840,21 @@ void GeometryManager::buildVecGeomGeometry(bool flattenAssemblies, int booleanTh
824840
});
825841
}
826842

843+
//_____________________________________________________________________________________
844+
void GeometryManager::setVecGeomBvhForSmallMothers(bool bvh)
845+
{
846+
sSmallMothersUseBvh = bvh;
847+
for (auto& lvol : vecgeom::GeoManager::Instance().GetLogicalVolumesMap()) {
848+
auto* vol = lvol.second;
849+
if (!usesBvhAcceleration(vol) && vol->GetDaughtersp()->size() > 0) {
850+
setSmallVolumeNavigation(vol, bvh);
851+
}
852+
}
853+
}
854+
855+
//_____________________________________________________________________________________
856+
bool GeometryManager::vecGeomSmallMothersUseBvh() { return sSmallMothersUseBvh; }
857+
827858
//_____________________________________________________________________________________
828859
o2::base::MatBudget GeometryManager::vecGeomMaterialBudget(float x0, float y0, float z0, float x1, float y1, float z1)
829860
{
@@ -904,6 +935,7 @@ o2::base::MatBudget GeometryManager::vecGeomMaterialBudget(float x0, float y0, f
904935
? static_cast<double>(vecgeom::BVHNavigator::ComputeStepAndPropagatedState(currPoint, dirr, remainingDist, *currnavstate, *newnavstate))
905936
: static_cast<double>(lvol->GetNavigator()->ComputeStepAndPropagatedState(currPoint, dirr, remainingDist, *currnavstate, *newnavstate));
906937
#else
938+
// After setVecGeomBvhForSmallMothers(true) small mothers step with BVHNavigatorV here too: same answer.
907939
const double step = static_cast<double>(lvol->GetNavigator()->ComputeStepAndPropagatedState(currPoint, dirr, remainingDist, *currnavstate, *newnavstate));
908940
#endif
909941
if (step < 2.E-10) {

‎Detectors/gconfig/src/VecGeomG4Navigator.cxx‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
// granted to it by virtue of its status as an Intergovernmental Organization
1010
// or submit itself to any jurisdiction.
1111

12+
// before the VecGeom headers, which define a VECGEOM macro
13+
#include "DetectorsBase/GeometryManager.h"
14+
1215
#include "VecGeomG4Navigator.h"
1316

1417
#include "VecGeomParallelWorld.h"
@@ -19,7 +22,6 @@
1922

2023
#include <VecGeom/base/Transformation3D.h>
2124
#include <VecGeom/management/GeoManager.h>
22-
#include <VecGeom/navigation/BVHNavigatorV.h>
2325
#include <VecGeom/navigation/GlobalLocator.h>
2426
#include <VecGeom/navigation/VNavigator.h>
2527
#include <VecGeom/navigation/VSafetyEstimator.h>
@@ -124,10 +126,8 @@ G4double VecGeomG4Navigator::ComputeStep(const G4ThreeVector& globalPoint, const
124126
if (top == nullptr) { // the track is outside the world
125127
return kInfinity;
126128
}
127-
auto const* lvol = top->GetLogicalVolume();
128-
// The lean locate relies on the BVH navigator refusing a daughter the ray leaves, also for small mothers.
129-
auto const* navigator = mLeanLocate && lvol->GetDaughtersp()->size() > 0 ? vecgeom::BVHNavigatorV<>::Instance()
130-
: lvol->GetNavigator();
129+
// With the lean locate, the geometry setup gives every mother with daughters the BVH navigator.
130+
auto const* navigator = top->GetLogicalVolume()->GetNavigator();
131131

132132
// On the point a boundary locate left the track on, the safety is zero; a point seen before
133133
// reuses its safety. Otherwise the navigator computes it with the step.
@@ -384,6 +384,9 @@ G4VPhysicalVolume* VecGeomG4Navigator::LocateGlobalPointAndSetup(const G4ThreeVe
384384

385385
if (!relativeSearch) {
386386
resetZeroSteps(); // a new track: nothing of the previous one applies
387+
if (mLeanLocate && !o2::base::GeometryManager::vecGeomSmallMothersUseBvh()) {
388+
LOG(fatal) << "The lean locate needs GeometryManager::setVecGeomBvhForSmallMothers(true)";
389+
}
387390
locateFromWorld(p);
388391
} else if (topOf(mCurState) == nullptr) {
389392
locateFromWorld(p);

‎Detectors/gconfig/src/VecGeomG4Navigator.h‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@ class VecGeomParallelWorld;
2727
/// - ComputeStep leaves the current volume alone. It records whether the step ends on a boundary
2828
/// and, for a daughter hit, the state that enters it.
2929
/// - The locate on a boundary relocates: into the recorded daughter, or out of the current volume.
30-
/// The point is first pushed across the face by a small depth, and afterwards leaves every volume
31-
/// it is flush with and heading out of. As in Geant4, the volume left is blocked only in the first
32-
/// ComputeStep after the exit, and only while the direction points away from it.
33-
/// - With the lean locate (G4.vecgeomLeanLocate) the point is pushed 10 nm along the step instead,
34-
/// which takes it past every coincident face, so no flush-volume loop is needed. Every mother with
35-
/// daughters is then stepped with the BVH navigator, whose daughter query refuses at zero distance a
36-
/// daughter the ray is leaving; that is the block of the volume just left, without a normal.
30+
/// - With the lean locate (G4.vecgeomLeanLocate, the default) the point is pushed 10 nm along the step,
31+
/// which takes it past every coincident face. The geometry setup then steps every mother with daughters
32+
/// with the BVH navigator (GeometryManager::setVecGeomBvhForSmallMothers), whose daughter query refuses
33+
/// at zero distance a daughter the ray is leaving; that is the block of the volume just left.
34+
/// - With the previous locate, kept as a fallback, the point is pushed across the face by a small depth
35+
/// and afterwards leaves every volume it is flush with and heading out of. As in Geant4, the volume
36+
/// left is blocked only in the first ComputeStep after the exit, and only while the direction points
37+
/// away from it.
3738
/// - Safety is zero only at the boundary point itself. A bounded query is answered from the last safety
3839
/// computed in the same volume when that covers the bound.
3940
///
@@ -46,11 +47,13 @@ class VecGeomG4Navigator : public VecGeomG4NavigatorBase
4647
/// it is located. \param zeroSafety answers zero to every safety query.
4748
/// \param parallelWorld the prioritised placements, or null if there are none.
4849
/// \param leanLocate locates a crossing as VecGeom relocates: 10 nm along the step, no face normal, no
49-
/// flush-volume loop; the volume left is refused by the BVH navigator's rule for a daughter the ray leaves.
50+
/// flush-volume loop; it needs GeometryManager::setVecGeomBvhForSmallMothers(true).
5051
VecGeomG4Navigator(VecGeomG4Map const& map, double pushDepth, bool zeroSafety,
51-
VecGeomParallelWorld const* parallelWorld = nullptr, bool leanLocate = false);
52+
VecGeomParallelWorld const* parallelWorld, bool leanLocate);
5253
~VecGeomG4Navigator() override;
5354

55+
bool leanLocate() const { return mLeanLocate; }
56+
5457
G4double ComputeStep(const G4ThreeVector& globalPoint, const G4ThreeVector& direction,
5558
const G4double proposedStepLength, G4double& newSafety) override;
5659

‎Detectors/gconfig/src/VecGeomNavigation.cxx‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ void installVecGeomNavigator()
8585
o2::base::GeometryManager::buildVecGeomGeometry(g4Params.vecgeomFlattenAssemblies, g4Params.vecgeomBooleanThreshold);
8686
timer.Stop();
8787
LOG(info) << "VecGeom geometry built in " << timer.RealTime() << " s";
88+
// The lean locate needs every mother with daughters stepped with the BVH navigator; kPropagated keeps the build's.
89+
const bool leanLocate = g4Params.vecgeomLeanLocate && g4Params.vecgeomNavigator == o2::conf::EVecGeomNav::kRelocating;
90+
o2::base::GeometryManager::setVecGeomBvhForSmallMothers(leanLocate);
8891

8992
timer.Start();
9093
// Owned here for the lifetime of the process; the navigator keeps a reference to it.
@@ -123,11 +126,9 @@ void installVecGeomNavigator()
123126
LOG(info) << "VecGeom navigation: propagating navigator";
124127
navigator = new VecGeomG4PropagatingNavigator(map, g4Params.vecgeomZeroSafety);
125128
} else {
126-
if (g4Params.vecgeomLeanLocate) {
127-
LOG(info) << "VecGeom relocating navigator with the lean locate";
128-
}
129+
LOG(info) << "VecGeom relocating navigator with the " << (leanLocate ? "lean" : "previous") << " locate";
129130
navigator = new VecGeomG4Navigator(map, g4Params.vecgeomPushDepth, g4Params.vecgeomZeroSafety, parallelWorldPtr,
130-
g4Params.vecgeomLeanLocate);
131+
leanLocate);
131132
}
132133
navigator->SetWorldVolume(detConstruction->GetTopPV());
133134

‎Detectors/gconfig/test/testVecGeomG4Navigator.cxx‎

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ struct Fixture {
9393
BOOST_REQUIRE(world != nullptr);
9494
o2::base::GeometryManager::buildVecGeomGeometry(true);
9595
map.build(*construction, true);
96-
navigator = std::make_unique<VecGeomG4Navigator>(map, 1.e-9, false);
96+
navigator = std::make_unique<VecGeomG4Navigator>(map, 1.e-9, false, nullptr, false);
9797
navigator->SetWorldVolume(world);
9898
lean = std::make_unique<VecGeomG4Navigator>(map, 1.e-9, false, nullptr, true);
9999
lean->SetWorldVolume(world);
@@ -108,10 +108,23 @@ Fixture& fixture()
108108
return f;
109109
}
110110

111-
/// Both locates: the default one and the lean one.
111+
/// Both locates: the previous one and the lean one.
112112
std::vector<std::pair<std::string, VecGeomG4Navigator*>> navigators()
113113
{
114-
return {{"default locate", fixture().navigator.get()}, {"lean locate", fixture().lean.get()}};
114+
return {{"previous locate", fixture().navigator.get()}, {"lean locate", fixture().lean.get()}};
115+
}
116+
117+
/// The navigator, with the small mothers set up for its locate as installVecGeomNavigator does.
118+
VecGeomG4Navigator& withSetup(VecGeomG4Navigator& nav)
119+
{
120+
o2::base::GeometryManager::setVecGeomBvhForSmallMothers(nav.leanLocate());
121+
return nav;
122+
}
123+
124+
VecGeomG4PropagatingNavigator& withSetup(VecGeomG4PropagatingNavigator& nav)
125+
{
126+
o2::base::GeometryManager::setVecGeomBvhForSmallMothers(false);
127+
return nav;
115128
}
116129

117130
/// The path of the navigator's touchable, in TGeo's notation.
@@ -269,7 +282,7 @@ double chordsUntilPush(G4Navigator& nav, const G4ThreeVector& p, int& zeroSteps,
269282
BOOST_AUTO_TEST_CASE(rays_visit_the_volumes_tgeo_visits)
270283
{
271284
for (auto& [name, navp] : navigators()) {
272-
auto& nav = *navp;
285+
auto& nav = withSetup(*navp);
273286
BOOST_TEST_CONTEXT(name)
274287
{
275288
std::mt19937_64 gen(1);
@@ -321,7 +334,7 @@ BOOST_AUTO_TEST_CASE(rays_visit_the_volumes_tgeo_visits)
321334
BOOST_AUTO_TEST_CASE(a_turn_at_the_face_does_not_change_the_volume_entered)
322335
{
323336
for (auto& [name, navp] : navigators()) {
324-
auto& nav = *navp;
337+
auto& nav = withSetup(*navp);
325338
BOOST_TEST_CONTEXT(name)
326339
{
327340
std::mt19937_64 gen(2);
@@ -359,7 +372,7 @@ BOOST_AUTO_TEST_CASE(a_turn_at_the_face_does_not_change_the_volume_entered)
359372
BOOST_AUTO_TEST_CASE(a_track_tangent_to_a_field_boundary_is_freed_along_its_momentum)
360373
{
361374
for (auto& [name, navp] : navigators()) {
362-
auto& nav = *navp;
375+
auto& nav = withSetup(*navp);
363376
BOOST_TEST_CONTEXT(name)
364377
{
365378
// 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
413426
BOOST_AUTO_TEST_CASE(a_push_restarts_the_run_of_zero_steps_only_in_another_volume_it_reached)
414427
{
415428
for (auto& [name, navp] : navigators()) {
416-
auto& nav = *navp;
429+
auto& nav = withSetup(*navp);
417430
BOOST_TEST_CONTEXT(name)
418431
{
419432
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
452465
BOOST_AUTO_TEST_CASE(a_track_pushed_from_place_to_place_is_aborted_after_its_fourth_run)
453466
{
454467
for (auto& [name, navp] : navigators()) {
455-
auto& nav = *navp;
468+
auto& nav = withSetup(*navp);
456469
BOOST_TEST_CONTEXT(name)
457470
{
458471
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
485498

486499
BOOST_AUTO_TEST_CASE(the_propagating_navigator_has_a_zero_safety_only_on_the_face_it_crossed)
487500
{
488-
auto& nav = *fixture().propagating;
501+
auto& nav = withSetup(*fixture().propagating);
489502
const G4ThreeVector down(0., 0., -1.);
490503
const G4ThreeVector face = enterLead(nav);
491504
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
497510

498511
BOOST_AUTO_TEST_CASE(a_new_track_after_a_crossing_is_not_on_a_boundary)
499512
{
500-
auto& nav = *fixture().propagating;
513+
auto& nav = withSetup(*fixture().propagating);
501514
enterLead(nav);
502515
const G4ThreeVector up(0., 0., 1.);
503516
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)
508521

509522
BOOST_AUTO_TEST_CASE(a_resumed_track_after_a_crossing_is_not_on_a_boundary)
510523
{
511-
auto& nav = *fixture().propagating;
524+
auto& nav = withSetup(*fixture().propagating);
512525
const G4ThreeVector up(0., 0., 1.);
513526
nav.LocateGlobalPointAndSetup(inPaper, &up, false, false);
514527
std::unique_ptr<G4TouchableHistory> touchable(nav.CreateTouchableHistory());

0 commit comments

Comments
 (0)