Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-11 08:29: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 DetectorConstruction class
0028 
0029 #include "DetectorConstruction.hh"
0030 
0031 #include "DetectorMessenger.hh"
0032 
0033 #include "G4Box.hh"
0034 #include "G4GDMLParser.hh"
0035 #include "G4GeometryManager.hh"
0036 #include "G4LogicalVolume.hh"
0037 #include "G4LogicalVolumeStore.hh"
0038 #include "G4Material.hh"
0039 #include "G4PVPlacement.hh"
0040 #include "G4PhysicalVolumeStore.hh"
0041 #include "G4PropagatorInField.hh"
0042 #include "G4RunManager.hh"
0043 #include "G4SolidStore.hh"
0044 #include "G4SystemOfUnits.hh"
0045 #include "G4TransportationManager.hh"
0046 #include "G4UnitsTable.hh"
0047 #include "G4UserLimits.hh"
0048 
0049 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0050 
0051 DetectorConstruction::DetectorConstruction() : G4VUserDetectorConstruction(), fBoxSize(500 * m)
0052 {
0053   DefineMaterials();
0054   SetMaterial("Iron");
0055 
0056   // create UserLimits
0057   fUserLimits = new G4UserLimits();
0058 
0059   // create commands for interactive definition of the detector
0060   fDetectorMessenger = new DetectorMessenger(this);
0061 }
0062 
0063 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0064 
0065 G4VPhysicalVolume* DetectorConstruction::Construct()
0066 {
0067   return ConstructVolumes();
0068 }
0069 
0070 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0071 
0072 void DetectorConstruction::DefineMaterials()
0073 {
0074   G4double a, z, density;
0075 
0076   new G4Material("Beryllium", z = 4., a = 9.012182 * g / mole, density = 1.848 * g / cm3);
0077   new G4Material("Carbon", z = 6., a = 12.011 * g / mole, density = 2.265 * g / cm3);
0078   new G4Material("Iron", z = 26., a = 55.85 * g / mole, density = 7.870 * g / cm3);
0079 
0080   // define a vacuum with a restgas pressure  typical for accelerators
0081   G4double const Torr = atmosphere / 760.;  // 1 Torr
0082   G4double pressure = 10e-9 * Torr,
0083            temperature = 296.150 * kelvin;  // 23  Celsius
0084   new G4Material("Vacuum", z = 7., a = 14.01 * g / mole, density = 1.516784e-11 * kg / m3,
0085                  kStateGas, temperature, pressure);
0086   G4cout << *(G4Material::GetMaterialTable()) << G4endl;  // print the materials defined
0087 }
0088 
0089 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0090 
0091 G4VPhysicalVolume* DetectorConstruction::ConstructVolumes()
0092 {
0093   G4GeometryManager::GetInstance()->OpenGeometry();
0094   G4PhysicalVolumeStore::GetInstance()->Clean();
0095   G4LogicalVolumeStore::GetInstance()->Clean();
0096   G4SolidStore::GetInstance()->Clean();
0097 
0098   if (fGeomFileName == G4String()) {
0099     auto sBox = new G4Box("Container", fBoxSize / 2, fBoxSize / 2, fBoxSize / 2);
0100     fLBox = new G4LogicalVolume(sBox, fMaterial, fMaterial->GetName());
0101     fLBox->SetUserLimits(fUserLimits);
0102     fBox = new G4PVPlacement(0, G4ThreeVector(), fLBox, fMaterial->GetName(), 0, false, 0);
0103     PrintParameters();
0104   }
0105   else {
0106     G4GDMLParser parser;
0107     std::size_t nmat = G4Material::GetNumberOfMaterials();
0108     parser.Read(fGeomFileName.c_str());  // volumes and materials
0109     if (G4Material::GetNumberOfMaterials() > nmat) {
0110       const std::vector<G4Material*> MatPtrVec = *G4Material::GetMaterialTable();
0111       if (G4Material::GetNumberOfMaterials() > nmat)
0112         G4cout << "Materials defined by " << fGeomFileName << " :" << G4endl;
0113       for (std::size_t imat = nmat; imat < MatPtrVec.size(); ++imat)
0114         G4cout << MatPtrVec[imat] << G4endl;
0115     }
0116     fBox = parser.GetWorldVolume();
0117   }
0118   return fBox;
0119 }
0120 
0121 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0122 
0123 void DetectorConstruction::PrintParameters()
0124 {
0125   G4cout << "\n The Box is " << G4BestUnit(fBoxSize, "Length") << " of " << fMaterial->GetName()
0126          << G4endl;
0127 }
0128 
0129 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0130 
0131 void DetectorConstruction::SetMaterial(G4String materialChoice)
0132 {
0133   // search the material by its name
0134   G4Material* pttoMaterial = G4Material::GetMaterial(materialChoice);
0135   if (pttoMaterial) {
0136     fMaterial = pttoMaterial;
0137     if (fLBox) fLBox->SetMaterial(fMaterial);
0138   }
0139   G4RunManager::GetRunManager()->PhysicsHasBeenModified();
0140 }
0141 
0142 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0143 
0144 void DetectorConstruction::SetSize(G4double value)
0145 {
0146   fBoxSize = value;
0147   G4RunManager::GetRunManager()->ReinitializeGeometry();
0148 }
0149 
0150 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0151 
0152 #include "G4AutoDelete.hh"
0153 #include "G4GlobalMagFieldMessenger.hh"
0154 
0155 void DetectorConstruction::ConstructSDandField()
0156 {
0157   if (fFieldMessenger.Get() == 0) {
0158     // Create global magnetic field messenger.
0159     // Uniform magnetic field is then created automatically if
0160     // the field value is not zero.
0161     G4ThreeVector fieldValue = G4ThreeVector();
0162     auto msg = new G4GlobalMagFieldMessenger(fieldValue);
0163     // msg->SetVerboseLevel(1);
0164     G4AutoDelete::Register(msg);
0165     fFieldMessenger.Put(msg);
0166   }
0167 }
0168 
0169 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0170 
0171 void DetectorConstruction::SetMaxStepSize(G4double val)
0172 {
0173   // set the maximum allowed step size
0174   //
0175   if (val <= DBL_MIN) {
0176     G4cout << "\n --->warning from SetMaxStepSize: maxStep " << val
0177            << " out of range. Command refused" << G4endl;
0178     return;
0179   }
0180   fUserLimits->SetMaxAllowedStep(val);
0181 }
0182 
0183 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0184 
0185 void DetectorConstruction::SetMaxStepLength(G4double val)
0186 {
0187   // set the maximum length of tracking step
0188   //
0189   if (val <= DBL_MIN) {
0190     G4cout << "\n --->warning from SetMaxStepLength: maxStep " << val
0191            << " out of range. Command refused" << G4endl;
0192     return;
0193   }
0194   G4TransportationManager* tmanager = G4TransportationManager::GetTransportationManager();
0195   tmanager->GetPropagatorInField()->SetLargestAcceptableStep(val);
0196 }
0197 
0198 void DetectorConstruction::SetGeomFileName(G4String GeomFileName)
0199 {
0200   fGeomFileName = GeomFileName;
0201 }
0202 
0203 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......