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