Belle II Software development
Manager Class Reference

Global list of available variables. More...

#include <Manager.h>

Classes

struct  MetaVar
 A variable taking string arguments returning a variable. More...
 
struct  ParameterVar
 A variable taking additional floating-point arguments to influence the behaviour. More...
 
struct  Var
 A variable returning a floating-point value for a given Particle. More...
 
struct  VarBase
 Base class for information common to all types of variables. More...
 

Public Types

enum  VariableDataType {
  c_double = 0 ,
  c_int = 1 ,
  c_bool = 2
}
 data type of variables More...
 
typedef std::variant< double, int, bool > VarVariant
 NOTE: the python interface is documented manually in analysis/doc/Variables.rst (because we use ROOT to expose this in python rather than boost::python).
 
typedef std::function< VarVariant(const Particle *)> FunctionPtr
 functions stored take a const Particle* and return VarVariant.
 
typedef std::function< VarVariant(const Particle *, const std::vector< double > &)> ParameterFunctionPtr
 parameter functions stored take a const Particle*, const std::vector<double>& and return VarVariant.
 
typedef std::function< FunctionPtr(const std::vector< std::string > &)> MetaFunctionPtr
 meta functions stored take a const std::vector<std::string>& and return a FunctionPtr.
 
typedef Particle Object
 Typedef for the cut, that we use Particles as our base objects.
 

Public Member Functions

const VargetVariable (std::string name)
 Get the variable belonging to the given key.
 
const VargetVariable (const std::string &functionName, const std::vector< std::string > &functionArguments)
 Get Meta and Parameter Variables for a function call which is already parsed into name and arguments.
 
std::vector< const Belle2::Variable::Manager::Var * > getVariables (const std::vector< std::string > &variables)
 Get variables belonging to the given keys.
 
bool addAlias (const std::string &alias, const std::string &variable)
 Add alias Return true if the alias was successfully added.
 
void printAliases ()
 Print existing aliases.
 
void clearAliases ()
 Clear existing aliases.
 
std::string resolveAlias (const std::string &alias)
 Resolve alias Return original variable name.
 
bool addCollection (const std::string &collection, const std::vector< std::string > &variables)
 Add collection Return true if the collection was successfully added.
 
std::vector< std::string > getCollection (const std::string &collection)
 Get Collection Returns variable names corresponding to the given collection.
 
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 collection just the variable itself.
 
std::vector< const Belle2::Variable::Manager::VarBase * > getVariables () const
 Return list of all variables (in order registered).
 
void setVariableGroup (const std::string &groupName)
 All variables registered after VARIABLE_GROUP(groupName) will be added to this group.
 
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.
 
void registerVariable (const std::string &name, const Manager::ParameterFunctionPtr &f, const std::string &description, const Manager::VariableDataType &v, const std::string &unit="")
 Register a variable that takes floating-point arguments (see Variable::Manager::ParameterFunctionPtr).
 
void registerVariable (const std::string &name, const Manager::MetaFunctionPtr &f, const std::string &description, const Manager::VariableDataType &v)
 Register a meta-variable that takes string arguments and returns a variable(see Variable::Manager::MetaFunctionPtr).
 
void deprecateVariable (const std::string &name, bool make_fatal, const std::string &version, const std::string &description)
 Make a variable deprecated.
 
void checkDeprecatedVariable (const std::string &name)
 Check if a variable is deprecated.
 
double evaluate (const std::string &varName, const Particle *p)
 evaluate variable 'varName' on given Particle.
 
std::vector< std::string > getNames () const
 Return list of all variable names (in order registered).
 
std::vector< std::string > getAliasNames () const
 Return a list of all variable alias names (in reverse order added).
 
void assertValidName (const std::string &name)
 Abort with B2FATAL if name is not a valid name for a variable.
 

Static Public Member Functions

static ManagerInstance ()
 get singleton instance.
 

Private Member Functions

 Manager (const Manager &)
 Copy constructor disabled (not defined).
 
bool createVariable (const std::string &name)
 Creates and registers a concrete variable (Var) from a MetaVar, ParameterVar or numeric constant.
 
