File indexing completed on 2026-04-17 07:51:59
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029 #include "DetectorConstruction0.hh"
0030
0031 #include "G4Box.hh"
0032 #include "G4GenericMessenger.hh"
0033 #include "G4LogicalVolume.hh"
0034 #include "G4Material.hh"
0035 #include "G4NistManager.hh"
0036 #include "G4PVPlacement.hh"
0037
0038 namespace Common
0039 {
0040
0041
0042
0043 DetectorConstruction0::DetectorConstruction0(const G4String& materialName, G4double hx, G4double hy,
0044 G4double hz)
0045 : fMaterialName(materialName), fDimensions(hx, hy, hz)
0046 {
0047 DefineCommands();
0048 }
0049
0050
0051
0052 DetectorConstruction0::~DetectorConstruction0()
0053 {
0054 delete fMessenger;
0055 }
0056
0057
0058
0059 G4VPhysicalVolume* DetectorConstruction0::Construct()
0060 {
0061
0062
0063 auto nistManager = G4NistManager::Instance();
0064
0065 auto material = nistManager->FindOrBuildMaterial(fMaterialName);
0066
0067
0068
0069 auto sWorld = new G4Box("World",
0070 fDimensions.x(),
0071 fDimensions.y(), fDimensions.z());
0072
0073 fWorldVolume = new G4LogicalVolume(sWorld,
0074 material,
0075 "World");
0076
0077 auto pWorld = new G4PVPlacement(0,
0078 G4ThreeVector(),
0079 fWorldVolume,
0080 "World",
0081 0,
0082 false,
0083 0);
0084
0085
0086
0087 return pWorld;
0088 }
0089
0090
0091
0092 void DetectorConstruction0::SetMaterial(const G4String& materialName)
0093 {
0094 auto nistManager = G4NistManager::Instance();
0095
0096 auto newMaterial = nistManager->FindOrBuildMaterial(materialName);
0097 if (!newMaterial) {
0098 G4cerr << "Material " << materialName << " not found." << G4endl;
0099 G4cerr << "The box material was not changed." << G4endl;
0100 return;
0101 }
0102
0103 if (fWorldVolume) fWorldVolume->SetMaterial(newMaterial);
0104 G4cout << "Material of box changed to " << materialName << G4endl;
0105 }
0106
0107
0108
0109 void DetectorConstruction0::SetDimensions(G4ThreeVector dimensions)
0110 {
0111
0112
0113
0114 fDimensions = dimensions;
0115 }
0116
0117
0118
0119 void DetectorConstruction0::DefineCommands()
0120 {
0121
0122 fMessenger = new G4GenericMessenger(this, "/detector/", "Detector control");
0123
0124
0125 auto& setMaterialCmd = fMessenger->DeclareMethod(
0126 "setMaterial", &DetectorConstruction0::SetMaterial, "Set world material name.");
0127 setMaterialCmd.SetParameterName("materialName", false);
0128 setMaterialCmd.SetDefaultValue("G4_AIR");
0129 setMaterialCmd.SetStates(G4State_PreInit);
0130
0131
0132 auto& setDimensionsCmd =
0133 fMessenger->DeclareMethodWithUnit("setDimensions", "mm", &DetectorConstruction0::SetDimensions,
0134 "Set world dimensions (in half lentgh).");
0135 setDimensionsCmd.SetParameterName("dimensions", false);
0136 setDimensionsCmd.SetStates(G4State_PreInit);
0137 }
0138
0139
0140
0141 }