Belle II Software  release-08-01-10
Gearbox Class Reference

Singleton class responsible for loading detector parameters from an XML file. More...

#include <Gearbox.h>

Inheritance diagram for Gearbox:
Collaboration diagram for Gearbox:

Classes

struct  PathOverride
 Struct to override a path in the XML file with a custom value. More...
 
struct  PathValue
 Struct for caching results from the xml file. More...
 

Public Types

enum  { c_DefaultCacheSize = 200 }
 Default cache size (in entries) for the Gearbox.
 

Public Member Functions

 ~Gearbox ()
 Free structures on destruction.
 
void setBackends (const std::vector< std::string > &backends)
 Select the backends to use to find resources. More...
 
void clearBackends ()
 Clear list of backends.
 
void addOverride (const PathOverride &poverride)
 Add an override for a given XPath expression.
 
void clearOverrides ()
 Clear all existing overrides.
 
void open (const std::string &name="Belle2.xml", size_t cacheSize=c_DefaultCacheSize)
 Open connection to backend and parse tree. More...
 
void close ()
 Free internal structures of previously parsed tree and clear cache.
 
bool isOpen () const
 Return the state of the Gearbox. More...
 
virtual int getNumberNodes (const std::string &path="") const override
 Return the number of nodes a given path will expand to. More...
 
virtual std::string getString (const std::string &path="") const noexcept(false) override
 Get the parameter path as a string. More...
 
std::string getString (const std::string &path, const std::string &defaultValue) const
 Get the parameter path as a string. More...
 
virtual std::pair< std::string, std::string > getStringWithUnit (const std::string &path="") const noexcept(false) override
 Get the parameter path as string and also return the unit it was defined with. More...
 
virtual const TObject * getTObject (const std::string &path) const noexcept(false) override
 Get the parameter path as a TObject. More...
 
GearDir getDetectorComponent (const std::string &component)
 Return GearDir representing a given DetectorComponent. More...
 
bool exists (const std::string &path="") const
 Check if a given parameter path exists. More...
 
 operator bool () const
 Convert to bool to simplify checking of existence. More...
 
std::vector< GearDirgetNodes (const std::string &path="") const
 Get vector of GearDirs which point to all the nodes the given path evaluates to. More...
 
std::string getPath () const
 Return path of the current interface.
 
double getDouble (const std::string &path="") const noexcept(false)
 Get the parameter path as a double. More...
 
double getDouble (const std::string &path, double defaultValue) const noexcept(false)
 Get the parameter path as a double. More...
 
int getInt (const std::string &path="") const noexcept(false)
 Get the parameter path as a int. More...
 
int getInt (const std::string &path, int defaultValue) const noexcept(false)
 Get the parameter path as a int. More...
 
bool getBool (const std::string &path="") const noexcept(false)
 Get the parameter path as a bool. More...
 
bool getBool (const std::string &path, bool defaultValue) const
 Get the parameter path as a bool. More...
 
double getWithUnit (const std::string &path) const noexcept(false)
 Get the parameter path as a double converted to the standard unit. More...
 
double getWithUnit (const std::string &path, double defaultValue) const noexcept(false)
 Get the parameter path as a double converted to the standard unit. More...
 
std::vector< double > getArray (const std::string &path) const noexcept(false)
 Get the parameter path as a list of double values converted to the standard unit. More...
 
std::vector< double > getArray (const std::string &path, const std::vector< double > &defaultValue) const noexcept(false)
 Get the parameter path as a list of double values converted to the standard unit. More...
 
double getLength (const std::string &path="") const noexcept(false)
 Get the parameter path as a double converted to the standard length unit. More...
 
double getLength (const std::string &path, double defaultValue) const noexcept(false)
 Get the parameter path as a double converted to the standard length unit. More...
 
double getAngle (const std::string &path="") const noexcept(false)
 Get the parameter path as a double converted to the standard angle unit. More...
 
double getAngle (const std::string &path, double defaultValue) const noexcept(false)
 Get the parameter path as a double converted to the standard angle unit. More...
 
double getEnergy (const std::string &path="") const noexcept(false)
 Get the parameter path as a double converted to the standard energy unit. More...
 
