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 8 of file ClhepEvaluator.h.

Constructor & Destructor Documentation

◆ ClhepEvaluator()

ClhepEvaluator::ClhepEvaluator ( )

Definition at line 11 of file ClhepEvaluator.cc.

References MillePedeFileConverter_cfg::e, and evaluator_.

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

◆ ~ClhepEvaluator()

ClhepEvaluator::~ClhepEvaluator ( )

Definition at line 24 of file ClhepEvaluator.cc.

References clear().

24 { clear(); }

Member Function Documentation

◆ checkname()

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

Definition at line 235 of file ClhepEvaluator.cc.

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

Referenced by set().

235  {
236  // '[' ']' ' ' ':' are forbidden for names and namespaces of parameters
237  std::string::size_type sz = s.size();
238  while (sz) {
239  --sz;
240  //bool stop = false;
241  switch (s[sz]) {
242  case ']':
243  case '[':
244  case ' ':
245  case ':':
246  case '\n':
247  case '\t':
248  // case '.':
249  case '&':
250  case '*':
251  case '+':
252  case '-':
253  case '/':
254  case '^':
255  std::string e = std::string("ClhepEvaluator ERROR: forbidden character '") + s[sz] +
256  std::string("' found in '") + s + std::string("' !");
257  throw cms::Exception("DDException") << e;
258  break;
259  }
260  }
261 }
uint16_t size_type

◆ clear()

void ClhepEvaluator::clear ( void  )

Definition at line 130 of file ClhepEvaluator.cc.

References evaluator_, values_, and variables_.

Referenced by ~ClhepEvaluator().

130  {
131  // clear the dictionary
132  evaluator_.clear();
133 
134  // clear the cache of values & variable-names
135  variables_.clear();
136  values_.clear();
137 }
std::vector< std::string > variables_
std::vector< std::string > values_
HepTool::Evaluator evaluator_

◆ eval() [1/2]

double ClhepEvaluator::eval ( const std::string &  ns,
const std::string &  expr 
)

Definition at line 85 of file ClhepEvaluator.cc.

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

Referenced by DDConstant::createConstantsFromEvaluator(), DDLVector::do_makeDouble(), DDLMap::do_makeDouble(), DDLAlgorithm::processElement(), DDLSpecPar::processElement(), DDLNumeric::processElement(), DDLVector::processElement(), DDLMap::processElement(), regressionTest_first(), and regressionTest_setup().

85  {
86  // eval does not store std::strings in the values_!
87  // eval throws if it can't evaluate!
88  std::string pseudo("(evaluating)");
89  std::string prepared;
90 
91  prepare(ns, pseudo, expr, pseudo, prepared);
92 
93  double result = evaluator_.evaluate(prepared.c_str());
94  if (evaluator_.status() != HepTool::Evaluator::OK) {
95  std::cout << "expr: " << prepared << std::endl;
96  std::cout << "------";
97  for (int i = 0; i < evaluator_.error_position(); ++i)
98  std::cout << "-";
99  std::cout << "^" << std::endl;
100  evaluator_.print_error();
101  throwex(ns, prepared, expr, "can't evaluate: " + expr + std::string("!"));
102  }
103 
104  return result;
105 }
void prepare(const std::string &ns, const std::string &name, const std::string &exprValue, std::string &nameResult, std::string &valResult) const
HepTool::Evaluator evaluator_
std::pair< int, edm::FunctionWithDict > OK
Definition: findMethod.cc:126
void throwex(const std::string &ns, const std::string &name, const std::string &expr, const std::string &reason, int idx=0) const

◆ eval() [2/2]

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 107 of file ClhepEvaluator.cc.

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

107  {
108  double result = evaluator_.evaluate(expression);
109  if (evaluator_.status() != HepTool::Evaluator::OK) {
110  std::cout << "expr: " << expression << std::endl;
111  std::cout << "------";
112  for (int i = 0; i < evaluator_.error_position(); ++i)
113  std::cout << "-";
114  std::cout << "^" << std::endl;
115  evaluator_.print_error();
116  throwex("", expression, "", "can't evaluate: " + std::string(expression) + std::string("!"));
117  }
118  return result;
119 }
HepTool::Evaluator evaluator_
std::pair< int, edm::FunctionWithDict > OK
Definition: findMethod.cc:126
void throwex(const std::string &ns, const std::string &name, const std::string &expr, const std::string &reason, int idx=0) const

