33 createG4SystemFactory(gopts,
35 gopts->getScalarString(
"useBackupMaterial"),
36 gopts->getScalarInt(
"check_overlaps")
40 buildMaterials(gsystemMap);
43 buildDefaultMaterialsElementsAndIsotopes();
47 std::vector<GVolume *> thisIterationRemainingVolumes;
48 unsigned long allRemainingVolumes = 0;
51 thisIterationRemainingVolumes.clear();
54 for (
auto &[systemName,
gsystem]: *gsystemMap) {
55 std::string g4Factory = g4FactoryNameFromSystemFactory(
gsystem->getFactoryName());
58 for (
auto &[volumeName, gvolumePtr]:
gsystem->getGVolumesMap()) {
59 auto *gvolume = gvolumePtr.get();
62 if (!build_g4volume(gvolume, objectsFactory)) {
64 if (gvolume->getExistence()) {
65 log->
info(2,
" >> adding volumeName <", volumeName,
"> to the list of remaining volumes");
66 thisIterationRemainingVolumes.push_back(gvolume);
72 if (!thisIterationRemainingVolumes.empty()) {
73 log->
info(2,
"G4World: ", systemName,
" : ",
74 thisIterationRemainingVolumes.size(),
75 " remaining motherless g4volumes to be built:");
76 for (
auto *gvolumeLeft: thisIterationRemainingVolumes) {
77 log->
info(2,
"G4World: ", gvolumeLeft->getName(),
78 " with mother <", gvolumeLeft->getG4MotherName(),
"> ");
85 if (allRemainingVolumes != 0 && !thisIterationRemainingVolumes.empty()) {
86 if (allRemainingVolumes >= thisIterationRemainingVolumes.size()) {
87 for (
auto *gvolumeLeft: thisIterationRemainingVolumes) {
89 " with mother <", gvolumeLeft->getG4MotherName(),
"> not built");
92 "dependencies are not being resolved: their number should diminish. "
93 "Above are the outstanding gvolumes");
95 }
else { allRemainingVolumes = thisIterationRemainingVolumes.size(); }
96 }
while (!thisIterationRemainingVolumes.empty());
99 if (gopts->getSwitch(
"showPredefinedMaterials")) { G4NistManager::Instance()->ListMaterials(
"all"); }
102 if (gopts->getSwitch(
"printSystemsMaterials")) {
103 auto matTable = (G4MaterialTable *) G4Material::GetMaterialTable();
104 for (
auto thisMat: *matTable) {
105 log->
info(0, 2,
"G4World: GEMC Material: <", thisMat->GetName(),
">, density: ",
106 thisMat->GetDensity() / (CLHEP::g / CLHEP::cm3),
"g/cm3");
109 for (
auto &[material, component]: thisMat->GetMatComponents()) {
110 if (component > 0.0) {
111 log->
info(0,
"element", material->GetName(),
"number of atoms: ", component);
112 }
else {
log->
info(0,
"element", material->GetName(),
"fractional mass: ", component); }
121 auto it = g4volumesMap.find(volumeName);
122 return (it != g4volumesMap.end()) ? it->second :
nullptr;
127 bool forceToAllDaughters) {
128 auto it = g4volumesMap.find(volumeName);
129 if (it != g4volumesMap.end()) it->second->setFieldManager(fm, forceToAllDaughters);
133std::string G4World::g4FactoryNameFromSystemFactory(
const std::string &factory)
const {
135 if (factory == GSYSTEMASCIIFACTORYLABEL ||
136 factory == GSYSTEMSQLITETFACTORYLABEL ||
140 "gsystemFactory factory <", factory,
"> is not mapped to any G4SystemFactory");
144void G4World::createG4SystemFactory(
const std::shared_ptr<GOptions> &gopts,
146 const std::string &backup_material,
147 int check_overlaps) {
156 for (
auto &[gsystemName,
gsystem]: *gsystemsMap) {
157 std::string factory =
gsystem->getFactoryName();
158 std::string g4Factory = g4FactoryNameFromSystemFactory(factory);
160 log->
info(2,
"G4World: creating factory <", g4Factory,
"> to for system <", gsystemName,
">");
164 if (factory == GSYSTEMASCIIFACTORYLABEL || factory == GSYSTEMSQLITETFACTORYLABEL ||
165 factory == GSYSTEMMYSQLTFACTORYLABEL) {
166 if (g4systemFactory.find(g4Factory) == g4systemFactory.end()) {
169 }
else if (factory == GSYSTEMCADTFACTORYLABEL) {
170 if (g4systemFactory.find(GSYSTEMCADTFACTORYLABEL) == g4systemFactory.end()) {
176 if (g4systemFactory.find(g4Factory) == g4systemFactory.end()) {
177 g4systemFactory[g4Factory] = manager.CreateObject<
G4ObjectsFactory>(g4Factory);
178 g4systemFactory[g4Factory]->initialize_context(check_overlaps, backup_material);
183void G4World::buildMaterials(
SystemMap *system_map) {
186 std::vector<GMaterial *> thisIterationRemainingMaterials;
187 unsigned long allRemainingMaterials = 0;
189 thisIterationRemainingMaterials.clear();
191 for (
const auto &[systemName, system]: *system_map) {
193 for (
const auto &[gmaterialName, gmaterialPtr]: system->getGMaterialMap()) {
194 if (createG4Material(gmaterialPtr) ==
false) {
195 thisIterationRemainingMaterials.push_back(gmaterialPtr.get());
201 if (allRemainingMaterials != 0 && !thisIterationRemainingMaterials.empty()) {
202 if (allRemainingMaterials >= thisIterationRemainingMaterials.size()) {
203 for (
auto &gmaterialLeft: thisIterationRemainingMaterials) {
log->
warning(gmaterialLeft->getName()); }
205 "Dependencies are not being resolved: their number should diminish. Above are the Outstanding gmaterials");
207 }
else { allRemainingMaterials = thisIterationRemainingMaterials.size(); }
208 }
while (!thisIterationRemainingMaterials.empty());
213 "> to build g4volume <", s->
getG4Name(),
">");
Factory that converts CAD files (PLY / STL) into Geant4 tessellated solids via CADMesh.
Builds a tessellated solid from CAD files using CADMesh.
Implements solid creation for Geant4 CSG primitives and validates constructor parameter counts.
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.
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.
std::shared_ptr< GLogger > log
void warning(Args &&... args) const
void info(int level, Args &&... args) const
void error(int exit_code, Args &&... args) 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.
#define ERR_G4SYSTEMFACTORYNOTFOUND
A required Geant4 system factory was not found/mapped.
#define ERR_G4DEPENDENCIESNOTSOLVED
Geometry/material dependencies could not be resolved.
#define G4SYSTEMNATFACTORY
#define G4SYSTEMCADFACTORY
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