Geant4 Cross Reference |
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 /// \file DetectorConstruction.cc 27 /// \brief Implementation of the DetectorConstruction class 28 // 29 // 30 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 31 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 32 33 #include "DetectorConstruction.hh" 34 35 #include "DetectorMessenger.hh" 36 37 #include "G4AutoDelete.hh" 38 #include "G4Box.hh" 39 #include "G4GeometryManager.hh" 40 #include "G4GlobalMagFieldMessenger.hh" 41 #include "G4LogicalVolume.hh" 42 #include "G4LogicalVolumeStore.hh" 43 #include "G4Material.hh" 44 #include "G4NistManager.hh" 45 #include "G4PVPlacement.hh" 46 #include "G4PVReplica.hh" 47 #include "G4PhysicalConstants.hh" 48 #include "G4PhysicalVolumeStore.hh" 49 #include "G4RunManager.hh" 50 #include "G4SolidStore.hh" 51 #include "G4SystemOfUnits.hh" 52 #include "G4UImanager.hh" 53 #include "G4UniformMagField.hh" 54 #include "G4UnitsTable.hh" 55 56 #include <iomanip> 57 58 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 59 60 DetectorConstruction::DetectorConstruction() 61 { 62 // default parameter values of the absorbers 63 fNbOfAbsor = 1; 64 fAbsorThickness[0] = 0 * mm; // dummy, for initialization 65 fAbsorThickness[1] = 1 * mm; 66 fAbsorSizeYZ = 1 * mm; 67 68 ComputeParameters(); 69 70 // materials 71 DefineMaterials(); 72 SetAbsorMaterial(1, "G4_Si"); 73 74 // create commands for interactive definition of the calorimeter 75 fDetectorMessenger = new DetectorMessenger(this); 76 } 77 78 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 79 80 DetectorConstruction::~DetectorConstruction() 81 { 82 delete fDetectorMessenger; 83 } 84 85 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 86 87 G4VPhysicalVolume* DetectorConstruction::Construct() 88 { 89 return ConstructVolumes(); 90 } 91 92 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 93 94 void DetectorConstruction::DefineMaterials() 95 { 96 G4NistManager* man = G4NistManager::Instance(); 97 98 man->FindOrBuildMaterial("G4_AIR"); 99 100 G4Element* H = man->FindOrBuildElement("H"); 101 G4Element* O = man->FindOrBuildElement("O"); 102 103 G4Material* H2O = new G4Material("Water", 1.000 * g / cm3, 2); 104 H2O->AddElement(H, 2); 105 H2O->AddElement(O, 1); 106 H2O->GetIonisation()->SetMeanExcitationEnergy(78.0 * eV); 107 108 G4Element* Hf = man->FindOrBuildElement("Hf"); 109 110 G4Material* HfO2 = new G4Material("HfO2", 9.68 * g / cm3, 2); 111 HfO2->AddElement(Hf, 1); 112 HfO2->AddElement(O, 2); 113 114 // example of vacuum 115 G4double density = universe_mean_density; // from PhysicalConstants.h 116 G4double pressure = 3.e-18 * pascal; 117 G4double temperature = 2.73 * kelvin; 118 G4Material* Galactic = 119 new G4Material("Galactic", 1., 1.008 * g / mole, density, kStateGas, temperature, pressure); 120 121 fDefaultMaterial = Galactic; 122 123 // G4cout << *(G4Material::GetMaterialTable()) << G4endl; 124 } 125 126 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 127 128 G4Material* DetectorConstruction::MaterialWithSingleIsotope(G4String name, G4String symbol, 129 G4double density, G4int Z, G4int A) 130 { 131 // define a material from an isotope 132 // 133 G4int ncomponents; 134 G4double abundance, massfraction; 135 136 G4Isotope* isotope = new G4Isotope(symbol, Z, A); 137 138 G4Element* element = new G4Element(name, symbol, ncomponents = 1); 139 element->AddIsotope(isotope, abundance = 100. * perCent); 140 141 G4Material* material = new G4Material(name, density, ncomponents = 1); 142 material->AddElement(element, massfraction = 100. * perCent); 143 144 return material; 145 } 146 147 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 148 149 void DetectorConstruction::ComputeParameters() 150 { 151 // Compute total thickness of absorbers 152 fAbsorSizeX = 0.; 153 for (G4int iAbs = 1; iAbs <= fNbOfAbsor; iAbs++) { 154 fAbsorSizeX += fAbsorThickness[iAbs]; 155 } 156 fWorldSizeX = 1.2 * fAbsorSizeX; 157 fWorldSizeYZ = 1.2 * fAbsorSizeYZ; 158 } 159 160 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 161 162 G4VPhysicalVolume* DetectorConstruction::ConstructVolumes() 163 { 164 // complete the Calor parameters definition 165 ComputeParameters(); 166 167 // Cleanup old geometry 168 G4GeometryManager::GetInstance()->OpenGeometry(); 169 G4PhysicalVolumeStore::GetInstance()->Clean(); 170 G4LogicalVolumeStore::GetInstance()->Clean(); 171 G4SolidStore::GetInstance()->Clean(); 172 173 // 174 // World 175 // 176 G4Box* solidWorld = new G4Box("World", // name 177 fWorldSizeX / 2, fWorldSizeYZ / 2, fWorldSizeYZ / 2); // size 178 179 G4LogicalVolume* logicWorld = new G4LogicalVolume(solidWorld, // solid 180 fDefaultMaterial, // material 181 "World"); // name 182 183 fPhysiWorld = new G4PVPlacement(0, // no rotation 184 G4ThreeVector(), // at (0,0,0) 185 logicWorld, // logical volume 186 "World", // name 187 0, // mother volume 188 false, // no boolean operation 189 0); // copy number 190 191 // 192 // Absorbers 193 // 194 fXfront[0] = -0.5 * fAbsorSizeX; 195 // 196 for (G4int k = 1; k <= fNbOfAbsor; k++) { 197 G4Material* material = fAbsorMaterial[k]; 198 G4String matname = material->GetName(); 199 200 G4Box* solidAbsor = 201 new G4Box(matname, fAbsorThickness[k] / 2, fAbsorSizeYZ / 2, fAbsorSizeYZ / 2); 202 203 G4LogicalVolume* logicAbsor = new G4LogicalVolume(solidAbsor, // solid 204 material, // material 205 matname); // name 206 207 fXfront[k] = fXfront[k - 1] + fAbsorThickness[k - 1]; 208 G4double xcenter = fXfront[k] + 0.5 * fAbsorThickness[k]; 209 G4ThreeVector position = G4ThreeVector(xcenter, 0., 0.); 210 211 new G4PVPlacement(0, // no rotation 212 position, // position 213 logicAbsor, // logical volume 214 matname, // name 215 logicWorld, // mother 216 false, // no boulean operat 217 k); // copy number 218 } 219 220 PrintParameters(); 221 222 // always return the physical World 223 // 224 return fPhysiWorld; 225 } 226 227 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 228 229 void DetectorConstruction::PrintParameters() 230 { 231 G4cout << "\n-------------------------------------------------------------" 232 << "\n ---> The Absorber is " << fNbOfAbsor << " layers of:"; 233 for (G4int i = 1; i <= fNbOfAbsor; i++) { 234 G4cout << "\n \t" << std::setw(12) << fAbsorMaterial[i]->GetName() << ": " << std::setw(6) 235 << G4BestUnit(fAbsorThickness[i], "Length"); 236 } 237 G4cout << "\n-------------------------------------------------------------\n" << G4endl; 238 } 239 240 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 241 242 void DetectorConstruction::SetNbOfAbsor(G4int ival) 243 { 244 // set the number of Absorbers 245 // 246 if (ival < 1 || ival > (kMaxAbsor - 1)) { 247 G4cout << "\n ---> warning from SetfNbOfAbsor: " << ival << " must be at least 1 and and most " 248 << kMaxAbsor - 1 << ". Command refused" << G4endl; 249 return; 250 } 251 fNbOfAbsor = ival; 252 G4RunManager::GetRunManager()->ReinitializeGeometry(); 253 } 254 255 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 256 257 void DetectorConstruction::SetAbsorMaterial(G4int iabs, const G4String& material) 258 { 259 // search the material by its name 260 // 261 if (iabs > fNbOfAbsor || iabs <= 0) { 262 G4cout << "\n --->warning from SetfAbsorMaterial: absor number " << iabs 263 << " out of range. Command refused" << G4endl; 264 return; 265 } 266 267 G4Material* pttoMaterial = G4NistManager::Instance()->FindOrBuildMaterial(material); 268 if (pttoMaterial) { 269 fAbsorMaterial[iabs] = pttoMaterial; 270 G4RunManager::GetRunManager()->PhysicsHasBeenModified(); 271 } 272 } 273 274 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 275 276 void DetectorConstruction::SetAbsorThickness(G4int iabs, G4double val) 277 { 278 // change Absorber thickness 279 // 280 if (iabs > fNbOfAbsor || iabs <= 0) { 281 G4cout << "\n --->warning from SetfAbsorThickness: absor number " << iabs 282 << " out of range. Command refused" << G4endl; 283 return; 284 } 285 if (val <= DBL_MIN) { 286 G4cout << "\n --->warning from SetfAbsorThickness: thickness " << val 287 << " out of range. Command refused" << G4endl; 288 return; 289 } 290 fAbsorThickness[iabs] = val; 291 G4RunManager::GetRunManager()->ReinitializeGeometry(); 292 } 293 294 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 295 296 void DetectorConstruction::SetAbsorSizeYZ(G4double val) 297 { 298 // change the transverse size 299 // 300 if (val <= DBL_MIN) { 301 G4cout << "\n --->warning from SetfAbsorSizeYZ: thickness " << val 302 << " out of range. Command refused" << G4endl; 303 return; 304 } 305 fAbsorSizeYZ = val; 306 G4RunManager::GetRunManager()->ReinitializeGeometry(); 307 } 308 309 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 310 311 void DetectorConstruction::ConstructSDandField() 312 { 313 if (fFieldMessenger.Get() == 0) { 314 // Create global magnetic field messenger. 315 // Uniform magnetic field is then created automatically if 316 // the field value is not zero. 317 G4ThreeVector fieldValue = G4ThreeVector(); 318 G4GlobalMagFieldMessenger* msg = new G4GlobalMagFieldMessenger(fieldValue); 319 // msg->SetVerboseLevel(1); 320 G4AutoDelete::Register(msg); 321 fFieldMessenger.Put(msg); 322 } 323 } 324 325 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 326