Belle II Software development
Manager.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
9#pragma once
10
11#include <analysis/DecayDescriptor/DecayDescriptor.h>
12#include <framework/logging/Logger.h>
13
14#include <string>
15#include <map>
16#include <vector>
17#include <functional>
18#include <memory>
19#include <variant>
20#include <type_traits>
21
22namespace Belle2 {
27 class Particle;
28
29 namespace Variable {
101 class Manager {
109 public:
111 typedef std::variant<double, int, bool> VarVariant;
113 typedef std::function<VarVariant(const Particle*)> FunctionPtr;
115 typedef std::function<VarVariant(const Particle*, const std::vector<double>&)> ParameterFunctionPtr;
117 typedef std::function<FunctionPtr(const std::vector<std::string>&)> MetaFunctionPtr;
120
123 c_double = 0,
124 c_int = 1,
125 c_bool = 2
126 };
127
129 struct VarBase {
130 std::string name;
131 std::string description;
132 std::string group;
135 VarBase(const std::string& n, const std::string& d, const std::string& g, const VariableDataType& v)
136 : name(n), description(d), group(g), variabletype(v) { }
137
139 void extendDescriptionString(const std::string& d)
140 {
141 description.append(d);
142 }
143 };
144
146 struct Var : public VarBase {
149 Var(const std::string& n, FunctionPtr f, const std::string& d, const std::string& g = "",
150 const VariableDataType& v = VariableDataType::c_double)
151 : VarBase(n, d, g, v), function(f) { }
152 };
153
155 struct ParameterVar : public VarBase {
158 ParameterVar(const std::string& n, ParameterFunctionPtr f, const std::string& d, const std::string& g = "",
159 const VariableDataType& v = VariableDataType::c_double)
160 : VarBase(n, d, g, v), function(f) { }
161 };
162
164 struct MetaVar : public VarBase {
167 explicit MetaVar(const std::string& n, MetaFunctionPtr f, const std::string& d, const std::string& g = "",
168 const VariableDataType& v = VariableDataType::c_double)
169 : VarBase(n, d, g, v), function(f) { }
170 };
171
173 static Manager& Instance();
174
179 const Var* getVariable(std::string name);
180
187 const Var* getVariable(const std::string& functionName, const std::vector<std::string>& functionArguments);
188
192 std::vector<const Belle2::Variable::Manager::Var*> getVariables(const std::vector<std::string>& variables);
193
197 bool addAlias(const std::string& alias, const std::string& variable);
198
202 void printAliases();
203
207 void clearAliases();
208
212 std::string resolveAlias(const std::string& alias);
213
217 bool addCollection(const std::string& collection, const std::vector<std::string>& variables);
218
222 std::vector<std::string> getCollection(const std::string& collection);
223
227 std::vector<std::string> resolveCollections(const std::vector<std::string>& variables);
228
230 std::vector<const Belle2::Variable::Manager::VarBase*> getVariables() const { return m_variablesInRegistrationOrder; }
231
232
234 void setVariableGroup(const std::string& groupName);
235
237 void registerVariable(const std::string& name, const Manager::FunctionPtr& f, const std::string& description,
238 const Manager::VariableDataType& v, const std::string& unit = "");
240 void registerVariable(const std::string& name, const Manager::ParameterFunctionPtr& f, const std::string& description,
241 const Manager::VariableDataType& v, const std::string& unit = "");
243 void registerVariable(const std::string& name, const Manager::MetaFunctionPtr& f, const std::string& description,
246 void deprecateVariable(const std::string& name, bool make_fatal, const std::string& version, const std::string& description);
247
253 void checkDeprecatedVariable(const std::string& name);
254
261 double evaluate(const std::string& varName, const Particle* p);
262
264 std::vector<std::string> getNames() const;
265
267 std::vector<std::string> getAliasNames() const;
268
270 void assertValidName(const std::string& name);
271
272 private:
273 Manager() {};
276 ~Manager();
277
279 bool createVariable(const std::string& name);
280
285 bool createVariable(const std::string& fullname, const std::string& functionName,
286 const std::vector<std::string>& functionArguments);
287
289 std::string m_currentGroup;
290
292 std::vector<const VarBase*> m_variablesInRegistrationOrder;
293
295 std::map<std::string, std::string> m_alias;
297 std::map<std::string, std::vector<std::string>> m_collection;
299 std::map<std::string, std::shared_ptr<Var>> m_variables;
301 std::map<std::string, std::shared_ptr<ParameterVar>> m_parameter_variables;
303 std::map<std::string, std::shared_ptr<MetaVar>> m_meta_variables;
305 std::map<std::string, std::pair<bool, std::string>> m_deprecated;
306 };
307
309 class Proxy {
310 public:
312 Proxy(const std::string& name, Manager::FunctionPtr f, const std::string& description, Manager::VariableDataType v,
313 const std::string& unit = "")
314 {
315 Manager::Instance().registerVariable(name, f, description, v, unit);
316 }
318 Proxy(const std::string& name, Manager::ParameterFunctionPtr f, const std::string& description, Manager::VariableDataType v,
319 const std::string& unit = "")
320 {
321 Manager::Instance().registerVariable(name, f, description, v, unit);
322 }
324 Proxy(const std::string& name, Manager::MetaFunctionPtr f, const std::string& description, Manager::VariableDataType v)
325 {
326 Manager::Instance().registerVariable(name, f, description, v);
327 }
328 };
329
332 public:
334 explicit GroupProxy(const std::string& groupName)
335 {
337 }
338 };
339
342 public:
344 DeprecateProxy(const std::string& name, bool make_fatal, const std::string& version, const std::string& description)
345 {
346 Manager::Instance().deprecateVariable(name, make_fatal, version, description);
347 }
348 };
349
350 template<typename T>
351 std::function<T> make_function(T* t)
352 {
353 return { t };
354 }
355
356 template<typename T>
357 Belle2::Variable::Manager::VariableDataType get_function_type(const std::string& name, T* t)
358 {
359 auto func = std::function{t};
360 using ReturnType = typename decltype(func)::result_type;
361 if (std::is_same_v<ReturnType, double>) {
362 return Belle2::Variable::Manager::VariableDataType::c_double;
363 } else if (std::is_same_v<ReturnType, int>) {
364 return Belle2::Variable::Manager::VariableDataType::c_int;
365 } else if (std::is_same_v<ReturnType, bool>) {
366 return Belle2::Variable::Manager::VariableDataType::c_bool;
367 } else {
368 B2FATAL("Metavariables must be registered using the REGISTER_METAVARIABLE macro." << LogVar("Variablename", name));
369 }
370 }
371
375#define VARMANAGER_CONCATENATE_DETAIL(x, y) x##y
379#define VARMANAGER_CONCATENATE(x, y) VARMANAGER_CONCATENATE_DETAIL(x, y)
383#define VARMANAGER_MAKE_UNIQUE(x) VARMANAGER_CONCATENATE(x, __LINE__)
384
390#define REGISTER_VARIABLE_NO_UNIT(name, function, description) \
391 static Proxy VARMANAGER_MAKE_UNIQUE(_variableproxy)(std::string(name), Belle2::Variable::make_function(function), std::string(description), Belle2::Variable::get_function_type(name,function));
392
398#define REGISTER_VARIABLE_WITH_UNIT(name, function, description, unit) \
399 static Proxy VARMANAGER_MAKE_UNIQUE(_variableproxy)(std::string(name), Belle2::Variable::make_function(function), std::string(description), Belle2::Variable::get_function_type(name,function), std::string(unit));
400
406#define PICK_FIFTH_ARG(arg1, arg2, arg3, arg4, arg5, ...) arg5
407
413#define REGISTER_VARIABLE_MACRO_CHOOSER(...) PICK_FIFTH_ARG(__VA_ARGS__, REGISTER_VARIABLE_WITH_UNIT, REGISTER_VARIABLE_NO_UNIT, )
414
420#define REGISTER_VARIABLE(...) REGISTER_VARIABLE_MACRO_CHOOSER(__VA_ARGS__)(__VA_ARGS__)
421
427#define REGISTER_METAVARIABLE(name, function, description, variabledatatype) \
428 static Proxy VARMANAGER_MAKE_UNIQUE(_variableproxy)(std::string(name), Belle2::Variable::make_function(function), std::string(description), Belle2::Variable::Manager::VariableDataType(variabledatatype));
429
435#define VARIABLE_GROUP(groupName) \
436 static GroupProxy VARMANAGER_MAKE_UNIQUE(_variablegroupproxy)(groupName);
437
438 }
443#define MAKE_DEPRECATED(name, make_fatal, version, description) \
444 static DeprecateProxy VARMANAGER_MAKE_UNIQUE(_deprecateproxy)(std::string(name), bool(make_fatal), std::string(version), std::string(description));
446}
Class to store reconstructed particles.
Definition: Particle.h:75
Internal class that registers a variable as deprecated.
Definition: Manager.h:341
DeprecateProxy(const std::string &name, bool make_fatal, const std::string &version, const std::string &description)
constructor.
Definition: Manager.h:344
Internal class that registers a variable group with Manager when constructed.
Definition: Manager.h:331
GroupProxy(const std::string &groupName)
constructor.
Definition: Manager.h:334
Global list of available variables.
Definition: Manager.h:101
std::vector< const Belle2::Variable::Manager::VarBase * > getVariables() const
Return list of all variables (in order registered).
Definition: Manager.h:230
Particle Object
Typedef for the cut, that we use Particles as our base objects.
Definition: Manager.h:119
std::vector< const VarBase * > m_variablesInRegistrationOrder
List of variables in registration order.
Definition: Manager.h:292
std::vector< std::string > getAliasNames() const
Return a list of all variable alias names (in reverse order added).
Definition: Manager.cc:469
std::function< VarVariant(const Particle *)> FunctionPtr
functions stored take a const Particle* and return VarVariant.
Definition: Manager.h:113
std::vector< std::string > resolveCollections(const std::vector< std::string > &variables)
Resolve Collection Returns variable names corresponding to the given collection or if it is not a col...
Definition: Manager.cc:179
Manager(const Manager &)
Copy constructor disabled (not defined).
std::string m_currentGroup
Group last set via VARIABLE_GROUP().
Definition: Manager.h:289
const Var * getVariable(std::string name)
Get the variable belonging to the given key.
Definition: Manager.cc:57
std::variant< double, int, bool > VarVariant
NOTE: the python interface is documented manually in analysis/doc/Variables.rst (because we use ROOT ...
Definition: Manager.h:111
std::map< std::string, std::string > m_alias
List of registered aliases.
Definition: Manager.h:295
std::map< std::string, std::shared_ptr< Var > > m_variables
List of registered variables.
Definition: Manager.h:299
void deprecateVariable(const std::string &name, bool make_fatal, const std::string &version, const std::string &description)
Make a variable deprecated.
Definition: Manager.cc:400
std::map< std::string, std::shared_ptr< ParameterVar > > m_parameter_variables
List of registered parameter variables.
Definition: Manager.h:301
static Manager & Instance()
get singleton instance.
Definition: Manager.cc:25
void printAliases()
Print existing aliases.
Definition: Manager.cc:122
std::map< std::string, std::pair< bool, std::string > > m_deprecated
List of deprecated variables.
Definition: Manager.h:305
bool createVariable(const std::string &name)
Creates and registers a concrete variable (Var) from a MetaVar, ParameterVar or numeric constant.
Definition: Manager.cc:213
void assertValidName(const std::string &name)
Abort with B2FATAL if name is not a valid name for a variable.
Definition: Manager.cc:197
void clearAliases()
Clear existing aliases.
Definition: Manager.cc:117
std::function< FunctionPtr(const std::vector< std::string > &)> MetaFunctionPtr
meta functions stored take a const std::vector<std::string>& and return a FunctionPtr.
Definition: Manager.h:117
std::vector< std::string > getNames() const
Return list of all variable names (in order registered).
Definition: Manager.cc:460
std::map< std::string, std::vector< std::string > > m_collection
List of registered collections.
Definition: Manager.h:297
void setVariableGroup(const std::string &groupName)
All variables registered after VARIABLE_GROUP(groupName) will be added to this group.
Definition: Manager.cc:208
std::string resolveAlias(const std::string &alias)
Resolve alias Return original variable name.
Definition: Manager.cc:139
std::vector< std::string > getCollection(const std::string &collection)
Get Collection Returns variable names corresponding to the given collection.
Definition: Manager.cc:172
bool addAlias(const std::string &alias, const std::string &variable)
Add alias Return true if the alias was successfully added.
Definition: Manager.cc:95
bool addCollection(const std::string &collection, const std::vector< std::string > &variables)
Add collection Return true if the collection was successfully added.
Definition: Manager.cc:151
std::function< VarVariant(const Particle *, const std::vector< double > &)> ParameterFunctionPtr
parameter functions stored take a const Particle*, const std::vector<double>& and return VarVariant.
Definition: Manager.h:115
VariableDataType
data type of variables
Definition: Manager.h:122
void registerVariable(const std::string &name, const Manager::FunctionPtr &f, const std::string &description, const Manager::VariableDataType &v, const std::string &unit="")
Register a variable.
Definition: Manager.cc:331
void checkDeprecatedVariable(const std::string &name)
Check if a variable is deprecated.
Definition: Manager.cc:443
double evaluate(const std::string &varName, const Particle *p)
evaluate variable 'varName' on given Particle.
Definition: Manager.cc:476
std::map< std::string, std::shared_ptr< MetaVar > > m_meta_variables
List of registered meta variables.
Definition: Manager.h:303
Internal class that registers a variable with Manager when constructed.
Definition: Manager.h:309
Proxy(const std::string &name, Manager::ParameterFunctionPtr f, const std::string &description, Manager::VariableDataType v, const std::string &unit="")
constructor.
Definition: Manager.h:318
Proxy(const std::string &name, Manager::MetaFunctionPtr f, const std::string &description, Manager::VariableDataType v)
constructor.
Definition: Manager.h:324
Proxy(const std::string &name, Manager::FunctionPtr f, const std::string &description, Manager::VariableDataType v, const std::string &unit="")
constructor.
Definition: Manager.h:312
Class to store variables with their name which were sent to the logging service.
Abstract base class for different kinds of events.
A variable taking string arguments returning a variable.
Definition: Manager.h:164
MetaVar(const std::string &n, MetaFunctionPtr f, const std::string &d, const std::string &g="", const VariableDataType &v=VariableDataType::c_double)
ctor
Definition: Manager.h:167
MetaFunctionPtr function
Pointer to function.
Definition: Manager.h:165
A variable taking additional floating-point arguments to influence the behaviour.
Definition: Manager.h:155
ParameterVar(const std::string &n, ParameterFunctionPtr f, const std::string &d, const std::string &g="", const VariableDataType &v=VariableDataType::c_double)
ctor
Definition: Manager.h:158
ParameterFunctionPtr function
Pointer to function.
Definition: Manager.h:156
Base class for information common to all types of variables.
Definition: Manager.h:129
std::string description
Description of what this function does.
Definition: Manager.h:131
VarBase(const std::string &n, const std::string &d, const std::string &g, const VariableDataType &v)
ctor
Definition: Manager.h:135
void extendDescriptionString(const std::string &d)
function to extend the description of the variable
Definition: Manager.h:139
VariableDataType variabletype
data type of variable
Definition: Manager.h:133
std::string group
Associated group.
Definition: Manager.h:132
std::string name
Unique identifier of the function, used as key.
Definition: Manager.h:130
A variable returning a floating-point value for a given Particle.
Definition: Manager.h:146
Var(const std::string &n, FunctionPtr f, const std::string &d, const std::string &g="", const VariableDataType &v=VariableDataType::c_double)
ctor
Definition: Manager.h:149
FunctionPtr function
Pointer to function.
Definition: Manager.h:147