GGEMS  1.1
GPU GEant4-based Monte Carlo Simulations
GGEMSTextReader.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 
32 #include <string>
33 
36 
40 
41 std::string GGEMSMaterialReader::ReadMaterialName(std::string const& line)
42 {
43  return line.substr(line.find_first_not_of("\t "), line.find(":"));
44 }
45 
49 
51 {
52  // Get the position of the first and last number of density
53  GGsize first_pos = line.find_first_of("0123456789", line.find("d="));
54  GGsize last_pos = line.find_first_not_of("0123456789.", first_pos);
55  std::string density_str = line.substr(first_pos, last_pos != std::string::npos ? last_pos - first_pos : last_pos);
56 
57  // Convert string to float
58  GGfloat density = 0.0f;
59  std::stringstream(density_str) >> density;
60 
61  // Check units of density and convert
62  first_pos = last_pos;
63  last_pos = line.find_first_of(";");
64  std::string unit_str = line.substr(first_pos, last_pos != std::string::npos ? last_pos - first_pos : last_pos);
65 
66  if (unit_str == "g/cm3") {
67  density *= g/cm3;
68  }
69  else if (unit_str == "mg/cm3") {
70  density *= mg/cm3;
71  }
72  else {
73  GGEMSMisc::ThrowException("GGEMSMaterialReader", "ReadMaterialDensity", "Unknown density unit in material database file!!!");
74  }
75 
76  return density;
77 }
78 
82 
84 {
85  // Get the position of the first and last number of density
86  GGsize first_pos = line.find_first_of("0123456789", line.find("n="));
87  GGsize last_pos = line.find_last_of(";");
88  std::string element_str = line.substr(first_pos,last_pos != std::string::npos ? last_pos - first_pos : last_pos);
89 
90  // Convert string to unsigned char
91  GGsize number_elements = 0;
92  std::stringstream(element_str) >> number_elements;
93 
94  return number_elements;
95 }
96 
100 
101 std::string GGEMSMaterialReader::ReadMaterialElementName(std::string const& line)
102 {
103  GGsize first_pos = line.find("name=")+5;
104  GGsize last_pos = line.find_first_of(";");
105  std::string element_name_str = line.substr(first_pos, last_pos != std::string::npos ? last_pos - first_pos : last_pos);
106 
107  return element_name_str;
108 }
109 
113 
115 {
116  GGsize first_pos = line.find_first_of("0123456789", line.find("f="));
117  GGsize last_pos = line.find_last_of(";");
118  std::string fraction_str = line.substr(first_pos, last_pos != std::string::npos ? last_pos - first_pos : last_pos);
119 
120  // Convert string to float
121  GGfloat fraction = 0.0f;
122  std::stringstream(fraction_str) >> fraction;
123 
124  return fraction;
125 }
126 
130 
131 void GGEMSTextReader::SkipComment(std::ifstream& stream, std::string& line, char const comment)
132 {
133  // If first caracter = comment -> it's a comment and get the next line
134  while (1) {
135  if (line[line.find_first_not_of("\t ")] == comment) {
136  std::getline(stream, line);
137  }
138  else break;
139  }
140 }
141 
145 
146 bool GGEMSTextReader::IsBlankLine(std::string const& line)
147 {
148  if (line.find_first_not_of("\t\n ") == std::string::npos) return true;
149  else return false;
150 }
151 
155 
156 void GGEMSTextReader::RemoveSpace(std::string& line)
157 {
158  // Erasing tab
159  line.erase(std::remove(line.begin(), line.end(), '\t'), line.end());
160  // Erasing space
161  line.erase(std::remove(line.begin(), line.end(), ' '), line.end());
162  // Erasing eof
163  line.erase(std::remove(line.begin(), line.end(), '\n'), line.end());
164  // Erasing carriage return
165  line.erase(std::remove(line.begin(), line.end(), '\r'), line.end());
166  // Erasing form feed
167  line.erase(std::remove(line.begin(), line.end(), '\f'), line.end());
168  // Erasing vertical tab
169  line.erase(std::remove(line.begin(), line.end(), '\v'), line.end());
170 }
171 
175 
176 std::string GGEMSMHDReader::ReadKey(std::string& line)
177 {
178  // Getting the key
179  std::string key = line.substr(line.find_first_not_of("\t "), line.find('=')-1);
180 
181  // Remove space/tab
183 
184  // Return the key
185  return key;
186 }
187 
191 
192 std::istringstream GGEMSMHDReader::ReadValue(std::string& line)
193 {
194  std::istringstream iss(line.substr(line.find("=")+1), std::istringstream::in);
195  return iss;
196 }
197 
201 
202 std::istringstream GGEMSRangeReader::ReadRangeMaterial(std::string& line)
203 {
204  std::istringstream iss(line, std::istringstream::in);
205  return iss;
206 }
GGEMSTextReader::SkipComment
void SkipComment(std::ifstream &stream, std::string &line, char const comment='#')
skip a special line beginning by a comment caracter
Definition: GGEMSTextReader.cc:131
GGEMSMaterialReader::ReadMaterialNumberOfElements
GGsize ReadMaterialNumberOfElements(std::string const &line)
return the number of elements in material
Definition: GGEMSTextReader.cc:83
GGEMSMaterialReader::ReadMaterialName
std::string ReadMaterialName(std::string const &line)
return the name of the material
Definition: GGEMSTextReader.cc:41
GGsize
#define GGsize
Definition: GGEMSTypes.hh:252
GGEMSMaterialReader::ReadMaterialElementName
std::string ReadMaterialElementName(std::string const &line)
return the name of material element name
Definition: GGEMSTextReader.cc:101
GGEMSRangeReader::ReadRangeMaterial
std::istringstream ReadRangeMaterial(std::string &line)
get string stream of value for material range
Definition: GGEMSTextReader.cc:202
GGEMSMaterialReader::ReadMaterialElementFraction
GGfloat ReadMaterialElementFraction(std::string const &line)
return the number of element fraction
Definition: GGEMSTextReader.cc:114
cm3
__constant GGfloat cm3
Definition: GGEMSSystemOfUnits.hh:59
GGEMSTextReader.hh
Namespaces for different useful fonctions reading input text file. Namespaces for material database f...
GGEMSTextReader::RemoveSpace
void RemoveSpace(std::string &line)
remove all spaces and tab from a string
Definition: GGEMSTextReader.cc:156
GGEMSTextReader::IsBlankLine
bool IsBlankLine(std::string const &line)
check if the line is blank or not
Definition: GGEMSTextReader.cc:146
GGEMSMaterialReader::ReadMaterialDensity
GGfloat ReadMaterialDensity(std::string const &line)
return the density of material
Definition: GGEMSTextReader.cc:50
GGEMSSystemOfUnits.hh
Namespace storing all the usefull physical units.
mg
__constant GGfloat mg
Definition: GGEMSSystemOfUnits.hh:112
g
__constant GGfloat g
Definition: GGEMSSystemOfUnits.hh:111
GGEMSMHDReader::ReadKey
std::string ReadKey(std::string &line)
get the key of MHD header
Definition: GGEMSTextReader.cc:176
GGEMSMHDReader::ReadValue
std::istringstream ReadValue(std::string &line)
get string stream of value
Definition: GGEMSTextReader.cc:192
GGEMSMisc::ThrowException
void ThrowException(std::string const &class_name, std::string const &method_name, std::string const &message)
Throw a C++ exception.
Definition: GGEMSTools.cc:61
GGfloat
#define GGfloat
Definition: GGEMSTypes.hh:273