20#include "BDSAcceleratorModel.hh"
21#include "BDSColourFromMaterial.hh"
22#include "BDSColours.hh"
24#include "BDSException.hh"
25#include "BDSGeometryExternal.hh"
26#include "BDSGeometryFactoryGDML.hh"
27#include "BDSGeometryInspector.hh"
28#include "BDSGDMLPreprocessor.hh"
29#include "BDSGlobalConstants.hh"
30#include "BDSMaterials.hh"
31#include "BDSWarning.hh"
35#include "G4GDMLParser.hh"
36#include "G4LogicalVolume.hh"
37#include "G4UserLimits.hh"
38#include "G4VisAttributes.hh"
39#include "G4VPhysicalVolume.hh"
53BDSGeometryFactoryGDML::BDSGeometryFactoryGDML()
58 std::map<G4String, G4Colour*>* mapping,
62 std::vector<G4String>* namedVacuumVolumes,
66 G4UserLimits* userLimitsToAttachToAllLVs)
72 G4String processedFile;
76 {processedFile = BDS::PreprocessGDML(fileName, componentName, preprocessGDMLSchema);}
77 else if (preprocessGDMLSchema)
78 {processedFile = BDS::PreprocessGDMLSchemaOnly(fileName);}
80 {processedFile = fileName;}
81 G4String preprocessNameToStrip = preprocessGDML ? componentName+
"_" :
"";
83 G4GDMLParser* parser =
new G4GDMLParser();
85 parser->Read(processedFile,
true);
87 G4VPhysicalVolume* containerPV = parser->GetWorldVolume();
88 G4LogicalVolume* containerLV = containerPV->GetLogicalVolume();
89 G4VSolid* containerSolid = containerLV->GetSolid();
90 G4ThreeVector gdmlWorldOrigin = G4ThreeVector();
91 if (containerPV->GetName() ==
"world_volume_lv_PV")
93 gdmlWorldOrigin = parser->GetPosition(
"PygdmlOrigin");
94 gdmlWorldOrigin[2] = 0.0;
98 std::set<G4VPhysicalVolume*> pvsGDML;
99 std::set<G4LogicalVolume*> lvsGDML;
100 std::map<G4String, G4Material*> materialsGDML;
106 std::map<G4String, G4Colour*> gdmlColours;
108 for (
auto lv : lvsGDML)
110 auto auxInfo = parser->GetVolumeAuxiliaryInformation(lv);
111 for (
const auto& af : auxInfo)
113 if (af.type ==
"colour")
115 std::stringstream ss(af.value);
116 std::vector<G4String> colVals((std::istream_iterator<G4String>(ss)), std::istream_iterator<G4String>());
117 if (colVals.size() != 4)
118 {BDS::Warning(__METHOD_NAME__,
"invalid number of colour values for logical volume " + lv->GetName());}
119 G4String colourName = componentName +
"_colour_"+std::to_string(iColour);
121 G4String colourString = colourName +
":";
122 for (
const auto& c : colVals)
123 {colourString +=
" " + c;}
126 gdmlColours[lv->GetName()] = colour;
131 G4cout <<
"Loaded GDML file \"" << processedFile <<
"\" containing:" << G4endl;
132 G4cout << pvsGDML.size() <<
" physical volumes, and " << lvsGDML.size() <<
" logical volumes" << G4endl;
135 std::map<G4String, G4Colour*>* mappingToUse =
nullptr;
136 G4bool deleteMap =
false;
137 if (!gdmlColours.empty())
141 mappingToUse =
new std::map<G4String, G4Colour*>(*mapping);
142 mappingToUse->insert(gdmlColours.begin(), gdmlColours.end());
146 {mappingToUse = &gdmlColours;}
149 {mappingToUse = mapping;}
151 auto visesGDML =
ApplyColourMapping(lvsGDML, mappingToUse, autoColour, preprocessNameToStrip);
153 {
delete mappingToUse;}
160 G4VisAttributes* vis =
new G4VisAttributes(*c);
161 vis->SetVisibility(
true);
162 visesGDML.insert(vis);
163 containerLV->SetVisAttributes(vis);
183 vacuumSensitivityType);
191 const G4String& acceleratorComponentName)
const
195 std::set<G4VPhysicalVolume*>& pvsIn,
196 std::set<G4LogicalVolume*>& lvsIn,
197 std::map<G4String, G4Material*>& materialsGDML)
199 const auto& lv = volume->GetLogicalVolume();
201 G4Material* mat = lv->GetMaterial();
202 materialsGDML[mat->GetName()] = mat;
203 for (G4int i = 0; i < (G4int)lv->GetNoDaughters(); i++)
205 const auto& pv = lv->GetDaughter(i);
212 const G4String& outputFileName,
214 const G4String& replacement)
217 std::ifstream ifs(fileName);
221 {
throw BDSException(__METHOD_NAME__,
"Cannot open file \"" + fileName +
"\"");}
223 std::ofstream fout(outputFileName);
225 G4cout << __METHOD_NAME__ <<
"Original file: " << fileName << G4endl;
226 G4cout << __METHOD_NAME__ <<
"Temporary file: " << outputFileName << G4endl;
229 int lenOfKey = key.size();
233 while (std::getline(ifs, buffer))
235 int f = buffer.find(key);
238 std::string outputString = std::string(buffer);
239 outputString.replace(f, lenOfKey, replacement);
240 fout << outputString <<
"\n";
243 {fout << buffer <<
"\n";}
253void _SymbolToPreventWarningGeomFacGDML(){;}
static BDSColourFromMaterial * Instance()
Singleton pattern.
G4Colour * GetColour(const G4Material *material, const G4String &prefixToStripFromName="")
Get colour from name.
static BDSColours * Instance()
singleton pattern
G4Colour * GetColour(const G4String &type, G4bool normaliseTo255=true)
Get colour from name.
General exception with possible name of object and message.
static G4String ProcessedNodeName(const G4String &nodeName, const G4String &prefix)
void RegisterLogicalVolume(G4LogicalVolume *logicalVolume)
virtual std::set< G4LogicalVolume * > GetAllLogicalVolumes() const
Access all logical volumes belonging to this component.
void RegisterPhysicalVolume(G4VPhysicalVolume *physicalVolume)
void RegisterVisAttributes(G4VisAttributes *visAttribute)
A loaded piece of externally provided geometry.
void RegisterVacuumVolumes(const std::set< G4LogicalVolume * > &vacuumVolumesIn)
Register a set of volumes to be identified as vacuum volumes for the BDSAcceleratorComponent.
const std::set< G4LogicalVolume * > & VacuumVolumes() const
Access the vacuum volumes.
virtual void ApplySensitivity(BDSGeometryExternal *result, const std::set< G4LogicalVolume * > &allLogicalVolumesIn, BDSSDType generalSensitivity, const std::set< G4LogicalVolume * > &vacuumLogicalVolumes, BDSSDType vacuumSensitivity)
Attach the relevant general and vacuum sensitivity to each volume.
std::set< G4LogicalVolume * > GetVolumes(const std::set< G4LogicalVolume * > &allLVs, std::vector< G4String > *volumeNames, G4bool preprocessGDML, const G4String &componentName) const
Get the volumes that match the name. Volume names are matched exactly and are case sensitive.
virtual void CleanUp()
Virtual clean up that derived classes can override that calls CleanUpBase().
virtual void ApplyUserLimits(const std::set< G4LogicalVolume * > &lvsIn, G4UserLimits *userLimits)
Attach a set of user limits to every logical volume supplied.
virtual std::set< G4VisAttributes * > ApplyColourMapping(std::set< G4LogicalVolume * > &lvs, std::map< G4String, G4Colour * > *mapping, G4bool autoColour, const G4String &preprocessPrefix="")
void GetAllLogicalPhysicalAndMaterials(const G4VPhysicalVolume *volume, std::set< G4VPhysicalVolume * > &pvs, std::set< G4LogicalVolume * > &lvs, std::map< G4String, G4Material * > &materialsGDML)
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 > *namedVacuumVolumes=nullptr, G4bool makeSensitive=true, BDSSDType sensitivityType=BDSSDType::energydep, BDSSDType vacuumSensitivityType=BDSSDType::energydepvacuum, G4UserLimits *userLimitsToAttachToAllLVs=nullptr)
virtual G4String PreprocessedName(const G4String &objectName, const G4String &acceleratorComponentName) const
Use the GDML preprocessing scheme to prepare the preprocesseed volume names.
void ReplaceStringInFile(const G4String &filename, const G4String &outputFileName, const G4String &key, const G4String &replacement)
static BDSGlobalConstants * Instance()
Access method.
static BDSMaterials * Instance()
Singleton pattern access.
void CacheMaterialsFromGDML(const std::map< G4String, G4Material * > &materialsGDML)
Introduce materials loaded from GDML into this instance.
void CheckForConflictingMaterialsAfterLoad(const G4String &geometryFileName, const G4String &componentName) const
Improve type-safety of native enum data type in C++.
std::pair< BDSExtent, BDSExtent > DetermineExtents(const G4VSolid *solid)