Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion include/AdePT/integration/AdePTTrackingManager.hh
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,11 @@ private:
/// @brief Get the corresponding VecGeom NavigationState from the track's G4NavigationHistory, and set the boundary
/// status based on the track's state
/// @param aG4Track The G4Track from which to extract the NavigationState
/// @param aG4NavigationHistory Navigation history that is used to define the navState. If not provided, it will
/// default to aG4Track.GetNextTouchableHandle()->GetHistory()
/// @return The corresponding vecgeom::NavigationState
const vecgeom::NavigationState GetVecGeomFromG4State(const G4Track &aG4Track);
const vecgeom::NavigationState GetVecGeomFromG4State(const G4Track &aG4Track,
const G4NavigationHistory *aG4NavigationHistory = nullptr);

std::unique_ptr<G4HepEmTrackingManagerSpecialized> fHepEmTrackingManager;
static inline int fNumThreads{0};
Expand Down
5 changes: 3 additions & 2 deletions include/AdePT/integration/HostTrackDataMapper.hh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

#include "G4VUserTrackInformation.hh"
#include "G4VProcess.hh"
#include "G4TouchableHandle.hh"

#include <VecGeom/navigation/NavigationState.h>

#include <unordered_map>
#include <cstdint>
Expand All @@ -28,7 +29,7 @@ struct HostTrackData {
G4LogicalVolume *logicalVolumeAtVertex = nullptr;
G4ThreeVector vertexPosition;
G4ThreeVector vertexMomentumDirection;
std::unique_ptr<G4TouchableHandle> originTouchableHandle;
vecgeom::NavigationState originNavState;
G4double vertexKineticEnergy = 0.0;
unsigned char particleType;

Expand Down
10 changes: 4 additions & 6 deletions src/AdePTGeant4Integration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -662,10 +662,7 @@ void AdePTGeant4Integration::FillG4Step(GPUHit const *aGPUHit, G4Step *aG4Step,
G4ThreeVector{aGPUHit->fPostStepPoint.fMomentumDirection.x(), aGPUHit->fPostStepPoint.fMomentumDirection.y(),
aGPUHit->fPostStepPoint.fMomentumDirection.z()};
hostTData.vertexKineticEnergy = aGPUHit->fPostStepPoint.fEKin;
if (hostTData.originTouchableHandle) {
hostTData.originTouchableHandle =
std::make_unique<G4TouchableHandle>(MakeTouchableFromNavState(aGPUHit->fPostStepPoint.fNavigationState));
}
hostTData.originNavState = aGPUHit->fPostStepPoint.fNavigationState;

// For the initializing step, the step defining process ID is the creator process
const int stepId = aGPUHit->fStepLimProcessId;
Expand Down Expand Up @@ -906,8 +903,9 @@ void AdePTGeant4Integration::ReturnTrack(adeptint::TrackData const &track, unsig
leakedTrack->SetVertexMomentumDirection(hostTData.vertexMomentumDirection);
leakedTrack->SetVertexKineticEnergy(hostTData.vertexKineticEnergy);
leakedTrack->SetLogicalVolumeAtVertex(hostTData.logicalVolumeAtVertex);
if (hostTData.originTouchableHandle) {
leakedTrack->SetOriginTouchableHandle(*hostTData.originTouchableHandle);
if (callUserActions) {
auto originTouchableHandle = MakeTouchableFromNavState(hostTData.originNavState);
leakedTrack->SetOriginTouchableHandle(originTouchableHandle);
}

// ------ Handle leaked tracks according to their status, if not LeakStatus::OutOfGPURegion ---------
Expand Down
28 changes: 16 additions & 12 deletions src/AdePTTrackingManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -397,23 +397,21 @@ void AdePTTrackingManager::ProcessTrack(G4Track *aTrack)
// Get VecGeom Navigation state from G4History
vecgeom::NavigationState converted = GetVecGeomFromG4State(*aTrack);

// Setting of the touchable should only be done when the userActions are called,
// as this otherwise leaves the touchable data for tracks that die on the GPU intact,
// causing a destructor fiasco when G4 is already shut down
// The VecGeom NavState is stored in the hostTrackData; in principle, the G4TouchableHandle could also be stored
// directly, but it has proven to be very expensive to create new G4TouchableHandle objects for each track in the
// HostTrackData. Instead, it was much cheaper to just store the vecgeom::NavState and create the
// G4TouchableHandle only when the track is returned from the GPU
if (callUserActions) {
if (aTrack->GetParentID() == 0 && aTrack->GetCurrentStepNumber() == 0) {
// For the first step of primary tracks, the origin touchable handle is not set,
// so we need to use the track's current position
// If the vertex is not in a GPU region, the origin touchable handle will be set by the HepEmTrackingManager
if (aTrack->GetTouchable()) {
hostTrackData.originTouchableHandle = std::make_unique<G4TouchableHandle>(aTrack->GetTouchableHandle());
}
hostTrackData.originNavState = converted;
} else {
// For secondary tracks, the origin touchable handle is set when they are stacked
if (aTrack->GetOriginTouchable()) {
hostTrackData.originTouchableHandle =
std::make_unique<G4TouchableHandle>(aTrack->GetOriginTouchableHandle());
}
vecgeom::NavigationState convertedOrigin =
GetVecGeomFromG4State(*aTrack, aTrack->GetOriginTouchableHandle()->GetHistory());
hostTrackData.originNavState = convertedOrigin;
}
}

Expand Down Expand Up @@ -494,9 +492,15 @@ const vecgeom::NavigationState AdePTTrackingManager::GetVecGeomFromG4State(
return aNavState;
}

const vecgeom::NavigationState AdePTTrackingManager::GetVecGeomFromG4State(const G4Track &aG4Track)
const vecgeom::NavigationState AdePTTrackingManager::GetVecGeomFromG4State(
const G4Track &aG4Track, const G4NavigationHistory *aG4NavigationHistory)
{
auto aNavState = GetVecGeomFromG4State(*aG4Track.GetNextTouchableHandle()->GetHistory());

if (!aG4NavigationHistory) {
aG4NavigationHistory = aG4Track.GetNextTouchableHandle()->GetHistory();
}

auto aNavState = GetVecGeomFromG4State(*aG4NavigationHistory);

// Set boundary status based on the track
if (aG4Track.GetStep() != nullptr) { // at initialization, the G4Step is not set yet, then we put OnBoundary to false
Expand Down