Geant4 Cross Reference |
1 // -*- C++ -*- 1 // -*- C++ -*- >> 2 // $Id:$ 2 // ------------------------------------------- 3 // --------------------------------------------------------------------------- 3 4 4 #ifndef HEP_EVALUATOR_H 5 #ifndef HEP_EVALUATOR_H 5 #define HEP_EVALUATOR_H 6 #define HEP_EVALUATOR_H 6 7 7 #include <string> 8 #include <string> 8 9 9 namespace HepTool { 10 namespace HepTool { 10 11 11 /** 12 /** 12 * Evaluator of arithmetic expressions with an 13 * Evaluator of arithmetic expressions with an extendable dictionary. 13 * Example: 14 * Example: 14 * @code 15 * @code 15 * #include "CLHEP/Evaluator/Evaluator.h" 16 * #include "CLHEP/Evaluator/Evaluator.h" 16 * HepTool::Evaluator eval; 17 * HepTool::Evaluator eval; 17 * eval.setStdMath(); 18 * eval.setStdMath(); 18 * double res = eval.evaluate("sin(30*degree 19 * double res = eval.evaluate("sin(30*degree)"); 19 * if (eval.status() != HepTool::Evaluator:: 20 * if (eval.status() != HepTool::Evaluator::OK) eval.print_error(); 20 * @endcode 21 * @endcode 21 * 22 * 22 * @author Evgeni Chernyaev <Evgueni.Tcherniae 23 * @author Evgeni Chernyaev <Evgueni.Tcherniaev@cern.ch> 23 * @ingroup evaluator 24 * @ingroup evaluator 24 */ 25 */ 25 class Evaluator { 26 class Evaluator { 26 public: 27 public: 27 28 28 /** 29 /** 29 * List of possible statuses. 30 * List of possible statuses. 30 * Status of the last operation can be obtai 31 * Status of the last operation can be obtained with status(). 31 * In case if status() is an ERROR the corre 32 * In case if status() is an ERROR the corresponding error message 32 * can be printed with print_error(). 33 * can be printed with print_error(). 33 * 34 * 34 * @see status 35 * @see status 35 * @see error_position 36 * @see error_position 36 * @see print_error 37 * @see print_error 37 */ 38 */ 38 enum { 39 enum { 39 OK, /**< Everythin 40 OK, /**< Everything OK */ 40 WARNING_EXISTING_VARIABLE, /**< Redefinit 41 WARNING_EXISTING_VARIABLE, /**< Redefinition of existing variable */ 41 WARNING_EXISTING_FUNCTION, /**< Redefinit 42 WARNING_EXISTING_FUNCTION, /**< Redefinition of existing function */ 42 WARNING_BLANK_STRING, /**< Empty inp 43 WARNING_BLANK_STRING, /**< Empty input string */ 43 ERROR_NOT_A_NAME, /**< Not allow 44 ERROR_NOT_A_NAME, /**< Not allowed sysmbol in the name of variable or function */ 44 ERROR_SYNTAX_ERROR, /**< Systax er 45 ERROR_SYNTAX_ERROR, /**< Systax error */ 45 ERROR_UNPAIRED_PARENTHESIS, /**< Unpaired 46 ERROR_UNPAIRED_PARENTHESIS, /**< Unpaired parenthesis */ 46 ERROR_UNEXPECTED_SYMBOL, /**< Unexpecte 47 ERROR_UNEXPECTED_SYMBOL, /**< Unexpected sysbol */ 47 ERROR_UNKNOWN_VARIABLE, /**< Non-exist 48 ERROR_UNKNOWN_VARIABLE, /**< Non-existing variable */ 48 ERROR_UNKNOWN_FUNCTION, /**< Non-exist 49 ERROR_UNKNOWN_FUNCTION, /**< Non-existing function */ 49 ERROR_EMPTY_PARAMETER, /**< Function 50 ERROR_EMPTY_PARAMETER, /**< Function call has empty parameter */ 50 ERROR_CALCULATION_ERROR /**< Error dur 51 ERROR_CALCULATION_ERROR /**< Error during calculation */ 51 }; 52 }; 52 53 53 /** 54 /** 54 * Constructor. 55 * Constructor. 55 */ 56 */ 56 Evaluator(); 57 Evaluator(); 57 58 58 /** 59 /** 59 * Destructor. 60 * Destructor. 60 */ 61 */ 61 ~Evaluator(); 62 ~Evaluator(); 62 63 63 /** 64 /** 64 * Evaluates the arithmetic expression given 65 * Evaluates the arithmetic expression given as character string. 65 * The expression may consist of numbers, va 66 * The expression may consist of numbers, variables and functions 66 * separated by arithmetic (+, - , /, *, ^, 67 * separated by arithmetic (+, - , /, *, ^, **) and logical 67 * operators (==, !=, >, >=, <, <=, &&, ||). 68 * operators (==, !=, >, >=, <, <=, &&, ||). 68 * 69 * 69 * @param expression input expression. 70 * @param expression input expression. 70 * @return result of the evaluation. 71 * @return result of the evaluation. 71 * @see status 72 * @see status 72 * @see error_position 73 * @see error_position 73 * @see print_error 74 * @see print_error 74 */ 75 */ 75 double evaluate(const char * expression); 76 double evaluate(const char * expression); 76 77 77 /** 78 /** 78 * Returns status of the last operation with 79 * Returns status of the last operation with the evaluator. 79 */ 80 */ 80 int status() const; 81 int status() const; 81 82 82 /** 83 /** 83 * Returns position in the input string wher 84 * Returns position in the input string where the problem occured. 84 */ 85 */ 85 int error_position() const; 86 int error_position() const; 86 87 87 /** 88 /** 88 * Prints error message if status() is an ER 89 * Prints error message if status() is an ERROR. 89 */ 90 */ 90 void print_error() const; 91 void print_error() const; 91 /** 92 /** 92 * get a string defining the error name 93 * get a string defining the error name 93 */ 94 */ 94 std::string error_name() const; 95 std::string error_name() const; 95 96 96 /** 97 /** 97 * Adds to the dictionary a variable with gi 98 * Adds to the dictionary a variable with given value. 98 * If a variable with such a name already ex 99 * If a variable with such a name already exist in the dictionary, 99 * then status will be set to WARNING_EXISTI 100 * then status will be set to WARNING_EXISTING_VARIABLE. 100 * 101 * 101 * @param name name of the variable. 102 * @param name name of the variable. 102 * @param value value assigned to the variab 103 * @param value value assigned to the variable. 103 */ 104 */ 104 void setVariable(const char * name, double v 105 void setVariable(const char * name, double value); 105 106 106 /** 107 /** 107 * Adds to the dictionary a variable with an 108 * Adds to the dictionary a variable with an arithmetic expression 108 * assigned to it. 109 * assigned to it. 109 * If a variable with such a name already ex 110 * If a variable with such a name already exist in the dictionary, 110 * then status will be set to WARNING_EXISTI 111 * then status will be set to WARNING_EXISTING_VARIABLE. 111 * 112 * 112 * @param name name of the variable. 113 * @param name name of the variable. 113 * @param expression arithmetic expression. 114 * @param expression arithmetic expression. 114 */ 115 */ 115 void setVariable(const char * name, const ch 116 void setVariable(const char * name, const char * expression); 116 117 117 /** 118 /** 118 * Adds to the dictionary a function without 119 * Adds to the dictionary a function without parameters. 119 * If such a function already exist in the d 120 * If such a function already exist in the dictionary, 120 * then status will be set to WARNING_EXISTI 121 * then status will be set to WARNING_EXISTING_FUNCTION. 121 * 122 * 122 * @param name function name. 123 * @param name function name. 123 * @param fun pointer to the real function i 124 * @param fun pointer to the real function in the user code. 124 */ 125 */ 125 void setFunction(const char * name, double ( 126 void setFunction(const char * name, double (*fun)()); 126 127 127 /** 128 /** 128 * Adds to the dictionary a function with on 129 * Adds to the dictionary a function with one parameter. 129 * If such a function already exist in the d 130 * If such a function already exist in the dictionary, 130 * then status will be set to WARNING_EXISTI 131 * then status will be set to WARNING_EXISTING_FUNCTION. 131 * 132 * 132 * @param name function name. 133 * @param name function name. 133 * @param fun pointer to the real function i 134 * @param fun pointer to the real function in the user code. 134 */ 135 */ 135 void setFunction(const char * name, double ( 136 void setFunction(const char * name, double (*fun)(double)); 136 137 137 /** 138 /** 138 * Adds to the dictionary a function with tw 139 * Adds to the dictionary a function with two parameters. 139 * If such a function already exist in the d 140 * If such a function already exist in the dictionary, 140 * then status will be set to WARNING_EXISTI 141 * then status will be set to WARNING_EXISTING_FUNCTION. 141 * 142 * 142 * @param name function name. 143 * @param name function name. 143 * @param fun pointer to the real function i 144 * @param fun pointer to the real function in the user code. 144 */ 145 */ 145 void setFunction(const char * name, double ( 146 void setFunction(const char * name, double (*fun)(double,double)); 146 147 147 /** 148 /** 148 * Adds to the dictionary a function with th 149 * Adds to the dictionary a function with three parameters. 149 * If such a function already exist in the d 150 * If such a function already exist in the dictionary, 150 * then status will be set to WARNING_EXISTI 151 * then status will be set to WARNING_EXISTING_FUNCTION. 151 * 152 * 152 * @param name function name. 153 * @param name function name. 153 * @param fun pointer to the real function i 154 * @param fun pointer to the real function in the user code. 154 */ 155 */ 155 void setFunction(const char * name, double ( 156 void setFunction(const char * name, double (*fun)(double,double,double)); 156 157 157 /** 158 /** 158 * Adds to the dictionary a function with fo 159 * Adds to the dictionary a function with four parameters. 159 * If such a function already exist in the d 160 * If such a function already exist in the dictionary, 160 * then status will be set to WARNING_EXISTI 161 * then status will be set to WARNING_EXISTING_FUNCTION. 161 * 162 * 162 * @param name function name. 163 * @param name function name. 163 * @param fun pointer to the real function i 164 * @param fun pointer to the real function in the user code. 164 */ 165 */ 165 void setFunction(const char * name, 166 void setFunction(const char * name, 166 double (*fun)(double,double,double,doub 167 double (*fun)(double,double,double,double)); 167 168 168 /** 169 /** 169 * Adds to the dictionary a function with fi 170 * Adds to the dictionary a function with five parameters. 170 * If such a function already exist in the d 171 * If such a function already exist in the dictionary, 171 * then status will be set to WARNING_EXISTI 172 * then status will be set to WARNING_EXISTING_FUNCTION. 172 * 173 * 173 * @param name function name. 174 * @param name function name. 174 * @param fun pointer to the real function i 175 * @param fun pointer to the real function in the user code. 175 */ 176 */ 176 void setFunction(const char * name, 177 void setFunction(const char * name, 177 double (*fun)(double,double 178 double (*fun)(double,double,double,double,double)); 178 179 179 /** 180 /** 180 * Finds the variable in the dictionary. 181 * Finds the variable in the dictionary. 181 * 182 * 182 * @param name name of the variable. 183 * @param name name of the variable. 183 * @return true if such a variable exists, f 184 * @return true if such a variable exists, false otherwise. 184 */ 185 */ 185 bool findVariable(const char * name) const; 186 bool findVariable(const char * name) const; 186 187 187 /** 188 /** 188 * Finds the function in the dictionary. 189 * Finds the function in the dictionary. 189 * 190 * 190 * @param name name of the function to be u 191 * @param name name of the function to be unset. 191 * @param npar number of parameters of the 192 * @param npar number of parameters of the function. 192 * @return true if such a function exists, f 193 * @return true if such a function exists, false otherwise. 193 */ 194 */ 194 bool findFunction(const char * name, int npa 195 bool findFunction(const char * name, int npar) const; 195 196 196 /** 197 /** 197 * Removes the variable from the dictionary. 198 * Removes the variable from the dictionary. 198 * 199 * 199 * @param name name of the variable. 200 * @param name name of the variable. 200 */ 201 */ 201 void removeVariable(const char * name); 202 void removeVariable(const char * name); 202 203 203 /** 204 /** 204 * Removes the function from the dictionary. 205 * Removes the function from the dictionary. 205 * 206 * 206 * @param name name of the function to be un 207 * @param name name of the function to be unset. 207 * @param npar number of parameters of the f 208 * @param npar number of parameters of the function. 208 */ 209 */ 209 void removeFunction(const char * name, int n 210 void removeFunction(const char * name, int npar); 210 211 211 /** 212 /** 212 * Clear all settings. 213 * Clear all settings. 213 */ 214 */ 214 void clear(); 215 void clear(); 215 216 216 /** 217 /** 217 * Sets standard mathematical functions and 218 * Sets standard mathematical functions and constants. 218 */ 219 */ 219 void setStdMath(); 220 void setStdMath(); 220 221 221 /** 222 /** 222 * Sets system of units. Default is the SI s 223 * Sets system of units. Default is the SI system of units. 223 * To set the CGS (Centimeter-Gram-Second) s 224 * To set the CGS (Centimeter-Gram-Second) system of units 224 * one should call: 225 * one should call: 225 * setSystemOfUnits(100., 1000., 1.0, 1.0, 226 * setSystemOfUnits(100., 1000., 1.0, 1.0, 1.0, 1.0, 1.0); 226 * 227 * 227 * To set system of units accepted in the GE 228 * To set system of units accepted in the GEANT4 simulation toolkit 228 * one should call: 229 * one should call: 229 * @code 230 * @code 230 * setSystemOfUnits(1.e+3, 1./1.60217733e- 231 * setSystemOfUnits(1.e+3, 1./1.60217733e-25, 1.e+9, 1./1.60217733e-10, 231 * 1.0, 1.0, 1.0); 232 * 1.0, 1.0, 1.0); 232 * @endcode 233 * @endcode 233 * 234 * 234 * The basic units in GEANT4 are: 235 * The basic units in GEANT4 are: 235 * @code 236 * @code 236 * millimeter (millimeter = 1 237 * millimeter (millimeter = 1.) 237 * nanosecond (nanosecond = 1 238 * nanosecond (nanosecond = 1.) 238 * Mega electron Volt (MeV = 1 239 * Mega electron Volt (MeV = 1.) 239 * positron charge (eplus = 1 240 * positron charge (eplus = 1.) 240 * degree Kelvin (kelvin = 1 241 * degree Kelvin (kelvin = 1.) 241 * the amount of substance (mole = 1 242 * the amount of substance (mole = 1.) 242 * luminous intensity (candela = 1 243 * luminous intensity (candela = 1.) 243 * radian (radian = 1 244 * radian (radian = 1.) 244 * steradian (steradian = 1 245 * steradian (steradian = 1.) 245 * @endcode 246 * @endcode 246 */ 247 */ 247 void setSystemOfUnits(double meter = 1.0, 248 void setSystemOfUnits(double meter = 1.0, 248 double kilogram = 1.0, 249 double kilogram = 1.0, 249 double second = 1.0, 250 double second = 1.0, 250 double ampere = 1.0, 251 double ampere = 1.0, 251 double kelvin = 1.0, 252 double kelvin = 1.0, 252 double mole = 1.0, 253 double mole = 1.0, 253 double candela = 1.0) 254 double candela = 1.0); 254 255 255 private: 256 private: 256 void * p; // 257 void * p; // private data 257 Evaluator(const Evaluator &); // 258 Evaluator(const Evaluator &); // copy constructor is not allowed 258 Evaluator & operator=(const Evaluator &); // 259 Evaluator & operator=(const Evaluator &); // assignment is not allowed 259 }; 260 }; 260 261 261 } // namespace HepTool 262 } // namespace HepTool 262 263 263 #endif /* HEP_EVALUATOR_H */ 264 #endif /* HEP_EVALUATOR_H */ 264 265