Skip to content

Commit 8f8fe4d

Browse files
authored
chore: Cleanup Fatras interaction code (#5257)
- use concrete types over `auto` - use `const` when possible - group related variables
1 parent d157670 commit 8f8fe4d

File tree

8 files changed

+143
-137
lines changed

8 files changed

+143
-137
lines changed

Fatras/include/ActsFatras/Physics/ElectroMagnetic/BetheBloch.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ struct BetheBloch {
5656
// probable value and the Gaussian-equivalent sigma
5757
LandauDistribution lossDistribution(scaleFactorMPV * energyLoss,
5858
scaleFactorSigma * energyLossSigma);
59-
const auto loss = lossDistribution(generator);
59+
const double loss = lossDistribution(generator);
6060

6161
// Apply the energy loss
6262
particle.correctEnergy(-loss);

Fatras/include/ActsFatras/Physics/ElectroMagnetic/BetheHeitler.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ struct BetheHeitler {
6161
detail::FpeSafeGammaDistribution gDist(
6262
slab.thicknessInX0() / std::numbers::ln2, 1.);
6363

64-
const auto u = gDist(generator);
65-
const auto z = std::exp(-u);
66-
const auto sampledEnergyLoss =
64+
const double u = gDist(generator);
65+
const double z = std::exp(-u);
66+
const double sampledEnergyLoss =
6767
std::abs(scaleFactor * particle.energy() * (z - 1.));
6868

6969
std::uniform_real_distribution<double> uDist(0., 1.);

Fatras/include/ActsFatras/Physics/ElectroMagnetic/PhotonConversion.hpp

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ class PhotonConversion {
6767
/// This method constructs and returns the child particles.
6868
///
6969
/// @param [in] photon The interacting photon
70-
/// @param [in] childEnergy The energy of one child particle
71-
/// @param [in] childDirection The direction of the child particle
70+
/// @param [in] childEnergy The energy of the first child particle
71+
/// @param [in] childDirection The direction of the first child particle
7272
///
7373
/// @return Array containing the produced leptons
7474
std::array<Particle, 2> generateChildren(
@@ -225,34 +225,30 @@ double PhotonConversion::generateFirstChildEnergyFraction(
225225
template <typename generator_t>
226226
Acts::Vector3 PhotonConversion::generateChildDirection(
227227
generator_t& generator, const Particle& particle) const {
228-
/// This method is based upon the Athena class PhotonConversionTool
229-
230-
// Following the Geant4 approximation from L. Urban
231-
// the azimutal angle
232-
double theta = electronMass() / particle.energy();
228+
// This method is based upon the Athena class PhotonConversionTool
233229

230+
// Following the Geant4 approximation from L. Urban the azimutal angle
234231
std::uniform_real_distribution<double> uniformDistribution{0., 1.};
235232
const double u = -std::log(uniformDistribution(generator) *
236233
uniformDistribution(generator)) *
237234
1.6;
238-
239-
theta *= (uniformDistribution(generator) < 0.25)
240-
? u
241-
: u * 1. / 3.; // 9./(9.+27) = 0.25
235+
const double theta = (electronMass() / particle.energy()) *
236+
((uniformDistribution(generator) < 0.25)
237+
? u
238+
: u * 1. / 3.); // 9./(9.+27) = 0.25
242239

243240
// draw the random orientation angle
244241
const auto psi = std::uniform_real_distribution<double>(
245242
-std::numbers::pi, std::numbers::pi)(generator);
246243

247-
Acts::Vector3 direction = particle.direction();
248244
// construct the combined rotation to the scattered direction
249-
Acts::RotationMatrix3 rotation(
245+
const Acts::RotationMatrix3 rotation(
250246
// rotation of the scattering deflector axis relative to the reference
251-
Acts::AngleAxis3(psi, direction) *
247+
Acts::AngleAxis3(psi, particle.direction()) *
252248
// rotation by the scattering angle around the deflector axis
253-
Acts::AngleAxis3(theta, Acts::createCurvilinearUnitU(direction)));
254-
direction.applyOnTheLeft(rotation);
255-
return direction;
249+
Acts::AngleAxis3(theta,
250+
Acts::createCurvilinearUnitU(particle.direction())));
251+
return rotation * particle.direction();
256252
}
257253

258254
inline std::array<Particle, 2> PhotonConversion::generateChildren(
@@ -262,13 +258,13 @@ inline std::array<Particle, 2> PhotonConversion::generateChildren(
262258

263259
// Calculate the child momentum
264260
const double massChild = electronMass();
265-
const double momentum1 = Acts::fastCathetus(childEnergy, massChild);
261+
const double absoluteMomentum1 = Acts::fastCathetus(childEnergy, massChild);
266262

267263
// Use energy-momentum conservation for the other child
268264
const Acts::Vector3 vtmp =
269265
photon.fourMomentum().template segment<3>(Acts::eMom0) -
270-
momentum1 * childDirection;
271-
const double momentum2 = vtmp.norm();
266+
absoluteMomentum1 * childDirection;
267+
const double absoluteMomentum2 = vtmp.norm();
272268

273269
// The daughter particles are created with the explicit electron mass used in
274270
// the calculations for consistency. Using the full Particle constructor with
@@ -279,14 +275,14 @@ inline std::array<Particle, 2> PhotonConversion::generateChildren(
279275
electronMass())
280276
.setPosition4(photon.fourPosition())
281277
.setDirection(childDirection)
282-
.setAbsoluteMomentum(momentum1)
278+
.setAbsoluteMomentum(absoluteMomentum1)
283279
.setProcess(ProcessType::ePhotonConversion)
284280
.setReferenceSurface(photon.referenceSurface()),
285281
Particle(photon.particleId().makeDescendant(1), Acts::ePositron, 1_e,
286282
electronMass())
287283
.setPosition4(photon.fourPosition())
288284
.setDirection(childDirection)
289-
.setAbsoluteMomentum(momentum2)
285+
.setAbsoluteMomentum(absoluteMomentum2)
290286
.setProcess(ProcessType::ePhotonConversion)
291287
.setReferenceSurface(photon.referenceSurface()),
292288
};
@@ -311,11 +307,11 @@ bool PhotonConversion::run(generator_t& generator, Particle& particle,
311307
const double childEnergy = p * generateFirstChildEnergyFraction(generator, p);
312308

313309
// Now get the deflection
314-
const Acts::Vector3 childDir = generateChildDirection(generator, particle);
310+
const Acts::Vector3 child1Dir = generateChildDirection(generator, particle);
315311

316312
// Produce the final state
317313
const std::array<Particle, 2> finalState =
318-
generateChildren(particle, childEnergy, childDir);
314+
generateChildren(particle, childEnergy, child1Dir);
319315
generated.insert(generated.end(), finalState.begin(), finalState.end());
320316

321317
return true;

Fatras/include/ActsFatras/Physics/ElectroMagnetic/Scattering.hpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,19 @@ struct GenericScattering {
5353
// drawn from the specific scattering model distribution.
5454

5555
// draw the random orientation angle
56-
const auto psi = std::uniform_real_distribution<double>(
56+
const double psi = std::uniform_real_distribution<double>(
5757
-std::numbers::pi, std::numbers::pi)(generator);
5858
// draw the scattering angle
59-
const auto theta = angle(generator, slab, particle);
59+
const double theta = angle(generator, slab, particle);
6060

61-
Acts::Vector3 direction = particle.direction();
6261
// construct the combined rotation to the scattered direction
63-
Acts::RotationMatrix3 rotation(
62+
const Acts::RotationMatrix3 rotation(
6463
// rotation of the scattering deflector axis relative to the reference
65-
Acts::AngleAxis3(psi, direction) *
64+
Acts::AngleAxis3(psi, particle.direction()) *
6665
// rotation by the scattering angle around the deflector axis
67-
Acts::AngleAxis3(theta, Acts::createCurvilinearUnitU(direction)));
68-
direction.applyOnTheLeft(rotation);
69-
particle.setDirection(direction);
66+
Acts::AngleAxis3(theta,
67+
Acts::createCurvilinearUnitU(particle.direction())));
68+
particle.setDirection(rotation * particle.direction());
7069

7170
// scattering is non-destructive and produces no secondaries
7271
return {};

Fatras/include/ActsFatras/Physics/ElectroMagnetic/detail/GaussianMixture.hpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,9 @@ struct GaussianMixture {
4242
double operator()(generator_t &generator, const Acts::MaterialSlab &slab,
4343
Particle &particle) const {
4444
/// Calculate the highland formula first
45-
double sigma = Acts::computeMultipleScatteringTheta0(
45+
const double sigma = Acts::computeMultipleScatteringTheta0(
4646
slab, particle.absolutePdg(), particle.mass(), particle.qOverP(),
4747
particle.absoluteCharge());
48-
double sigma2 = sigma * sigma;
4948

5049
// Gauss distribution, will be sampled with generator
5150
std::normal_distribution<double> gaussDist(0., 1.);
@@ -56,30 +55,31 @@ struct GaussianMixture {
5655
// d_0'
5756
// beta² = (p/E)² = p²/(p² + m²) = 1/(1 + (m/p)²)
5857
// 1/beta² = 1 + (m/p)²
59-
double mOverP = particle.mass() / particle.absoluteMomentum();
60-
double beta2inv = 1 + mOverP * mOverP;
61-
double dprime = slab.thicknessInX0() * beta2inv;
62-
double log_dprime = std::log(dprime);
58+
const double mOverP = particle.mass() / particle.absoluteMomentum();
59+
const double beta2inv = 1 + mOverP * mOverP;
60+
const double dprime = slab.thicknessInX0() * beta2inv;
61+
const double log_dprime = std::log(dprime);
6362
// d_0''
64-
double log_dprimeprime =
63+
const double log_dprimeprime =
6564
std::log(std::pow(slab.material().Z(), 2.0 / 3.0) * dprime);
6665

6766
// get epsilon
68-
double epsilon =
67+
const double epsilon =
6968
log_dprimeprime < 0.5
7069
? gausMixEpsilon_a0 + gausMixEpsilon_a1 * log_dprimeprime +
7170
gausMixEpsilon_a2 * log_dprimeprime * log_dprimeprime
7271
: gausMixEpsilon_b0 + gausMixEpsilon_b1 * log_dprimeprime +
7372
gausMixEpsilon_b2 * log_dprimeprime * log_dprimeprime;
7473

7574
// the standard sigma
76-
double sigma1square = gausMixSigma1_a0 + gausMixSigma1_a1 * log_dprime +
77-
gausMixSigma1_a2 * log_dprime * log_dprime;
75+
const double sigma1square = gausMixSigma1_a0 +
76+
gausMixSigma1_a1 * log_dprime +
77+
gausMixSigma1_a2 * log_dprime * log_dprime;
7878

79+
double sigma2 = Acts::square(sigma);
7980
// G4 optimised / native double Gaussian model
8081
if (optGaussianMixtureG4) {
81-
sigma2 = 225. * dprime /
82-
(particle.absoluteMomentum() * particle.absoluteMomentum());
82+
sigma2 = 225. * dprime / Acts::square(particle.absoluteMomentum());
8383
}
8484
// throw the random number core/tail
8585
if (uniformDist(generator) < epsilon) {

0 commit comments

Comments
 (0)