CMS 3D CMS Logo

List of all members | Public Member Functions | Private Member Functions | Private Attributes
ClhepEvaluator Class Reference

#include <ClhepEvaluator.h>

Public Member Functions

void clear ()
 
 ClhepEvaluator ()
 
double eval (const std::string &ns, const std::string &expr)
 
double eval (const char *expression)
 evaluations using directly the CLHEP-evaluator More...
 
bool isDefined (const std::string &ns, const std::string &name)
 
void set (const std::string &ns, const std::string &name, const std::string &exprValue)
 
void set (const std::string &name, const std::string &value)
 filling the clhep-implementation of the dictionary More...
 
const std::vector< std::string > & values () const
 
const std::vector< std::string > & variables () const
 access to the clhep-implementation of the dictionary variables More...
 
 ~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_
 

Detailed Description

Definition at line 10 of file ClhepEvaluator.h.

Constructor & Destructor Documentation

ClhepEvaluator::ClhepEvaluator ( )

Definition at line 11 of file ClhepEvaluator.cc.

References MillePedeFileConverter_cfg::e, and evaluator_.

12 {
13  // enable standard mathematical funtions
14  evaluator_.setStdMath();
15 
16  // set Geant4 compatible units
17  evaluator_.setSystemOfUnits(1.e+3, 1./1.60217733e-25, 1.e+9, 1./1.60217733e-10,
18  1.0, 1.0, 1.0);
19 
20  // set some global vars, which are in fact known by Clhep::SystemOfUnits
21  // but are NOT set in CLHEP::Evaluator ...
22  evaluator_.setVariable("mum","1.e-3*mm");
23  evaluator_.setVariable("fm","1.e-15*meter");
24 }
HepTool::Evaluator evaluator_
ClhepEvaluator::~ClhepEvaluator ( )

Definition at line 27 of file ClhepEvaluator.cc.

References clear().

28 {
29  clear();
30 }

Member Function Documentation

void ClhepEvaluator::checkname ( const std::string &  name) const
private

Definition at line 271 of file ClhepEvaluator.cc.

References MillePedeFileConverter_cfg::e, Exception, and AlCaHLTBitMon_QueryRunRegistry::string.

Referenced by set(), and values().

272 {
273  // '[' ']' ' ' ':' are forbidden for names and namespaces of parameters
274  std::string::size_type sz = s.size();
275  while(sz)
276  {
277  --sz;
278  //bool stop = false;
279  switch (s[sz])
280  {
281  case ']':
282  case '[':
283  case ' ':
284  case ':':
285  case '\n':
286  case '\t':
287  // case '.':
288  case '&':
289  case '*':
290  case '+':
291  case '-':
292  case '/':
293  case '^':
294  std::string e = std::string("ClhepEvaluator ERROR: forbidden character '")
295  + s[sz] + std::string("' found in '") + s + std::string("' !");
296  throw cms::Exception("DDException") << e;
297  break;
298  }
299  }
300 }
uint16_t size_type
void ClhepEvaluator::clear ( void  )
double ClhepEvaluator::eval ( const std::string &  ns,
const std::string &  expr 
)

Definition at line 94 of file ClhepEvaluator.cc.

References gather_cfg::cout, evaluator_, mps_fire::i, prepare(), mps_fire::result, AlCaHLTBitMon_QueryRunRegistry::string, and throwex().

