Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-03-28 07:50:11

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 /// \file DetectorConstruction.cc
0027 /// \brief Implementation of the Common::DetectorConstruction class
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 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
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 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0056 
0057 DetectorConstruction::~DetectorConstruction()
0058 {
0059   delete fMessenger;
0060 }
0061 
0062 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0063 
0064 G4VPhysicalVolume* DetectorConstruction::Construct()
0065 {
0066   // Define materials via NIST manager
0067   //
0068   auto nistManager = G4NistManager::Instance();
0069 
0070   auto worldMaterial = nistManager->FindOrBuildMaterial(fWorldMaterialName);
0071   auto boxMaterial = nistManager->FindOrBuildMaterial(fBoxMaterialName);
0072 
0073   // Geometry parameters
0074   //
0075   G4ThreeVector worldDimensions = fBoxDimensions * fWorldSizeFactor;
0076 
0077   // World
0078   //
0079   auto sWorld = new G4Box("World",  // name
0080                           worldDimensions.x(),  // dimensions (half-lentghs)
0081                           worldDimensions.y(), worldDimensions.z());
0082 
0083   fWorldVolume = new G4LogicalVolume(sWorld,  // shape
0084                                      worldMaterial,  // material
0085                                      "World");  // name
0086 
0087   auto pWorld = new G4PVPlacement(0,  // no rotation
0088                                   G4ThreeVector(),  // at (0,0,0)
0089                                   fWorldVolume,  // logical volume
0090                                   "World",  // name
0091                                   0,  // mother  volume
0092                                   false,  // no boolean operation
0093                                   0);  // copy number
0094 
0095   // Box
0096   //
0097   auto sBox = new G4Box("Box",  // its name
0098                         fBoxDimensions.x(),  // dimensions (half-lengths)
0099                         fBoxDimensions.y(), fBoxDimensions.z());
0100 
0101   fBoxVolume = new G4LogicalVolume(sBox,  // its shape
0102                                    boxMaterial,  // its material
0103                                    "Box");  // its name
0104 
0105   new G4PVPlacement(0,  // no rotation
0106                     G4ThreeVector(),  // at (0,0,0)
0107                     fBoxVolume,  // its logical volume
0108                     "Box",  // its name
0109                     fWorldVolume,  // its mother  volume
0110                     false,  // no boolean operation
0111                     0);  // copy number
0112 
0113   // always return the root volume
0114   //
0115   return pWorld;
0116 }
0117 
0118 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
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 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
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 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0153 
0154 void DetectorConstruction::SetBoxDimensions(G4ThreeVector dimensions)
0155 {
0156   /// Set box dimension (in half lengths).
0157   /// This setting has effect only if called in PreInit> phase
0158 
0159   fBoxDimensions = dimensions;
0160 }
0161 
0162 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0163 
0164 void DetectorConstruction::SetWorldSizeFactor(G4double factor)
0165 {
0166   /// Set the multiplication factor from box dimensions to world dimensions.
0167   /// This setting has effect only if called in PreInit> phase
0168 
0169   fWorldSizeFactor = factor;
0170 }
0171 
0172 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0173 
0174 void DetectorConstruction::DefineCommands()
0175 {
0176   // Define /B5/detector command directory using generic messenger class
0177   fMessenger = new G4GenericMessenger(this, "/detector/", "Detector control");
0178 
0179   // setBoxMaterial command
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   // setWorldMaterial command
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   // setBoxDimensions command
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   // setWorldSizeFactor command
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 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0208 
0209 }  // namespace Common