double getEnergy (const std::string &path, double defaultValue) const noexcept(false)
 Get the parameter path as a double converted to the standard energy unit. More...
 
double getDensity (const std::string &path="") const noexcept(false)
 Get the parameter path as a double converted to the standard density unit. More...
 
double getDensity (const std::string &path, double defaultValue) const noexcept(false)
 Get the parameter path as a double converted to the standard density unit. More...
 
double getTime (const std::string &path="") const noexcept(false)
 Get the parameter path as a double converted to the standard time unit. More...
 
double getTime (const std::string &path, double defaultValue) const noexcept(false)
 Get the parameter path as a double converted to the standard time unit. More...
 
double getFrequency (const std::string &path="") const noexcept(false)
 Get the parameter path as a double converted to the standard frequency unit. More...
 
double getFrequency (const std::string &path, double defaultValue) const noexcept(false)
 Get the parameter path as a double converted to the standard frequency unit. More...
 

Static Public Member Functions

static GearboxgetInstance ()
 Return reference to the Gearbox instance.
 
static void registerInputHandler (const std::string &prefix, gearbox::InputHandler::Factory *factory)
 Register a new input handler. More...
 

Protected Member Functions

std::string ensureNode (const std::string &path) const
 make sure the path really corresponds to an XPath node expression by removing trailing slashes
 
std::string ensurePath (const std::string &path) const
 make sure the path really corresponds to a path by appending a trailing slash if neccessary
 
std::string addIndex (const std::string &path, int index) const
 add [index] to the path (after stripping trailing slashes)
 
std::string joinPath (const std::string &path, const std::string &subpath) const
 joind to paths, inserting a slash if neccessary
 

Protected Attributes

std::string m_path
 path of the current interface
 

Private Member Functions

 Gearbox ()
 Singleton: private constructor.
 
 Gearbox (const Gearbox &)=delete
 Singleton: private copy constructor.
 
Gearboxoperator= (const Gearbox &)=delete
 Also no assignment operator.
 
gearbox::InputContextopenXmlUri (const std::string &uri) const
 Function to be called when libxml requests a new input uri to be opened.
 
void overridePathValue (const PathOverride &poverride)
 Change the value of a given path expression.
 
PathValue getPathValue (const std::string &path) const
 Return the (cached) value of a given path.
 

Private Attributes

xmlDocPtr m_xmlDocument
 Pointer to the libxml Document structure.
 
xmlXPathContextPtr m_xpathContext
 Pointer to the libxml XPath context.
 
MRUCache< std::string, PathValue > * m_parameterCache
 Cache for already queried paths.
 
std::map< std::string, TObject * > m_ownedObjects
 Map of queried objects (path -> TObject*). More...
 
std::vector< gearbox::InputHandler * > m_handlers
 List of input handlers which will be used to find resources.
 
std::map< std::string, gearbox::InputHandler::Factory * > m_registeredHandlers
 Map of registered InputHandlers.
 
std::vector< PathOverridem_overrides
 the existing overrides
 

Friends

void * gearbox::openXmlUri (const char *)
 friend to internal c-like function to interface libxml2 callback More...
 

Detailed Description

Singleton class responsible for loading detector parameters from an XML file.

See also
GearboxModule

Definition at line 34 of file Gearbox.h.

Member Function Documentation

◆ exists()

bool exists ( const std::string &  path = "") const
inlineinherited

Check if a given parameter path exists.

Returns
true if path exists, false otherwise

Definition at line 54 of file Interface.h.

54 { return getNumberNodes(path) > 0; }
virtual int getNumberNodes(const std::string &path="") const =0
Return the number of nodes a given path will expand to.

◆ getAngle() [1/2]

double getAngle ( const std::string &  path,
double  defaultValue 
) const
inlinenoexceptinherited

Get the parameter path as a double converted to the standard angle unit.

Gearbox will try to determine the unit with which the parameter was saved and convert it to the default system. If no unit can be determinded, it will be assumed that the parameter does not need to be converted.

If the parameter is empty or does not exist, the defaultValue will be returned.

Exceptions
gearbox::PathEmptyErrorif path is empty or does not exist
gearbox::ConversionErrorif the value could not converted to a numerical type
Parameters
pathPath of the parameter to get
defaultValueValue to return if the Path es empty or does not exist
Returns
value of the parameter

