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