Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-03 08:19:44

0001 /***********************************************************************
0002 See ParameterReader.h for brief description and change log.
0003 09-09-2011 Zhi Qiu
0004 ***********************************************************************/
0005 
0006 #include <iostream>
0007 #include <fstream>
0008 #include "stdlib.h"
0009 
0010 #include "arsenal.h"
0011 #include "ParameterReader.h"
0012 
0013 using namespace std;
0014 
0015 //----------------------------------------------------------------------
0016 ParameterReader::ParameterReader()
0017 {
0018   names = new vector<string>;
0019   values = new vector<double>;
0020 }
0021 
0022 
0023 //----------------------------------------------------------------------
0024 ParameterReader::~ParameterReader()
0025 {
0026   delete names; // in principle garbage collection for vector should be automatic, just to be safe
0027   delete values;
0028 }
0029 
0030 
0031 //----------------------------------------------------------------------
0032 string ParameterReader::removeComments(string str, string commentSymbol)
0033 /*
0034   Remove comments from a string "str". Comments are all characters after the string "commentSymbol".
0035 */
0036 {
0037   return str.substr(0, str.find(commentSymbol));
0038 }
0039 
0040 
0041 //----------------------------------------------------------------------
0042 void ParameterReader::phraseEquationWithoutComments(string equation)
0043 /*
0044   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.
0045 */
0046 {
0047   if (trim(equation).compare("")==0) return;
0048   size_t symbolPos = equation.find('=');
0049   if (symbolPos==string::npos)
0050   {
0051     cout << "ParameterReader::phraseEquationWithoutComments error: \"=\" symbol not found in equation assignment " << equation << endl;
0052     exit(-1);
0053   }
0054   string LHS (equation.begin(), equation.begin()+symbolPos);
0055   string RHS (equation.begin()+symbolPos+1, equation.end());
0056   setVal(LHS, stringToDouble(trim(RHS)));
0057 }
0058 
0059 
0060 //----------------------------------------------------------------------
0061 long ParameterReader::find(string name)
0062 /*
0063   Check if the parameter with "name" already exists in the internal "names" list. If yes, it returns its
0064 */
0065 {
0066   for (long ii=0; ii<names->size(); ii++)
0067     if ((*names)[ii].compare(toLower(trim(name)))==0) return ii;
0068   return -1;
0069 }
0070 
0071 
0072 //----------------------------------------------------------------------
0073 void ParameterReader::phraseOneLine(string str, string commentSymbol)
0074 /*
0075   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".
0076 */
0077 {
0078   if (trim(str).compare("")==0) return;
0079   phraseEquationWithoutComments(removeComments(str, commentSymbol));
0080 }
0081 
0082 
0083 //----------------------------------------------------------------------
0084 void ParameterReader::readFromFile(string filename, string commentSymbol)
0085 /*
0086   Read all lines in a file as parameter assignment list. Each line is processed by the phraseOneLine function.
0087 */
0088 {
0089   ifstream parameterFile(filename.c_str());
0090   if (!parameterFile)
0091   {
0092     cout << "ParameterReader::readFromFile error: file " << filename << " does not exist." << endl;
0093     exit(-1);
0094   }
0095   char buffer[9999];
0096   while (!parameterFile.eof())
0097   {
0098     parameterFile.getline(buffer, 9999);
0099     phraseOneLine(buffer);
0100   }
0101   parameterFile.close();
0102 }
0103 
0104 
0105 //----------------------------------------------------------------------
0106 void ParameterReader::readFromArguments(long argc, char * argv[], string commentSymbol, long start_from)
0107 /*
0108   Read all strings in argv[]. Each string is processed by the phraseOneLine function.
0109 */
0110 {
0111   for (long ii=start_from; ii<argc; ii++) phraseOneLine(argv[ii], commentSymbol);
0112 }
0113 
0114 
0115 //----------------------------------------------------------------------
0116 bool ParameterReader::exist(string name)
0117 /*
0118   Return true if parameter with "name" is registered.
0119 */
0120 {
0121   return find(name)==-1 ? false: true;
0122 }
0123 
0124 
0125 //----------------------------------------------------------------------
0126 void ParameterReader::setVal(string name, double value)
0127 /*
0128   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.
0129 */
0130 {
0131   long idx = find(name);
0132   if (idx==-1)
0133   {
0134     names->push_back(toLower(trim(name))); values->push_back(value);
0135   }
0136   else
0137   {
0138     (*names)[idx]=toLower(trim(name)); (*values)[idx]=value;
0139   }
0140 }
0141 
0142 
0143 //----------------------------------------------------------------------
0144 double ParameterReader::getVal(string name)
0145 /*
0146   Get the value for the parameter with "name".
0147 */
0148 {
0149   long idx = find(name);
0150   if (idx!=-1)
0151     return (*values)[idx];
0152   else
0153   {
0154     cout << "ParameterReader::getVal error: parameter with name " << name << " not found." << endl;
0155     exit(-1);
0156   }
0157 }
0158 
0159 
0160 //----------------------------------------------------------------------
0161 void ParameterReader::echo()
0162 /*
0163   Print out all stored parameters to screen.
0164 */
0165 {
0166   if (names->size()==0) return;
0167   for (long ii=0; ii<names->size(); ii++) cout << (*names)[ii] << "=" << (*values)[ii] << "  ";
0168   cout << endl;
0169 }