diff --git a/Detectors/Base/include/DetectorsBase/MTWorkerHooks.h b/Detectors/Base/include/DetectorsBase/MTWorkerHooks.h index 36bf1975640e0..2198211fb7e65 100644 --- a/Detectors/Base/include/DetectorsBase/MTWorkerHooks.h +++ b/Detectors/Base/include/DetectorsBase/MTWorkerHooks.h @@ -35,6 +35,8 @@ struct MTWorkerHooks { std::function dataChannel; /// Creates the external decayer of the calling thread (empty if there is none) std::function makeDecayer; + /// Called once on a worker thread after its MC exists and before Geant4 builds the physics of this thread + std::function initOnWorker; /// Ends the event loop of the calling thread after the current event std::function stopWorker; /// Called on a worker thread at the start of each run, after its user actions exist diff --git a/Detectors/gconfig/g4Config.C b/Detectors/gconfig/g4Config.C index bbbc2539bfad3..2dc6af4947c13 100644 --- a/Detectors/gconfig/g4Config.C +++ b/Detectors/gconfig/g4Config.C @@ -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"); diff --git a/Detectors/gconfig/include/SimSetup/VecGeomNavigation.h b/Detectors/gconfig/include/SimSetup/VecGeomNavigation.h index a26a2700f8cb9..31de2aba843bf 100644 --- a/Detectors/gconfig/include/SimSetup/VecGeomNavigation.h +++ b/Detectors/gconfig/include/SimSetup/VecGeomNavigation.h @@ -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 diff --git a/Detectors/gconfig/src/VecGeomNavigation.cxx b/Detectors/gconfig/src/VecGeomNavigation.cxx index 3072e7e186fd5..1c8e946224e79 100644 --- a/Detectors/gconfig/src/VecGeomNavigation.cxx +++ b/Detectors/gconfig/src/VecGeomNavigation.cxx @@ -38,6 +38,7 @@ #include "G4SteppingManager.hh" #include "G4TrackingManager.hh" #include "G4TransportationManager.hh" +#include "G4Threading.hh" #include "TMCManager.h" #include "TStopwatch.h" @@ -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(); @@ -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)) { @@ -121,30 +164,25 @@ void installVecGeomNavigator() checkVecGeomLocation(static_cast(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 @@ -157,6 +195,8 @@ void installVecGeomNavigator() "the VNavigator family (BVHNavigatorV), which were not found at configure time"; } +void installVecGeomNavigatorOnWorker() { installVecGeomNavigator(); } + #endif } // namespace o2::simsetup diff --git a/Steer/src/O2MCApplication.cxx b/Steer/src/O2MCApplication.cxx index e79264744d3ac..8db7dd3c54b12 100644 --- a/Steer/src/O2MCApplication.cxx +++ b/Steer/src/O2MCApplication.cxx @@ -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()); }