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 <", volumeName,
"> to the list of remaining volumes");
69 thisIterationRemainingVolumes.push_back(gvolume);
75 if (!thisIterationRemainingVolumes.empty()) {
76 log->info(2,
"G4World: ", systemName,
" : ",
77 thisIterationRemainingVolumes.size(),
78 " remaining motherless g4volumes to be built:");
79 for (
auto *gvolumeLeft: thisIterationRemainingVolumes) {
80 log->info(2,
"G4World: ", gvolumeLeft->getName(),
81 " with mother <", gvolumeLeft->getG4MotherName(),
"> ");
88 if (previousRemainingVolumes != 0 && !thisIterationRemainingVolumes.empty() &&
89 thisIterationRemainingVolumes.size() >= previousRemainingVolumes) {
90 for (
auto *gvolumeLeft: thisIterationRemainingVolumes) {
91 log->warning(
" >> ", gvolumeLeft->getName(),
92 " with mother <", gvolumeLeft->getG4MotherName(),
"> not built");
95 "dependencies are not being resolved: their number should diminish. "
96 "Above are the outstanding gvolumes");
98 previousRemainingVolumes = thisIterationRemainingVolumes.size();
99 }
while (!thisIterationRemainingVolumes.empty());
102 buildOpticalSurfaces(gsystemMap);
105 if (gopts->getSwitch(
"showPredefinedMaterials")) { G4NistManager::Instance()->ListMaterials(
"all"); }
108 if (gopts->getSwitch(
"printSystemsMaterials")) {
109 auto matTable = (G4MaterialTable *) G4Material::GetMaterialTable();
110 for (
auto thisMat: *matTable) {
111 log->info(0, 2,
"G4World: GEMC Material: <", thisMat->GetName(),
">, density: ",
112 thisMat->GetDensity() / (CLHEP::g / CLHEP::cm3),
"g/cm3");
115 for (
auto &[material, component]: thisMat->GetMatComponents()) {
116 if (component > 0.0) {
117 log->info(0,
"element", material->GetName(),
"number of atoms: ", component);
118 }
else {
log->info(0,
"element", material->GetName(),
"fractional mass: ", component); }
127 auto it = g4volumesMap.find(volumeName);
128 return (it != g4volumesMap.end()) ? it->second :
nullptr;
133 bool forceToAllDaughters) {
134 auto it = g4volumesMap.find(volumeName);
135 if (it != g4volumesMap.end()) it->second->
setFieldManager(fm, forceToAllDaughters);
139std::string G4World::g4FactoryNameFromSystemFactory(
const std::string &factory)
const {
146 "gsystemFactory factory <", factory,
"> is not mapped to any G4SystemFactory");
150void G4World::createG4SystemFactory(
const std::shared_ptr<GOptions> &gopts,
152 const std::optional<std::string> &backup_material,
153 int check_overlaps) {
155 GManager manager(gopts);
162 for (
auto &[gsystemName, gsystem]: *gsystemsMap) {
163 std::string factory = gsystem->getFactoryName();
164 std::string g4Factory = g4FactoryNameFromSystemFactory(factory);
166 log->info(2,
"G4World: creating factory <", g4Factory,
"> to for system <", gsystemName,
">");
172 if (g4systemFactory.find(g4Factory) == g4systemFactory.end()) {
173 manager.RegisterObjectFactory<G4NativeSystemFactory>(g4Factory, gopts);
177 manager.RegisterObjectFactory<G4CadSystemFactory>(g4Factory, gopts);
182 if (g4systemFactory.find(g4Factory) == g4systemFactory.end()) {
183 g4systemFactory[g4Factory] = manager.CreateObject<G4ObjectsFactory>(g4Factory);
184 g4systemFactory[g4Factory]->initialize_context(check_overlaps, backup_material);
189 for (
const auto &[volumeName, gvolume]: gsystem->getGVolumesMap()) {
203void G4World::buildMaterials(
SystemMap *system_map) {
206 std::vector<GMaterial *> thisIterationRemainingMaterials;
207 unsigned long previousRemainingMaterials = 0;
209 thisIterationRemainingMaterials.clear();
211 for (
const auto &[systemName, system]: *system_map) {
213 for (
const auto &[gmaterialName, gmaterialPtr]: system->getGMaterialMap()) {
214 if (createG4Material(gmaterialPtr) ==
false) {
215 thisIterationRemainingMaterials.push_back(gmaterialPtr.get());
222 if (previousRemainingMaterials != 0 && !thisIterationRemainingMaterials.empty() &&
223 thisIterationRemainingMaterials.size() >= previousRemainingMaterials) {
224 for (
auto &gmaterialLeft: thisIterationRemainingMaterials) {
log->warning(gmaterialLeft->getName()); }
226 "Dependencies are not being resolved: their number should diminish. Above are the Outstanding gmaterials");
228 previousRemainingMaterials = thisIterationRemainingMaterials.size();
229 }
while (!thisIterationRemainingMaterials.empty());
233 log->info(2,
"G4World: using factory <", objectsFactory->
className(),
234 "> to build g4volume <", 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[]