Belle II Software  release-06-02-00
ExportGeometryModule.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/ExportGeometryModule.h>
10 
11 #include <framework/utilities/FileSystem.h>
12 #include <geometry/GeometryManager.h>
13 #include <TGeoManager.h>
14 #ifdef HAS_GDML
15 #include <G4GDMLParser.hh>
16 #endif
17 
18 using namespace std;
19 using namespace Belle2;
20 
21 //-----------------------------------------------------------------
22 // Register the Module
23 //-----------------------------------------------------------------
24 REG_MODULE(ExportGeometry)
25 
26 //-----------------------------------------------------------------
27 // Implementation
28 //-----------------------------------------------------------------
29 
31 {
32  //Set module properties
33  setDescription("Saves the Belle II detector geometry to a root or GDML file. "
34  "GDML support is only available if Geant4 has been compiled with "
35  "GDML enabled. This requires the xerces XML parser headers to "
36  "be installed when compiling the externals");
37 
38  //Parameter definition
39  addParam("Filename", m_filenameROOT, "The filename of the output file.", string("Belle2.root"));
40  addParam("asGDML", m_exportAsGDML, "If True export as Geant4 GDML format, "
41  "otherwise export as ROOT file", m_exportAsGDML);
42  addParam("addAddresses", m_gdmlAdresses, "If True the names in the GDML file "
43  "will be unique by adding their pointer address. This makes checking "
44  "for differences problematic but would allow to use the Geometry directly",
45  m_gdmlAdresses);
46 }
47 
48 
49 ExportGeometryModule::~ExportGeometryModule() = default;
50 
51 
52 void ExportGeometryModule::initialize()
53 {
54  //Check parameters
55  if (!FileSystem::fileDirExists(m_filenameROOT)) {
56  B2ERROR("Parameter <Filename>: The path of the filename " << m_filenameROOT << " does not exist !");
57  }
58 #ifndef HAS_GDML
59  if (m_exportAsGDML) {
60  B2ERROR("This Geant4 version does not have GDML support, please recompile the externals with the xerces headers installed");
61  }
62 #endif
63 }
64 
65 
66 void ExportGeometryModule::beginRun()
67 {
68  if (!geometry::GeometryManager::getInstance().getTopVolume()) {
69  B2ERROR("No Geometry found, cannot export anything");
70  return;
71  }
72  if (m_exportAsGDML) {
73 #ifdef HAS_GDML
74  G4GDMLParser parser;
75  if (FileSystem::fileExists(m_filenameROOT)) {
76  B2ERROR("Cannot export Geometry to GDML: file '" << m_filenameROOT << "' already exists");
77  } else {
78  parser.Write(m_filenameROOT, geometry::GeometryManager::getInstance().getTopVolume(), m_gdmlAdresses);
79  }
80  return;
81 #else
82  // we should never come here as we checked in initialize but just in case ...
83  B2FATAL("This Geant4 version does not have GDML support, please recompile the externals with the xerces headers installed");
84 #endif
85  }
86  geometry::GeometryManager::getInstance().createTGeoRepresentation();
87  gGeoManager->Export(m_filenameROOT.c_str());
88 }
The ExportGeometry module.
Base class for Modules.
Definition: Module.h:72
#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.