Belle II Software  release-06-00-14
VariableFormulaConstructor.h
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 #include <analysis/VariableManager/Manager.h>
9 #include <framework/utilities/FormulaParser.h>
10 
11 namespace Belle2 {
25  type operator()(const std::string& name)
26  {
27  auto var = Variable::Manager::Instance().getVariable(name);
28  if (!var) throw std::runtime_error("Could not find " + name + " via the Variable::Manager. Check the name");
29  return var->function;
30  }
31 
33  type operator()(double value)
34  {
35  return [value](const Particle*) { return value; };
36  }
37 
39  type operator()(Op op, const type& a, const type& b)
40  {
41  return [op, a, b](const Particle * p) {
42  switch (op) {
43  case Op::c_plus: return a(p) + b(p);
44  case Op::c_minus: return a(p) - b(p);
45  case Op::c_multiply: return a(p) * b(p);
46  case Op::c_divide: return a(p) / b(p);
47  case Op::c_power: return std::pow(a(p), b(p));
48  default: break;
49  }
50  throw std::runtime_error("Cannot handle operator " + std::to_string((int)op));
51  };
52  }
53 
55  type operator()(Op op, double& a, const type& b)
56  {
57  return [op, a, b](const Particle * p) {
58  switch (op) {
59  case Op::c_plus: return a + b(p);
60  case Op::c_minus: return a - b(p);
61  case Op::c_multiply: return a * b(p);
62  case Op::c_divide: return a / b(p);
63  case Op::c_power: return std::pow(a, b(p));
64  default: break;
65  }
66  throw std::runtime_error("Cannot handle operator " + std::to_string((int)op));
67  };
68  }
69 
71  type operator()(Op op, const type& a, double b)
72  {
73  return [op, a, b](const Particle * p) {
74  switch (op) {
75  case Op::c_plus: return a(p) + b;
76  case Op::c_minus: return a(p) - b;
77  case Op::c_multiply: return a(p) * b;
78  case Op::c_divide: return a(p) / b;
79  case Op::c_power: return std::pow(a(p), b);
80  default: break;
81  }
82  throw std::runtime_error("Cannot handle operator " + std::to_string((int)op));
83  };
84  }
85  };
87 }
EOperator
List of known operators.
Definition: FormulaParser.h:31
Class to store reconstructed particles.
Definition: Particle.h:74
const Var * getVariable(std::string name)
Get the variable belonging to the given key.
Definition: Manager.cc:31
static Manager & Instance()
get singleton instance.
Definition: Manager.cc:25
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:108
Abstract base class for different kinds of events.
Struct to construct new variable function objects from a name or a double value or to apply operation...
type operator()(Op op, const type &a, double b)
Apply operator on a variable and a double.
FormulaParserBase::EOperator Op
Shorthand for the operator enum.
type operator()(Op op, const type &a, const type &b)
Apply operator on two variables.
type operator()(Op op, double &a, const type &b)
Apply operator on a double and a variable.
type operator()(const std::string &name)
Construct a variable from a given name.
type operator()(double value)
Construct a variable from a double value.
Variable::Manager::FunctionPtr type
Return value we want for the FormulaParser::parse.