Geant4  10.03.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4STRead.cc
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 // $Id: G4STRead.cc 68053 2013-03-13 14:39:51Z gcosmo $
27 //
28 // class G4STRead Implementation
29 //
30 // History:
31 // - Created. Zoltan Torzsok, November 2007
32 // -------------------------------------------------------------------------
33 
34 #include <fstream>
35 
36 #include "G4STRead.hh"
37 
38 #include "G4Material.hh"
39 #include "G4Box.hh"
40 #include "G4QuadrangularFacet.hh"
41 #include "G4TriangularFacet.hh"
42 #include "G4TessellatedSolid.hh"
43 #include "G4LogicalVolume.hh"
44 #include "G4PVPlacement.hh"
45 #include "G4AffineTransform.hh"
46 #include "G4VoxelLimits.hh"
47 
48 void G4STRead::TessellatedRead(const std::string& line)
49 {
50  if (tessellatedList.size()>0)
51  {
52  tessellatedList.back()->SetSolidClosed(true);
53  // Finish the previous solid at first!
54  }
55 
56  std::istringstream stream(line.substr(2));
57 
58  G4String name;
59  stream >> name;
60 
61  G4TessellatedSolid* tessellated = new G4TessellatedSolid(name);
62  volumeMap[tessellated] =
63  new G4LogicalVolume(tessellated, solid_material, name+"_LV" , 0, 0, 0);
64  tessellatedList.push_back(tessellated);
65 
66  G4cout << "G4STRead: Reading solid: " << name << G4endl;
67 }
68 
69 void G4STRead::FacetRead(const std::string& line)
70 {
71  if (tessellatedList.size()==0)
72  {
73  G4Exception("G4STRead::FacetRead()", "ReadError", FatalException,
74  "A solid must be defined before defining a facet!");
75  }
76 
77  if (line[2]=='3') // Triangular facet
78  {
79  G4double x1,y1,z1;
80  G4double x2,y2,z2;
81  G4double x3,y3,z3;
82 
83  std::istringstream stream(line.substr(4));
84  stream >> x1 >> y1 >> z1 >> x2 >> y2 >> z2 >> x3 >> y3 >> z3;
85  tessellatedList.back()->
86  AddFacet(new G4TriangularFacet(G4ThreeVector(x1,y1,z1),
87  G4ThreeVector(x2,y2,z2),
88  G4ThreeVector(x3,y3,z3), ABSOLUTE));
89  }
90  else if (line[2]=='4') // Quadrangular facet
91  {
92  G4double x1,y1,z1;
93  G4double x2,y2,z2;
94  G4double x3,y3,z3;
95  G4double x4,y4,z4;
96 
97  std::istringstream stream(line.substr(4));
98  stream >> x1 >> y1 >> z1 >> x2 >> y2 >> z2
99  >> x3 >> y3 >> z3 >> x4 >> y4 >> z4;
100  tessellatedList.back()->
101  AddFacet(new G4QuadrangularFacet(G4ThreeVector(x1,y1,z1),
102  G4ThreeVector(x2,y2,z2),
103  G4ThreeVector(x3,y3,z3),
104  G4ThreeVector(x4,y4,z4), ABSOLUTE));
105  }
106  else
107  {
108  G4Exception("G4STRead::FacetRead()", "ReadError", FatalException,
109  "Number of vertices per facet should be either 3 or 4!");
110  }
111 }
112 
113 void G4STRead::PhysvolRead(const std::string& line)
114 {
115  G4int level;
116  G4String name;
117  G4double r1,r2,r3;
118  G4double r4,r5,r6;
119  G4double r7,r8,r9;
120  G4double pX,pY,pZ;
121  G4double n1,n2,n3,n4,n5;
122 
123  std::istringstream stream(line.substr(2));
124  stream >> level >> name >> r1 >> r2 >> r3 >> n1 >> r4 >> r5 >> r6
125  >> n2 >> r7 >> r8 >> r9 >> n3 >> pX >> pY >> pZ >> n4 >> n5;
126  std::string::size_type idx = name.rfind("_");
127  if (idx!=std::string::npos)
128  {
129  name.resize(idx);
130  }
131  else
132  {
133  G4Exception("G4STRead::PhysvolRead()", "ReadError",
134  FatalException, "Invalid input stream!");
135  return;
136  }
137 
138  G4cout << "G4STRead: Placing tessellated solid: " << name << G4endl;
139 
140  G4TessellatedSolid* tessellated = 0;
141 
142  for (size_t i=0; i<tessellatedList.size(); i++)
143  { // Find the volume for this physvol!
144  if (tessellatedList[i]->GetName() == G4String(name))
145  {
146  tessellated = tessellatedList[i];
147  break;
148  }
149  }
150 
151  if (tessellated == 0)
152  {
153  G4String error_msg = "Referenced solid '" + name + "' not found!";
154  G4Exception("G4STRead::PhysvolRead()", "ReadError",
155  FatalException, error_msg);
156  }
157  if (volumeMap.find(tessellated) == volumeMap.end())
158  {
159  G4String error_msg = "Referenced solid '" + name
160  + "' is not associated with a logical volume!";
161  G4Exception("G4STRead::PhysvolRead()", "InvalidSetup",
162  FatalException, error_msg);
163  }
164  const G4RotationMatrix rot(G4ThreeVector(r1,r2,r3),
165  G4ThreeVector(r4,r5,r6),
166  G4ThreeVector(r7,r8,r9));
167  const G4ThreeVector pos(pX,pY,pZ);
168 
169  new G4PVPlacement(G4Transform3D(rot.inverse(),pos),
170  volumeMap[tessellated], name+"_PV", world_volume, 0, 0);
171  // Note: INVERSE of rotation is needed!!!
172 
173  G4double minx,miny,minz;
174  G4double maxx,maxy,maxz;
175  const G4VoxelLimits limits;
176 
177  tessellated->CalculateExtent(kXAxis,limits,
178  G4AffineTransform(rot,pos),minx,maxx);
179  tessellated->CalculateExtent(kYAxis,limits,
180  G4AffineTransform(rot,pos),miny,maxy);
181  tessellated->CalculateExtent(kZAxis,limits,
182  G4AffineTransform(rot,pos),minz,maxz);
183 
184  if (world_extent.x() < std::fabs(minx))
185  { world_extent.setX(std::fabs(minx)); }
186  if (world_extent.y() < std::fabs(miny))
187  { world_extent.setY(std::fabs(miny)); }
188  if (world_extent.z() < std::fabs(minz))
189  { world_extent.setZ(std::fabs(minz)); }
190  if (world_extent.x() < std::fabs(maxx))
191  { world_extent.setX(std::fabs(maxx)); }
192  if (world_extent.y() < std::fabs(maxy))
193  { world_extent.setY(std::fabs(maxy)); }
194  if (world_extent.z() < std::fabs(maxz))
195  { world_extent.setZ(std::fabs(maxz)); }
196 }
197 
198 void G4STRead::ReadGeom(const G4String& name)
199 {
200  G4cout << "G4STRead: Reading '" << name << "'..." << G4endl;
201 
202  std::ifstream GeomFile(name);
203 
204  if (!GeomFile)
205  {
206  G4String error_msg = "Cannot open file: " + name;
207  G4Exception("G4STRead::ReadGeom()", "ReadError",
208  FatalException, error_msg);
209  }
210 
211  tessellatedList.clear();
212  volumeMap.clear();
213  std::string line;
214 
215  while (getline(GeomFile,line))
216  {
217  if (line[0] == 'f') { TessellatedRead(line); } else
218  if (line[0] == 'p') { FacetRead(line); }
219  }
220 
221  if (tessellatedList.size()>0) // Finish the last solid!
222  {
223  tessellatedList.back()->SetSolidClosed(true);
224  }
225 
226  G4cout << "G4STRead: Reading '" << name << "' done." << G4endl;
227 }
228 
229 void G4STRead::ReadTree(const G4String& name)
230 {
231  G4cout << "G4STRead: Reading '" << name << "'..." << G4endl;
232 
233  std::ifstream TreeFile(name);
234 
235  if (!TreeFile)
236  {
237  G4String error_msg = "Cannot open file: " + name;
238  G4Exception("G4STRead::ReadTree()", "ReadError",
239  FatalException, error_msg);
240  }
241 
242  std::string line;
243 
244  while (getline(TreeFile,line))
245  {
246  if (line[0] == 'g') { PhysvolRead(line); }
247  }
248 
249  G4cout << "G4STRead: Reading '" << name << "' done." << G4endl;
250 }
251 
253 G4STRead::Read(const G4String& name, G4Material* mediumMaterial,
254  G4Material* solidMaterial)
255 {
256  if (mediumMaterial == 0)
257  {
258  G4Exception("G4STRead::Read()", "InvalidSetup", FatalException,
259  "Pointer to medium material is not valid!");
260  }
261  if (solidMaterial == 0)
262  {
263  G4Exception("G4STRead::Read()", "InvalidSetup", FatalException,
264  "Pointer to solid material is not valid!");
265  }
266 
267  solid_material = solidMaterial;
268 
269  world_box = new G4Box("TessellatedWorldBox",kInfinity,kInfinity,kInfinity);
270  // We don't know the extent of the world yet!
271  world_volume = new G4LogicalVolume(world_box, mediumMaterial,
272  "TessellatedWorldLV", 0, 0, 0);
273  world_extent = G4ThreeVector(0,0,0);
274 
275  ReadGeom(name+".geom");
276  ReadTree(name+".tree");
277 
278  // Now setting the world extent ...
279  //
280  if (world_box->GetXHalfLength() > world_extent.x())
281  { world_box->SetXHalfLength(world_extent.x()); }
282  if (world_box->GetYHalfLength() > world_extent.y())
283  { world_box->SetYHalfLength(world_extent.y()); }
284  if (world_box->GetZHalfLength() > world_extent.z())
285  { world_box->SetZHalfLength(world_extent.z()); }
286 
287  return world_volume;
288 }
const XML_Char * name
Definition: expat.h:151
G4double GetXHalfLength() const
void SetZHalfLength(G4double dz)
Definition: G4Box.cc:174
G4LogicalVolume * Read(const G4String &, G4Material *mediumMaterial, G4Material *solidMaterial)
Definition: G4STRead.cc:253
static const G4double kInfinity
Definition: geomdefs.hh:42
CLHEP::Hep3Vector G4ThreeVector
double x() const
Definition: G4Box.hh:64
tuple world_volume
Definition: write_gdml.py:36
int G4int
Definition: G4Types.hh:78
void setY(double)
G4double GetZHalfLength() const
double z() const
void setZ(double)
void setX(double)
G4GLOB_DLL std::ostream G4cout
G4double GetYHalfLength() const
HepGeom::Transform3D G4Transform3D
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
double y() const
void SetYHalfLength(G4double dy)
Definition: G4Box.cc:154
void SetXHalfLength(G4double dx)
Definition: G4Box.cc:134
#define G4endl
Definition: G4ios.hh:61
double G4double
Definition: G4Types.hh:76
virtual G4bool CalculateExtent(const EAxis pAxis, const G4VoxelLimits &pVoxelLimit, const G4AffineTransform &pTransform, G4double &pMin, G4double &pMax) const
static const G4double pos