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