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