LDMX Software
RunManager.cxx
Go to the documentation of this file.
1
8
9//-------------//
10// ldmx-sw //
11//-------------//
12#include "G4DarkBreM/G4DarkBremsstrahlung.h" //for process name
14#include "SimCore/BiasOperators/XsecBiasingOperator.h"
15#include "SimCore/DetectorConstruction.h"
16#include "SimCore/FCPPhysics.h"
18#include "SimCore/G4User/RunAction.h"
19#include "SimCore/G4User/StackingAction.h"
20#include "SimCore/G4User/SteppingAction.h"
23#include "SimCore/ParallelWorld.h"
25
26//------------//
27// Geant4 //
28//------------//
29#include "FTFP_BERT.hh"
30#include "G4GDMLParser.hh"
31#include "G4GenericBiasingPhysics.hh"
32#include "G4ParallelWorldPhysics.hh"
33#include "G4ProcessTable.hh"
34#include "G4VModularPhysicsList.hh"
35
36namespace simcore {
37
40 parameters_ = parameters;
41
42 // Set whether the ROOT primary generator should use the persisted seed.
43 auto root_primary_gen_use_seed{
44 parameters.get<bool>("root_primary_gen_use_seed")};
45
46 // Validate the geometry if specified.
47 setUseRootSeed(root_primary_gen_use_seed);
48}
49
51 auto p_list{physics_list_factory_.GetReferencePhysList("FTFP_BERT")};
52 p_list->SetVerboseLevel(0);
53
54 parallel_world_path_ = parameters_.get<std::string>("scoring_planes");
56 if (is_pw_enabled_) {
57 ldmx_log(debug) << "Parallel worlds physics list has been registered";
58 p_list->RegisterPhysics(new G4ParallelWorldPhysics("ldmxParallelWorld"));
59 }
60
61 p_list->RegisterPhysics(new GammaPhysics{"GammaPhysics", parameters_});
62 p_list->RegisterPhysics(new APrimePhysics(
64 p_list->RegisterPhysics(new KaonPhysics(
65 "KaonPhysics",
66 parameters_.get<framework::config::Parameters>("kaon_parameters")));
67 p_list->RegisterPhysics(new FCPPhysics(
68 "FCPPhysics",
70
71 auto biasing_operators{
72 parameters_.get<std::vector<framework::config::Parameters>>(
73 "biasing_operators", {})};
74 if (!biasing_operators.empty()) {
75 ldmx_log(info) << " Biasing enabled with " << biasing_operators.size()
76 << " operator(s)";
77
78 // create all the biasing operators that will be used
79 for (framework::config::Parameters& bop : biasing_operators) {
80 if (not simcore::XsecBiasingOperator::Factory::get().make(
81 bop.get<std::string>("class_name"),
82 bop.get<std::string>("instance_name"), bop)) {
83 EXCEPTION_RAISE("UnableToCreate",
84 "Unable to create a XsecBiasingOperator of type " +
85 bop.get<std::string>("class_name"));
86 }
87 }
88
89 // Instantiate the constructor used when biasing
90 G4GenericBiasingPhysics* biasing_physics = new G4GenericBiasingPhysics();
91
92 // specify which particles are going to be biased
93 // this will put a biasing interface wrapper around *all* processes
94 // associated with these particles
95 simcore::XsecBiasingOperator::Factory::get().apply(
96 [this, biasing_physics](auto bop) {
97 ldmx_log(info) << "Biasing operator '" << bop->GetName()
98 << "' set to bias " << bop->getParticleToBias();
99 biasing_physics->Bias(bop->getParticleToBias());
100 });
101
102 // Register the physics constructor to the physics list:
103 p_list->RegisterPhysics(biasing_physics);
104 }
105
106 this->SetUserInitialization(p_list);
107}
108
110 setupPhysics();
111
112 // The parallel world needs to be registered before the mass world is
113 // constructed i.e. before G4RunManager::Initialize() is called.
114 if (is_pw_enabled_) {
115 ldmx_log(debug) << "Parallel worlds have been enabled";
116
117 auto validate_geometry{parameters_.get<bool>("validate_detector")};
118 G4GDMLParser* pw_parser = new G4GDMLParser();
119 pw_parser->Read(parallel_world_path_, validate_geometry);
120 this->getDetectorConstruction()->RegisterParallelWorld(
121 new ParallelWorld(pw_parser, "ldmxParallelWorld"));
122 }
123
124 // This is where the physics lists are told to construct their particles and
125 // their processes
126 // They are constructed in order, so it is important to register the biasing
127 // physics *after* any other processes that need to be able to be biased
128 G4RunManager::Initialize();
129
130 // create our G4User actions
131 auto primary_action{new PrimaryGeneratorAction(parameters_)};
132 auto run_action{new g4user::RunAction};
133 auto event_action{new g4user::EventAction};
134 auto tracking_action{new g4user::TrackingAction};
135 auto stepping_action{new g4user::SteppingAction};
136 auto stacking_action{new g4user::StackingAction};
137 // ...and register them with G4
138 SetUserAction(primary_action);
139 SetUserAction(run_action);
140 SetUserAction(event_action);
141 SetUserAction(tracking_action);
142 SetUserAction(stepping_action);
143 SetUserAction(stacking_action);
144
145 // Create all user actions and attch them to the corresponding G4 actions
146 auto user_actions{parameters_.get<std::vector<framework::config::Parameters>>(
147 "actions", {})};
148 for (auto& user_action : user_actions) {
149 auto ua = UserAction::Factory::get().make(
150 user_action.get<std::string>("class_name"),
151 user_action.get<std::string>("instance_name"), user_action);
152 if (not ua) {
153 EXCEPTION_RAISE(
154 "UnableToCreate",
155 "Unable to create a UserAction of type " +
156 user_action.get<std::string>("class_name") +
157 ". Did you inherit from simcore::UserAction? "
158 "Do you have DECLARE_ACTION in your implementation (.cxx) file? "
159 "Did you include the fully-specified class name in your python "
160 "configuration class? "
161 "Did you specify the correct library in the python configuration "
162 "class?");
163 }
164 for (auto& type : ua.value()->getTypes()) {
165 if (type == simcore::TYPE::RUN) {
166 run_action->registerAction(ua.value());
167 } else if (type == simcore::TYPE::EVENT) {
168 event_action->registerAction(ua.value());
169 } else if (type == simcore::TYPE::TRACKING) {
170 tracking_action->registerAction(ua.value());
171 } else if (type == simcore::TYPE::STEPPING) {
172 stepping_action->registerAction(ua.value());
173 } else if (type == simcore::TYPE::STACKING) {
174 stacking_action->registerAction(ua.value());
175 } else {
176 EXCEPTION_RAISE("ActionType", "Action type does not exist.");
177 }
178 }
179 }
180}
181
183 // have geant4 do its own thing
184 G4RunManager::TerminateOneEvent();
185
186 // go through the processes attached to the electron and
187 // reactivate any process that contains the G4DarkBremmstrahlung name
188 // this covers both cases where the process is biased and not
189 static auto reactivate_dark_brem = [](G4ProcessManager* pman) {
190 for (int i_proc{0}; i_proc < pman->GetProcessList()->size(); i_proc++) {
191 G4VProcess* p{(*(pman->GetProcessList()))[i_proc]};
192 if (p->GetProcessName().contains(G4DarkBremsstrahlung::PROCESS_NAME)) {
193 pman->SetProcessActivation(p, true);
194 break;
195 }
196 }
197 };
198
199 reactivate_dark_brem(G4Electron::Definition()->GetProcessManager());
200}
201
203 return static_cast<DetectorConstruction*>(this->userDetector);
204}
205
206} // namespace simcore
Class which defines basic APrime physics.
Class which implements the Geant4 user event action.
Physics constructor for fractionally charged particle (FCP) processes.
Class used to enhanced the gamma physics list.
Class implementing the Geant4 primary generator action.
Class providing a Geant4 run manager implementation.
Class which implements the user tracking action.
Class encapsulating parameters for configuring a processor.
Definition Parameters.h:29
const T & get(const std::string &name) const
Retrieve the parameter of the given name.
Definition Parameters.h:78
Defines basic APrime physics.
Handle to the conditions system, provided at construction to classes which require it.
Implements the Geant4 detector construction.
Physics constructor for fractionally charged particles.
Definition FCPPhysics.h:32
extra gamma particle physics for simulation and sets up the photonuclear model to use from the config...
Allows the configuration of properties of kaons produced in the simulation, in particular setting the...
Definition KaonPhysics.h:32
Implementation of Geant4 primary generator action.
std::string parallel_world_path_
Path to GDML description of parallel world.
Definition RunManager.h:105
framework::config::Parameters parameters_
The set of parameters used to configure the RunManager.
Definition RunManager.h:91
void TerminateOneEvent()
Called at the end of each event.
void setupPhysics()
Initialize physics.
void setUseRootSeed(bool useIt=true)
Tell RunManager to use the seed from the root file.
Definition RunManager.h:82
bool is_pw_enabled_
Flag indicating whether a parallel world should be registered.
Definition RunManager.h:102
void Initialize()
Perform application initialization.
DetectorConstruction * getDetectorConstruction()
Get the user detector construction cast to a specific type.
G4PhysListFactory physics_list_factory_
Factory class for instantiating the physics list.
Definition RunManager.h:96
RunManager(framework::config::Parameters &parameters, ConditionsInterface &)
Class constructor.
Implementation of user event action hook.
Definition EventAction.h:44
Implementation of user run action hook.
Definition RunAction.h:34
Class implementing a user stacking action.
Implements the Geant4 user stepping action.
Implementation of user tracking action.
Dynamically loadable photonuclear models either from SimCore or external libraries implementing this ...