Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Common/SimConfig/include/SimConfig/G4Params.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ struct G4Params : public o2::conf::ConfigurableParamHelper<G4Params> {
// 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
Expand Down
4 changes: 4 additions & 0 deletions Detectors/Base/include/DetectorsBase/GeometryManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 34 additions & 2 deletions Detectors/Base/src/GeometryManager.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -904,6 +935,7 @@ o2::base::MatBudget GeometryManager::vecGeomMaterialBudget(float x0, float y0, f
? static_cast<double>(vecgeom::BVHNavigator::ComputeStepAndPropagatedState(currPoint, dirr, remainingDist, *currnavstate, *newnavstate))
: static_cast<double>(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<double>(lvol->GetNavigator()->ComputeStepAndPropagatedState(currPoint, dirr, remainingDist, *currnavstate, *newnavstate));
#endif
if (step < 2.E-10) {
Expand Down
13 changes: 8 additions & 5 deletions Detectors/gconfig/src/VecGeomG4Navigator.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -19,7 +22,6 @@

#include <VecGeom/base/Transformation3D.h>
#include <VecGeom/management/GeoManager.h>
#include <VecGeom/navigation/BVHNavigatorV.h>
#include <VecGeom/navigation/GlobalLocator.h>
#include <VecGeom/navigation/VNavigator.h>
#include <VecGeom/navigation/VSafetyEstimator.h>
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand Down
21 changes: 12 additions & 9 deletions Detectors/gconfig/src/VecGeomG4Navigator.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand All @@ -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;

Expand Down
9 changes: 5 additions & 4 deletions Detectors/gconfig/src/VecGeomNavigation.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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());

Expand Down
35 changes: 24 additions & 11 deletions Detectors/gconfig/test/testVecGeomG4Navigator.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ struct Fixture {
BOOST_REQUIRE(world != nullptr);
o2::base::GeometryManager::buildVecGeomGeometry(true);
map.build(*construction, true);
navigator = std::make_unique<VecGeomG4Navigator>(map, 1.e-9, false);
navigator = std::make_unique<VecGeomG4Navigator>(map, 1.e-9, false, nullptr, false);
navigator->SetWorldVolume(world);
lean = std::make_unique<VecGeomG4Navigator>(map, 1.e-9, false, nullptr, true);
lean->SetWorldVolume(world);
Expand All @@ -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<std::pair<std::string, VecGeomG4Navigator*>> 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.
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.);
Expand All @@ -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);
Expand All @@ -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<G4TouchableHistory> touchable(nav.CreateTouchableHistory());
Expand Down