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/DetectorConstruction.h"
17#include "SimCore/G4User/RunAction.h"
18#include "SimCore/G4User/StackingAction.h"
19#include "SimCore/G4User/SteppingAction.h"
22#include "SimCore/ParallelWorld.h"
23#include "SimCore/XsecBiasingOperator.h"
24
25//------------//
26// Geant4 //
27//------------//
28#include "FTFP_BERT.hh"
29#include "G4GDMLParser.hh"
30#include "G4GenericBiasingPhysics.hh"
31#include "G4ParallelWorldPhysics.hh"
32#include "G4ProcessTable.hh"
33#include "G4VModularPhysicsList.hh"
34
35namespace simcore {
36
39 parameters_ = parameters;
40
41 // Set whether the ROOT primary generator should use the persisted seed.
42 auto rootPrimaryGenUseSeed{
43 parameters.getParameter<bool>("rootPrimaryGenUseSeed")};
44
45 // Validate the geometry if specified.
46 setUseRootSeed(rootPrimaryGenUseSeed);
47}
48
50 auto pList{physicsListFactory_.GetReferencePhysList("FTFP_BERT")};
51
52 parallelWorldPath_ = parameters_.getParameter<std::string>("scoringPlanes");
54 if (isPWEnabled_) {
55 ldmx_log(debug) << "Parallel worlds physics list has been registered";
56 pList->RegisterPhysics(new G4ParallelWorldPhysics("ldmxParallelWorld"));
57 }
58
59 pList->RegisterPhysics(new GammaPhysics{"GammaPhysics", parameters_});
60 pList->RegisterPhysics(new APrimePhysics(
61 parameters_.getParameter<framework::config::Parameters>("dark_brem")));
62 pList->RegisterPhysics(new KaonPhysics(
63 "KaonPhysics", parameters_.getParameter<framework::config::Parameters>(
64 "kaon_parameters")));
65
66 auto biasing_operators{
67 parameters_.getParameter<std::vector<framework::config::Parameters>>(
68 "biasing_operators", {})};
69 if (!biasing_operators.empty()) {
70 ldmx_log(info) << " Biasing enabled with " << biasing_operators.size()
71 << " operator(s)";
72
73 // create all the biasing operators that will be used
74 for (framework::config::Parameters& bop : biasing_operators) {
76 bop.getParameter<std::string>("class_name"),
77 bop.getParameter<std::string>("instance_name"), bop);
78 }
79
80 // Instantiate the constructor used when biasing
81 G4GenericBiasingPhysics* biasingPhysics = new G4GenericBiasingPhysics();
82
83 // specify which particles are going to be biased
84 // this will put a biasing interface wrapper around *all* processes
85 // associated with these particles
87 [this, biasingPhysics](auto bop) {
88 ldmx_log(info) << "Biasing operator '" << bop->GetName()
89 << "' set to bias " << bop->getParticleToBias();
90 biasingPhysics->Bias(bop->getParticleToBias());
91 });
92
93 // Register the physics constructor to the physics list:
94 pList->RegisterPhysics(biasingPhysics);
95 }
96
97 this->SetUserInitialization(pList);
98}
99
101 setupPhysics();
102
103 // The parallel world needs to be registered before the mass world is
104 // constructed i.e. before G4RunManager::Initialize() is called.
105 if (isPWEnabled_) {
106 ldmx_log(debug) << "Parallel worlds have been enabled";
107
108 auto validateGeometry_{parameters_.getParameter<bool>("validate_detector")};
109 G4GDMLParser* pwParser = new G4GDMLParser();
110 pwParser->Read(parallelWorldPath_, validateGeometry_);
111 this->getDetectorConstruction()->RegisterParallelWorld(
112 new ParallelWorld(pwParser, "ldmxParallelWorld"));
113 }
114
115 // This is where the physics lists are told to construct their particles and
116 // their processes
117 // They are constructed in order, so it is important to register the biasing
118 // physics *after* any other processes that need to be able to be biased
119 G4RunManager::Initialize();
120
121 // create our G4User actions
122 auto primary_action{new g4user::PrimaryGeneratorAction(parameters_)};
123 auto run_action{new g4user::RunAction};
124 auto event_action{new g4user::EventAction};
125 auto tracking_action{new g4user::TrackingAction};
126 auto stepping_action{new g4user::SteppingAction};
127 auto stacking_action{new g4user::StackingAction};
128 // ...and register them with G4
129 SetUserAction(primary_action);
130 SetUserAction(run_action);
131 SetUserAction(event_action);
132 SetUserAction(tracking_action);
133 SetUserAction(stepping_action);
134 SetUserAction(stacking_action);
135
136 // Create all user actions and attch them to the corresponding G4 actions
137 auto user_actions{
138 parameters_.getParameter<std::vector<framework::config::Parameters>>(
139 "actions", {})};
140 for (auto& user_action : user_actions) {
141 auto ua = UserAction::Factory::get().make(
142 user_action.getParameter<std::string>("class_name"),
143 user_action.getParameter<std::string>("instance_name"), user_action);
144 for (auto& type : ua->getTypes()) {
145 if (type == simcore::TYPE::RUN) {
146 run_action->registerAction(ua.get());
147 } else if (type == simcore::TYPE::EVENT) {
148 event_action->registerAction(ua.get());
149 } else if (type == simcore::TYPE::TRACKING) {
150 tracking_action->registerAction(ua.get());
151 } else if (type == simcore::TYPE::STEPPING) {
152 stepping_action->registerAction(ua.get());
153 } else if (type == simcore::TYPE::STACKING) {
154 stacking_action->registerAction(ua.get());
155 } else {
156 EXCEPTION_RAISE("ActionType", "Action type does not exist.");
157 }
158 }
159 }
160}
161
163 // have geant4 do its own thing
164 G4RunManager::TerminateOneEvent();
165
166 // go through the processes attached to the electron and
167 // reactivate any process that contains the G4DarkBremmstrahlung name
168 // this covers both cases where the process is biased and not
169 static auto reactivate_dark_brem = [](G4ProcessManager* pman) {
170 for (int i_proc{0}; i_proc < pman->GetProcessList()->size(); i_proc++) {
171 G4VProcess* p{(*(pman->GetProcessList()))[i_proc]};
172 if (p->GetProcessName().contains(G4DarkBremsstrahlung::PROCESS_NAME)) {
173 pman->SetProcessActivation(p, true);
174 break;
175 }
176 }
177 };
178
179 reactivate_dark_brem(G4Electron::Definition()->GetProcessManager());
180
181 if (this->GetVerboseLevel() > 1) {
182 ldmx_log(debug) << "Reset the dark brem process (if it was activated)";
183 }
184}
185
187 return static_cast<DetectorConstruction*>(this->userDetector);
188}
189
190} // namespace simcore
Class which defines basic APrime physics.
Class which implements the Geant4 user event action.
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
Defines basic APrime physics.
Handle to the conditions system, provided at construction to classes which require it.
Implements the Geant4 detector construction.
void apply(UnaryFunction f) const
Apply the input UnaryFunction to each entry in the inventory.
Definition Factory.h:283
static Factory & get()
get the factory instance
Definition Factory.h:217
PrototypePtr make(const std::string &full_name, PrototypeConstructorArgs... maker_args)
make a new object by name
Definition Factory.h:265
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
std::string parallelWorldPath_
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
bool isPWEnabled_
Flag indicating whether a parallel world should be registered.
Definition RunManager.h:102
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
void Initialize()
Perform application initialization.
DetectorConstruction * getDetectorConstruction()
Get the user detector construction cast to a specific type.
G4PhysListFactory physicsListFactory_
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 Geant4 primary generator action.
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.