Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-06-13 07:53:59

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 B2b::DetectorConstruction class
0028 
0029 #include "DetectorConstruction.hh"
0030 
0031 #include "ChamberParameterisation.hh"
0032 #include "DetectorMessenger.hh"
0033 #include "TrackerSD.hh"
0034 
0035 #include "G4AutoDelete.hh"
0036 #include "G4Box.hh"
0037 #include "G4Colour.hh"
0038 #include "G4GeometryManager.hh"
0039 #include "G4GeometryTolerance.hh"
0040 #include "G4GlobalMagFieldMessenger.hh"
0041 #include "G4LogicalVolume.hh"
0042 #include "G4Material.hh"
0043 #include "G4NistManager.hh"
0044 #include "G4PVParameterised.hh"
0045 #include "G4PVPlacement.hh"
0046 #include "G4SDManager.hh"
0047 #include "G4SystemOfUnits.hh"
0048 #include "G4ThreeVector.hh"
0049 #include "G4Tubs.hh"
0050 #include "G4UserLimits.hh"
0051 #include "G4VisAttributes.hh"
0052 
0053 using namespace B2;
0054 
0055 namespace B2b
0056 {
0057 
0058 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0059 
0060 G4ThreadLocal G4GlobalMagFieldMessenger* DetectorConstruction::fMagFieldMessenger = nullptr;
0061 
0062 DetectorConstruction::DetectorConstruction()
0063 {
0064   fMessenger = new DetectorMessenger(this);
0065 }
0066 
0067 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0068 
0069 DetectorConstruction::~DetectorConstruction()
0070 {
0071   delete fStepLimit;
0072   delete fMessenger;
0073 }
0074 
0075 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0076 
0077 G4VPhysicalVolume* DetectorConstruction::Construct()
0078 {
0079   // Define materials
0080   DefineMaterials();
0081 
0082   // Define volumes
0083   return DefineVolumes();
0084 }
0085 
0086 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0087 
0088 void DetectorConstruction::DefineMaterials()
0089 {
0090   // Material definition
0091 
0092   G4NistManager* nistManager = G4NistManager::Instance();
0093 
0094   // Air defined using NIST Manager
0095   nistManager->FindOrBuildMaterial("G4_AIR");
0096 
0097   // Lead defined using NIST Manager
0098   fTargetMaterial = nistManager->FindOrBuildMaterial("G4_Pb");
0099 
0100   // Xenon gas defined using NIST Manager
0101   fChamberMaterial = nistManager->FindOrBuildMaterial("G4_Xe");
0102 
0103   // Print materials
0104   G4cout << *(G4Material::GetMaterialTable()) << G4endl;
0105 }
0106 
0107 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0108 
0109 G4VPhysicalVolume* DetectorConstruction::DefineVolumes()
0110 {
0111   G4Material* air = G4Material::GetMaterial("G4_AIR");
0112 
0113   // Sizes of the principal geometrical components (solids)
0114 
0115   G4int NbOfChambers = 5;
0116   G4double chamberSpacing = 80 * cm;  // from chamber center to center!
0117 
0118   G4double chamberWidth = 20.0 * cm;  // width of the chambers
0119   G4double targetLength = 5.0 * cm;  // full length of Target
0120 
0121   G4double trackerLength = (NbOfChambers + 1) * chamberSpacing;
0122 
0123   G4double worldLength = 1.2 * (2 * targetLength + trackerLength);
0124 
0125   G4double targetRadius = 0.5 * targetLength;  // Radius of Target
0126   targetLength = 0.5 * targetLength;  // Half length of the Target
0127   G4double trackerSize = 0.5 * trackerLength;  // Half length of the Tracker
0128 
0129   // Definitions of Solids, Logical Volumes, Physical Volumes
0130 
0131   // World
0132 
0133   G4GeometryManager::GetInstance()->SetWorldMaximumExtent(worldLength);
0134 
0135   G4cout << "Computed tolerance = "
0136          << G4GeometryTolerance::GetInstance()->GetSurfaceTolerance() / mm << " mm" << G4endl;
0137 
0138   auto worldS = new G4Box("world",  // its name
0139                           worldLength / 2, worldLength / 2, worldLength / 2);  // its size
0140   auto worldLV = new G4LogicalVolume(worldS,  // its solid
0141                                      air,  // its material
0142                                      "World");  // its name
0143 
0144   //  Must place the World Physical volume unrotated at (0,0,0).
0145   //
0146   auto worldPV = new G4PVPlacement(nullptr,  // no rotation
0147                                    G4ThreeVector(),  // at (0,0,0)
0148                                    worldLV,  // its logical volume
0149                                    "World",  // its name
0150                                    nullptr,  // its mother  volume
0151                                    false,  // no boolean operations
0152                                    0,  // copy number
0153                                    fCheckOverlaps);  // checking overlaps
0154 
0155   // Target
0156 
0157   G4ThreeVector positionTarget = G4ThreeVector(0, 0, -(targetLength + trackerSize));
0158 
0159   auto targetS = new G4Tubs("target", 0., targetRadius, targetLength, 0. * deg, 360. * deg);
0160   fLogicTarget = new G4LogicalVolume(targetS, fTargetMaterial, "Target", nullptr, nullptr, nullptr);
0161   new G4PVPlacement(nullptr,  // no rotation
0162                     positionTarget,  // at (x,y,z)
0163                     fLogicTarget,  // its logical volume
0164                     "Target",  // its name
0165                     worldLV,  // its mother volume
0166                     false,  // no boolean operations
0167                     0,  // copy number
0168                     fCheckOverlaps);  // checking overlaps
0169 
0170   G4cout << "Target is " << 2 * targetLength / cm << " cm of " << fTargetMaterial->GetName()
0171          << G4endl;
0172 
0173   // Tracker
0174 
0175   G4ThreeVector positionTracker = G4ThreeVector(0, 0, 0);
0176 
0177   auto trackerS = new G4Tubs("tracker", 0, trackerSize, trackerSize, 0. * deg, 360. * deg);
0178   auto trackerLV = new G4LogicalVolume(trackerS, air, "Tracker", nullptr, nullptr, nullptr);
0179   new G4PVPlacement(nullptr,  // no rotation
0180                     positionTracker,  // at (x,y,z)
0181                     trackerLV,  // its logical volume
0182                     "Tracker",  // its name
0183                     worldLV,  // its mother  volume
0184                     false,  // no boolean operations
0185                     0,  // copy number
0186                     fCheckOverlaps);  // checking overlaps
0187 
0188   // Tracker segments
0189 
0190   // An example of Parameterised volumes
0191   // Dummy values for G4Tubs -- modified by parameterised volume
0192 
0193   auto chamberS = new G4Tubs("tracker", 0, 100 * cm, 100 * cm, 0. * deg, 360. * deg);
0194   fLogicChamber =
0195     new G4LogicalVolume(chamberS, fChamberMaterial, "Chamber", nullptr, nullptr, nullptr);
0196 
0197   G4double firstPosition = -trackerSize + chamberSpacing;
0198   G4double firstLength = trackerLength / 10;
0199   G4double lastLength = trackerLength;
0200 
0201   G4VPVParameterisation* chamberParam =
0202     new ChamberParameterisation(NbOfChambers,  // NoChambers
0203                                 firstPosition,  // Z of center of first
0204                                 chamberSpacing,  // Z spacing of centers
0205                                 chamberWidth,  // chamber width
0206                                 firstLength,  // initial length
0207                                 lastLength);  // final length
0208 
0209   // dummy value : kZAxis -- modified by parameterised volume
0210 
0211   new G4PVParameterised("Chamber",  // their name
0212                         fLogicChamber,  // their logical volume
0213                         trackerLV,  // Mother logical volume
0214                         kZAxis,  // Are placed along this axis
0215                         NbOfChambers,  // Number of chambers
0216                         chamberParam,  // The parametrisation
0217                         fCheckOverlaps);  // checking overlaps
0218 
0219   G4cout << "There are " << NbOfChambers << " chambers in the tracker region. " << G4endl
0220          << "The chambers are " << chamberWidth / cm << " cm of " << fChamberMaterial->GetName()
0221          << G4endl << "The distance between chamber is " << chamberSpacing / cm << " cm" << G4endl;
0222 
0223   // Visualization attributes
0224 
0225   G4VisAttributes boxVisAtt(G4Colour::White());
0226 
0227   worldLV->SetVisAttributes(boxVisAtt);
0228   fLogicTarget->SetVisAttributes(boxVisAtt);
0229   trackerLV->SetVisAttributes(boxVisAtt);
0230 
0231   G4VisAttributes chamberVisAtt(G4Colour::Yellow());
0232   fLogicChamber->SetVisAttributes(chamberVisAtt);
0233 
0234   // Example of User Limits
0235   //
0236   // Below is an example of how to set tracking constraints in a given
0237   // logical volume
0238   //
0239   // Sets a max step length in the tracker region, with G4StepLimiter
0240 
0241   G4double maxStep = 0.5 * chamberWidth;
0242   fStepLimit = new G4UserLimits(maxStep);
0243   trackerLV->SetUserLimits(fStepLimit);
0244 
0245   /// Set additional contraints on the track, with G4UserSpecialCuts
0246   ///
0247   /// G4double maxLength = 2*trackerLength, maxTime = 0.1*ns, minEkin = 10*MeV;
0248   /// trackerLV->SetUserLimits(new G4UserLimits(maxStep,
0249   ///                                           maxLength,
0250   ///                                           maxTime,
0251   ///                                           minEkin));
0252 
0253   // Always return the physical world
0254 
0255   return worldPV;
0256 }
0257 
0258 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0259 
0260 void DetectorConstruction::ConstructSDandField()
0261 {
0262   // Sensitive detectors
0263 
0264   G4String trackerChamberSDname = "B2/TrackerChamberSD";
0265   auto trackerSD = new TrackerSD(trackerChamberSDname, "TrackerHitsCollection");
0266   G4SDManager::GetSDMpointer()->AddNewDetector(trackerSD);
0267   SetSensitiveDetector(fLogicChamber, trackerSD);
0268 
0269   // Create global magnetic field messenger.
0270   // Uniform magnetic field is then created automatically if
0271   // the field value is not zero.
0272   G4ThreeVector fieldValue = G4ThreeVector();
0273   fMagFieldMessenger = new G4GlobalMagFieldMessenger(fieldValue);
0274   fMagFieldMessenger->SetVerboseLevel(1);
0275 
0276   // Register the field messenger for deleting
0277   G4AutoDelete::Register(fMagFieldMessenger);
0278 }
0279 
0280 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0281 
0282 void DetectorConstruction::SetTargetMaterial(G4String materialName)
0283 {
0284   G4NistManager* nistManager = G4NistManager::Instance();
0285 
0286   G4Material* pttoMaterial = nistManager->FindOrBuildMaterial(materialName);
0287 
0288   if (fTargetMaterial != pttoMaterial) {
0289     if (pttoMaterial) {
0290       fTargetMaterial = pttoMaterial;
0291       if (fLogicTarget) fLogicTarget->SetMaterial(fTargetMaterial);
0292       G4cout << G4endl << "----> The target is made of " << materialName << G4endl;
0293     }
0294     else {
0295       G4cout << G4endl << "-->  WARNING from SetTargetMaterial : " << materialName << " not found"
0296              << G4endl;
0297     }
0298   }
0299 }
0300 
0301 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0302 
0303 void DetectorConstruction::SetChamberMaterial(G4String materialName)
0304 {
0305   G4NistManager* nistManager = G4NistManager::Instance();
0306 
0307   G4Material* pttoMaterial = nistManager->FindOrBuildMaterial(materialName);
0308 
0309   if (fChamberMaterial != pttoMaterial) {
0310     if (pttoMaterial) {
0311       fChamberMaterial = pttoMaterial;
0312       if (fLogicChamber) fLogicChamber->SetMaterial(fChamberMaterial);
0313       G4cout << G4endl << "----> The chambers are made of " << materialName << G4endl;
0314     }
0315     else {
0316       G4cout << G4endl << "-->  WARNING from SetChamberMaterial : " << materialName << " not found"
0317              << G4endl;
0318     }
0319   }
0320 }
0321 
0322 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0323 
0324 void DetectorConstruction::SetMaxStep(G4double maxStep)
0325 {
0326   if ((fStepLimit) && (maxStep > 0.)) fStepLimit->SetMaxAllowedStep(maxStep);
0327 }
0328 
0329 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0330 
0331 }  // namespace B2b