From 01d9149ba0670fd2ef65e69595bbddc4bc37c02e Mon Sep 17 00:00:00 2001
From: John Chapman <jchapman@cern.ch>
Date: Fri, 13 Nov 2020 18:03:25 +0100
Subject: [PATCH] Adapt code to use G4AtlasActionInitialization

Here we adapt the various clients of the `UserActionSvc` to create
(in the case of `G4AtlasAlg`, `G4LegacyTransportTool` and
`G4TransportTool`) `G4AtlasActionInitialization` or use it
(`G4AtlasRunManager`, `G4AtlasUserWorkerThreadInitialization`,
`G4AtlasWorkerRunManager`).
---
 .../G4AtlasAlg/G4AtlasAlg/G4AtlasRunManager.h | 16 +++-------------
 .../G4AtlasUserWorkerThreadInitialization.h   |  8 +-------
 .../G4AtlasAlg/G4AtlasWorkerRunManager.h      | 11 +----------
 .../G4Atlas/G4AtlasAlg/src/G4AtlasAlg.cxx     | 11 ++++++++---
 .../G4AtlasAlg/src/G4AtlasRunManager.cxx      | 19 +------------------
 .../G4AtlasUserWorkerThreadInitialization.cxx |  3 +--
 .../src/G4AtlasWorkerRunManager.cxx           | 16 ++--------------
 .../src/G4LegacyTransportTool.cxx             |  5 ++++-
 .../ISF_Geant4Tools/src/TransportTool.cxx     |  9 +++++++--
 9 files changed, 28 insertions(+), 70 deletions(-)

diff --git a/Simulation/G4Atlas/G4AtlasAlg/G4AtlasAlg/G4AtlasRunManager.h b/Simulation/G4Atlas/G4AtlasAlg/G4AtlasAlg/G4AtlasRunManager.h
index b5a1ff692391..df7b8df2abcb 100644
--- a/Simulation/G4Atlas/G4AtlasAlg/G4AtlasAlg/G4AtlasRunManager.h
+++ b/Simulation/G4Atlas/G4AtlasAlg/G4AtlasAlg/G4AtlasRunManager.h
@@ -1,5 +1,5 @@
 /*
-  Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
+  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
 */
 
 #ifndef G4ATLASALG_G4AtlasRunManager_h
@@ -17,7 +17,6 @@
 #include "G4AtlasInterfaces/ISensitiveDetectorMasterTool.h"
 #include "G4AtlasInterfaces/IFastSimulationMasterTool.h"
 #include "G4AtlasInterfaces/IPhysicsListSvc.h"
-#include "G4AtlasInterfaces/IUserActionSvc.h"
 #include "G4AtlasInterfaces/IDetectorGeometrySvc.h"
 #include "G4AtlasInterfaces/IFluxRecorder.h"
 
@@ -41,13 +40,6 @@ public:
 
   /// G4 function called at end of run
   void RunTermination() override final;
-  
-  /// @name Methods to pass configuration in from G4AtlasAlg
-  /// @{
-  /// Configure the user action service handle
-  void SetUserActionSvc(const std::string& typeAndName) {
-    m_userActionSvc.setTypeAndName(typeAndName);
-  }
 
   /// Configure the detector geometry service handle
   void SetDetGeoSvc(const std::string& typeAndName) {
@@ -103,12 +95,10 @@ private:
   ToolHandle<IFastSimulationMasterTool> m_fastSimTool;
   ServiceHandle<IPhysicsListSvc> m_physListSvc;
 
-  /// Handle to the user action service
-  ServiceHandle<G4UA::IUserActionSvc> m_userActionSvc;
   ServiceHandle<IDetectorGeometrySvc> m_detGeoSvc;
-  
+
   /// Interface to flux recording
-  
+
   std::unique_ptr<IFluxRecorder> m_fluxRecorder;
 };
 
diff --git a/Simulation/G4Atlas/G4AtlasAlg/G4AtlasAlg/G4AtlasUserWorkerThreadInitialization.h b/Simulation/G4Atlas/G4AtlasAlg/G4AtlasAlg/G4AtlasUserWorkerThreadInitialization.h
index f8983ee6fd77..107901ae2b85 100644
--- a/Simulation/G4Atlas/G4AtlasAlg/G4AtlasAlg/G4AtlasUserWorkerThreadInitialization.h
+++ b/Simulation/G4Atlas/G4AtlasAlg/G4AtlasAlg/G4AtlasUserWorkerThreadInitialization.h
@@ -1,5 +1,5 @@
 /*
-  Copyright (C) 2002-2018 CERN for the benefit of the ATLAS collaboration
+  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
 */
 
 #ifndef G4ATLASALG_G4ATLASUSERWORKERTHREADINITIALIZATION_H
