Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-25 09:22:34

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 //  Gorad (Geant4 Open-source Radiation Analysis and Design)
0027 //
0028 //  Author : Makoto Asai (SLAC National Accelerator Laboratory)
0029 //
0030 //  Development of Gorad is funded by NASA Johnson Space Center (JSC)
0031 //  under the contract NNJ15HK11B.
0032 //
0033 // ********************************************************************
0034 //
0035 // GRDetectorConstruction.cc
0036 //   Read a GDML file to set up the geometry.
0037 //   This class also takes care of several utility methods on geometry
0038 //   and creates a parallel world for geometry importance biasing if
0039 //   requested.
0040 //
0041 // History
0042 //   September 8th, 2020 : first implementation
0043 //
0044 // ********************************************************************
0045 
0046 #include "GRDetectorConstruction.hh"
0047 
0048 #include "G4GDMLParser.hh"
0049 #include "GRDetectorConstructionMessenger.hh"
0050 #include "GRGeomImpBiasWorld.hh"
0051 
0052 #include "G4VPhysicalVolume.hh"
0053 #include "G4RunManager.hh"
0054 #include "G4LogicalVolume.hh"
0055 #include "G4Box.hh"
0056 #include <algorithm>
0057 
0058 G4double GRDetectorConstruction::worldSize = -1.;
0059 
0060 GRDetectorConstruction::GRDetectorConstruction()
0061 : gdmlFile("noName"), fWorld(nullptr), initialized(false)
0062 {
0063   messenger = new GRDetectorConstructionMessenger(this);
0064   parser = new G4GDMLParser();
0065 }
0066 
0067 GRDetectorConstruction::~GRDetectorConstruction()
0068 {
0069   delete messenger;
0070   delete parser;
0071 }
0072 
0073 G4VPhysicalVolume* GRDetectorConstruction::Construct()
0074 {
0075   if(!initialized)
0076   {
0077     Read();
0078     if(applyGeomImpBias)
0079     {
0080       G4String wName = "GeomBias";
0081       geomImpBiasWorld = new GRGeomImpBiasWorld(wName,this);
0082       RegisterParallelWorld(geomImpBiasWorld);
0083     }
0084   }
0085   return fWorld;
0086 }
0087 
0088 void GRDetectorConstruction::ConstructSDAndField()
0089 { ; }
0090 
0091 G4bool GRDetectorConstruction::SetGDMLFile(G4String& gdml)
0092 {
0093   G4bool valid = true; // parser->IsValid(gdml); ## GDML parser fix needed
0094   if(valid)
0095   {
0096     if(initialized)
0097     {
0098       parser->Clear();
0099       G4RunManager::GetRunManager()->ReinitializeGeometry(true);
0100     }
0101     gdmlFile = gdml;
0102   }
0103   return valid;
0104 }
0105 
0106 void GRDetectorConstruction::Read()
0107 {
0108   parser->Read(gdmlFile);
0109   fWorld = parser->GetWorldVolume();
0110   const G4Box* worldBox = dynamic_cast<const G4Box*>(fWorld->GetLogicalVolume()->GetSolid());
0111   if(!worldBox)
0112   {
0113     G4ExceptionDescription ed;
0114     ed << "PANIC!!! World volume defined in "<<gdmlFile<<" is not Box!!!";
0115     G4Exception("GRDetectorConstruction::Read()","GOoradGeom00012",FatalException,ed);
0116   }
0117   worldSize = std::min( {worldBox->GetXHalfLength(), worldBox->GetYHalfLength(), worldBox->GetZHalfLength()},
0118                         [](G4double a,G4double b) {return a<b;} );
0119   initialized = true;
0120 }
0121 
0122 #include "G4UnitsTable.hh"
0123 #include "G4VSolid.hh"
0124 #include "G4SolidStore.hh"
0125 void GRDetectorConstruction::ListSolids(G4int lvl)
0126 {
0127   G4cout << "*********** List of registered solids *************" << G4endl;
0128   auto store = G4SolidStore::GetInstance();
0129   auto itr = store->begin();
0130   for(;itr!=store->end();itr++)
0131   {
0132     switch(lvl)
0133     {
0134       case 0:
0135         G4cout << (*itr)->GetName() << G4endl;
0136         break;
0137       case 1:
0138         G4cout << (*itr)->GetName()
0139                << "\t volume = " << G4BestUnit((*itr)->GetCubicVolume(),"Volume")
0140                << "\t surface = " << G4BestUnit((*itr)->GetSurfaceArea(),"Surface")
0141                << G4endl;
0142         break;
0143       default:
0144         (*itr)->DumpInfo();
0145         break;
0146     }
0147   }
0148 }
0149 
0150 #include "G4LogicalVolume.hh"
0151 #include "G4LogicalVolumeStore.hh"
0152 #include "G4Material.hh"
0153 #include "G4VSensitiveDetector.hh"
0154 void GRDetectorConstruction::ListLogVols(G4int lvl)
0155 {
0156   G4cout << "*********** List of registered logical volumes *************" << G4endl;
0157   auto store = G4LogicalVolumeStore::GetInstance();
0158   auto itr = store->begin();
0159   for(;itr!=store->end();itr++)
0160   {
0161     G4cout << (*itr)->GetName() << "\t Solid = " << (*itr)->GetSolid()->GetName();
0162     if((*itr)->GetMaterial())
0163     { G4cout << "\t Material = " << (*itr)->GetMaterial()->GetName() << G4endl; }
0164     else
0165     { G4cout << "\t Material : not defined " << G4endl; }
0166     if(lvl<1) continue;
0167     G4cout << "\t region = ";
0168     if((*itr)->GetRegion())
0169     { G4cout << (*itr)->GetRegion()->GetName(); }
0170     else
0171     { G4cout << "not defined"; }
0172     G4cout << "\t sensitive detector = ";
0173     if((*itr)->GetSensitiveDetector())
0174     { G4cout << (*itr)->GetSensitiveDetector()->GetName(); }
0175     else
0176     { G4cout << "not defined"; }
0177     G4cout << G4endl;
0178     G4cout << "\t daughters = " << (*itr)->GetNoDaughters();
0179     if((*itr)->GetNoDaughters()>0)
0180     {
0181       switch((*itr)->CharacteriseDaughters())
0182       {
0183         case kNormal:
0184           G4cout << " (placement)"; break;
0185         case kReplica:
0186           G4cout << " (replica : " << (*itr)->GetDaughter(0)->GetMultiplicity() << ")"; break;
0187         case kParameterised:
0188           G4cout << " (parameterized : " << (*itr)->GetDaughter(0)->GetMultiplicity() << ")"; break;
0189         default:
0190           ;
0191       }
0192     }
0193     G4cout << G4endl;
0194     if(lvl<2) continue;
0195     if((*itr)->GetMaterial())
0196     { G4cout << "\t weight = " << G4BestUnit((*itr)->GetMass(),"Mass") << G4endl; }
0197     else
0198     { G4cout << "\t weight : not available" << G4endl; }
0199   }
0200 }
0201 
0202 #include "G4VPhysicalVolume.hh"
0203 #include "G4PhysicalVolumeStore.hh"
0204 void GRDetectorConstruction::ListPhysVols(G4int lvl)
0205 {
0206   G4cout << "*********** List of registered physical volumes *************" << G4endl;
0207   auto store = G4PhysicalVolumeStore::GetInstance();
0208   auto itr = store->begin();
0209   for(;itr!=store->end();itr++)
0210   {
0211     switch(lvl)
0212     {
0213       case 0:
0214         G4cout << (*itr)->GetName() << G4endl;
0215         break;
0216       case 1:
0217         G4cout << (*itr)->GetName()
0218                << "\t logical volume = " << (*itr)->GetLogicalVolume()->GetName()
0219                << "\t mother logical = ";
0220         if((*itr)->GetMotherLogical())
0221         { G4cout << (*itr)->GetMotherLogical()->GetName(); }
0222         else
0223         { G4cout << "not defined"; }
0224         G4cout << G4endl;
0225         break;
0226       default:
0227         G4cout << (*itr)->GetName()
0228                << "\t logical volume = " << (*itr)->GetLogicalVolume()->GetName()
0229                << "\t mother logical = ";
0230         if((*itr)->GetMotherLogical())
0231         { G4cout << (*itr)->GetMotherLogical()->GetName(); }
0232         else
0233         { G4cout << "not defined"; }
0234         G4cout << "\t type = ";
0235         switch((*itr)->VolumeType())
0236         {
0237           case kNormal:
0238             G4cout << "placement"; break;
0239           case kReplica:
0240             G4cout << "replica"; break;
0241           case kParameterised:
0242             G4cout << "parameterized"; break;
0243           default:
0244             ;
0245         }
0246         G4cout << G4endl;
0247     }
0248   }
0249 }
0250 
0251 G4bool GRDetectorConstruction::CheckOverlap(G4String& physVolName, G4int nSpots,
0252                  G4int maxErr, G4double tol)
0253 {
0254   G4cout << "*********** Checking overlap for <" << physVolName << "> *************" << G4endl;
0255   G4bool checkAll = (physVolName=="**ALL**");
0256   auto store = G4PhysicalVolumeStore::GetInstance();
0257   auto itr = store->begin();
0258   G4VPhysicalVolume* physVol = nullptr;
0259   for(;itr!=store->end();itr++)
0260   {
0261     if(checkAll || (*itr)->GetName()==physVolName)
0262     {
0263       physVol = (*itr);
0264       physVol->CheckOverlaps(nSpots,tol,true,maxErr);
0265       if(!checkAll) break;
0266     }
0267   }
0268   return (physVol!=nullptr);
0269 }
0270 
0271 #include "G4Region.hh"
0272 #include "G4RegionStore.hh"
0273 #include "G4RunManagerKernel.hh"
0274 void GRDetectorConstruction::ListRegions(G4int lvl)
0275 {
0276   if(lvl==2)
0277   {
0278     G4RunManagerKernel::GetRunManagerKernel()->DumpRegion();
0279     return;
0280   }
0281   G4cout << "*********** List of registered regions *************" << G4endl;
0282   auto store = G4RegionStore::GetInstance();
0283   auto itr = store->begin();
0284   for(;itr!=store->end();itr++)
0285   {
0286     G4cout << (*itr)->GetName();
0287     if((*itr)->GetWorldPhysical())
0288     {
0289       G4cout << "\t in the world volume <" << (*itr)->GetWorldPhysical()->GetName() << "> ";
0290       if((*itr)->IsInMassGeometry()) G4cout << "-- mass world";
0291       if((*itr)->IsInParallelGeometry()) G4cout << "-- parallel world";
0292     }
0293     else
0294     { G4cout << " -- is not associated to any world."; }
0295     G4cout << G4endl;
0296     if(lvl==0) continue;
0297     G4cout << "\t\t Root logical volume(s) : ";
0298     size_t nRootLV = (*itr)->GetNumberOfRootVolumes();
0299     std::vector<G4LogicalVolume*>::iterator lvItr = (*itr)->GetRootLogicalVolumeIterator();
0300     for(size_t j=0;j<nRootLV;j++)
0301     { G4cout << (*lvItr)->GetName() << " "; lvItr++; }
0302     G4cout << G4endl;
0303     G4cout << "\t\t Pointers : G4VUserRegionInformation[" << (*itr)->GetUserInformation()
0304            << "], G4UserLimits[" << (*itr)->GetUserLimits()
0305            << "], G4FastSimulationManager[" << (*itr)->GetFastSimulationManager()
0306            << "], G4UserSteppingAction[" << (*itr)->GetRegionalSteppingAction() << "]" << G4endl;
0307   }
0308 }
0309 
0310 G4bool GRDetectorConstruction::CreateRegion(G4String& regionName,G4String& logVolName)
0311 {
0312   auto logVolStore = G4LogicalVolumeStore::GetInstance();
0313   auto itr = logVolStore->begin();
0314   G4LogicalVolume* logVol = nullptr;
0315   for(;itr!=logVolStore->end();itr++)
0316   {
0317     if((*itr)->GetName() == logVolName)
0318     { logVol = (*itr); break; }
0319   }
0320   if(!logVol) return false;
0321 
0322   auto regionStore = G4RegionStore::GetInstance();
0323   auto region = regionStore->FindOrCreateRegion(regionName);
0324   logVol->SetRegion(region);
0325   region->AddRootLogicalVolume(logVol);
0326   return true;
0327 }
0328   
0329 #include "G4MaterialTable.hh"
0330 void GRDetectorConstruction::ListAllMaterial()
0331 {
0332   auto materialTable = G4Material::GetMaterialTable();
0333   auto matItr = materialTable->begin();
0334   G4cout << "*********** List of instantiated materials **************" << G4endl;
0335   G4int i = 0;
0336   for(;matItr!=materialTable->end();matItr++)
0337   {
0338     G4cout << (*matItr)->GetName() << "\t";
0339     if(++i%5==0) G4cout << G4endl;
0340   }
0341   G4cout << G4endl;
0342 }
0343 
0344 G4bool GRDetectorConstruction::ListMaterial(G4String& matName)
0345 {
0346   auto materialTable = G4Material::GetMaterialTable();
0347   auto matItr = materialTable->begin();
0348   for(;matItr!=materialTable->end();matItr++)
0349   {
0350     if((*matItr)->GetName()==matName) 
0351     {
0352       G4cout << *matItr << G4endl;
0353       return true;
0354     }
0355   }
0356   return false;
0357 }
0358 
0359 #include "G4NistManager.hh"
0360 void GRDetectorConstruction::DumpNistMaterials()
0361 {
0362   auto nameVec = G4NistManager::Instance()->GetNistMaterialNames();
0363   auto itr = nameVec.begin();
0364   G4int i = 0;
0365   for(;itr!=nameVec.end();itr++)
0366   {
0367     G4cout << std::setw(26) << *itr;
0368     if(++i%3==0) G4cout << G4endl;
0369   }
0370   G4cout << G4endl;
0371 }
0372 
0373 G4bool GRDetectorConstruction::CreateMaterial(G4String& matName)
0374 {
0375   auto mat = G4NistManager::Instance()->FindOrBuildMaterial(matName);
0376   return (mat!=nullptr);
0377 }
0378 
0379 G4bool GRDetectorConstruction::GetMaterial(G4String& logVol)
0380 {
0381   auto store = G4LogicalVolumeStore::GetInstance();
0382   std::vector<G4LogicalVolume*>::iterator itr = store->begin();
0383   for(;itr!=store->end();itr++)
0384   {
0385     if((*itr)->GetName()==logVol)
0386     {
0387       G4cout << "Logical volume <" << (*itr)->GetName() << "> is made of <"
0388            << (*itr)->GetMaterial()->GetName() << ">" << G4endl;
0389       return true;
0390     }
0391   }
0392   return false;
0393 }
0394 
0395 G4int GRDetectorConstruction::SetMaterial(G4String& logVolName,G4String& matName)
0396 {
0397   G4LogicalVolume* logVol = nullptr;
0398   G4Material* mat = nullptr;
0399 
0400   auto store = G4LogicalVolumeStore::GetInstance();
0401   auto itr = store->begin();
0402   for(;itr!=store->end();itr++)
0403   {
0404     if((*itr)->GetName()==logVolName)
0405     {
0406       logVol = *itr;
0407       break;
0408     }
0409   }
0410 
0411   auto materialTable = G4Material::GetMaterialTable();
0412   auto matItr = materialTable->begin();
0413   for(;matItr!=materialTable->end();matItr++)
0414   {
0415     if((*matItr)->GetName()==matName) 
0416     {
0417       mat = *matItr;
0418       break;
0419     }
0420   }
0421 
0422   G4int retVal = 0;
0423   if(!logVol && !mat) 
0424   { retVal = 3; }
0425   else if(!logVol)
0426   { retVal = 1; }
0427   else if(!mat)
0428   { retVal = 2; }
0429   else
0430   { logVol->SetMaterial(mat); }
0431 
0432   return retVal;
0433 }
0434 
0435