4 // See the class description in the header file.
6 #include "TG4XMLConvertor.h"
9 #include <G4LogicalVolume.hh>
10 #include <G4Material.hh>
11 #include <G4VSolid.hh>
17 #include <g4std/iostream>
18 #include <g4std/iomanip>
20 const G4int TG4XMLConvertor::fgkMaxVolumeNameLength = 10;
21 const G4int TG4XMLConvertor::fgkMaxMaterialNameLength = 20;
23 TG4XMLConvertor::TG4XMLConvertor(G4std::ofstream& outFile)
26 fIndention(fBasicIndention)
31 TG4XMLConvertor::~TG4XMLConvertor() {
37 void TG4XMLConvertor::CutName(G4String& name) const
39 // Removes spaces after the name if present.
42 G4int i = name.length();
43 while (name(--i) == ' ') name = name(0,i);
46 void TG4XMLConvertor::CutName(G4String& name, G4int size) const
48 // Cuts name to given size.
51 if (name.length() > size) name = name(0, size);
54 void TG4XMLConvertor::PutName(G4String& element, G4String name,
57 // Replaces given template in string element with a give name.
63 CutName(name, fgkMaxVolumeNameLength);
64 else if (templ == "!")
65 CutName(name, fgkMaxMaterialNameLength);
67 element.replace(element.find(templ), name.size(), name);
68 element.replace(element.find(templ), 1, "\"");
69 while (element.contains(templ)) element.replace(element.find(templ), 1 , " ");
72 void TG4XMLConvertor::WriteBox(const G4Box* box, G4String materialName)
74 // Writes G4box solid.
78 G4String solidName = box->GetName();
79 G4double x = box->GetXHalfLength()/TG3Units::Length();
80 G4double y = box->GetYHalfLength()/TG3Units::Length();
81 G4double z = box->GetZHalfLength()/TG3Units::Length();
83 // compose element string template
85 = "<box name=\"########### material=\"!!!!!!!!!!!!!!!!!!!!! X_Y_Z=\"";
86 G4String element2 = "\" />";
88 // put solid and material names
89 PutName(element1, solidName, "#");
90 PutName(element1, materialName, "!");
93 fOutFile << fBasicIndention
95 << G4std::setw(7) << G4std::setprecision(2) << x << " "
96 << G4std::setw(7) << G4std::setprecision(2) << y << " "
97 << G4std::setw(7) << G4std::setprecision(2) << z
102 void TG4XMLConvertor::WriteTubs(const G4Tubs* tubs, G4String materialName)
104 // Writes G4tubs solid.
108 G4String solidName = tubs->GetName();
109 G4double rmin = tubs->GetInnerRadius()/TG3Units::Length();
110 G4double rmax = tubs->GetOuterRadius()/TG3Units::Length();
111 G4double hz = tubs->GetZHalfLength()/TG3Units::Length();
112 G4double sphi = tubs->GetStartPhiAngle()/TG3Units::Angle();
113 G4double dphi = tubs->GetDeltaPhiAngle()/TG3Units::Angle();
115 // compose element string template
117 = "<tubs name=\"########### material=\"!!!!!!!!!!!!!!!!!!!!! Rio_Z=\"";
118 G4String element2 = "\" profile=\"";
119 G4String element3 = "\" />";
121 // put solid and material names
122 PutName(element1, solidName, "#");
123 PutName(element1, materialName, "!");
126 fOutFile << fBasicIndention
128 << G4std::setw(7) << G4std::setprecision(2) << rmin << " "
129 << G4std::setw(7) << G4std::setprecision(2) << rmax << " "
130 << G4std::setw(7) << G4std::setprecision(2) << hz
132 << G4std::setw(7) << G4std::setprecision(2) << sphi << " "
133 << G4std::setw(7) << G4std::setprecision(2) << dphi
139 void TG4XMLConvertor::WriteTrd(const G4Trd* trd, G4String materialName)
141 // Writes G4Trd solid.
145 G4String solidName = trd->GetName();
146 G4double x1 = trd->GetXHalfLength1()/TG3Units::Length();
147 G4double x2 = trd->GetXHalfLength2()/TG3Units::Length();
148 G4double y1 = trd->GetYHalfLength1()/TG3Units::Length();
149 G4double y2 = trd->GetYHalfLength2()/TG3Units::Length();
150 G4double hz = trd->GetZHalfLength()/TG3Units::Length();
152 // compose element string template
154 = "<trd name=\"########### material=\"!!!!!!!!!!!!!!!!!!!!! Xmp_Ymp_Z=\"";
155 G4String element2 = "\" />";
157 // put solid and material names
158 // put solid and material names
159 PutName(element1, solidName, "#");
160 PutName(element1, materialName, "!");
163 fOutFile << fBasicIndention
165 << G4std::setw(7) << G4std::setprecision(2) << x1 << " "
166 << G4std::setw(7) << G4std::setprecision(2) << x2 << " "
167 << G4std::setw(7) << G4std::setprecision(2) << y1 << " "
168 << G4std::setw(7) << G4std::setprecision(2) << y2 << " "
169 << G4std::setw(7) << G4std::setprecision(2) << hz
177 void TG4XMLConvertor::OpenMaterials(const G4String& version,
178 const G4String& date, const G4String& author,
179 const G4String dtdVersion)
181 // Writes section opening.
184 G4String element1 = "<materials version = \"";
185 G4String element2 = " date = \"";
186 G4String element3 = " author = \"";
187 G4String element4 = " DTD_version=\"";
188 G4String element5 = " >";
189 G4String quota = "\"";
192 fOutFile << element1 << version << quota << G4endl
193 << element2 << date << quota << G4endl
194 << element3 << author << quota << G4endl
195 << element4 << dtdVersion << quota
196 << element5 << G4endl;
199 void TG4XMLConvertor::OpenSection(const G4String& name, const G4String& version,
200 const G4String& date, const G4String& author,
201 const G4String& topVolume)
203 // Writes section opening.
206 G4String element1 = "<section name = \"";
207 G4String element2 = " version = \"";
208 G4String element3 = " date = \"";
209 G4String element4 = " author = \"";
210 G4String element5 = " topVolume = \"";
211 G4String element6 = " >";
212 G4String quota = "\"";
215 fOutFile << element1 << name << quota << G4endl
216 << element2 << version << quota << G4endl
217 << element3 << date << quota << G4endl
218 << element4 << author << quota << G4endl
219 << element5 << topVolume << quota
220 << element6 << G4endl;
223 void TG4XMLConvertor::OpenComposition(const G4String& name)
225 // Writes composition opening.
228 G4String element = "<composition name=\"";
229 element.append(name);
233 fOutFile << fIndention
237 // increase indention
241 void TG4XMLConvertor::CloseMaterials()
243 // Writes materials closing.
247 G4String element = "</materials>";
254 void TG4XMLConvertor::CloseSection()
256 // Writes section closing.
260 G4String element = "</section>";
267 void TG4XMLConvertor::CloseComposition()
269 // Writes composition closing.
272 // decrease indention
276 G4String element = "</composition>";
279 fOutFile << fIndention
284 void TG4XMLConvertor::WriteMaterial(const G4Material* material)
286 // Writes G4Material.
287 // Not yet implemented, only XML comment element is written.
290 G4String name = material->GetName();
293 // return if material of this name was already written
294 if (fMaterialNames.find(name) != fMaterialNames.end()) return;
296 fMaterialNames.insert(fMaterialNames.begin(), name);
299 G4String element1 = "<!-- material = \"";
300 G4String element2 = "\" -->";
303 fOutFile << fBasicIndention
309 void TG4XMLConvertor::WriteSolid(const G4VSolid* solid, G4String materialName)
311 // Finds G4Solid concrete type and calls writing function.
312 // For not yet implemented solids, only XML comment element is written.
315 G4String name = solid->GetName();
317 // return if solid of this name was already written
318 if (fSolidNames.find(name) != fSolidNames.end()) return;
320 fSolidNames.insert(fSolidNames.begin(), name);
322 // find concrete solid type and write it
324 const G4Box* box = dynamic_cast<const G4Box*>(solid);
326 WriteBox(box, materialName);
330 const G4Tubs* tubs = dynamic_cast<const G4Tubs*>(solid);
332 WriteTubs(tubs, materialName);
336 const G4Trd* trd = dynamic_cast<const G4Trd*>(solid);
338 WriteTrd(trd, materialName);
342 // write comment line in case of unsupported
346 G4String element1 = "<!-- unsupported shape name= \"";
347 G4String element2 = "\" -->";
350 fOutFile << fBasicIndention
356 void TG4XMLConvertor::WriteRotation(const G4RotationMatrix* rotation)
358 // Writes G4RotationMatrix.
359 // Not yet implemented, only XML comment element is written.
362 // return if this rotation was already written
363 G4int nofRotations = fRotations.size();
365 for (G4int i=0; i<nofRotations; i++)
366 if (fRotations[i] == rotation) return;
368 fRotations.push_back(rotation);
371 G4double xx = rotation->xx();
372 G4double xy = rotation->xy();
373 G4double xz = rotation->xz();
374 G4double yx = rotation->yx();
375 G4double yy = rotation->yy();
376 G4double yz = rotation->yz();
377 G4double zx = rotation->zx();
378 G4double zy = rotation->zy();
379 G4double zz = rotation->zz();
381 TG4Globals::AppendNumberToString(id, nofRotations);
383 // compose element string template
384 G4String quota = "\"\n";
385 G4String element1 = "<rot_matrix id=\"####### XX_XY_XZ=\"";
386 G4String element2 = " YX_YY_YZ=\"";
387 G4String element3 = " ZX_ZY_ZZ=\"";
388 G4String element4 = "\" />";
391 PutName(element1, id, "#");
394 fOutFile << fBasicIndention
396 << G4std::setw(8) << G4std::setprecision(5) << xx << " "
397 << G4std::setw(8) << G4std::setprecision(5) << xy << " "
398 << G4std::setw(8) << G4std::setprecision(5) << xz << quota
401 << G4std::setw(8) << G4std::setprecision(5) << yx << " "
402 << G4std::setw(8) << G4std::setprecision(5) << yy << " "
403 << G4std::setw(8) << G4std::setprecision(5) << yz << quota
406 << G4std::setw(8) << G4std::setprecision(5) << zx << " "
407 << G4std::setw(8) << G4std::setprecision(5) << zy << " "
408 << G4std::setw(8) << G4std::setprecision(5) << zz
413 void TG4XMLConvertor::WritePosition(G4String solidName, G4ThreeVector position)
415 // Writes position without rotation with a given solid name.
419 G4double x = position.x()/TG3Units::Length();
420 G4double y = position.y()/TG3Units::Length();
421 G4double z = position.z()/TG3Units::Length();
423 // compose element string template
424 G4String element1 = "<posXYZ volume=\"########### X_Y_Z=\"";
425 G4String element2 = "\" />";
428 PutName(element1, solidName, "#");
431 fOutFile << fIndention
433 << G4std::setw(8) << G4std::setprecision(2) << x << " "
434 << G4std::setw(8) << G4std::setprecision(2) << y << " "
435 << G4std::setw(8) << G4std::setprecision(2) << z
440 void TG4XMLConvertor::WritePositionWithRotation(
441 G4String solidName, G4ThreeVector position,
442 const G4RotationMatrix* rotation)
444 // Writes position with rotation with a given solid name.
445 // Not yet implemented, only XML comment element is written.
449 G4double x = position.x()/TG3Units::Length();
450 G4double y = position.y()/TG3Units::Length();
451 G4double z = position.z()/TG3Units::Length();
455 while (i<fRotations.size() && fRotations[i] != rotation) i++;
456 if (i==fRotations.size()) {
457 G4String text = "TG4XMLConvertor::WritePositionWithRotation: ";
458 text = text + " Unknown rotation - fatal error.";
459 TG4Globals::Exception(text);
462 TG4Globals::AppendNumberToString(id, i);
464 // compose element string template
465 G4String element1 = "<posXYZRot volume=\"########### X_Y_Z=\"";
466 G4String element2 = "\" rot=\"";
467 G4String element3 = "\" />";
470 PutName(element1, solidName, "#");
473 fOutFile << fIndention
475 << G4std::setw(8) << G4std::setprecision(2) << x << " "
476 << G4std::setw(8) << G4std::setprecision(2) << y << " "
477 << G4std::setw(8) << G4std::setprecision(2) << z
484 void TG4XMLConvertor::WriteEmptyLine()
486 // Writes empty line.
492 void TG4XMLConvertor::IncreaseIndention()
494 // increase indention
495 fIndention.append(fBasicIndention);
498 void TG4XMLConvertor::DecreaseIndention()
500 // decrease indention
501 fIndention.replace(fIndention.find(fBasicIndention), 3 , "");