bool createVariable (const std::string &fullname, const std::string &functionName, const std::vector< std::string > &functionArguments)
 Creates and registers a MetaVar, ParameterVar Called by the corresponding getVariable(const std::string&, const std::vector<std::string>&) to register non-existing variables The fullname of the function call is also passed for alias resolving.
 

Private Attributes

std::string m_currentGroup
 Group last set via VARIABLE_GROUP().
 
std::vector< const VarBase * > m_variablesInRegistrationOrder
 List of variables in registration order.
 
std::map< std::string, std::string > m_alias
 List of registered aliases.
 
std::map< std::string, std::vector< std::string > > m_collection
 List of registered collections.
 
std::map< std::string, std::shared_ptr< Var > > m_variables
 List of registered variables.
 
std::map< std::string, std::shared_ptr< ParameterVar > > m_parameter_variables
 List of registered parameter variables.
 
std::map< std::string, std::shared_ptr< MetaVar > > m_meta_variables
 List of registered meta variables.
 
std::map< std::string, std::pair< bool, std::string > > m_deprecated
 List of deprecated variables.
 

Detailed Description

Global list of available variables.

Each variable has an associated unique string key through which it can be accessed. The Manager can be used to get an instance of the Variable class for this key. All ordinary variables take a const Particle* and return a double.

List of available variables

To simply get a list of all variables registered in the main analysis library, run basf2 analysis/scripts/variables.py .

Parameter variables

Parameter variables take a const Particle* and a const std::vector<double>&. They return a double. If a Parameter Variable is accessed via the VariableManager::getVariable a ordinary variable is dynamically registered which calls the Parameter variable with the correct parameters.

Meta variables

Meta variables take a const std::vector<std::string>& and return a pointer to a ordinary variable function. If a Meta Variable is accessed via the VariableManager::getVariable a ordinary variable is dynamically registered which calls the function returned by the meta variable.

There are a number of meta-variables that can be combined with existing variables to do more powerful calculations:

abs(varName)
Return absolute value of variable varName.
daughter(N, varName)
Calculate variable varName for Nth daughter
daughterProductOf(varName)
Calculate variable varName for each daughter, and return product of values
daughterSumOf(varName)
Calculate variable varName for each daughter, and return sum of values
extraInfo(extraInfoName)
Return value of extra info 'extraInfoName' from Particle (see Particle::getExtraInfo()).

So, if you wanted to get the momentum of the first daughter, you can use "daughter(1, p)" anywhere where variable names are accepted. Nesting is also possible, e.g. "daughter(1, abs(extraInfo(SignalProbability)))" can be used.

Adding variables

Variables will automatically register themselves when the library that contains them is loaded. They only need to be put in a .cc file, with a call to REGISTER_VARIABLE() in the global scope to set a name and description. Newly registered variables will be added to the group defined by the last call to VARIABLE_GROUP(), please search the list of variables (see 'List of available variables' section) for a matching group before adding your own.

Example:

double particleFlavorType(const Particle* part)
{
return part->getFlavorType();
}
REGISTER_VARIABLE("flavor", particleFlavorType, "flavor type of decay (0=unflavored, 1=flavored)");
Class to store reconstructed particles.
Definition: Particle.h:75
EFlavorType getFlavorType() const
Returns flavor type of the decay (for FS particles: flavor type of particle)
Definition: Particle.h:469

This registers the variable "flavor" with the given function and description. Note that only alphanumeric characters (0-9, a-Z) plus '_' are permitted in variable names.

Variables can then be accessed using getVariable(name) and getVariables().

Python interface

This class is exported to Python, and can be used to use variables in Python basf2 modules:

from variables import variables
from ROOT.Math import PxPyPzEVector
someParticle = Belle2.Particle(PxPyPzEVector(1.0, 0, 0, 1.0), 321)
print(variables.evaluate('E', someParticle))
Abstract base class for different kinds of events.

Definition at line 101 of file Manager.h.

Member Typedef Documentation

◆ FunctionPtr

typedef std::function<VarVariant(const Particle*)> FunctionPtr

functions stored take a const Particle* and return VarVariant.

Definition at line 113 of file Manager.h.

◆ MetaFunctionPtr

typedef std::function<FunctionPtr(const std::vector<std::string>&)> MetaFunctionPtr