Referenced by DDLVector::do_makeDouble(), DDLMap::do_makeDouble(), DDLRotationAndReflection::isLeftHanded(), DDLRotationAndReflection::makeX(), DDLRotationAndReflection::makeY(), DDLRotationAndReflection::makeZ(), DDLPgonGenerator::processElement(), DDLTubs::processElement(), DDLPseudoTrap::processElement(), DDLOrb::processElement(), DDLTorus::processElement(), DDLTrapezoid::processElement(), DDLParallelepiped::processElement(), DDLEllipsoid::processElement(), DDLEllipticalTube::processElement(), DDLSphere::processElement(), DDLAlgorithm::processElement(), DDLSpecPar::processElement(), DDLBox::processElement(), DDLCone::processElement(), DDLElementaryMaterial::processElement(), DDLPolyGenerator::processElement(), DDLDivision::processElement(), DDLRotationAndReflection::processElement(), DDLPosPart::processElement(), DDLBooleanSolid::processElement(), DDLNumeric::processElement(), DDLCompositeMaterial::processElement(), DDLVector::processElement(), DDLMap::processElement(), DDLRotationByAxis::processOne(), regressionTest_first(), regressionTest_setup(), and values().

95 {
96 
97  // eval does not store std::strings in the values_!
98  // eval throws if it can't evaluate!
99  std::string pseudo("(evaluating)");
100  std::string prepared;
101 
102  prepare(ns,pseudo,expr, pseudo,prepared);
103 
104  double result = evaluator_.evaluate(prepared.c_str());
105  if(evaluator_.status()!=HepTool::Evaluator::OK) {
106  std::cout << "expr: " << prepared << std::endl;
107  std::cout << "------";
108  for (int i=0; i<evaluator_.error_position(); ++i) std::cout << "-";
109  std::cout << "^" << std::endl;
110  evaluator_.print_error();
111  throwex(ns,prepared,expr,"can't evaluate: " + expr + std::string("!"));
112  }
113 
114  return result;
115 }
HepTool::Evaluator evaluator_
std::pair< int, edm::FunctionWithDict > OK
Definition: findMethod.cc:136
void throwex(const std::string &ns, const std::string &name, const std::string &expr, const std::string &reason, int idx=0) const
void prepare(const std::string &ns, const std::string &name, const std::string &exprValue, std::string &nameResult, std::string &valResult) const
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 117 of file ClhepEvaluator.cc.

References gather_cfg::cout, evaluator_, mps_fire::i, mps_fire::result, AlCaHLTBitMon_QueryRunRegistry::string, and throwex().

118 {
119  double result = evaluator_.evaluate(expression);
120  if (evaluator_.status()!=HepTool::Evaluator::OK) {
121  std::cout << "expr: " << expression << std::endl;
122  std::cout << "------";
123  for (int i=0; i<evaluator_.error_position(); ++i) std::cout << "-";
124  std::cout << "^" << std::endl;
125  evaluator_.print_error();
126  throwex("",expression,"","can't evaluate: " + std::string(expression) + std::string("!"));
127  }
128  return result;
129 }
HepTool::Evaluator evaluator_
std::pair< int, edm::FunctionWithDict > OK
Definition: findMethod.cc:136
void throwex(const std::string &ns, const std::string &name, const std::string &expr, const std::string &reason, int idx=0) const
bool ClhepEvaluator::isDefined ( const std::string &  ns,
const std::string &  name 
)

Definition at line 131 of file ClhepEvaluator.cc.

References evaluator_, prepare(), and AlCaHLTBitMon_QueryRunRegistry::string.

134 {
135  std::string newVar;
136  std::string newVal;
137  prepare(ns,name,"0", newVar,newVal);
138  return evaluator_.findVariable(newVar.c_str());
139 }
HepTool::Evaluator evaluator_
void prepare(const std::string &ns, const std::string &name, const std::string &exprValue, std::string &nameResult, std::string &valResult) const
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 153 of file ClhepEvaluator.cc.

References training_settings::idx, dataset::name, AlCaHLTBitMon_QueryRunRegistry::string, groupFilesInBlocks::temp, and throwex().

Referenced by eval(), isDefined(), set(), and values().