@@ -32,11 +32,6 @@ public:
 
   /// @name Methods to pass configuration in from G4AtlasAlg
   /// @{
-  /// Configure the user action service handle
-  void SetUserActionSvc(const std::string& typeAndName) {
-    m_userActionSvcName = typeAndName;
-  }
-
   /// Configure the detector geometry service handle
   void SetDetGeoSvc(const std::string& typeAndName) {
     m_detGeoSvcName = typeAndName;
@@ -56,7 +51,6 @@ private:
   std::string m_detGeoSvcName{"DetectorGeometrySvc"};
   std::string m_senDetToolName{"SensitiveDetectorMasterTool"};
   std::string m_fastSimToolName{"FastSimulationMasterTool"};
-  std::string m_userActionSvcName{"G4UA::UserActionSvc"};
 
 }; // class G4AtlasUserWorkerThreadInitialization
 
diff --git a/Simulation/G4Atlas/G4AtlasAlg/G4AtlasAlg/G4AtlasWorkerRunManager.h b/Simulation/G4Atlas/G4AtlasAlg/G4AtlasAlg/G4AtlasWorkerRunManager.h
index 93bc9c448a02..e9e93601809e 100644
--- a/Simulation/G4Atlas/G4AtlasAlg/G4AtlasAlg/G4AtlasWorkerRunManager.h
+++ b/Simulation/G4Atlas/G4AtlasAlg/G4AtlasAlg/G4AtlasWorkerRunManager.h
@@ -1,5 +1,5 @@
 /*
-  Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
+  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
 */
 
 #ifndef G4ATLASALG_G4ATLASWORKERRUNMANAGER_H
@@ -18,7 +18,6 @@
 #include "G4AtlasInterfaces/IDetectorGeometrySvc.h"
 #include "G4AtlasInterfaces/ISensitiveDetectorMasterTool.h"
 #include "G4AtlasInterfaces/IFastSimulationMasterTool.h"
-#include "G4AtlasInterfaces/IUserActionSvc.h"
 
 
 /// @brief ATLAS worker run manager for master-slave multi-threading model
@@ -41,11 +40,6 @@ public:
 
   /// @name Methods to pass configuration in from G4AtlasUserWorkerThreadInitialization
   /// @{
-  /// Configure the user action service handle
-  void SetUserActionSvc(const std::string& typeAndName) {
-    m_userActionSvc.setTypeAndName(typeAndName);
-  }
-
   /// Configure the detector geometry service handle
   void SetDetGeoSvc(const std::string& typeAndName) {
     m_detGeoSvc.setTypeAndName(typeAndName);
@@ -97,9 +91,6 @@ private:
   /// Handle to the FastSim tool
   ToolHandle<IFastSimulationMasterTool> m_fastSimTool;
 
-  /// Handle to the user action service
-  ServiceHandle<G4UA::IUserActionSvc> m_userActionSvc;
-
 };
 
 #endif // G4MULTITHREADED
diff --git a/Simulation/G4Atlas/G4AtlasAlg/src/G4AtlasAlg.cxx b/Simulation/G4Atlas/G4AtlasAlg/src/G4AtlasAlg.cxx
index 6cdac043e136..1f5ee78cbff1 100644
--- a/Simulation/G4Atlas/G4AtlasAlg/src/G4AtlasAlg.cxx
+++ b/Simulation/G4Atlas/G4AtlasAlg/src/G4AtlasAlg.cxx
@@ -1,10 +1,11 @@
 /*
-  Copyright (C) 2002-2018 CERN for the benefit of the ATLAS collaboration
+  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
 */
 
 // Local includes
 #include "G4AtlasAlg.h"
 #include "G4AtlasFluxRecorder.h"
+#include "G4AtlasAlg/G4AtlasActionInitialization.h"
 
 #include "AthenaKernel/RNGWrapper.h"
 
@@ -121,11 +122,13 @@ void G4AtlasAlg::initializeOnce()
     // Worker Thread initialization used to create worker run manager on demand.
     std::unique_ptr<G4AtlasUserWorkerThreadInitialization> workerInit =
       std::make_unique<G4AtlasUserWorkerThreadInitialization>();
