Geant4  v4-10.4-release
 모두 클래스 네임스페이스들 파일들 함수 변수 타입정의 열거형 타입 열거형 멤버 Friends 매크로 그룹들 페이지들
G4STRead.cc
이 파일의 문서화 페이지로 가기
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 108895 2018-03-15 10:27:25Z 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 #ifdef G4VERBOSE
67  G4cout << "G4STRead: Reading solid: " << name << G4endl;
68 #endif
69 }
70 
71 void G4STRead::FacetRead(const std::string& line)
72 {
73  if (tessellatedList.size()==0)
74  {
75  G4Exception("G4STRead::FacetRead()", "ReadError", FatalException,
76  "A solid must be defined before defining a facet!");
77  }
78 
79  if (line[2]=='3') // Triangular facet
80  {
81  G4double x1,y1,z1;
82  G4double x2,y2,z2;
83  G4double x3,y3,z3;
84 
85  std::istringstream stream(line.substr(4));
86  stream >> x1 >> y1 >> z1 >> x2 >> y2 >> z2 >> x3 >> y3 >> z3;
87  tessellatedList.back()->
88  AddFacet(new G4TriangularFacet(G4ThreeVector(x1,y1,z1),
89  G4ThreeVector(x2,y2,z2),
90  G4ThreeVector(x3,y3,z3), ABSOLUTE));
91  }
92  else if (line[2]=='4') // Quadrangular facet
93  {
94  G4double x1,y1,z1;
95  G4double x2,y2,z2;
96  G4double x3,y3,z3;
97  G4double x4,y4,z4;
98 
99  std::istringstream stream(line.substr(4));
100  stream >> x1 >> y1 >> z1 >> x2 >> y2 >> z2
101  >> x3 >> y3 >> z3 >> x4 >> y4 >> z4;
102  tessellatedList.back()->
103  AddFacet(new G4QuadrangularFacet(G4ThreeVector(x1,y1,z1),
104  G4ThreeVector(x2,y2,z2),
105  G4ThreeVector(x3,y3,z3),
106  G4ThreeVector(x4,y4,z4), ABSOLUTE));
107  }
108  else
109  {
110  G4Exception("G4STRead::FacetRead()", "ReadError", FatalException,
111  "Number of vertices per facet should be either 3 or 4!");
112  }
113 }
114 
115 void G4STRead::PhysvolRead(const std::string& line)
116 {
117  G4int level;
118  G4String name;
119  G4double r1,r2,r3;
120  G4double r4,r5,r6;
121  G4double r7,r8,r9;
122  G4double pX,pY,pZ;
123  G4double n1,n2,n3,n4,n5;
124 
125  std::istringstream stream(line.substr(2));
126  stream >> level >> name >> r1 >> r2 >> r3 >> n1 >> r4 >> r5 >> r6
127  >> n2 >> r7 >> r8 >> r9 >> n3 >> pX >> pY >> pZ >> n4 >> n5;
128  std::string::size_type idx = name.rfind("_");
129  if (idx!=std::string::npos)
130  {
131  name.resize(idx);
132  }
133  else
134  {
135  G4Exception("G4STRead::PhysvolRead()", "ReadError",
136  FatalException, "Invalid input stream!");
137  return;
138  }
139 
140  G4cout << "G4STRead: Placing tessellated solid: " << name << G4endl;
141 
142  G4TessellatedSolid* tessellated = 0;
143 
144  for (size_t i=0; i<tessellatedList.size(); i++)
145  { // Find the volume for this physvol!
146  if (tessellatedList[i]->GetName() == G4String(name))
147  {
148  tessellated = tessellatedList[i];
149  break;
150  }
151  }
152 
153  if (tessellated == 0)
154  {
155  G4String error_msg = "Referenced solid '" + name + "' not found!";
156  G4Exception("G4STRead::PhysvolRead()", "ReadError",
157  FatalException, error_msg);
158  }
159  if (volumeMap.find(tessellated) == volumeMap.end())
160  {
161  G4String error_msg = "Referenced solid '" + name
162  + "' is not associated with a logical volume!";
163  G4Exception("G4STRead::PhysvolRead()", "InvalidSetup",
164  FatalException, error_msg);
165  }
166  const G4RotationMatrix rot(G4ThreeVector(r1,r2,r3),
167  G4ThreeVector(r4,r5,r6),
168  G4ThreeVector(r7,r8,r9));
169  const G4ThreeVector pos(pX,pY,pZ);
170 
172  volumeMap[tessellated], name+"_PV", world_volume, 0, 0);
173  // Note: INVERSE of rotation is needed!!!
174 
175  G4double minx,miny,minz;
176  G4double maxx,maxy,maxz;
177  const G4VoxelLimits limits;
178 
179  tessellated->CalculateExtent(kXAxis,limits,
180  G4AffineTransform(rot,pos),minx,maxx);
181  tessellated->CalculateExtent(kYAxis,limits,
182  G4AffineTransform(rot,pos),miny,maxy);
183  tessellated->CalculateExtent(kZAxis,limits,
184  G4AffineTransform(rot,pos),minz,maxz);
185 
186  if (world_extent.x() < std::fabs(minx))
187  { world_extent.setX(std::fabs(minx)); }
188  if (world_extent.y() < std::fabs(miny))
189  { world_extent.setY(std::fabs(miny)); }
190  if (world_extent.z() < std::fabs(minz))
191  { world_extent.setZ(std::fabs(minz)); }
192  if (world_extent.x() < std::fabs(maxx))
193  { world_extent.setX(std::fabs(maxx)); }
194  if (world_extent.y() < std::fabs(maxy))
195  { world_extent.setY(std::fabs(maxy)); }
196  if (world_extent.z() < std::fabs(maxz))
197  { world_extent.setZ(std::fabs(maxz)); }
198 }
199 
201 {
202 #ifdef G4VERBOSE
203  G4cout << "G4STRead: Reading '" << name << "'..." << G4endl;
204 #endif
205  std::ifstream GeomFile(name);
206 
207  if (!GeomFile)
208  {
209  G4String error_msg = "Cannot open file: " + name;
210  G4Exception("G4STRead::ReadGeom()", "ReadError",
211  FatalException, error_msg);
212  }
213 
214  tessellatedList.clear();
215  volumeMap.clear();
216  std::string line;
217 
218  while (getline(GeomFile,line))
219  {
220  if (line[0] == 'f') { TessellatedRead(line); } else
221  if (line[0] == 'p') { FacetRead(line); }
222  }
223 
224  if (tessellatedList.size()>0) // Finish the last solid!
225  {
226  tessellatedList.back()->SetSolidClosed(true);
227  }
228 
229  G4cout << "G4STRead: Reading '" << name << "' done." << G4endl;
230 }
231 
233 {
234 #ifdef G4VERBOSE
235  G4cout << "G4STRead: Reading '" << name << "'..." << G4endl;
236 #endif
237  std::ifstream TreeFile(name);
238 
239  if (!TreeFile)
240  {
241  G4String error_msg = "Cannot open file: " + name;
242  G4Exception("G4STRead::ReadTree()", "ReadError",
243  FatalException, error_msg);
244  }
245 
246  std::string line;
247 
248  while (getline(TreeFile,line))
249  {
250  if (line[0] == 'g') { PhysvolRead(line); }
251  }
252 
253  G4cout << "G4STRead: Reading '" << name << "' done." << G4endl;
254 }
255 
257 G4STRead::Read(const G4String& name, G4Material* mediumMaterial,
258  G4Material* solidMaterial)
259 {
260  if (mediumMaterial == 0)
261  {
262  G4Exception("G4STRead::Read()", "InvalidSetup", FatalException,
263  "Pointer to medium material is not valid!");
264  }
265  if (solidMaterial == 0)
266  {
267  G4Exception("G4STRead::Read()", "InvalidSetup", FatalException,
268  "Pointer to solid material is not valid!");
269  }
270 
271  solid_material = solidMaterial;
272 
273  world_box = new G4Box("TessellatedWorldBox",kInfinity,kInfinity,kInfinity);
274  // We don't know the extent of the world yet!
275  world_volume = new G4LogicalVolume(world_box, mediumMaterial,
276  "TessellatedWorldLV", 0, 0, 0);
277  world_extent = G4ThreeVector(0,0,0);
278 
279  ReadGeom(name+".geom");
280  ReadTree(name+".tree");
281 
282  // Now setting the world extent ...
283  //
290 
291  return world_volume;
292 }
void SetXHalfLength(G4double dx)
Definition: G4Box.cc:136
const XML_Char * name
Definition: expat.h:151
G4double GetYHalfLength() const
CLHEP::Hep3Vector G4ThreeVector
static const G4double pos
static const G4double kInfinity
Definition: geomdefs.hh:42
Float_t y1[n_points_granero]
Definition: compare.C:5
HepGeom::Transform3D G4Transform3D
Float_t x1[n_points_granero]
Definition: compare.C:5
#define G4endl
Definition: G4ios.hh:61
void SetZHalfLength(G4double dz)
Definition: G4Box.cc:183
double z() const
void setX(double)
Float_t y2[n_points_geant4]
Definition: compare.C:26
double G4double
Definition: G4Types.hh:76
void setZ(double)
G4LogicalVolume * Read(const G4String &, G4Material *mediumMaterial, G4Material *solidMaterial)
Definition: G4STRead.cc:257
G4Material * solid_material
Definition: G4STRead.hh:73
Definition: G4Box.hh:64
std::map< G4TessellatedSolid *, G4LogicalVolume * > volumeMap
Definition: G4STRead.hh:76
void TessellatedRead(const std::string &)
Definition: G4STRead.cc:48
void ReadTree(const G4String &)
Definition: G4STRead.cc:232
G4double GetXHalfLength() const
void SetYHalfLength(G4double dy)
Definition: G4Box.cc:160
std::vector< G4TessellatedSolid * > tessellatedList
Definition: G4STRead.hh:75
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.hh:65
int G4int
Definition: G4Types.hh:78
G4LogicalVolume * world_volume
Definition: G4STRead.hh:74
G4GLOB_DLL std::ostream G4cout
double x() const
G4Box * world_box
Definition: G4STRead.hh:71
void ReadGeom(const G4String &)
Definition: G4STRead.cc:200
double y() const
void FacetRead(const std::string &)
Definition: G4STRead.cc:71
Float_t x2[n_points_geant4]
Definition: compare.C:26
G4double GetZHalfLength() const
void PhysvolRead(const std::string &)
Definition: G4STRead.cc:115
HepRotation inverse() const
void setY(double)
G4ThreeVector world_extent
Definition: G4STRead.hh:72