forked from acts-project/acts
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathGeant4Manager.cpp
More file actions
151 lines (127 loc) · 4.95 KB
/
Geant4Manager.cpp
File metadata and controls
151 lines (127 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// This file is part of the ACTS project.
//
// Copyright (C) 2016 CERN for the benefit of the ACTS project
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
#include "ActsExamples/Geant4/Geant4Manager.hpp"
#include "ActsExamples/Geant4/MaterialPhysicsList.hpp"
#include "ActsExamples/Geant4/PhysicsListFactory.hpp"
#include <memory>
#include <stdexcept>
#include <FTFP_BERT.hh>
#include <FTFP_BERT_ATL.hh>
#include <G4EmParameters.hh>
#include <G4HadronicParameters.hh>
#include <G4HadronicProcessStore.hh>
#include <G4RunManager.hh>
#include <G4RunManagerFactory.hh>
#include <G4UserEventAction.hh>
#include <G4UserRunAction.hh>
#include <G4UserSteppingAction.hh>
#include <G4UserTrackingAction.hh>
#include <G4VUserDetectorConstruction.hh>
#include <G4VUserPhysicsList.hh>
#include <G4VUserPrimaryGeneratorAction.hh>
#include <G4Version.hh>
namespace ActsExamples {
Geant4Handle::Geant4Handle(std::unique_ptr<G4RunManager> _runManager,
std::unique_ptr<G4VUserPhysicsList> _physicsList,
std::string _physicsListName)
: runManager(std::move(_runManager)),
physicsList(_physicsList.release()),
physicsListName(std::move(_physicsListName)) {
if (runManager == nullptr) {
std::invalid_argument("runManager cannot be null");
}
if (physicsList == nullptr) {
std::invalid_argument("physicsList cannot be null");
}
// Set physics list
runManager->SetUserInitialization(physicsList);
}
Geant4Handle::~Geant4Handle() = default;
void Geant4Handle::tweakLogging(int level) const {
Geant4Manager::tweakLogging(*runManager, level);
}
Geant4Manager& Geant4Manager::instance() {
static Geant4Manager manager;
return manager;
}
void Geant4Manager::tweakLogging(G4RunManager& runManager, int level) {
runManager.SetVerboseLevel(level);
G4EventManager::GetEventManager()->SetVerboseLevel(level);
G4EventManager::GetEventManager()->GetTrackingManager()->SetVerboseLevel(
level);
G4EventManager::GetEventManager()->GetStackManager()->SetVerboseLevel(level);
// Suppress the printing of physics information.
#if G4VERSION_NUMBER >= 1100
G4HadronicParameters::Instance()->SetVerboseLevel(0);
G4HadronicProcessStore::Instance()->SetVerbose(0);
G4EmParameters::Instance()->SetIsPrintedFlag(true);
#endif
}
std::shared_ptr<Geant4Handle> Geant4Manager::currentHandle() const {
return m_handle.lock();
}
std::shared_ptr<Geant4Handle> Geant4Manager::createHandle(
const std::string& physicsList) {
return createHandle(createPhysicsList(physicsList), physicsList);
}
std::shared_ptr<Geant4Handle> Geant4Manager::createHandle(
std::unique_ptr<G4VUserPhysicsList> physicsList,
std::string physicsListName) {
if (!m_handle.expired()) {
throw std::runtime_error("creating a second handle is prohibited");
}
if (m_created) {
throw std::runtime_error(
"creating a new handle is prohibited. you have to hold onto the "
"first one.");
}
auto runManager = std::unique_ptr<G4RunManager>(
G4RunManagerFactory::CreateRunManager(G4RunManagerType::SerialOnly));
auto handle = std::make_shared<Geant4Handle>(std::move(runManager),
std::move(physicsList),
std::move(physicsListName));
m_created = true;
m_handle = handle;
return handle;
}
void Geant4Manager::registerPhysicsListFactory(
std::string name,
std::shared_ptr<Geant4::PhysicsListFactory> physicsListFactory) {
if (m_physicsListFactories.contains(name)) {
throw std::invalid_argument("name already mapped");
}
m_physicsListFactories.emplace(std::move(name),
std::move(physicsListFactory));
}
std::unique_ptr<G4VUserPhysicsList> Geant4Manager::createPhysicsList(
const std::string& name) const {
auto it = m_physicsListFactories.find(name);
if (it == m_physicsListFactories.end()) {
throw std::invalid_argument("name not mapped");
}
return it->second->factorize();
}
const std::unordered_map<std::string,
std::shared_ptr<Geant4::PhysicsListFactory>>&
Geant4Manager::getPhysicsListFactories() const {
return m_physicsListFactories;
}
Geant4Manager::Geant4Manager() {
registerPhysicsListFactory(
"FTFP_BERT", std::make_shared<Geant4::PhysicsListFactoryFunction>(
[]() { return std::make_unique<FTFP_BERT>(); }));
registerPhysicsListFactory(
"FTFP_BERT_ATL", std::make_shared<Geant4::PhysicsListFactoryFunction>(
[]() { return std::make_unique<FTFP_BERT_ATL>(); }));
registerPhysicsListFactory(
"MaterialPhysicsList",
std::make_shared<Geant4::PhysicsListFactoryFunction>(
[]() { return std::make_unique<Geant4::MaterialPhysicsList>(); }));
}
Geant4Manager::~Geant4Manager() = default;
} // namespace ActsExamples