meta functions stored take a const std::vector<std::string>& and return a FunctionPtr.

Definition at line 117 of file Manager.h.

◆ Object

typedef Particle Object

Typedef for the cut, that we use Particles as our base objects.

Definition at line 119 of file Manager.h.

◆ ParameterFunctionPtr

typedef 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 at line 115 of file Manager.h.

◆ VarVariant

typedef std::variant<double, int, bool> VarVariant

NOTE: the python interface is documented manually in analysis/doc/Variables.rst (because we use ROOT to expose this in python rather than boost::python).

Please also keep that up-to-date with any modifications. Typedef for variable return type, can either be double, int or bool in std::variant

Definition at line 111 of file Manager.h.

Member Enumeration Documentation

◆ VariableDataType

data type of variables

Definition at line 122 of file Manager.h.

122 {
123 c_double = 0,
124 c_int = 1,
125 c_bool = 2
126 };

Constructor & Destructor Documentation

◆ Manager()

Manager ( )
inlineprivate

Definition at line 273 of file Manager.h.

273{};

Member Function Documentation

◆ addAlias()

bool addAlias ( const std::string &  alias,
const std::string &  variable 
)

Add alias Return true if the alias was successfully added.

Definition at line 95 of file Manager.cc.

96{
97
98 assertValidName(alias);
99
100 if (m_alias.find(alias) != m_alias.end()) {
101 if (variable == m_alias[alias]) { return true; }
102 B2WARNING("An alias with the name '" << alias << "' exists and is set to '" << m_alias[alias] << "', setting it to '" << variable <<
103 "'. Be aware: only the last alias defined before processing the events will be used!");
104 m_alias[alias] = variable;
105 return true;
106 }
107
108 if (m_variables.find(alias) != m_variables.end()) {
109 B2ERROR("Variable with the name '" << alias << "' exists already, cannot add it as an alias!");
110 return false;
111 }
112
113 m_alias.insert(std::make_pair(alias, variable));
114 return true;
115}
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 assertValidName(const std::string &name)
Abort with B2FATAL if name is not a valid name for a variable.
Definition: Manager.cc:197

◆ addCollection()

bool addCollection ( const std::string &  collection,
const std::vector< std::string > &  variables 
)

Add collection Return true if the collection was successfully added.

Definition at line 151 of file Manager.cc.

152{
153
154 assertValidName(collection);
155
156 if (m_collection.find(collection) != m_collection.end()) {
157 B2WARNING("Another collection with the name'" << collection << "' is already set! I overwrite it!");
158 m_collection[collection] = variables;
159 return true;
160 }
161
162 if (m_variables.find(collection) != m_variables.end()) {
163 B2ERROR("Variable with the name '" << collection << "' exists already, won't add it as an collection!");
164 return false;
165 }
166
167 m_collection.insert(std::make_pair(collection, variables));
168 return true;
169}
std::map< std::string, std::vector< std::string > > m_collection
List of registered collections.
Definition: Manager.h:297

◆ assertValidName()

void assertValidName ( const std::string &  name)

Abort with B2FATAL if name is not a valid name for a variable.

Definition at line 197 of file Manager.cc.

198{
199 const static std::regex allowedNameRegex("^[a-zA-Z0-9_]*$");
200
201 if (!std::regex_match(name, allowedNameRegex)) {
202 B2FATAL("Variable '" << name <<
203 "' contains forbidden characters! Only alphanumeric characters plus underscores (_) are allowed for variable names.");
204 }
205}

◆ checkDeprecatedVariable()

void checkDeprecatedVariable ( const std::string &  name)

Check if a variable is deprecated.

If it is marked as deprecated a warning is issued or a B2FATAL aborts the event loop. Which action is taken depends on the depreciation setting.

Definition at line 443 of file Manager.cc.

444{
445 auto varIter = m_deprecated.find(name);
446
447 if (varIter == m_deprecated.end())
448 return;
449 else {
450 bool make_fatal = varIter->second.first;
451 std::string message = varIter->second.second;
452 if (make_fatal)
453 B2FATAL("Variable " << name << " is deprecated. " << message);
454 else
455 B2WARNING("Variable " << name << " is deprecated. " << message);
456 }
457}
std::map< std::string, std::pair< bool, std::string > > m_deprecated
List of deprecated variables.
Definition: Manager.h:305