158 {
159  static const std::string sep("___"); // separator between ns and name
160  // SOME SPAGHETTI CODE ...
161  // break it down into some addional member functions ...
162 
163  // the name and namespaces are not checked for 'forbidden' symbols like [,],: ...
164  nameResult = ns + sep + name;
165 
166  // scan the expression std::string and remove [ ], and insert the current namespace if it's missing
168 
169  // 2 pass for simplicity (which is NOT efficient ...)
170  // pass 1: find variables without namespace, e.g. [abcd], and mark them
171  // pass 2: remove [ ] & ( exchange ':' with '_' | add the namespace at marked variables )
172 
173  std::string::size_type sz = exprValue.size();
175  bool insideBracket = false;
176  bool nsFound = false;
177  int varCount=0; // count the variables from 1,2,3,...
178  std::vector<int> hasNs(1); // marked[i]=1 ... variable number i has a namespace attached with ':'
179 
180  while(idx<sz)
181  {
182  switch(exprValue[idx])
183  {
184  case '[':
185  if (nsFound || insideBracket)
186  { // oops, something went wrong. simply throw!
187  throwex(ns,name,exprValue,
188  "found a ':' outside '[..]' , or too many '[' !",idx);
189  }
190  insideBracket=true;
191  ++varCount;
192  break;
193  case ']':
194  if (!insideBracket)
195  {
196  throwex(ns,name,exprValue,"too many ']' !",idx);
197  }
198  insideBracket=false;
199  if (nsFound)
200  {
201  nsFound=false; // reset
202  hasNs.push_back(1);
203  }
204  else
205  {
206  hasNs.push_back(0);
207  }
208  break;
209  case ':':
210  if ( (!insideBracket) || nsFound )
211  { // oops, a namespace outside [] or a 2nd ':' inside []! !
212  throwex(ns,name,exprValue,
213  "found a ':' outside '[..]' , or multiple ':' inside '[..]'",idx);
214  }
215  nsFound=true;
216  break;
217  default:
218  ;
219  } // switch
220  ++idx;
221  } // while(sz)
222 
223  // status after pass 1 must be: every [ ] is closed and no ':'
224  if ( insideBracket || nsFound ) {
225  throwex(ns,name,exprValue,
226  "'[..]' not closed , or ':' outside of '[..]'",idx);
227  }
228 
229  // Pass 2: now remove all '[' ']', replace ':' or add 'ns' + '_'
230  //sz = exprValue.size();
231  idx=0;
232  varCount=0;
233  //bool ommit = false;
234  while (idx<sz)
235  {
236  switch(exprValue[idx])
237  {
238  case '[':
239  ++varCount;
240  if ( !hasNs[varCount] )
241  {
242  valResult = valResult + ns + sep;
243  }
244  break;
245  case ']':
246  break;
247  case ':':
248  valResult = valResult + sep;
249  break;
250  default:
251  valResult = valResult + exprValue[idx];
252  } // switch
253  ++idx;
254  } // while
255 }
uint16_t size_type
void throwex(const std::string &ns, const std::string &name, const std::string &expr, const std::string &reason, int idx=0) const
void ClhepEvaluator::set ( const std::string &  ns,
const std::string &  name,
const std::string &  exprValue 
)

Definition at line 48 of file ClhepEvaluator.cc.

References checkname(), gather_cfg::cout, dd_exchange_value(), evaluator_, prepare(), AlCaHLTBitMon_QueryRunRegistry::string, throwex(), values_, and variables_.

Referenced by regressionTest_setup(), and DDStreamer::vars_read().