◆ isDefined()

bool ClhepEvaluator::isDefined ( const std::string &  ns,
const std::string &  name 
)

Definition at line 121 of file ClhepEvaluator.cc.

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

123  {
124  std::string newVar;
125  std::string newVal;
126  prepare(ns, name, "0", newVar, newVal);
127  return evaluator_.findVariable(newVar.c_str());
128 }
void prepare(const std::string &ns, const std::string &name, const std::string &exprValue, std::string &nameResult, std::string &valResult) const
HepTool::Evaluator evaluator_

◆ prepare()

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 139 of file ClhepEvaluator.cc.

References heavyIonCSV_trainingSettings::idx, Skims_PA_cff::name, AlCaHLTBitMon_QueryRunRegistry::string, groupFilesInBlocks::temp, and throwex().

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

143  {
144  static const std::string sep("___"); // separator between ns and name
145  // SOME SPAGHETTI CODE ...
146  // break it down into some addional member functions ...
147 
148  // the name and namespaces are not checked for 'forbidden' symbols like [,],: ...
149  nameResult = ns + sep + name;
150 
151  // scan the expression std::string and remove [ ], and insert the current namespace if it's missing
153 
154  // 2 pass for simplicity (which is NOT efficient ...)
155  // pass 1: find variables without namespace, e.g. [abcd], and mark them
156  // pass 2: remove [ ] & ( exchange ':' with '_' | add the namespace at marked variables )
157 
158  std::string::size_type sz = exprValue.size();
160  bool insideBracket = false;
161  bool nsFound = false;
162  int varCount = 0; // count the variables from 1,2,3,...
163  std::vector<int> hasNs(1); // marked[i]=1 ... variable number i has a namespace attached with ':'
164 
165  while (idx < sz) {
166  switch (exprValue[idx]) {
167  case '[':
168  if (nsFound || insideBracket) { // oops, something went wrong. simply throw!
169  throwex(ns, name, exprValue, "found a ':' outside '[..]' , or too many '[' !", idx);
170  }
171  insideBracket = true;
172  ++varCount;
173  break;
174  case ']':
175  if (!insideBracket) {
176  throwex(ns, name, exprValue, "too many ']' !", idx);
177  }
178  insideBracket = false;
179  if (nsFound) {
180  nsFound = false; // reset
181  hasNs.emplace_back(1);
182  } else {
183  hasNs.emplace_back(0);
184  }
185  break;
186  case ':':
187  if ((!insideBracket) || nsFound) { // oops, a namespace outside [] or a 2nd ':' inside []! !
188  throwex(ns, name, exprValue, "found a ':' outside '[..]' , or multiple ':' inside '[..]'", idx);
189  }
190  nsFound = true;
191  break;
192  default:;
193  } // switch
194  ++idx;
195  } // while(sz)
196 
197  // status after pass 1 must be: every [ ] is closed and no ':'
198  if (insideBracket || nsFound) {
199  throwex(ns, name, exprValue, "'[..]' not closed , or ':' outside of '[..]'", idx);
200  }
201 
202  // Pass 2: now remove all '[' ']', replace ':' or add 'ns' + '_'
203  //sz = exprValue.size();
204  idx = 0;
205  varCount = 0;
206  //bool ommit = false;
207  while (idx < sz) {
208  switch (exprValue[idx]) {
209  case '[':
210  ++varCount;
211  if (!hasNs[varCount]) {
212  valResult = valResult + ns + sep;
213  }
214  break;
215  case ']':
216  break;
217  case ':':
218  valResult = valResult + sep;
219  break;
220  default:
221  valResult = valResult + exprValue[idx];
222  } // switch
223  ++idx;
224  } // while
225 }
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

◆ set() [1/2]

void ClhepEvaluator::set ( const std::string &  ns,
const std::string &  name,
const std::string &  exprValue 
)

Definition at line 42 of file ClhepEvaluator.cc.

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

