Skip to content

Commit

Permalink
refactor: Refactor MaterialInteractor (#2691)
Browse files Browse the repository at this point in the history
Aimed to improve consistency and split surface and volume interactions into two logical parts

pulled out of #2603
  • Loading branch information
andiwand authored Nov 20, 2023
1 parent 1d8d3ec commit 825db48
Showing 1 changed file with 96 additions and 73 deletions.
169 changes: 96 additions & 73 deletions Core/include/Acts/Propagator/MaterialInteractor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,115 +60,134 @@ struct MaterialInteractor {
return;
}

// In case of Volume material update the result of the previous step
if (recordInteractions && !result.materialInteractions.empty() &&
!result.materialInteractions.back().volume.empty() &&
result.materialInteractions.back().updatedVolumeStep == false) {
updateResult(state, stepper, result);
}

// Do nothing if nothing is what is requested.
if (!(multipleScattering || energyLoss || recordInteractions)) {
return;
}
// We only have material interactions if there is potential material
const Surface* surface = navigator.currentSurface(state.navigation);
const TrackingVolume* volume = navigator.currentVolume(state.navigation);

if (!(surface && surface->surfaceMaterial()) &&
!(volume && volume->volumeMaterial())) {
return;
}
// Handle surface material

// Note that start and target surface conditions are handled in the
// interaction code
const Surface* surface = navigator.currentSurface(state.navigation);

// We only have material interactions if there is potential material
if (surface && surface->surfaceMaterial()) {
ACTS_VERBOSE("MaterialInteractor | "
<< "Found material on surface " << surface->geometryId());

// Prepare relevant input particle properties
detail::PointwiseMaterialInteraction d(surface, state, stepper);
detail::PointwiseMaterialInteraction interaction(surface, state, stepper);

// Determine the effective traversed material and its properties
// Material exists but it's not real, i.e. vacuum; there is nothing to do
if (!d.evaluateMaterialSlab(state, navigator)) {
return;
if (interaction.evaluateMaterialSlab(state, navigator)) {
// Evaluate the material effects
interaction.evaluatePointwiseMaterialInteraction(multipleScattering,
energyLoss);

if (energyLoss) {
using namespace UnitLiterals;
ACTS_VERBOSE("MaterialInteractor | "
<< interaction.slab << " absPdg=" << interaction.absPdg
<< " mass=" << interaction.mass / 1_MeV << "MeV"
<< " momentum=" << interaction.momentum / 1_GeV << "GeV"
<< " energyloss=" << interaction.Eloss / 1_MeV << "MeV");
}

// To integrate process noise, we need to transport
// the covariance to the current position in space
if (interaction.performCovarianceTransport) {
stepper.transportCovarianceToCurvilinear(state.stepping);
}
// Change the noise updater depending on the navigation direction
NoiseUpdateMode mode = (state.options.direction == Direction::Forward)
? addNoise
: removeNoise;
// Apply the material interactions
interaction.updateState(state, stepper, mode);

// Record the result
recordResult(interaction, result);
}
}

// Evaluate the material effects
d.evaluatePointwiseMaterialInteraction(multipleScattering, energyLoss);
// Handle volume material

if (energyLoss) {
using namespace UnitLiterals;
ACTS_VERBOSE(d.slab << " absPdg=" << d.absPdg
<< " mass=" << d.mass / 1_MeV << "MeV"
<< " momentum=" << d.momentum / 1_GeV << "GeV"
<< " energyloss=" << d.Eloss / 1_MeV << "MeV");
}
// In case of Volume material update the result of the previous step
if (!result.materialInteractions.empty() &&
!result.materialInteractions.back().volume.empty() &&
result.materialInteractions.back().updatedVolumeStep == false) {
updateResult(state, stepper, result);
}

const TrackingVolume* volume = navigator.currentVolume(state.navigation);

// We only have material interactions if there is potential material
if (volume && volume->volumeMaterial()) {
ACTS_VERBOSE("MaterialInteractor | "
<< "Found material in volume " << volume->geometryId());

// To integrate process noise, we need to transport
// the covariance to the current position in space
if (d.performCovarianceTransport) {
stepper.transportCovarianceToCurvilinear(state.stepping);
}
// Change the noise updater depending on the navigation direction
NoiseUpdateMode mode = (state.options.direction == Direction::Forward)
? addNoise
: removeNoise;
// Apply the material interactions
d.updateState(state, stepper, mode);
// Record the result
recordResult(d, result);
} else if (recordInteractions && volume && volume->volumeMaterial()) {
// Prepare relevant input particle properties
detail::VolumeMaterialInteraction d(volume, state, stepper);
detail::VolumeMaterialInteraction interaction(volume, state, stepper);
// Determine the effective traversed material and its properties
// Material exists but it's not real, i.e. vacuum; there is nothing to do
if (!d.evaluateMaterialSlab(state, navigator)) {
return;
if (interaction.evaluateMaterialSlab(state, navigator)) {
// Record the result
recordResult(interaction, result);
}
// Record the result
recordResult(d, result);
}
}

private:
/// @brief This function records the material effect
///
/// @param [in] d Data cache container
/// @param [in] interaction Interaction cache container
/// @param [in, out] result Result storage
void recordResult(const detail::PointwiseMaterialInteraction& d,
void recordResult(const detail::PointwiseMaterialInteraction& interaction,
result_type& result) const {
result.materialInX0 += d.slab.thicknessInX0();
result.materialInL0 += d.slab.thicknessInL0();
result.materialInX0 += interaction.slab.thicknessInX0();
result.materialInL0 += interaction.slab.thicknessInL0();

// Record the interaction if requested
if (recordInteractions) {
MaterialInteraction mi;
mi.position = d.pos;
mi.time = d.time;
mi.direction = d.dir;
mi.deltaP = d.nextP - d.momentum;
mi.sigmaPhi2 = d.variancePhi;
mi.sigmaTheta2 = d.varianceTheta;
mi.sigmaQoP2 = d.varianceQoverP;
mi.surface = d.surface;
mi.volume = InteractionVolume();
mi.pathCorrection = d.pathCorrection;
mi.materialSlab = d.slab;
result.materialInteractions.push_back(std::move(mi));
if (!recordInteractions) {
return;
}

MaterialInteraction mi;
mi.position = interaction.pos;
mi.time = interaction.time;
mi.direction = interaction.dir;
mi.deltaP = interaction.nextP - interaction.momentum;
mi.sigmaPhi2 = interaction.variancePhi;
mi.sigmaTheta2 = interaction.varianceTheta;
mi.sigmaQoP2 = interaction.varianceQoverP;
mi.surface = interaction.surface;
mi.volume = InteractionVolume();
mi.pathCorrection = interaction.pathCorrection;
mi.materialSlab = interaction.slab;
result.materialInteractions.push_back(std::move(mi));
}

/// @brief This function records the material effect
///
/// @param [in] d Data cache container
/// @param [in] interaction Interaction cache container
/// @param [in, out] result Result storage
void recordResult(const detail::VolumeMaterialInteraction& d,
void recordResult(const detail::VolumeMaterialInteraction& interaction,
result_type& result) const {
// Record the interaction
// Record the interaction if requested
if (!recordInteractions) {
return;
}

MaterialInteraction mi;
mi.position = d.pos;
mi.time = d.time;
mi.direction = d.dir;
mi.position = interaction.pos;
mi.time = interaction.time;
mi.direction = interaction.dir;
mi.surface = nullptr;
mi.volume = d.volume;
mi.pathCorrection = d.pathCorrection;
mi.materialSlab = d.slab;
mi.volume = interaction.volume;
mi.pathCorrection = interaction.pathCorrection;
mi.materialSlab = interaction.slab;
result.materialInteractions.push_back(std::move(mi));
}

Expand All @@ -180,7 +199,11 @@ struct MaterialInteractor {
template <typename propagator_state_t, typename stepper_t>
void updateResult(propagator_state_t& state, const stepper_t& stepper,
result_type& result) const {
// Update the previous interaction
// Update the previous interaction if requested
if (!recordInteractions) {
return;
}

Vector3 shift = stepper.position(state.stepping) -
result.materialInteractions.back().position;
double momentum = stepper.direction(state.stepping).norm();
Expand Down

0 comments on commit 825db48

Please sign in to comment.