◆ clearAliases()

void clearAliases ( )

Clear existing aliases.

Definition at line 117 of file Manager.cc.

118{
119 m_alias.clear();
120}

◆ createVariable() [1/2]

bool createVariable ( const std::string &  fullname,
const std::string &  functionName,
const std::vector< std::string > &  functionArguments 
)
private

Creates and registers a MetaVar, ParameterVar Called by the corresponding getVariable(const std::string&, const std::vector<std::string>&) to register non-existing variables The fullname of the function call is also passed for alias resolving.

Definition at line 296 of file Manager.cc.

298{
299 // Search function name in parameter variables
300 auto parameterIter = m_parameter_variables.find(functionName);
301 if (parameterIter != m_parameter_variables.end()) {
302
303 std::vector<double> arguments;
304 for (auto& arg : functionArguments) {
305 double number = 0;
306 number = Belle2::convertString<double>(arg);
307 arguments.push_back(number);
308 }
309 auto pfunc = parameterIter->second->function;
310 auto func = [pfunc, arguments](const Particle * particle) -> std::variant<double, int, bool> { return pfunc(particle, arguments); };
311 m_variables[fullname] = std::make_shared<Var>(fullname, func, parameterIter->second->description, parameterIter->second->group,
312 parameterIter->second->variabletype);
313 return true;
314
315 }
316
317 // Search function fullname in meta variables
318 auto metaIter = m_meta_variables.find(functionName);
319 if (metaIter != m_meta_variables.end()) {
320 auto func = metaIter->second->function(functionArguments);
321 m_variables[fullname] = std::make_shared<Var>(fullname, func, metaIter->second->description, metaIter->second->group,
322 metaIter->second->variabletype);
323 return true;
324 }
325
326 B2FATAL("Encountered bad variable name '" << fullname << "'. Maybe you misspelled it?");
327 return false;
328}
std::map< std::string, std::shared_ptr< ParameterVar > > m_parameter_variables
List of registered parameter variables.
Definition: Manager.h:301
std::map< std::string, std::shared_ptr< MetaVar > > m_meta_variables
List of registered meta variables.
Definition: Manager.h:303

◆ createVariable() [2/2]

bool createVariable ( const std::string &  name)
private

Creates and registers a concrete variable (Var) from a MetaVar, ParameterVar or numeric constant.

Definition at line 213 of file Manager.cc.

