33 createG4SystemFactory(gopts,
35 gopts->getOptionalScalarString(
"useBackupMaterial"),
36 gopts->getRequiredScalarInt(
"check_overlaps")
40 buildMaterials(gsystemMap);
43 buildDefaultMaterialsElementsAndIsotopes();
47 std::vector<GVolume *> thisIterationRemainingVolumes;
48 unsigned long previousRemainingVolumes = 0;
51 thisIterationRemainingVolumes.clear();
54 for (
auto &[systemName,
gsystem]: *gsystemMap) {
55 const std::string defaultG4Factory = g4FactoryNameFromSystemFactory(
gsystem->getFactoryName());
57 for (
auto &[volumeName, gvolumePtr]:
gsystem->getGVolumesMap()) {
58 auto *gvolume = gvolumePtr.get();
65 if (!build_g4volume(gvolume, objectsFactory)) {
67 if (gvolume->getExistence()) {
68 log->info(2,
" >> adding volumeName <%1> to the list of remaining volumes", volumeName);
69 thisIterationRemainingVolumes.push_back(gvolume);
75 if (!thisIterationRemainingVolumes.empty()) {
77 2,
"G4World: %1 : %2 remaining motherless g4volumes to be built:", systemName,
78 thisIterationRemainingVolumes.size());
79 for (
auto *gvolumeLeft: thisIterationRemainingVolumes) {
81 2,
"G4World: %1 with mother <%2> ", gvolumeLeft->getName(),
82 gvolumeLeft->getG4MotherName());
89 if (previousRemainingVolumes != 0 && !thisIterationRemainingVolumes.empty() &&
90 thisIterationRemainingVolumes.size() >= previousRemainingVolumes) {
91 for (
auto *gvolumeLeft: thisIterationRemainingVolumes) {
93 " >> %1 with mother <%2> not built", gvolumeLeft->getName(),
94 gvolumeLeft->getG4MotherName());
98 "dependencies are not being resolved: their number should diminish. Above are the "
99 "outstanding gvolumes");
101 previousRemainingVolumes = thisIterationRemainingVolumes.size();
102 }
while (!thisIterationRemainingVolumes.empty());
105 buildOpticalSurfaces(gsystemMap);
108 if (gopts->getSwitch(
"showPredefinedMaterials")) { G4NistManager::Instance()->ListMaterials(
"all"); }
111 if (gopts->getSwitch(
"printSystemsMaterials")) {
112 auto matTable = (G4MaterialTable *) G4Material::GetMaterialTable();
113 for (
auto thisMat: *matTable) {
115 0,
"%1G4World: GEMC Material: <%2>, density: %3g/cm3", 2, thisMat->GetName(),
116 thisMat->GetDensity() / (CLHEP::g / CLHEP::cm3));
119 for (
auto &[material, component]: thisMat->GetMatComponents()) {
120 if (component > 0.0) {
121 log->info(0,
"element%1number of atoms: %2", material->GetName(), component);
122 }
else {
log->info(0,
"element%1fractional mass: %2", material->GetName(), component); }
131 auto it = g4volumesMap.find(volumeName);
132 return (it != g4volumesMap.end()) ? it->second :
nullptr;
137 bool forceToAllDaughters) {
138 auto it = g4volumesMap.find(volumeName);
139 if (it != g4volumesMap.end()) it->second->
setFieldManager(fm, forceToAllDaughters);
143std::string G4World::g4FactoryNameFromSystemFactory(
const std::string &factory)
const {
151 "gsystemFactory factory <%1> is not mapped to any G4SystemFactory", factory);
155void G4World::createG4SystemFactory(
const std::shared_ptr<GOptions> &gopts,
157 const std::optional<std::string> &backup_material,
158 int check_overlaps) {
160 GManager manager(gopts);
167 for (
auto &[gsystemName, gsystem]: *gsystemsMap) {
168 std::string factory = gsystem->getFactoryName();
169 std::string g4Factory = g4FactoryNameFromSystemFactory(factory);
171 log->info(2,
"G4World: creating factory <%1> to for system <%2>", g4Factory, gsystemName);
177 if (!g4systemFactory.contains(g4Factory)) {
178 manager.RegisterObjectFactory<G4NativeSystemFactory>(g4Factory, gopts);
182 manager.RegisterObjectFactory<G4CadSystemFactory>(g4Factory, gopts);
187 if (!g4systemFactory.contains(g4Factory)) {
188 g4systemFactory[g4Factory] = manager.CreateObject<G4ObjectsFactory>(g4Factory);
189 g4systemFactory[g4Factory]->initialize_context(check_overlaps, backup_material);
194 for (
const auto &[volumeName, gvolume]: gsystem->getGVolumesMap()) {
208void G4World::buildMaterials(
SystemMap *system_map) {
211 std::vector<GMaterial *> thisIterationRemainingMaterials;
212 unsigned long previousRemainingMaterials = 0;
214 thisIterationRemainingMaterials.clear();
216 for (
const auto &[systemName, system]: *system_map) {
218 for (
const auto &[gmaterialName, gmaterialPtr]: system->getGMaterialMap()) {
219 if (createG4Material(gmaterialPtr) ==
false) {
220 thisIterationRemainingMaterials.push_back(gmaterialPtr.get());
227 if (previousRemainingMaterials != 0 && !thisIterationRemainingMaterials.empty() &&
228 thisIterationRemainingMaterials.size() >= previousRemainingMaterials) {
229 for (
auto &gmaterialLeft: thisIterationRemainingMaterials) {
230 log->warning(
"%1", gmaterialLeft->getName());
234 "Dependencies are not being resolved: their number should diminish. Above are the "
235 "Outstanding gmaterials");
237 previousRemainingMaterials = thisIterationRemainingMaterials.size();
238 }
while (!thisIterationRemainingMaterials.empty());
243 2,
"G4World: using factory <%1> to build g4volume <%2>", objectsFactory->
className(), s->
getG4Name());
Factory that converts CAD files (PLY / STL) into Geant4 tessellated solids via CADMesh.
Base class orchestrating the conversion of a GVolume into a Geant4 representation.
bool build_g4volume(const GVolume *s, std::unordered_map< std::string, G4Volume * > *g4s)
Build (or retrieve) solid, logical, and physical volumes for a given GVolume.
virtual std::string_view className() const =0
Short, human-readable factory name for logging.
Convenience container holding a Geant4 solid, logical, and physical volume.
void setFieldManager(G4FieldManager *fm, bool forceToAllDaughters)
Attach a G4FieldManager to the stored logical volume, if present.
G4World(const GWorld *gworld, const std::shared_ptr< GOptions > &gopts)
Construct and build the Geant4 world from a GEMC world.
void setFieldManagerForVolume(const std::string &volumeName, G4FieldManager *fm, bool forceToAllDaughters)
Attach a G4FieldManager to the logical volume of a named volume.
G4ObjectsFactory * get_factory(const std::string &factoryName)
Retrieve a registered factory by name.
const G4Volume * getG4Volume(const std::string &volumeName) const
Return the G4Volume wrapper for a volume name.
GBase(const std::shared_ptr< GOptions > &gopt, std::string logger_name="")
std::shared_ptr< GLogger > log
const std::string & getG4Name() const
SystemMap * getSystemsMap() const
Factory that builds Geant4 native primitive solids (G4Box, G4Cons, G4Trap, ...) from GEMC GVolume rec...
Conventions, labels, and error codes used by the g4system geometry/material layer.
Option definitions for the g4system module (geometry/material construction layer).
constexpr const char * G4SYSTEM_LOGGER
Logger name used by the module-level builder (e.g. G4World).
High-level builder that turns a GEMC world description into Geant4 geometry.
std::map< std::string, SystemPtr > SystemMap
constexpr char G4SYSTEMNATFACTORY[]
constexpr char G4SYSTEMCADFACTORY[]
constexpr int ERR_G4SYSTEMFACTORYNOTFOUND
A required Geant4 system factory was not found/mapped.
constexpr int ERR_G4DEPENDENCIESNOTSOLVED
Geometry/material dependencies could not be resolved.
constexpr char GSYSTEMCADTFACTORYLABEL[]
constexpr char GSYSTEMMYSQLTFACTORYLABEL[]
constexpr char GSYSTEMSQLITETFACTORYLABEL[]
constexpr char GSYSTEMASCIIFACTORYLABEL[]