Definition at line 321 of file Interface.h.

◆ getAngle() [2/2]

double getAngle ( const std::string &  path = "") const
inlinenoexceptinherited

Get the parameter path as a double converted to the standard angle unit.

Gearbox will try to determine the unit with which the parameter was saved and convert it to the default system. If no unit can be determinded, it will be assumed that the parameter does not need to be converted.

Exceptions
gearbox::PathEmptyErrorif path is empty or does not exist
gearbox::ConversionErrorif the value could not converted to a numerical type
Parameters
pathPath of the parameter to get
Returns
value of the parameter

Definition at line 299 of file Interface.h.

◆ getArray() [1/2]

std::vector< double > getArray ( const std::string &  path) const
noexceptinherited

Get the parameter path as a list of double values converted to the standard unit.

The content of the parameter will be split at whitespace, comma and semi-colon and returned as a vector of numbers

Gearbox will try to determine the unit with which the parameter was saved and convert it to the default system. If no unit can be determinded, it will be assumed that the parameter does not need to be converted.

Exceptions
gearbox::PathEmptyErrorif path is empty or does not exist
gearbox::ConversionErrorif the value could not converted to a numerical type
Parameters
pathPath of the parameter to get
Returns
value of the parameter

Definition at line 123 of file Interface.cc.

124  {
125  using tokenizer = boost::tokenizer<boost::char_separator<char> >;
126  boost::char_separator<char> sep(",; \t\n\r");
127 
128  std::pair<std::string, std::string> value = getStringWithUnit(path);
129  tokenizer tokens(value.first, sep);
130  std::vector<double> result;
131  double numValue(0);
132  for (const std::string& tok : tokens) {
133  try {
134  numValue = boost::lexical_cast<double>(tok);
135  } catch (boost::bad_lexical_cast&) {
136  throw (ConversionError() << path << value.first);
137  }
138  if (!value.second.empty()) {
139  numValue = Unit::convertValue(numValue, value.second);
140  }
141  result.push_back(numValue);
142  }
143  return result;
144  }
virtual std::pair< std::string, std::string > getStringWithUnit(const std::string &path) const noexcept(false)=0
Get the parameter path as string and also return the unit it was defined with.
static double convertValue(double value, const std::string &unitString)
Converts a floating point value to the standard framework unit.
Definition: UnitConst.cc:129

◆ getArray() [2/2]

std::vector< double > getArray ( const std::string &  path,
const std::vector< double > &  defaultValue 
) const
noexceptinherited

Get the parameter path as a list of double values converted to the standard unit.

The content of the parameter will be split at whitespace, comma and semi-colon and returned as a vector of numbers

Gearbox will try to determine the unit with which the parameter was saved and convert it to the default system. If no unit can be determinded, it will be assumed that the parameter does not need to be converted.

If the parameter is empty or does not exist, the defaultValue will be returned.

Exceptions
gearbox::ConversionErrorif the value could not converted to a numerical type
Parameters
pathPath of the parameter to get
defaultValueValue to return if the Path es empty or does not exist
Returns
value of the parameter

Definition at line 146 of file Interface.cc.

◆ getBool() [1/2]

bool getBool ( const std::string &  path,
bool  defaultValue 
) const
inherited

Get the parameter path as a bool.

If the parameter is empty or does not exist, the defaultValue will be returned.

Parameters
pathPath of the parameter to get
defaultValueValue to return if the Path es empty or does not exist
Returns
value of the parameter

Definition at line 88 of file Interface.cc.

◆ getBool() [2/2]

bool getBool ( const std::string &  path = "") const
noexceptinherited

Get the parameter path as a bool.

Exceptions
gearbox::PathEmptyErrorif path is empty or does not exist
Parameters
pathPath of the parameter to get
Returns
value of the parameter

Definition at line 80 of file Interface.cc.

◆ getDensity() [1/2]

double getDensity ( const std::string &  path,
double  defaultValue 
) const
inlinenoexceptinherited

Get the parameter path as a double converted to the standard density unit.

Gearbox will try to determine the unit with which the parameter was saved and convert it to the default system. If no unit can be determinded, it will be assumed that the parameter does not need to be converted.

