18#include "G4LogicalVolumeStore.hh"
19#include "G4Material.hh"
20#include "G4NistManager.hh"
21#include "G4PVPlacement.hh"
31 std::vector<std::string> tokenizeRotation(std::string rotation) {
32 std::replace(rotation.begin(), rotation.end(),
',',
' ');
36 std::string lowercase(std::string value) {
37 std::transform(value.begin(), value.end(), value.begin(),
38 [](
unsigned char c) { return static_cast<char>(std::tolower(c)); });
42 void requireRotationTokenCount(
const std::vector<std::string> &tokens,
45 if (tokens.size() == expected) {
return; }
46 std::cerr <<
" >> ERROR: rotation <" << s->
getRot() <<
"> for " << s->
getName()
47 <<
" has wrong number of entries. Exiting." << std::endl;
51 void applyOrderedRotation(G4RotationMatrix *rot,
52 const std::string &order,
53 const std::vector<double> &angles,
56 rot->rotateX(angles[0]);
57 rot->rotateZ(angles[1]);
58 rot->rotateY(angles[2]);
60 else if (order ==
"yxz") {
61 rot->rotateY(angles[0]);
62 rot->rotateX(angles[1]);
63 rot->rotateZ(angles[2]);
65 else if (order ==
"yzx") {
66 rot->rotateY(angles[0]);
67 rot->rotateZ(angles[1]);
68 rot->rotateX(angles[2]);
70 else if (order ==
"zxy") {
71 rot->rotateZ(angles[0]);
72 rot->rotateX(angles[1]);
73 rot->rotateY(angles[2]);
75 else if (order ==
"zyx") {
76 rot->rotateZ(angles[0]);
77 rot->rotateY(angles[1]);
78 rot->rotateX(angles[2]);
81 std::cerr <<
" >> ERROR: Ordered rotation <" << order <<
"> for " << s->
getName()
82 <<
" is wrong, it's none of the following: xzy, yxz, yzx, zxy or zyx. Exiting."
91 const std::string &backup_mat) {
98 std::unordered_map<std::string, G4Volume *> *g4s) {
99 auto it = g4s->find(vname);
100 return (it != g4s->end()) ? it->second->getSolid() :
nullptr;
105 std::unordered_map<std::string, G4Volume *> *g4s) {
106 auto it = g4s->find(volume_name);
107 return (it != g4s->end()) ? it->second->getLogical() :
nullptr;
112 std::unordered_map<std::string, G4Volume *> *g4s) {
113 auto it = g4s->find(vname);
114 return (it != g4s->end()) ? it->second->getPhysical() :
nullptr;
119 std::string_view color = s->
getColor();
125 " resulted in RGB = (", g4color.GetRed(),
", ", g4color.GetGreen(),
", ", g4color.GetBlue(),
126 ", opacity: ", opacity,
")");
128 G4VisAttributes attr(g4color);
131 s->
isVisible() ? attr.SetVisibility(
true) : attr.SetVisibility(
false);
135 attr.SetForceWireframe(
true);
138 attr.SetForceSolid(
true);
141 attr.SetForceCloud(
true);
152 auto rot =
new G4RotationMatrix();
154 const auto tokens = tokenizeRotation(s->
getRot());
155 if (tokens.empty()) {
return rot; }
157 const auto rotationType = tokens[0];
158 if (rotationType !=
"ordered:" && rotationType !=
"doubleRotation:") {
159 requireRotationTokenCount(tokens, 3, s);
161 rot->rotateX(pars[0]);
162 rot->rotateY(pars[1]);
163 rot->rotateZ(pars[2]);
165 else if (rotationType ==
"doubleRotation:") {
166 requireRotationTokenCount(tokens, 7, s);
168 {tokens[1], tokens[2], tokens[3], tokens[4], tokens[5], tokens[6]});
169 rot->rotateX(pars[0]);
170 rot->rotateY(pars[1]);
171 rot->rotateZ(pars[2]);
172 rot->rotateX(pars[3]);
173 rot->rotateY(pars[4]);
174 rot->rotateZ(pars[5]);
176 else if (rotationType ==
"ordered:") {
177 requireRotationTokenCount(tokens, 5, s);
178 const auto order = lowercase(tokens[1]);
180 applyOrderedRotation(rot, order, pars, s);
183 std::cerr <<
" >> ERROR: rotation type <" << rotationType <<
"> unknown. Exiting."
192 G4ThreeVector pos(0., 0., 0.);
194 if (vec.size() == 3) pos.set(vec[0], vec[1], vec[2]);
199 if (shift.size() == 3) pos += G4ThreeVector(shift[0], shift[1], shift[2]);
205 std::unordered_map<std::string, G4Volume *> *g4s) {
206 const std::string g4name = s->
getG4Name();
210 if (thisG4Volume->getLogical())
return thisG4Volume->getLogical();
216 auto volume_copy =
gsystem +
"/" + copyOf;
218 if (copyG4Volume->getLogical() !=
nullptr) {
219 return copyG4Volume->getLogical();
226 auto *nist = G4NistManager::Instance();
227 auto *mat = nist->FindOrBuildMaterial(s->
getMaterial());
239 auto *logical =
new G4LogicalVolume(thisG4Volume->getSolid(),
244 thisG4Volume->setLogical(logical,
log);
249 std::unordered_map<std::string, G4Volume *> *g4s) {
256 const std::string g4name = s->
getG4Name();
258 auto logicalVolume = thisG4Volume->getLogical();
264 auto volume_copy =
gsystem +
"/" + copyOf;
266 if (copyG4Volume->getLogical() !=
nullptr) {
267 logicalVolume = copyG4Volume->getLogical();
272 if (!thisG4Volume->getPhysical()) {
277 if (placementType ==
"active") {
278 G4RotationMatrix rotation_instance = *rotation;
280 thisG4Volume->setPhysical(
new G4PVPlacement(G4Transform3D(rotation_instance, translation),
289 else if (placementType ==
"passive") {
290 thisG4Volume->setPhysical(
new G4PVPlacement(rotation,
302 "GVolume <", s->
getName(),
"> has unsupported g4placement_type <",
303 placementType,
">. Use 'active' or 'passive'.");
306 return thisG4Volume->getPhysical();
310 std::unordered_map<std::string, G4Volume *> *g4s) {
318 const bool okSolid = (sbuild !=
nullptr);
319 const bool okLogical = (lbuild !=
nullptr);
320 const bool okPhysical = (pbuild !=
nullptr);
321 const bool okAll = okSolid && okLogical && okPhysical;
325 ": solid: ", okSolid,
326 " logical: ", okLogical,
327 " physical: ", okPhysical);
G4Volume * getOrCreateG4Volume(const std::string &volume_name, std::unordered_map< std::string, G4Volume * > *g4s)
Get or create a G4Volume wrapper entry in the map.
virtual G4LogicalVolume * buildLogical(const GVolume *s, std::unordered_map< std::string, G4Volume * > *g4s)
Build or retrieve the G4LogicalVolume for a volume.
virtual G4VPhysicalVolume * buildPhysical(const GVolume *s, std::unordered_map< std::string, G4Volume * > *g4s)
Build or retrieve the G4VPhysicalVolume for a volume.
static G4VPhysicalVolume * getPhysicalFromMap(const std::string &volume_name, std::unordered_map< std::string, G4Volume * > *g4s)
Lookup physical volume in the g4s map.
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.
static G4RotationMatrix * getRotation(const GVolume *s)
Parse rotation string and build a Geant4 rotation matrix.
virtual std::string_view className() const =0
Short, human-readable factory name for logging.
virtual G4VSolid * buildSolid(const GVolume *s, std::unordered_map< std::string, G4Volume * > *g4s)=0
Build the G4VSolid for a volume.
bool checkPhysicalDependencies(const GVolume *s, std::unordered_map< std::string, G4Volume * > *g4s)
Verify prerequisites to build a physical placement.
static G4VSolid * getSolidFromMap(const std::string &volume_name, std::unordered_map< std::string, G4Volume * > *g4s)
Lookup solid in the g4s map.
static G4LogicalVolume * getLogicalFromMap(const std::string &volume_name, std::unordered_map< std::string, G4Volume * > *g4s)
Lookup logical volume in the g4s map.
void initialize_context(int checkOverlaps, const std::string &backupMaterial)
Configure overlap checking and backup material behavior for this factory.
static G4ThreeVector getPosition(const GVolume *s)
Parse position and optional shift strings to compute placement translation.
G4VisAttributes createVisualAttributes(const GVolume *s)
Build visualization attributes from the volume definition.
std::string backupMaterial
Backup material name used if the requested material is absent.
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 getRot() const
std::string getCopyOf() const
bool getExistence() const
std::string getPos() const
std::string getShift() const
std::string getG4PlacementType() const
std::string getName() const
std::string getMaterial() const
std::string getG4Name() const
std::string getSystem() const
std::string_view getColor() const
double getOpacity() const
std::string getG4MotherName() const
Abstract factory that converts a GEMC DB GVolume into Geant4 objects.
Conventions, labels, and error codes used by the g4system geometry/material layer.
#define ERR_G4MATERIALNOTFOUND
Material lookup failed and no fallback was available.
#define ERR_G4PLACEMENTTYPE
#define GSYSTEMNOMODIFIER
#define UNINITIALIZEDSTRINGQUANTITY
vector< double > getG4NumbersFromString(const string &vstring, bool warnIfNotUnit=false)
G4Colour makeG4Colour(std::string_view code, double opacity)
vector< double > getG4NumbersFromStringVector(const vector< string > &vstring, bool warnIfNotUnit=false)
vector< std::string > getStringVectorFromString(const std::string &input)