GGS(GenericGEANT4Simulation)Software  2.99.0
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Macros
GGSMUAMessenger.cpp
Go to the documentation of this file.
1 /*
2  * GGSMUAMessenger.cpp
3  *
4  * Created on: 10 Nov 10 2020
5  * Author: Nicola Mori
6  */
7 
12 #include "montecarlo/mccore/GGSRunManagerExtensions.h"
13 #include "utils/GGSFactory.h"
14 
15 GGSMUAMessenger::GGSMUAMessenger(GGSMultiUserAction *muAction, const G4String &path, const G4String &dsc)
16  : G4UImessenger(path, dsc), _muAction(muAction), _gaBuildCommand("/GGS/generatorActions/set", this) {
17  CreateCommands(GGSFactory<GGSUserAction>::GetInstance().GetListOfRegisteredBuilders());
18  CreateCommands(GGSFactory<G4UserSteppingAction>::GetInstance().GetListOfRegisteredBuilders());
19  CreateCommands(GGSFactory<G4UserTrackingAction>::GetInstance().GetListOfRegisteredBuilders());
20  CreateCommands(GGSFactory<G4UserEventAction>::GetInstance().GetListOfRegisteredBuilders());
21  CreateCommands(GGSFactory<G4UserRunAction>::GetInstance().GetListOfRegisteredBuilders());
22  CreateCommands(GGSFactory<G4UserStackingAction>::GetInstance().GetListOfRegisteredBuilders());
23 }
24 
25 void GGSMUAMessenger::CreateCommands(const std::vector<std::string> &userActionClasses) {
26  for (auto &className : userActionClasses) {
27  _uaBuildCommands.push_back(std::make_unique<G4UIcommand>(("/GGS/userActions/add" + className).c_str(), this));
28  }
29 }
30 
31 void GGSMUAMessenger::BuildAction(const std::string &actionClassName) {
32 
33  auto BuildWithFactory = [&actionClassName, this](auto &factory) {
34  const auto &avaialbleBuilders = factory.GetListOfRegisteredBuilders();
35  if (std::find(avaialbleBuilders.begin(), avaialbleBuilders.end(), actionClassName) != avaialbleBuilders.end()) {
36  _muAction->AddAction(factory.CreateObject(actionClassName));
37  return true;
38  }
39  return false;
40  };
41 
42  if (!(BuildWithFactory(GGSFactory<GGSUserAction>::GetInstance()))) {
43  if (!(BuildWithFactory(GGSFactory<G4UserSteppingAction>::GetInstance()))) {
44  if (!(BuildWithFactory(GGSFactory<G4UserTrackingAction>::GetInstance()))) {
45  if (!(BuildWithFactory(GGSFactory<G4UserEventAction>::GetInstance()))) {
46  if (!(BuildWithFactory(GGSFactory<G4UserRunAction>::GetInstance()))) {
47  if (!(BuildWithFactory(GGSFactory<G4UserStackingAction>::GetInstance()))) {
48  G4ExceptionDescription ed;
49  ed << "Can't build " << actionClassName << " user action" << std::endl;
50  G4Exception("GGSUserActionInitialization::UAIMessenger::SetNewValue", "No builder for given user action",
51  G4ExceptionSeverity::FatalException, ed);
52  }
53  }
54  }
55  }
56  }
57  }
58 }
59 
60 void GGSMUAMessenger::SetNewValue(G4UIcommand *command, G4String newValue) {
61  std::string path = command->GetCommandPath();
62  if (path.size() > 20 && path.substr(0, 20) == "/GGS/userActions/add") {
63  // Build user actions
64  auto actionClassName = path.substr(20);
65  BuildAction(actionClassName);
66  } else if (path == "/GGS/generatorActions/set") {
67  auto &rme = dynamic_cast<GGSRunManagerExtensions &>(*(G4RunManager::GetRunManager()));
68  if (G4RunManager::GetRunManager()->GetUserPrimaryGeneratorAction()) {
69  delete G4RunManager::GetRunManager()->GetUserPrimaryGeneratorAction();
70  }
71  // Build the generator
72  std::unique_ptr<G4VUserPrimaryGeneratorAction> gen =
74  if (!gen) {
75  G4ExceptionDescription ed;
76  ed << "No registered builder for generator " << newValue << std::endl;
77  G4Exception("GGSUserActionInitialization::UAIMessenger::SetNewValue", "Invalid generator",
78  G4ExceptionSeverity::FatalException, ed);
79  }
80  // Add to the run manager
81  if (G4Threading::IsMultithreadedApplication() && G4Threading::IsMasterThread()) {
82  // Hold the generator to keep eventual secondary commands alive, but don't pass it to the run manager of the
83  // master thread
84  _genForMaster = std::move(gen);
85  } else {
86  rme.SetGGSGeneratorAction(gen.release());
87  }
88  }
89 }
std::unique_ptr< T > CreateObject(const std::string &name, ConstructorArgs...arguments)
Create an object.
Definition: GGSFactory.h:116
Class with additional functionalities for run managers.
Template factory class.
Definition: GGSFactory.h:30
A multiplexer container for user actions.
void AddAction(std::unique_ptr< GGSUserAction > &&userAction)
Adds a general user action.
static GGSFactory & GetInstance()
Getter method for singleton pointer.
Definition: GGSFactory.h:72