-    workerInit->SetUserActionSvc( m_userActionSvc.typeAndName() );
     workerInit->SetDetGeoSvc( m_detGeoSvc.typeAndName() );
     workerInit->SetSDMasterTool( m_senDetTool.typeAndName() );
     workerInit->SetFastSimMasterTool( m_fastSimTool.typeAndName() );
     runMgr->SetUserInitialization( workerInit.release() );
+    std::unique_ptr<G4AtlasActionInitialization> actionInitialization =
+      std::make_unique<G4AtlasActionInitialization>(&*m_userActionSvc);
+    runMgr->SetUserInitialization(actionInitialization.release());
 #else
     throw std::runtime_error("Trying to use multi-threading in non-MT build!");
 #endif
@@ -136,11 +139,13 @@ void G4AtlasAlg::initializeOnce()
     m_physListSvc->SetPhysicsList();
     runMgr->SetRecordFlux( m_recordFlux, std::make_unique<G4AtlasFluxRecorder>() );
     runMgr->SetLogLevel( int(msg().level()) ); // Synch log levels
-    runMgr->SetUserActionSvc( m_userActionSvc.typeAndName() );
     runMgr->SetDetGeoSvc( m_detGeoSvc.typeAndName() );
     runMgr->SetSDMasterTool(m_senDetTool.typeAndName() );
     runMgr->SetFastSimMasterTool(m_fastSimTool.typeAndName() );
     runMgr->SetPhysListSvc(m_physListSvc.typeAndName() );
+    std::unique_ptr<G4AtlasActionInitialization> actionInitialization =
+      std::make_unique<G4AtlasActionInitialization>(&*m_userActionSvc);
+    runMgr->SetUserInitialization(actionInitialization.release());
   }
 
   // G4 user interface commands
diff --git a/Simulation/G4Atlas/G4AtlasAlg/src/G4AtlasRunManager.cxx b/Simulation/G4Atlas/G4AtlasAlg/src/G4AtlasRunManager.cxx
index 399ce45aaf84..7a538629ad0c 100644
--- a/Simulation/G4Atlas/G4AtlasAlg/src/G4AtlasRunManager.cxx
+++ b/Simulation/G4Atlas/G4AtlasAlg/src/G4AtlasRunManager.cxx
@@ -1,5 +1,5 @@
 /*
-  Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
+  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
 */
 
 #include "G4AtlasAlg/G4AtlasRunManager.h"
@@ -27,7 +27,6 @@ G4AtlasRunManager::G4AtlasRunManager()
   , m_senDetTool("SensitiveDetectorMasterTool")
   , m_fastSimTool("FastSimulationMasterTool")
   , m_physListSvc("PhysicsListSvc", "G4AtlasRunManager")
-  , m_userActionSvc("G4UA::UserActionSvc", "G4AtlasRunManager")
   , m_detGeoSvc("DetectorGeometrySvc", "G4AtlasRunManager")
 {  }
 
@@ -42,26 +41,10 @@ G4AtlasRunManager* G4AtlasRunManager::GetG4AtlasRunManager()
 
 void G4AtlasRunManager::Initialize()
 {
-  const std::string methodName = "G4AtlasRunManager::Initialize";
-  
   // ADA 11/28.2018: switch initialization order to meet ISF requirements
   // Call the base class Initialize method. This will call
   // InitializeGeometry and InitializePhysics.
   G4RunManager::Initialize();
-  
-  // Setup the user actions now.
-  if( !m_userActionSvc.name().empty() ) {
-    ATH_MSG_INFO("Creating user actions now");
-    if(m_userActionSvc.retrieve().isFailure()) {
-      throw GaudiException("Could not retrieve UserActionSvc",
-                           methodName, StatusCode::FAILURE);
-    }
-    if(m_userActionSvc->initializeActions().isFailure()) {
-      throw GaudiException("Failed to initialize actions",
-                           methodName, StatusCode::FAILURE);
-    }
-  }
-
 }
 
 