214{
215 std::match_results<std::string::const_iterator> results;
216
217 // Check if name is a simple number
218 if (std::regex_match(name, results, std::regex("^([0-9]+\\.?[0-9]*)$"))) {
219 float float_number = std::stof(results[1]);
220 auto func = [float_number](const Particle*) -> double {
221 return float_number;
222 };
223 m_variables[name] = std::make_shared<Var>(name, func, std::string("Returns number ") + name);
224 return true;
225 }
226
227 // Check if name is a function call
228 if (std::regex_match(name, results, std::regex("^([a-zA-Z0-9_]*)\\((.*)\\)$"))) {
229
230 std::string functionName = results[1];
231 boost::algorithm::trim(functionName);
232 std::vector<std::string> functionArguments = splitOnDelimiterAndConserveParenthesis(results[2], ',', '(', ')');
233 for (auto& str : functionArguments) {
234 boost::algorithm::trim(str);
235 }
236
237 // Search function name in parameter variables
238 auto parameterIter = m_parameter_variables.find(functionName);
239 if (parameterIter != m_parameter_variables.end()) {
240
241 std::vector<double> arguments;
242 for (auto& arg : functionArguments) {
243 double number = 0;
244 number = Belle2::convertString<double>(arg);
245 arguments.push_back(number);
246 }
247 auto pfunc = parameterIter->second->function;
248 auto func = [pfunc, arguments](const Particle * particle) -> VarVariant { return pfunc(particle, arguments); };
249 m_variables[name] = std::make_shared<Var>(name, func, parameterIter->second->description, parameterIter->second->group,
250 parameterIter->second->variabletype);
251 return true;
252
253 }
254
255 // Search function name in meta variables
256 auto metaIter = m_meta_variables.find(functionName);
257 if (metaIter != m_meta_variables.end()) {
258 auto func = metaIter->second->function(functionArguments);
259 m_variables[name] = std::make_shared<Var>(name, func, metaIter->second->description, metaIter->second->group,
260 metaIter->second->variabletype);
261 return true;
262 }
263 }
264 // Try Formula registration with python parser if variable is not a simple identifier (else we get a infinite loop)
265 if (not std::regex_match(name, std::regex("^[a-zA-Z_][a-zA-Z_0-9]*$")) and not name.empty()) {
266 Py_Initialize();
267 try {
268 // Import parser
269 py::object b2parser_namespace = py::import("b2parser");
270 // Parse Expression
271 py::tuple expression_tuple = py::extract<boost::python::tuple>(b2parser_namespace.attr("parse_expression")(name));
272 try {
273 // Compile ExpressionNode
274 std::shared_ptr<const AbstractExpressionNode<Belle2::Variable::Manager>> expression_node =
275 NodeFactory::compile_expression_node<Belle2::Variable::Manager>(expression_tuple);
276 // Create lambda capturing the ExpressionNode
277 Variable::Manager::FunctionPtr func = [expression_node](const Particle * object) -> VarVariant {
278 return expression_node->evaluate(object);
279 };
280 // Put new variable into set of variables
281 m_variables[name] = std::make_shared<Var>(name, func, std::string("Returns expression ") + name);
282 return true;
283 } catch (std::runtime_error& exception) {
284 B2FATAL("Encountered bad variable name '" << name << "'. Maybe you misspelled it?");
285 }
286 } catch (py::error_already_set&) {
287 PyErr_Print();
288 B2FATAL("Parsing error for formula: '" << name << "'");
289 }
290 }
291
292 B2FATAL("Encountered bad variable name '" << name << "'. Maybe you misspelled it?");
293 return false;
294}
std::function< VarVariant(const Particle *)> FunctionPtr
functions stored take a const Particle* and return VarVariant.
Definition: Manager.h:113
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::vector< std::string > splitOnDelimiterAndConserveParenthesis(std::string str, char delimiter, char open, char close)
Split into std::vector on delimiter ignoring delimiters between parenthesis.
Definition: CutHelpers.cc:81

◆ deprecateVariable()

void deprecateVariable ( const std::string &  name,
bool  make_fatal,
const std::string &  version,
const std::string &  description 
)

Make a variable deprecated.

Definition at line 400 of file Manager.cc.

402{
403 auto varIter = m_deprecated.find(name);
404 if (varIter == m_deprecated.end())
405 m_deprecated.insert(std::make_pair(name, std::make_pair(make_fatal, description)));
406 else
407 B2FATAL("There seem to be two calls to deprecate the variable: Please remove one.");
408
409 auto mapIter = m_variables.find(name);
410 if (mapIter != m_variables.end()) {
411 if (make_fatal) {
412 mapIter->second.get()->extendDescriptionString("\n\n.. warning:: ");
413 } else {
414 mapIter->second.get()->extendDescriptionString("\n\n.. note:: ");
415 }
416 mapIter->second.get()->extendDescriptionString(".. deprecated:: " + version + "\n " + description);
417 } else {
418 auto parMapIter = m_parameter_variables.find(name);
419 if (parMapIter != m_parameter_variables.end()) {
420 if (make_fatal) {
421 parMapIter->second.get()->extendDescriptionString("\n\n.. warning:: ");
422 } else {
423 parMapIter->second.get()->extendDescriptionString("\n\n.. note:: ");
424 }
425 parMapIter->second.get()->extendDescriptionString(".. deprecated:: " + version + "\n " + description);
426 } else {
427 auto metaMapIter = m_meta_variables.find(name);
428 if (metaMapIter != m_meta_variables.end()) {
429 if (make_fatal) {
430 metaMapIter->second.get()->extendDescriptionString("\n\n.. warning:: ");
431 } else {
432 metaMapIter->second.get()->extendDescriptionString("\n\n.. note:: ");
433 }
434 metaMapIter->second.get()->extendDescriptionString(".. deprecated:: " + version + "\n " + description);
435 } else {
436 B2FATAL("The variable '" << name << "' is not registered so it makes no sense to try to deprecate it.");
437 }
438 }
439 }
440
441}

