Geant4  10.02
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 // Alain Boudard, CEA-Saclay, France
28 // Joseph Cugnon, University of Liege, Belgium
29 // Jean-Christophe David, CEA-Saclay, France
30 // Pekka Kaitaniemi, CEA-Saclay, France, and Helsinki Institute of Physics, Finland
31 // Sylvie Leray, CEA-Saclay, France
32 // Davide Mancusi, CEA-Saclay, France
33 //
34 #define INCLXX_IN_GEANT4_MODE 1
35 
36 #include "globals.hh"
37 
38 /*
39  * G4INCLParticleSpecies.cc
40  *
41  * \date Nov 25, 2011
42  * \author Davide Mancusi
43  */
44 
45 #include "G4INCLParticleSpecies.hh"
46 #include "G4INCLParticleTable.hh"
47 #include <algorithm>
48 #include <cctype>
49 #include <sstream>
50 #include <algorithm>
51 
52 namespace G4INCL {
53 
54  ParticleSpecies::ParticleSpecies(std::string const &pS) {
55  // Normalise the string to lower case
56  if(pS=="p" || pS=="proton") {
57  theA = 1;
58  theZ = 1;
60  } else if(pS=="n" || pS=="neutron") {
61  theA = 1;
62  theZ = 0;
64  } else if(pS=="delta++" || pS=="deltaplusplus") {
65  theA = 1;
66  theZ = 2;
68  } else if(pS=="delta+" || pS=="deltaplus") {
69  theA = 1;
70  theZ = 1;
72  } else if(pS=="delta0" || pS=="deltazero") {
73  theA = 1;
74  theZ = 0;
76  } else if(pS=="delta-" || pS=="deltaminus") {
77  theA = 1;
78  theZ = -1;
80  } else if(pS=="pi+" || pS=="pion+" || pS=="piplus" || pS=="pionplus") {
81  theA = 0;
82  theZ = 1;
84  } else if(pS=="pi0" || pS=="pion0" || pS=="pizero" || pS=="pionzero") {
85  theA = 0;
86  theZ = 0;
88  } else if(pS=="pi-" || pS=="pion-" || pS=="piminus" || pS=="pionminus") {
89  theA = 0;
90  theZ = -1;
92  } else if(pS=="d" || pS=="deuteron") {
93  theA = 2;
94  theZ = 1;
96  } else if(pS=="t" || pS=="triton") {
97  theA = 3;
98  theZ = 1;
100  } else if(pS=="a" || pS=="alpha") {
101  theA = 4;
102  theZ = 2;
104  } else
105  parseNuclide(pS);
106  }
107 
109  theType(t),
110  theA(ParticleTable::getMassNumber(theType)),
111  theZ(ParticleTable::getChargeNumber(theType))
112  {}
113 
115  theType(Composite),
116  theA(A),
117  theZ(Z)
118  {}
119 
120  void ParticleSpecies::parseNuclide(std::string const &pS) {
121  theType = Composite;
122 
123  // Allowed characters
124  const std::string separators("-_");
125  std::string allowed("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
126  allowed += separators;
127 
128  // There must be at least one character
129  if(pS.find_first_not_of(allowed)!=std::string::npos) {
130  // Malformed input string
131  // Setting unknown particle species
132  (*this) = ParticleSpecies(UnknownParticle);
133  return;
134  }
135  if(pS.size()<1) {
136  // Malformed input string
137  // Setting unknown particle species
138  (*this) = ParticleSpecies(UnknownParticle);
139  return;
140  }
141 
142  std::size_t firstSeparator = pS.find_first_of(separators);
143  std::size_t lastSeparator = pS.find_last_of(separators);
144  if(firstSeparator!=std::string::npos && firstSeparator!=lastSeparator) {
145  // Several separators in malformed input string
146  // Setting unknown particle species
147  (*this) = ParticleSpecies(UnknownParticle);
148  return;
149  }
150 
151  // Identify the type of the first character
152  G4int (*predicate)(G4int);
153  G4bool startsWithAlpha = std::isalpha(pS.at(0));
154  if(startsWithAlpha) {
155  predicate=std::isdigit;
156  } else if(std::isdigit(pS.at(0))) {
157  predicate=std::isalpha;
158  } else {
159  // Non-alphanumeric character in string
160  // Setting unknown particle species
161  (*this) = ParticleSpecies(UnknownParticle);
162  return;
163  }
164 
165  G4bool hasIsotope = true;
166  size_t endFirstSection, beginSecondSection;
167  if(firstSeparator==std::string::npos) {
168  // No separator, Fe56 or 56Fe style
169  // Identify the end of the first section
170 
171  // Find the first character that is not of the same type as the first one
172  beginSecondSection = std::find_if(pS.begin()+1, pS.end(), predicate) - pS.begin();
173 
174  if(beginSecondSection>=pS.size()) {
175  if(startsWithAlpha) {
176  // Only alphabetic characters are present -- must be an element name
177  hasIsotope = false;
178  } else {
179  // Only numeric characters in the string
180  // Setting unknown particle species
181  (*this) = ParticleSpecies(UnknownParticle);
182  return;
183  }
184  }
185 
186  endFirstSection = beginSecondSection;
187 
188  } else {
189  // One separator, Fe-56 or 56-Fe style
190  endFirstSection = firstSeparator;
191  beginSecondSection = firstSeparator+1;
192  }
193 
194  std::string firstSection(pS.substr(0,endFirstSection));
195  std::string secondSection(pS.substr(beginSecondSection,std::string::npos));
196  std::stringstream parsingStream;
197 
198  // Parse the sections
199  G4bool success;
200  if(startsWithAlpha) {
201  parsingStream.str(secondSection);
202  success = parseElement(firstSection);
203  } else {
204  parsingStream.str(firstSection);
205  success = parseElement(secondSection);
206  }
207  if(!success) {
208  // Couldn't parse the element section
209  // Setting unknown particle species
210  (*this) = ParticleSpecies(UnknownParticle);
211  return;
212  }
213 
214  if(hasIsotope) {
215  parsingStream >> theA;
216  if(parsingStream.fail()) {
217  // Couldn't parse the mass section
218  // Setting unknown particle species
219  (*this) = ParticleSpecies(UnknownParticle);
220  return;
221  }
222  } else
223  theA = 0;
224 
225  // Check that Z<=A
226  if(theZ>theA && hasIsotope) {
227  // Setting unknown particle species
228  (*this) = ParticleSpecies(UnknownParticle);
229  return;
230  }
231 
232  // Special particle type for protons
233  if(theZ==1 && theA==1)
234  theType = Proton;
235  }
236 
237  G4bool ParticleSpecies::parseElement(std::string const &s) {
239 
240  if(theZ<0)
242 
243  if(theZ<0)
244  return false;
245  else
246  return true;
247  }
248 
251  if(theZ==0)
252  return false;
253  else
254  return true;
255  }
256 }
257 
G4bool parseIUPACElement(std::string const &s)
Parse a IUPAC element name.
void parseNuclide(std::string const &pS)
Parse a nuclide name.
G4int getChargeNumber(const ParticleType t)
Get charge number from particle type.
int G4int
Definition: G4Types.hh:78
static const double s
Definition: G4SIunits.hh:168
G4int getMassNumber(const ParticleType t)
Get mass number from particle type.
double A(double temperature)
G4int parseElement(std::string pS)
Get the name of the element from the atomic number.
bool G4bool
Definition: G4Types.hh:79
ParticleSpecies()
Convert a string to a particle species.
G4bool parseElement(std::string const &pS)
Parse an element name.
G4int parseIUPACElement(std::string const &pS)
Parse a IUPAC element name.