49 {
50  checkname(ns); // fancy characters in ns or name ??
51  checkname(name);
52  std::string newVar;
53  std::string newVal;
54  prepare(ns,name,exprValue,newVar,newVal);
55  evaluator_.setVariable(newVar.c_str(), newVal.c_str());
56  switch(evaluator_.status()) {
57  case HepTool::Evaluator::WARNING_EXISTING_VARIABLE:
58  dd_exchange_value(variables_,values_,newVar,newVal);
59  break;
61  case HepTool::Evaluator::WARNING_EXISTING_FUNCTION:
62  case HepTool::Evaluator::WARNING_BLANK_STRING:
63  variables_.push_back(newVar);
64  values_.push_back(newVal);
65  break;
66  default:
67  std::cout << "set-var: ns=" << ns << " nm=" << name << " val=" << exprValue << std::endl;
68  evaluator_.print_error();
69  throwex(ns,name,exprValue,"can't set parameter !");
70  }
71 }
std::vector< std::string > variables_
std::vector< std::string > values_
HepTool::Evaluator evaluator_
std::pair< int, edm::FunctionWithDict > OK
Definition: findMethod.cc:136
void checkname(const std::string &name) const
void throwex(const std::string &ns, const std::string &name, const std::string &expr, const std::string &reason, int idx=0) const
void prepare(const std::string &ns, const std::string &name, const std::string &exprValue, std::string &nameResult, std::string &valResult) const
void dd_exchange_value(std::vector< std::string > &vars, std::vector< std::string > &vals, const std::string &var, const std::string &val)
void ClhepEvaluator::set ( const std::string &  name,
const std::string &  value 
)

filling the clhep-implementation of the dictionary

Definition at line 73 of file ClhepEvaluator.cc.

References gather_cfg::cout, dd_exchange_value(), evaluator_, throwex(), values_, and variables_.

74 {
75  evaluator_.setVariable(n.c_str(),v.c_str());
76  switch(evaluator_.status()) {
77  case HepTool::Evaluator::WARNING_EXISTING_VARIABLE:
79  break;
81  case HepTool::Evaluator::WARNING_EXISTING_FUNCTION:
82  case HepTool::Evaluator::WARNING_BLANK_STRING:
83  variables_.push_back(n);
84  values_.push_back(v);
85  break;
86  default:
87  std::cout << "set-varname=" << n << " val=" << v << std::endl;
88  evaluator_.print_error();
89  throwex("",n,v,"can't set parameter !");
90  }
91 }
std::vector< std::string > variables_
std::vector< std::string > values_
HepTool::Evaluator evaluator_
std::pair< int, edm::FunctionWithDict > OK
Definition: findMethod.cc:136
void throwex(const std::string &ns, const std::string &name, const std::string &expr, const std::string &reason, int idx=0) const
void dd_exchange_value(std::vector< std::string > &vars, std::vector< std::string > &vals, const std::string &var, const std::string &val)
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 257 of file ClhepEvaluator.cc.

References Exception, and AlCaHLTBitMon_QueryRunRegistry::string.

Referenced by eval(), prepare(), set(), and values().

262 {
263  std::string er = std::string("ClhepEvaluator ERROR: ") + reason + std::string("\n")
264  + std::string(" nmspace=") + ns
265  + std::string("\n varname=") + name
266  + std::string("\n exp=") + expr
267  + std::string("\n at=") + expr.substr(0,idx);
268  throw cms::Exception("DDException") << er;
269 }
const std::vector<std::string>& ClhepEvaluator::values ( ) const
inline
const std::vector<std::string>& ClhepEvaluator::variables ( ) const
inline

access to the clhep-implementation of the dictionary variables

Definition at line 26 of file ClhepEvaluator.h.

References variables_.

Referenced by DDStreamer::vars_write().

26 { return variables_;}
std::vector< std::string > variables_

Member Data Documentation

HepTool::Evaluator ClhepEvaluator::evaluator_
private

Definition at line 53 of file ClhepEvaluator.h.

Referenced by clear(), ClhepEvaluator(), eval(), isDefined(), and set().

std::vector<std::string> ClhepEvaluator::values_
private

Definition at line 55 of file ClhepEvaluator.h.

Referenced by clear(), set(), and values().

std::vector<std::string> ClhepEvaluator::variables_
private

Definition at line 54 of file ClhepEvaluator.h.

Referenced by clear(), set(), and variables().