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/DetectorConstruction0.cc 28 /// \brief Implementation of the Common::DetectorConstruction0 class 29 30 #include "DetectorConstruction0.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 DetectorConstruction0::DetectorConstruction0(const G4String& materialName, G4double hx, G4double hy, 45 G4double hz) 46 : fMaterialName(materialName), fDimensions(hx, hy, hz) 47 { 48 DefineCommands(); 49 } 50 51 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 52 53 DetectorConstruction0::~DetectorConstruction0() 54 { 55 delete fMessenger; 56 } 57 58 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 59 60 G4VPhysicalVolume* DetectorConstruction0::Construct() 61 { 62 // Define materials via NIST manager 63 // 64 auto nistManager = G4NistManager::Instance(); 65 66 auto material = nistManager->FindOrBuildMaterial(fMaterialName); 67 68 // World 69 // 70 auto sWorld = new G4Box("World", // name 71 fDimensions.x(), // dimensions (half-lentghs) 72 fDimensions.y(), fDimensions.z()); 73 74 fWorldVolume = new G4LogicalVolume(sWorld, // shape 75 material, // material 76 "World"); // name 77 78 auto pWorld = new G4PVPlacement(0, // no rotation 79 G4ThreeVector(), // at (0,0,0) 80 fWorldVolume, // logical volume 81 "World", // name 82 0, // mother volume 83 false, // no boolean operation 84 0); // copy number 85 86 // always return the root volume 87 // 88 return pWorld; 89 } 90 91 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 92 93 void DetectorConstruction0::SetMaterial(const G4String& materialName) 94 { 95 auto nistManager = G4NistManager::Instance(); 96 97 auto newMaterial = nistManager->FindOrBuildMaterial(materialName); 98 if (!newMaterial) { 99 G4cerr << "Material " << materialName << " not found." << G4endl; 100 G4cerr << "The box material was not changed." << G4endl; 101 return; 102 } 103 104 if (fWorldVolume) fWorldVolume->SetMaterial(newMaterial); 105 G4cout << "Material of box changed to " << materialName << G4endl; 106 } 107 108 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 109 110 void DetectorConstruction0::SetDimensions(G4ThreeVector dimensions) 111 { 112 /// Set world dimension (in half lengths). 113 /// This setting has effect only if called in PreInit> phase 114 115 fDimensions = dimensions; 116 } 117 118 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 119 120 void DetectorConstruction0::DefineCommands() 121 { 122 // Define /B5/detector command directory using generic messenger class 123 fMessenger = new G4GenericMessenger(this, "/detector/", "Detector control"); 124 125 // setMaterial command 126 auto& setMaterialCmd = fMessenger->DeclareMethod( 127 "setMaterial", &DetectorConstruction0::SetMaterial, "Set world material name."); 128 setMaterialCmd.SetParameterName("materialName", false); 129 setMaterialCmd.SetDefaultValue("G4_AIR"); 130 setMaterialCmd.SetStates(G4State_PreInit); 131 132 // setDimensions command 133 auto& setDimensionsCmd = 134 fMessenger->DeclareMethodWithUnit("setDimensions", "mm", &DetectorConstruction0::SetDimensions, 135 "Set world dimensions (in half lentgh)."); 136 setDimensionsCmd.SetParameterName("dimensions", false); 137 setDimensionsCmd.SetStates(G4State_PreInit); 138 } 139 140 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 141 142 } // namespace Common 143