Geant4 9.6.0
Toolkit for the simulation of the passage of particles through matter
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
G4INCLParticleSpecies.cc
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26// INCL++ intra-nuclear cascade model
27// Pekka Kaitaniemi, CEA and Helsinki Institute of Physics
28// Davide Mancusi, CEA
29// Alain Boudard, CEA
30// Sylvie Leray, CEA
31// Joseph Cugnon, University of Liege
32//
33// INCL++ revision: v5.1.8
34//
35#define INCLXX_IN_GEANT4_MODE 1
36
37#include "globals.hh"
38
39/*
40 * G4INCLParticleSpecies.cc
41 *
42 * \date Nov 25, 2011
43 * \author Davide Mancusi
44 */
45
48#include <algorithm>
49#include <cctype>
50#include <sstream>
51#include <algorithm>
52
53namespace G4INCL {
54
55 ParticleSpecies::ParticleSpecies(std::string const &pS) {
56 // Normalise the string to lower case
57 std::string pSNorm = pS;
58 std::transform(pSNorm.begin(), pSNorm.end(), pSNorm.begin(), ::tolower);
59 if(pSNorm=="p" || pSNorm=="proton") {
60 theA = 1;
61 theZ = 1;
63 } else if(pSNorm=="n" || pSNorm=="neutron") {
64 theA = 1;
65 theZ = 0;
67 } else if(pSNorm=="delta++" || pSNorm=="deltaplusplus") {
68 theA = 1;
69 theZ = 2;
71 } else if(pSNorm=="delta+" || pSNorm=="deltaplus") {
72 theA = 1;
73 theZ = 1;
75 } else if(pSNorm=="delta0" || pSNorm=="deltazero") {
76 theA = 1;
77 theZ = 0;
79 } else if(pSNorm=="delta-" || pSNorm=="deltaminus") {
80 theA = 1;
81 theZ = -1;
83 } else if(pSNorm=="pi+" || pSNorm=="pion+" || pSNorm=="piplus" || pSNorm=="pionplus") {
84 theA = 0;
85 theZ = 1;
87 } else if(pSNorm=="pi0" || pSNorm=="pion0" || pSNorm=="pizero" || pSNorm=="pionzero") {
88 theA = 0;
89 theZ = 0;
91 } else if(pSNorm=="pi-" || pSNorm=="pion-" || pSNorm=="piminus" || pSNorm=="pionminus") {
92 theA = 0;
93 theZ = -1;
95 } else if(pSNorm=="d" || pSNorm=="deuteron") {
96 theA = 2;
97 theZ = 1;
99 } else if(pSNorm=="t" || pSNorm=="triton") {
100 theA = 3;
101 theZ = 1;
103 } else if(pSNorm=="a" || pSNorm=="alpha") {
104 theA = 4;
105 theZ = 2;
107 } else
108 parseNuclide(pSNorm);
109 }
110
112 theType(t),
113 theA(ParticleTable::getMassNumber(theType)),
114 theZ(ParticleTable::getChargeNumber(theType))
115 {}
116
118 theType(Composite),
119 theA(A),
120 theZ(Z)
121 {}
122
123 void ParticleSpecies::parseNuclide(std::string const &pS) {
125
126 // Allowed characters
127 const std::string separators("-_");
128 std::string allowed("0123456789abcdefghijklmnopqrstuvwxyz");
129 allowed += separators;
130
131 // There must be at least one character
132 if(pS.find_first_not_of(allowed)!=std::string::npos) {
133 // Malformed input string
134 // Setting unknown particle species
136 return;
137 }
138 if(pS.size()<1) {
139 // Malformed input string
140 // Setting unknown particle species
142 return;
143 }
144
145 std::size_t firstSeparator = pS.find_first_of(separators);
146 std::size_t lastSeparator = pS.find_last_of(separators);
147 if(firstSeparator!=std::string::npos && firstSeparator!=lastSeparator) {
148 // Several separators in malformed input string
149 // Setting unknown particle species
151 return;
152 }
153
154 // Identify the type of the first character
155 G4int (*predicate)(G4int);
156 G4bool startsWithAlpha = std::isalpha(pS.at(0));
157 if(startsWithAlpha) {
158 predicate=std::isdigit;
159 } else if(std::isdigit(pS.at(0))) {
160 predicate=std::isalpha;
161 } else {
162 // Non-alphanumeric character in string
163 // Setting unknown particle species
165 return;
166 }
167
168 G4bool hasIsotope = true;
169 size_t endFirstSection, beginSecondSection;
170 if(firstSeparator==std::string::npos) {
171 // No separator, Fe56 or 56Fe style
172 // Identify the end of the first section
173
174 // Find the first character that is not of the same type as the first one
175 beginSecondSection = std::find_if(pS.begin()+1, pS.end(), predicate) - pS.begin();
176
177 if(beginSecondSection>=pS.size()) {
178 if(startsWithAlpha) {
179 // Only alphabetic characters are present -- must be an element name
180 hasIsotope = false;
181 } else {
182 // Only numeric characters in the string
183 // Setting unknown particle species
185 return;
186 }
187 }
188
189 endFirstSection = beginSecondSection;
190
191 } else {
192 // One separator, Fe-56 or 56-Fe style
193 endFirstSection = firstSeparator;
194 beginSecondSection = firstSeparator+1;
195 }
196
197 std::string firstSection(pS.substr(0,endFirstSection));
198 std::string secondSection(pS.substr(beginSecondSection,std::string::npos));
199 std::stringstream parsingStream;
200
201 // Parse the sections
202 G4bool success;
203 if(startsWithAlpha) {
204 parsingStream.str(secondSection);
205 success = parseElement(firstSection);
206 } else {
207 parsingStream.str(firstSection);
208 success = parseElement(secondSection);
209 }
210 if(!success) {
211 // Couldn't parse the element section
212 // Setting unknown particle species
214 return;
215 }
216
217 if(hasIsotope) {
218 parsingStream >> theA;
219 if(parsingStream.fail()) {
220 // Couldn't parse the mass section
221 // Setting unknown particle species
223 return;
224 }
225 } else
226 theA = 0;
227
228 // Check that Z<=A
229 if(theZ>theA && hasIsotope) {
230 // Setting unknown particle species
232 return;
233 }
234
235 // Special particle type for protons
236 if(theZ==1 && theA==1)
237 theType = Proton;
238 }
239
240 G4bool ParticleSpecies::parseElement(std::string const &s) {
242 std::string elementName = ParticleTable::getElementName(theZ);
243 // Normalize the element name
244 std::transform(elementName.begin(), elementName.end(), elementName.begin(), ::tolower);
245 if(s.compare(elementName)==0)
246 return true;
247 }
248 return parseIUPACElement(s);
249 }
250
251 G4bool ParticleSpecies::parseIUPACElement(std::string const &s) {
253 if(theZ==0)
254 return false;
255 else
256 return true;
257 }
258}
259
@ allowed
int G4int
Definition: G4Types.hh:66
bool G4bool
Definition: G4Types.hh:67
ParticleSpecies()
Convert a string to a particle species.
static G4int parseIUPACElement(std::string const &pS)
Parse a IUPAC element name.
static const G4int elementTableSize
static std::string getElementName(const G4int Z)
Get the name of the element from the atomic number.