Belle II Software development
VariableFormulaConstructor Struct Reference

Struct to construct new variable function objects from a name or a double value or to apply operations on these variable function objects. More...

#include <VariableFormulaConstructor.h>

Public Types

typedef Variable::Manager::FunctionPtr type
 Return value we want for the FormulaParser::parse.
 
typedef FormulaParserBase::EOperator Op
 Shorthand for the operator enum.
 

Public Member Functions

type operator() (const std::string &name)
 Construct a variable from a given name.
 
type operator() (double value)
 Construct a variable from a double value.
 
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() (Op op, const type &a, double b)
 Apply operator on a variable and a double.
 

Detailed Description

Struct to construct new variable function objects from a name or a double value or to apply operations on these variable function objects.

Used for the formula() meta variable and passed to FormulaParser

Definition at line 20 of file VariableFormulaConstructor.h.

Member Typedef Documentation

◆ Op

Shorthand for the operator enum.

Definition at line 24 of file VariableFormulaConstructor.h.

◆ type

Return value we want for the FormulaParser::parse.

Definition at line 22 of file VariableFormulaConstructor.h.

Member Function Documentation

◆ operator()() [1/5]

type operator() ( const std::string &  name)
inline

Construct a variable from a given name.

Definition at line 26 of file VariableFormulaConstructor.h.

27 {
29 if (!var) throw std::runtime_error("Could not find " + name + " via the Variable::Manager. Check the name");
30 return var->function;
31 }
const Var * getVariable(std::string name)
Get the variable belonging to the given key.
Definition: Manager.cc:57
static Manager & Instance()
get singleton instance.
Definition: Manager.cc:25

◆ operator()() [2/5]

type operator() ( double  value)
inline

Construct a variable from a double value.

Definition at line 34 of file VariableFormulaConstructor.h.

35 {
36 return [value](const Particle*) { return value; };
37 }

◆ operator()() [3/5]

type operator() ( Op  op,
const type a,
const type b 
)
inline

Apply operator on two variables.

Definition at line 40 of file VariableFormulaConstructor.h.

41 {
42 return [op, a, b](const Particle * p) {
43 switch (op) {
44 case Op::c_plus:
45 try {
46 return std::get<double>(a(p)) + std::get<double>(b(p));
47 } catch (std::bad_variant_access&) {
48 try {
49 return std::get<double>(a(p)) + std::get<int>(b(p));
50 } catch (std::bad_variant_access&) {
51 try {
52 return std::get<int>(a(p)) + std::get<double>(b(p));
53 } catch (std::bad_variant_access&) {
54 return double(std::get<int>(a(p)) + std::get<int>(b(p)));
55 }
56 }
57 }
58 case Op::c_minus:
59 try {
60 return std::get<double>(a(p)) - std::get<double>(b(p));
61 } catch (std::bad_variant_access&) {
62 try {
63 return std::get<double>(a(p)) - std::get<int>(b(p));
64 } catch (std::bad_variant_access&) {
65 try {
66 return std::get<int>(a(p)) - std::get<double>(b(p));
67 } catch (std::bad_variant_access&) {
68 return double(std::get<int>(a(p)) - std::get<int>(b(p)));
69 }
70 }
71 }
72 case Op::c_multiply:
73 try {
74 return std::get<double>(a(p)) * std::get<double>(b(p));
75 } catch (std::bad_variant_access&) {
76 try {
77 return std::get<double>(a(p)) * std::get<int>(b(p));
78 } catch (std::bad_variant_access&) {
79 try {
80 return std::get<int>(a(p)) * std::get<double>(b(p));
81 } catch (std::bad_variant_access&) {
82 return double(std::get<int>(a(p)) * std::get<int>(b(p)));
83 }
84 }
85 }
86 case Op::c_divide:
87 try {
88 return std::get<double>(a(p)) / std::get<double>(b(p));
89 } catch (std::bad_variant_access&) {
90 try {
91 return std::get<double>(a(p)) / std::get<int>(b(p));
92 } catch (std::bad_variant_access&) {
93 try {
94 return std::get<int>(a(p)) / std::get<double>(b(p));
95 } catch (std::bad_variant_access&) {
96 return double(std::get<int>(a(p)) / std::get<int>(b(p)));
97 }
98 }
99 }
100 case Op::c_power:
101 try {
102 return std::pow(std::get<double>(a(p)), std::get<double>(b(p)));
103 } catch (std::bad_variant_access&) {
104 try {
105 return std::pow(std::get<double>(a(p)), std::get<int>(b(p)));
106 } catch (std::bad_variant_access&) {
107 try {
108 return std::pow(std::get<int>(a(p)), std::get<double>(b(p)));
109 } catch (std::bad_variant_access&) {
110 return std::pow(std::get<int>(a(p)), std::get<int>(b(p)));
111 }
112 }
113 }
114 default: break;
115 }
116 throw std::runtime_error("Cannot handle operator " + std::to_string((int)op));
117 };
118 }

