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 | // |
---|
27 | // $Id: TEx01DetectorConstruction.cc,v 1.3 2007/05/18 10:36:48 gcosmo Exp $ |
---|
28 | // GEANT4 tag $Name: geant4-09-04-beta-cand-01 $ |
---|
29 | // |
---|
30 | //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... |
---|
31 | //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... |
---|
32 | |
---|
33 | #include "TEx01DetectorConstruction.hh" |
---|
34 | |
---|
35 | #include "G4Material.hh" |
---|
36 | #include "G4Box.hh" |
---|
37 | #include "G4LogicalVolume.hh" |
---|
38 | #include "G4PVPlacement.hh" |
---|
39 | #include "G4PVParameterised.hh" |
---|
40 | #include "G4SDManager.hh" |
---|
41 | |
---|
42 | #include "G4UserLimits.hh" |
---|
43 | |
---|
44 | #include "G4VisAttributes.hh" |
---|
45 | #include "G4Colour.hh" |
---|
46 | |
---|
47 | #include "G4Tokenizer.hh" |
---|
48 | |
---|
49 | #include "G4ios.hh" |
---|
50 | |
---|
51 | #include "G4TriangularFacet.hh" |
---|
52 | #include "G4VFacet.hh" |
---|
53 | |
---|
54 | #include <fstream> |
---|
55 | #include <sstream> |
---|
56 | |
---|
57 | //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... |
---|
58 | |
---|
59 | TEx01DetectorConstruction::TEx01DetectorConstruction() |
---|
60 | :solidWorld(0), logicWorld(0), physiWorld(0), |
---|
61 | solidTarget(0), logicTarget(0), physiTarget(0) |
---|
62 | {;} |
---|
63 | |
---|
64 | //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... |
---|
65 | |
---|
66 | TEx01DetectorConstruction::~TEx01DetectorConstruction() |
---|
67 | {;} |
---|
68 | |
---|
69 | //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... |
---|
70 | |
---|
71 | G4VPhysicalVolume* TEx01DetectorConstruction::Construct() |
---|
72 | { |
---|
73 | // |
---|
74 | //--------- Material definition --------- |
---|
75 | |
---|
76 | G4double a, z; |
---|
77 | G4double density; |
---|
78 | G4int nel; |
---|
79 | |
---|
80 | //Air |
---|
81 | G4Element* N = new G4Element("Nitrogen", "N", z=7., a= 14.01*g/mole); |
---|
82 | G4Element* O = new G4Element("Oxygen" , "O", z=8., a= 16.00*g/mole); |
---|
83 | |
---|
84 | G4Material* Air = new G4Material("Air", density= 1.29*mg/cm3, nel=2); |
---|
85 | Air->AddElement(N, 70*perCent); |
---|
86 | Air->AddElement(O, 30*perCent); |
---|
87 | |
---|
88 | //Lead |
---|
89 | G4Material* Pb = |
---|
90 | new G4Material("Lead", z=82., a= 207.19*g/mole, density= 11.35*g/cm3); |
---|
91 | |
---|
92 | // Print all the materials defined. |
---|
93 | // |
---|
94 | G4cout << G4endl << "The materials defined are : " << G4endl << G4endl; |
---|
95 | G4cout << *(G4Material::GetMaterialTable()) << G4endl; |
---|
96 | |
---|
97 | //------------------------------ |
---|
98 | // Tesselated Solid |
---|
99 | //------------------------------ |
---|
100 | |
---|
101 | G4String solidName, logicName, physiName; |
---|
102 | std::vector <G4TessellatedSolid*> solidStore; |
---|
103 | |
---|
104 | // |
---|
105 | // G4String geomFile ; |
---|
106 | |
---|
107 | // char val[100]; |
---|
108 | // std::ostrstream os(val,100); |
---|
109 | // os <<dirName <<"/z" <<Z <<".a" <<A <<'\0'; |
---|
110 | // G4String file(val); |
---|
111 | std::ifstream GeomFile("./test.geom"); |
---|
112 | //solidTarget = new G4Box("target",targetSize,targetSize,targetSize); |
---|
113 | |
---|
114 | if (!GeomFile){ |
---|
115 | G4cerr << "Geometry file not opened !!!" << G4endl; |
---|
116 | }else { |
---|
117 | G4cout << " Tessellated Geometry opened "<<G4endl; |
---|
118 | char inputChars[150]={' '}; |
---|
119 | G4String inputLine; |
---|
120 | G4String recordType(""); |
---|
121 | G4double a1,a2,a3,a4,a5,a6,a7,a8,a9; |
---|
122 | G4TriangularFacet *facet; |
---|
123 | G4bool firstSolid (true); |
---|
124 | // |
---|
125 | while (!GeomFile.getline(inputChars,150).eof()) { |
---|
126 | inputLine = inputChars; |
---|
127 | inputLine = inputLine.strip(1); |
---|
128 | // G4cout << inputLine << G4endl; |
---|
129 | if (inputChars[0] == 'f') { |
---|
130 | if (firstSolid) { |
---|
131 | firstSolid = false; |
---|
132 | }else { |
---|
133 | solidTarget->SetSolidClosed(true); |
---|
134 | solidStore.push_back(solidTarget); |
---|
135 | G4cout << solidName <<" has been created " <<G4endl; |
---|
136 | } |
---|
137 | std::istringstream tmpstream(inputLine); |
---|
138 | tmpstream >>recordType >>solidName ; |
---|
139 | solidTarget = new G4TessellatedSolid(solidName); |
---|
140 | } else if (inputChars[0] == 'p' ) { |
---|
141 | std::istringstream tmpstream(inputLine); |
---|
142 | tmpstream >>recordType >>a1 >> a1 >> a2 >> a3 >> a4 >> a5 >> a6 |
---|
143 | >> a7 >> a8 >> a9 ; |
---|
144 | facet = new |
---|
145 | G4TriangularFacet (G4ThreeVector(a1,a2,a3), |
---|
146 | G4ThreeVector(a4,a5,a6), |
---|
147 | G4ThreeVector(a7,a8,a9), |
---|
148 | ABSOLUTE); |
---|
149 | solidTarget->AddFacet((G4VFacet*) facet); |
---|
150 | } |
---|
151 | } |
---|
152 | // need to close and store the last solid |
---|
153 | solidTarget->SetSolidClosed(true); |
---|
154 | solidStore.push_back(solidTarget); |
---|
155 | G4cout << solidName <<" has been created " <<G4endl; |
---|
156 | GeomFile.close(); |
---|
157 | G4cout << " Tessellated Geometry file completed "<<G4endl; |
---|
158 | |
---|
159 | } |
---|
160 | |
---|
161 | //--------- Sizes of the principal geometrical components (solids) --------- |
---|
162 | G4double fWorld_XMin, fWorld_YMin, fWorld_ZMin; |
---|
163 | fWorld_XMin=fWorld_YMin=fWorld_ZMin = 0.; |
---|
164 | G4double fWorld_XMax, fWorld_YMax, fWorld_ZMax; |
---|
165 | fWorld_XMax=fWorld_YMax=fWorld_ZMax = 0.; |
---|
166 | std::vector<G4TessellatedSolid*>::iterator itr; |
---|
167 | for (itr = solidStore.begin(); itr != solidStore.end(); itr++) { |
---|
168 | if ((*itr)->GetMinXExtent() < fWorld_XMin ) fWorld_XMin = (*itr)->GetMinXExtent(); |
---|
169 | if ((*itr)->GetMinYExtent() < fWorld_YMin ) fWorld_YMin = (*itr)->GetMinYExtent(); |
---|
170 | if ((*itr)->GetMinZExtent() < fWorld_ZMin ) fWorld_ZMin = (*itr)->GetMinZExtent(); |
---|
171 | if ((*itr)->GetMaxXExtent() > fWorld_XMax ) fWorld_XMax = (*itr)->GetMaxXExtent(); |
---|
172 | if ((*itr)->GetMaxYExtent() > fWorld_YMax ) fWorld_YMax = (*itr)->GetMaxYExtent(); |
---|
173 | if ((*itr)->GetMaxZExtent() > fWorld_ZMin ) fWorld_ZMax = (*itr)->GetMaxZExtent(); |
---|
174 | } |
---|
175 | G4ThreeVector Total_Translate = G4ThreeVector( (fWorld_XMin+fWorld_XMax)/2., |
---|
176 | (fWorld_YMin+fWorld_YMax)/2., |
---|
177 | (fWorld_ZMin+fWorld_ZMax)/2.); |
---|
178 | G4double fWorldHX = (-fWorld_XMin+fWorld_XMax)/2.; |
---|
179 | G4double fWorldHY = (-fWorld_YMin+fWorld_YMax)/2.; |
---|
180 | G4double fWorldHZ = (-fWorld_ZMin+fWorld_ZMax)/2.; |
---|
181 | |
---|
182 | //--------- Definitions of Solids, Logical Volumes, Physical Volumes --------- |
---|
183 | |
---|
184 | //------------------------------ |
---|
185 | // World |
---|
186 | //------------------------------ |
---|
187 | |
---|
188 | solidWorld= new G4Box("world",fWorldHX,fWorldHY,fWorldHZ); |
---|
189 | logicWorld= new G4LogicalVolume( solidWorld, Air, "World", 0, 0, 0); |
---|
190 | |
---|
191 | // Must place the World Physical volume unrotated at (0,0,0). |
---|
192 | // |
---|
193 | physiWorld = new G4PVPlacement(0, // no rotation |
---|
194 | G4ThreeVector(), // at (0,0,0) |
---|
195 | logicWorld, // its logical volume |
---|
196 | "World", // its name |
---|
197 | 0, // its mother volume |
---|
198 | false, // no boolean operations |
---|
199 | 0); // no field specific to volume |
---|
200 | // |
---|
201 | // |
---|
202 | // now the logical and physical volumes |
---|
203 | // |
---|
204 | std::ifstream TreeFile("./test.tree"); |
---|
205 | if (!TreeFile){ |
---|
206 | G4cerr << "Geometry tree file not opened !!!" << G4endl; |
---|
207 | }else { |
---|
208 | G4cout << " Tessellated Geometry tree file opened "<<G4endl; |
---|
209 | G4String inputLine; |
---|
210 | G4String lsName, copy; |
---|
211 | G4String recordType(""); |
---|
212 | G4int level; |
---|
213 | char *sname, *tokenPtr; |
---|
214 | G4double a[17]; |
---|
215 | G4RotationMatrix rM; |
---|
216 | // |
---|
217 | while (!std::getline(TreeFile,inputLine).eof()) { |
---|
218 | // G4cout << inputLine <<G4endl; |
---|
219 | if (inputLine.substr(0,1) == "g") { |
---|
220 | std::istringstream tmpstream(inputLine); |
---|
221 | tmpstream >>recordType >> level >> lsName >>a[0] >>a[1] >> a[2] >>a[3] >>a[4] >> |
---|
222 | a[5] >>a[6] >>a[7] >>a[8] >>a[9] >>a[10] >>a[11] >>a[12] >>a[13] >>a[14] >> |
---|
223 | a[15] >> a[16]; |
---|
224 | G4cout << inputLine <<G4endl; |
---|
225 | sname = new char[strlen(lsName)+1]; |
---|
226 | strcpy(sname,lsName); |
---|
227 | tokenPtr = strtok(sname,"_"); |
---|
228 | lsName = G4String(tokenPtr); |
---|
229 | tokenPtr = strtok( NULL, "_"); |
---|
230 | copy = G4String(tokenPtr); |
---|
231 | tokenPtr = strtok( NULL, "_"); |
---|
232 | while (tokenPtr != NULL) { |
---|
233 | lsName += "_"; |
---|
234 | lsName += copy; |
---|
235 | copy = G4String(tokenPtr); |
---|
236 | tokenPtr = strtok( NULL, "_"); |
---|
237 | } |
---|
238 | // the name |
---|
239 | logicName = "L_"+lsName; |
---|
240 | physiName = "P_"+lsName; |
---|
241 | // the copy number |
---|
242 | std::istringstream is2(copy); |
---|
243 | is2 >> level; |
---|
244 | // G4cout << logicName << " " << physiName << " " << level << G4endl; |
---|
245 | // |
---|
246 | std::vector<G4TessellatedSolid*>::iterator itr=solidStore.begin(); |
---|
247 | while ((*itr)->GetName() != lsName && itr != solidStore.end()) |
---|
248 | itr++ ; |
---|
249 | if (itr != solidStore.end()) { |
---|
250 | solidTarget = (*itr); |
---|
251 | } else { |
---|
252 | G4cerr << " the .geom and .tree files don't match!" << G4endl; |
---|
253 | } |
---|
254 | logicTarget = new G4LogicalVolume(solidTarget,Pb,logicName,0,0,0); |
---|
255 | |
---|
256 | rM = G4RotationMatrix (G4ThreeVector(a[0],a[1],a[2]), |
---|
257 | G4ThreeVector(a[4],a[5],a[6]), |
---|
258 | G4ThreeVector(a[8],a[9],a[10])); |
---|
259 | physiTarget = new G4PVPlacement( G4Transform3D(rM, //rotation |
---|
260 | (G4ThreeVector(a[12], a[13], a[14]) - Total_Translate)),// position |
---|
261 | logicTarget, // its logical volume |
---|
262 | physiName, // its name |
---|
263 | logicWorld, // its mother volume |
---|
264 | false, // no boolean operations |
---|
265 | level-1); // copy no. |
---|
266 | } |
---|
267 | } |
---|
268 | TreeFile.close(); |
---|
269 | G4cout << " Tessellated Geometry tree file completed "<<G4endl; |
---|
270 | } |
---|
271 | //--------- Visualization attributes ------------------------------- |
---|
272 | |
---|
273 | G4VisAttributes* BoxVisAtt= new G4VisAttributes(G4Colour(1.0,1.0,1.0)); |
---|
274 | BoxVisAtt->SetVisibility(false); |
---|
275 | G4VisAttributes* FacetVisAtt= new G4VisAttributes(G4Colour(1.0,0.0,0.0)); |
---|
276 | FacetVisAtt->SetVisibility(true); |
---|
277 | |
---|
278 | logicWorld ->SetVisAttributes(BoxVisAtt); |
---|
279 | G4int i = 1; |
---|
280 | G4double r, g, b; |
---|
281 | for (itr = solidStore.begin(); itr != solidStore.end(); itr++) { |
---|
282 | r = (256-i)/256; |
---|
283 | g= 1.- r; |
---|
284 | b = 1- r*r; |
---|
285 | FacetVisAtt= new G4VisAttributes(G4Colour(r, g, b)); |
---|
286 | // works only on logic volume |
---|
287 | // (*itr) ->SetVisAttributes(FacetVisAtt); |
---|
288 | } |
---|
289 | //--------- example of User Limits ------------------------------- |
---|
290 | |
---|
291 | // below is an example of how to set tracking constraints in a given |
---|
292 | // logical volume(see also in N02PhysicsList how to setup the process |
---|
293 | // G4UserSpecialCuts). |
---|
294 | // Sets a max Step length in the tracker region |
---|
295 | // G4double maxStep = 0.5*ChamberWidth, maxLength = 2*fTrackerLength; |
---|
296 | // G4double maxTime = 0.1*ns, minEkin = 10*MeV; |
---|
297 | // logicTracker->SetUserLimits(new G4UserLimits(maxStep,maxLength,maxTime, |
---|
298 | // minEkin)); |
---|
299 | |
---|
300 | return physiWorld; |
---|
301 | } |
---|
302 | //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... |
---|