Belle II Software  release-05-02-19
VariableFormulaConstructor.h
1 #include <analysis/VariableManager/Manager.h>
2 #include <framework/utilities/FormulaParser.h>
3 
4 namespace Belle2 {
18  type operator()(const std::string& name)
19  {
20  auto var = Variable::Manager::Instance().getVariable(name);
21  if (!var) throw std::runtime_error("Could not find " + name + " via the Variable::Manager. Check the name");
22  return var->function;
23  }
24 
26  type operator()(double value)
27  {
28  return [value](const Particle*) { return value; };
29  }
30 
32  type operator()(Op op, const type& a, const type& b)
33  {
34  return [op, a, b](const Particle * p) {
35  switch (op) {
36  case Op::c_plus: return a(p) + b(p);
37  case Op::c_minus: return a(p) - b(p);
38  case Op::c_multiply: return a(p) * b(p);
39  case Op::c_divide: return a(p) / b(p);
40  case Op::c_power: return std::pow(a(p), b(p));
41  default: break;
42  }
43  throw std::runtime_error("Cannot handle operator " + std::to_string((int)op));
44  };
45  }
46 
48  type operator()(Op op, double& a, const type& b)
49  {
50  return [op, a, b](const Particle * p) {
51  switch (op) {
52  case Op::c_plus: return a + b(p);
53  case Op::c_minus: return a - b(p);
54  case Op::c_multiply: return a * b(p);
55  case Op::c_divide: return a / b(p);
56  case Op::c_power: return std::pow(a, b(p));
57  default: break;
58  }
59  throw std::runtime_error("Cannot handle operator " + std::to_string((int)op));
60  };
61  }
62 
64  type operator()(Op op, const type& a, double b)
65  {
66  return [op, a, b](const Particle * p) {
67  switch (op) {
68  case Op::c_plus: return a(p) + b;
69  case Op::c_minus: return a(p) - b;
70  case Op::c_multiply: return a(p) * b;
71  case Op::c_divide: return a(p) / b;
72  case Op::c_power: return std::pow(a(p), b);
73  default: break;
74  }
75  throw std::runtime_error("Cannot handle operator " + std::to_string((int)op));
76  };
77  }
78  };
80 }
Belle2::VariableFormulaConstructor::operator()
type operator()(const std::string &name)
Construct a variable from a given name.
Definition: VariableFormulaConstructor.h:18
Belle2::FormulaParserBase::EOperator::c_plus
@ c_plus
Addition.
Belle2::FormulaParserBase::EOperator::c_divide
@ c_divide
Division.
Belle2::VariableFormulaConstructor
Struct to construct new variable function objects from a name or a double value or to apply operation...
Definition: VariableFormulaConstructor.h:12
Belle2::VariableFormulaConstructor::type
Variable::Manager::FunctionPtr type
Return value we want for the FormulaParser::parse.
Definition: VariableFormulaConstructor.h:14
Belle2::Variable::Manager::getVariable
const Var * getVariable(std::string name)
Get the variable belonging to the given key.
Definition: Manager.cc:33
Belle2::FormulaParserBase::EOperator::c_power
@ c_power
Exponentation.
Belle2::VariableFormulaConstructor::operator()
type operator()(Op op, double &a, const type &b)
Apply operator on a double and a variable.
Definition: VariableFormulaConstructor.h:48
Belle2::VariableFormulaConstructor::operator()
type operator()(Op op, const type &a, double b)
Apply operator on a variable and a double.
Definition: VariableFormulaConstructor.h:64
Belle2::FormulaParserBase::EOperator::c_multiply
@ c_multiply
Multiply.
Belle2::VariableFormulaConstructor::operator()
type operator()(double value)
Construct a variable from a double value.
Definition: VariableFormulaConstructor.h:26
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::Variable::Manager::FunctionPtr
std::function< double(const Particle *)> FunctionPtr
NOTE: the python interface is documented manually in analysis/doc/Variables.rst (because we use ROOT ...
Definition: Manager.h:118
Belle2::Particle
Class to store reconstructed particles.
Definition: Particle.h:77
Belle2::FormulaParserBase::EOperator
EOperator
List of known operators.
Definition: FormulaParser.h:41
Belle2::VariableFormulaConstructor::operator()
type operator()(Op op, const type &a, const type &b)
Apply operator on two variables.
Definition: VariableFormulaConstructor.h:32
Belle2::VariableFormulaConstructor::Op
FormulaParserBase::EOperator Op
Shorthand for the operator enum.
Definition: VariableFormulaConstructor.h:16
Belle2::FormulaParserBase::EOperator::c_minus
@ c_minus
Subtraction.
Belle2::Variable::Manager::Instance
static Manager & Instance()
get singleton instance.
Definition: Manager.cc:27