|
| 1 | +#include "SimConfig/FluenceWeightCalculator.h" |
| 2 | +#include <TFile.h> |
| 3 | +#include <fstream> |
| 4 | +#include <sstream> |
| 5 | +#include <iostream> |
| 6 | + |
| 7 | +std::unique_ptr<TGraph> FluenceWeightCalculator::neutronG; |
| 8 | +std::unique_ptr<TGraph> FluenceWeightCalculator::protonG; |
| 9 | +std::unique_ptr<TGraph> FluenceWeightCalculator::pionG; |
| 10 | + |
| 11 | +double FluenceWeightCalculator::GetWeight(const int pdg, const double kineticEnergy) |
| 12 | +{ |
| 13 | + // |
| 14 | + // Obtain weight for given particle and kinetic energy |
| 15 | + if (!neutronG || !protonG || !pionG) { |
| 16 | + std::cerr << "FluenceWeightCalculator not initialized\n"; |
| 17 | + return 0.; |
| 18 | + } |
| 19 | + switch (std::abs(pdg)) { |
| 20 | + case 2112: { |
| 21 | + return neutronG->Eval(kineticEnergy, 0, "S"); |
| 22 | + } |
| 23 | + case 2212: { |
| 24 | + return ((kineticEnergy > 1e-3) ? protonG->Eval(kineticEnergy, 0, "S") : 0.); |
| 25 | + } |
| 26 | + case 211: { |
| 27 | + return ((kineticEnergy > 10.) ? pionG->Eval(kineticEnergy, 0, "S") : 0.); |
| 28 | + } |
| 29 | + default: |
| 30 | + return 0.0; |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +void FluenceWeightCalculator::InitWeights(const std::string& filename) |
| 35 | +{ |
| 36 | + // |
| 37 | + // Read graphs from file |
| 38 | + TFile inFile(filename.c_str(), "READ"); |
| 39 | + if (inFile.IsZombie()) { |
| 40 | + std::cerr << "Cannot open " << filename << "\n"; |
| 41 | + return; |
| 42 | + } |
| 43 | + // |
| 44 | + TGraph* tmp = nullptr; |
| 45 | + inFile.GetObject("neutronDW", tmp); |
| 46 | + neutronG.reset(tmp ? static_cast<TGraph*>(tmp->Clone()) : nullptr); |
| 47 | + if (!neutronG) { |
| 48 | + std::cerr << "Missing graph neutronDW\n"; |
| 49 | + return; |
| 50 | + } |
| 51 | + neutronG->SetBit(TGraph::kIsSortedX); |
| 52 | + inFile.GetObject("protonDW", tmp); |
| 53 | + protonG.reset(tmp ? static_cast<TGraph*>(tmp->Clone()) : nullptr); |
| 54 | + if (!protonG) { |
| 55 | + std::cerr << "Missing graph protonDW\n"; |
| 56 | + return; |
| 57 | + } |
| 58 | + protonG->SetBit(TGraph::kIsSortedX); |
| 59 | + inFile.GetObject("pionDW", tmp); |
| 60 | + pionG.reset(tmp ? static_cast<TGraph*>(tmp->Clone()) : nullptr); |
| 61 | + if (!pionG) { |
| 62 | + std::cerr << "Missing graph pionDW\n"; |
| 63 | + return; |
| 64 | + } |
| 65 | + pionG->SetBit(TGraph::kIsSortedX); |
| 66 | +} |
| 67 | + |
| 68 | +void FluenceWeightCalculator::InitWeightsFromCSV(const std::string& filename) |
| 69 | +{ |
| 70 | + // |
| 71 | + // read the NIEL weights from input file and store them as TGraphs |
| 72 | + neutronG = std::make_unique<TGraph>(); |
| 73 | + neutronG->SetName("neutronDW"); |
| 74 | + auto neuN = 0; |
| 75 | + protonG = std::make_unique<TGraph>(); |
| 76 | + protonG->SetName("protonDW"); |
| 77 | + auto proN = 0; |
| 78 | + pionG = std::make_unique<TGraph>(); |
| 79 | + pionG->SetName("pionDW"); |
| 80 | + auto pioN = 0; |
| 81 | + |
| 82 | + std::ifstream in(filename); |
| 83 | + if (!in.is_open()) { |
| 84 | + std::cerr << "Error: cannot open file with damage weights.\n"; |
| 85 | + return; |
| 86 | + } |
| 87 | + std::string line; |
| 88 | + while (std::getline(in, line)) { |
| 89 | + if (line.empty() || line[0] == '#') |
| 90 | + continue; |
| 91 | + std::istringstream ss(line); |
| 92 | + std::string particle, e_str, w_str; |
| 93 | + if (!std::getline(ss, particle, ',')) |
| 94 | + continue; |
| 95 | + if (!std::getline(ss, e_str, ',')) |
| 96 | + continue; |
| 97 | + if (!std::getline(ss, w_str, ',')) |
| 98 | + continue; |
| 99 | + auto e = std::stod(e_str); |
| 100 | + auto w = std::stod(w_str); |
| 101 | + auto pdg = std::stoi(particle); |
| 102 | + switch (pdg) { |
| 103 | + case 2112: { |
| 104 | + neutronG->SetPoint(neuN++, e, w); |
| 105 | + break; |
| 106 | + } |
| 107 | + case 2212: { |
| 108 | + protonG->SetPoint(proN++, e, w); |
| 109 | + break; |
| 110 | + } |
| 111 | + case 211: { |
| 112 | + pionG->SetPoint(pioN++, e, w); |
| 113 | + break; |
| 114 | + } |
| 115 | + default:; |
| 116 | + } |
| 117 | + } |
| 118 | + auto fout = new TFile("rd50_niel.root", "recreate"); |
| 119 | + neutronG->Write(); |
| 120 | + protonG->Write(); |
| 121 | + pionG->Write(); |
| 122 | + fout->Close(); |
| 123 | +} |
0 commit comments