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