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