forked from celeritas-project/celeritas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GeantGeoParams.cc
302 lines (269 loc) · 8.98 KB
/
GeantGeoParams.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
//----------------------------------*-C++-*----------------------------------//
// Copyright 2023-2024 UT-Battelle, LLC, and other Celeritas developers.
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file celeritas/ext/GeantGeoParams.cc
//---------------------------------------------------------------------------//
#include "GeantGeoParams.hh"
#include <vector>
#include <G4GeometryManager.hh>
#include <G4LogicalVolume.hh>
#include <G4LogicalVolumeStore.hh>
#include <G4Transportation.hh>
#include <G4TransportationManager.hh>
#include <G4VSolid.hh>
#include <G4Version.hh>
#include <G4VisExtent.hh>
#if G4VERSION_NUMBER >= 1070
# include <G4Backtrace.hh>
#endif
#include "celeritas_config.h"
#include "corecel/cont/Range.hh"
#include "corecel/io/Logger.hh"
#include "corecel/io/StringUtils.hh"
#include "corecel/sys/Device.hh"
#include "corecel/sys/ScopedMem.hh"
#include "Convert.geant.hh" // IWYU pragma: associated
#include "GeantGeoData.hh" // IWYU pragma: associated
#include "GeantGeoUtils.hh"
#include "GeantUtils.hh"
#include "ScopedGeantExceptionHandler.hh"
#include "ScopedGeantLogger.hh"
#include "VisitGeantVolumes.hh"
namespace celeritas
{
namespace
{
//---------------------------------------------------------------------------//
std::vector<Label>
get_volume_labels(G4LogicalVolume const& world, bool unique_volumes)
{
std::vector<Label> labels;
visit_geant_volumes(
[&](G4LogicalVolume const& lv) {
auto i = static_cast<std::size_t>(lv.GetInstanceID());
if (i >= labels.size())
{
labels.resize(i + 1);
}
if (unique_volumes)
{
labels[i] = Label::from_geant(make_gdml_name(lv));
}
else
{
labels[i] = Label::from_geant(lv.GetName());
}
},
world);
return labels;
}
//---------------------------------------------------------------------------//
} // namespace
//---------------------------------------------------------------------------//
/*!
* Construct from a GDML input.
*
* This assumes that Celeritas is driving and will manage Geant4 exceptions
* etc.
*/
GeantGeoParams::GeantGeoParams(std::string const& filename)
{
ScopedMem record_mem("GeantGeoParams.construct");
disable_geant_signal_handler();
if (!ends_with(filename, ".gdml"))
{
CELER_LOG(warning) << "Expected '.gdml' extension for GDML input";
}
host_ref_.world = load_geant_geometry(filename);
loaded_gdml_ = true;
// NOTE: only instantiate the logger/exception handler *after* loading
// Geant4 geometry, since something in the GDML parser's call chain resets
// the Geant logger.
scoped_logger_ = std::make_unique<ScopedGeantLogger>();
scoped_exceptions_ = std::make_unique<ScopedGeantExceptionHandler>();
this->build_tracking();
this->build_metadata();
CELER_ENSURE(this->num_volumes() > 0);
CELER_ENSURE(host_ref_);
}
//---------------------------------------------------------------------------//
/*!
* Use an existing loaded Geant4 geometry.
*/
GeantGeoParams::GeantGeoParams(G4VPhysicalVolume const* world)
{
CELER_EXPECT(world);
host_ref_.world = const_cast<G4VPhysicalVolume*>(world);
ScopedMem record_mem("GeantGeoParams.construct");
// Verify consistency of the world volume
G4VPhysicalVolume const* nav_world = [] {
auto* man = G4TransportationManager::GetTransportationManager();
CELER_ASSERT(man);
auto* nav = man->GetNavigatorForTracking();
CELER_ENSURE(nav);
return nav->GetWorldVolume();
}();
if (world != nav_world)
{
auto msg = CELER_LOG(warning);
msg << "Geant4 geometry was initialized with inconsistent "
"world volume: given '"
<< world->GetName() << "'@' " << static_cast<void const*>(world)
<< "; navigation world is ";
if (nav_world)
{
msg << nav_world->GetName() << "'@' "
<< static_cast<void const*>(nav_world);
}
else
{
msg << "unset";
}
}
this->build_tracking();
this->build_metadata();
CELER_ENSURE(this->num_volumes() > 0);
CELER_ENSURE(host_ref_);
}
//---------------------------------------------------------------------------//
/*!
* Clean up on destruction.
*/
GeantGeoParams::~GeantGeoParams()
{
if (closed_geometry_)
{
G4GeometryManager::GetInstance()->OpenGeometry(host_ref_.world);
}
if (loaded_gdml_)
{
reset_geant_geometry();
}
}
//---------------------------------------------------------------------------//
/*!
* Get the label for a placed volume ID.
*/
Label const& GeantGeoParams::id_to_label(VolumeId vol) const
{
CELER_EXPECT(vol < vol_labels_.size());
return vol_labels_.get(vol);
}
//---------------------------------------------------------------------------//
/*!
* Get the ID corresponding to a label.
*/
auto GeantGeoParams::find_volume(std::string const& name) const -> VolumeId
{
auto result = vol_labels_.find_all(name);
if (result.empty())
return {};
CELER_VALIDATE(result.size() == 1,
<< "volume '" << name << "' is not unique");
return result.front();
}
//---------------------------------------------------------------------------//
/*!
* Locate the volume ID corresponding to a label.
*
* If the label isn't in the geometry, a null ID will be returned.
*/
VolumeId GeantGeoParams::find_volume(Label const& label) const
{
return vol_labels_.find(label);
}
//---------------------------------------------------------------------------//
/*!
* Locate the volume ID corresponding to a Geant4 logical volume.
*/
VolumeId GeantGeoParams::find_volume(G4LogicalVolume const* volume) const
{
CELER_EXPECT(volume);
auto inst_id = volume->GetInstanceID();
CELER_ENSURE(inst_id >= 0);
VolumeId result{static_cast<VolumeId::size_type>(inst_id)};
if (!(result < this->num_volumes()))
{
// Volume is out of range: possibly an LV defined after this geometry
// class was created
result = {};
}
return result;
}
//---------------------------------------------------------------------------//
/*!
* Get zero or more volume IDs corresponding to a name.
*
* This is useful for volumes that are repeated in the geometry with different
* uniquifying 'extensions' from Geant4.
*/
auto GeantGeoParams::find_volumes(std::string const& name) const
-> SpanConstVolumeId
{
return vol_labels_.find_all(name);
}
//---------------------------------------------------------------------------//
/*!
* Get the Geant4 logical volume corresponding to a volume ID.
*
* If the input volume ID is false, a null pointer will be returned.
*/
G4LogicalVolume const* GeantGeoParams::id_to_lv(VolumeId id) const
{
CELER_EXPECT(!id || id < this->num_volumes());
if (!id)
{
return nullptr;
}
G4LogicalVolumeStore* lv_store = G4LogicalVolumeStore::GetInstance();
CELER_ASSERT(id < lv_store->size());
return (*lv_store)[id.unchecked_get()];
}
//---------------------------------------------------------------------------//
/*!
* Complete geometry construction
*/
void GeantGeoParams::build_tracking()
{
// Close the geometry if needed
auto* geo_man = G4GeometryManager::GetInstance();
CELER_ASSERT(geo_man);
if (!geo_man->IsGeometryClosed())
{
geo_man->CloseGeometry(
/* optimize = */ true, /* verbose = */ false, host_ref_.world);
closed_geometry_ = true;
}
}
//---------------------------------------------------------------------------//
/*!
* Construct Celeritas host-only metadata.
*/
void GeantGeoParams::build_metadata()
{
CELER_EXPECT(host_ref_);
ScopedMem record_mem("GeantGeoParams.build_metadata");
auto const* world_lv = host_ref_.world->GetLogicalVolume();
CELER_ASSERT(world_lv);
// Construct volume labels
vol_labels_ = LabelIdMultiMap<VolumeId>(
get_volume_labels(*world_lv, !loaded_gdml_));
// Save world bbox (NOTE: assumes no transformation on PV?)
bbox_ = [world_lv] {
G4VSolid const* solid = world_lv->GetSolid();
CELER_ASSERT(solid);
G4VisExtent const& extent = solid->GetExtent();
return BBox({convert_from_geant(G4ThreeVector(extent.GetXmin(),
extent.GetYmin(),
extent.GetZmin()),
clhep_length),
convert_from_geant(G4ThreeVector(extent.GetXmax(),
extent.GetYmax(),
extent.GetZmax()),
clhep_length)});
}();
}
//---------------------------------------------------------------------------//
} // namespace celeritas