Belle II Software  release-08-01-10
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 
17 using namespace std;
18 using namespace Belle2;
19 
20 //-----------------------------------------------------------------
21 // Register the Module
22 //-----------------------------------------------------------------
23 REG_MODULE(Geometry);
24 
25 GeometryModule::GeometryModule()
26 {
27  setDescription("Setup geometry description");
28  setPropertyFlags(c_ParallelProcessingCertified);
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 
54 void GeometryModule::initialize()
55 {
56  geometry::GeometryManager& geoManager = geometry::GeometryManager::getInstance();
57  if (geoManager.getTopVolume()) {
58  if (m_ignoreIfPresent) {
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);
72  geoManager.setExcludedComponents(m_excluded);
73  geoManager.setAdditionalComponents(m_additional);
74  geoManager.setAssignRegions(m_assignRegions);
75 
76  if (m_createGeometryPayload) {
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  }
83  IntervalOfValidity iov(m_payloadIov[0], m_payloadIov[1], m_payloadIov[2], m_payloadIov[3]);
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  }
102  m_geometryConfig = new DBObjPtr<GeoConfiguration>();
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");
143  geoManager.createGeometry(GearDir(m_geometryPath), geometry::FullGeometry);
144  }
145 }
146 
147 void GeometryModule::terminate()
148 {
149  geometry::GeometryManager& geoManager = geometry::GeometryManager::getInstance();
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
A class that describes the interval of experiments/runs for which an object in the database is valid.
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.
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.
G4VPhysicalVolume * getTopVolume()
Return a pointer to the top volume.
void setAdditionalComponents(const std::vector< std::string > &components)
Set the names of addtional components to be added to the default set.
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:650
Abstract base class for different kinds of events.