Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-23 09:21:19

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 //
0027 /// \file common/src/DetectorConstruction.cc
0028 /// \brief Implementation of the Common::DetectorConstruction class
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 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
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 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0057 
0058 DetectorConstruction::~DetectorConstruction()
0059 {
0060   delete fMessenger;
0061 }
0062 
0063 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0064 
0065 G4VPhysicalVolume* DetectorConstruction::Construct()
0066 {
0067   // Define materials via NIST manager
0068   //
0069   auto nistManager = G4NistManager::Instance();
0070 
0071   auto worldMaterial = nistManager->FindOrBuildMaterial(fWorldMaterialName);
0072   auto boxMaterial = nistManager->FindOrBuildMaterial(fBoxMaterialName);
0073 
0074   // Geometry parameters
0075   //
0076   G4ThreeVector worldDimensions = fBoxDimensions * fWorldSizeFactor;
0077 
0078   // World
0079   //
0080   auto sWorld = new G4Box("World",  // name
0081                           worldDimensions.x(),  // dimensions (half-lentghs)
0082                           worldDimensions.y(), worldDimensions.z());
0083 
0084   fWorldVolume = new G4LogicalVolume(sWorld,  // shape
0085                                      worldMaterial,  // material
0086                                      "World");  // name
0087 
0088   auto pWorld = new G4PVPlacement(0,  // no rotation
0089                                   G4ThreeVector(),  // at (0,0,0)
0090                                   fWorldVolume,  // logical volume
0091                                   "World",  // name
0092                                   0,  // mother  volume
0093                                   false,  // no boolean operation
0094                                   0);  // copy number
0095 
0096   // Box
0097   //
0098   auto sBox = new G4Box("Box",  // its name
0099                         fBoxDimensions.x(),  // dimensions (half-lengths)
0100                         fBoxDimensions.y(), fBoxDimensions.z());
0101 
0102   fBoxVolume = new G4LogicalVolume(sBox,  // its shape
0103                                    boxMaterial,  // its material
0104                                    "Box");  // its name
0105 
0106   new G4PVPlacement(0,  // no rotation
0107                     G4ThreeVector(),  // at (0,0,0)
0108                     fBoxVolume,  // its logical volume
0109                     "Box",  // its name
0110                     fWorldVolume,  // its mother  volume
0111                     false,  // no boolean operation
0112                     0);  // copy number
0113 
0114   // always return the root volume
0115   //
0116   return pWorld;
0117 }
0118 
0119 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
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 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
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 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0154 
0155 void DetectorConstruction::SetBoxDimensions(G4ThreeVector dimensions)
0156 {
0157   /// Set box dimension (in half lengths).
0158   /// This setting has effect only if called in PreInit> phase
0159 
0160   fBoxDimensions = dimensions;
0161 }
0162 
0163 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0164 
0165 void DetectorConstruction::SetWorldSizeFactor(G4double factor)
0166 {
0167   /// Set the multiplication factor from box dimensions to world dimensions.
0168   /// This setting has effect only if called in PreInit> phase
0169 
0170   fWorldSizeFactor = factor;
0171 }
0172 
0173 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0174 
0175 void DetectorConstruction::DefineCommands()
0176 {
0177   // Define /B5/detector command directory using generic messenger class
0178   fMessenger = new G4GenericMessenger(this, "/detector/", "Detector control");
0179 
0180   // setBoxMaterial command
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   // setWorldMaterial command
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   // setBoxDimensions command
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   // setWorldSizeFactor command
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 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0209 
0210 }  // namespace Common