|
||||
File indexing completed on 2025-01-31 09:19:57
0001 //========================================================================== 0002 // AIDA Detector description implementation 0003 //-------------------------------------------------------------------------- 0004 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN) 0005 // All rights reserved. 0006 // 0007 // For the licensing terms see $DD4hepINSTALL/LICENSE. 0008 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS. 0009 // 0010 //========================================================================== 0011 // -*- C++ -*- 0012 // --------------------------------------------------------------------------- 0013 0014 #ifndef EVALUATOR_DETAIL_EVALUATOR_H 0015 #define EVALUATOR_DETAIL_EVALUATOR_H 0016 0017 #include <ostream> 0018 0019 /// Namespace for the AIDA detector description toolkit 0020 namespace dd4hep { 0021 0022 /// Namespace containing XML tools. 0023 namespace tools { 0024 0025 /// Evaluator of arithmetic expressions with an extendable dictionary. 0026 /** 0027 * Taken from CLHEP 1.9.2.1 0028 * 0029 * Example: 0030 * @code 0031 * #include "XmlTools/Evaluator.h" 0032 * XmlTools::Evaluator eval; 0033 * eval.setStdMath(); 0034 * double res = eval.evaluate("sin(30*degree)"); 0035 * if (eval.status() != XmlTools::Evaluator::OK) eval.print_error(); 0036 * @endcode 0037 * 0038 * @author Evgeni Chernyaev <Evgueni.Tcherniaev@cern.ch> 0039 * @ingroup evaluator 0040 */ 0041 class Evaluator::Object { 0042 public: 0043 0044 /** 0045 * List of possible statuses. 0046 * Status of the last operation can be obtained with status(). 0047 * In case if status() is an ERROR the corresponding error message 0048 * can be printed with print_error(). 0049 * 0050 * @see status 0051 * @see error_position 0052 * @see print_error 0053 */ 0054 struct EvalStatus { 0055 /** 0056 * Returns status of the last operation with the evaluator. 0057 */ 0058 int status() const; 0059 0060 /** 0061 * Returns result of the last operation with the evaluator. 0062 */ 0063 double result() const; 0064 0065 /** 0066 * Returns position in the input string where the problem occured. 0067 */ 0068 int error_position(const char* expression) const; 0069 0070 /** 0071 * Prints error message if status() is an ERROR. 0072 */ 0073 void print_error(std::ostream& os) const; 0074 0075 /** 0076 * Prints error message if status() is an ERROR using std::cerr. 0077 */ 0078 void print_error() const; 0079 0080 char const* thePosition = 0; 0081 int theStatus = OK; 0082 double theResult = 0.0; 0083 }; 0084 0085 /** 0086 * Constructor. 0087 */ 0088 0089 /** 0090 * Sets system of units. Default is the SI system of units. 0091 * To set the CGS (Centimeter-Gram-Second) system of units 0092 * one should call: 0093 * Object o(100., 1000., 1.0, 1.0, 1.0, 1.0, 1.0); 0094 * 0095 * To set system of units accepted in the GEANT4 simulation toolkit 0096 * one should call: 0097 * @code 0098 * setSystemOfUnits(1.e+3, 1./1.60217733e-25, 1.e+9, 1./1.60217733e-10, 0099 * 1.0, 1.0, 1.0); 0100 * @endcode 0101 * 0102 * The basic units in GEANT4 are: 0103 * @code 0104 * millimeter (millimeter = 1.) 0105 * nanosecond (nanosecond = 1.) 0106 * Mega electron Volt (MeV = 1.) 0107 * positron charge (eplus = 1.) 0108 * degree Kelvin (kelvin = 1.) 0109 * the amount of substance (mole = 1.) 0110 * luminous intensity (candela = 1.) 0111 * radian (radian = 1.) 0112 * steradian (steradian = 1.) 0113 * @endcode 0114 */ 0115 0116 Object(double meter = 1.0, double kilogram = 1.0, double second = 1.0, double ampere = 1.0, double kelvin = 0117 1.0, double mole = 1.0, double candela = 1.0, double radians = 1.0 ); 0118 0119 /** 0120 * Destructor. 0121 */ 0122 ~Object(); 0123 0124 /** 0125 * Evaluates the arithmetic expression given as character string. 0126 * The expression may consist of numbers, variables and functions 0127 * separated by arithmetic (+, - , /, *, ^, **) and logical 0128 * operators (==, !=, >, >=, <, <=, &&, ||). 0129 * 0130 * @param expression input expression. 0131 * @return result of the evaluation. 0132 * @see status 0133 * @see error_position 0134 * @see print_error 0135 */ 0136 EvalStatus evaluate(const char* expression) const; 0137 0138 /** 0139 * Adds to the dictionary a string constant 0140 * 0141 * @param name name of the variable. 0142 * @param value value assigned to the variable. 0143 * @return returns status 0144 */ 0145 int setEnviron(const char* name, const char* value); 0146 0147 /** 0148 * Lookup the dictionary for a string constant 0149 * 0150 * @param name name of the variable. 0151 */ 0152 std::pair<const char*, int> getEnviron(const char* name) const; 0153 0154 /** 0155 * Adds to the dictionary a variable with given value. 0156 * If a variable with such a name already exist in the dictionary, 0157 * then status will be set to WARNING_EXISTING_VARIABLE. 0158 * 0159 * @param name name of the variable. 0160 * @param value value assigned to the variable. 0161 */ 0162 int setVariable(const char* name, double value); 0163 0164 /** 0165 * Adds to the dictionary a variable with an arithmetic expression 0166 * assigned to it. 0167 * If a variable with such a name already exist in the dictionary, 0168 * then status will be set to WARNING_EXISTING_VARIABLE. 0169 * 0170 * @param name name of the variable. 0171 * @param expression arithmetic expression. 0172 */ 0173 int setVariable(const char* name, const char* expression); 0174 0175 /** 0176 * Adds to the dictionary a function without parameters. 0177 * If such a function already exist in the dictionary, 0178 * then status will be set to WARNING_EXISTING_FUNCTION. 0179 * 0180 * @param name function name. 0181 * @param fun pointer to the real function in the user code. 0182 */ 0183 int setFunction(const char* name, double (*fun)()); 0184 0185 /** 0186 * Adds to the dictionary a function with one parameter. 0187 * If such a function already exist in the dictionary, 0188 * then status will be set to WARNING_EXISTING_FUNCTION. 0189 * 0190 * @param name function name. 0191 * @param fun pointer to the real function in the user code. 0192 */ 0193 int setFunction(const char* name, double (*fun)(double)); 0194 0195 /** 0196 * Adds to the dictionary a function with two parameters. 0197 * If such a function already exist in the dictionary, 0198 * then status will be set to WARNING_EXISTING_FUNCTION. 0199 * 0200 * @param name function name. 0201 * @param fun pointer to the real function in the user code. 0202 */ 0203 int setFunction(const char* name, double (*fun)(double, double)); 0204 0205 /** 0206 * Adds to the dictionary a function with three parameters. 0207 * If such a function already exist in the dictionary, 0208 * then status will be set to WARNING_EXISTING_FUNCTION. 0209 * 0210 * @param name function name. 0211 * @param fun pointer to the real function in the user code. 0212 */ 0213 int setFunction(const char* name, double (*fun)(double, double, double)); 0214 0215 /** 0216 * Adds to the dictionary a function with four parameters. 0217 * If such a function already exist in the dictionary, 0218 * then status will be set to WARNING_EXISTING_FUNCTION. 0219 * 0220 * @param name function name. 0221 * @param fun pointer to the real function in the user code. 0222 */ 0223 int setFunction(const char* name, double (*fun)(double, double, double, double)); 0224 0225 /** 0226 * Adds to the dictionary a function with five parameters. 0227 * If such a function already exist in the dictionary, 0228 * then status will be set to WARNING_EXISTING_FUNCTION. 0229 * 0230 * @param name function name. 0231 * @param fun pointer to the real function in the user code. 0232 */ 0233 int setFunction(const char* name, double (*fun)(double, double, double, double, double)); 0234 0235 /** 0236 * Finds the variable in the dictionary. 0237 * 0238 * @param name name of the variable. 0239 * @return true if such a variable exists, false otherwise. 0240 */ 0241 bool findVariable(const char* name) const; 0242 0243 /** 0244 * Finds the function in the dictionary. 0245 * 0246 * @param name name of the function to be unset. 0247 * @param npar number of parameters of the function. 0248 * @return true if such a function exists, false otherwise. 0249 */ 0250 bool findFunction(const char* name, int npar) const; 0251 0252 /** 0253 * Removes the variable from the dictionary. 0254 * 0255 * @param name name of the variable. 0256 */ 0257 void removeVariable(const char* name); 0258 0259 /** 0260 * Removes the function from the dictionary. 0261 * 0262 * @param name name of the function to be unset. 0263 * @param npar number of parameters of the function. 0264 */ 0265 void removeFunction(const char* name, int npar); 0266 0267 /** 0268 * Clear all settings. 0269 */ 0270 void clear(); 0271 0272 struct Struct; 0273 0274 private: 0275 /** 0276 * Sets system of units. Default is the SI system of units. 0277 * To set the CGS (Centimeter-Gram-Second) system of units 0278 * one should call: 0279 * setSystemOfUnits(100., 1000., 1.0, 1.0, 1.0, 1.0, 1.0); 0280 * 0281 * To set system of units accepted in the GEANT4 simulation toolkit 0282 * one should call: 0283 * @code 0284 * setSystemOfUnits(1.e+3, 1./1.60217733e-25, 1.e+9, 1./1.60217733e-10, 0285 * 1.0, 1.0, 1.0); 0286 * @endcode 0287 * 0288 * The basic units in GEANT4 are: 0289 * @code 0290 * millimeter (millimeter = 1.) 0291 * nanosecond (nanosecond = 1.) 0292 * Mega electron Volt (MeV = 1.) 0293 * positron charge (eplus = 1.) 0294 * degree Kelvin (kelvin = 1.) 0295 * the amount of substance (mole = 1.) 0296 * luminous intensity (candela = 1.) 0297 * radian (radian = 1.) 0298 * steradian (steradian = 1.) 0299 * @endcode 0300 */ 0301 void setSystemOfUnits(double meter, double kilogram, double second, double ampere, double kelvin 0302 , double mole, double candela, double radians ); 0303 0304 /** 0305 * Sets standard mathematical functions and constants. 0306 */ 0307 void setStdMath(); 0308 0309 /** 0310 * Used only during construction 0311 */ 0312 void setVariableNoLock(const char* name, double value); 0313 void setFunctionNoLock(const char* name, double (*fun)(double)); 0314 void setFunctionNoLock(const char* name, double (*fun)(double, double)); 0315 0316 0317 Struct* imp {0}; // private data 0318 Object(const Object &) = delete; // copy constructor is not allowed 0319 Object & operator=(const Object &) = delete; // assignment is not allowed 0320 }; 0321 0322 } // namespace tools 0323 } // namespace dd4hep 0324 0325 #endif // EVALUATOR_DETAIL_EVALUATOR_H
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |