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