Belle II Software development
GeometryModule.cc
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#include <geometry/modules/GeometryModule.h>
10#include <geometry/GeometryManager.h>
11#include <framework/gearbox/GearDir.h>
12#include <framework/database/DBImportObjPtr.h>
13
14#include <algorithm>
15#include <iterator>
16
17using namespace std;
18using namespace Belle2;
19
20//-----------------------------------------------------------------
21// Register the Module
22//-----------------------------------------------------------------
23REG_MODULE(Geometry);
24
26{
27 setDescription("Setup geometry description");
29
30 addParam("geometryPath", m_geometryPath,
31 "Path where the parameters for the Geometry can be found", string("/Detector"));
32 addParam("geometryType", m_geometryType,
33 "Type of geometry to build. Valid values: Full, Tracking, Display", 0);
34 addParam("components", m_components,
35 "Name of the components to be created. If not empty, all other components found "
36 "in the parameter file will be ignored", m_components);
37 addParam("excludedComponents", m_excluded,
38 "Name of the components to excluded from creation", m_excluded);
39 addParam("additionalComponents", m_additional,
40 "Name of components to be created in addition to the default parameters", m_additional);
41 addParam("assignRegions", m_assignRegions,
42 "If true, automatically assign a Geant4 Region with the name of the "
43 "creator to all volumes created by that creator", m_assignRegions);
44 addParam("ignoreIfPresent", m_ignoreIfPresent,
45 "If true this module will silently ignore if the geometry is already "
46 "present and do nothing in that case. If false a B2FATAL will be "
47 "if the geometry was already created before", m_ignoreIfPresent);
48 addParam("useDB", m_useDB, "If true load the Geometry from the database instead of the gearbox", m_useDB);
49 addParam("payloadIov", m_payloadIov, "Payload IoV when creating a geometry configuration", m_payloadIov);
50 addParam("createPayloads", m_createGeometryPayload, "If true create a "
51 "Geometry payload with the given configuration", m_createGeometryPayload);
52}
53
55{
57 if (geoManager.getTopVolume()) {
59 B2DEBUG(10, "Geometry already created, skipping");
60 return;
61 } else {
62 B2FATAL("Geometry already created, more than one Geometry module present?");
63 }
64 }
65
66 // filter out components which are not part of the load-able geometry
67 vector<string> filteredComponents;
68 copy_if(m_components.begin(), m_components.end(),
69 std::back_inserter(filteredComponents), [](const std::string & component) { return component != "TRG"; });
70
71 geoManager.setDetectorComponents(filteredComponents);
75
77 B2INFO("Creating Database configuration.");
78 if (m_payloadIov.size() != 4) {
79 B2ERROR("Geometry: payloadIov must be exactly 4 values: [first experiment, first run, final experiment, final run]");
80 return;
81 }
84 if (iov.empty()) {
85 B2ERROR("Cannot create payloads for an empty iov");
86 return;
87 }
88 import.construct(geoManager.createGeometryConfig(GearDir(m_geometryPath), iov));
89 import.import(iov);
90 return;
91 }
92
93 if (m_useDB) {
94 if (getParam<std::string>("geometryPath").isSetInSteering()) {
95 B2WARNING("Loading Geometry from Database: parameter 'geometryPath' is ignored");
96 }
97 for (auto par : {"components", "additionalComponents", "excludedComponents"}) {
98 if (getParam<std::vector<std::string>>(par).isSetInSteering()) {
99 B2WARNING("Loading Geometry from Database: parameter '" << par << "' is ignored");
100 }
101 }
103 if (!m_geometryConfig->isValid()) {
104 B2ERROR("Cannot create Geometry from Database: no configuration found");
105 return;
106 }
107 // Make sure that we abort as soon as the geometry changes
108 m_geometryConfig->addCallback([]() {B2FATAL("Geometry cannot change during processing, aborting");});
109 geoManager.createGeometry(**m_geometryConfig);
110 } else {
112 if (!evtMeta.isValid() or not(evtMeta->getExperiment() == 0 and evtMeta->getRun() == 0)) {
113 B2FATAL(R"RAW(We no longer allow to disable the database when exp, run != 0, 0
114
115 If you want to create geometry configuration please create them from the
116 correct set of xml files and test them with exp,run == 0, 0 and then create
117 payloads for the correct iovs.
118
119 Otherwise just use the database configuration created by the experts.
120
121 This is for your own protection.)RAW");
122 return;
123 }
124 B2WARNING(R"RAW(You've decided to disable database for the Geometry.
125
126 Be aware, this is ONLY VALID for debugging purposes and validation of
127 geometry updates.
128
129 Do NOT USE THIS just to disable some parts in the geometry.
130
131 -> This is in any case very dangerous and will result in unrealistic
132 results. If you really want this and really know what you are doing you
133 will have no problems to create and use a custom geometry configuration
134 using the parameter `createPayloads=True`.
135
136 DEFINITELY don't use this just because it works without internet connection.
137
138 -> Please just use `b2conditionsdb` to download a snapshot of the global
139 tag you want to use ahead of time. Your results will not be correct if
140 you disable building the geometry from the database configuration
141
142 YOU HAVE BEEN WARNED!)RAW");
144 }
145}
146
148{
150 geoManager.clear();
151 delete m_geometryConfig;
152}
Class for importing a single object to the database.
Class for accessing objects in the database.
Definition: DBObjPtr.h:21
GearDir is the basic class used for accessing the parameter store.
Definition: GearDir.h:31
std::string m_geometryPath
Path for the geometry in the parameter space.
std::vector< std::string > m_components
Components to be created, if empty all components will be created.
void initialize() override
Create geometry.
bool m_assignRegions
Whether or not a region should be assigned to all volumes created by a given creator.
void terminate() override
Clean up the geometry.
std::vector< std::string > m_additional
Components to be added in addition to the default ones.
std::vector< int > m_payloadIov
Payload iov when creating a geometry configuration.
DBObjPtr< GeoConfiguration > * m_geometryConfig
Database object pointing to the geometry configuration in case we load the geometry from database.
bool m_createGeometryPayload
If true we need to create a payload.
std::vector< std::string > m_excluded
Components to excluded from creation.
GeometryModule()
Constructor.
int m_geometryType
Type of the geometry to be built.
bool m_useDB
Whether or not to build the geometry from the database.
bool m_ignoreIfPresent
Whether or not this module will raise an error if the geometry is already present.
A class that describes the interval of experiments/runs for which an object in the database is valid.
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:214
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
Definition: Module.cc:208
@ 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:80
Type-safe access to single objects in the data store.
Definition: StoreObjPtr.h:96
bool isValid() const
Check whether the object was created.
Definition: StoreObjPtr.h:111
Class to manage the creation and conversion of the geometry.
void setDetectorComponents(const std::vector< std::string > &components)
Set the names of the components to create.
void setAssignRegions(bool assignRegions)
Choose whether a region should be assigned to each creator.
void setExcludedComponents(const std::vector< std::string > &components)
Set the names of the components to exclude from creation.
G4VPhysicalVolume * getTopVolume()
Return a pointer to the top volume.
GeoConfiguration createGeometryConfig(const GearDir &detectorDir, const IntervalOfValidity &iov)
Create Geometry configuration object.
void createGeometry(const GearDir &params, GeometryTypes type=FullGeometry)
Create Geometry.
void clear()
Delete the existing Geant4 Geometry.
static GeometryManager & getInstance()
Return a reference to the instance.
void setAdditionalComponents(const std::vector< std::string > &components)
Set the names of addtional components to be added to the default set.
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:560
ModuleParam< T > & getParam(const std::string &name) const
Returns a reference to a parameter.
Definition: Module.h:573
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:650
@ FullGeometry
Full geometry for simulation.
Abstract base class for different kinds of events.
STL namespace.