If the parameter is empty or does not exist, the defaultValue will be returned.

Exceptions
gearbox::PathEmptyErrorif path is empty or does not exist
gearbox::ConversionErrorif the value could not converted to a numerical type
Parameters
pathPath of the parameter to get
defaultValueValue to return if the Path es empty or does not exist
Returns
value of the parameter

Definition at line 401 of file Interface.h.

◆ getDensity() [2/2]

double getDensity ( const std::string &  path = "") const
inlinenoexceptinherited

Get the parameter path as a double converted to the standard density unit.

Gearbox will try to determine the unit with which the parameter was saved and convert it to the default system. If no unit can be determinded, it will be assumed that the parameter does not need to be converted.

Exceptions
gearbox::PathEmptyErrorif path is empty or does not exist
gearbox::ConversionErrorif the value could not converted to a numerical type
Parameters
pathPath of the parameter to get
Returns
value of the parameter

Definition at line 379 of file Interface.h.

◆ getDouble() [1/2]

double getDouble ( const std::string &  path,
double  defaultValue 
) const
noexceptinherited

Get the parameter path as a double.

If the parameter is empty or does not exist, the defaultValue will be returned.

Exceptions
gearbox::ConversionErrorif the value could not converted to a numerical type
Parameters
pathPath of the parameter to get
defaultValueValue to return if the Path es empty or does not exist
Returns
value of the parameter

Definition at line 51 of file Interface.cc.

◆ getDouble() [2/2]

double getDouble ( const std::string &  path = "") const
noexceptinherited

Get the parameter path as a double.

Exceptions
gearbox::PathEmptyErrorif path is empty or does not exist
gearbox::ConversionErrorif the value could not converted to a numerical type
Parameters
pathPath of the parameter to get
Returns
value of the parameter

Definition at line 41 of file Interface.cc.

◆ getEnergy() [1/2]

double getEnergy ( const std::string &  path,
double  defaultValue 
) const
inlinenoexceptinherited

Get the parameter path as a double converted to the standard energy unit.

Gearbox will try to determine the unit with which the parameter was saved and convert it to the default system. If no unit can be determinded, it will be assumed that the parameter does not need to be converted.

If the parameter is empty or does not exist, the defaultValue will be returned.

Exceptions
gearbox::PathEmptyErrorif path is empty or does not exist
gearbox::ConversionErrorif the value could not converted to a numerical type
Parameters
pathPath of the parameter to get
defaultValueValue to return if the Path es empty or does not exist
Returns
value of the parameter

Definition at line 361 of file Interface.h.

◆ getEnergy() [2/2]

double getEnergy ( const std::string &  path = "") const
inlinenoexceptinherited

Get the parameter path as a double converted to the standard energy unit.

Gearbox will try to determine the unit with which the parameter was saved and convert it to the default system. If no unit can be determinded, it will be assumed that the parameter does not need to be converted.

Exceptions
gearbox::PathEmptyErrorif path is empty or does not exist
gearbox::ConversionErrorif the value could not converted to a numerical type
Parameters
pathPath of the parameter to get
Returns
value of the parameter

Definition at line 339 of file Interface.h.

◆ getFrequency() [1/2]

double getFrequency ( const std::string &  path,
double  defaultValue 
) const
inlinenoexceptinherited

Get the parameter path as a double converted to the standard frequency unit.

Gearbox will try to determine the unit with which the parameter was saved and convert it to the default system. If no unit can be determinded, it will be assumed that the parameter does not need to be converted.

If the parameter is empty or does not exist, the defaultValue will be returned.

Exceptions
gearbox::PathEmptyErrorif path is empty or does not exist
gearbox::ConversionErrorif the value could not converted to a numerical type
Parameters
pathPath of the parameter to get
defaultValueValue to return if the Path es empty or does not exist
Returns
value of the parameter

Definition at line 481 of file Interface.h.

◆ getFrequency() [2/2]

double getFrequency ( const std::string &  path = "") const
inlinenoexceptinherited

Get the parameter path as a double converted to the standard frequency unit.

Gearbox will try to determine the unit with which the parameter was saved and convert it to the default system. If no unit can be determinded, it will be assumed that the parameter does not need to be converted.