◆ evaluate()

double evaluate ( const std::string &  varName,
const Particle p 
)

evaluate variable 'varName' on given Particle.

Mainly provided for the Python interface. For performance critical code, it is recommended to use getVariable() once and keep the Var* pointer around.

Throws exception if variable isn't found. Assumes 'p' is != NULL.

Definition at line 476 of file Manager.cc.

477{
478 const Var* var = getVariable(varName);
479 if (!var) {
480 throw std::runtime_error("Variable::Manager::evaluate(): variable '" + varName + "' not found!");
481 }
482
483 if (var->variabletype == Variable::Manager::VariableDataType::c_double)
484 return std::get<double>(var->function(p));
485 else if (var->variabletype == Variable::Manager::VariableDataType::c_int)
486 return (double)std::get<int>(var->function(p));
487 else if (var->variabletype == Variable::Manager::VariableDataType::c_bool)
488 return (double)std::get<bool>(var->function(p));
489 else return std::numeric_limits<double>::quiet_NaN();
490}
const Var * getVariable(std::string name)
Get the variable belonging to the given key.
Definition: Manager.cc:57

◆ getAliasNames()

std::vector< std::string > getAliasNames ( ) const

Return a list of all variable alias names (in reverse order added).

Definition at line 469 of file Manager.cc.

470{
471 std::vector<std::string> names;
472 for (auto al : m_alias) names.push_back(al.first);
473 return names;
474}

◆ getCollection()

std::vector< std::string > getCollection ( const std::string &  collection)

Get Collection Returns variable names corresponding to the given collection.

Definition at line 172 of file Manager.cc.

173{
174
175 return m_collection[collection];
176
177}

◆ getNames()

std::vector< std::string > getNames ( ) const

Return list of all variable names (in order registered).

Definition at line 460 of file Manager.cc.

461{
462 std::vector<std::string> names;
463 for (const VarBase* var : m_variablesInRegistrationOrder) {
464 names.push_back(var->name);
465 }
466 return names;
467}
std::vector< const VarBase * > m_variablesInRegistrationOrder
List of variables in registration order.
Definition: Manager.h:292

◆ getVariable() [1/2]

const Variable::Manager::Var * getVariable ( const std::string &  functionName,
const std::vector< std::string > &  functionArguments 
)

Get Meta and Parameter Variables for a function call which is already parsed into name and arguments.

Used by FunctionNode because there we already have the MetaVariable in parsed form

Returns NULL if name not found

Definition at line 31 of file Manager.cc.

33{
34 // Combine to full name for alias resolving
35 std::string fullname = functionName + "(" + boost::algorithm::join(functionArguments, ", ") + ")";
36
37 // resolve aliases. Aliases might point to other aliases so we need to keep a
38 // set of what we have seen so far to avoid running into infinite loops
39 std::set<std::string> aliasesSeen;
40 for (auto aliasIter = m_alias.find(fullname); aliasIter != m_alias.end(); aliasIter = m_alias.find(fullname)) {
41 const auto [it, added] = aliasesSeen.insert(fullname);
42 if (!added) {
43 B2FATAL("Encountered a loop in the alias definitions between the aliases "
44 << boost::algorithm::join(aliasesSeen, ", "));
45 }
46 fullname = aliasIter->second;
47 }
48 auto mapIter = m_variables.find(fullname);
49 if (mapIter == m_variables.end()) {
50 if (!createVariable(fullname, functionName, functionArguments)) return nullptr;
51 mapIter = m_variables.find(fullname);
52 if (mapIter == m_variables.end()) return nullptr;
53 }
54 return mapIter->second.get();
55}
bool createVariable(const std::string &name)
Creates and registers a concrete variable (Var) from a MetaVar, ParameterVar or numeric constant.
Definition: Manager.cc:213

◆ getVariable() [2/2]

const Variable::Manager::Var * getVariable ( std::string  name)

Get the variable belonging to the given key.