◆ operator()() [4/5]

type operator() ( Op  op,
const type a,
double  b 
)
inline

Apply operator on a variable and a double.

Definition at line 182 of file VariableFormulaConstructor.h.

183 {
184 return [op, a, b](const Particle * p) {
185 switch (op) {
186 case Op::c_plus:
187 try {
188 return std::get<double>(a(p)) + b;
189 } catch (std::bad_variant_access&) {
190 try {
191 return std::get<int>(a(p)) + b;
192 } catch (std::bad_variant_access&) {
193 return std::get<bool>(a(p)) + b;
194 }
195 }
196 case Op::c_minus:
197 try {
198 return std::get<double>(a(p)) - b;
199 } catch (std::bad_variant_access&) {
200 try {
201 return std::get<int>(a(p)) - b;
202 } catch (std::bad_variant_access&) {
203 return std::get<bool>(a(p)) - b;
204 }
205 }
206 case Op::c_multiply:
207 try {
208 return std::get<double>(a(p)) * b;
209 } catch (std::bad_variant_access&) {
210 try {
211 return std::get<int>(a(p)) * b;
212 } catch (std::bad_variant_access&) {
213 return std::get<bool>(a(p)) * b;
214 }
215 }
216 case Op::c_divide:
217 try {
218 return std::get<double>(a(p)) / b;
219 } catch (std::bad_variant_access&) {
220 try {
221 return std::get<int>(a(p)) / b;
222 } catch (std::bad_variant_access&) {
223 return std::get<bool>(a(p)) / b;
224 }
225 }
226 case Op::c_power:
227 try {
228 return std::pow(std::get<double>(a(p)), b);
229 } catch (std::bad_variant_access&) {
230 try {
231 return std::pow(std::get<int>(a(p)), b);
232 } catch (std::bad_variant_access&) {
233 return std::pow(std::get<bool>(a(p)), b);
234 }
235 }
236 default: break;
237 }
238 throw std::runtime_error("Cannot handle operator " + std::to_string((int)op));
239 };
240 }

◆ operator()() [5/5]

type operator() ( Op  op,
double &  a,
const type b 
)
inline

Apply operator on a double and a variable.

Definition at line 121 of file VariableFormulaConstructor.h.

122 {
123 return [op, a, b](const Particle * p) {
124 switch (op) {
125 case Op::c_plus:
126 try {
127 return a + std::get<double>(b(p));
128 } catch (std::bad_variant_access&) {
129 try {
130 return a + std::get<int>(b(p));
131 } catch (std::bad_variant_access&) {
132 return a + std::get<bool>(b(p));
133 }
134 }
135 case Op::c_minus:
136 try {
137 return a - std::get<double>(b(p));
138 } catch (std::bad_variant_access&) {
139 try {
140 return a - std::get<int>(b(p));
141 } catch (std::bad_variant_access&) {
142 return a - std::get<bool>(b(p));
143 }
144 }
145 case Op::c_multiply:
146 try {
147 return a * std::get<double>(b(p));
148 } catch (std::bad_variant_access&) {
149 try {
150 return a * std::get<int>(b(p));
151 } catch (std::bad_variant_access&) {
152 return a * std::get<bool>(b(p));
153 }
154 }
155 case Op::c_divide:
156 try {
157 return a / std::get<double>(b(p));
158 } catch (std::bad_variant_access&) {
159 try {
160 return a / std::get<int>(b(p));
161 } catch (std::bad_variant_access&) {
162 return a / std::get<bool>(b(p));
163 }
164 }
165 case Op::c_power:
166 try {
167 return std::pow(a, std::get<double>(b(p)));
168 } catch (std::bad_variant_access&) {
169 try {
170 return std::pow(a, std::get<int>(b(p)));
171 } catch (std::bad_variant_access&) {
172 return std::pow(a, std::get<bool>(b(p)));
173 }
174 }
175 default: break;
176 }
177 throw std::runtime_error("Cannot handle operator " + std::to_string((int)op));
178 };
179 }

The documentation for this struct was generated from the following file: