00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include "globals.hh"
00035
00036 #include "G4GDMLRead.hh"
00037
00038 #include "G4UnitsTable.hh"
00039 #include "G4Element.hh"
00040 #include "G4Material.hh"
00041 #include "G4SolidStore.hh"
00042 #include "G4LogicalVolumeStore.hh"
00043 #include "G4PhysicalVolumeStore.hh"
00044
00045 G4GDMLRead::G4GDMLRead()
00046 : validate(true), check(false), inLoop(0), loopCount(0)
00047 {
00048 G4UnitDefinition::BuildUnitsTable();
00049 }
00050
00051 G4GDMLRead::~G4GDMLRead()
00052 {
00053 }
00054
00055 G4String G4GDMLRead::Transcode(const XMLCh* const toTranscode)
00056 {
00057 char* char_str = xercesc::XMLString::transcode(toTranscode);
00058 G4String my_str(char_str);
00059 xercesc::XMLString::release(&char_str);
00060 return my_str;
00061 }
00062
00063 void G4GDMLRead::OverlapCheck(G4bool flag)
00064 {
00065 check = flag;
00066 }
00067
00068 G4String G4GDMLRead::GenerateName(const G4String& nameIn, G4bool strip)
00069 {
00070 G4String nameOut(nameIn);
00071
00072 if (inLoop>0)
00073 {
00074 nameOut = eval.SolveBrackets(nameOut);
00075
00076
00077
00078 }
00079 if (strip) { StripName(nameOut); }
00080
00081 return nameOut;
00082 }
00083
00084 void G4GDMLRead::GeneratePhysvolName(const G4String& nameIn,
00085 G4VPhysicalVolume* physvol)
00086 {
00087 G4String nameOut(nameIn);
00088
00089 if (nameIn.empty())
00090 {
00091 std::stringstream stream;
00092 stream << physvol->GetLogicalVolume()->GetName() << "_PV";
00093 nameOut = stream.str();
00094 }
00095 nameOut = eval.SolveBrackets(nameOut);
00096
00097 physvol->SetName(nameOut);
00098 }
00099
00100 G4String G4GDMLRead::Strip(const G4String& name) const
00101 {
00102 G4String sname(name);
00103 return sname.remove(sname.find("0x"));
00104 }
00105
00106 void G4GDMLRead::StripName(G4String& name) const
00107 {
00108 name.remove(name.find("0x"));
00109 }
00110
00111 void G4GDMLRead::StripNames() const
00112 {
00113
00114
00115
00116 G4PhysicalVolumeStore* pvols = G4PhysicalVolumeStore::GetInstance();
00117 G4LogicalVolumeStore* lvols = G4LogicalVolumeStore::GetInstance();
00118 G4SolidStore* solids = G4SolidStore::GetInstance();
00119 const G4ElementTable* elements = G4Element::GetElementTable();
00120 const G4MaterialTable* materials = G4Material::GetMaterialTable();
00121
00122 G4cout << "Stripping off GDML names of materials, solids and volumes ..."
00123 << G4endl;
00124
00125 G4String sname;
00126 register size_t i;
00127
00128
00129
00130 for (i=0; i<solids->size(); i++)
00131 {
00132 G4VSolid* psol = (*solids)[i];
00133 sname = psol->GetName();
00134 StripName(sname);
00135 psol->SetName(sname);
00136 }
00137
00138
00139
00140 for (i=0; i<lvols->size(); i++)
00141 {
00142 G4LogicalVolume* lvol = (*lvols)[i];
00143 sname = lvol->GetName();
00144 StripName(sname);
00145 lvol->SetName(sname);
00146 }
00147
00148
00149
00150 for (i=0; i<pvols->size(); i++)
00151 {
00152 G4VPhysicalVolume* pvol = (*pvols)[i];
00153 sname = pvol->GetName();
00154 StripName(sname);
00155 pvol->SetName(sname);
00156 }
00157
00158
00159
00160 for (i=0; i<materials->size(); i++)
00161 {
00162 G4Material* pmat = (*materials)[i];
00163 sname = pmat->GetName();
00164 StripName(sname);
00165 pmat->SetName(sname);
00166 }
00167
00168
00169
00170 for (i=0; i<elements->size(); i++)
00171 {
00172 G4Element* pelm = (*elements)[i];
00173 sname = pelm->GetName();
00174 StripName(sname);
00175 pelm->SetName(sname);
00176 }
00177 }
00178
00179 void G4GDMLRead::LoopRead(const xercesc::DOMElement* const element,
00180 void(G4GDMLRead::*func)(const xercesc::DOMElement* const))
00181 {
00182 G4String var;
00183 G4String from;
00184 G4String to;
00185 G4String step;
00186
00187 const xercesc::DOMNamedNodeMap* const attributes = element->getAttributes();
00188 XMLSize_t attributeCount = attributes->getLength();
00189
00190 for (XMLSize_t attribute_index=0;
00191 attribute_index<attributeCount;attribute_index++)
00192 {
00193 xercesc::DOMNode* attribute_node = attributes->item(attribute_index);
00194
00195 if (attribute_node->getNodeType() != xercesc::DOMNode::ATTRIBUTE_NODE)
00196 { continue; }
00197
00198 const xercesc::DOMAttr* const attribute
00199 = dynamic_cast<xercesc::DOMAttr*>(attribute_node);
00200 if (!attribute)
00201 {
00202 G4Exception("G4GDMLRead::LoopRead()", "InvalidRead",
00203 FatalException, "No attribute found!");
00204 return;
00205 }
00206 const G4String attribute_name = Transcode(attribute->getName());
00207 const G4String attribute_value = Transcode(attribute->getValue());
00208
00209 if (attribute_name=="for") { var = attribute_value; } else
00210 if (attribute_name=="from") { from = attribute_value; } else
00211 if (attribute_name=="to") { to = attribute_value; } else
00212 if (attribute_name=="step") { step = attribute_value; }
00213 }
00214
00215 if (var.empty())
00216 {
00217 G4Exception("G4GDMLRead::loopRead()", "InvalidRead",
00218 FatalException, "No variable is determined for loop!");
00219 }
00220
00221 if (!eval.IsVariable(var))
00222 {
00223 G4Exception("G4GDMLRead::loopRead()", "InvalidRead",
00224 FatalException, "Variable is not defined in loop!");
00225 }
00226
00227 G4int _var = eval.EvaluateInteger(var);
00228 G4int _from = eval.EvaluateInteger(from);
00229 G4int _to = eval.EvaluateInteger(to);
00230 G4int _step = eval.EvaluateInteger(step);
00231
00232 if (!from.empty()) { _var = _from; }
00233
00234 if (_from == _to)
00235 {
00236 G4Exception("G4GDMLRead::loopRead()", "InvalidRead",
00237 FatalException, "Empty loop!");
00238 }
00239 if ((_from < _to) && (_step <= 0))
00240 {
00241 G4Exception("G4GDMLRead::loopRead()", "InvalidRead",
00242 FatalException, "Infinite loop!");
00243 }
00244 if ((_from > _to) && (_step >= 0))
00245 {
00246 G4Exception("G4GDMLRead::loopRead()", "InvalidRead",
00247 FatalException, "Infinite loop!");
00248 }
00249
00250 inLoop++;
00251
00252 while (_var <= _to)
00253 {
00254 eval.SetVariable(var,_var);
00255 (this->*func)(element);
00256 _var += _step;
00257 loopCount++;
00258 }
00259
00260 inLoop--;
00261 if (!inLoop) { loopCount = 0; }
00262 }
00263
00264 void G4GDMLRead::ExtensionRead(const xercesc::DOMElement* const)
00265 {
00266 G4String error_msg = "No handle to user-code for parsing extensions!";
00267 G4Exception("G4GDMLRead::ExtensionRead()",
00268 "NotImplemented", JustWarning, error_msg);
00269 }
00270
00271 void G4GDMLRead::Read(const G4String& fileName,
00272 G4bool validation,
00273 G4bool isModule,
00274 G4bool strip)
00275 {
00276 if (isModule)
00277 {
00278 G4cout << "G4GDML: Reading module '" << fileName << "'..." << G4endl;
00279 }
00280 else
00281 {
00282 G4cout << "G4GDML: Reading '" << fileName << "'..." << G4endl;
00283 }
00284
00285 inLoop = 0;
00286 validate = validation;
00287
00288 xercesc::ErrorHandler* handler = new G4GDMLErrorHandler(!validate);
00289 xercesc::XercesDOMParser* parser = new xercesc::XercesDOMParser;
00290
00291 parser->setValidationScheme(xercesc::XercesDOMParser::Val_Always);
00292 parser->setValidationSchemaFullChecking(true);
00293 parser->setCreateEntityReferenceNodes(false);
00294
00295
00296 parser->setDoNamespaces(true);
00297 parser->setDoSchema(true);
00298 parser->setErrorHandler(handler);
00299
00300 try { parser->parse(fileName.c_str()); }
00301 catch (const xercesc::XMLException &e)
00302 { G4cout << "G4GDML: " << Transcode(e.getMessage()) << G4endl; }
00303 catch (const xercesc::DOMException &e)
00304 { G4cout << "G4GDML: " << Transcode(e.getMessage()) << G4endl; }
00305
00306 xercesc::DOMDocument* doc = parser->getDocument();
00307
00308 if (!doc)
00309 {
00310 G4String error_msg = "Unable to open document: " + fileName;
00311 G4Exception("G4GDMLRead::Read()", "InvalidRead",
00312 FatalException, error_msg);
00313 return;
00314 }
00315 xercesc::DOMElement* element = doc->getDocumentElement();
00316
00317 if (!element)
00318 {
00319 G4Exception("G4GDMLRead::Read()", "InvalidRead",
00320 FatalException, "Empty document!");
00321 return;
00322 }
00323
00324 for (xercesc::DOMNode* iter = element->getFirstChild();
00325 iter != 0; iter = iter->getNextSibling())
00326 {
00327 if (iter->getNodeType() != xercesc::DOMNode::ELEMENT_NODE) { continue; }
00328
00329 const xercesc::DOMElement* const child
00330 = dynamic_cast<xercesc::DOMElement*>(iter);
00331 if (!child)
00332 {
00333 G4Exception("G4GDMLRead::Read()", "InvalidRead",
00334 FatalException, "No child found!");
00335 return;
00336 }
00337 const G4String tag = Transcode(child->getTagName());
00338
00339 if (tag=="define") { DefineRead(child); } else
00340 if (tag=="materials") { MaterialsRead(child); } else
00341 if (tag=="solids") { SolidsRead(child); } else
00342 if (tag=="setup") { SetupRead(child); } else
00343 if (tag=="structure") { StructureRead(child); } else
00344 if (tag=="extension") { ExtensionRead(child); }
00345 else
00346 {
00347 G4String error_msg = "Unknown tag in gdml: " + tag;
00348 G4Exception("G4GDMLRead::Read()", "InvalidRead",
00349 FatalException, error_msg);
00350 }
00351 }
00352
00353 delete parser;
00354 delete handler;
00355
00356 if (isModule)
00357 {
00358 G4cout << "G4GDML: Reading module '" << fileName << "' done!" << G4endl;
00359 }
00360 else
00361 {
00362 G4cout << "G4GDML: Reading '" << fileName << "' done!" << G4endl;
00363 if (strip) { StripNames(); }
00364 }
00365 }