Returns NULL if name not found.

Definition at line 57 of file Manager.cc.

58{
59 // resolve aliases. Aliases might point to other aliases so we need to keep a
60 // set of what we have seen so far to avoid running into infinite loops
61 std::set<std::string> aliasesSeen;
62 for (auto aliasIter = m_alias.find(name); aliasIter != m_alias.end(); aliasIter = m_alias.find(name)) {
63 const auto [it, added] = aliasesSeen.insert(name);
64 if (!added) {
65 B2FATAL("Encountered a loop in the alias definitions between the aliases "
66 << boost::algorithm::join(aliasesSeen, ", "));
67 }
68 name = aliasIter->second;
69 }
70 auto mapIter = m_variables.find(name);
71 if (mapIter == m_variables.end()) {
72 if (!createVariable(name)) return nullptr;
73 mapIter = m_variables.find(name);
74 if (mapIter == m_variables.end()) return nullptr;
75 }
76 return mapIter->second.get();
77}

◆ getVariables() [1/2]

std::vector< const Belle2::Variable::Manager::VarBase * > getVariables ( ) const
inline

Return list of all variables (in order registered).

Definition at line 230 of file Manager.h.

◆ getVariables() [2/2]

std::vector< const Variable::Manager::Var * > getVariables ( const std::vector< std::string > &  variables)

Get variables belonging to the given keys.

Definition at line 79 of file Manager.cc.

80{
81
82 std::vector<const Variable::Manager::Var*> variable_pointers;
83 for (auto& variable : variables) {
84 const Var* x = getVariable(variable);
85 if (x == nullptr) {
86 B2WARNING("Couldn't find variable " << variable << " via the Variable::Manager. Check the name!");
87 }
88 variable_pointers.push_back(x);
89 }
90 return variable_pointers;
91
92}

◆ Instance()

Variable::Manager & Instance ( )
static

get singleton instance.

Definition at line 25 of file Manager.cc.

26{
27 static Variable::Manager v;
28 return v;
29}
Global list of available variables.
Definition: Manager.h:101

◆ printAliases()

void printAliases ( )

Print existing aliases.

Definition at line 122 of file Manager.cc.

123{
124 long unsigned int longest_alias_size = 0;
125 for (const auto& a : m_alias) {
126 if (a.first.length() > longest_alias_size) {
127 longest_alias_size = a.first.length();
128 }
129 }
130 B2INFO("=====================================");
131 B2INFO("The following aliases are registered:");
132 for (const auto& a : m_alias) {
133 B2INFO(std::string(a.first, 0, longest_alias_size) << std::string(longest_alias_size - a.first.length(),
134 ' ') << " --> " << a.second);
135 }
136 B2INFO("=====================================");
137}

◆ registerVariable() [1/3]

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 at line 331 of file Manager.cc.

334{
335 if (!f) {
336 B2FATAL("No function provided for variable '" << name << "'.");
337 }
338
339 assertValidName(name);
340
341 auto mapIter = m_variables.find(name);
342 if (mapIter == m_variables.end()) {
343
344 auto var = std::make_shared<Var>(name, f, description, m_currentGroup, variabletype);
345 B2DEBUG(19, "Registered Variable " << name);
346 m_variables[name] = var;
347 m_variablesInRegistrationOrder.push_back(var.get());
348 if (!unit.empty()) {
349 var.get()->extendDescriptionString(":Unit: " + unit);
350 }
351 } else {
352 B2FATAL("A variable named '" << name << "' was already registered! Note that all variables need a unique name!");
353 }
354}
std::string m_currentGroup
Group last set via VARIABLE_GROUP().
Definition: Manager.h:289

◆ registerVariable() [2/3]

void registerVariable ( const std::string &  name,
const Manager::MetaFunctionPtr f,
const std::string &  description,
const Manager::VariableDataType v 
)

Register a meta-variable that takes string arguments and returns a variable(see Variable::Manager::MetaFunctionPtr).

Definition at line 380 of file Manager.cc.