Exceptions
gearbox::PathEmptyErrorif path is empty or does not exist
gearbox::ConversionErrorif the value could not converted to a numerical type
Parameters
pathPath of the parameter to get
Returns
value of the parameter

Definition at line 459 of file Interface.h.

◆ getInt() [1/2]

int getInt ( const std::string &  path,
int  defaultValue 
) const
noexceptinherited

Get the parameter path as a int.

If the parameter is empty or does not exist, the defaultValue will be returned.

Exceptions
gearbox::ConversionErrorif the value could not converted to a numerical type
Parameters
pathPath of the parameter to get
defaultValueValue to return if the Path es empty or does not exist
Returns
value of the parameter

Definition at line 71 of file Interface.cc.

◆ getInt() [2/2]

int getInt ( const std::string &  path = "") const
noexceptinherited

Get the parameter path as a int.

Exceptions
gearbox::PathEmptyErrorif path is empty or does not exist
gearbox::ConversionErrorif the value could not converted to a numerical type
Parameters
pathPath of the parameter to get
Returns
value of the parameter

Definition at line 60 of file Interface.cc.

◆ getLength() [1/2]

double getLength ( const std::string &  path,
double  defaultValue 
) const
inlinenoexceptinherited

Get the parameter path as a double converted to the standard length unit.

Gearbox will try to determine the unit with which the parameter was saved and convert it to the default system. If no unit can be determinded, it will be assumed that the parameter does not need to be converted.

If the parameter is empty or does not exist, the defaultValue will be returned.

Exceptions
gearbox::PathEmptyErrorif path is empty or does not exist
gearbox::ConversionErrorif the value could not converted to a numerical type
Parameters
pathPath of the parameter to get
defaultValueValue to return if the Path es empty or does not exist
Returns
value of the parameter

Definition at line 281 of file Interface.h.

◆ getLength() [2/2]

double getLength ( const std::string &  path = "") const
inlinenoexceptinherited

Get the parameter path as a double converted to the standard length unit.

Gearbox will try to determine the unit with which the parameter was saved and convert it to the default system. If no unit can be determinded, it will be assumed that the parameter does not need to be converted.

Exceptions
gearbox::PathEmptyErrorif path is empty or does not exist
gearbox::ConversionErrorif the value could not converted to a numerical type
Parameters
pathPath of the parameter to get
Returns
value of the parameter

Definition at line 259 of file Interface.h.

◆ getNodes()

std::vector< GearDir > getNodes ( const std::string &  path = "") const
inherited

Get vector of GearDirs which point to all the nodes the given path evaluates to.

Parameters
pathPath of the parameters to get

Definition at line 21 of file Interface.cc.

◆ getNumberNodes()

virtual int getNumberNodes ( const std::string &  path = "") const
inlineoverridevirtual

Return the number of nodes a given path will expand to.

Returns
number of nodes, 0 if path does not exist

Implements Interface.

Definition at line 112 of file Gearbox.h.

113  {
114  return getPathValue(path).numNodes;
115  }
PathValue getPathValue(const std::string &path) const
Return the (cached) value of a given path.
Definition: Gearbox.cc:247
int numNodes
number of nodes corresponding to the path
Definition: Gearbox.h:43

◆ getString() [1/2]

std::string getString ( const std::string &  path,
const std::string &  defaultValue 
) const
inline

Get the parameter path as a string.

if the parameter is empty or does not exist, defaultValue will be returned.

Parameters
pathpath of the parameter to get
defaultValuevalue to return if the path es empty or does not exist
Returns
value of the parameter

Definition at line 138 of file Gearbox.h.

◆ getString() [2/2]

virtual std::string getString ( const std::string &  path = "") const
inlineoverridevirtualnoexcept

Get the parameter path as a string.

Exceptions
gearbox::PathEmptyErrorif path is empty or does not exist
Parameters
pathPath of the parameter to get
Returns
value of the parameter

Implements Interface.

Definition at line 123 of file Gearbox.h.

◆ getStringWithUnit()

virtual std::pair<std::string, std::string> getStringWithUnit ( const std::string &  path = "") const
inlineoverridevirtualnoexcept

Get the parameter path as string and also return the unit it was defined with.

