Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ParameterReader.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file ParameterReader.cpp
1 /***********************************************************************
2 See ParameterReader.h for brief description and change log.
3 09-09-2011 Zhi Qiu
4 ***********************************************************************/
5 
6 #include <iostream>
7 #include <fstream>
8 #include "stdlib.h"
9 
10 #include "arsenal.h"
11 #include "ParameterReader.h"
12 
13 using namespace std;
14 
15 //----------------------------------------------------------------------
17 {
18  names = new vector<string>;
19  values = new vector<double>;
20 }
21 
22 
23 //----------------------------------------------------------------------
25 {
26  delete names; // in principle garbage collection for vector should be automatic, just to be safe
27  delete values;
28 }
29 
30 
31 //----------------------------------------------------------------------
32 string ParameterReader::removeComments(string str, string commentSymbol)
33 /*
34  Remove comments from a string "str". Comments are all characters after the string "commentSymbol".
35 */
36 {
37  return str.substr(0, str.find(commentSymbol));
38 }
39 
40 
41 //----------------------------------------------------------------------
43 /*
44  Phrase an equation like "x=1", and store the result into "names" and "values". The equation is first separated according to the equal sign, then the left and right hand side will be trimmed, after that the right hand side will be converted to double type number.
45 */
46 {
47  if (trim(equation).compare("")==0) return;
48  size_t symbolPos = equation.find('=');
49  if (symbolPos==string::npos)
50  {
51  cout << "ParameterReader::phraseEquationWithoutComments error: \"=\" symbol not found in equation assignment " << equation << endl;
52  exit(-1);
53  }
54  string LHS (equation.begin(), equation.begin()+symbolPos);
55  string RHS (equation.begin()+symbolPos+1, equation.end());
56  setVal(LHS, stringToDouble(trim(RHS)));
57 }
58 
59 
60 //----------------------------------------------------------------------
62 /*
63  Check if the parameter with "name" already exists in the internal "names" list. If yes, it returns its
64 */
65 {
66  for (long ii=0; ii<names->size(); ii++)
67  if ((*names)[ii].compare(toLower(trim(name)))==0) return ii;
68  return -1;
69 }
70 
71 
72 //----------------------------------------------------------------------
73 void ParameterReader::phraseOneLine(string str, string commentSymbol)
74 /*
75  Interpret a string like " x = 1.1 #bla " to get the associated parameter name and value information, and put them into the internal variables "names" and "values".
76 */
77 {
78  if (trim(str).compare("")==0) return;
79  phraseEquationWithoutComments(removeComments(str, commentSymbol));
80 }
81 
82 
83 //----------------------------------------------------------------------
84 void ParameterReader::readFromFile(string filename, string commentSymbol)
85 /*
86  Read all lines in a file as parameter assignment list. Each line is processed by the phraseOneLine function.
87 */
88 {
89  ifstream parameterFile(filename.c_str());
90  if (!parameterFile)
91  {
92  cout << "ParameterReader::readFromFile error: file " << filename << " does not exist." << endl;
93  exit(-1);
94  }
95  char buffer[9999];
96  while (!parameterFile.eof())
97  {
98  parameterFile.getline(buffer, 9999);
99  phraseOneLine(buffer);
100  }
101  parameterFile.close();
102 }
103 
104 
105 //----------------------------------------------------------------------
106 void ParameterReader::readFromArguments(long argc, char * argv[], string commentSymbol, long start_from)
107 /*
108  Read all strings in argv[]. Each string is processed by the phraseOneLine function.
109 */
110 {
111  for (long ii=start_from; ii<argc; ii++) phraseOneLine(argv[ii], commentSymbol);
112 }
113 
114 
115 //----------------------------------------------------------------------
117 /*
118  Return true if parameter with "name" is registered.
119 */
120 {
121  return find(name)==-1 ? false: true;
122 }
123 
124 
125 //----------------------------------------------------------------------
126 void ParameterReader::setVal(string name, double value)
127 /*
128  Set the parameter with "name" to "value". It is appended to the internal "names" and "values" vector if "name" does not exist; otherwise it is rewitten.
129 */
130 {
131  long idx = find(name);
132  if (idx==-1)
133  {
134  names->push_back(toLower(trim(name))); values->push_back(value);
135  }
136  else
137  {
138  (*names)[idx]=toLower(trim(name)); (*values)[idx]=value;
139  }
140 }
141 
142 
143 //----------------------------------------------------------------------
145 /*
146  Get the value for the parameter with "name".
147 */
148 {
149  long idx = find(name);
150  if (idx!=-1)
151  return (*values)[idx];
152  else
153  {
154  cout << "ParameterReader::getVal error: parameter with name " << name << " not found." << endl;
155  exit(-1);
156  }
157 }
158 
159 
160 //----------------------------------------------------------------------
162 /*
163  Print out all stored parameters to screen.
164 */
165 {
166  if (names->size()==0) return;
167  for (long ii=0; ii<names->size(); ii++) cout << (*names)[ii] << "=" << (*values)[ii] << " ";
168  cout << endl;
169 }