diff --git a/Simulation/G4Atlas/G4AtlasAlg/src/G4AtlasUserWorkerThreadInitialization.cxx b/Simulation/G4Atlas/G4AtlasAlg/src/G4AtlasUserWorkerThreadInitialization.cxx
index 4e52944fbd79..dd0ea43feb85 100644
--- a/Simulation/G4Atlas/G4AtlasAlg/src/G4AtlasUserWorkerThreadInitialization.cxx
+++ b/Simulation/G4Atlas/G4AtlasAlg/src/G4AtlasUserWorkerThreadInitialization.cxx
@@ -1,5 +1,5 @@
 /*
-  Copyright (C) 2002-2018 CERN for the benefit of the ATLAS collaboration
+  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
 */
 
 // Hide multi-threading classes from builds without G4MT
@@ -14,7 +14,6 @@ CreateWorkerRunManager() const
 {
   // TODO: maybe better to just use the pseudo-singleton mechanism
   auto* workerRunManager = G4AtlasWorkerRunManager::GetG4AtlasWorkerRunManager();
-  workerRunManager->SetUserActionSvc(m_userActionSvcName);
   workerRunManager->SetDetGeoSvc(m_detGeoSvcName);
   workerRunManager->SetSDMasterTool(m_senDetToolName);
   workerRunManager->SetFastSimMasterTool(m_fastSimToolName);
diff --git a/Simulation/G4Atlas/G4AtlasAlg/src/G4AtlasWorkerRunManager.cxx b/Simulation/G4Atlas/G4AtlasAlg/src/G4AtlasWorkerRunManager.cxx
index a8df9171b80d..28c651b654f7 100644
--- a/Simulation/G4Atlas/G4AtlasAlg/src/G4AtlasWorkerRunManager.cxx
+++ b/Simulation/G4Atlas/G4AtlasAlg/src/G4AtlasWorkerRunManager.cxx
@@ -1,5 +1,5 @@
 /*
-  Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
+  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
 */
 
 // Hide multi-threading classes from builds without G4MT
@@ -36,7 +36,6 @@ G4AtlasWorkerRunManager::G4AtlasWorkerRunManager()
   , m_detGeoSvc("DetectorGeometrySvc", "G4AtlasWorkerRunManager")
   , m_senDetTool("SensitiveDetectorMasterTool")
   , m_fastSimTool("FastSimulationMasterTool")
-  , m_userActionSvc("G4UA::UserActionSvc", "G4AtlasWorkerRunManager")
 {}
 
 
@@ -54,18 +53,6 @@ void G4AtlasWorkerRunManager::Initialize()
   // Locking this initialization to protect currently thread-unsafe services
   std::lock_guard<std::mutex> lock(workerInitMutex);
 
-  const std::string methodName = "G4AtlasWorkerRunManager::Initialize";
-
-  // Setup the user actions for current worker thread.
-  if(m_userActionSvc.retrieve().isFailure()) {
-    throw GaudiException("Could not retrieve UserActionSvc for worker thread",
-                         methodName, StatusCode::FAILURE);
-  }
-  if(m_userActionSvc->initializeActions().isFailure()) {
-    throw GaudiException("Failed to initialize actions for worker thread",
-                         methodName, StatusCode::FAILURE);
-  }
-
   // Setup geometry and physics via the base class
   G4RunManager::Initialize();
 
@@ -84,6 +71,7 @@ void G4AtlasWorkerRunManager::Initialize()
   ** will have to be thoroughly reviewed.
   */
   ATH_MSG_DEBUG("G4 Command: Trying at the end of Initialize()");
+  const std::string methodName = "G4AtlasWorkerRunManager::Initialize";
   G4MTRunManager* masterRM = G4MTRunManager::GetMasterRunManager();
   std::vector<G4String> cmds = masterRM->GetCommandStack();
   G4UImanager* uimgr = G4UImanager::GetUIpointer();
diff --git a/Simulation/ISF/ISF_Geant4/ISF_Geant4Tools/src/G4LegacyTransportTool.cxx b/Simulation/ISF/ISF_Geant4/ISF_Geant4Tools/src/G4LegacyTransportTool.cxx
index 8502dcaa70b2..4dbc60e91e74 100644
--- a/Simulation/ISF/ISF_Geant4/ISF_Geant4Tools/src/G4LegacyTransportTool.cxx
+++ b/Simulation/ISF/ISF_Geant4/ISF_Geant4Tools/src/G4LegacyTransportTool.cxx
@@ -7,6 +7,7 @@
 
 //package includes
 #include "G4AtlasAlg/G4AtlasRunManager.h"
+#include "G4AtlasAlg/G4AtlasActionInitialization.h"
 #include "ISFFluxRecorder.h"
 
 #include "AthenaKernel/RNGWrapper.h"
@@ -118,11 +119,13 @@ void iGeant4::G4LegacyTransportTool::initializeOnce()
 
   m_pRunMgr->SetRecordFlux( m_recordFlux, std::make_unique<ISFFluxRecorder>() );
   m_pRunMgr->SetLogLevel( int(msg().level()) ); // Synch log levels
-  m_pRunMgr->SetUserActionSvc( m_userActionSvc.typeAndName() );
   m_pRunMgr->SetDetGeoSvc( m_detGeoSvc.typeAndName() );
   m_pRunMgr->SetSDMasterTool(m_senDetTool.typeAndName() );
   m_pRunMgr->SetFastSimMasterTool(m_fastSimTool.typeAndName() );
   m_pRunMgr->SetPhysListSvc(m_physListSvc.typeAndName() );
+  std::unique_ptr<G4AtlasActionInitialization> actionInitialization =
+    std::make_unique<G4AtlasActionInitialization>(&*m_userActionSvc);
+  m_pRunMgr->SetUserInitialization(actionInitialization.release());
 
   G4UImanager *ui = G4UImanager::GetUIpointer();
 
diff --git a/Simulation/ISF/ISF_Geant4/ISF_Geant4Tools/src/TransportTool.cxx b/Simulation/ISF/ISF_Geant4/ISF_Geant4Tools/src/TransportTool.cxx
index 11d066c1a012..1eeef3c0b973 100644
--- a/Simulation/ISF/ISF_Geant4/ISF_Geant4Tools/src/TransportTool.cxx
+++ b/Simulation/ISF/ISF_Geant4/ISF_Geant4Tools/src/TransportTool.cxx
@@ -10,6 +10,7 @@
 #include "G4AtlasAlg/G4AtlasWorkerRunManager.h"
 #include "G4AtlasAlg/G4AtlasUserWorkerThreadInitialization.h"
 #include "G4AtlasAlg/G4AtlasRunManager.h"
+#include "G4AtlasAlg/G4AtlasActionInitialization.h"
 #include "ISFFluxRecorder.h"
 
 #include "AthenaKernel/RNGWrapper.h"
@@ -121,11 +122,13 @@ void iGeant4::G4TransportTool::initializeOnce()
     // Worker Thread initialization used to create worker run manager on demand.
     std::unique_ptr<G4AtlasUserWorkerThreadInitialization> workerInit =
       std::make_unique<G4AtlasUserWorkerThreadInitialization>();
-    workerInit->SetUserActionSvc( m_userActionSvc.typeAndName() );
     workerInit->SetDetGeoSvc( m_detGeoSvc.typeAndName() );
     workerInit->SetSDMasterTool( m_senDetTool.typeAndName() );
     workerInit->SetFastSimMasterTool( m_fastSimTool.typeAndName() );
     runMgr->SetUserInitialization( workerInit.release() );
+    std::unique_ptr<G4AtlasActionInitialization> actionInitialization =
+      std::make_unique<G4AtlasActionInitialization>(&*m_userActionSvc);
+    runMgr->SetUserInitialization(actionInitialization.release());
 #else
     throw std::runtime_error("Trying to use multi-threading in non-MT build!");
 #endif
@@ -136,11 +139,13 @@ void iGeant4::G4TransportTool::initializeOnce()
     m_physListSvc->SetPhysicsList();
     runMgr->SetRecordFlux( m_recordFlux, std::make_unique<ISFFluxRecorder>() );
     runMgr->SetLogLevel( int(msg().level()) ); // Synch log levels
-    runMgr->SetUserActionSvc( m_userActionSvc.typeAndName() );
     runMgr->SetDetGeoSvc( m_detGeoSvc.typeAndName() );
     runMgr->SetSDMasterTool(m_senDetTool.typeAndName() );
     runMgr->SetFastSimMasterTool(m_fastSimTool.typeAndName() );
     runMgr->SetPhysListSvc(m_physListSvc.typeAndName() );
+    std::unique_ptr<G4AtlasActionInitialization> actionInitialization =
+      std::make_unique<G4AtlasActionInitialization>(&*m_userActionSvc);
+    runMgr->SetUserInitialization(actionInitialization.release());
   }
 
   G4UImanager *ui = G4UImanager::GetUIpointer();
-- 
GitLab