382{
383 if (!f) {
384 B2FATAL("No function provided for variable '" << name << "'.");
385 }
386
387 auto mapIter = m_meta_variables.find(name);
388 if (mapIter == m_meta_variables.end()) {
389 auto var = std::make_shared<MetaVar>(name, f, description, m_currentGroup, variabletype);
390 std::string rawName = name.substr(0, name.find('('));
391 assertValidName(rawName);
392 B2DEBUG(19, "Registered meta Variable " << rawName);
393 m_meta_variables[rawName] = var;
394 m_variablesInRegistrationOrder.push_back(var.get());
395 } else {
396 B2FATAL("A variable named '" << name << "' was already registered! Note that all variables need a unique name!");
397 }
398}

◆ registerVariable() [3/3]

void registerVariable ( const std::string &  name,
const Manager::ParameterFunctionPtr f,
const std::string &  description,
const Manager::VariableDataType v,
const std::string &  unit = "" 
)

Register a variable that takes floating-point arguments (see Variable::Manager::ParameterFunctionPtr).

Definition at line 356 of file Manager.cc.

359{
360 if (!f) {
361 B2FATAL("No function provided for variable '" << name << "'.");
362 }
363
364 auto mapIter = m_parameter_variables.find(name);
365 if (mapIter == m_parameter_variables.end()) {
366 auto var = std::make_shared<ParameterVar>(name, f, description, m_currentGroup, variabletype);
367 std::string rawName = name.substr(0, name.find('('));
368 assertValidName(rawName);
369 B2DEBUG(19, "Registered parameter Variable " << rawName);
370 m_parameter_variables[rawName] = var;
371 m_variablesInRegistrationOrder.push_back(var.get());
372 if (!unit.empty()) {
373 var.get()->extendDescriptionString(":Unit: " + unit);
374 }
375 } else {
376 B2FATAL("A variable named '" << name << "' was already registered! Note that all variables need a unique name!");
377 }
378}

◆ resolveAlias()

std::string resolveAlias ( const std::string &  alias)

Resolve alias Return original variable name.

Definition at line 139 of file Manager.cc.

140{
141
142 assertValidName(alias);
143
144 if (m_alias.find(alias) == m_alias.end()) {
145 return alias;
146 } else {
147 return m_alias[alias];
148 }
149}

◆ resolveCollections()

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 collection just the variable itself.

Definition at line 179 of file Manager.cc.

180{
181
182 std::vector<std::string> temp;
183
184 for (const auto& var : variables) {
185 auto it = m_collection.find(var);
186 if (it != m_collection.end()) {
187 temp.insert(temp.end(), it->second.begin(), it->second.end());
188 } else {
189 temp.push_back(var);
190 }
191 }
192 return temp;
193
194}

◆ setVariableGroup()

void setVariableGroup ( const std::string &  groupName)

All variables registered after VARIABLE_GROUP(groupName) will be added to this group.

Definition at line 208 of file Manager.cc.

209{
210 m_currentGroup = groupName;
211}

Member Data Documentation

◆ m_alias

std::map<std::string, std::string> m_alias
private

List of registered aliases.

Definition at line 295 of file Manager.h.

◆ m_collection

std::map<std::string, std::vector<std::string> > m_collection
private

List of registered collections.

Definition at line 297 of file Manager.h.

◆ m_currentGroup

std::string m_currentGroup
private

Group last set via VARIABLE_GROUP().

Definition at line 289 of file Manager.h.

◆ m_deprecated

std::map<std::string, std::pair<bool, std::string> > m_deprecated
private

List of deprecated variables.

Definition at line 305 of file Manager.h.

◆ m_meta_variables

std::map<std::string, std::shared_ptr<MetaVar> > m_meta_variables
private

List of registered meta variables.

Definition at line 303 of file Manager.h.

◆ m_parameter_variables

std::map<std::string, std::shared_ptr<ParameterVar> > m_parameter_variables
private

List of registered parameter variables.

Definition at line 301 of file Manager.h.

◆ m_variables

std::map<std::string, std::shared_ptr<Var> > m_variables
private

List of registered variables.

Definition at line 299 of file Manager.h.

◆ m_variablesInRegistrationOrder

std::vector<const VarBase*> m_variablesInRegistrationOrder
private

List of variables in registration order.

Definition at line 292 of file Manager.h.


The documentation for this class was generated from the following files: