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