GGEMS  1.1
GPU GEant4-based Monte Carlo Simulations
GGEMSLogEnergyTable.cc
Go to the documentation of this file.
1 // ************************************************************************
2 // * This file is part of GGEMS. *
3 // * *
4 // * GGEMS is free software: you can redistribute it and/or modify *
5 // * it under the terms of the GNU General Public License as published by *
6 // * the Free Software Foundation, either version 3 of the License, or *
7 // * (at your option) any later version. *
8 // * *
9 // * GGEMS is distributed in the hope that it will be useful, *
10 // * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 // * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 // * GNU General Public License for more details. *
13 // * *
14 // * You should have received a copy of the GNU General Public License *
15 // * along with GGEMS. If not, see <https://www.gnu.org/licenses/>. *
16 // * *
17 // ************************************************************************
18 
34 
38 
39 GGEMSLogEnergyTable::GGEMSLogEnergyTable(GGfloat const& lowest_energy, GGfloat const& highest_energy, GGsize const& number_of_bins)
40 {
41  GGcout("GGEMSLogEnergyTable", "GGEMSLogEnergyTable", 3) << "GGEMSLogEnergyTable creating..." << GGendl;
42 
43  bin_width_ = logf(highest_energy / lowest_energy) / static_cast<GGfloat>(number_of_bins);
44  base_bin_ = logf(lowest_energy) / bin_width_;
45 
46  number_of_nodes_ = number_of_bins + 1;
47 
49  bins_.reserve(number_of_nodes_);
50 
51  // Begin of vector
52  loss_table_data_.push_back(0.0f);
53  bins_.push_back(lowest_energy);
54 
55  // Filling the vectors
56  for (GGsize i = 1; i < number_of_nodes_-1; ++i) {
57  loss_table_data_.push_back(0.0f);
58  bins_.push_back(expf((base_bin_+static_cast<GGfloat>(i))*bin_width_));
59  }
60 
61  // End of vector
62  loss_table_data_.push_back(0.0f);
63  bins_.push_back(highest_energy);
64 
65  // Storing edges
66  edge_min_ = bins_.front();
67  edge_max_ = bins_.back();
68 
69  GGcout("GGEMSLogEnergyTable", "GGEMSLogEnergyTable", 3) << "GGEMSLogEnergyTable created!!!" << GGendl;
70 }
71 
75 
77 {
78  GGcout("GGEMSLogEnergyTable", "~GGEMSLogEnergyTable", 3) << "GGEMSLogEnergyTable erasing..." << GGendl;
79 
80  GGcout("GGEMSLogEnergyTable", "~GGEMSLogEnergyTable", 3) << "GGEMSLogEnergyTable erased!!!" << GGendl;
81 }
82 
86 
87 void GGEMSLogEnergyTable::SetValue(GGsize const& index, GGfloat const& value)
88 {
89  loss_table_data_.at(index) = value;
90 }
91 
95 
97 {
98  GGsize last_index = 0;
99  GGfloat y = 0.0f;
100 
101  if (energy <= edge_min_) {
102  last_index = 0;
103  y = loss_table_data_.at(0);
104  }
105  else if (energy >= edge_max_) {
106  last_index = number_of_nodes_ - 1;
107  y = loss_table_data_.at(last_index);
108  }
109  else {
110  last_index = FindBin(energy, last_index);
112  bins_.at(last_index), loss_table_data_.at(last_index),
113  bins_.at(last_index+1), loss_table_data_.at(last_index+1),
114  energy
115  );
116  }
117 
118  return y;
119 }
120 
124 
126 {
127  GGsize bin = static_cast<GGsize>(log(energy) / bin_width_ - base_bin_);
128 
129  if (bin + 2 > number_of_nodes_) {
130  bin = number_of_nodes_ - 2;
131  }
132  else if (bin > 0 && energy < bins_.at(bin)) {
133  --bin;
134  }
135  else if (bin + 2 < number_of_nodes_ && energy > bins_.at(bin+1)) {
136  ++bin;
137  }
138 
139  return bin;
140 }
141 
145 
146 GGsize GGEMSLogEnergyTable::FindBin(GGfloat const& energy, GGsize const& index) const
147 {
148  GGsize id = index;
149 
150  if (energy < bins_.at(1)) {
151  id = 0;
152  }
153  else if (energy >= bins_.at(number_of_nodes_-2)) {
154  id = number_of_nodes_ - 2;
155  }
156  else if (index >= number_of_nodes_ || energy < bins_.at(index) || energy > bins_.at(index+1)) {
157  id = FindBinLocation(energy);
158  }
159 
160  return id;
161 }
GGEMSMathAlgorithms.hh
Definitions of miscellaneous mathematical functions.
GGEMSLogEnergyTable::loss_table_data_
std::vector< GGfloat > loss_table_data_
Definition: GGEMSLogEnergyTable.hh:157
LinearInterpolation
GGfloat LinearInterpolation(GGfloat const xa, GGfloat const ya, GGfloat const xb, GGfloat const yb, GGfloat const x)
interpolate the x value between point A and B
Definition: GGEMSMathAlgorithms.hh:86
GGEMSLogEnergyTable::bins_
std::vector< GGfloat > bins_
Definition: GGEMSLogEnergyTable.hh:158
GGsize
#define GGsize
Definition: GGEMSTypes.hh:252
GGEMSLogEnergyTable::GetLossTableValue
GGfloat GetLossTableValue(GGfloat const &energy) const
get the interpolated loss table value
Definition: GGEMSLogEnergyTable.cc:96
GGEMSLogEnergyTable::edge_min_
GGfloat edge_min_
Definition: GGEMSLogEnergyTable.hh:154
GGEMSLogEnergyTable::GGEMSLogEnergyTable
GGEMSLogEnergyTable(GGfloat const &lowest_energy, GGfloat const &highest_energy, GGsize const &number_of_bins)
GGEMSLogEnergyTable constructor.
Definition: GGEMSLogEnergyTable.cc:39
GGEMSLogEnergyTable::FindBinLocation
GGsize FindBinLocation(GGfloat const &energy) const
get the bin where is the energy
Definition: GGEMSLogEnergyTable.cc:125
GGEMSLogEnergyTable::base_bin_
GGfloat base_bin_
Definition: GGEMSLogEnergyTable.hh:160
GGEMSLogEnergyTable::edge_max_
GGfloat edge_max_
Definition: GGEMSLogEnergyTable.hh:155
GGcout
GGEMSStream GGcout
Definition: GGEMSPrint.cc:34
GGendl
#define GGendl
overload C++ std::endl
Definition: GGEMSPrint.hh:60
GGEMSLogEnergyTable::SetValue
void SetValue(GGsize const &index, GGfloat const &value)
Set the data vector at index.
Definition: GGEMSLogEnergyTable.cc:87
GGEMSPrint.hh
Print a custom std::cout end std::cerr handling verbosity.
GGEMSLogEnergyTable::FindBin
GGsize FindBin(GGfloat const &energy, GGsize const &index) const
get the bin where is the energy depending on a index
Definition: GGEMSLogEnergyTable.cc:146
GGEMSLogEnergyTable::number_of_nodes_
GGsize number_of_nodes_
Definition: GGEMSLogEnergyTable.hh:156
GGEMSLogEnergyTable::~GGEMSLogEnergyTable
~GGEMSLogEnergyTable(void)
GGEMSLogEnergyTable destructor.
Definition: GGEMSLogEnergyTable.cc:76
GGfloat
#define GGfloat
Definition: GGEMSTypes.hh:273
GGEMSLogEnergyTable::bin_width_
GGfloat bin_width_
Definition: GGEMSLogEnergyTable.hh:159
GGEMSLogEnergyTable.hh
GGEMS class computing log table for cut convertion from length to energy.