File indexing completed on 2026-03-28 07:50:11
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 "DetectorConstruction.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 DetectorConstruction::DetectorConstruction(const G4String& boxMaterialName, G4double boxHx,
0044 G4double boxHy, G4double boxHz,
0045 const G4String& worldMaterialName,
0046 G4double worldSizeFactor)
0047 : fBoxMaterialName(boxMaterialName),
0048 fWorldMaterialName(worldMaterialName),
0049 fBoxDimensions(boxHx * 2, boxHy * 2, boxHz * 2),
0050 fWorldSizeFactor(worldSizeFactor)
0051 {
0052 DefineCommands();
0053 }
0054
0055
0056
0057 DetectorConstruction::~DetectorConstruction()
0058 {
0059 delete fMessenger;
0060 }
0061
0062
0063
0064 G4VPhysicalVolume* DetectorConstruction::Construct()
0065 {
0066
0067
0068 auto nistManager = G4NistManager::Instance();
0069
0070 auto worldMaterial = nistManager->FindOrBuildMaterial(fWorldMaterialName);
0071 auto boxMaterial = nistManager->FindOrBuildMaterial(fBoxMaterialName);
0072
0073
0074
0075 G4ThreeVector worldDimensions = fBoxDimensions * fWorldSizeFactor;
0076
0077
0078
0079 auto sWorld = new G4Box("World",
0080 worldDimensions.x(),
0081 worldDimensions.y(), worldDimensions.z());
0082
0083 fWorldVolume = new G4LogicalVolume(sWorld,
0084 worldMaterial,
0085 "World");
0086
0087 auto pWorld = new G4PVPlacement(0,
0088 G4ThreeVector(),
0089 fWorldVolume,
0090 "World",
0091 0,
0092 false,
0093 0);
0094
0095
0096
0097 auto sBox = new G4Box("Box",
0098 fBoxDimensions.x(),
0099 fBoxDimensions.y(), fBoxDimensions.z());
0100
0101 fBoxVolume = new G4LogicalVolume(sBox,
0102 boxMaterial,
0103 "Box");
0104
0105 new G4PVPlacement(0,
0106 G4ThreeVector(),
0107 fBoxVolume,
0108 "Box",
0109 fWorldVolume,
0110 false,
0111 0);
0112
0113
0114
0115 return pWorld;
0116 }
0117
0118
0119
0120 void DetectorConstruction::SetBoxMaterial(const G4String& materialName)
0121 {
0122 auto nistManager = G4NistManager::Instance();
0123
0124 auto newMaterial = nistManager->FindOrBuildMaterial(materialName);
0125 if (!newMaterial) {
0126 G4cerr << "Material " << materialName << " not found." << G4endl;
0127 G4cerr << "The box material was not changed." << G4endl;
0128 return;
0129 }
0130
0131 if (fBoxVolume) fBoxVolume->SetMaterial(newMaterial);
0132 G4cout << "Material of box changed to " << materialName << G4endl;
0133 }
0134
0135
0136
0137 void DetectorConstruction::SetWorldMaterial(const G4String& materialName)
0138 {
0139 auto nistManager = G4NistManager::Instance();
0140
0141 auto newMaterial = nistManager->FindOrBuildMaterial(materialName);
0142 if (!newMaterial) {
0143 G4cerr << "Material " << materialName << " not found." << G4endl;
0144 G4cerr << "The box material was not changed." << G4endl;
0145 return;
0146 }
0147
0148 if (fWorldVolume) fWorldVolume->SetMaterial(newMaterial);
0149 G4cout << "Material of box changed to " << materialName << G4endl;
0150 }
0151
0152
0153
0154 void DetectorConstruction::SetBoxDimensions(G4ThreeVector dimensions)
0155 {
0156
0157
0158
0159 fBoxDimensions = dimensions;
0160 }
0161
0162
0163
0164 void DetectorConstruction::SetWorldSizeFactor(G4double factor)
0165 {
0166
0167
0168
0169 fWorldSizeFactor = factor;
0170 }
0171
0172
0173
0174 void DetectorConstruction::DefineCommands()
0175 {
0176
0177 fMessenger = new G4GenericMessenger(this, "/detector/", "Detector control");
0178
0179
0180 auto& setBoxMaterialCmd = fMessenger->DeclareMethod(
0181 "setBoxMaterial", &DetectorConstruction::SetBoxMaterial, "Set box material name.");
0182 setBoxMaterialCmd.SetParameterName("boxMaterialName", false);
0183 setBoxMaterialCmd.SetDefaultValue("G4_AIR");
0184
0185
0186 auto& setWorldMaterialCmd = fMessenger->DeclareMethod(
0187 "setWorldMaterial", &DetectorConstruction::SetWorldMaterial, "Set world material name.");
0188 setWorldMaterialCmd.SetParameterName("worldMaterialName", false);
0189 setWorldMaterialCmd.SetDefaultValue("G4_AIR");
0190
0191
0192 auto& setBoxDimensionsCmd = fMessenger->DeclareMethodWithUnit(
0193 "setBoxDimensions", "mm", &DetectorConstruction::SetBoxDimensions,
0194 "Set box dimensions (in half lentgh).");
0195 setBoxDimensionsCmd.SetParameterName("boxDimensions", false);
0196 setBoxDimensionsCmd.SetStates(G4State_PreInit);
0197
0198
0199 auto& setWorldSizeFactorCmd = fMessenger->DeclareMethod(
0200 "setWorldSizeFactor", &DetectorConstruction::SetWorldSizeFactor,
0201 "Set the multiplication factor from box dimensions to world dimensions.");
0202 setWorldSizeFactorCmd.SetParameterName("worldSizeFactor", false);
0203 setWorldSizeFactorCmd.SetRange("WorldSizeFactor >= 1");
0204 setWorldSizeFactorCmd.SetStates(G4State_PreInit);
0205 }
0206
0207
0208
0209 }