]> git.uio.no Git - u/mrichter/AliRoot.git/blame - TGeant4/TG4XMLGeometryGenerator.cxx
added WriteReplica(), WriteCons/Polycone/Polyhedra(); changed layout of elements
[u/mrichter/AliRoot.git] / TGeant4 / TG4XMLGeometryGenerator.cxx
CommitLineData
240601e9 1// $Id$
2// Category: geometry
3// by I. Hrivnacova, 27.07.2000
4//
5// See the class description in the header file.
6
7#include "TG4XMLGeometryGenerator.h"
8#include "TG4XMLConvertor.h"
9#include "TG4Globals.h"
10
11#include <G4Material.hh>
12#include <G4VSolid.hh>
13#include <G4LogicalVolume.hh>
14#include <G4VPhysicalVolume.hh>
15#include <G4PVPlacement.hh>
16#include <G4ThreeVector.hh>
17#include <G4RotationMatrix.hh>
18
19#include <g4std/iomanip>
20#include <g4std/vector>
21
22TG4XMLGeometryGenerator* TG4XMLGeometryGenerator::fgInstance = 0;
23
24TG4XMLGeometryGenerator::TG4XMLGeometryGenerator()
25{
26//
27 if (fgInstance) {
28 TG4Globals::Exception(
29 "TG4XMLGeometryGenerator: attempt to create two instances of singleton.");
30 }
31
32 fConvertor = new TG4XMLConvertor(fOutFile);
33}
34
35TG4XMLGeometryGenerator::TG4XMLGeometryGenerator(const TG4XMLGeometryGenerator& right)
36{
37//
38 TG4Globals::Exception(
39 "TG4XMLGeometryGenerator: attempt to copy singleton.");
40}
41
42
43TG4XMLGeometryGenerator::~TG4XMLGeometryGenerator() {
44//
45}
46
47// operators
48
49TG4XMLGeometryGenerator&
50TG4XMLGeometryGenerator::operator=(const TG4XMLGeometryGenerator& right)
51{
52 // check assignement to self
53 if (this == &right) return *this;
54
55 TG4Globals::Exception(
56 "Attempt to assign TG4XMLGeometryGenerator singleton.");
57
58 return *this;
59}
60
61
62// private methods
63
4032dc03 64void TG4XMLGeometryGenerator::CutName(G4String& name) const
65{
66// Removes spaces after the name if present.
67// ---
68
69 G4int i = name.length();
70 while (name(--i) == ' ') name = name(0,i);
71}
72
240601e9 73void TG4XMLGeometryGenerator::ProcessSolids(G4LogicalVolume* lv)
74{
75// Writes all solids of given logical volume.
76// ---
77
78 G4VSolid* solid = lv->GetSolid();
4032dc03 79 G4String lvName = lv->GetName();
240601e9 80 G4String material = lv->GetMaterial()->GetName();
4032dc03 81 fConvertor->WriteSolid(lvName, solid, material);
240601e9 82
4032dc03 83 // store the name of logical volume in the set
84 fVolumeNames.insert(fVolumeNames.begin(), lvName);
85
86 // process daughters
240601e9 87 G4int nofDaughters = lv->GetNoDaughters();
88 if (nofDaughters>0)
89 for (G4int i=0; i<nofDaughters; i++) {
4032dc03 90 //G4cout << "processing " << i << "th daughter of "
91 // << lv->GetName() << G4endl;
240601e9 92 G4LogicalVolume* lvd = lv->GetDaughter(i)->GetLogicalVolume();
4032dc03 93 G4String lvdName = lvd->GetName();
94
95 if (fVolumeNames.find(lvdName) == fVolumeNames.end()) {
96 // process lvd only if it was not yet processed
97 ProcessSolids(lvd);
98 }
240601e9 99 }
100}
101
102void TG4XMLGeometryGenerator::ProcessMaterials(G4LogicalVolume* lv)
103{
104// Writes all materials of given logical volume.
105// ---
106
107 G4Material* material = lv->GetMaterial();
240601e9 108
4032dc03 109 // check if this material was already written
110 G4bool written = false;
111 G4String name = material->GetName();
112 CutName(name);
113 if (fMaterialNames.find(name) != fMaterialNames.end()) written = true;
114
115 if (!written) {
116 fConvertor->WriteMaterial(material);
117 fMaterialNames.insert(fMaterialNames.begin(), name);
118 }
119
120 // store the name of logical volume in the set
121 G4String lvName = lv->GetName();
122 fVolumeNames.insert(fVolumeNames.begin(), lvName);
123
240601e9 124 G4int nofDaughters = lv->GetNoDaughters();
125 if (nofDaughters>0)
126 for (G4int i=0; i<nofDaughters; i++) {
127 G4LogicalVolume* lvd = lv->GetDaughter(i)->GetLogicalVolume();
4032dc03 128 G4String lvdName = lvd->GetName();
129
130 if (fVolumeNames.find(lvdName) == fVolumeNames.end()) {
131 // process lvd only if it was not yet processed
132 ProcessMaterials(lvd);
133 }
240601e9 134 }
135}
136
9d37cb3b 137void TG4XMLGeometryGenerator::ProcessRotations(G4LogicalVolume* lv)
138{
139// Writes all rotation matrices of given logical volume.
140// ---
141
4032dc03 142 G4String lvName = lv->GetName();
143
144 // store the name of logical volume in the set
145 fVolumeNames.insert(fVolumeNames.begin(), lvName);
146
9d37cb3b 147 G4int nofDaughters = lv->GetNoDaughters();
4032dc03 148
149 if (nofDaughters>0) {
150 G4int i;
151 for (i=0; i<nofDaughters; i++) {
152
9d37cb3b 153 G4VPhysicalVolume* pvd = lv->GetDaughter(i);
154 const G4RotationMatrix* kRotation = pvd->GetRotation();
4032dc03 155 if (kRotation)
156 fConvertor->WriteRotation(kRotation);
157
9d37cb3b 158 G4LogicalVolume* lvd = pvd->GetLogicalVolume();
4032dc03 159 G4String lvdName = lvd->GetName();
160
161 if (fVolumeNames.find(lvdName) == fVolumeNames.end()) {
162 // process lvd only if it was not yet processed
163 ProcessRotations(lvd);
164 }
9d37cb3b 165 }
4032dc03 166 }
9d37cb3b 167}
168
240601e9 169void TG4XMLGeometryGenerator::ProcessLogicalVolume(G4LogicalVolume* lv)
170{
171// Writes logical volume tree.
172// ---
173
174 G4int nofDaughters = lv->GetNoDaughters();
175 if (nofDaughters == 0) return;
176
9d37cb3b 177 // open composition
4032dc03 178 G4String lvName = lv->GetName();
179 G4String name = lvName;
9d37cb3b 180 name.append("_comp");
181 fConvertor->OpenComposition(name);
4032dc03 182
183 if (lvName == "FTOC" )
184 G4cout << "FTOC daughters: " << nofDaughters;
185
9d37cb3b 186 // write positions
c7049a8c 187 G4int i;
188 for (i=0; i<nofDaughters; i++) {
4032dc03 189 // G4cout << "processing " << i << "th daughter of "
190 // << lv->GetName() << G4endl;
191
9d37cb3b 192 G4VPhysicalVolume* vpvd = lv->GetDaughter(i);
193 G4LogicalVolume* lvd = vpvd->GetLogicalVolume();
194
4032dc03 195 if (lvName == "FTOC" )
196 G4cout << vpvd << " " << lvd << "; ";
197
198
9d37cb3b 199 // only placements are processed
200 G4PVPlacement* pvd = dynamic_cast<G4PVPlacement*>(vpvd);
201 if (pvd) {
4032dc03 202 G4String lvName = lvd->GetName();
9d37cb3b 203 G4String compName = lvd->GetName();
204 compName.append("_comp");
205 G4int nd = lvd->GetNoDaughters();
4032dc03 206 G4ThreeVector position = vpvd->GetTranslation();
207 const G4RotationMatrix* kMatrix = vpvd->GetRotation();
9d37cb3b 208
209 if (!kMatrix) {
4032dc03 210 fConvertor->WritePosition(lvName, position);
9d37cb3b 211 // if volume is not leaf node place its logical volume
212 if (nd>0)
213 fConvertor->WritePosition(compName, position);
214 }
215 else {
4032dc03 216 fConvertor->WritePositionWithRotation(lvName, position, kMatrix);
9d37cb3b 217 if (nd>0)
218 fConvertor->WritePositionWithRotation(compName, position, kMatrix);
219 }
220
221 }
222 else {
223 G4String text = "TG4XMLGeometryGenerator::ProcessLogicalVolume: \n";
224 text = text + " Limitation: \n";
225 text = text + " Other physical volumes than PVPlacement";
226 text = text + " are not implemented.";
227 TG4Globals::Warning(text);
228 }
229 }
230
4032dc03 231 if (lvName == "FTOC" )
232 G4cout << G4endl;
233
9d37cb3b 234 // close composition
235 fConvertor->CloseComposition();
236 fConvertor->WriteEmptyLine();
237
4032dc03 238 // store the name of logical volume in the set
239 fVolumeNames.insert(fVolumeNames.begin(), lvName);
240
241 // process daughters
c7049a8c 242 for (i=0; i<nofDaughters; i++) {
9d37cb3b 243 G4LogicalVolume* lvd = lv->GetDaughter(i)->GetLogicalVolume();
4032dc03 244 G4String lvdName = lvd->GetName();
245
246 if (lvdName == "FTOC" )
247 G4cout << "*****************Daughter FTOC:" << lvd << " in " << lvName << G4endl;
248
249 if (fVolumeNames.find(lvdName) == fVolumeNames.end()) {
250 // process lvd only if it was not yet processed
251 ProcessLogicalVolume(lvd);
252 }
253 }
9d37cb3b 254}
255
4032dc03 256void TG4XMLGeometryGenerator::ClearMaterialNames()
9d37cb3b 257{
4032dc03 258// Clears the set of material names.
9d37cb3b 259// ---
240601e9 260
4032dc03 261 fMaterialNames.erase(fMaterialNames.begin(), fMaterialNames.end());
262}
263
264void TG4XMLGeometryGenerator::ClearVolumeNames()
265{
266// Clears the set of volume names.
267// ---
268
269 fVolumeNames.erase(fVolumeNames.begin(), fVolumeNames.end());
240601e9 270}
271
272// public methods
273
9d37cb3b 274void TG4XMLGeometryGenerator::GenerateMaterials(
275 const G4String& version, const G4String& date,
276 const G4String& author, const G4String dtdVersion,
277 G4LogicalVolume* lv)
278{
279// Generates the XML material element containing
280// all materials present in given logical volume.
281// ---
282
283 // create section
284 fConvertor->OpenMaterials(version, date, author, dtdVersion);
285 fConvertor->WriteEmptyLine();
286
287 // process materials
288 ProcessMaterials(lv);
289 fConvertor->WriteEmptyLine();
4032dc03 290 ClearMaterialNames();
291 ClearVolumeNames();
9d37cb3b 292
293 // close section
294 fConvertor->CloseMaterials();
295 fConvertor->WriteEmptyLine();
296}
297
240601e9 298void TG4XMLGeometryGenerator::GenerateSection(const G4String& name,
299 const G4String& version, const G4String& date,
300 const G4String& author, const G4String& topVolume,
301 G4LogicalVolume* lv)
302{
9d37cb3b 303// Generates the XML section element containing
240601e9 304// all geometry objects defined in given logical volume:
9d37cb3b 305// rotation matrices, solids and volumes hierarchy.
240601e9 306// ---
307
308 // create section
309 fConvertor->OpenSection(name, version, date, author, topVolume);
310 fConvertor->WriteEmptyLine();
311
9d37cb3b 312 // process rotations
4032dc03 313 //ProcessRotations(lv);
314 //fConvertor->WriteEmptyLine();
315 //ClearRotations();
316 //ClearVolumeNames();
9d37cb3b 317
240601e9 318 // process solids
319 ProcessSolids(lv);
320 fConvertor->WriteEmptyLine();
4032dc03 321 ClearVolumeNames();
240601e9 322
323 // process geometry tree
240601e9 324 ProcessLogicalVolume(lv);
240601e9 325 fConvertor->WriteEmptyLine();
4032dc03 326 ClearVolumeNames();
240601e9 327
328 // close section
329 fConvertor->CloseSection();
330}
331
332void TG4XMLGeometryGenerator::OpenFile(G4String filePath)
333{
334// Opens output file.
335// ---
336
337 G4cout << "TG4XMLGeometryGenerator::OpenFile: " << filePath << G4endl;
338
339 fOutFile.open(filePath, G4std::ios::out);
340
341 if (!fOutFile) {
342 G4String text = "Cannot open ";
343 text = text + filePath;
344 TG4Globals::Warning(text);
345 }
346
347 // use FORTRAN compatibility output
348 fOutFile.setf(G4std::ios::fixed, G4std::ios::floatfield);
349}
350
351
352void TG4XMLGeometryGenerator::CloseFile()
353{
354// Closes output file.
355// ---
356
357 fOutFile.close();
358}