Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // 26 // 26 // 27 /// \file common/src/DetectorConstruction.cc << 27 /// \file DetectorConstruction.cc 28 /// \brief Implementation of the Common::Detec << 28 /// \brief Implementation of the DetectorConstruction class 29 29 30 #include "DetectorConstruction.hh" 30 #include "DetectorConstruction.hh" 31 31 32 #include "G4Box.hh" << 33 #include "G4GenericMessenger.hh" << 34 #include "G4LogicalVolume.hh" << 35 #include "G4Material.hh" 32 #include "G4Material.hh" 36 #include "G4NistManager.hh" 33 #include "G4NistManager.hh" >> 34 #include "G4Box.hh" >> 35 #include "G4LogicalVolume.hh" 37 #include "G4PVPlacement.hh" 36 #include "G4PVPlacement.hh" 38 << 37 #include "G4GenericMessenger.hh" 39 namespace Common << 40 { << 41 38 42 //....oooOO0OOooo........oooOO0OOooo........oo 39 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 43 40 44 DetectorConstruction::DetectorConstruction(con << 41 DetectorConstruction::DetectorConstruction( 45 G4d << 42 const G4String& boxMaterialName, 46 con << 43 G4double boxHx, G4double boxHy, G4double boxHz, 47 G4d << 44 const G4String& worldMaterialName, 48 : fBoxMaterialName(boxMaterialName), << 45 G4double worldSizeFactor) 49 fWorldMaterialName(worldMaterialName), << 46 : G4VUserDetectorConstruction(), 50 fBoxDimensions(boxHx * 2, boxHy * 2, boxHz << 47 fMessenger(nullptr), 51 fWorldSizeFactor(worldSizeFactor) << 48 fBoxMaterialName(boxMaterialName), >> 49 fWorldMaterialName(worldMaterialName), >> 50 fBoxDimensions(boxHx*2, boxHy*2, boxHz*2), >> 51 fWorldSizeFactor(worldSizeFactor), >> 52 fBoxVolume(nullptr), >> 53 fWorldVolume(nullptr) 52 { 54 { 53 DefineCommands(); 55 DefineCommands(); 54 } 56 } 55 57 56 //....oooOO0OOooo........oooOO0OOooo........oo 58 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 57 59 58 DetectorConstruction::~DetectorConstruction() 60 DetectorConstruction::~DetectorConstruction() 59 { 61 { 60 delete fMessenger; 62 delete fMessenger; 61 } 63 } 62 64 63 //....oooOO0OOooo........oooOO0OOooo........oo 65 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 64 66 65 G4VPhysicalVolume* DetectorConstruction::Const 67 G4VPhysicalVolume* DetectorConstruction::Construct() 66 { 68 { 67 // Define materials via NIST manager 69 // Define materials via NIST manager 68 // 70 // 69 auto nistManager = G4NistManager::Instance() 71 auto nistManager = G4NistManager::Instance(); 70 72 71 auto worldMaterial = nistManager->FindOrBuil 73 auto worldMaterial = nistManager->FindOrBuildMaterial(fWorldMaterialName); 72 auto boxMaterial = nistManager->FindOrBuildM 74 auto boxMaterial = nistManager->FindOrBuildMaterial(fBoxMaterialName); 73 << 75 74 // Geometry parameters 76 // Geometry parameters 75 // 77 // 76 G4ThreeVector worldDimensions = fBoxDimensio 78 G4ThreeVector worldDimensions = fBoxDimensions * fWorldSizeFactor; 77 << 79 78 // World 80 // World 79 // 81 // 80 auto sWorld = new G4Box("World", // name << 82 auto sWorld 81 worldDimensions.x(), << 83 = new G4Box("World", //name 82 worldDimensions.y(), << 84 worldDimensions.x(), //dimensions (half-lentghs) 83 << 85 worldDimensions.y(), 84 fWorldVolume = new G4LogicalVolume(sWorld, << 86 worldDimensions.z()); 85 worldMate << 87 86 "World"); << 88 fWorldVolume 87 << 89 = new G4LogicalVolume(sWorld, //shape 88 auto pWorld = new G4PVPlacement(0, // no ro << 90 worldMaterial, //material 89 G4ThreeVecto << 91 "World"); //name 90 fWorldVolume << 92 91 "World", // << 93 auto pWorld 92 0, // mothe << 94 = new G4PVPlacement(0, //no rotation 93 false, // n << 95 G4ThreeVector(), //at (0,0,0) 94 0); // copy << 96 fWorldVolume, //logical volume 95 << 97 "World", //name >> 98 0, //mother volume >> 99 false, //no boolean operation >> 100 0); //copy number >> 101 96 // Box 102 // Box 97 // << 103 // 98 auto sBox = new G4Box("Box", // its name << 104 auto sBox 99 fBoxDimensions.x(), / << 105 = new G4Box("Box", //its name 100 fBoxDimensions.y(), fB << 106 fBoxDimensions.x(), //dimensions (half-lengths) 101 << 107 fBoxDimensions.y(), 102 fBoxVolume = new G4LogicalVolume(sBox, // i << 108 fBoxDimensions.z()); 103 boxMaterial << 109 104 "Box"); // << 110 fBoxVolume 105 << 111 = new G4LogicalVolume(sBox, //its shape 106 new G4PVPlacement(0, // no rotation << 112 boxMaterial, //its material 107 G4ThreeVector(), // at (0 << 113 "Box"); //its name 108 fBoxVolume, // its logica << 114 109 "Box", // its name << 115 new G4PVPlacement(0, //no rotation 110 fWorldVolume, // its moth << 116 G4ThreeVector(), //at (0,0,0) 111 false, // no boolean oper << 117 fBoxVolume, //its logical volume 112 0); // copy number << 118 "Box", //its name >> 119 fWorldVolume, //its mother volume >> 120 false, //no boolean operation >> 121 0); //copy number 113 122 114 // always return the root volume << 123 //always return the root volume 115 // 124 // 116 return pWorld; 125 return pWorld; 117 } 126 } 118 127 119 //....oooOO0OOooo........oooOO0OOooo........oo 128 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 120 << 129 121 void DetectorConstruction::SetBoxMaterial(cons 130 void DetectorConstruction::SetBoxMaterial(const G4String& materialName) 122 { 131 { 123 auto nistManager = G4NistManager::Instance() 132 auto nistManager = G4NistManager::Instance(); 124 133 125 auto newMaterial = nistManager->FindOrBuildM 134 auto newMaterial = nistManager->FindOrBuildMaterial(materialName); 126 if (!newMaterial) { << 135 if ( ! newMaterial ) { 127 G4cerr << "Material " << materialName << " 136 G4cerr << "Material " << materialName << " not found." << G4endl; 128 G4cerr << "The box material was not change 137 G4cerr << "The box material was not changed." << G4endl; 129 return; 138 return; 130 } << 139 } 131 << 140 132 if (fBoxVolume) fBoxVolume->SetMaterial(newM << 141 if ( fBoxVolume ) fBoxVolume->SetMaterial(newMaterial); 133 G4cout << "Material of box changed to " << m 142 G4cout << "Material of box changed to " << materialName << G4endl; 134 } 143 } 135 144 136 //....oooOO0OOooo........oooOO0OOooo........oo 145 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 137 << 146 138 void DetectorConstruction::SetWorldMaterial(co 147 void DetectorConstruction::SetWorldMaterial(const G4String& materialName) 139 { 148 { 140 auto nistManager = G4NistManager::Instance() 149 auto nistManager = G4NistManager::Instance(); 141 150 142 auto newMaterial = nistManager->FindOrBuildM 151 auto newMaterial = nistManager->FindOrBuildMaterial(materialName); 143 if (!newMaterial) { << 152 if ( ! newMaterial ) { 144 G4cerr << "Material " << materialName << " 153 G4cerr << "Material " << materialName << " not found." << G4endl; 145 G4cerr << "The box material was not change 154 G4cerr << "The box material was not changed." << G4endl; 146 return; 155 return; 147 } << 156 } 148 << 157 149 if (fWorldVolume) fWorldVolume->SetMaterial( << 158 if ( fWorldVolume ) fWorldVolume->SetMaterial(newMaterial); 150 G4cout << "Material of box changed to " << m 159 G4cout << "Material of box changed to " << materialName << G4endl; 151 } 160 } 152 161 153 //....oooOO0OOooo........oooOO0OOooo........oo 162 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 154 << 163 155 void DetectorConstruction::SetBoxDimensions(G4 << 164 void DetectorConstruction::SetBoxDimensions(G4ThreeVector dimensions) 156 { 165 { 157 /// Set box dimension (in half lengths). << 166 /// Set box dimension (in half lengths). 158 /// This setting has effect only if called i << 167 /// This setting has effect only if called in PreInit> phase 159 168 160 fBoxDimensions = dimensions; 169 fBoxDimensions = dimensions; 161 } << 170 } 162 << 171 163 //....oooOO0OOooo........oooOO0OOooo........oo 172 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 164 173 165 void DetectorConstruction::SetWorldSizeFactor( << 174 void DetectorConstruction::SetWorldSizeFactor(G4double factor) 166 { 175 { 167 /// Set the multiplication factor from box d << 176 /// Set the multiplication factor from box dimensions to world dimensions. 168 /// This setting has effect only if called i << 177 /// This setting has effect only if called in PreInit> phase 169 178 170 fWorldSizeFactor = factor; 179 fWorldSizeFactor = factor; 171 } << 180 } 172 << 181 173 //....oooOO0OOooo........oooOO0OOooo........oo 182 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 174 183 175 void DetectorConstruction::DefineCommands() 184 void DetectorConstruction::DefineCommands() 176 { 185 { 177 // Define /B5/detector command directory usi 186 // Define /B5/detector command directory using generic messenger class 178 fMessenger = new G4GenericMessenger(this, "/ << 187 fMessenger = new G4GenericMessenger(this, >> 188 "/detector/", >> 189 "Detector control"); 179 190 180 // setBoxMaterial command 191 // setBoxMaterial command 181 auto& setBoxMaterialCmd = fMessenger->Declar << 192 auto& setBoxMaterialCmd 182 "setBoxMaterial", &DetectorConstruction::S << 193 = fMessenger->DeclareMethod("setBoxMaterial", >> 194 &DetectorConstruction::SetBoxMaterial, >> 195 "Set box material name."); 183 setBoxMaterialCmd.SetParameterName("boxMater 196 setBoxMaterialCmd.SetParameterName("boxMaterialName", false); 184 setBoxMaterialCmd.SetDefaultValue("G4_AIR"); 197 setBoxMaterialCmd.SetDefaultValue("G4_AIR"); 185 198 186 // setWorldMaterial command 199 // setWorldMaterial command 187 auto& setWorldMaterialCmd = fMessenger->Decl << 200 auto& setWorldMaterialCmd 188 "setWorldMaterial", &DetectorConstruction: << 201 = fMessenger->DeclareMethod("setWorldMaterial", >> 202 &DetectorConstruction::SetWorldMaterial, >> 203 "Set world material name."); 189 setWorldMaterialCmd.SetParameterName("worldM 204 setWorldMaterialCmd.SetParameterName("worldMaterialName", false); 190 setWorldMaterialCmd.SetDefaultValue("G4_AIR" 205 setWorldMaterialCmd.SetDefaultValue("G4_AIR"); 191 206 192 // setBoxDimensions command 207 // setBoxDimensions command 193 auto& setBoxDimensionsCmd = fMessenger->Decl << 208 auto& setBoxDimensionsCmd 194 "setBoxDimensions", "mm", &DetectorConstru << 209 = fMessenger->DeclareMethodWithUnit("setBoxDimensions", "mm", 195 "Set box dimensions (in half lentgh)."); << 210 &DetectorConstruction::SetBoxDimensions, >> 211 "Set box dimensions (in half lentgh)."); 196 setBoxDimensionsCmd.SetParameterName("boxDim 212 setBoxDimensionsCmd.SetParameterName("boxDimensions", false); 197 setBoxDimensionsCmd.SetStates(G4State_PreIni 213 setBoxDimensionsCmd.SetStates(G4State_PreInit); 198 214 199 // setWorldSizeFactor command 215 // setWorldSizeFactor command 200 auto& setWorldSizeFactorCmd = fMessenger->De << 216 auto& setWorldSizeFactorCmd 201 "setWorldSizeFactor", &DetectorConstructio << 217 = fMessenger->DeclareMethod("setWorldSizeFactor", 202 "Set the multiplication factor from box di << 218 &DetectorConstruction::SetWorldSizeFactor, >> 219 "Set the multiplication factor from box dimensions to world dimensions."); 203 setWorldSizeFactorCmd.SetParameterName("worl 220 setWorldSizeFactorCmd.SetParameterName("worldSizeFactor", false); 204 setWorldSizeFactorCmd.SetRange("WorldSizeFac 221 setWorldSizeFactorCmd.SetRange("WorldSizeFactor >= 1"); 205 setWorldSizeFactorCmd.SetStates(G4State_PreI 222 setWorldSizeFactorCmd.SetStates(G4State_PreInit); 206 } 223 } 207 << 224 208 //....oooOO0OOooo........oooOO0OOooo........oo 225 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 209 << 210 } // namespace Common << 211 226