Geant4  10.01.p03
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  std::string pSNorm = pS;
57  std::transform(pSNorm.begin(), pSNorm.end(), pSNorm.begin(), ::tolower);
58  if(pSNorm=="p" || pSNorm=="proton") {
59  theA = 1;
60  theZ = 1;
62  } else if(pSNorm=="n" || pSNorm=="neutron") {
63  theA = 1;
64  theZ = 0;
66  } else if(pSNorm=="delta++" || pSNorm=="deltaplusplus") {
67  theA = 1;
68  theZ = 2;
70  } else if(pSNorm=="delta+" || pSNorm=="deltaplus") {
71  theA = 1;
72  theZ = 1;
74  } else if(pSNorm=="delta0" || pSNorm=="deltazero") {
75  theA = 1;
76  theZ = 0;
78  } else if(pSNorm=="delta-" || pSNorm=="deltaminus") {
79  theA = 1;
80  theZ = -1;
82  } else if(pSNorm=="pi+" || pSNorm=="pion+" || pSNorm=="piplus" || pSNorm=="pionplus") {
83  theA = 0;
84  theZ = 1;
86  } else if(pSNorm=="pi0" || pSNorm=="pion0" || pSNorm=="pizero" || pSNorm=="pionzero") {
87  theA = 0;
88  theZ = 0;
90  } else if(pSNorm=="pi-" || pSNorm=="pion-" || pSNorm=="piminus" || pSNorm=="pionminus") {
91  theA = 0;
92  theZ = -1;
94  } else if(pSNorm=="d" || pSNorm=="deuteron") {
95  theA = 2;
96  theZ = 1;
98  } else if(pSNorm=="t" || pSNorm=="triton") {
99  theA = 3;
100  theZ = 1;
102  } else if(pSNorm=="a" || pSNorm=="alpha") {
103  theA = 4;
104  theZ = 2;
106  } else
107  parseNuclide(pSNorm);
108  }
109 
111  theType(t),
112  theA(ParticleTable::getMassNumber(theType)),
113  theZ(ParticleTable::getChargeNumber(theType))
114  {}
115 
117  theType(Composite),
118  theA(A),
119  theZ(Z)
120  {}
121 
122  void ParticleSpecies::parseNuclide(std::string const &pS) {
123  theType = Composite;
124 
125  // Allowed characters
126  const std::string separators("-_");
127  std::string allowed("0123456789abcdefghijklmnopqrstuvwxyz");
128  allowed += separators;
129 
130  // There must be at least one character
131  if(pS.find_first_not_of(allowed)!=std::string::npos) {
132  // Malformed input string
133  // Setting unknown particle species
134  (*this) = ParticleSpecies(UnknownParticle);
135  return;
136  }
137  if(pS.size()<1) {
138  // Malformed input string
139  // Setting unknown particle species
140  (*this) = ParticleSpecies(UnknownParticle);
141  return;
142  }
143 
144  std::size_t firstSeparator = pS.find_first_of(separators);
145  std::size_t lastSeparator = pS.find_last_of(separators);
146  if(firstSeparator!=std::string::npos && firstSeparator!=lastSeparator) {
147  // Several separators in malformed input string
148  // Setting unknown particle species
149  (*this) = ParticleSpecies(UnknownParticle);
150  return;
151  }
152 
153  // Identify the type of the first character
154  G4int (*predicate)(G4int);
155  G4bool startsWithAlpha = std::isalpha(pS.at(0));
156  if(startsWithAlpha) {
157  predicate=std::isdigit;
158  } else if(std::isdigit(pS.at(0))) {
159  predicate=std::isalpha;
160  } else {
161  // Non-alphanumeric character in string
162  // Setting unknown particle species
163  (*this) = ParticleSpecies(UnknownParticle);
164  return;
165  }
166 
167  G4bool hasIsotope = true;
168  size_t endFirstSection, beginSecondSection;
169  if(firstSeparator==std::string::npos) {
170  // No separator, Fe56 or 56Fe style
171  // Identify the end of the first section
172 
173  // Find the first character that is not of the same type as the first one
174  beginSecondSection = std::find_if(pS.begin()+1, pS.end(), predicate) - pS.begin();
175 
176  if(beginSecondSection>=pS.size()) {
177  if(startsWithAlpha) {
178  // Only alphabetic characters are present -- must be an element name
179  hasIsotope = false;
180  } else {
181  // Only numeric characters in the string
182  // Setting unknown particle species
183  (*this) = ParticleSpecies(UnknownParticle);
184  return;
185  }
186  }
187 
188  endFirstSection = beginSecondSection;
189 
190  } else {
191  // One separator, Fe-56 or 56-Fe style
192  endFirstSection = firstSeparator;
193  beginSecondSection = firstSeparator+1;
194  }
195 
196  std::string firstSection(pS.substr(0,endFirstSection));
197  std::string secondSection(pS.substr(beginSecondSection,std::string::npos));
198  std::stringstream parsingStream;
199 
200  // Parse the sections
201  G4bool success;
202  if(startsWithAlpha) {
203  parsingStream.str(secondSection);
204  success = parseElement(firstSection);
205  } else {
206  parsingStream.str(firstSection);
207  success = parseElement(secondSection);
208  }
209  if(!success) {
210  // Couldn't parse the element section
211  // Setting unknown particle species
212  (*this) = ParticleSpecies(UnknownParticle);
213  return;
214  }
215 
216  if(hasIsotope) {
217  parsingStream >> theA;
218  if(parsingStream.fail()) {
219  // Couldn't parse the mass section
220  // Setting unknown particle species
221  (*this) = ParticleSpecies(UnknownParticle);
222  return;
223  }
224  } else
225  theA = 0;
226 
227  // Check that Z<=A
228  if(theZ>theA && hasIsotope) {
229  // Setting unknown particle species
230  (*this) = ParticleSpecies(UnknownParticle);
231  return;
232  }
233 
234  // Special particle type for protons
235  if(theZ==1 && theA==1)
236  theType = Proton;
237  }
238 
239  G4bool ParticleSpecies::parseElement(std::string const &s) {
241 
242  if(theZ<0)
244 
245  if(theZ<0)
246  return false;
247  else
248  return true;
249  }
250 
253  if(theZ==0)
254  return false;
255  else
256  return true;
257  }
258 }
259 
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:150
G4int getMassNumber(const ParticleType t)
Get mass number from particle type.
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.
static const G4double A[nN]
G4bool parseElement(std::string const &pS)
Parse an element name.
G4int parseIUPACElement(std::string const &pS)
Parse a IUPAC element name.