forked from celeritas-project/celeritas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GlobalSetup.cc
197 lines (177 loc) · 6.57 KB
/
GlobalSetup.cc
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//----------------------------------*-C++-*----------------------------------//
// Copyright 2022-2023 UT-Battelle, LLC, and other Celeritas developers.
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file celer-g4/GlobalSetup.cc
//---------------------------------------------------------------------------//
#include "GlobalSetup.hh"
#include <fstream>
#include <utility>
#include <G4GenericMessenger.hh>
#include <G4UImanager.hh>
#include "corecel/Assert.hh"
#include "corecel/io/Logger.hh"
#include "corecel/io/StringUtils.hh"
#include "corecel/sys/Device.hh"
#include "celeritas/ext/RootFileManager.hh"
#include "celeritas/field/RZMapFieldInput.hh"
#include "accel/ExceptionConverter.hh"
#include "accel/SetupOptionsMessenger.hh"
#include "HepMC3PrimaryGeneratorAction.hh"
#include "RootIO.hh"
#if CELERITAS_USE_JSON
# include <nlohmann/json.hpp>
# include "RunInputIO.json.hh"
#endif
namespace celeritas
{
namespace app
{
//---------------------------------------------------------------------------//
/*!
* Return non-owning pointer to a singleton.
*
* Creating the instance also creates a "messenger" that allows control over
* the Celeritas user inputs.
*/
GlobalSetup* GlobalSetup::Instance()
{
static GlobalSetup setup;
static SetupOptionsMessenger mess{setup.options_.get()};
return &setup;
}
//---------------------------------------------------------------------------//
/*!
* Set configurable properties from the UI.
*/
GlobalSetup::GlobalSetup()
{
options_ = std::make_shared<SetupOptions>();
messenger_ = std::make_unique<G4GenericMessenger>(
this, "/celerg4/", "Demo geant integration setup");
{
auto& cmd = messenger_->DeclareProperty("geometryFile",
input_.geometry_file);
cmd.SetGuidance("Filename of the GDML detector geometry");
}
{
auto& cmd = messenger_->DeclareProperty("eventFile", input_.event_file);
cmd.SetGuidance("Filename of the event input read by HepMC3");
}
{
auto& cmd = messenger_->DeclareProperty("stepDiagnostic",
input_.step_diagnostic);
cmd.SetGuidance("Collect the distribution of steps per Geant4 track");
cmd.SetDefaultValue("false");
}
{
auto& cmd = messenger_->DeclareProperty("stepDiagnosticBins",
input_.step_diagnostic_bins);
cmd.SetGuidance("Number of bins for the Geant4 step diagnostic");
cmd.SetDefaultValue(std::to_string(input_.step_diagnostic_bins));
}
// Setup options for the magnetic field
{
auto& cmd = messenger_->DeclareProperty("fieldType", input_.field_type);
cmd.SetGuidance("Select the field type [rzmap|uniform]");
cmd.SetDefaultValue(input_.field_type);
}
{
auto& cmd = messenger_->DeclareProperty("fieldFile", input_.field_file);
cmd.SetGuidance("Filename of the rz-map loaded by RZMapFieldInput");
}
{
messenger_->DeclareMethod("magFieldZ",
&GlobalSetup::SetMagFieldZTesla,
"Set Z-axis magnetic field strength (T)");
}
}
//---------------------------------------------------------------------------//
/*!
* Set the list of ignored EM process names.
*/
void GlobalSetup::SetIgnoreProcesses(SetupOptions::VecString ignored)
{
options_->ignore_processes = std::move(ignored);
}
//---------------------------------------------------------------------------//
/*!
* Read input from macro or JSON.
*/
void GlobalSetup::ReadInput(std::string const& filename)
{
if (ends_with(filename, ".json"))
{
CELER_LOG(status) << "Reading JSON input from '" << filename << "'";
std::ifstream infile(filename);
CELER_VALIDATE(infile, << "failed to open '" << filename << "'");
#if CELERITAS_USE_JSON
nlohmann::json::parse(infile).get_to(input_);
#else
CELER_NOT_CONFIGURED("nlohmann_json");
#endif
// Input options
if (CELERITAS_CORE_GEO == CELERITAS_CORE_GEO_ORANGE)
{
// To allow ORANGE to work for testing purposes, pass the GDML
// input filename to Celeritas
options_->geometry_file = input_.geometry_file;
}
// Output options
options_->output_file = input_.output_file;
options_->physics_output_file = input_.physics_output_file;
options_->offload_output_file = input_.offload_output_file;
// Apply Celeritas \c SetupOptions commands
options_->max_num_tracks = input_.num_track_slots;
options_->max_num_events = input_.max_events;
options_->max_steps = input_.max_steps;
options_->initializer_capacity = input_.initializer_capacity;
options_->secondary_stack_factor = input_.secondary_stack_factor;
options_->sd.enabled = input_.sd_type != SensitiveDetectorType::none;
options_->cuda_stack_size = input_.cuda_stack_size;
options_->cuda_heap_size = input_.cuda_heap_size;
options_->sync = input_.sync;
options_->default_stream = input_.default_stream;
}
else if (ends_with(filename, ".mac"))
{
input_.macro_file = filename;
}
// Execute macro for Geant4 commands (e.g. to set verbosity)
if (!input_.macro_file.empty())
{
CELER_LOG(status) << "Executing macro commands from '" << filename
<< "'";
G4UImanager* ui = G4UImanager::GetUIpointer();
CELER_ASSERT(ui);
ui->ApplyCommand("/control/execute " + input_.macro_file);
}
// Set the filename for JSON output
if (CELERITAS_USE_JSON && input_.output_file.empty())
{
input_.output_file = "celer-g4.out.json";
options_->output_file = input_.output_file;
}
if (input_.sd_type == SensitiveDetectorType::event_hit)
{
root_sd_io_ = RootFileManager::use_root();
if (!root_sd_io_)
{
CELER_LOG(warning) << "Collecting SD hit data that will not be "
"written because ROOT is disabled";
}
}
else
{
root_sd_io_ = false;
}
// Start the timer for setup time
get_setup_time_ = {};
}
//---------------------------------------------------------------------------//
//! Default destructor
GlobalSetup::~GlobalSetup() = default;
//---------------------------------------------------------------------------//
} // namespace app
} // namespace celeritas