#include <DetectorDescription/ExprAlgo/interface/ClhepEvaluator.h>
Public Member Functions | |
void | clear () |
access to the dictionary (namespace,name)->value | |
ClhepEvaluator () | |
double | eval (const char *expression) |
evaluations using directly the CLHEP-evaluator | |
double | eval (const std::string &ns, const std::string &expr) |
evaluate an expression expr inside the local namespace | |
bool | isDefined (const std::string &ns, const std::string &name) |
check whether a variable is already defined or not | |
void | set (const std::string &name, const std::string &value) |
filling the clhep-implementation of the dictionary | |
void | set (const std::string &ns, const std::string &name, const std::string &exprValue) |
put a new variable named 'namespace:name' into the dictionary of the evaluator | |
const std::vector< std::string > & | values () const |
const std::vector< std::string > & | variables () const |
access to the clhep-implementation of the dictionary variables | |
~ClhepEvaluator () | |
Private Member Functions | |
void | checkname (const std::string &name) const |
void | prepare (const std::string &ns, const std::string &name, const std::string &exprValue, std::string &nameResult, std::string &valResult) const |
void | throwex (const std::string &ns, const std::string &name, const std::string &expr, const std::string &reason, int idx=0) const |
Private Attributes | |
HepTool::Evaluator | evaluator_ |
std::vector< std::string > | values_ |
std::vector< std::string > | variables_ |
Definition at line 11 of file ClhepEvaluator.h.
ClhepEvaluator::ClhepEvaluator | ( | ) |
Definition at line 6 of file ClhepEvaluator.cc.
References e, and evaluator_.
00007 { 00008 // enable standard mathematical funtions 00009 evaluator_.setStdMath(); 00010 00011 // set Geant4 compatible units 00012 evaluator_.setSystemOfUnits(1.e+3, 1./1.60217733e-25, 1.e+9, 1./1.60217733e-10, 00013 1.0, 1.0, 1.0); 00014 00015 // set some global vars, which are in fact known by Clhep::SystemOfUnits 00016 // but are NOT set in CLHEP::Evaluator ... 00017 evaluator_.setVariable("mum","1.e-3*mm"); 00018 evaluator_.setVariable("fm","1.e-15*meter"); 00019 }
ClhepEvaluator::~ClhepEvaluator | ( | ) |
void ClhepEvaluator::checkname | ( | const std::string & | name | ) | const [private] |
Definition at line 274 of file ClhepEvaluator.cc.
References e.
Referenced by set().
00275 { 00276 // '[' ']' ' ' ':' are forbidden for names and namespaces of parameters 00277 std::string::size_type sz = s.size(); 00278 while(sz) { 00279 --sz; 00280 //bool stop = false; 00281 switch (s[sz]) { 00282 case ']': 00283 case '[': 00284 case ' ': 00285 case ':': 00286 case '\n': 00287 case '\t': 00288 // case '.': 00289 case '&': 00290 case '*': 00291 case '+': 00292 case '-': 00293 case '/': 00294 case '^': 00295 std::string e = std::string("ClhepEvaluator ERROR: forbidden character '") 00296 + s[sz] + std::string("' found in '") + s + std::string("' !"); 00297 throw DDException(e); 00298 break; 00299 } 00300 } 00301 00302 }
void ClhepEvaluator::clear | ( | ) | [virtual] |
access to the dictionary (namespace,name)->value
if not implemented in a sub-class it returns 0 and does nothing to result clear the dictionary of the evaluator
Implements ExprEvalInterface.
Definition at line 141 of file ClhepEvaluator.cc.
References evaluator_, values_, and variables_.
Referenced by ~ClhepEvaluator().
00142 { 00143 // clear the dictionary 00144 evaluator_.clear(); 00145 00146 // clear the cache of values & variable-names 00147 variables_.clear(); 00148 values_.clear(); 00149 }
double ClhepEvaluator::eval | ( | const char * | expression | ) |
evaluations using directly the CLHEP-evaluator
expression must be an expression compatible with the CLHEP-Evaluator syntax
Definition at line 116 of file ClhepEvaluator.cc.
References GenMuonPlsPt100GeV_cfg::cout, lat::endl(), evaluator_, i, HLT_VtxMuL3::result, and throwex().
00117 { 00118 double result = evaluator_.evaluate(expression); 00119 if (evaluator_.status()!=HepTool::Evaluator::OK) { 00120 std::cout << "expr: " << expression << std::endl; 00121 std::cout << "------"; 00122 for (int i=0; i<evaluator_.error_position(); ++i) std::cout << "-"; 00123 std::cout << "^" << std::endl; 00124 evaluator_.print_error(); 00125 throwex("",expression,"","can't evaluate: " + std::string(expression) + std::string("!")); 00126 } 00127 return result; 00128 }
double ClhepEvaluator::eval | ( | const std::string & | ns, | |
const std::string & | expr | |||
) | [virtual] |
evaluate an expression expr inside the local namespace
Implements ExprEvalInterface.
Definition at line 93 of file ClhepEvaluator.cc.
References GenMuonPlsPt100GeV_cfg::cout, lat::endl(), evaluator_, i, prepare(), HLT_VtxMuL3::result, and throwex().
Referenced by DDConstant::createConstantsFromEvaluator().
00094 { 00095 00096 // eval does not store std::strings in the values_! 00097 // eval throws if it can't evaluate! 00098 std::string pseudo("(evaluating)"); 00099 std::string prepared; 00100 00101 prepare(ns,pseudo,expr, pseudo,prepared); 00102 00103 double result = evaluator_.evaluate(prepared.c_str()); 00104 if(evaluator_.status()!=HepTool::Evaluator::OK) { 00105 std::cout << "expr: " << prepared << std::endl; 00106 std::cout << "------"; 00107 for (int i=0; i<evaluator_.error_position(); ++i) std::cout << "-"; 00108 std::cout << "^" << std::endl; 00109 evaluator_.print_error(); 00110 throwex(ns,prepared,expr,"can't evaluate: " + expr + std::string("!")); 00111 } 00112 00113 return result; 00114 }
bool ClhepEvaluator::isDefined | ( | const std::string & | ns, | |
const std::string & | name | |||
) | [virtual] |
check whether a variable is already defined or not
Implements ExprEvalInterface.
Definition at line 130 of file ClhepEvaluator.cc.
References evaluator_, and prepare().
00133 { 00134 std::string newVar; 00135 std::string newVal; 00136 prepare(ns,name,"0", newVar,newVal); 00137 return evaluator_.findVariable(newVar.c_str()); 00138 }
void ClhepEvaluator::prepare | ( | const std::string & | ns, | |
const std::string & | name, | |||
const std::string & | exprValue, | |||
std::string & | nameResult, | |||
std::string & | valResult | |||
) | const [private] |
Definition at line 152 of file ClhepEvaluator.cc.
References pyDBSRunClass::temp, and throwex().
Referenced by eval(), isDefined(), and set().
00157 { 00158 static const std::string sep("___"); // separator between ns and name 00159 // SOME SPAGHETTI CODE ... 00160 // break it down into some addional member functions ... 00161 00162 // the name and namespaces are not checked for 'forbidden' symbols like [,],: ... 00163 nameResult = ns + sep + name; 00164 00165 // scan the expression std::string and remove [ ], and insert the current namespace if it's missing 00166 std::string temp; 00167 00168 // 2 pass for simplicity (which is NOT efficient ...) 00169 // pass 1: find variables without namespace, e.g. [abcd], and mark them 00170 // pass 2: remove [ ] & ( exchange ':' with '_' | add the namespace at marked variables ) 00171 00172 std::string::size_type sz = exprValue.size(); 00173 std::string::size_type idx =0; 00174 bool insideBracket = false; 00175 bool nsFound = false; 00176 int varCount=0; // count the variables from 1,2,3,... 00177 std::vector<int> hasNs(1); // marked[i]=1 ... variable number i has a namespace attached with ':' 00178 00179 while(idx<sz) { 00180 switch(exprValue[idx]) { 00181 00182 case '[': 00183 if (nsFound || insideBracket) { // oops, something went wrong. simply throw! 00184 throwex(ns,name,exprValue, 00185 "found a ':' outside '[..]' , or too many '[' !",idx); 00186 } 00187 insideBracket=true; 00188 ++varCount; 00189 break; 00190 00191 case ']': 00192 if (!insideBracket) { 00193 throwex(ns,name,exprValue,"too many ']' !",idx); 00194 } 00195 insideBracket=false; 00196 if (nsFound) { 00197 nsFound=false; // reset 00198 hasNs.push_back(1); 00199 } 00200 else { 00201 hasNs.push_back(0); 00202 } 00203 break; 00204 00205 case ':': 00206 if ( (!insideBracket) || nsFound ) { // oops, a namespace outside [] or a 2nd ':' inside []! ! 00207 throwex(ns,name,exprValue, 00208 "found a ':' outside '[..]' , or multiple ':' inside '[..]'",idx); 00209 } 00210 nsFound=true; 00211 break; 00212 00213 default: 00214 ; 00215 } // switch 00216 00217 ++idx; 00218 } // while(sz) 00219 00220 // status after pass 1 must be: every [ ] is closed and no ':' 00221 if ( insideBracket || nsFound ) { 00222 throwex(ns,name,exprValue, 00223 "'[..]' not closed , or ':' outside of '[..]'",idx); 00224 } 00225 00226 // Pass 2: now remove all '[' ']', replace ':' or add 'ns' + '_' 00227 //sz = exprValue.size(); 00228 idx=0; 00229 varCount=0; 00230 //bool ommit = false; 00231 while (idx<sz) { 00232 switch(exprValue[idx]) { 00233 00234 case '[': 00235 ++varCount; 00236 if ( !hasNs[varCount] ) { 00237 valResult = valResult + ns + sep; 00238 } 00239 break; 00240 00241 case ']': 00242 break; 00243 00244 case ':': 00245 valResult = valResult + sep; 00246 break; 00247 00248 default: 00249 valResult = valResult + exprValue[idx]; 00250 } // switch 00251 00252 ++idx; 00253 } // while 00254 }
void ClhepEvaluator::set | ( | const std::string & | name, | |
const std::string & | value | |||
) |
filling the clhep-implementation of the dictionary
Definition at line 72 of file ClhepEvaluator.cc.
References GenMuonPlsPt100GeV_cfg::cout, dd_exchange_value(), lat::endl(), evaluator_, throwex(), values_, and variables_.
00073 { 00074 evaluator_.setVariable(n.c_str(),v.c_str()); 00075 switch(evaluator_.status()) { 00076 case HepTool::Evaluator::WARNING_EXISTING_VARIABLE: 00077 dd_exchange_value(variables_,values_,n,v); 00078 break; 00079 case HepTool::Evaluator::OK: 00080 case HepTool::Evaluator::WARNING_EXISTING_FUNCTION: 00081 case HepTool::Evaluator::WARNING_BLANK_STRING: 00082 variables_.push_back(n); 00083 values_.push_back(v); 00084 break; 00085 default: 00086 std::cout << "set-varname=" << n << " val=" << v << std::endl; 00087 evaluator_.print_error(); 00088 throwex("",n,v,"can't set parameter !"); 00089 } 00090 }
void ClhepEvaluator::set | ( | const std::string & | ns, | |
const std::string & | name, | |||
const std::string & | valueExpr | |||
) | [virtual] |
put a new variable named 'namespace:name' into the dictionary of the evaluator
Implements ExprEvalInterface.
Definition at line 43 of file ClhepEvaluator.cc.
References checkname(), GenMuonPlsPt100GeV_cfg::cout, DCOUT_V, dd_exchange_value(), lat::endl(), evaluator_, prepare(), throwex(), values_, and variables_.
Referenced by DDStreamer::vars_read().
00044 { 00045 checkname(ns); // fancy characters in ns or name ?? 00046 checkname(name); 00047 std::string newVar; 00048 std::string newVal; 00049 prepare(ns,name,exprValue,newVar,newVal); 00050 DCOUT_V('C', "ClhepEvaluator: " 00051 << " in: " << ns << " " << name << " " << exprValue 00052 << " pr: " << newVar << " " << newVal); 00053 //set(newVar,newVal); 00054 evaluator_.setVariable(newVar.c_str(), newVal.c_str()); 00055 switch(evaluator_.status()) { 00056 case HepTool::Evaluator::WARNING_EXISTING_VARIABLE: 00057 dd_exchange_value(variables_,values_,newVar,newVal); 00058 break; 00059 case HepTool::Evaluator::OK: 00060 case HepTool::Evaluator::WARNING_EXISTING_FUNCTION: 00061 case HepTool::Evaluator::WARNING_BLANK_STRING: 00062 variables_.push_back(newVar); 00063 values_.push_back(newVal); 00064 break; 00065 default: 00066 std::cout << "set-var: ns=" << ns << " nm=" << name << " val=" << exprValue << std::endl; 00067 evaluator_.print_error(); 00068 throwex(ns,name,exprValue,"can't set parameter !"); 00069 } 00070 }
void ClhepEvaluator::throwex | ( | const std::string & | ns, | |
const std::string & | name, | |||
const std::string & | expr, | |||
const std::string & | reason, | |||
int | idx = 0 | |||
) | const [private] |
Definition at line 258 of file ClhepEvaluator.cc.
Referenced by eval(), prepare(), and set().
00263 { 00264 std::string er = std::string("ClhepEvaluator ERROR: ") + reason + std::string("\n") 00265 + std::string(" nmspace=") + ns 00266 + std::string("\n varname=") + name 00267 + std::string("\n exp=") + expr 00268 + std::string("\n at=") + expr.substr(0,idx); 00269 throw DDException(er); 00270 00271 }
const std::vector<std::string>& ClhepEvaluator::values | ( | ) | const [inline] |
Definition at line 28 of file ClhepEvaluator.h.
References values_.
Referenced by DDConstant::createConstantsFromEvaluator(), and DDStreamer::vars_write().
00028 { return values_;}
const std::vector<std::string>& ClhepEvaluator::variables | ( | ) | const [inline] |
access to the clhep-implementation of the dictionary variables
Definition at line 27 of file ClhepEvaluator.h.
References variables_.
Referenced by DDConstant::createConstantsFromEvaluator(), and DDStreamer::vars_write().
00027 { return variables_;}
HepTool::Evaluator ClhepEvaluator::evaluator_ [private] |
Definition at line 54 of file ClhepEvaluator.h.
Referenced by clear(), ClhepEvaluator(), eval(), isDefined(), and set().
std::vector<std::string> ClhepEvaluator::values_ [private] |
std::vector<std::string> ClhepEvaluator::variables_ [private] |