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 | // File name: RadmonDetectorConstruction.cc |
---|
28 | // Creation date: Sep 2005 |
---|
29 | // Main author: Riccardo Capra <capra@ge.infn.it> |
---|
30 | // |
---|
31 | // Id: $Id: RadmonDetectorConstruction.cc,v 1.4.2.2 2006/06/29 16:13:28 gunter Exp $ |
---|
32 | // Tag: $Name: geant4-09-01-patch-02 $ |
---|
33 | // |
---|
34 | |
---|
35 | // Include files |
---|
36 | #include "RadmonDetectorConstruction.hh" |
---|
37 | |
---|
38 | #include "RadmonVDetectorLayout.hh" |
---|
39 | #include "RadmonVDetectorEntityConstructor.hh" |
---|
40 | #include "RadmonVDetectorEntitiesConstructorsFactory.hh" |
---|
41 | #include "RadmonMaterialsManager.hh" |
---|
42 | |
---|
43 | #include "G4RunManager.hh" |
---|
44 | #include "G4UIcommand.hh" |
---|
45 | #include "G4Sphere.hh" |
---|
46 | #include "G4LogicalVolume.hh" |
---|
47 | #include "G4PVPlacement.hh" |
---|
48 | |
---|
49 | |
---|
50 | |
---|
51 | RadmonDetectorConstruction :: RadmonDetectorConstruction(RadmonVDetectorLayout * layout, RadmonVDetectorEntitiesConstructorsFactory * factory) |
---|
52 | : |
---|
53 | detectorLayout(layout), |
---|
54 | constructorsFactory(factory), |
---|
55 | environmentConstructor(0), |
---|
56 | environmentPhysicalVolume(0), |
---|
57 | environmentLogicalVolume(0), |
---|
58 | environmentSolid(0) |
---|
59 | { |
---|
60 | if (detectorLayout==0) |
---|
61 | G4Exception("RadmonDetectorConstruction::RadmonDetectorConstruction: layout==0."); |
---|
62 | |
---|
63 | if (factory==0) |
---|
64 | G4Exception("RadmonDetectorConstruction::RadmonDetectorConstruction: factory==0."); |
---|
65 | |
---|
66 | detectorLayout->AttachObserver(this); |
---|
67 | } |
---|
68 | |
---|
69 | |
---|
70 | |
---|
71 | RadmonDetectorConstruction :: ~RadmonDetectorConstruction() |
---|
72 | { |
---|
73 | detectorLayout->DetachObserver(this); |
---|
74 | |
---|
75 | Destruct(); |
---|
76 | |
---|
77 | delete constructorsFactory; |
---|
78 | } |
---|
79 | |
---|
80 | |
---|
81 | |
---|
82 | |
---|
83 | |
---|
84 | G4VPhysicalVolume * RadmonDetectorConstruction :: Construct(void) |
---|
85 | { |
---|
86 | // This method will not change the detector layout |
---|
87 | RadmonVDetectorLayout const * const & const_detectorLayout(detectorLayout); |
---|
88 | |
---|
89 | if (const_detectorLayout->IsEnabledEnvironment()) |
---|
90 | BuildEnvironmentFromType(const_detectorLayout->GetEnvironmentType()); |
---|
91 | else |
---|
92 | BuildEnvironmentSphere(); |
---|
93 | |
---|
94 | environmentPhysicalVolume=new G4PVPlacement(0, G4ThreeVector(), "worldPV", environmentLogicalVolume, 0, false, 0); |
---|
95 | |
---|
96 | G4int placement(const_detectorLayout->GetNPlacements()); |
---|
97 | while (placement>0) |
---|
98 | { |
---|
99 | placement--; |
---|
100 | BuildMultilayer(placement); |
---|
101 | } |
---|
102 | |
---|
103 | return environmentPhysicalVolume; |
---|
104 | } |
---|
105 | |
---|
106 | |
---|
107 | |
---|
108 | |
---|
109 | |
---|
110 | void RadmonDetectorConstruction :: OnLayoutChange(void) |
---|
111 | { |
---|
112 | Destruct(); |
---|
113 | G4RunManager::GetRunManager()->DefineWorldVolume(Construct(), true); |
---|
114 | } |
---|
115 | |
---|
116 | |
---|
117 | |
---|
118 | |
---|
119 | void RadmonDetectorConstruction :: Destruct(void) |
---|
120 | { |
---|
121 | // Layers destruction |
---|
122 | LayerItem item; |
---|
123 | |
---|
124 | while (!layersStack.empty()) |
---|
125 | { |
---|
126 | item=layersStack.top(); |
---|
127 | |
---|
128 | environmentLogicalVolume->RemoveDaughter(item.second); |
---|
129 | delete item.second; |
---|
130 | delete item.first; |
---|
131 | |
---|
132 | layersStack.pop(); |
---|
133 | } |
---|
134 | |
---|
135 | // Environment destruction |
---|
136 | delete environmentPhysicalVolume; |
---|
137 | environmentPhysicalVolume=0; |
---|
138 | |
---|
139 | if (environmentConstructor) |
---|
140 | { |
---|
141 | delete environmentConstructor; |
---|
142 | environmentConstructor=0; |
---|
143 | } |
---|
144 | else |
---|
145 | { |
---|
146 | delete environmentLogicalVolume; |
---|
147 | delete environmentSolid; |
---|
148 | environmentSolid=0; |
---|
149 | } |
---|
150 | |
---|
151 | environmentLogicalVolume=0; |
---|
152 | } |
---|
153 | |
---|
154 | |
---|
155 | |
---|
156 | |
---|
157 | |
---|
158 | void RadmonDetectorConstruction :: BuildEnvironmentFromType(const G4String & type) |
---|
159 | { |
---|
160 | environmentConstructor=constructorsFactory->CreateEntityConstructor(type); |
---|
161 | |
---|
162 | if (environmentConstructor==0) |
---|
163 | { |
---|
164 | G4cout << "RadmonDetectorConstruction::BuildEnvironmentFromType: Environment type \"" << type << "\" not found. Environment ignored." << G4endl; |
---|
165 | BuildEnvironmentSphere(); |
---|
166 | } |
---|
167 | |
---|
168 | // This method will not change the detector layout |
---|
169 | RadmonVDetectorLayout const * const & const_detectorLayout(detectorLayout); |
---|
170 | |
---|
171 | G4int n(const_detectorLayout->GetEnvironmentNAttributes()); |
---|
172 | G4String name; |
---|
173 | G4String value; |
---|
174 | |
---|
175 | while (n>0) |
---|
176 | { |
---|
177 | n--; |
---|
178 | name=const_detectorLayout->GetEnvironmentAttributeName(n); |
---|
179 | value=const_detectorLayout->GetEnvironmentAttribute(name, G4String()); |
---|
180 | |
---|
181 | environmentConstructor->SetEntityAttribute(name, value); |
---|
182 | } |
---|
183 | |
---|
184 | environmentLogicalVolume=environmentConstructor->ConstructLogicalVolume(); |
---|
185 | |
---|
186 | if (environmentLogicalVolume==0) |
---|
187 | { |
---|
188 | delete environmentConstructor; |
---|
189 | environmentConstructor=0; |
---|
190 | G4cout << "RadmonDetectorConstruction::BuildEnvironmentFromType: Environment type \"" << type << "\" not built. Environment ignored." << G4endl; |
---|
191 | BuildEnvironmentSphere(); |
---|
192 | } |
---|
193 | } |
---|
194 | |
---|
195 | |
---|
196 | |
---|
197 | void RadmonDetectorConstruction :: BuildEnvironmentSphere(void) |
---|
198 | { |
---|
199 | // This method will not change the detector layout |
---|
200 | RadmonVDetectorLayout const * const & const_detectorLayout(detectorLayout); |
---|
201 | |
---|
202 | G4double maxRadius(-0.1); |
---|
203 | G4double radius; |
---|
204 | G4ThreeVector vect; |
---|
205 | |
---|
206 | G4int placement(const_detectorLayout->GetNPlacements()); |
---|
207 | G4String placementLabel; |
---|
208 | G4String multilayerLabel; |
---|
209 | |
---|
210 | while (placement>0) |
---|
211 | { |
---|
212 | placement--; |
---|
213 | placementLabel=const_detectorLayout->GetPlacementLabel(placement); |
---|
214 | multilayerLabel=const_detectorLayout->GetPlacementMultilayerType(placementLabel); |
---|
215 | |
---|
216 | vect.setX(const_detectorLayout->GetMultilayerWidth(multilayerLabel)); |
---|
217 | vect.setY(const_detectorLayout->GetMultilayerHeight(multilayerLabel)); |
---|
218 | vect.setZ(const_detectorLayout->GetMultilayerTotalThickness(multilayerLabel)); |
---|
219 | radius=const_detectorLayout->GetPlacementPosition(placementLabel).getR()+vect.getR()/2.; |
---|
220 | |
---|
221 | if (radius>maxRadius) |
---|
222 | maxRadius=radius; |
---|
223 | } |
---|
224 | |
---|
225 | if (maxRadius<=0.) |
---|
226 | maxRadius=5*m; |
---|
227 | |
---|
228 | G4Material & vacuum(RadmonMaterialsManager::Instance()->GetMaterial("RADMON_VACUUM")); |
---|
229 | |
---|
230 | environmentSolid=new G4Sphere("worldSphere", 0.*mm, maxRadius, 0.*deg, 360.*deg, 0.*deg, 180.*deg); |
---|
231 | environmentLogicalVolume=new G4LogicalVolume(environmentSolid, &vacuum, "worldLV", 0, 0, 0); |
---|
232 | } |
---|
233 | |
---|
234 | |
---|
235 | |
---|
236 | void RadmonDetectorConstruction :: BuildMultilayer(G4int index) |
---|
237 | { |
---|
238 | // This method will not change the detector layout |
---|
239 | RadmonVDetectorLayout const * const & const_detectorLayout(detectorLayout); |
---|
240 | |
---|
241 | G4String placementLabel(const_detectorLayout->GetPlacementLabel(index)); |
---|
242 | G4String multilayerLabel(const_detectorLayout->GetPlacementMultilayerType(placementLabel)); |
---|
243 | G4RotationMatrix rotation(const_detectorLayout->GetPlacementRotation(placementLabel)); |
---|
244 | G4ThreeVector position(const_detectorLayout->GetPlacementPosition(placementLabel)); |
---|
245 | |
---|
246 | G4double width(const_detectorLayout->GetMultilayerWidth(multilayerLabel)); |
---|
247 | if (width<=0.) |
---|
248 | return; |
---|
249 | |
---|
250 | G4String widthStr(G4UIcommand::ConvertToString(width/mm)+" mm"); |
---|
251 | |
---|
252 | G4double height(const_detectorLayout->GetMultilayerHeight(multilayerLabel)); |
---|
253 | if (height<=0.) |
---|
254 | return; |
---|
255 | |
---|
256 | G4String heightStr(G4UIcommand::ConvertToString(height/mm)+" mm"); |
---|
257 | |
---|
258 | G4double z(const_detectorLayout->GetMultilayerTotalThickness(multilayerLabel)); |
---|
259 | if (z<=0.) |
---|
260 | return; |
---|
261 | |
---|
262 | z/=2.; |
---|
263 | |
---|
264 | G4int n(const_detectorLayout->GetMultilayerNLayers(multilayerLabel)); |
---|
265 | if (n==0) |
---|
266 | return; |
---|
267 | |
---|
268 | while (n>0) |
---|
269 | { |
---|
270 | n--; |
---|
271 | |
---|
272 | G4String layerLabel(const_detectorLayout->GetMultilayerLayerLabel(multilayerLabel, n)); |
---|
273 | G4String layerType(const_detectorLayout->GetLayerType(multilayerLabel, layerLabel)); |
---|
274 | |
---|
275 | G4double layerThickness(const_detectorLayout->GetLayerThickness(multilayerLabel, layerLabel)); |
---|
276 | if (layerThickness<=0.) |
---|
277 | continue; |
---|
278 | |
---|
279 | RadmonVDetectorEntityConstructor * entityConstructor(constructorsFactory->CreateEntityConstructor(layerType)); |
---|
280 | |
---|
281 | if (entityConstructor==0) |
---|
282 | { |
---|
283 | z-=layerThickness; |
---|
284 | G4cout << "RadmonDetectorConstruction::BuildMultilayer: Layer type \"" << layerType << "\" not found. Layer ignored." << G4endl; |
---|
285 | continue; |
---|
286 | } |
---|
287 | |
---|
288 | G4int m(const_detectorLayout->GetLayerNAttributes(multilayerLabel, layerLabel)); |
---|
289 | G4String name; |
---|
290 | G4String value; |
---|
291 | |
---|
292 | while (m>0) |
---|
293 | { |
---|
294 | m--; |
---|
295 | name=const_detectorLayout->GetLayerAttributeName(multilayerLabel, layerLabel, m); |
---|
296 | value=const_detectorLayout->GetLayerAttribute(multilayerLabel, layerLabel, name, G4String()); |
---|
297 | |
---|
298 | entityConstructor->SetEntityAttribute(name, value); |
---|
299 | } |
---|
300 | |
---|
301 | entityConstructor->SetEntityAttribute("_WIDTH", widthStr); |
---|
302 | entityConstructor->SetEntityAttribute("_HEIGHT", heightStr); |
---|
303 | entityConstructor->SetEntityAttribute("_THICKNESS", G4UIcommand::ConvertToString(layerThickness/mm)+" mm"); |
---|
304 | |
---|
305 | G4LogicalVolume * entityLogicalVolume(entityConstructor->ConstructLogicalVolume()); |
---|
306 | |
---|
307 | if (entityLogicalVolume==0) |
---|
308 | { |
---|
309 | delete entityConstructor; |
---|
310 | G4cout << "RadmonDetectorConstruction::BuildMultilayer: Layer \"" << layerLabel << "\" from \"" << multilayerLabel << "\" not built. Layer ignored." << G4endl; |
---|
311 | z-=layerThickness; |
---|
312 | continue; |
---|
313 | } |
---|
314 | |
---|
315 | layerThickness/=2.; |
---|
316 | |
---|
317 | z-=layerThickness; |
---|
318 | G4ThreeVector layerPosition(rotation.colZ()); |
---|
319 | layerPosition*=z; |
---|
320 | layerPosition+=position; |
---|
321 | |
---|
322 | G4VPhysicalVolume * entityPhysicalVolume(new G4PVPlacement(G4Transform3D(rotation, layerPosition), entityLogicalVolume, placementLabel+"_"+multilayerLabel+"_"+layerLabel, environmentLogicalVolume, false, 0)); |
---|
323 | |
---|
324 | z-=layerThickness; |
---|
325 | |
---|
326 | layersStack.push(LayerItem(entityConstructor, entityPhysicalVolume)); |
---|
327 | } |
---|
328 | } |
---|