[987] | 1 | // |
---|
| 2 | // ******************************************************************** |
---|
| 3 | // * License and Disclaimer * |
---|
| 4 | // * * |
---|
| 5 | // * The Geant4 software is copyright of the Copyright Holders of * |
---|
| 6 | // * the Geant4 Collaboration. It is provided under the terms and * |
---|
| 7 | // * conditions of the Geant4 Software License, included in the file * |
---|
| 8 | // * LICENSE and available at http://cern.ch/geant4/license . These * |
---|
| 9 | // * include a list of copyright holders. * |
---|
| 10 | // * * |
---|
| 11 | // * Neither the authors of this software system, nor their employing * |
---|
| 12 | // * institutes,nor the agencies providing financial support for this * |
---|
| 13 | // * work make any representation or warranty, express or implied, * |
---|
| 14 | // * regarding this software system or assume any liability for its * |
---|
| 15 | // * use. Please see the license in the file LICENSE and URL above * |
---|
| 16 | // * for the full disclaimer and the limitation of liability. * |
---|
| 17 | // * * |
---|
| 18 | // * This code implementation is the result of the scientific and * |
---|
| 19 | // * technical work of the GEANT4 collaboration. * |
---|
| 20 | // * By using, copying, modifying or distributing the software (or * |
---|
| 21 | // * any work based on the software) you agree to acknowledge its * |
---|
| 22 | // * use in resulting scientific publications, and indicate your * |
---|
| 23 | // * acceptance of all terms of the Geant4 Software license. * |
---|
| 24 | // ******************************************************************** |
---|
| 25 | // |
---|
[1228] | 26 | // $Id: G4GDMLRead.cc,v 1.47 2009/05/12 15:46:43 gcosmo Exp $ |
---|
| 27 | // GEANT4 tag $Name: geant4-09-03 $ |
---|
[987] | 28 | // |
---|
| 29 | // class G4GDMLRead Implementation |
---|
| 30 | // |
---|
| 31 | // History: |
---|
| 32 | // - Created. Zoltan Torzsok, November 2007 |
---|
| 33 | // ------------------------------------------------------------------------- |
---|
| 34 | |
---|
[1228] | 35 | #include "globals.hh" |
---|
| 36 | |
---|
[987] | 37 | #include "G4GDMLRead.hh" |
---|
[1228] | 38 | |
---|
| 39 | #include "G4UnitsTable.hh" |
---|
[987] | 40 | #include "G4Element.hh" |
---|
| 41 | #include "G4Material.hh" |
---|
| 42 | #include "G4SolidStore.hh" |
---|
| 43 | #include "G4LogicalVolumeStore.hh" |
---|
| 44 | #include "G4PhysicalVolumeStore.hh" |
---|
| 45 | |
---|
| 46 | G4GDMLRead::G4GDMLRead() |
---|
[1228] | 47 | : validate(true), check(false), inLoop(0), loopCount(0) |
---|
[987] | 48 | { |
---|
| 49 | G4UnitDefinition::BuildUnitsTable(); |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | G4GDMLRead::~G4GDMLRead() |
---|
| 53 | { |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | G4String G4GDMLRead::Transcode(const XMLCh* const toTranscode) |
---|
| 57 | { |
---|
| 58 | char* char_str = xercesc::XMLString::transcode(toTranscode); |
---|
| 59 | G4String my_str(char_str); |
---|
| 60 | xercesc::XMLString::release(&char_str); |
---|
| 61 | return my_str; |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | void G4GDMLRead::OverlapCheck(G4bool flag) |
---|
| 65 | { |
---|
| 66 | check = flag; |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | G4String G4GDMLRead::GenerateName(const G4String& nameIn, G4bool strip) |
---|
| 70 | { |
---|
| 71 | G4String nameOut(nameIn); |
---|
| 72 | |
---|
[1228] | 73 | if (inLoop>0) |
---|
| 74 | { |
---|
| 75 | nameOut = eval.SolveBrackets(nameOut); |
---|
| 76 | // std::stringstream stream; |
---|
| 77 | // stream << "0x" << loopCount; |
---|
| 78 | // nameOut = nameOut + stream.str(); |
---|
| 79 | } |
---|
[987] | 80 | if (strip) { StripName(nameOut); } |
---|
| 81 | |
---|
| 82 | return nameOut; |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | void G4GDMLRead::GeneratePhysvolName(const G4String& nameIn, |
---|
| 86 | G4VPhysicalVolume* physvol) |
---|
| 87 | { |
---|
| 88 | G4String nameOut(nameIn); |
---|
| 89 | |
---|
| 90 | if (nameIn.empty()) |
---|
| 91 | { |
---|
| 92 | std::stringstream stream; |
---|
| 93 | stream << physvol->GetLogicalVolume()->GetName() << "_PV"; |
---|
| 94 | nameOut = stream.str(); |
---|
| 95 | } |
---|
| 96 | nameOut = eval.SolveBrackets(nameOut); |
---|
| 97 | |
---|
| 98 | physvol->SetName(nameOut); |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | G4String G4GDMLRead::Strip(const G4String& name) const |
---|
| 102 | { |
---|
| 103 | G4String sname(name); |
---|
| 104 | return sname.remove(sname.find("0x")); |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | void G4GDMLRead::StripName(G4String& name) const |
---|
| 108 | { |
---|
| 109 | name.remove(name.find("0x")); |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | void G4GDMLRead::StripNames() const |
---|
| 113 | { |
---|
| 114 | // Strips off names of volumes, solids elements and materials from possible |
---|
| 115 | // reference pointers or IDs attached to their original identifiers. |
---|
| 116 | |
---|
| 117 | G4PhysicalVolumeStore* pvols = G4PhysicalVolumeStore::GetInstance(); |
---|
| 118 | G4LogicalVolumeStore* lvols = G4LogicalVolumeStore::GetInstance(); |
---|
| 119 | G4SolidStore* solids = G4SolidStore::GetInstance(); |
---|
| 120 | const G4ElementTable* elements = G4Element::GetElementTable(); |
---|
| 121 | const G4MaterialTable* materials = G4Material::GetMaterialTable(); |
---|
| 122 | |
---|
| 123 | G4cout << "Stripping off GDML names of materials, solids and volumes ..." |
---|
| 124 | << G4endl; |
---|
| 125 | |
---|
| 126 | G4String sname; |
---|
| 127 | register size_t i; |
---|
| 128 | |
---|
| 129 | // Solids... |
---|
| 130 | // |
---|
| 131 | for (i=0; i<solids->size(); i++) |
---|
| 132 | { |
---|
| 133 | G4VSolid* psol = (*solids)[i]; |
---|
| 134 | sname = psol->GetName(); |
---|
| 135 | StripName(sname); |
---|
| 136 | psol->SetName(sname); |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | // Logical volumes... |
---|
| 140 | // |
---|
| 141 | for (i=0; i<lvols->size(); i++) |
---|
| 142 | { |
---|
| 143 | G4LogicalVolume* lvol = (*lvols)[i]; |
---|
| 144 | sname = lvol->GetName(); |
---|
| 145 | StripName(sname); |
---|
| 146 | lvol->SetName(sname); |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | // Physical volumes... |
---|
| 150 | // |
---|
| 151 | for (i=0; i<pvols->size(); i++) |
---|
| 152 | { |
---|
| 153 | G4VPhysicalVolume* pvol = (*pvols)[i]; |
---|
| 154 | sname = pvol->GetName(); |
---|
| 155 | StripName(sname); |
---|
| 156 | pvol->SetName(sname); |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | // Materials... |
---|
| 160 | // |
---|
| 161 | for (i=0; i<materials->size(); i++) |
---|
| 162 | { |
---|
| 163 | G4Material* pmat = (*materials)[i]; |
---|
| 164 | sname = pmat->GetName(); |
---|
| 165 | StripName(sname); |
---|
| 166 | pmat->SetName(sname); |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | // Elements... |
---|
| 170 | // |
---|
| 171 | for (i=0; i<elements->size(); i++) |
---|
| 172 | { |
---|
| 173 | G4Element* pelm = (*elements)[i]; |
---|
| 174 | sname = pelm->GetName(); |
---|
| 175 | StripName(sname); |
---|
| 176 | pelm->SetName(sname); |
---|
| 177 | } |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | void G4GDMLRead::LoopRead(const xercesc::DOMElement* const element, |
---|
| 181 | void(G4GDMLRead::*func)(const xercesc::DOMElement* const)) |
---|
| 182 | { |
---|
| 183 | G4String var; |
---|
| 184 | G4String from; |
---|
| 185 | G4String to; |
---|
| 186 | G4String step; |
---|
| 187 | |
---|
| 188 | const xercesc::DOMNamedNodeMap* const attributes = element->getAttributes(); |
---|
| 189 | XMLSize_t attributeCount = attributes->getLength(); |
---|
| 190 | |
---|
| 191 | for (XMLSize_t attribute_index=0; |
---|
| 192 | attribute_index<attributeCount;attribute_index++) |
---|
| 193 | { |
---|
| 194 | xercesc::DOMNode* attribute_node = attributes->item(attribute_index); |
---|
| 195 | |
---|
| 196 | if (attribute_node->getNodeType() != xercesc::DOMNode::ATTRIBUTE_NODE) |
---|
| 197 | { continue; } |
---|
| 198 | |
---|
| 199 | const xercesc::DOMAttr* const attribute |
---|
| 200 | = dynamic_cast<xercesc::DOMAttr*>(attribute_node); |
---|
| 201 | const G4String attribute_name = Transcode(attribute->getName()); |
---|
| 202 | const G4String attribute_value = Transcode(attribute->getValue()); |
---|
| 203 | |
---|
| 204 | if (attribute_name=="for") { var = attribute_value; } else |
---|
| 205 | if (attribute_name=="from") { from = attribute_value; } else |
---|
| 206 | if (attribute_name=="to") { to = attribute_value; } else |
---|
| 207 | if (attribute_name=="step") { step = attribute_value; } |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | if (var.empty()) |
---|
| 211 | { |
---|
| 212 | G4Exception("G4GDMLRead::loopRead()", "InvalidRead", |
---|
| 213 | FatalException, "No variable is determined for loop!"); |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | if (!eval.IsVariable(var)) |
---|
| 217 | { |
---|
| 218 | G4Exception("G4GDMLRead::loopRead()", "InvalidRead", |
---|
| 219 | FatalException, "Variable is not defined in loop!"); |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | G4int _var = eval.EvaluateInteger(var); |
---|
| 223 | G4int _from = eval.EvaluateInteger(from); |
---|
| 224 | G4int _to = eval.EvaluateInteger(to); |
---|
| 225 | G4int _step = eval.EvaluateInteger(step); |
---|
| 226 | |
---|
| 227 | if (!from.empty()) { _var = _from; } |
---|
| 228 | |
---|
| 229 | if (_from == _to) |
---|
| 230 | { |
---|
| 231 | G4Exception("G4GDMLRead::loopRead()", "InvalidRead", |
---|
| 232 | FatalException, "Empty loop!"); |
---|
| 233 | } |
---|
| 234 | if ((_from < _to) && (_step <= 0)) |
---|
| 235 | { |
---|
| 236 | G4Exception("G4GDMLRead::loopRead()", "InvalidRead", |
---|
| 237 | FatalException, "Infinite loop!"); |
---|
| 238 | } |
---|
| 239 | if ((_from > _to) && (_step >= 0)) |
---|
| 240 | { |
---|
| 241 | G4Exception("G4GDMLRead::loopRead()", "InvalidRead", |
---|
| 242 | FatalException, "Infinite loop!"); |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | inLoop++; |
---|
[1228] | 246 | |
---|
[987] | 247 | while (_var <= _to) |
---|
| 248 | { |
---|
| 249 | eval.SetVariable(var,_var); |
---|
| 250 | (this->*func)(element); |
---|
| 251 | _var += _step; |
---|
[1228] | 252 | loopCount++; |
---|
[987] | 253 | } |
---|
| 254 | |
---|
| 255 | inLoop--; |
---|
[1228] | 256 | if (!inLoop) { loopCount = 0; } |
---|
[987] | 257 | } |
---|
| 258 | |
---|
| 259 | void G4GDMLRead::ExtensionRead(const xercesc::DOMElement* const) |
---|
| 260 | { |
---|
| 261 | G4String error_msg = "No handle to user-code for parsing extensions!"; |
---|
| 262 | G4Exception("G4GDMLRead::ExtensionRead()", |
---|
| 263 | "NotImplemented", JustWarning, error_msg); |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | void G4GDMLRead::Read(const G4String& fileName, |
---|
| 267 | G4bool validation, |
---|
| 268 | G4bool isModule) |
---|
| 269 | { |
---|
| 270 | if (isModule) |
---|
| 271 | { |
---|
| 272 | G4cout << "G4GDML: Reading module '" << fileName << "'..." << G4endl; |
---|
| 273 | } |
---|
| 274 | else |
---|
| 275 | { |
---|
| 276 | G4cout << "G4GDML: Reading '" << fileName << "'..." << G4endl; |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | inLoop = 0; |
---|
| 280 | validate = validation; |
---|
| 281 | |
---|
| 282 | xercesc::ErrorHandler* handler = new G4GDMLErrorHandler(!validate); |
---|
| 283 | xercesc::XercesDOMParser* parser = new xercesc::XercesDOMParser; |
---|
| 284 | |
---|
| 285 | parser->setValidationScheme(xercesc::XercesDOMParser::Val_Always); |
---|
| 286 | parser->setValidationSchemaFullChecking(true); |
---|
| 287 | parser->setCreateEntityReferenceNodes(false); |
---|
| 288 | // Entities will be automatically resolved by Xerces |
---|
| 289 | |
---|
| 290 | parser->setDoNamespaces(true); |
---|
| 291 | parser->setDoSchema(true); |
---|
| 292 | parser->setErrorHandler(handler); |
---|
| 293 | |
---|
| 294 | try { parser->parse(fileName.c_str()); } |
---|
| 295 | catch (const xercesc::XMLException &e) |
---|
| 296 | { G4cout << "G4GDML: " << Transcode(e.getMessage()) << G4endl; } |
---|
| 297 | catch (const xercesc::DOMException &e) |
---|
| 298 | { G4cout << "G4GDML: " << Transcode(e.getMessage()) << G4endl; } |
---|
| 299 | |
---|
| 300 | xercesc::DOMDocument* doc = parser->getDocument(); |
---|
| 301 | |
---|
| 302 | if (!doc) |
---|
| 303 | { |
---|
| 304 | G4String error_msg = "Unable to open document: " + fileName; |
---|
| 305 | G4Exception("G4GDMLRead::Read()", "InvalidRead", |
---|
| 306 | FatalException, error_msg); |
---|
| 307 | } |
---|
| 308 | xercesc::DOMElement* element = doc->getDocumentElement(); |
---|
| 309 | |
---|
| 310 | if (!element) |
---|
| 311 | { |
---|
| 312 | G4Exception("G4GDMLRead::Read()", "InvalidRead", |
---|
| 313 | FatalException, "Empty document!"); |
---|
| 314 | } |
---|
| 315 | |
---|
| 316 | for (xercesc::DOMNode* iter = element->getFirstChild(); |
---|
| 317 | iter != 0; iter = iter->getNextSibling()) |
---|
| 318 | { |
---|
| 319 | if (iter->getNodeType() != xercesc::DOMNode::ELEMENT_NODE) { continue; } |
---|
| 320 | |
---|
| 321 | const xercesc::DOMElement* const child |
---|
| 322 | = dynamic_cast<xercesc::DOMElement*>(iter); |
---|
| 323 | const G4String tag = Transcode(child->getTagName()); |
---|
| 324 | |
---|
| 325 | if (tag=="define") { DefineRead(child); } else |
---|
| 326 | if (tag=="materials") { MaterialsRead(child); } else |
---|
| 327 | if (tag=="solids") { SolidsRead(child); } else |
---|
| 328 | if (tag=="setup") { SetupRead(child); } else |
---|
| 329 | if (tag=="structure") { StructureRead(child); } else |
---|
| 330 | if (tag=="extension") { ExtensionRead(child); } |
---|
| 331 | else |
---|
| 332 | { |
---|
| 333 | G4String error_msg = "Unknown tag in gdml: " + tag; |
---|
| 334 | G4Exception("G4GDMLRead::Read()", "InvalidRead", |
---|
| 335 | FatalException, error_msg); |
---|
| 336 | } |
---|
| 337 | } |
---|
| 338 | |
---|
| 339 | if (parser) { delete parser; } |
---|
| 340 | if (handler) { delete handler; } |
---|
| 341 | |
---|
| 342 | if (isModule) |
---|
| 343 | { |
---|
| 344 | G4cout << "G4GDML: Reading module '" << fileName << "' done!" << G4endl; |
---|
| 345 | } |
---|
| 346 | else |
---|
| 347 | { |
---|
| 348 | G4cout << "G4GDML: Reading '" << fileName << "' done!" << G4endl; |
---|
| 349 | StripNames(); |
---|
| 350 | } |
---|
| 351 | } |
---|