Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-24 07:41:36

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 RadioBio::DetectorConstruction class
0028 
0029 #include "DetectorConstruction.hh"
0030 
0031 #include "G4Box.hh"
0032 #include "G4GeometryManager.hh"
0033 #include "G4LogicalVolume.hh"
0034 #include "G4LogicalVolumeStore.hh"
0035 #include "G4Material.hh"
0036 #include "G4NistManager.hh"
0037 #include "G4PVPlacement.hh"
0038 #include "G4PhysicalVolumeStore.hh"
0039 #include "G4RunManager.hh"
0040 #include "G4SolidStore.hh"
0041 #include "G4SystemOfUnits.hh"
0042 #include "G4UnitsTable.hh"
0043 #include "G4VisAttributes.hh"
0044 
0045 #include "DetectorMessenger.hh"
0046 #include "VoxelizedSensitiveDetector.hh"
0047 
0048 #include <sstream>
0049 
0050 namespace RadioBio
0051 {
0052 
0053 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0054 
0055 DetectorConstruction::DetectorConstruction()
0056 {
0057   // Set the default box material
0058   SetMaterial("G4_WATER");
0059 
0060   // Create the messenger
0061   fDetectorMessenger = new DetectorMessenger(this);
0062 
0063   // Voxelize the detecctor with default size
0064   VoxelizedSensitiveDetector::CreateInstance(this, 0.1 * m, 1 * m, 1 * m);
0065 }
0066 
0067 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0068 
0069 DetectorConstruction::~DetectorConstruction()
0070 {
0071   delete fDetectorMessenger;
0072 }
0073 
0074 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0075 
0076 G4VPhysicalVolume* DetectorConstruction::Construct()
0077 {
0078   return ConstructVolumes();
0079 }
0080 
0081 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0082 
0083 void DetectorConstruction::ConstructSDandField()
0084 {
0085   VoxelizedSensitiveDetector::GetInstance()->ConstructSD();
0086 }
0087 
0088 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0089 
0090 G4VPhysicalVolume* DetectorConstruction::ConstructVolumes()
0091 {
0092   // Cleanup old geometry
0093   G4GeometryManager::GetInstance()->OpenGeometry();
0094   G4PhysicalVolumeStore::GetInstance()->Clean();
0095   G4LogicalVolumeStore::GetInstance()->Clean();
0096   G4SolidStore::GetInstance()->Clean();
0097 
0098   // Materials
0099   G4bool isotopes = false;
0100   G4Material* airNist = G4NistManager::Instance()->FindOrBuildMaterial("G4_AIR", isotopes);
0101 
0102   // Simulation world
0103   const G4double worldX = 400.0 * cm;
0104   const G4double worldY = 400.0 * cm;
0105   const G4double worldZ = 400.0 * cm;
0106 
0107   G4Box* sWorld = new G4Box("TreatmentRoom", worldX, worldY, worldZ);
0108 
0109   G4LogicalVolume* lWorld = new G4LogicalVolume(sWorld, airNist, "logicWorld", 0, 0, 0);
0110 
0111   pWorld = new G4PVPlacement(0, G4ThreeVector(), "physicsWorld", lWorld, 0, false, 0);
0112 
0113   // Create a visual attribute for the world
0114   G4VisAttributes* worldVisAttributes = new G4VisAttributes(G4Colour(0.0, 0.0, 1.0));  // Blue color
0115   worldVisAttributes->SetVisibility(true);  // Ensure visibility
0116   worldVisAttributes->SetForceWireframe(true);  // Optional: make it wireframe for clarity
0117   lWorld->SetVisAttributes(worldVisAttributes);
0118 
0119   G4Box* sBox = new G4Box("Container", fBoxSizeX / 2, fBoxSizeY / 2, fBoxSizeZ / 2);
0120 
0121   fLBox = new G4LogicalVolume(sBox, fMaterial, fMaterial->GetName());
0122 
0123   fPBox = new G4PVPlacement(0, G4ThreeVector(), fLBox, fMaterial->GetName(), lWorld, false, 0);
0124 
0125   // Initialize pointer to world for voxelization
0126   VoxelizedSensitiveDetector::GetInstance()->InitializeWorldPtr(fPBox);
0127 
0128   // Create Voxelized Geometry
0129   VoxelizedSensitiveDetector::GetInstance()->Construct();
0130 
0131   // Always return the root volume
0132   return pWorld;
0133 }
0134 
0135 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0136 
0137 void DetectorConstruction::PrintParameters()
0138 {
0139   G4cout << "\n The Box dimensions are: " << G4endl
0140          << "x: " << G4BestUnit(fBoxSizeX, "Length") << G4endl
0141          << "y: " << G4BestUnit(fBoxSizeY, "Length") << G4endl
0142          << "z: " << G4BestUnit(fBoxSizeZ, "Length") << G4endl;
0143 
0144   G4cout << "And its volume therefore is: "
0145          << fPBox->GetLogicalVolume()->GetSolid()->GetCubicVolume() / m3 << " m3" << G4endl;
0146 }
0147 
0148 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0149 
0150 void DetectorConstruction::SetMaterial(G4String materialChoice)
0151 {
0152   G4Material* pttoMaterial = G4NistManager::Instance()->FindOrBuildMaterial(materialChoice);
0153 
0154   if (pttoMaterial) {
0155     if (fMaterial != pttoMaterial) {
0156       fMaterial = pttoMaterial;
0157       if (fLBox) {
0158         fLBox->SetMaterial(pttoMaterial);
0159       }
0160       G4RunManager::GetRunManager()->PhysicsHasBeenModified();
0161     }
0162   } else {
0163     std::stringstream sstr;
0164     sstr << "material " << +materialChoice << " does not exist, keeping material "
0165          << fMaterial->GetName();
0166 
0167     G4Exception("DetectorConstruction::SetMaterial", "NoWorldMat", JustWarning, sstr.str().c_str());
0168   }
0169 }
0170 
0171 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0172 
0173 void DetectorConstruction::SetSize(G4double value)
0174 {
0175   SetSizeX(value);
0176   SetSizeY(value);
0177   SetSizeZ(value);
0178 }
0179 
0180 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0181 
0182 void DetectorConstruction::SetSize(G4ThreeVector size)
0183 {
0184   SetSizeX(size.getX());
0185   SetSizeY(size.getY());
0186   SetSizeZ(size.getZ());
0187 }
0188 
0189 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0190 
0191 void DetectorConstruction::SetSizeX(G4double sizeX)
0192 {
0193   fBoxSizeX = sizeX;
0194 }
0195 
0196 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0197 
0198 void DetectorConstruction::SetSizeY(G4double sizeY)
0199 {
0200   fBoxSizeY = sizeY;
0201 }
0202 
0203 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0204 
0205 void DetectorConstruction::SetSizeZ(G4double sizeZ)
0206 {
0207   fBoxSizeZ = sizeZ;
0208 }
0209 
0210 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0211 
0212 }  // namespace RadioBio