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