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