|
| 1 | +// Copyright 2019-2020 CERN and copyright holders of ALICE O2. |
| 2 | +// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders. |
| 3 | +// All rights not expressly granted are reserved. |
| 4 | +// |
| 5 | +// This software is distributed under the terms of the GNU General Public |
| 6 | +// License v3 (GPL Version 3), copied verbatim in the file "COPYING". |
| 7 | +// |
| 8 | +// In applying this license CERN does not waive the privileges and immunities |
| 9 | +// granted to it by virtue of its status as an Intergovernmental Organization |
| 10 | +// or submit itself to any jurisdiction. |
| 11 | + |
| 12 | +// Writes the TRD gas collision tables (C++) from Geant4's PAI cross-section: number of collisions per cm with an |
| 13 | +// energy transfer below the delta-ray cut, restricted dE/dx, and N(>E)/N on a log energy grid, per beta*gamma. |
| 14 | +// Standalone program, not built with O2. Build: g++ -O2 $(geant4-config --cflags) makeIonisationTables.cxx -o makeIonisationTables $(geant4-config --libs) |
| 15 | +#include "G4Element.hh" |
| 16 | +#include "G4Material.hh" |
| 17 | +#include "G4PAIxSection.hh" |
| 18 | +#include "G4SandiaTable.hh" |
| 19 | +#include "G4SystemOfUnits.hh" |
| 20 | +#include "G4Version.hh" |
| 21 | +#include <cmath> |
| 22 | +#include <cstdio> |
| 23 | +#include <string> |
| 24 | +#include <vector> |
| 25 | + |
| 26 | +constexpr int kNBetaGamma = 31; |
| 27 | +constexpr double kLogBetaGammaMin = -1.4; |
| 28 | +constexpr double kLogBetaGammaStep = 0.2; |
| 29 | + |
| 30 | +struct Gas { |
| 31 | + std::string name; |
| 32 | + double z, a, nNoble, nC, nO, fNoble, dNoble; |
| 33 | +}; |
| 34 | + |
| 35 | +static void table(const Gas& gas, double cut) |
| 36 | +{ |
| 37 | + const double dco = 0.00186; // CO2 at 20 C, as in Detector::createMaterials |
| 38 | + const double dens = (gas.fNoble * gas.dNoble + (1. - gas.fNoble) * dco) * g / cm3; |
| 39 | + auto* el = new G4Element(gas.name, gas.name, gas.z, gas.a * g / mole); |
| 40 | + auto* c = new G4Element("C_" + gas.name, "C", 6., 12.0107 * g / mole); |
| 41 | + auto* o = new G4Element("O_" + gas.name, "O", 8., 15.9994 * g / mole); |
| 42 | + const double w[3] = {gas.nNoble * gas.a, gas.nC * 12.0107, gas.nO * 15.9994}; |
| 43 | + const double ws = w[0] + w[1] + w[2]; |
| 44 | + auto* mat = new G4Material("TRD_" + gas.name + "CO2", dens, 3, kStateGas, 293.15 * kelvin); |
| 45 | + mat->AddElement(el, w[0] / ws); |
| 46 | + mat->AddElement(c, w[1] / ws); |
| 47 | + mat->AddElement(o, w[2] / ws); |
| 48 | + |
| 49 | + std::vector<double> lbgs; |
| 50 | + for (int k = 0; k < kNBetaGamma; ++k) { |
| 51 | + lbgs.push_back(kLogBetaGammaMin + kLogBetaGammaStep * k); |
| 52 | + } |
| 53 | + std::vector<double> egrid; |
| 54 | + std::vector<std::vector<double>> ccdf; |
| 55 | + std::vector<double> nsub, dedx; |
| 56 | + for (double lbg : lbgs) { |
| 57 | + const double bg = std::pow(10., lbg); |
| 58 | + G4SandiaTable sandia; |
| 59 | + sandia.Initialize(mat); |
| 60 | + G4PAIxSection xs; |
| 61 | + xs.Initialize(mat, 1. * MeV, bg * bg, &sandia); |
| 62 | + const int n = xs.GetSplineSize(); |
| 63 | + std::vector<double> en(n), in(n), de(n); |
| 64 | + for (int i = 1; i <= n; ++i) { |
| 65 | + en[i - 1] = xs.GetSplineEnergy(i); |
| 66 | + in[i - 1] = xs.GetIntegralPAIxSection(i); |
| 67 | + de[i - 1] = xs.GetIntegralPAIdEdx(i); |
| 68 | + } |
| 69 | + auto interp = [&](const std::vector<double>& v, double e) { |
| 70 | + if (e <= en.front()) { |
| 71 | + return v.front(); |
| 72 | + } |
| 73 | + for (int i = 1; i < n; ++i) { |
| 74 | + if (en[i] >= e) { |
| 75 | + const double t = std::log(e / en[i - 1]) / std::log(en[i] / en[i - 1]); |
| 76 | + return v[i - 1] + t * (v[i] - v[i - 1]); |
| 77 | + } |
| 78 | + } |
| 79 | + return v.back(); |
| 80 | + }; |
| 81 | + if (egrid.empty()) { |
| 82 | + egrid.push_back(en[0] / eV); |
| 83 | + for (int j = 0; j <= 30; ++j) { |
| 84 | + const double e = std::pow(10., 1. + 0.1 * j); |
| 85 | + if (e > en[0] / eV * 1.001) { |
| 86 | + egrid.push_back(e); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + const double ns = (in[0] - interp(in, cut)) * 10.; // per cm |
| 91 | + nsub.push_back(ns); |
| 92 | + dedx.push_back((de[0] - interp(de, cut)) * 10. / eV); |
| 93 | + std::vector<double> row; |
| 94 | + for (double e : egrid) { |
| 95 | + row.push_back(std::max((interp(in, e * eV) - interp(in, cut)) * 10. / ns, 0.)); |
| 96 | + } |
| 97 | + row.front() = 1.; |
| 98 | + row.back() = 0.; |
| 99 | + ccdf.push_back(row); |
| 100 | + } |
| 101 | + std::printf("// %s/CO2: density %.6g g/cm3, mean excitation energy %.4g eV\n", gas.name.c_str(), dens / (g / cm3), |
| 102 | + mat->GetIonisation()->GetMeanExcitationEnergy() / eV); |
| 103 | + std::printf("constexpr double k%sEnergy[%zu] = {", gas.name.c_str(), egrid.size()); |
| 104 | + for (size_t j = 0; j < egrid.size(); ++j) { |
| 105 | + std::printf("%s%.5g", j ? ", " : "", egrid[j]); |
| 106 | + } |
| 107 | + std::printf("};\nconstexpr double k%sCollisionsPerCm[%zu] = {", gas.name.c_str(), nsub.size()); |
| 108 | + for (size_t k = 0; k < nsub.size(); ++k) { |
| 109 | + std::printf("%s%.6g", k ? ", " : "", nsub[k]); |
| 110 | + } |
| 111 | + std::printf("};\nconstexpr double k%sRestrictedDEdx[%zu] = {", gas.name.c_str(), dedx.size()); |
| 112 | + for (size_t k = 0; k < dedx.size(); ++k) { |
| 113 | + std::printf("%s%.6g", k ? ", " : "", dedx[k]); |
| 114 | + } |
| 115 | + std::printf("};\nconstexpr float k%sFractionAbove[%zu][%zu] = {\n", gas.name.c_str(), ccdf.size(), egrid.size()); |
| 116 | + for (auto& row : ccdf) { |
| 117 | + std::printf(" {"); |
| 118 | + for (size_t j = 0; j < row.size(); ++j) { |
| 119 | + std::printf("%s%.5g", j ? ", " : "", row[j]); |
| 120 | + } |
| 121 | + std::printf("},\n"); |
| 122 | + } |
| 123 | + std::printf("};\n"); |
| 124 | +} |
| 125 | + |
| 126 | +int main() |
| 127 | +{ |
| 128 | + const double cut = 10. * keV; |
| 129 | + std::printf("// Generated with Geant4 %d G4PAIxSection, delta-ray cut %.4g keV\n", G4VERSION_NUMBER, cut / keV); |
| 130 | + std::printf("constexpr double kDeltaRayCut = %.6g; // eV\n", cut / eV); |
| 131 | + std::printf("constexpr int kNBetaGamma = %d;\n", kNBetaGamma); |
| 132 | + std::printf("constexpr double kLogBetaGammaMin = %.6g; // beta*gamma of row k is 10^(min + k * step)\n", kLogBetaGammaMin); |
| 133 | + std::printf("constexpr double kLogBetaGammaStep = %.6g;\n", kLogBetaGammaStep); |
| 134 | + table({"Xe", 54., 131.29, 8.5, 1.5, 3.0, 0.85, 0.00549}, cut); |
| 135 | + table({"Ar", 18., 39.948, 8.2, 1.8, 3.6, 0.82, 0.00166}, cut); |
| 136 | + return 0; |
| 137 | +} |
0 commit comments