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