BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSGeometryFactory.cc
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) BDSIM Collaboration, 2001 - 2026.
3
4This file is part of BDSIM.
5
6BDSIM is free software: you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published
8by the Free Software Foundation version 3 of the License.
9
10BDSIM is distributed in the hope that it will be useful, but
11WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
17*/
18#include "BDSDebug.hh"
19#include "BDSException.hh"
20#include "BDSSDEnergyDeposition.hh"
21#include "BDSGeometryExternal.hh"
22#include "BDSGeometryFactory.hh"
23#include "BDSGeometryFactoryBase.hh"
24#ifdef USE_GDML
25#include "BDSGeometryFactoryGDML.hh"
26#endif
27#include "BDSGeometryFactorySQL.hh"
28#include "BDSGeometryType.hh"
29#include "BDSSDType.hh"
30#include "BDSUtilities.hh"
31
32#include "G4String.hh"
33#include "G4Types.hh"
34
35#include <map>
36#include <string>
37#include <utility>
38
40
47
49{
50#ifdef USE_GDML
52#else
53 gdml = nullptr;
54#endif
56}
57
58BDSGeometryFactory::~BDSGeometryFactory()
59{
60 delete gdml;
61 delete sql;
62 for (auto& geom : storage)
63 {delete geom;}
64 instance = nullptr;
65}
66
68{
69 switch(type.underlying())
70 {
71#ifdef USE_GDML
72 case BDSGeometryType::gdml:
73 {return gdml; break;}
74#endif
75 case BDSGeometryType::mokka:
76 {return sql; break;}
77 default:
78 {
79 G4cout << "Unsupported factory type " << type;
80 return nullptr;
81 }
82 }
83}
84
86 const G4String& formatAndFileName,
87 std::map<G4String, G4Colour*>* colourMapping,
88 G4bool autoColour,
89 G4double suggestedLength,
90 G4double suggestedHorizontalWidth,
91 std::vector<G4String>* namedVacuumVolumes,
92 G4bool makeSensitive,
93 BDSSDType sensitivityType,
94 BDSSDType vacuumSensitivityType,
95 G4bool stripOuterVolumeAndMakeAssembly,
96 G4UserLimits* userLimitsToAttachToAllLVs,
97 G4bool dontReloadGeometry)
98{
99 std::pair<G4String, G4String> ff = BDS::SplitOnColon(formatAndFileName);
100 G4String fileName = BDS::GetFullPath(ff.second);
101
102 G4String searchName = fileName;
103 // If we strip the outer volume we're technically modifying the geometry
104 // and should prepare a unique load of it and cache that otherwise, we
105 // will get either the original or the stripped version only if we place
106 // the load the same geometry twice with/without stripping
107 if (stripOuterVolumeAndMakeAssembly)
108 {searchName += "_stripped";}
109
110 // we have this option to purposively not reload geometry uniquely for multiple
111 // instances by different component names - we need this sometimes, but we must
112 // use it cautiously knowing all the instances will be identical
113 // therefore we use a fixed nonsense name that someone is prevented from calling
114 // a component (because it's an option)
115 G4String searchComponentName = componentName;
116 if (dontReloadGeometry)
117 {searchComponentName = "dontReloadGeometry";}
118 // we must still build it with component name in case it's preprocessed. If we used
119 // 'dontReloadGeometry' then variables / stuff in that file could be degeneratively
120 // named in other unique geometry files that are loaded.
121
122 auto nameAndField = std::make_pair((std::string)searchName, searchComponentName);
123 const auto search = registry.find(nameAndField);
124 if (search != registry.end())
125 {return search->second;}// it was found already in registry
126 // else wasn't found so continue
127
128 // Check the file exists.
129 if (!BDS::FileExists(fileName))
130 {throw BDSException(__METHOD_NAME__, "No such file \"" + fileName + "\"");}
131
134 if (!factory)
135 {return nullptr;}
136
137 BDSGeometryExternal* result = factory->Build(componentName,
138 fileName,
139 colourMapping,
140 autoColour,
141 suggestedLength,
142 suggestedHorizontalWidth,
143 namedVacuumVolumes,
144 makeSensitive,
145 sensitivityType,
146 vacuumSensitivityType,
147 userLimitsToAttachToAllLVs);
148
149 if (result)
150 {
151 if (stripOuterVolumeAndMakeAssembly)
153
154 // cache using optionally modified name
155 auto key = std::make_pair((std::string)searchName, searchComponentName);
156 registry[key] = result;
157 storage.insert(result);
158 }
159
160 return result;
161}
General exception with possible name of object and message.
void StripOuterAndMakeAssemblyVolume()
Change from a container logical volume to an assembly volume.
A loaded piece of externally provided geometry.
Base class for external geometry loading factories.
virtual BDSGeometryExternal * Build(G4String componentName, G4String fileName, std::map< G4String, G4Colour * > *colourMapping=nullptr, G4bool autoColour=true, G4double suggestedLength=0, G4double suggestedHorizontalWidth=0, std::vector< G4String > *vacuumBiasVolumeNames=nullptr, G4bool makeSensitive=true, BDSSDType sensitivityType=BDSSDType::energydep, BDSSDType vacuumSensitivityType=BDSSDType::energydepvacuum, G4UserLimits *userLimitsToAttachToAllLVs=nullptr)=0
Main method to load and construct geometry.
Interface to Geant4's GDML loader.
Geometry factory for SQL geometry.
Interface to external geometry construction.
BDSGeometryExternal * BuildGeometry(G4String componentName, const G4String &formatAndFilePath, std::map< G4String, G4Colour * > *colourMapping=nullptr, G4bool autoColour=true, G4double suggestedLength=0, G4double suggestedHorizontalWidth=0, std::vector< G4String > *namedVacuumVolumes=nullptr, G4bool makeSensitive=true, BDSSDType sensitivityType=BDSSDType::energydep, BDSSDType vacuumSensitivityType=BDSSDType::energydepvacuum, G4bool stripOuterVolumeAndMakeAssembly=false, G4UserLimits *userLimitsToAttachToAllLVs=nullptr, G4bool dontReloadGeometry=false)
static BDSGeometryFactory * Instance()
Singleton accessor.
static BDSGeometryFactory * instance
Singleton instance.
BDSGeometryFactoryBase * gdml
Factory instance.
std::map< std::pair< std::string, std::string >, BDSGeometryExternal * > registry
BDSGeometryFactoryBase * GetAppropriateFactory(BDSGeometryType type)
Get the appropriate geometry factory.
BDSGeometryFactory()
Private accessor as singleton.
std::set< BDSGeometryExternal * > storage
BDSGeometryFactoryBase * sql
Factory instance.
Improve type-safety of native enum data type in C++.
type underlying() const
return underlying value (can be used in switch statement)
std::pair< G4String, G4String > SplitOnColon(const G4String &formatAndPath)
BDSGeometryType DetermineGeometryType(G4String geometryType)
function that gives corresponding enum value for string (case-insensitive)
G4String GetFullPath(G4String filename, bool excludeNameFromPath=false, bool useCWDForPrefix=false)
G4bool FileExists(const G4String &filename)
Checks if filename exists.