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