GEMC  1.8
Geant4 Monte-Carlo Framework
string_utilities.cc
Go to the documentation of this file.
1 // %%%%%%%%%%%%%
2 // gemc headers
3 // %%%%%%%%%%%%%
4 #include "string_utilities.h"
5 
6 // %%%%%%%%%%%%%
7 // gemc headers
8 // %%%%%%%%%%%%%
9 #include "G4UnitsTable.hh"
10 
11 
12 #include <algorithm>
13 vector<string> get_info(string input)
14 {
15 
16  char chars[] = "(),\"";
17  string stripped = replaceCharsWithSpaces(input, chars);
18 
19 
20 
21  return get_strings(stripped);
22 }
23 
24 // Trims Both leading and trailing spaces
25 string TrimSpaces(string in)
26 {
27  string out;
28 
29  size_t leapos = in.find_first_not_of(" \t"); // Find the first character position after excluding leading blank spaces
30  size_t endpos = in.find_last_not_of(" \t"); // Find the first character position from reverse af
31 
32  // if all spaces or empty return an empty string
33  if(( string::npos == leapos ) || ( string::npos == endpos))
34  out = "";
35  else
36  out = in.substr( leapos, endpos-leapos+1 );
37 
38  return out;
39 }
40 
41 
42 
43 // returns a vector of strings from a stringstream
44 vector<string> get_strings(string input)
45 {
46  vector<string> pvalues;
47  stringstream plist(input);
48  while(!plist.eof())
49  {
50  string tmp;
51  plist >> tmp;
52  pvalues.push_back(tmp);
53  }
54  return pvalues;
55 }
56 
57 
58 
59 
60 // assigns G4 units to a variable
65 double get_number(string v)
66 {
67  string value = TrimSpaces(v);
68 
69  if(value.find("*") == string::npos)
70  {
71  // no * found, this should be a number
72  return atof(value.c_str());
73  }
74 
75  else
76  {
77  double answer = atof(value.substr(0, value.find("*")).c_str());
78  string units = TrimSpaces(value.substr(value.find("*")+1, value.find("*") + 20));
79  if( units == "m") answer *= m;
80  else if( units == "inches") answer *= 2.54*cm;
81  else if( units == "cm") answer *= cm;
82  else if( units == "mm") answer *= mm;
83  else if( units == "um") answer *= 1E-6*m;
84  else if( units == "fm") answer *= 1E-15*m;
85  else if( units == "deg") answer *= deg;
86  else if( units == "arcmin") answer = answer/60.0*deg;
87  else if( units == "rad") answer *= rad;
88  else if( units == "mrad") answer *= mrad;
89  else if( units == "eV") answer *= eV;
90  else if( units == "MeV") answer *= MeV;
91  else if( units == "KeV") answer *= 0.001*MeV;
92  else if( units == "GeV") answer *= GeV;
93  else if( units == "T") answer *= tesla;
94  else if( units == "Tesla") answer *= tesla;
95  else if( units == "ns") answer *= ns;
96  else cout << ">" << units << "<: unit not recognized for string <" << v << ">" << endl;
97  return answer;
98  }
99 
100  return 0;
101 }
102 
103 
104 
105 
106 void print_vstring(vector<string> s)
107 {
108  for(unsigned int i=0; i<s.size(); i++)
109  cout << "string element: " << i << " content: " << s[i] << endl;
110 }
111 
112 
113 
114 
115 
116 
vector< string > get_strings(string input)
returns a vector of strings from a stringstream
vector< string > get_info(string input)
get information from strings such as "5*GeV, 2*deg, 10*deg"
string replaceCharsWithSpaces(string input, char *x)
double get_number(string v)
Returns dimension from string, i.e. 100*cm.
string TrimSpaces(string in)
Removes leading and trailing spaces.
void print_vstring(vector< string > s)
prints each element of a string vector