forked from celeritas-project/celeritas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HitProcessor.cc
273 lines (241 loc) · 9.71 KB
/
HitProcessor.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
//----------------------------------*-C++-*----------------------------------//
// Copyright 2022-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 accel/detail/HitProcessor.cc
//---------------------------------------------------------------------------//
#include "HitProcessor.hh"
#include <string>
#include <utility>
#include <CLHEP/Units/SystemOfUnits.h>
#include <G4LogicalVolume.hh>
#include <G4Navigator.hh>
#include <G4Step.hh>
#include <G4StepPoint.hh>
#include <G4ThreeVector.hh>
#include <G4TouchableHistory.hh>
#include <G4Track.hh>
#include <G4TransportationManager.hh>
#include <G4VSensitiveDetector.hh>
#include <G4Version.hh>
#include "corecel/cont/EnumArray.hh"
#include "corecel/cont/Range.hh"
#include "corecel/io/Logger.hh"
#include "celeritas/Quantities.hh"
#include "celeritas/Types.hh"
#include "celeritas/ext/Convert.geant.hh"
#include "celeritas/user/DetectorSteps.hh"
#include "celeritas/user/StepData.hh"
#include "TouchableUpdater.hh"
namespace celeritas
{
namespace detail
{
//---------------------------------------------------------------------------//
/*!
* Construct local navigator and step data.
*/
HitProcessor::HitProcessor(SPConstVecLV detector_volumes,
VecParticle const& particles,
StepSelection const& selection,
bool locate_touchable)
: detector_volumes_(std::move(detector_volumes))
{
CELER_EXPECT(detector_volumes_ && !detector_volumes_->empty());
CELER_VALIDATE(!locate_touchable || selection.points[StepPoint::pre].pos,
<< "cannot set 'locate_touchable' because the pre-step "
"position is not being collected");
CELER_VALIDATE(!locate_touchable || selection.points[StepPoint::pre].pos,
<< "cannot set 'locate_touchable' because the pre-step "
"position is not being collected");
// Create step and step-owned structures
step_ = std::make_unique<G4Step>();
step_->NewSecondaryVector();
#if G4VERSION_NUMBER >= 1103
# define HP_CLEAR_STEP_POINT(CMD) step_->CMD(nullptr)
#else
# define HP_CLEAR_STEP_POINT(CMD) /* no "reset" before v11.0.3 */
#endif
#define HP_SETUP_POINT(LOWER, TITLE) \
do \
{ \
if (!selection.points[StepPoint::LOWER]) \
{ \
HP_CLEAR_STEP_POINT(Reset##TITLE##StepPoint); \
} \
else \
{ \
step_->Get##TITLE##StepPoint()->SetStepStatus(fUserDefinedLimit); \
} \
} while (0)
HP_SETUP_POINT(pre, Pre);
HP_SETUP_POINT(post, Post);
#undef HP_SETUP_POINT
#undef HP_CLEAR_STEP_POINT
if (locate_touchable)
{
CELER_ASSERT(selection.points[StepPoint::pre].pos
&& selection.points[StepPoint::pre].dir);
// Create navigator
G4VPhysicalVolume* world_volume
= G4TransportationManager::GetTransportationManager()
->GetNavigatorForTracking()
->GetWorldVolume();
navi_ = std::make_unique<G4Navigator>();
navi_->SetWorldVolume(world_volume);
touch_handle_ = new G4TouchableHistory;
step_->GetPreStepPoint()->SetTouchableHandle(touch_handle_);
}
// Create track if user requested particle types
for (G4ParticleDefinition const* pd : particles)
{
CELER_ASSERT(pd);
tracks_.emplace_back(new G4Track(
new G4DynamicParticle(pd, G4ThreeVector()), 0.0, G4ThreeVector()));
tracks_.back()->SetTrackID(-1);
tracks_.back()->SetParentID(-1);
}
// Create secondary vector if using track data
// Convert logical volumes (global) to sensitive detectors (thread local)
CELER_LOG_LOCAL(debug) << "Setting up " << detector_volumes_->size()
<< " sensitive detectors";
detectors_.resize(detector_volumes_->size());
for (auto i : range(detectors_.size()))
{
G4LogicalVolume const* lv = (*detector_volumes_)[i];
CELER_ASSERT(lv);
detectors_[i] = lv->GetSensitiveDetector();
CELER_VALIDATE(detectors_[i],
<< "no sensitive detector is attached to volume '"
<< lv->GetName() << "'@"
<< static_cast<void const*>(lv));
}
CELER_ENSURE(!detectors_.empty());
}
//---------------------------------------------------------------------------//
HitProcessor::~HitProcessor() = default;
//---------------------------------------------------------------------------//
/*!
* Process detector tallies (CPU).
*/
void HitProcessor::operator()(StepStateHostRef const& states)
{
copy_steps(&steps_, states);
if (steps_)
{
(*this)(steps_);
}
}
//---------------------------------------------------------------------------//
/*!
* Process detector tallies (GPU).
*/
void HitProcessor::operator()(StepStateDeviceRef const& states)
{
copy_steps(&steps_, states);
if (steps_)
{
(*this)(steps_);
}
}
//---------------------------------------------------------------------------//
/*!
* Generate and call hits from a detector output.
*
* In an application setting, this is always called with our local data \c
* steps_ as an argument. For tests, we can call this function explicitly using
* local test data.
*/
void HitProcessor::operator()(DetectorStepOutput const& out) const
{
CELER_EXPECT(!out.detector.empty());
CELER_ASSERT(!navi_ || !out.points[StepPoint::pre].pos.empty());
CELER_ASSERT(!navi_ || !out.points[StepPoint::pre].dir.empty());
CELER_ASSERT(tracks_.empty() || !out.particle.empty());
CELER_LOG_LOCAL(debug) << "Processing " << out.size() << " hits";
EnumArray<StepPoint, G4StepPoint*> points
= {step_->GetPreStepPoint(), step_->GetPostStepPoint()};
for (auto i : range(out.size()))
{
#define HP_SET(SETTER, OUT, UNITS) \
do \
{ \
if (!OUT.empty()) \
{ \
SETTER(convert_to_geant(OUT[i], UNITS)); \
} \
} while (0)
HP_SET(step_->SetTotalEnergyDeposit, out.energy_deposition, CLHEP::MeV);
for (auto sp : range(StepPoint::size_))
{
if (!points[sp])
{
continue;
}
HP_SET(points[sp]->SetGlobalTime, out.points[sp].time, clhep_time);
HP_SET(points[sp]->SetPosition, out.points[sp].pos, clhep_length);
HP_SET(points[sp]->SetKineticEnergy,
out.points[sp].energy,
CLHEP::MeV);
HP_SET(points[sp]->SetMomentumDirection, out.points[sp].dir, 1);
}
#undef HP_SET
if (navi_)
{
G4LogicalVolume const* lv = this->detector_volume(out.detector[i]);
// Update navigation state
constexpr auto sp = StepPoint::pre;
TouchableUpdater update_touchable{navi_.get(), touch_handle_()};
bool success = update_touchable(
out.points[sp].pos[i], out.points[sp].dir[i], lv);
if (CELER_UNLIKELY(!success))
{
// Inconsistent touchable: skip this energy deposition
CELER_LOG_LOCAL(error)
<< "Omitting energy deposition of "
<< step_->GetTotalEnergyDeposit() / CLHEP::MeV << " [MeV]";
continue;
}
// Copy attributes from logical volume
points[sp]->SetMaterial(lv->GetMaterial());
points[sp]->SetMaterialCutsCouple(lv->GetMaterialCutsCouple());
points[sp]->SetSensitiveDetector(lv->GetSensitiveDetector());
}
if (!tracks_.empty())
{
this->update_track(out.particle[i]);
}
// Hit sensitive detector
this->detector(out.detector[i])->Hit(step_.get());
}
}
//---------------------------------------------------------------------------//
/*!
* Recreate the track from the particle ID and saved post-step data.
*
* This is a bit like \c G4Step::UpdateTrack .
*/
void HitProcessor::update_track(ParticleId id) const
{
CELER_EXPECT(id < tracks_.size());
G4Track& track = *tracks_[id.unchecked_get()];
step_->SetTrack(&track);
if (G4StepPoint* pre_step = step_->GetPreStepPoint())
{
// Copy data from track to pre-step
G4ParticleDefinition const& pd = *track.GetParticleDefinition();
pre_step->SetCharge(pd.GetPDGCharge());
}
if (G4StepPoint* post_step = step_->GetPostStepPoint())
{
// Copy data from post-step to track
track.SetGlobalTime(post_step->GetGlobalTime());
track.SetPosition(post_step->GetPosition());
track.SetKineticEnergy(post_step->GetKineticEnergy());
track.SetMomentumDirection(post_step->GetMomentumDirection());
}
}
//---------------------------------------------------------------------------//
} // namespace detail
} // namespace celeritas