Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-26 09:24:36

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #pragma once
0010 
0011 #include "Acts/Definitions/PdgParticle.hpp"
0012 #include "Acts/Definitions/Units.hpp"
0013 #include "Acts/Material/Interactions.hpp"
0014 #include "ActsFatras/EventData/Particle.hpp"
0015 
0016 #include <numbers>
0017 #include <random>
0018 
0019 namespace ActsFatras::detail {
0020 
0021 /// Generate scattering angles using a general mixture model.
0022 ///
0023 /// Emulates core and tail scattering as described in
0024 ///
0025 ///     General mixture model Fruehwirth, M. Liendl.
0026 ///     Comp. Phys. Comm. 141 (2001) 230-246
0027 ///
0028 struct GeneralMixture {
0029   /// Steering parameter
0030   bool logInclude = true;
0031   /// Scale the mixture level
0032   double genMixtureScalor = 1.;
0033 
0034   /// Generate a single 3D scattering angle.
0035   ///
0036   /// @param[in]     generator is the random number generator
0037   /// @param[in]     slab      defines the passed material
0038   /// @param[in,out] particle  is the particle being scattered
0039   /// @return a 3d scattering angle
0040   ///
0041   /// @tparam generator_t is a RandomNumberEngine
0042   template <typename generator_t>
0043   double operator()(generator_t &generator, const Acts::MaterialSlab &slab,
0044                     Particle &particle) const {
0045     double theta = 0.0;
0046 
0047     if (particle.absolutePdg() != Acts::PdgParticle::eElectron) {
0048       //----------------------------------------------------------------------------
0049       // see Mixture models of multiple scattering: computation and simulation.
0050       // -
0051       // R.Fruehwirth, M. Liendl. -
0052       // Computer Physics Communications 141 (2001) 230–246
0053       //----------------------------------------------------------------------------
0054 
0055       // Decide which mixture is best
0056       //   beta² = (p/E)² = p²/(p² + m²) = 1/(1 + (m/p)²)
0057       // 1/beta² = 1 + (m/p)²
0058       //    beta = 1/sqrt(1 + (m/p)²)
0059       const double mOverP = particle.mass() / particle.absoluteMomentum();
0060       const double beta2Inv = 1 + mOverP * mOverP;
0061       const double beta = 1 / std::sqrt(beta2Inv);
0062       const double tInX0 = slab.thicknessInX0();
0063       const double tob2 = tInX0 * beta2Inv;
0064       if (tob2 > 0.6 / std::pow(slab.material().Z(), 0.6)) {
0065         std::array<double, 4> scatteringParams{};
0066         // Gaussian mixture or pure Gaussian
0067         if (tob2 > 10) {
0068           scatteringParams = createGaussian(beta, particle.absoluteMomentum(),
0069                                             tInX0, genMixtureScalor);
0070         } else {
0071           scatteringParams =
0072               createGaussmix(beta, particle.absoluteMomentum(), tInX0,
0073                              slab.material().Z(), genMixtureScalor);
0074         }
0075         // Simulate
0076         theta = gaussmix(generator, scatteringParams);
0077       } else {
0078         // Semigaussian mixture - get parameters
0079         const std::array<double, 6> scatteringParamsSg =
0080             createSemigauss(beta, particle.absoluteMomentum(), tInX0,
0081                             slab.material().Z(), genMixtureScalor);
0082         // Simulate
0083         theta = semigauss(generator, scatteringParamsSg);
0084       }
0085     } else {
0086       // for electrons we fall back to the Highland (extension)
0087       // return projection factor times sigma times gauss random
0088       const auto theta0 = Acts::computeMultipleScatteringTheta0(
0089           slab, particle.absolutePdg(), particle.mass(), particle.qOverP(),
0090           particle.absoluteCharge());
0091       // scale from planar to 3d angle
0092       theta = std::numbers::sqrt2 *
0093               std::normal_distribution<double>(0.0, theta0)(generator);
0094     }
0095     // the mixture samplers already return the 3d angle
0096     return theta;
0097   }
0098 
0099   // helper methods for getting parameters and simulating
0100 
0101   std::array<double, 4> createGaussian(double beta, double p, double tInX0,
0102                                        double scale) const {
0103     std::array<double, 4> scatteringParams{};
0104     // Total standard deviation of mixture
0105     scatteringParams[0] =
0106         15. * Acts::UnitConstants::MeV / beta / p * std::sqrt(tInX0) * scale;
0107     // Variance of core
0108     scatteringParams[1] = 1.0;
0109     // Variance of tails
0110     scatteringParams[2] = 1.0;
0111     // Mixture weight of tail component
0112     scatteringParams[3] = 0.5;
0113     return scatteringParams;
0114   }
0115 
0116   std::array<double, 4> createGaussmix(double beta, double p, double tInX0,
0117                                        double Z, double scale) const {
0118     std::array<double, 4> scatteringParams{};
0119     // Total standard deviation of mixture
0120     scatteringParams[0] =
0121         15. * Acts::UnitConstants::MeV / beta / p * std::sqrt(tInX0) * scale;
0122     const double d1 = std::log(tInX0 / (beta * beta));
0123     const double d2 = std::log(std::pow(Z, 2.0 / 3.0) * tInX0 / (beta * beta));
0124     const double var1 = (-1.843e-3 * d1 + 3.347e-2) * d1 + 8.471e-1;
0125     double epsi = 0;
0126     if (d2 < 0.5) {
0127       epsi = (6.096e-4 * d2 + 6.348e-3) * d2 + 4.841e-2;
0128     } else {
0129       epsi = (-5.729e-3 * d2 + 1.106e-1) * d2 - 1.908e-2;
0130     }
0131     // Variance of core
0132     scatteringParams[1] = var1;
0133     // Variance of tails
0134     scatteringParams[2] = (1 - (1 - epsi) * var1) / epsi;
0135     // Mixture weight of tail component
0136     scatteringParams[3] = epsi;
0137     return scatteringParams;
0138   }
0139 
0140   std::array<double, 6> createSemigauss(double beta, double p, double tInX0,
0141                                         double Z, double scale) const {
0142     std::array<double, 6> scatteringParams{};
0143     const double N = tInX0 * 1.587E7 * std::pow(Z, 1.0 / 3.0) / (beta * beta) /
0144                      (Z + 1) / std::log(287 / std::sqrt(Z));
0145     // Total standard deviation of mixture
0146     scatteringParams[4] =
0147         15. * Acts::UnitConstants::MeV / beta / p * std::sqrt(tInX0) * scale;
0148     const double rho = 41000 / std::pow(Z, 2.0 / 3.0);
0149     const double b = rho / std::sqrt(N * (std::log(rho) - 0.5));
0150     const double n = std::pow(Z, 0.1) * std::log(N);
0151     const double var1 = (5.783E-4 * n + 3.803E-2) * n + 1.827E-1;
0152     const double a =
0153         (((-4.590E-5 * n + 1.330E-3) * n - 1.355E-2) * n + 9.828E-2) * n +
0154         2.822E-1;
0155     const double epsi = (1 - var1) / (a * a * (std::log(b / a) - 0.5) - var1);
0156     // Mixture weight of tail component
0157     scatteringParams[3] = (epsi > 0) ? epsi : 0.0;
0158     // Parameter 1 of tails
0159     scatteringParams[0] = a;
0160     // Parameter 2 of tails
0161     scatteringParams[1] = b;
0162     // Variance of core
0163     scatteringParams[2] = var1;
0164     // Average number of scattering processes
0165     scatteringParams[5] = N;
0166     return scatteringParams;
0167   }
0168 
0169   /// @brief Retrieve the gaussian mixture
0170   ///
0171   /// @tparam generator_t Type of the generator
0172   ///
0173   /// @param udist The uniform distribution handed over by the call operator
0174   /// @param scattering_params the tuned parameters for the generation
0175   ///
0176   /// @return a double value that represents the gaussian mixture
0177   template <typename generator_t>
0178   double gaussmix(generator_t &generator,
0179                   const std::array<double, 4> &scatteringParams) const {
0180     std::uniform_real_distribution<double> udist(0.0, 1.0);
0181     const double sigmaTot = scatteringParams[0];
0182     const double var1 = scatteringParams[1];
0183     const double var2 = scatteringParams[2];
0184     const double epsi = scatteringParams[3];
0185     const bool ind = udist(generator) > epsi;
0186     const double u = udist(generator);
0187     if (ind) {
0188       return std::sqrt(var1) * std::sqrt(-2 * std::log(u)) * sigmaTot;
0189     } else {
0190       return std::sqrt(var2) * std::sqrt(-2 * std::log(u)) * sigmaTot;
0191     }
0192   }
0193 
0194   /// @brief Retrieve the semi-gaussian mixture
0195   ///
0196   /// @tparam generator_t Type of the generator
0197   ///
0198   /// @param udist The uniform distribution handed over by the call operator
0199   /// @param scattering_params the tuned parameters for the generation
0200   ///
0201   /// @return a double value that represents the gaussian mixture
0202   template <typename generator_t>
0203   double semigauss(generator_t &generator,
0204                    const std::array<double, 6> &scattering_params) const {
0205     std::uniform_real_distribution<double> udist(0.0, 1.0);
0206     const double a = scattering_params[0];
0207     const double b = scattering_params[1];
0208     const double var1 = scattering_params[2];
0209     const double epsi = scattering_params[3];
0210     const double sigmaTot = scattering_params[4];
0211     const bool ind = udist(generator) > epsi;
0212     const double u = udist(generator);
0213     if (ind) {
0214       return std::sqrt(var1) * std::sqrt(-2 * std::log(u)) * sigmaTot;
0215     } else {
0216       return a * b * std::sqrt((1 - u) / (u * b * b + a * a)) * sigmaTot;
0217     }
0218   }
0219 };
0220 
0221 }  // namespace ActsFatras::detail