Loading...
Searching...
No Matches
DetectorConstruction.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//
27/// \file common/src/DetectorConstruction.cc
28/// \brief Implementation of the Common::DetectorConstruction class
29
30#include "DetectorConstruction.hh"
31
32#include "G4Material.hh"
33#include "G4NistManager.hh"
34#include "G4Box.hh"
35#include "G4LogicalVolume.hh"
36#include "G4PVPlacement.hh"
37#include "G4GenericMessenger.hh"
38
39namespace Common
40{
41
42//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
43
45 const G4String& boxMaterialName,
46 G4double boxHx, G4double boxHy, G4double boxHz,
47 const G4String& worldMaterialName,
48 G4double worldSizeFactor)
49 : fBoxMaterialName(boxMaterialName),
50 fWorldMaterialName(worldMaterialName),
51 fBoxDimensions(boxHx*2, boxHy*2, boxHz*2),
52 fWorldSizeFactor(worldSizeFactor)
53{
55}
56
57//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
58
63
64//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
65
67{
68 // Define materials via NIST manager
69 //
70 auto nistManager = G4NistManager::Instance();
71
72 auto worldMaterial = nistManager->FindOrBuildMaterial(fWorldMaterialName);
73 auto boxMaterial = nistManager->FindOrBuildMaterial(fBoxMaterialName);
74
75 // Geometry parameters
76 //
77 G4ThreeVector worldDimensions = fBoxDimensions * fWorldSizeFactor;
78
79 // World
80 //
81 auto sWorld
82 = new G4Box("World", //name
83 worldDimensions.x(), //dimensions (half-lentghs)
84 worldDimensions.y(),
85 worldDimensions.z());
86
88 = new G4LogicalVolume(sWorld, //shape
89 worldMaterial, //material
90 "World"); //name
91
92 auto pWorld
93 = new G4PVPlacement(0, //no rotation
94 G4ThreeVector(), //at (0,0,0)
95 fWorldVolume, //logical volume
96 "World", //name
97 0, //mother volume
98 false, //no boolean operation
99 0); //copy number
100
101 // Box
102 //
103 auto sBox
104 = new G4Box("Box", //its name
105 fBoxDimensions.x(), //dimensions (half-lengths)
106 fBoxDimensions.y(),
107 fBoxDimensions.z());
108
110 = new G4LogicalVolume(sBox, //its shape
111 boxMaterial, //its material
112 "Box"); //its name
113
114 new G4PVPlacement(0, //no rotation
115 G4ThreeVector(), //at (0,0,0)
116 fBoxVolume, //its logical volume
117 "Box", //its name
118 fWorldVolume, //its mother volume
119 false, //no boolean operation
120 0); //copy number
121
122 //always return the root volume
123 //
124 return pWorld;
125}
126
127//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
128
130{
131 auto nistManager = G4NistManager::Instance();
132
133 auto newMaterial = nistManager->FindOrBuildMaterial(materialName);
134 if ( ! newMaterial ) {
135 G4cerr << "Material " << materialName << " not found." << G4endl;
136 G4cerr << "The box material was not changed." << G4endl;
137 return;
138 }
139
140 if ( fBoxVolume ) fBoxVolume->SetMaterial(newMaterial);
141 G4cout << "Material of box changed to " << materialName << G4endl;
142}
143
144//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
145
147{
148 auto nistManager = G4NistManager::Instance();
149
150 auto newMaterial = nistManager->FindOrBuildMaterial(materialName);
151 if ( ! newMaterial ) {
152 G4cerr << "Material " << materialName << " not found." << G4endl;
153 G4cerr << "The box material was not changed." << G4endl;
154 return;
155 }
156
157 if ( fWorldVolume ) fWorldVolume->SetMaterial(newMaterial);
158 G4cout << "Material of box changed to " << materialName << G4endl;
159}
160
161//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
162
163void DetectorConstruction::SetBoxDimensions(G4ThreeVector dimensions)
164{
165/// Set box dimension (in half lengths).
166/// This setting has effect only if called in PreInit> phase
167
168 fBoxDimensions = dimensions;
169}
170
171//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
172
174{
175/// Set the multiplication factor from box dimensions to world dimensions.
176/// This setting has effect only if called in PreInit> phase
177
178 fWorldSizeFactor = factor;
179}
180
181//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
182
184{
185 // Define /B5/detector command directory using generic messenger class
187 "/detector/",
188 "Detector control");
189
190 // setBoxMaterial command
191 auto& setBoxMaterialCmd
192 = fMessenger->DeclareMethod("setBoxMaterial",
194 "Set box material name.");
195 setBoxMaterialCmd.SetParameterName("boxMaterialName", false);
196 setBoxMaterialCmd.SetDefaultValue("G4_AIR");
197
198 // setWorldMaterial command
199 auto& setWorldMaterialCmd
200 = fMessenger->DeclareMethod("setWorldMaterial",
202 "Set world material name.");
203 setWorldMaterialCmd.SetParameterName("worldMaterialName", false);
204 setWorldMaterialCmd.SetDefaultValue("G4_AIR");
205
206 // setBoxDimensions command
207 auto& setBoxDimensionsCmd
208 = fMessenger->DeclareMethodWithUnit("setBoxDimensions", "mm",
210 "Set box dimensions (in half lentgh).");
211 setBoxDimensionsCmd.SetParameterName("boxDimensions", false);
212 setBoxDimensionsCmd.SetStates(G4State_PreInit);
213
214 // setWorldSizeFactor command
215 auto& setWorldSizeFactorCmd
216 = fMessenger->DeclareMethod("setWorldSizeFactor",
218 "Set the multiplication factor from box dimensions to world dimensions.");
219 setWorldSizeFactorCmd.SetParameterName("worldSizeFactor", false);
220 setWorldSizeFactorCmd.SetRange("WorldSizeFactor >= 1");
221 setWorldSizeFactorCmd.SetStates(G4State_PreInit);
222}
223
224//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
225
226}
DetectorConstruction(const G4String &boxMaterialName="G4_AIR", G4double boxHx=40 *CLHEP::cm, G4double boxHy=40 *CLHEP::cm, G4double boxHz=40 *CLHEP::cm, const G4String &worldMaterialName="G4_AIR", G4double worldSizeFactor=1.25)
void SetWorldMaterial(const G4String &materialName)
void SetBoxDimensions(G4ThreeVector dimensions)
void SetBoxMaterial(const G4String &materialName)
void SetWorldSizeFactor(G4double factor)
G4VPhysicalVolume * Construct() override

Applications | User Support | Publications | Collaboration