GEMC  2.3
Geant4 Monte-Carlo Framework
string_utilities.cc
Go to the documentation of this file.
1 // gemc headers
2 #include "string_utilities.h"
3 
4 
5 // C++ headers
6 #include <algorithm>
7 
8 
9 // CLHEP units
10 #include "CLHEP/Units/PhysicalConstants.h"
11 using namespace CLHEP;
12 
13 vector<string> get_info(string input, string chars)
14 {
15  string stripped = replaceCharWithChars(input, chars, " ");
16 
17  return get_strings(stripped);
18 }
19 
20 vector<string> get_info(string input)
21 {
22 
23  string chars("(),\"");
24 
25  string stripped = replaceCharWithChars(input, chars, " ");
26 
27  return get_strings(stripped);
28 }
29 
30 // Trims Both leading and trailing spaces
31 string TrimSpaces(string in)
32 {
33  string out;
34 
35  size_t leapos = in.find_first_not_of(" \t"); // Find the first character position after excluding leading blank spaces
36  size_t endpos = in.find_last_not_of(" \t"); // Find the first character position from reverse af
37 
38  // if all spaces or empty return an empty string
39  if(( string::npos == leapos ) || ( string::npos == endpos))
40  out = "";
41  else
42  out = in.substr( leapos, endpos-leapos+1 );
43 
44  return out;
45 }
46 
47 
48 
49 // returns a vector of strings from a stringstream, space is delimeter
50 vector<string> get_strings(string input)
51 {
52  vector<string> pvalues;
53  stringstream plist(input);
54  while(!plist.eof())
55  {
56  string tmp;
57  plist >> tmp;
58  pvalues.push_back(TrimSpaces(tmp));
59  }
60 
61  return pvalues;
62 }
63 
64 
65 // returns a vector of strings from a stringstream, space is delimeter
66 // ignore instances of second string
67 vector<string> get_strings_except(string input, string ignore)
68 {
69  vector<string> pvalues;
70  stringstream plist(input);
71  while(!plist.eof())
72  {
73  string tmp;
74  plist >> tmp;
75 
76  if(tmp.find(ignore) == string::npos)
77  pvalues.push_back(TrimSpaces(tmp));
78  }
79 
80  return pvalues;
81 }
82 
83 
84 // returns a vector of strings from a stringstream, x (one char) is delimiter
85 vector<string> get_strings(string input, string x)
86 {
87  vector<string> pvalues;
88 
89  string tmp;
90  for(unsigned int i=0; i<input.size(); i++)
91  {
92  if(input[i] != x[0])
93  tmp += input[i];
94 
95  else
96  {
97  pvalues.push_back(tmp);
98  tmp = "";
99  }
100 
101  // end of line
102  if(i==input.size() - 1)
103  {
104  pvalues.push_back(tmp);
105  }
106 
107  }
108 
109  return pvalues;
110 }
111 
112 double scan_number(const char *str)
113 {
114  // Scan the c_string str for numbers only, then return the value as a float.
115  // The str is not allowed to have spaces or alphanumerics, only 0-9 and .
116  int i=0;
117  while(char c=str[i++]) if(isalpha(c) && !(c=='-' || c=='+' || c=='e' || c=='E') )
118  {
119  cout << "WARNING: Unexpected Alphanumberic character found in number string:" << str << endl;
120  }
121 
122  return( stringToDouble(str));
123 }
124 
125 
126 
127 
128 // assigns G4 units to a variable
134 double get_number(string v,int warn_no_unit)
135 {
136  string value = TrimSpaces(v);
137 
138  if(value.find("*") == string::npos)
139  {
140  // no * found, this should be a number
141  // No unit is still ok if the number is 0
142  if(value.length()>0 && warn_no_unit && stringToDouble(value) != 0) cout << "Warning: All numbers should be paired with a unit: " << v << endl;
143  return stringToDouble(value);
144  }
145 
146  else
147  {
148  double answer = scan_number(value.substr(0, value.find("*")).c_str());
149  string units = TrimSpaces(value.substr(value.find("*")+1, value.find("*") + 20));
150  if( units == "m") answer *= m;
151  else if( units == "inches") answer *= 2.54*cm;
152  else if( units == "inch") answer *= 2.54*cm;
153  else if( units == "cm") answer *= cm;
154  else if( units == "mm") answer *= mm;
155  else if( units == "um") answer *= 1E-6*m;
156  else if( units == "fm") answer *= 1E-15*m;
157  else if( units == "deg") answer *= deg;
158  else if( units == "degrees") answer *= deg;
159  else if( units == "arcmin") answer = answer/60.0*deg;
160  else if( units == "rad") answer *= rad;
161  else if( units == "mrad") answer *= mrad;
162  else if( units == "eV") answer *= eV;
163  else if( units == "MeV") answer *= MeV;
164  else if( units == "KeV") answer *= 0.001*MeV;
165  else if( units == "GeV") answer *= GeV;
166  else if( units == "T") answer *= tesla;
167  else if( units == "T/m") answer *= tesla/m;
168  else if( units == "Tesla") answer *= tesla;
169  else if( units == "gauss") answer *= gauss;
170  else if( units == "kilogauss") answer *= gauss*1000;
171  else if( units == "ns") answer *= ns;
172  else if( units == "na") answer *= 1;
173  else if( units == "counts") answer *= 1;
174  else cout << ">" << units << "<: unit not recognized for string <" << v << ">" << endl;
175  return answer;
176  }
177 
178  return 0;
179 }
180 
181 
182 
183 
184 void print_vstring(vector<string> s)
185 {
186  for(unsigned int i=0; i<s.size(); i++)
187  cout << "string element: " << i << " content: " << s[i] << endl;
188 }
189 
190 
191 
192 string get_variation(string var)
193 {
194  string variation = var;
195  vector<string> vars = get_strings(var);
196  if(vars[0] == "main" && vars.size() > 1)
197  {
198  variation = vars[1];
199  }
200  return variation;
201 }
202 
203 bool is_main_variation(string var)
204 {
205  if(var.find("main:") == 0)
206  return 1;
207 
208  return 0;
209 }
210 
211 
212 
213 // overloading << for map<string, string>
214 ostream &operator<<(ostream &stream, map<string, string> smap)
215 {
216  cout << endl;
217  for(map<string, string>::iterator it = smap.begin(); it != smap.end(); it++)
218  cout << it->first << " " << it->second << endl;
219 
220  cout << endl;
221 
222  return stream;
223 }
224 
225 
226 
227 
228 
vector< string > get_strings(string input)
returns a vector of strings from a stringstream, space is delimiter
vector< string > get_info(string input, string chars)
get information from strings such as "5*GeV, 2*deg, 10*deg", parses out strings in second argument ...
double scan_number(const char *str)
string get_variation(string var)
parse variation name from string
string replaceCharWithChars(string input, string x, string y)
double get_number(string v, int warn_no_unit)
Returns number with dimension from string, i.e. 100*cm.
double stringToDouble(string v)
bool is_main_variation(string var)
returns 1 if the string "main:" is found on the input
string TrimSpaces(string in)
Removes leading and trailing spaces.
vector< string > get_strings_except(string input, string ignore)
returns a vector of strings from a stringstream, space is delimiter, ignore string with second argume...
void print_vstring(vector< string > s)
prints each element of a string vector