]> git.uio.no Git - u/mrichter/AliRoot.git/blame - TGeant4/TG4XMLConvertor.cxx
tags T/AliGeant4-3-03__ar-v3-05-Release__g4-3-2 commented
[u/mrichter/AliRoot.git] / TGeant4 / TG4XMLConvertor.cxx
CommitLineData
240601e9 1// $Id$
2// Category: geometry
3//
4// See the class description in the header file.
5
6#include "TG4XMLConvertor.h"
2217607d 7#include "TG4Polycone.h"
8#include "TG4Polyhedra.h"
b009ffa0 9#include "TG4G3Units.h"
240601e9 10
2217607d 11#include <G4PVReplica.hh>
240601e9 12#include <G4Material.hh>
13#include <G4VSolid.hh>
14#include <G4Box.hh>
15#include <G4Tubs.hh>
2217607d 16#include <G4Cons.hh>
240601e9 17#include <G4Trd.hh>
4032dc03 18#include <G4Trap.hh>
5d22ddb7 19#include <G4Para.hh>
2217607d 20#include <G4Polycone.hh>
21#include <G4Polyhedra.hh>
240601e9 22
23#include <g4std/iostream>
24#include <g4std/iomanip>
25
26const G4int TG4XMLConvertor::fgkMaxVolumeNameLength = 10;
27const G4int TG4XMLConvertor::fgkMaxMaterialNameLength = 20;
b7ee896b 28const G4int TG4XMLConvertor::fgkDefaultNumWidth = 7;
29const G4int TG4XMLConvertor::fgkDefaultNumPrecision = 4;
240601e9 30
31TG4XMLConvertor::TG4XMLConvertor(G4std::ofstream& outFile)
32 : fOutFile(outFile),
4b062c78 33 fkBasicIndention(" "),
b7ee896b 34 fNW(fgkDefaultNumWidth),
35 fNP(fgkDefaultNumPrecision),
4b062c78 36 fIndention(fkBasicIndention),
4032dc03 37 fRotationCounter(0)
240601e9 38{
b7ee896b 39 fOutFile.width(fgkDefaultNumWidth);
40 fOutFile.precision(fgkDefaultNumPrecision);
240601e9 41}
42
43TG4XMLConvertor::~TG4XMLConvertor() {
44//
45}
46
47// private methods
48
49void TG4XMLConvertor::CutName(G4String& name) const
50{
51// Removes spaces after the name if present.
52// ---
53
54 G4int i = name.length();
55 while (name(--i) == ' ') name = name(0,i);
56}
57
58void TG4XMLConvertor::CutName(G4String& name, G4int size) const
59{
60// Cuts name to given size.
61// ---
62
63 if (name.length() > size) name = name(0, size);
64}
65
66void TG4XMLConvertor::PutName(G4String& element, G4String name,
67 G4String templ) const
68{
69// Replaces given template in string element with a give name.
70// ---
71
72 CutName(name);
73 // make better
74 if (templ == "#")
75 CutName(name, fgkMaxVolumeNameLength);
76 else if (templ == "!")
77 CutName(name, fgkMaxMaterialNameLength);
78
79 element.replace(element.find(templ), name.size(), name);
80 element.replace(element.find(templ), 1, "\"");
81 while (element.contains(templ)) element.replace(element.find(templ), 1 , " ");
82}
83
4032dc03 84void TG4XMLConvertor::WriteBox(G4String lvName, const G4Box* box,
85 G4String materialName)
240601e9 86{
87// Writes G4box solid.
88// ---
89
90 // get parameters
b009ffa0 91 G4double x = box->GetXHalfLength()/TG4G3Units::Length()*2.;
92 G4double y = box->GetYHalfLength()/TG4G3Units::Length()*2.;
93 G4double z = box->GetZHalfLength()/TG4G3Units::Length()*2.;
240601e9 94
95 // compose element string template
2217607d 96 G4String quota = "\"";
97 G4String element1 = "<box name=\"" + lvName + quota;
98 G4String element2 = "material=\"" + materialName + quota;
99 G4String element3 = "X_Y_Z=\"";
100 G4String element4 = "\" />";
4b062c78 101 G4String indention = fkBasicIndention + fkBasicIndention;
240601e9 102
103 // write element
4b062c78 104 fOutFile << fkBasicIndention << element1 << G4endl
105 << indention << element2 << G4endl
106 << indention << element3
b7ee896b 107 << G4std::setw(fNW) << G4std::setprecision(fNP) << x << " "
108 << G4std::setw(fNW) << G4std::setprecision(fNP) << y << " "
109 << G4std::setw(fNW) << G4std::setprecision(fNP) << z
2217607d 110 << element4 << G4endl << G4endl;
240601e9 111}
112
4032dc03 113void TG4XMLConvertor::WriteTubs(G4String lvName, const G4Tubs* tubs,
114 G4String materialName)
240601e9 115{
116// Writes G4tubs solid.
117// ---
118
119 // get parameters
b009ffa0 120 G4double rmin = tubs->GetInnerRadius()/TG4G3Units::Length();
121 G4double rmax = tubs->GetOuterRadius()/TG4G3Units::Length();
122 G4double hz = tubs->GetZHalfLength()/TG4G3Units::Length()*2.;
123 G4double sphi = tubs->GetStartPhiAngle()/TG4G3Units::Angle();
124 G4double dphi = tubs->GetDeltaPhiAngle()/TG4G3Units::Angle();
240601e9 125
126 // compose element string template
2217607d 127 G4String quota = "\"";
128 G4String element1 = "<tubs name=\"" + lvName + quota;
129 G4String element2 = "material=\"" + materialName + quota;
130 G4String element3 = "profile=\"";
131 G4String element4 = "Rio_Z =\"";
132 G4String element5 = "\" />";
4b062c78 133 G4String indention = fkBasicIndention + fkBasicIndention;
240601e9 134
135 // write element
4b062c78 136 fOutFile << fkBasicIndention << element1 << G4endl
137 << indention << element2 << G4endl
138 << indention << element3
b7ee896b 139 << G4std::setw(fNW) << G4std::setprecision(fNP) << sphi << " "
140 << G4std::setw(fNW) << G4std::setprecision(fNP) << sphi+dphi
2217607d 141 << quota << G4endl
4b062c78 142 << indention << element4
b7ee896b 143 << G4std::setw(fNW) << G4std::setprecision(fNP) << rmin << " "
144 << G4std::setw(fNW) << G4std::setprecision(fNP) << rmax << " "
145 << G4std::setw(fNW) << G4std::setprecision(fNP) << hz
2217607d 146 << element5 << G4endl << G4endl;
147}
148
149
150void TG4XMLConvertor::WriteCons(G4String lvName, const G4Cons* cons,
151 G4String materialName)
152{
153// Writes G4cons solid.
154// ---
155
156 // get parameters
b009ffa0 157 G4double rmin1 = cons->GetInnerRadiusMinusZ()/TG4G3Units::Length();
158 G4double rmax1 = cons->GetOuterRadiusMinusZ()/TG4G3Units::Length();
159 G4double rmin2 = cons->GetInnerRadiusPlusZ()/TG4G3Units::Length();
160 G4double rmax2 = cons->GetOuterRadiusPlusZ()/TG4G3Units::Length();
161 G4double hz = cons->GetZHalfLength()/TG4G3Units::Length()*2.;
162 G4double sphi = cons->GetStartPhiAngle()/TG4G3Units::Angle();
163 G4double dphi = cons->GetDeltaPhiAngle()/TG4G3Units::Angle();
2217607d 164
165 // compose element string template
166 G4String quota = "\"";
167 G4String element1 = "<cons name=\"" + lvName + quota;
168 G4String element2 = "material=\"" + materialName + quota;
169 G4String element3 = "profile=\"";
170 G4String element4 = "Rio1_Rio2_Z =\"";
171 G4String element5 = "\" />";
4b062c78 172 G4String indention = fkBasicIndention + fkBasicIndention;
2217607d 173
174 // write element
4b062c78 175 fOutFile << fkBasicIndention << element1 << G4endl
176 << indention << element2 << G4endl
177 << indention << element3
b7ee896b 178 << G4std::setw(fNW) << G4std::setprecision(fNP) << sphi << " "
179 << G4std::setw(fNW) << G4std::setprecision(fNP) << sphi+dphi
2217607d 180 << quota << G4endl
4b062c78 181 << indention << element4
b7ee896b 182 << G4std::setw(fNW) << G4std::setprecision(fNP) << rmin1 << " "
183 << G4std::setw(fNW) << G4std::setprecision(fNP) << rmin2 << " "
184 << G4std::setw(fNW) << G4std::setprecision(fNP) << rmax1 << " "
185 << G4std::setw(fNW) << G4std::setprecision(fNP) << rmax2 << " "
186 << G4std::setw(fNW) << G4std::setprecision(fNP) << hz
2217607d 187 << element5 << G4endl << G4endl;
240601e9 188}
189
190
4032dc03 191void TG4XMLConvertor::WriteTrd(G4String lvName, const G4Trd* trd,
192 G4String materialName)
240601e9 193{
194// Writes G4Trd solid.
195// ---
196
197 // get parameters
b009ffa0 198 G4double x1 = trd->GetXHalfLength1()/TG4G3Units::Length()*2;
199 G4double x2 = trd->GetXHalfLength2()/TG4G3Units::Length()*2;
200 G4double y1 = trd->GetYHalfLength1()/TG4G3Units::Length()*2;
201 G4double y2 = trd->GetYHalfLength2()/TG4G3Units::Length()*2;
202 G4double hz = trd->GetZHalfLength()/TG4G3Units::Length()*2;
240601e9 203
204 // compose element string template
2217607d 205 G4String quota = "\"";
206 G4String element1 = "<trd name=\"" + lvName + quota;
207 G4String element2 = "material=\"" + materialName + quota;
208 G4String element3 = "Xmp_Ymp_Z=\"";
209 G4String element4 = "\" />";
4b062c78 210 G4String indention = fkBasicIndention + fkBasicIndention;
240601e9 211
212 // write element
4b062c78 213 fOutFile << fkBasicIndention << element1 << G4endl
214 << indention << element2 << G4endl
215 << indention << element3
b7ee896b 216 << G4std::setw(fNW) << G4std::setprecision(fNP) << x1 << " "
217 << G4std::setw(fNW) << G4std::setprecision(fNP) << x2 << " "
218 << G4std::setw(fNW) << G4std::setprecision(fNP) << y1 << " "
219 << G4std::setw(fNW) << G4std::setprecision(fNP) << y2 << " "
220 << G4std::setw(fNW) << G4std::setprecision(fNP) << hz
2217607d 221 << element4 << G4endl << G4endl;
240601e9 222}
223
224
4032dc03 225void TG4XMLConvertor::WriteTrap(G4String lvName, const G4Trap* trap,
226 G4String materialName)
227{
228// Writes G4Trap solid.
229// ---
230
231 // get parameters
b009ffa0 232 G4double dz = trap->GetZHalfLength()/TG4G3Units::Length()*2.;
4032dc03 233 G4ThreeVector symAxis = trap->GetSymAxis();
b009ffa0 234 G4double y1 = trap->GetYHalfLength1()/TG4G3Units::Length()*2.;
235 G4double x1 = trap->GetXHalfLength1()/TG4G3Units::Length()*2.;
236 G4double x2 = trap->GetXHalfLength2()/TG4G3Units::Length()*2.;
4032dc03 237 G4double tanAlpha1 = trap->GetTanAlpha1();
b009ffa0 238 G4double y2 = trap->GetYHalfLength2()/TG4G3Units::Length()*2.;
239 G4double x3 = trap->GetXHalfLength3()/TG4G3Units::Length()*2.;
240 G4double x4 = trap->GetXHalfLength4()/TG4G3Units::Length()*2.;
4032dc03 241 G4double tanAlpha2 = trap->GetTanAlpha2();
242
243 // ordering of parameters in XML element
244 // Xmumdpupd_Ymp_Z: 2x2 2x1 2x4 2x3 2y2 2y1 2dz
245 // inclination: atan(symAxis.x/symAxis.z), atan(symAxis.y/symAxis.z)
246 // declination: alpha1, alpha2
247
248 // get angles
249 G4double inc1 = atan(symAxis.x()/symAxis.z()) / deg;
250 G4double inc2 = atan(symAxis.y()/symAxis.z()) / deg;
251 G4double alpha1 = atan(tanAlpha1) / deg;
252 G4double alpha2 = atan(tanAlpha2) / deg;
253
254 // compose element string template
2217607d 255 G4String quota = "\"";
256 G4String element1 = "<trap name=\"" + lvName + quota;
257 G4String element2 = "material=\"" + materialName + quota;
258 G4String element3 = "Xmumdpupd_Ymp_Z=\"";
259 G4String element4 = "inclination=\"";
260 G4String element5 = "declination=\"";
261 G4String element6 = "\" />";
4b062c78 262 G4String indention = fkBasicIndention + fkBasicIndention;
2217607d 263
4032dc03 264 // write element
4b062c78 265 fOutFile << fkBasicIndention << element1 << G4endl
266 << indention << element2 << G4endl
267 << indention << element3
b7ee896b 268 << G4std::setw(fNW) << G4std::setprecision(fNP) << x2 << " "
269 << G4std::setw(fNW) << G4std::setprecision(fNP) << x1 << " "
270 << G4std::setw(fNW) << G4std::setprecision(fNP) << x4 << " "
271 << G4std::setw(fNW) << G4std::setprecision(fNP) << x3 << " "
272 << G4std::setw(fNW) << G4std::setprecision(fNP) << y2 << " "
273 << G4std::setw(fNW) << G4std::setprecision(fNP) << y1 << " "
274 << G4std::setw(fNW) << G4std::setprecision(fNP) << dz
2217607d 275 << quota << G4endl
276 << indention << element4
b7ee896b 277 << G4std::setw(fNW) << G4std::setprecision(fNP) << inc1 << " "
278 << G4std::setw(fNW) << G4std::setprecision(fNP) << inc2
2217607d 279 << quota << G4endl
280 << indention << element5
b7ee896b 281 << G4std::setw(fNW) << G4std::setprecision(fNP) << alpha1 << " "
282 << G4std::setw(fNW) << G4std::setprecision(fNP) << alpha2
2217607d 283 << element6 << G4endl << G4endl;
4032dc03 284}
285
5d22ddb7 286void TG4XMLConvertor::WritePara(G4String lvName, const G4Para* para,
287 G4String materialName)
288{
289// Writes G4Para solid.
290// ---
291
292 // get parameters
293 G4double dx = para->GetXHalfLength()/TG4G3Units::Length()*2.;
294 G4double dy = para->GetYHalfLength()/TG4G3Units::Length()*2.;
295 G4double dz = para->GetZHalfLength()/TG4G3Units::Length()*2.;
296 G4double tanAlpha = para->GetTanAlpha();
297 G4ThreeVector symAxis = para->GetSymAxis();
298
299 G4double alpha = atan(tanAlpha) / deg;
300 G4double theta = acos(symAxis.z()) / deg;
301 G4double phi;
302 if (theta == 0.)
303 phi = 0;
304 else
305 phi = atan(symAxis.y()/symAxis.x()) / deg;
306
307 // compose element string template
308 G4String quota = "\"";
309 G4String element1 = "<para name=\"" + lvName + quota;
310 G4String element2 = "material=\"" + materialName + quota;
311 G4String element3 = "X_Y_Z=\"";
312 G4String element4 = "alpha=\"";
313 G4String element5 = "theta=\"";
314 G4String element6 = "phi= \"";
315 G4String element7 = "\" />";
316 G4String indention = fkBasicIndention + fkBasicIndention;
317
318 // write element
319 fOutFile << fkBasicIndention << element1 << G4endl
320 << indention << element2 << G4endl
321 << indention << element3
b7ee896b 322 << G4std::setw(fNW) << G4std::setprecision(fNP) << dx << " "
323 << G4std::setw(fNW) << G4std::setprecision(fNP) << dy << " "
324 << G4std::setw(fNW) << G4std::setprecision(fNP) << dz
5d22ddb7 325 << quota << G4endl
326 << indention << element4
b7ee896b 327 << G4std::setw(fNW) << G4std::setprecision(fNP) << alpha
5d22ddb7 328 << quota << G4endl
329 << indention << element5
b7ee896b 330 << G4std::setw(fNW) << G4std::setprecision(fNP) << theta
5d22ddb7 331 << quota << G4endl
332 << indention << element6
b7ee896b 333 << G4std::setw(fNW) << G4std::setprecision(fNP) << phi
5d22ddb7 334 << element7 << G4endl << G4endl;
335}
336
2217607d 337void TG4XMLConvertor::WritePolycone(G4String lvName, const G4Polycone* polycone,
338 G4String materialName)
339{
340// Writes G4Polycone solid.
341// ---
342
343 // get profile parameters
b009ffa0 344 G4double sphi = polycone->GetStartPhi()/TG4G3Units::Angle();
345 G4double ephi = polycone->GetEndPhi()/TG4G3Units::Angle();
2217607d 346
347 // get polycone Z planes parameters
348 TG4Polycone historicalPolycone = TG4Polycone(*polycone);
349
350 G4int nofZPlanes = historicalPolycone.GetNofZPlanes();
351 G4double* rminArray = historicalPolycone.GetRmin();
352 G4double* rmaxArray = historicalPolycone.GetRmax();
353 G4double* zArray = historicalPolycone.GetZ();
354
355 // compose element string template
356 G4String quota = "\"";
357 G4String element1 = "<pcon name=\"" + lvName + quota;
358 G4String element2 = "material=\"" + materialName + quota;
359 G4String element3 = "profile=\"";
360 G4String element4 = "\" >";
361 G4String element5 = "<polyplane Rio_Z=\"";
362 G4String element6 = "\" />";
363 G4String element7 = "</pcon>";
4b062c78 364 G4String indention = fkBasicIndention + fkBasicIndention;
2217607d 365
366 // write pcon element
4b062c78 367 fOutFile << fkBasicIndention << element1 << G4endl
368 << indention << element2 << G4endl
369 << indention << element3
b7ee896b 370 << G4std::setw(fNW) << G4std::setprecision(fNP) << sphi << " "
371 << G4std::setw(fNW) << G4std::setprecision(fNP) << ephi
2217607d 372 << element4 << G4endl;
373
374 // write polyplane elements
375 for (G4int i=0; i<nofZPlanes; i++) {
376
377 // set units
b009ffa0 378 G4double rmin = rminArray[i]/TG4G3Units::Length();
379 G4double rmax = rmaxArray[i]/TG4G3Units::Length();
380 G4double z = zArray[i]/TG4G3Units::Length();
2217607d 381
382 fOutFile << indention << element5
b7ee896b 383 << G4std::setw(fNW) << G4std::setprecision(fNP) << rmin << " "
384 << G4std::setw(fNW) << G4std::setprecision(fNP) << rmax << " "
385 << G4std::setw(fNW) << G4std::setprecision(fNP) << z
2217607d 386 << element6
387 << G4endl;
388 }
389
390 // close pcon element
4b062c78 391 fOutFile << fkBasicIndention
2217607d 392 << element7 << G4endl << G4endl;
393}
394
395
396void TG4XMLConvertor::WritePolyhedra(G4String lvName, const G4Polyhedra* polyhedra,
397 G4String materialName)
398{
399// Writes G4Polycone solid.
400// ---
401
402 // get parameters
403 G4int nofSides = polyhedra->GetNumSide();
b009ffa0 404 G4double sphi = polyhedra->GetStartPhi()/TG4G3Units::Angle();
405 G4double ephi = polyhedra->GetEndPhi()/TG4G3Units::Angle();
2217607d 406
407 // get polyhedra Z planes parameters
408 TG4Polyhedra historicalPolyhedra = TG4Polyhedra(*polyhedra);
409
410 G4int nofZPlanes = historicalPolyhedra.GetNofZPlanes();
411 G4double* rminArray = historicalPolyhedra.GetRmin();
412 G4double* rmaxArray = historicalPolyhedra.GetRmax();
413 G4double* zArray = historicalPolyhedra.GetZ();
414
415 // compose element string template
416 G4String quota = "\"";
417 G4String element1 = "<phedra name=\"" + lvName + quota;
418 G4String element2 = "material=\"" + materialName + quota;
419 G4String element3 = "profile=\"";
420 G4String element4 = "sides =\"";
421 G4String element5 = "Ris=\"";
422 G4String element6 = "Ros=\"";
423 G4String element7 = "Zs =\"";
424 G4String element8 = "\" />";
4b062c78 425 G4String indention = fkBasicIndention + fkBasicIndention;
2217607d 426
427 // write element
4b062c78 428 fOutFile << fkBasicIndention << element1 << G4endl
429 << indention << element2 << G4endl
430 << indention << element3
b7ee896b 431 << G4std::setw(fNW) << G4std::setprecision(fNP) << sphi << " "
432 << G4std::setw(fNW) << G4std::setprecision(fNP) << ephi
2217607d 433 << quota << G4endl
434 << indention << element4
435 << nofSides
436 << quota << G4endl;
437
438 fOutFile << indention << element5;
439 G4int i;
440 for (i=0; i<nofZPlanes; i++) {
441 // set units
b009ffa0 442 G4double rmin = rminArray[i]/TG4G3Units::Length();
2217607d 443 if (i>0) fOutFile << " ";
b7ee896b 444 fOutFile << G4std::setw(fNW) << G4std::setprecision(fNP) << rmin;
2217607d 445 };
446 fOutFile << quota << G4endl;
447
448 fOutFile << indention << element6;
449 for (i=0; i<nofZPlanes; i++) {
450 // set units
b009ffa0 451 G4double rmax = rmaxArray[i]/TG4G3Units::Length();
2217607d 452 if (i>0) fOutFile << " ";
b7ee896b 453 fOutFile << G4std::setw(fNW) << G4std::setprecision(fNP) << rmax;
2217607d 454 };
455 fOutFile << quota << G4endl;
456
457 fOutFile << indention << element7;
458 for (i=0; i<nofZPlanes; i++) {
459 // set units
b009ffa0 460 G4double z = zArray[i]/TG4G3Units::Length();
2217607d 461 if (i>0) fOutFile << " ";
b7ee896b 462 fOutFile << G4std::setw(fNW) << G4std::setprecision(fNP) << z;
2217607d 463 };
464 fOutFile << element8 << G4endl << G4endl;
465}
466
467
240601e9 468// public methods
469
3716fae8 470void TG4XMLConvertor::OpenMaterials(const G4String& version,
471 const G4String& date, const G4String& author,
472 const G4String dtdVersion)
473{
474// Writes section opening.
475// ---
476
477 G4String element1 = "<materials version = \"";
478 G4String element2 = " date = \"";
479 G4String element3 = " author = \"";
480 G4String element4 = " DTD_version=\"";
481 G4String element5 = " >";
482 G4String quota = "\"";
483
484 // write element
485 fOutFile << element1 << version << quota << G4endl
486 << element2 << date << quota << G4endl
487 << element3 << author << quota << G4endl
488 << element4 << dtdVersion << quota
489 << element5 << G4endl;
490}
491
240601e9 492void TG4XMLConvertor::OpenSection(const G4String& name, const G4String& version,
493 const G4String& date, const G4String& author,
494 const G4String& topVolume)
495{
496// Writes section opening.
497// ---
498
499 G4String element1 = "<section name = \"";
500 G4String element2 = " version = \"";
501 G4String element3 = " date = \"";
502 G4String element4 = " author = \"";
503 G4String element5 = " topVolume = \"";
504 G4String element6 = " >";
505 G4String quota = "\"";
506
507 // write element
508 fOutFile << element1 << name << quota << G4endl
509 << element2 << version << quota << G4endl
510 << element3 << date << quota << G4endl
511 << element4 << author << quota << G4endl
512 << element5 << topVolume << quota
513 << element6 << G4endl;
514}
515
516void TG4XMLConvertor::OpenComposition(const G4String& name)
517{
518// Writes composition opening.
519// ---
520
521 G4String element = "<composition name=\"";
522 element.append(name);
4032dc03 523 element.append("\">");
240601e9 524
525 // write element
526 fOutFile << fIndention
527 << element
528 << G4endl;
529
530 // increase indention
3716fae8 531 IncreaseIndention();
532}
533
534void TG4XMLConvertor::CloseMaterials()
535{
536// Writes materials closing.
537// ---
538
539 // define element
540 G4String element = "</materials>";
541
542 // write element
543 fOutFile << element
544 << G4endl;
240601e9 545}
546
547void TG4XMLConvertor::CloseSection()
548{
549// Writes section closing.
550// ---
551
552 // define element
553 G4String element = "</section>";
554
555 // write element
556 fOutFile << element
557 << G4endl;
558}
559
560void TG4XMLConvertor::CloseComposition()
561{
562// Writes composition closing.
563// ---
564
565 // decrease indention
3716fae8 566 DecreaseIndention();
240601e9 567
568 // define element
569 G4String element = "</composition>";
570
571 // write element
572 fOutFile << fIndention
573 << element
574 << G4endl;
575}
576
577void TG4XMLConvertor::WriteMaterial(const G4Material* material)
578{
579// Writes G4Material.
580// Not yet implemented, only XML comment element is written.
581// ---
582
583 G4String name = material->GetName();
584 CutName(name);
585
240601e9 586 // only comment line
587 G4String element1 = "<!-- material = \"";
588 G4String element2 = "\" -->";
589
590 // write element
4b062c78 591 fOutFile << fkBasicIndention
3716fae8 592 << element1 << name
240601e9 593 << element2
594 << G4endl;
595}
596
4032dc03 597void TG4XMLConvertor::WriteSolid(G4String lvName, const G4VSolid* solid,
598 G4String materialName)
240601e9 599{
600// Finds G4Solid concrete type and calls writing function.
601// For not yet implemented solids, only XML comment element is written.
602// ---
603
4032dc03 604 // to be removed when materials are supported
605 materialName = "Hydrogen";
606
240601e9 607 const G4Box* box = dynamic_cast<const G4Box*>(solid);
608 if (box) {
4032dc03 609 WriteBox(lvName, box, materialName);
240601e9 610 return;
611 }
612
613 const G4Tubs* tubs = dynamic_cast<const G4Tubs*>(solid);
614 if (tubs) {
4032dc03 615 WriteTubs(lvName, tubs, materialName);
240601e9 616 return;
617 }
618
2217607d 619 const G4Cons* cons = dynamic_cast<const G4Cons*>(solid);
620 if (cons) {
621 WriteCons(lvName, cons, materialName);
622 return;
623 }
624
240601e9 625 const G4Trd* trd = dynamic_cast<const G4Trd*>(solid);
626 if (trd) {
4032dc03 627 WriteTrd(lvName, trd, materialName);
628 return;
629 }
630
631 const G4Trap* trap = dynamic_cast<const G4Trap*>(solid);
632 if (trap) {
633 WriteTrap(lvName, trap, materialName);
240601e9 634 return;
635 }
636
5d22ddb7 637 const G4Para* para = dynamic_cast<const G4Para*>(solid);
638 if (para) {
639 WritePara(lvName, para, materialName);
640 return;
641 }
642
2217607d 643 const G4Polycone* polycone = dynamic_cast<const G4Polycone*>(solid);
644 if (polycone) {
645 WritePolycone(lvName, polycone, materialName);
646 return;
647 }
648
649 const G4Polyhedra* polyhedra = dynamic_cast<const G4Polyhedra*>(solid);
650 if (polyhedra) {
651 WritePolyhedra(lvName, polyhedra, materialName);
652 return;
653 }
654
240601e9 655 // write comment line in case of unsupported
656 // shape
657
658 // only comment line
659 G4String element1 = "<!-- unsupported shape name= \"";
660 G4String element2 = "\" -->";
661
662 // write element
4b062c78 663 fOutFile << fkBasicIndention
4032dc03 664 << element1 << lvName
240601e9 665 << element2
666 << G4endl;
667}
668
669void TG4XMLConvertor::WriteRotation(const G4RotationMatrix* rotation)
670{
671// Writes G4RotationMatrix.
672// Not yet implemented, only XML comment element is written.
673// ---
674
3716fae8 675 // return if this rotation was already written
676 G4int nofRotations = fRotations.size();
677 if (nofRotations>0)
678 for (G4int i=0; i<nofRotations; i++)
679 if (fRotations[i] == rotation) return;
680
681 fRotations.push_back(rotation);
682
4032dc03 683
3716fae8 684 // get parameters
685 G4double xx = rotation->xx();
686 G4double xy = rotation->xy();
687 G4double xz = rotation->xz();
688 G4double yx = rotation->yx();
689 G4double yy = rotation->yy();
690 G4double yz = rotation->yz();
691 G4double zx = rotation->zx();
692 G4double zy = rotation->zy();
693 G4double zz = rotation->zz();
694 G4String id = "RM";
4032dc03 695 TG4Globals::AppendNumberToString(id, ++fRotationCounter);
3716fae8 696
697 // compose element string template
698 G4String quota = "\"\n";
699 G4String element1 = "<rot_matrix id=\"####### XX_XY_XZ=\"";
700 G4String element2 = " YX_YY_YZ=\"";
701 G4String element3 = " ZX_ZY_ZZ=\"";
702 G4String element4 = "\" />";
240601e9 703
3716fae8 704 // put identifier
705 PutName(element1, id, "#");
706
240601e9 707 // write element
4b062c78 708 fOutFile << fkBasicIndention
3716fae8 709 << element1
710 << G4std::setw(8) << G4std::setprecision(5) << xx << " "
711 << G4std::setw(8) << G4std::setprecision(5) << xy << " "
712 << G4std::setw(8) << G4std::setprecision(5) << xz << quota
4b062c78 713 << fkBasicIndention
3716fae8 714 << element2
715 << G4std::setw(8) << G4std::setprecision(5) << yx << " "
716 << G4std::setw(8) << G4std::setprecision(5) << yy << " "
717 << G4std::setw(8) << G4std::setprecision(5) << yz << quota
4b062c78 718 << fkBasicIndention
3716fae8 719 << element3
720 << G4std::setw(8) << G4std::setprecision(5) << zx << " "
721 << G4std::setw(8) << G4std::setprecision(5) << zy << " "
722 << G4std::setw(8) << G4std::setprecision(5) << zz
723 << element4
240601e9 724 << G4endl;
725}
726
4032dc03 727void TG4XMLConvertor::WritePosition(G4String lvName, G4ThreeVector position)
240601e9 728{
729// Writes position without rotation with a given solid name.
730// ---
731
732 // get parameters
b009ffa0 733 G4double x = position.x()/TG4G3Units::Length();
734 G4double y = position.y()/TG4G3Units::Length();
735 G4double z = position.z()/TG4G3Units::Length();
240601e9 736
737 // compose element string template
3716fae8 738 G4String element1 = "<posXYZ volume=\"########### X_Y_Z=\"";
240601e9 739 G4String element2 = "\" />";
740
741 // put solid name
4032dc03 742 PutName(element1, lvName, "#");
240601e9 743
744 // write element
745 fOutFile << fIndention
746 << element1
b7ee896b 747 << G4std::setw(fNW+1) << G4std::setprecision(fNP) << x << " "
748 << G4std::setw(fNW+1) << G4std::setprecision(fNP) << y << " "
749 << G4std::setw(fNW+1) << G4std::setprecision(fNP) << z
240601e9 750 << element2
751 << G4endl;
752}
753
754void TG4XMLConvertor::WritePositionWithRotation(
4032dc03 755 G4String lvName, G4ThreeVector position,
240601e9 756 const G4RotationMatrix* rotation)
757{
758// Writes position with rotation with a given solid name.
759// Not yet implemented, only XML comment element is written.
760// ---
761
3716fae8 762 // get parameters
b009ffa0 763 G4double x = position.x()/TG4G3Units::Length();
764 G4double y = position.y()/TG4G3Units::Length();
765 G4double z = position.z()/TG4G3Units::Length();
4032dc03 766 G4double xx = rotation->xx();
767 G4double xy = rotation->xy();
768 G4double xz = rotation->xz();
769 G4double yx = rotation->yx();
770 G4double yy = rotation->yy();
771 G4double yz = rotation->yz();
772 G4double zx = rotation->zx();
773 G4double zy = rotation->zy();
774 G4double zz = rotation->zz();
3716fae8 775
4032dc03 776/*
3716fae8 777 // find rotation
778 G4int i=0;
779 while (i<fRotations.size() && fRotations[i] != rotation) i++;
780 if (i==fRotations.size()) {
781 G4String text = "TG4XMLConvertor::WritePositionWithRotation: ";
782 text = text + " Unknown rotation - fatal error.";
783 TG4Globals::Exception(text);
784 }
785 G4String id = "RM";
786 TG4Globals::AppendNumberToString(id, i);
4032dc03 787*/
3716fae8 788
789 // compose element string template
4032dc03 790 G4String quota = "\"\n";
791 G4String element1 = "<transform volume=\"########### pos=\"";
792 G4String element2 = " rot=\"";
793 G4String element3 = " ";
794 G4String element4 = "\" />";
3716fae8 795
796 // put solid name
4032dc03 797 PutName(element1, lvName, "#");
240601e9 798
799 // write element
800 fOutFile << fIndention
3716fae8 801 << element1
b7ee896b 802 << G4std::setw(fNW+1) << G4std::setprecision(fNP) << x << " "
803 << G4std::setw(fNW+1) << G4std::setprecision(fNP) << y << " "
804 << G4std::setw(fNW+1) << G4std::setprecision(fNP) << z << quota
4032dc03 805 << fIndention
806 << element2
807 << G4std::setw(8) << G4std::setprecision(5) << xx << " "
808 << G4std::setw(8) << G4std::setprecision(5) << xy << " "
809 << G4std::setw(8) << G4std::setprecision(5) << xz << G4endl
810 << fIndention
811 << element3
812 << G4std::setw(8) << G4std::setprecision(5) << yx << " "
813 << G4std::setw(8) << G4std::setprecision(5) << yy << " "
814 << G4std::setw(8) << G4std::setprecision(5) << yz << G4endl
815 << fIndention
816 << element3
817 << G4std::setw(8) << G4std::setprecision(5) << zx << " "
818 << G4std::setw(8) << G4std::setprecision(5) << zy << " "
819 << G4std::setw(8) << G4std::setprecision(5) << zz
820 << element4
3716fae8 821 << G4endl;
240601e9 822}
823
2217607d 824void TG4XMLConvertor::WriteReplica(G4String lvName, G4PVReplica* pvr)
825{
826// Writes position without rotation with a given solid name.
827// ---
828
829 // get parameters
830 EAxis axis;
831 G4int nReplicas;
832 G4double width;
833 G4double offset;
834 G4bool consuming;
835 pvr->GetReplicationData(axis, nReplicas, width, offset, consuming);
836
837 G4String tag;
838 switch (axis) {
839 case kXAxis: tag = "X"; break;
840 case kYAxis: tag = "Y"; break;
841 case kZAxis: tag = "Z"; break;
842 case kRho: tag = "R"; break;
843 case kPhi: tag = "Phi"; break;
844 }
845
846 // set units
847 G4double value0 = offset;
848 G4double dValue = width;
849 if (axis != kPhi) {
b009ffa0 850 value0 = value0/TG4G3Units::Length();
851 dValue = dValue/TG4G3Units::Length();
2217607d 852 }
853 else {
b009ffa0 854 value0 = value0/TG4G3Units::Angle();
855 dValue = dValue/TG4G3Units::Angle();
2217607d 856 }
857
858 // set tag and attributes names
859 G4String a0 = "mpos"; a0 = a0 + tag;
860 G4String a1 = tag; a1 = a1 + "0";
861 G4String a2 = "d"; a2 = a2 + tag;
862
863 // compose element string template
864 G4String element1 = "<" + a0 + " volume=\"########### ncopy=\"";
865 G4String element2 = "\" " + a1 + "=\"";
866 G4String element3 = "\" " + a2 + "=\"";
867 G4String element4 = "\" />";
868
869 // put solid name
870 PutName(element1, lvName, "#");
871
872 // write element
873 fOutFile << fIndention
874 << element1
b7ee896b 875 << G4std::setw(fNW+1) << G4std::setprecision(fNP) << nReplicas
2217607d 876 << element2
b7ee896b 877 << G4std::setw(fNW+1) << G4std::setprecision(fNP) << value0
2217607d 878 << element3
b7ee896b 879 << G4std::setw(fNW+1) << G4std::setprecision(fNP) << dValue
2217607d 880 << element4
881 << G4endl;
882}
883
240601e9 884void TG4XMLConvertor::WriteEmptyLine()
885{
886// Writes empty line.
887// ---
888
889 fOutFile << G4endl;
890}
891
3716fae8 892void TG4XMLConvertor::IncreaseIndention()
893{
894 // increase indention
4b062c78 895 fIndention.append(fkBasicIndention);
3716fae8 896}
240601e9 897
3716fae8 898void TG4XMLConvertor::DecreaseIndention()
899{
900 // decrease indention
4b062c78 901 fIndention.replace(fIndention.find(fkBasicIndention), 3 , "");
3716fae8 902}