If no unit was defined, an empty string will be returned for the unit. No parsing of the unit is performed, this funtion is primarily used by getWithUnit.

Exceptions
gearbox::PathEmptyErrorif path is empty or does not exist
Parameters
pathPath of the parameter to get
Returns
value of the parameter

Implements Interface.

Definition at line 154 of file Gearbox.h.

◆ getTime() [1/2]

double getTime ( const std::string &  path,
double  defaultValue 
) const
inlinenoexceptinherited

Get the parameter path as a double converted to the standard time unit.

Gearbox will try to determine the unit with which the parameter was saved and convert it to the default system. If no unit can be determinded, it will be assumed that the parameter does not need to be converted.

If the parameter is empty or does not exist, the defaultValue will be returned.

Exceptions
gearbox::PathEmptyErrorif path is empty or does not exist
gearbox::ConversionErrorif the value could not converted to a numerical type
Parameters
pathPath of the parameter to get
defaultValueValue to return if the Path es empty or does not exist
Returns
value of the parameter

Definition at line 441 of file Interface.h.

◆ getTime() [2/2]

double getTime ( const std::string &  path = "") const
inlinenoexceptinherited

Get the parameter path as a double converted to the standard time unit.

Gearbox will try to determine the unit with which the parameter was saved and convert it to the default system. If no unit can be determinded, it will be assumed that the parameter does not need to be converted.

Exceptions
gearbox::PathEmptyErrorif path is empty or does not exist
gearbox::ConversionErrorif the value could not converted to a numerical type
Parameters
pathPath of the parameter to get
Returns
value of the parameter

Definition at line 419 of file Interface.h.

◆ getWithUnit() [1/2]

double getWithUnit ( const std::string &  path) const
noexceptinherited

Get the parameter path as a double converted to the standard unit.

Gearbox will try to determine the unit with which the parameter was saved and convert it to the default system. If no unit can be determinded, it will be assumed that the parameter does not need to be converted

Exceptions
gearbox::PathEmptyErrorif path is empty or does not exist
gearbox::ConversionErrorif the value could not converted to a numerical type
Parameters
pathPath of the parameter to get
Returns
value of the parameter

Definition at line 97 of file Interface.cc.

◆ getWithUnit() [2/2]

double getWithUnit ( const std::string &  path,
double  defaultValue 
) const
noexceptinherited

Get the parameter path as a double converted to the standard unit.

Gearbox will try to determine the unit with which the parameter was saved and convert it to the default system. If no unit can be determinded, it will be assumed that the parameter does not need to be converted.

If the parameter is empty or does not exist, the defaultValue will be returned.

Exceptions
gearbox::ConversionErrorif the value could not converted to a numerical type
Parameters
pathPath of the parameter to get
defaultValueValue to return if the Path es empty or does not exist
Returns
value of the parameter

Definition at line 114 of file Interface.cc.

◆ isOpen()

bool isOpen ( ) const
inline

Return the state of the Gearbox.

Returns
True if the Gearbox has an initialized backend and opened a document for reading.

Definition at line 106 of file Gearbox.h.

◆ operator bool()

operator bool ( ) const
inlineinherited

Convert to bool to simplify checking of existence.

Returns
true if path exists, false otherwise

Definition at line 60 of file Interface.h.

◆ registerInputHandler()

static void registerInputHandler ( const std::string &  prefix,
gearbox::InputHandler::Factory factory 
)
inlinestatic

Register a new input handler.

Parameters
prefixprefix signaling which handler to use for a given backend uri. The prefix is everything up to the first colon in the backend uri
factoryPointer to the factory function which will return an instance of the handler

Definition at line 187 of file Gearbox.h.

Friends And Related Function Documentation

◆ gearbox::openXmlUri

void* gearbox::openXmlUri ( const char *  )
friend

friend to internal c-like function to interface libxml2 callback

This function is declared as friend since it needs access to Gearbox::openXmlUri, a function which should only be called by libxml2 and thus is declared private.

Member Data Documentation

◆ m_ownedObjects

std::map<std::string, TObject*> m_ownedObjects
mutableprivate

Map of queried objects (path -> TObject*).

Objects will be removed once they are no longer valid.

Definition at line 215 of file Gearbox.h.


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