1#include "SimCore/SimulatorBase.h"
10 "/persistency/gdml/read"
13SimulatorBase::SimulatorBase(
const std::string& name,
15 :
framework::Producer(name, process), conditionsIntf_(this) {
16 uiManager_ = G4UImanager::GetUIpointer();
19 auto event_info =
static_cast<UserEventInformation*
>(
20 runManager_->GetCurrentEvent()->GetUserInformation());
22 eventHeader.
setWeight(event_info->getWeight());
24 event_info->getPNEnergy());
26 event_info->getENEnergy());
28 event_info->getDarkBremMaterialZ());
30void SimulatorBase::onProcessEnd() {
31 runManager_->TerminateEventLoop();
32 runManager_->RunTermination();
44 runManager_.reset(
nullptr);
49 sessionHandle_.reset(
nullptr);
51void SimulatorBase::onProcessStart() {
53 runManager_->Initialize();
55 for (
const std::string& cmd : postInitCommands_) {
56 int g4Ret = uiManager_->ApplyCommand(cmd);
58 EXCEPTION_RAISE(
"PostInitCmd",
59 "Post Initialization command '" + cmd +
60 "' returned a failue status from Geant4: " +
61 std::to_string(g4Ret));
66 runManager_->ConstructScoringWorlds();
69 runManager_->RunInitialization();
72 runManager_->InitializeEventLoop(1);
76void SimulatorBase::verifyParameters()
const {
81 if (parameters_.exists(
"runNumber")) {
82 EXCEPTION_RAISE(
"InvalidParam",
83 "Remove old-style of setting the simulation run number "
85 " Replace with using the Process object (p.run).");
90 for (
const auto& invalidCommand : invalidCommands_) {
91 for (
const auto& cmd : preInitCommands_) {
92 if (cmd.find(invalidCommand) != std::string::npos) {
93 EXCEPTION_RAISE(
"PreInitCmd",
"Pre Initialization command '" + cmd +
94 "' is not allowed because another "
95 "part of Simulator handles it.");
98 for (
const auto& cmd : postInitCommands_) {
99 if (cmd.find(invalidCommand) != std::string::npos) {
100 EXCEPTION_RAISE(
"PostInitCmd",
"Post Initialization command '" + cmd +
101 "' is not allowed because another "
102 "part of Simulator handles it.");
110 parameters_ = parameters;
112 verbosity_ = parameters_.
getParameter<
int>(
"verbosity");
115 parameters_.getParameter<std::vector<std::string>>(
"preInitCommands", {});
118 postInitCommands_ = parameters_.getParameter<std::vector<std::string>>(
119 "postInitCommands", {});
125 "MultipleSimulators",
126 "A simulator or resimulator producer has already been created. Only "
127 "one of them can be present in a given run. To run the resimulator, "
128 "use a an existing eventFile as input.");
133 runManager_ = std::make_unique<RunManager>(parameters_, conditionsIntf_);
136 G4CascadeParameters::Instance();
139 for (
const std::string& cmd : preInitCommands_) {
140 int g4Ret = uiManager_->ApplyCommand(cmd);
142 EXCEPTION_RAISE(
"PreInitCmd",
143 "Pre Initialization command '" + cmd +
144 "' returned a failure status from Geant4: " +
145 std::to_string(g4Ret));
150void SimulatorBase::createLogging() {
151 auto loggingPrefix = parameters_.getParameter<std::string>(
"logging_prefix");
153 sessionHandle_ = std::make_unique<BatchSession>();
154 else if (verbosity_ > 1) {
155 if (loggingPrefix.empty())
156 sessionHandle_ = std::make_unique<LoggedSession>();
158 sessionHandle_ = std::make_unique<LoggedSession>(
159 loggingPrefix +
"_G4cout.log", loggingPrefix +
"_G4cerr.log");
161 if (sessionHandle_ !=
nullptr)
162 uiManager_->SetCoutDestination(sessionHandle_.get());
166 TrackMap& tracks{g4user::TrackingAction::get()->getTrackMap()};
167 tracks.traceAncestry();
168 event.add(
"SimParticles", tracks.getParticleMap());
172 SensitiveDetector::Factory::get().apply([&event](
auto sd) {
174 sd->OnFinishedEvent();
178void SimulatorBase::buildGeometry() {
182 "gdml", parameters_, conditionsIntf_)};
186 runManager_->SetUserInitialization(
187 new DetectorConstruction(parser, parameters_, conditionsIntf_));
190 auto detectorPath{parameters_.getParameter<std::string>(
"detector")};
191 if (verbosity_ > 0) {
192 std::cout <<
"[ Simulator ] : Reading in geometry from '" << detectorPath
193 <<
"'... " << std::flush;
195 G4GeometryManager::GetInstance()->OpenGeometry();
197 runManager_->DefineWorldVolume(parser->GetWorldVolume());
Implements an event buffer system for storing event data.
Class which represents the process under execution.
Class encapsulating parameters for configuring a processor.
T getParameter(const std::string &name) const
Retrieve the parameter of the given name.
static const std::vector< std::string > invalidCommands_
Commands not allowed to be passed from python config file This is because Simulator already runs them...
Parser * createParser(const std::string &name, framework::config::Parameters ¶meters, simcore::ConditionsInterface &ci)
Create an instance of the parser of the given type.
static ParserFactory & getInstance()
Get the instance to this factory.
virtual void read()=0
Parse the detector geometry and read it into memory.
All classes in the ldmx-sw project use this namespace.