Belle II Software  release-05-02-19
GearboxModule.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2010 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Andreas Moll, Martin Ritter *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #include <framework/modules/gearbox/GearboxModule.h>
12 #include <framework/gearbox/Gearbox.h>
13 #include <framework/datastore/StoreObjPtr.h>
14 #include <framework/dataobjects/EventMetaData.h>
15 // needed for complex module parameter
16 #include <framework/core/ModuleParam.templateDetails.h>
17 
18 using namespace std;
19 using namespace Belle2;
20 
21 //-----------------------------------------------------------------
22 // Register the Module
23 //-----------------------------------------------------------------
25 
26 //-----------------------------------------------------------------
27 // Implementation
28 //-----------------------------------------------------------------
29 
30 GearboxModule::GearboxModule() : Module()
31 {
32  //Set module properties
33  setDescription("Loads the Belle II detector parameters from an XML document.");
35 
36 
37  m_backends.emplace_back("file:");
38 
39  //Parameter definition
40  addParam("backends", m_backends, "The backends to use when looking for xml "
41  "data. A backend can also contain a search path after ':'. (If none "
42  "is given, '/data' will be used.)", m_backends);
43  addParam("fileName", m_fileName, "The filename of the main xml file",
44  string("geometry/Belle2.xml"));
45  addParam("override", m_unitOverrides, "Override single values from the XML "
46  "file. This should be a list of tuples containing an xpath "
47  "expression, a value and a unit (which can be empty). The xpath "
48  "expression must resolve to exactly one node in the XML tree which "
49  "does not contain any children except text. The supplied value and "
50  "unit will be set for this node. See "
51  "framework/examples/gearbox_override.py", m_unitOverrides);
52  addParam("overrideMultiple", m_multipleOverrides, "Same as override but the "
53  "xpath expression may evaluate to more than one node in which case "
54  "all occurances are set to the supplied value and unit",
56  addParam("overridePrefix", m_overridePrefix, "Common prefix which is "
57  "prepended to all overrides. Beware that '//' has a special meaning "
58  "meaning in xpath so be careful with leading and trailing slashes "
59  "in the overrides and the prefix respectively", std::string("/Detector"));
60 }
61 
63 {
64  //gearbox might need exp/run numbers
65  StoreObjPtr<EventMetaData>().isRequired();
66 
67  Gearbox& gearbox = Gearbox::getInstance();
68  for (auto& unit : m_unitOverrides) {
69  Gearbox::PathOverride poverride;
70  poverride.path = m_overridePrefix + std::get<0>(unit);
71  poverride.value = std::get<1>(unit);
72  poverride.unit = std::get<2>(unit);
73  gearbox.addOverride(poverride);
74  }
75  for (auto& multiple : m_multipleOverrides) {
76  Gearbox::PathOverride poverride;
77  poverride.path = m_overridePrefix + std::get<0>(multiple);
78  poverride.value = std::get<1>(multiple);
79  poverride.unit = std::get<2>(multiple);
80  poverride.multiple = true;
81  gearbox.addOverride(poverride);
82  }
83 
84  gearbox.setBackends(m_backends);
85  gearbox.open(m_fileName);
86 }
87 
89 {
90  //if only file backend is used, we don't want to reread the data in every run
91  if (m_backends.size() == 1 && m_backends[0] == "file:")
92  return;
93 
94  Gearbox& gearbox = Gearbox::getInstance();
95  gearbox.close();
96  gearbox.open(m_fileName);
97 }
Belle2::GearboxModule::beginRun
void beginRun() override
Load the (possibly rundependent) parameters from the chosen backends.
Definition: GearboxModule.cc:88
Belle2::Gearbox::PathOverride::value
std::string value
New value.
Definition: Gearbox.h:65
Belle2::Module::setDescription
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:216
Belle2::Gearbox::getInstance
static Gearbox & getInstance()
Return reference to the Gearbox instance.
Definition: Gearbox.cc:74
REG_MODULE
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:652
Belle2::Module::c_ParallelProcessingCertified
@ c_ParallelProcessingCertified
This module can be run in parallel processing mode safely (All I/O must be done through the data stor...
Definition: Module.h:82
Belle2::GearboxModule::m_overridePrefix
std::string m_overridePrefix
common prefix for all value overrides
Definition: GearboxModule.h:82
Belle2::Gearbox::PathOverride::multiple
bool multiple
if true, override all nodes when more than one node matches the XPath expression, bail otherwise
Definition: Gearbox.h:70
Belle2::GearboxModule::initialize
void initialize() override
Define backends.
Definition: GearboxModule.cc:62
Belle2::Module
Base class for Modules.
Definition: Module.h:74
Belle2::Module::setPropertyFlags
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
Definition: Module.cc:210
Belle2::Gearbox::PathOverride
Struct to override a path in the XML file with a custom value.
Definition: Gearbox.h:61
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::GearboxModule::m_multipleOverrides
std::vector< std::tuple< std::string, std::string, std::string > > m_multipleOverrides
overrides to override the value and unit of many parameters
Definition: GearboxModule.h:86
Belle2::StoreObjPtr
Type-safe access to single objects in the data store.
Definition: ParticleList.h:33
Belle2::Gearbox
Singleton class responsible for loading detector parameters from an XML file.
Definition: Gearbox.h:44
Belle2::Gearbox::PathOverride::path
std::string path
XPath expression of the path to override.
Definition: Gearbox.h:63
Belle2::GearboxModule::m_backends
std::vector< std::string > m_backends
The backend specifier.
Definition: GearboxModule.h:78
Belle2::GearboxModule::m_unitOverrides
std::vector< std::tuple< std::string, std::string, std::string > > m_unitOverrides
overrides to override the value and unit of a parameter
Definition: GearboxModule.h:84
Belle2::Module::addParam
void addParam(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
Definition: Module.h:562
Belle2::GearboxModule::m_fileName
std::string m_fileName
The toplevel filename for the parameters.
Definition: GearboxModule.h:79
Belle2::Gearbox::PathOverride::unit
std::string unit
new Unit
Definition: Gearbox.h:67