]> git.uio.no Git - u/mrichter/AliRoot.git/blame - TGeant4/TG4XMLConvertor.cxx
added WriteReplica(), WriteCons/Polycone/Polyhedra()
[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"
7#include "TG3Units.h"
8
9#include <G4LogicalVolume.hh>
10#include <G4Material.hh>
11#include <G4VSolid.hh>
12#include <G4Box.hh>
13#include <G4Tubs.hh>
14#include <G4Trd.hh>
4032dc03 15#include <G4Trap.hh>
240601e9 16#include <globals.hh>
17
18#include <g4std/iostream>
19#include <g4std/iomanip>
20
21const G4int TG4XMLConvertor::fgkMaxVolumeNameLength = 10;
22const G4int TG4XMLConvertor::fgkMaxMaterialNameLength = 20;
23
24TG4XMLConvertor::TG4XMLConvertor(G4std::ofstream& outFile)
25 : fOutFile(outFile),
3716fae8 26 fBasicIndention(" "),
4032dc03 27 fIndention(fBasicIndention),
28 fRotationCounter(0)
240601e9 29{
30//
31}
32
33TG4XMLConvertor::~TG4XMLConvertor() {
34//
35}
36
37// private methods
38
39void TG4XMLConvertor::CutName(G4String& name) const
40{
41// Removes spaces after the name if present.
42// ---
43
44 G4int i = name.length();
45 while (name(--i) == ' ') name = name(0,i);
46}
47
48void TG4XMLConvertor::CutName(G4String& name, G4int size) const
49{
50// Cuts name to given size.
51// ---
52
53 if (name.length() > size) name = name(0, size);
54}
55
56void TG4XMLConvertor::PutName(G4String& element, G4String name,
57 G4String templ) const
58{
59// Replaces given template in string element with a give name.
60// ---
61
62 CutName(name);
63 // make better
64 if (templ == "#")
65 CutName(name, fgkMaxVolumeNameLength);
66 else if (templ == "!")
67 CutName(name, fgkMaxMaterialNameLength);
68
69 element.replace(element.find(templ), name.size(), name);
70 element.replace(element.find(templ), 1, "\"");
71 while (element.contains(templ)) element.replace(element.find(templ), 1 , " ");
72}
73
4032dc03 74void TG4XMLConvertor::WriteBox(G4String lvName, const G4Box* box,
75 G4String materialName)
240601e9 76{
77// Writes G4box solid.
78// ---
79
80 // get parameters
240601e9 81 G4double x = box->GetXHalfLength()/TG3Units::Length();
82 G4double y = box->GetYHalfLength()/TG3Units::Length();
83 G4double z = box->GetZHalfLength()/TG3Units::Length();
84
85 // compose element string template
86 G4String element1
87 = "<box name=\"########### material=\"!!!!!!!!!!!!!!!!!!!!! X_Y_Z=\"";
88 G4String element2 = "\" />";
89
90 // put solid and material names
4032dc03 91 PutName(element1, lvName, "#");
240601e9 92 PutName(element1, materialName, "!");
93
94 // write element
3716fae8 95 fOutFile << fBasicIndention
96 << element1
4032dc03 97 << G4std::setw(7) << G4std::setprecision(2) << x*2. << " "
98 << G4std::setw(7) << G4std::setprecision(2) << y*2. << " "
99 << G4std::setw(7) << G4std::setprecision(2) << z*2.
240601e9 100 << element2
101 << G4endl;
102}
103
4032dc03 104void TG4XMLConvertor::WriteTubs(G4String lvName, const G4Tubs* tubs,
105 G4String materialName)
240601e9 106{
107// Writes G4tubs solid.
108// ---
109
110 // get parameters
240601e9 111 G4double rmin = tubs->GetInnerRadius()/TG3Units::Length();
112 G4double rmax = tubs->GetOuterRadius()/TG3Units::Length();
113 G4double hz = tubs->GetZHalfLength()/TG3Units::Length();
114 G4double sphi = tubs->GetStartPhiAngle()/TG3Units::Angle();
115 G4double dphi = tubs->GetDeltaPhiAngle()/TG3Units::Angle();
116
117 // compose element string template
118 G4String element1
119 = "<tubs name=\"########### material=\"!!!!!!!!!!!!!!!!!!!!! Rio_Z=\"";
120 G4String element2 = "\" profile=\"";
121 G4String element3 = "\" />";
122
123 // put solid and material names
4032dc03 124 PutName(element1, lvName, "#");
240601e9 125 PutName(element1, materialName, "!");
126
127 // write element
3716fae8 128 fOutFile << fBasicIndention
129 << element1
240601e9 130 << G4std::setw(7) << G4std::setprecision(2) << rmin << " "
131 << G4std::setw(7) << G4std::setprecision(2) << rmax << " "
4032dc03 132 << G4std::setw(7) << G4std::setprecision(2) << hz*2.
240601e9 133 << element2
134 << G4std::setw(7) << G4std::setprecision(2) << sphi << " "
4032dc03 135 << G4std::setw(7) << G4std::setprecision(2) << sphi+dphi
240601e9 136 << element3
137 << G4endl;
138}
139
140
4032dc03 141void TG4XMLConvertor::WriteTrd(G4String lvName, const G4Trd* trd,
142 G4String materialName)
240601e9 143{
144// Writes G4Trd solid.
145// ---
146
147 // get parameters
240601e9 148 G4double x1 = trd->GetXHalfLength1()/TG3Units::Length();
149 G4double x2 = trd->GetXHalfLength2()/TG3Units::Length();
150 G4double y1 = trd->GetYHalfLength1()/TG3Units::Length();
151 G4double y2 = trd->GetYHalfLength2()/TG3Units::Length();
152 G4double hz = trd->GetZHalfLength()/TG3Units::Length();
153
154 // compose element string template
155 G4String element1
156 = "<trd name=\"########### material=\"!!!!!!!!!!!!!!!!!!!!! Xmp_Ymp_Z=\"";
157 G4String element2 = "\" />";
158
159 // put solid and material names
160 // put solid and material names
4032dc03 161 PutName(element1, lvName, "#");
240601e9 162 PutName(element1, materialName, "!");
163
164 // write element
3716fae8 165 fOutFile << fBasicIndention
166 << element1
4032dc03 167 << G4std::setw(7) << G4std::setprecision(2) << x1*2. << " "
168 << G4std::setw(7) << G4std::setprecision(2) << x2*2. << " "
169 << G4std::setw(7) << G4std::setprecision(2) << y1*2. << " "
170 << G4std::setw(7) << G4std::setprecision(2) << y2*2. << " "
171 << G4std::setw(7) << G4std::setprecision(2) << hz*2.
240601e9 172 << element2
173 << G4endl;
174}
175
176
4032dc03 177void TG4XMLConvertor::WriteTrap(G4String lvName, const G4Trap* trap,
178 G4String materialName)
179{
180// Writes G4Trap solid.
181// ---
182
183 // get parameters
184 G4double dz = trap->GetZHalfLength()/TG3Units::Length();
185 G4ThreeVector symAxis = trap->GetSymAxis();
186 G4double y1 = trap->GetYHalfLength1()/TG3Units::Length();
187 G4double x1 = trap->GetXHalfLength1()/TG3Units::Length();
188 G4double x2 = trap->GetXHalfLength2()/TG3Units::Length();
189 G4double tanAlpha1 = trap->GetTanAlpha1();
190 G4double y2 = trap->GetYHalfLength2()/TG3Units::Length();
191 G4double x3 = trap->GetXHalfLength3()/TG3Units::Length();
192 G4double x4 = trap->GetXHalfLength4()/TG3Units::Length();
193 G4double tanAlpha2 = trap->GetTanAlpha2();
194
195 // ordering of parameters in XML element
196 // Xmumdpupd_Ymp_Z: 2x2 2x1 2x4 2x3 2y2 2y1 2dz
197 // inclination: atan(symAxis.x/symAxis.z), atan(symAxis.y/symAxis.z)
198 // declination: alpha1, alpha2
199
200 // get angles
201 G4double inc1 = atan(symAxis.x()/symAxis.z()) / deg;
202 G4double inc2 = atan(symAxis.y()/symAxis.z()) / deg;
203 G4double alpha1 = atan(tanAlpha1) / deg;
204 G4double alpha2 = atan(tanAlpha2) / deg;
205
206 // compose element string template
207 G4String element1
208 = "<trap name=\"########### material=\"!!!!!!!!!!!!!!!!!!!!! Xmumdpupd_Ymp_Z=\"";
209 G4String element2
210 = " inclination=\"";
211 G4String element3 = " declination=\"";
212 G4String element4 = "\" />";
213 G4String quota = "\"";
214
215 // put solid and material names
216 // put solid and material names
217 PutName(element1, lvName, "#");
218 PutName(element1, materialName, "!");
219
220 // write element
221 fOutFile << fBasicIndention
222 << element1
223 << G4std::setw(7) << G4std::setprecision(2) << x2*2. << " "
224 << G4std::setw(7) << G4std::setprecision(2) << x1*2. << " "
225 << G4std::setw(7) << G4std::setprecision(2) << x4*2. << " "
226 << G4std::setw(7) << G4std::setprecision(2) << x3*2. << " "
227 << G4std::setw(7) << G4std::setprecision(2) << y2*2. << " "
228 << G4std::setw(7) << G4std::setprecision(2) << y1*2. << " "
229 << G4std::setw(7) << G4std::setprecision(2) << dz*2. << quota << G4endl
230 << fBasicIndention
231 << element2
232 << G4std::setw(7) << G4std::setprecision(2) << inc1 << " "
233 << G4std::setw(7) << G4std::setprecision(2) << inc2 << quota
234 << element3
235 << G4std::setw(7) << G4std::setprecision(2) << alpha1 << " "
236 << G4std::setw(7) << G4std::setprecision(2) << alpha2
237 << element4
238 << G4endl;
239}
240
240601e9 241// public methods
242
3716fae8 243void TG4XMLConvertor::OpenMaterials(const G4String& version,
244 const G4String& date, const G4String& author,
245 const G4String dtdVersion)
246{
247// Writes section opening.
248// ---
249
250 G4String element1 = "<materials version = \"";
251 G4String element2 = " date = \"";
252 G4String element3 = " author = \"";
253 G4String element4 = " DTD_version=\"";
254 G4String element5 = " >";
255 G4String quota = "\"";
256
257 // write element
258 fOutFile << element1 << version << quota << G4endl
259 << element2 << date << quota << G4endl
260 << element3 << author << quota << G4endl
261 << element4 << dtdVersion << quota
262 << element5 << G4endl;
263}
264
240601e9 265void TG4XMLConvertor::OpenSection(const G4String& name, const G4String& version,
266 const G4String& date, const G4String& author,
267 const G4String& topVolume)
268{
269// Writes section opening.
270// ---
271
272 G4String element1 = "<section name = \"";
273 G4String element2 = " version = \"";
274 G4String element3 = " date = \"";
275 G4String element4 = " author = \"";
276 G4String element5 = " topVolume = \"";
277 G4String element6 = " >";
278 G4String quota = "\"";
279
280 // write element
281 fOutFile << element1 << name << quota << G4endl
282 << element2 << version << quota << G4endl
283 << element3 << date << quota << G4endl
284 << element4 << author << quota << G4endl
285 << element5 << topVolume << quota
286 << element6 << G4endl;
287}
288
289void TG4XMLConvertor::OpenComposition(const G4String& name)
290{
291// Writes composition opening.
292// ---
293
294 G4String element = "<composition name=\"";
295 element.append(name);
4032dc03 296 element.append("\">");
240601e9 297
298 // write element
299 fOutFile << fIndention
300 << element
301 << G4endl;
302
303 // increase indention
3716fae8 304 IncreaseIndention();
305}
306
307void TG4XMLConvertor::CloseMaterials()
308{
309// Writes materials closing.
310// ---
311
312 // define element
313 G4String element = "</materials>";
314
315 // write element
316 fOutFile << element
317 << G4endl;
240601e9 318}
319
320void TG4XMLConvertor::CloseSection()
321{
322// Writes section closing.
323// ---
324
325 // define element
326 G4String element = "</section>";
327
328 // write element
329 fOutFile << element
330 << G4endl;
331}
332
333void TG4XMLConvertor::CloseComposition()
334{
335// Writes composition closing.
336// ---
337
338 // decrease indention
3716fae8 339 DecreaseIndention();
240601e9 340
341 // define element
342 G4String element = "</composition>";
343
344 // write element
345 fOutFile << fIndention
346 << element
347 << G4endl;
348}
349
350void TG4XMLConvertor::WriteMaterial(const G4Material* material)
351{
352// Writes G4Material.
353// Not yet implemented, only XML comment element is written.
354// ---
355
356 G4String name = material->GetName();
357 CutName(name);
358
240601e9 359 // only comment line
360 G4String element1 = "<!-- material = \"";
361 G4String element2 = "\" -->";
362
363 // write element
3716fae8 364 fOutFile << fBasicIndention
365 << element1 << name
240601e9 366 << element2
367 << G4endl;
368}
369
4032dc03 370void TG4XMLConvertor::WriteSolid(G4String lvName, const G4VSolid* solid,
371 G4String materialName)
240601e9 372{
373// Finds G4Solid concrete type and calls writing function.
374// For not yet implemented solids, only XML comment element is written.
375// ---
376
4032dc03 377 // to be removed when materials are supported
378 materialName = "Hydrogen";
379
240601e9 380 const G4Box* box = dynamic_cast<const G4Box*>(solid);
381 if (box) {
4032dc03 382 WriteBox(lvName, box, materialName);
240601e9 383 return;
384 }
385
386 const G4Tubs* tubs = dynamic_cast<const G4Tubs*>(solid);
387 if (tubs) {
4032dc03 388 WriteTubs(lvName, tubs, materialName);
240601e9 389 return;
390 }
391
392 const G4Trd* trd = dynamic_cast<const G4Trd*>(solid);
393 if (trd) {
4032dc03 394 WriteTrd(lvName, trd, materialName);
395 return;
396 }
397
398 const G4Trap* trap = dynamic_cast<const G4Trap*>(solid);
399 if (trap) {
400 WriteTrap(lvName, trap, materialName);
240601e9 401 return;
402 }
403
404 // write comment line in case of unsupported
405 // shape
406
407 // only comment line
408 G4String element1 = "<!-- unsupported shape name= \"";
409 G4String element2 = "\" -->";
410
411 // write element
3716fae8 412 fOutFile << fBasicIndention
4032dc03 413 << element1 << lvName
240601e9 414 << element2
415 << G4endl;
416}
417
418void TG4XMLConvertor::WriteRotation(const G4RotationMatrix* rotation)
419{
420// Writes G4RotationMatrix.
421// Not yet implemented, only XML comment element is written.
422// ---
423
3716fae8 424 // return if this rotation was already written
425 G4int nofRotations = fRotations.size();
426 if (nofRotations>0)
427 for (G4int i=0; i<nofRotations; i++)
428 if (fRotations[i] == rotation) return;
429
430 fRotations.push_back(rotation);
431
4032dc03 432
3716fae8 433 // get parameters
434 G4double xx = rotation->xx();
435 G4double xy = rotation->xy();
436 G4double xz = rotation->xz();
437 G4double yx = rotation->yx();
438 G4double yy = rotation->yy();
439 G4double yz = rotation->yz();
440 G4double zx = rotation->zx();
441 G4double zy = rotation->zy();
442 G4double zz = rotation->zz();
443 G4String id = "RM";
4032dc03 444 TG4Globals::AppendNumberToString(id, ++fRotationCounter);
3716fae8 445
446 // compose element string template
447 G4String quota = "\"\n";
448 G4String element1 = "<rot_matrix id=\"####### XX_XY_XZ=\"";
449 G4String element2 = " YX_YY_YZ=\"";
450 G4String element3 = " ZX_ZY_ZZ=\"";
451 G4String element4 = "\" />";
240601e9 452
3716fae8 453 // put identifier
454 PutName(element1, id, "#");
455
240601e9 456 // write element
3716fae8 457 fOutFile << fBasicIndention
458 << element1
459 << G4std::setw(8) << G4std::setprecision(5) << xx << " "
460 << G4std::setw(8) << G4std::setprecision(5) << xy << " "
461 << G4std::setw(8) << G4std::setprecision(5) << xz << quota
462 << fBasicIndention
463 << element2
464 << G4std::setw(8) << G4std::setprecision(5) << yx << " "
465 << G4std::setw(8) << G4std::setprecision(5) << yy << " "
466 << G4std::setw(8) << G4std::setprecision(5) << yz << quota
467 << fBasicIndention
468 << element3
469 << G4std::setw(8) << G4std::setprecision(5) << zx << " "
470 << G4std::setw(8) << G4std::setprecision(5) << zy << " "
471 << G4std::setw(8) << G4std::setprecision(5) << zz
472 << element4
240601e9 473 << G4endl;
474}
475
4032dc03 476void TG4XMLConvertor::WritePosition(G4String lvName, G4ThreeVector position)
240601e9 477{
478// Writes position without rotation with a given solid name.
479// ---
480
481 // get parameters
482 G4double x = position.x()/TG3Units::Length();
483 G4double y = position.y()/TG3Units::Length();
484 G4double z = position.z()/TG3Units::Length();
485
486 // compose element string template
3716fae8 487 G4String element1 = "<posXYZ volume=\"########### X_Y_Z=\"";
240601e9 488 G4String element2 = "\" />";
489
490 // put solid name
4032dc03 491 PutName(element1, lvName, "#");
240601e9 492
493 // write element
494 fOutFile << fIndention
495 << element1
3716fae8 496 << G4std::setw(8) << G4std::setprecision(2) << x << " "
497 << G4std::setw(8) << G4std::setprecision(2) << y << " "
498 << G4std::setw(8) << G4std::setprecision(2) << z
240601e9 499 << element2
500 << G4endl;
501}
502
503void TG4XMLConvertor::WritePositionWithRotation(
4032dc03 504 G4String lvName, G4ThreeVector position,
240601e9 505 const G4RotationMatrix* rotation)
506{
507// Writes position with rotation with a given solid name.
508// Not yet implemented, only XML comment element is written.
509// ---
510
3716fae8 511 // get parameters
512 G4double x = position.x()/TG3Units::Length();
513 G4double y = position.y()/TG3Units::Length();
514 G4double z = position.z()/TG3Units::Length();
4032dc03 515 G4double xx = rotation->xx();
516 G4double xy = rotation->xy();
517 G4double xz = rotation->xz();
518 G4double yx = rotation->yx();
519 G4double yy = rotation->yy();
520 G4double yz = rotation->yz();
521 G4double zx = rotation->zx();
522 G4double zy = rotation->zy();
523 G4double zz = rotation->zz();
3716fae8 524
4032dc03 525/*
3716fae8 526 // find rotation
527 G4int i=0;
528 while (i<fRotations.size() && fRotations[i] != rotation) i++;
529 if (i==fRotations.size()) {
530 G4String text = "TG4XMLConvertor::WritePositionWithRotation: ";
531 text = text + " Unknown rotation - fatal error.";
532 TG4Globals::Exception(text);
533 }
534 G4String id = "RM";
535 TG4Globals::AppendNumberToString(id, i);
4032dc03 536*/
3716fae8 537
538 // compose element string template
4032dc03 539 G4String quota = "\"\n";
540 G4String element1 = "<transform volume=\"########### pos=\"";
541 G4String element2 = " rot=\"";
542 G4String element3 = " ";
543 G4String element4 = "\" />";
3716fae8 544
545 // put solid name
4032dc03 546 PutName(element1, lvName, "#");
240601e9 547
548 // write element
549 fOutFile << fIndention
3716fae8 550 << element1
551 << G4std::setw(8) << G4std::setprecision(2) << x << " "
552 << G4std::setw(8) << G4std::setprecision(2) << y << " "
4032dc03 553 << G4std::setw(8) << G4std::setprecision(2) << z << quota
554 << fIndention
555 << element2
556 << G4std::setw(8) << G4std::setprecision(5) << xx << " "
557 << G4std::setw(8) << G4std::setprecision(5) << xy << " "
558 << G4std::setw(8) << G4std::setprecision(5) << xz << G4endl
559 << fIndention
560 << element3
561 << G4std::setw(8) << G4std::setprecision(5) << yx << " "
562 << G4std::setw(8) << G4std::setprecision(5) << yy << " "
563 << G4std::setw(8) << G4std::setprecision(5) << yz << G4endl
564 << fIndention
565 << element3
566 << G4std::setw(8) << G4std::setprecision(5) << zx << " "
567 << G4std::setw(8) << G4std::setprecision(5) << zy << " "
568 << G4std::setw(8) << G4std::setprecision(5) << zz
569 << element4
3716fae8 570 << G4endl;
240601e9 571}
572
573void TG4XMLConvertor::WriteEmptyLine()
574{
575// Writes empty line.
576// ---
577
578 fOutFile << G4endl;
579}
580
3716fae8 581void TG4XMLConvertor::IncreaseIndention()
582{
583 // increase indention
584 fIndention.append(fBasicIndention);
585}
240601e9 586
3716fae8 587void TG4XMLConvertor::DecreaseIndention()
588{
589 // decrease indention
590 fIndention.replace(fIndention.find(fBasicIndention), 3 , "");
591}