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
2 changes: 2 additions & 0 deletions Detectors/Base/include/DetectorsBase/MTWorkerHooks.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ struct MTWorkerHooks {
std::function<fair::mq::Channel*()> dataChannel;
/// Creates the external decayer of the calling thread (empty if there is none)
std::function<TVirtualMCDecayer*()> makeDecayer;
/// Called once on a worker thread after its MC exists and before Geant4 builds the physics of this thread
std::function<void()> initOnWorker;
/// Ends the event loop of the calling thread after the current event
std::function<void()> stopWorker;
/// Called on a worker thread at the start of each run, after its user actions exist
Expand Down
3 changes: 2 additions & 1 deletion Detectors/gconfig/g4Config.C
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@ void Config()

if (mtMode) {
if (g4Params.navmode == o2::conf::EG4Nav::kVecGeom) {
LOG(fatal) << "G4.navmode=kVecGeom is not yet supported in multithreaded mode (--isMT)";
// each worker thread steps with its own navigator on the VecGeom geometry built below
o2::base::MTWorkerHooks::instance().initOnWorker = &o2::simsetup::installVecGeomNavigatorOnWorker;
}
// each thread takes one sub-event at a time
geant4->ProcessGeantCommand("/run/eventModulo 1");
Expand Down
5 changes: 5 additions & 0 deletions Detectors/gconfig/include/SimSetup/VecGeomNavigation.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ bool isVecGeomNavigationAvailable();
/// onto is built while TG4RunManager configures itself. Aborts if the backend is missing.
void installVecGeomNavigator();

/// Gives the calling Geant4 worker thread its own VecGeom navigator, on the geometry that
/// installVecGeomNavigator built on the master. Call on the worker after g4root has set up the
/// thread's navigation and before Geant4 builds the thread's physics, which keeps the navigator.
void installVecGeomNavigatorOnWorker();

} // namespace o2::simsetup

#endif
90 changes: 65 additions & 25 deletions Detectors/gconfig/src/VecGeomNavigation.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "G4SteppingManager.hh"
#include "G4TrackingManager.hh"
#include "G4TransportationManager.hh"
#include "G4Threading.hh"

#include "TMCManager.h"
#include "TStopwatch.h"
Expand All @@ -51,6 +52,49 @@ namespace o2::simsetup

bool isVecGeomNavigationAvailable() { return true; }

namespace
{
// The VecGeom side built once by installVecGeomNavigator; read-only afterwards and shared by the navigators of all threads.
struct SharedVecGeomSetup {
VecGeomG4Map map;
VecGeomParallelWorld parallelWorldStore;
VecGeomParallelWorld const* parallelWorld = nullptr;
bool leanLocate = false;
bool built = false;
};
SharedVecGeomSetup gShared;

// Creates a VecGeom navigator and makes it the tracking navigator of the calling thread.
void registerNavigator(TG4RootDetectorConstruction& detConstruction)
{
auto const& g4Params = o2::conf::G4Params::Instance();
G4Navigator* navigator = nullptr;
if (g4Params.vecgeomNavigator == o2::conf::EVecGeomNav::kPropagated) {
LOG(info) << "VecGeom navigation: propagating navigator";
navigator = new VecGeomG4PropagatingNavigator(gShared.map, g4Params.vecgeomZeroSafety);
} else {
LOG(info) << "VecGeom relocating navigator with the " << (gShared.leanLocate ? "lean" : "previous") << " locate";
navigator = new VecGeomG4Navigator(gShared.map, g4Params.vecgeomPushDepth, g4Params.vecgeomZeroSafety,
gShared.parallelWorld, gShared.leanLocate);
}
navigator->SetWorldVolume(detConstruction.GetTopPV());

// Same sequence TG4RootNavMgr::SetNavigator uses, run here because by the time the engine
// exists the navigator manager considers itself connected and refuses to swap.
auto* trMgr = G4TransportationManager::GetTransportationManager();
trMgr->SetNavigatorForTracking(navigator);
auto* fieldMgr = trMgr->GetPropagatorInField()->GetCurrentFieldManager();
delete trMgr->GetPropagatorInField();
trMgr->SetPropagatorInField(new G4PropagatorInField(navigator, fieldMgr));
trMgr->ActivateNavigator(navigator);
if (auto* evtMgr = G4EventManager::GetEventManager()) {
evtMgr->GetTrackingManager()->GetSteppingManager()->SetNavigator(navigator);
}
LOG(info) << "VecGeom navigator registered with the Geant4 transportation manager"
<< (G4Threading::IsWorkerThread() ? " of a worker thread" : "");
}
} // namespace

void installVecGeomNavigator()
{
auto const& g4Params = o2::conf::G4Params::Instance();
Expand Down Expand Up @@ -90,14 +134,13 @@ void installVecGeomNavigator()
o2::base::GeometryManager::setVecGeomBvhForSmallMothers(leanLocate);

timer.Start();
// Owned here for the lifetime of the process; the navigator keeps a reference to it.
static VecGeomG4Map map;
auto& map = gShared.map;
map.build(*detConstruction, g4Params.vecgeomFlattenAssemblies);
timer.Stop();
LOG(info) << "VecGeom to Geant4 map built in " << timer.RealTime() << " s";

// The prioritised placements of the TGeo parallel world, navigated with VecGeom.
static VecGeomParallelWorld parallelWorld;
auto& parallelWorld = gShared.parallelWorldStore;
VecGeomParallelWorld const* parallelWorldPtr = nullptr;
if (useParallelWorld) {
if (parallelWorld.build(*detConstruction, map)) {
Expand All @@ -121,30 +164,25 @@ void installVecGeomNavigator()
checkVecGeomLocation(static_cast<std::size_t>(g4Params.vecgeomCheckLocation));
}

G4Navigator* navigator = nullptr;
if (g4Params.vecgeomNavigator == o2::conf::EVecGeomNav::kPropagated) {
LOG(info) << "VecGeom navigation: propagating navigator";
navigator = new VecGeomG4PropagatingNavigator(map, g4Params.vecgeomZeroSafety);
} else {
LOG(info) << "VecGeom relocating navigator with the " << (leanLocate ? "lean" : "previous") << " locate";
navigator = new VecGeomG4Navigator(map, g4Params.vecgeomPushDepth, g4Params.vecgeomZeroSafety, parallelWorldPtr,
leanLocate);
}
navigator->SetWorldVolume(detConstruction->GetTopPV());
gShared.leanLocate = leanLocate;
gShared.parallelWorld = parallelWorldPtr;
gShared.built = true;
registerNavigator(*detConstruction);
}

// Same sequence TG4RootNavMgr::SetNavigator uses, run here because by the time the engine
// exists the navigator manager considers itself connected and refuses to swap.
auto* trMgr = G4TransportationManager::GetTransportationManager();
trMgr->SetNavigatorForTracking(navigator);
auto* fieldMgr = trMgr->GetPropagatorInField()->GetCurrentFieldManager();
delete trMgr->GetPropagatorInField();
trMgr->SetPropagatorInField(new G4PropagatorInField(navigator, fieldMgr));
trMgr->ActivateNavigator(navigator);
if (auto* evtMgr = G4EventManager::GetEventManager()) {
evtMgr->GetTrackingManager()->GetSteppingManager()->SetNavigator(navigator);
void installVecGeomNavigatorOnWorker()
{
if (!G4Threading::IsWorkerThread()) {
LOG(fatal) << "installVecGeomNavigatorOnWorker must be called on a Geant4 worker thread";
}

LOG(info) << "VecGeom navigator registered with the Geant4 transportation manager";
if (!gShared.built) {
LOG(fatal) << "The VecGeom geometry has not been built; installVecGeomNavigator must run on the master first";
}
auto* navMgr = TG4RootNavMgr::GetInstance(); // this thread's, cloned by geant4_vmc from the master's
if (navMgr == nullptr || navMgr->GetDetConstruction() == nullptr) {
LOG(fatal) << "G4.navmode=kVecGeom found no g4root navigation on this worker thread";
}
registerNavigator(*navMgr->GetDetConstruction());
}

#else
Expand All @@ -157,6 +195,8 @@ void installVecGeomNavigator()
"the VNavigator family (BVHNavigatorV), which were not found at configure time";
}

void installVecGeomNavigatorOnWorker() { installVecGeomNavigator(); }

#endif

} // namespace o2::simsetup
3 changes: 3 additions & 0 deletions Steer/src/O2MCApplication.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -1997,6 +1997,9 @@ void O2MCApplication::InitOnWorker()
o2::base::setSimRandom(mWorkerRandom.get());
// this thread's own external decayer; set here, before Geant4 builds the physics of this thread
auto& hooks = o2::base::MTWorkerHooks::instance();
if (hooks.initOnWorker) {
hooks.initOnWorker();
}
if (hooks.makeDecayer) {
fMC->SetExternalDecayer(hooks.makeDecayer());
}
Expand Down