Referenced by regressionTest_setup().

42  {
43  checkname(ns); // fancy characters in ns or name ??
44  checkname(name);
45  std::string newVar;
46  std::string newVal;
47  prepare(ns, name, exprValue, newVar, newVal);
48  evaluator_.setVariable(newVar.c_str(), newVal.c_str());
49  switch (evaluator_.status()) {
50  case HepTool::Evaluator::WARNING_EXISTING_VARIABLE:
51  dd_exchange_value(variables_, values_, newVar, newVal);
52  break;
54  case HepTool::Evaluator::WARNING_EXISTING_FUNCTION:
55  case HepTool::Evaluator::WARNING_BLANK_STRING:
56  variables_.emplace_back(newVar);
57  values_.emplace_back(newVal);
58  break;
59  default:
60  std::cout << "set-var: ns=" << ns << " nm=" << name << " val=" << exprValue << std::endl;
61  evaluator_.print_error();
62  throwex(ns, name, exprValue, "can't set parameter !");
63  }
64 }
std::vector< std::string > variables_
void prepare(const std::string &ns, const std::string &name, const std::string &exprValue, std::string &nameResult, std::string &valResult) const
void checkname(const std::string &name) const
std::vector< std::string > values_
HepTool::Evaluator evaluator_
std::pair< int, edm::FunctionWithDict > OK
Definition: findMethod.cc:126
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)

◆ set() [2/2]

void ClhepEvaluator::set ( const std::string &  name,
const std::string &  value 
)

filling the clhep-implementation of the dictionary

Definition at line 66 of file ClhepEvaluator.cc.

References gather_cfg::cout, dd_exchange_value(), evaluator_, dqmiodumpmetadata::n, throwex(), findQualityFiles::v, values_, and variables_.

66  {
67  evaluator_.setVariable(n.c_str(), v.c_str());
68  switch (evaluator_.status()) {
69  case HepTool::Evaluator::WARNING_EXISTING_VARIABLE:
71  break;
73  case HepTool::Evaluator::WARNING_EXISTING_FUNCTION:
74  case HepTool::Evaluator::WARNING_BLANK_STRING:
75  variables_.emplace_back(n);
76  values_.emplace_back(v);
77  break;
78  default:
79  std::cout << "set-varname=" << n << " val=" << v << std::endl;
80  evaluator_.print_error();
81  throwex("", n, v, "can't set parameter !");
82  }
83 }
std::vector< std::string > variables_
std::vector< std::string > values_
HepTool::Evaluator evaluator_
std::pair< int, edm::FunctionWithDict > OK
Definition: findMethod.cc:126
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)

◆ throwex()

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 227 of file ClhepEvaluator.cc.

References Exception, electrons_cff::expr, heavyIonCSV_trainingSettings::idx, Skims_PA_cff::name, PixelMapPlotter::reason, and AlCaHLTBitMon_QueryRunRegistry::string.

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

228  {
229  std::string er = std::string("ClhepEvaluator ERROR: ") + reason + std::string("\n") + std::string(" nmspace=") + ns +
230  std::string("\n varname=") + name + std::string("\n exp=") + expr + std::string("\n at=") +
231  expr.substr(0, idx);
232  throw cms::Exception("DDException") << er;
233 }

◆ values()

const std::vector<std::string>& ClhepEvaluator::values ( ) const
inline

Definition at line 24 of file ClhepEvaluator.h.

References values_.

Referenced by DDConstant::createConstantsFromEvaluator().

24 { return values_; }
std::vector< std::string > values_

◆ variables()

const std::vector<std::string>& ClhepEvaluator::variables ( ) const
inline

access to the clhep-implementation of the dictionary variables

Definition at line 23 of file ClhepEvaluator.h.

References variables_.

Referenced by DDConstant::createConstantsFromEvaluator().

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

Member Data Documentation

◆ evaluator_

HepTool::Evaluator ClhepEvaluator::evaluator_
private

Definition at line 50 of file ClhepEvaluator.h.

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

◆ values_

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

Definition at line 52 of file ClhepEvaluator.h.

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

◆ variables_

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

Definition at line 51 of file ClhepEvaluator.h.

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