Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:14:25

0001 //==========================================================================
0002 //  AIDA Detector description implementation 
0003 //--------------------------------------------------------------------------
0004 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
0005 // All rights reserved.
0006 //
0007 // For the licensing terms see $DD4hepINSTALL/LICENSE.
0008 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
0009 //
0010 // Author     : M.Frank
0011 //
0012 //==========================================================================
0013 
0014 // Framework include files
0015 #include <DD4hep/Plugins.h>
0016 #include <DD4hep/InstanceCount.h>
0017 #include <DDG4/Geant4Mapping.h>
0018 #include <DDG4/Geant4GeometryInfo.h>
0019 #include <DDG4/Geant4DetectorConstruction.h>
0020 
0021 #include <G4VUserDetectorConstruction.hh>
0022 #include <G4SDManager.hh>
0023 
0024 using namespace dd4hep::sim;
0025 
0026 /// Helper: Assign sensitive detector to logical volume
0027 void Geant4DetectorConstructionContext::setSensitiveDetector(G4LogicalVolume* vol, G4VSensitiveDetector* sd)  {
0028   //detector->SetSensitiveDetector(vol,sd);
0029   G4SDManager::GetSDMpointer()->AddNewDetector(sd);
0030   vol->SetSensitiveDetector(sd);
0031 }
0032 
0033 /// Standard Constructor
0034 Geant4DetectorConstruction::Geant4DetectorConstruction(Geant4Context* ctxt, const std::string& nam)
0035   : Geant4Action(ctxt,nam)
0036 {
0037 }
0038 
0039 /// Default destructor
0040 Geant4DetectorConstruction::~Geant4DetectorConstruction()  {
0041 }
0042 
0043 /// Create Geant4 sensitive detector object using the factory mechanism
0044 G4VSensitiveDetector*
0045 Geant4DetectorConstruction::createSensitiveDetector(const std::string& typ,
0046                                                     const std::string& nam)  {
0047   Detector& dsc = context()->detectorDescription();
0048   G4VSensitiveDetector* g4sd = PluginService::Create<G4VSensitiveDetector*>(typ, nam, &dsc);
0049   if( g4sd )  {
0050     print("+++ Subdetector: %-32s  type: %s.", nam.c_str(), typ.c_str());
0051   }
0052   else  {
0053     PluginDebug dbg;
0054     g4sd = PluginService::Create<G4VSensitiveDetector*>(typ, nam, &dsc);
0055     if ( !g4sd )  {
0056       except("createSensitiveDetector: FATAL Failed to "
0057              "create Geant4 sensitive detector %s of type %s.",
0058              nam.c_str(), typ.c_str());
0059     }
0060     print("+++ Subdetector: %-32s  type: %s.", nam.c_str(), typ.c_str());
0061   }
0062   if ( g4sd )  {
0063     g4sd->Activate(true);
0064     G4SDManager::GetSDMpointer()->AddNewDetector(g4sd);
0065   }
0066   return g4sd;
0067 }
0068 
0069 /// Geometry construction callback. Called at "Construct()"
0070 void Geant4DetectorConstruction::constructGeo(Geant4DetectorConstructionContext* )  {
0071 }
0072 
0073 /// Electromagnetic field construction callback. Called at "ConstructSDandField()"
0074 void Geant4DetectorConstruction::constructField(Geant4DetectorConstructionContext* )  {
0075 }
0076 
0077 /// Sensitive detector construction callback. Called at "ConstructSDandField()"
0078 void Geant4DetectorConstruction::constructSensitives(Geant4DetectorConstructionContext* )  {
0079 }
0080 
0081 /// Standard Constructor
0082 Geant4DetectorConstructionSequence::Geant4DetectorConstructionSequence(Geant4Context* ctxt, const std::string& nam)
0083   : Geant4Action(ctxt,nam)
0084 {
0085   m_needsControl = true;
0086   InstanceCount::increment(this);
0087 }
0088 
0089 /// Default destructor
0090 Geant4DetectorConstructionSequence::~Geant4DetectorConstructionSequence()  {
0091   m_actors(&Geant4DetectorConstruction::release);  
0092   InstanceCount::decrement(this);
0093 }
0094 
0095 /// Set or update client context
0096 void Geant4DetectorConstructionSequence::updateContext(Geant4Context* ctxt)  {
0097   m_context = ctxt;
0098   m_actors(&Geant4DetectorConstruction::updateContext,ctxt);  
0099 }
0100 
0101 /// Add an actor responding to all callbacks. Sequence takes ownership.
0102 void Geant4DetectorConstructionSequence::adopt(Geant4DetectorConstruction* action)  {
0103   if (action) {
0104     action->addRef();
0105     m_actors.add(action);
0106     return;
0107   }
0108   except("++ Attempt to add an invalid actor!");
0109 }
0110 
0111 /// Access an actor by name
0112 Geant4DetectorConstruction* Geant4DetectorConstructionSequence::get(const std::string& nam)  const  {
0113   for(auto* i : m_actors)  {
0114     if ( i->name() == nam )  {
0115       return i;
0116     }
0117   }
0118   except("++ Attempt to access invalid actor %s!",nam.c_str());
0119   return nullptr;
0120 }
0121 
0122 /// Geometry construction callback. Called at "Construct()"
0123 void Geant4DetectorConstructionSequence::constructGeo(Geant4DetectorConstructionContext* ctxt)  {
0124   m_actors(&Geant4DetectorConstruction::constructGeo, ctxt);  
0125 }
0126 
0127 /// Electromagnetic field construction callback. Called at "ConstructSDandField()"
0128 void Geant4DetectorConstructionSequence::constructField(Geant4DetectorConstructionContext* ctxt)  {
0129   m_actors(&Geant4DetectorConstruction::constructField, ctxt);  
0130 }
0131 
0132 /// Sensitive detector construction callback. Called at "ConstructSDandField()"
0133 void Geant4DetectorConstructionSequence::constructSensitives(Geant4DetectorConstructionContext* ctxt)  {
0134   m_actors(&Geant4DetectorConstruction::constructSensitives, ctxt);  
0135 }
0136 
0137 /// Access to the converted regions
0138 const std::map<dd4hep::Region, G4Region*>&
0139 Geant4DetectorConstructionSequence::regions() const  {
0140   Geant4GeometryInfo* p = Geant4Mapping::instance().ptr();
0141   if ( p ) return p->g4Regions;
0142   throw std::runtime_error("+++ Geant4DetectorConstructionSequence::regions: Access not possible. Geometry is not yet converted!");
0143 }
0144 
0145 /// Access to the converted volumes
0146 const std::map<dd4hep::Volume, G4LogicalVolume*>&
0147 Geant4DetectorConstructionSequence::volumes() const  {
0148   Geant4GeometryInfo* p = Geant4Mapping::instance().ptr();
0149   if ( p ) return p->g4Volumes;
0150   throw std::runtime_error("+++ Geant4DetectorConstructionSequence::volumes: Access not possible. Geometry is not yet converted!");
0151 }
0152 
0153 /// Access to the converted shapes
0154 const std::map<const TGeoShape*, G4VSolid*>& Geant4DetectorConstructionSequence::shapes() const  {
0155   Geant4GeometryInfo* p = Geant4Mapping::instance().ptr();
0156   if ( p ) return p->g4Solids;
0157   throw std::runtime_error("+++ Geant4DetectorConstructionSequence::shapes: Access not possible. Geometry is not yet converted!");
0158 }
0159 
0160 /// Access to the converted limit sets
0161 const std::map<dd4hep::LimitSet, G4UserLimits*>&
0162 Geant4DetectorConstructionSequence::limits() const  {
0163   Geant4GeometryInfo* p = Geant4Mapping::instance().ptr();
0164   if ( p ) return p->g4Limits;
0165   throw std::runtime_error("+++ Geant4DetectorConstructionSequence::limits: Access not possible. Geometry is not yet converted!");
0166 }
0167 
0168 /// Access to the converted assemblies
0169 const std::map<dd4hep::PlacedVolume, Geant4AssemblyVolume*>&
0170 Geant4DetectorConstructionSequence::assemblies() const  {
0171   Geant4GeometryInfo* p = Geant4Mapping::instance().ptr();
0172   if ( p ) return p->g4AssemblyVolumes;
0173   throw std::runtime_error("+++ Geant4DetectorConstructionSequence::assemblies: Access not possible. Geometry is not yet converted!");
0174 }
0175 
0176 /// Access to the converted placements
0177 const std::map<dd4hep::PlacedVolume, G4VPhysicalVolume*>&
0178 Geant4DetectorConstructionSequence::placements() const  {
0179   Geant4GeometryInfo* p = Geant4Mapping::instance().ptr();
0180   if ( p ) return p->g4Placements;
0181   throw std::runtime_error("+++ Geant4DetectorConstructionSequence::placements: Access not possible. Geometry is not yet converted!");
0182 }
0183 
0184 /// Access to the converted materials
0185 const Geant4GeometryMaps::MaterialMap& Geant4DetectorConstructionSequence::materials() const  {
0186   Geant4GeometryInfo* p = Geant4Mapping::instance().ptr();
0187   if ( p ) return p->g4Materials;
0188   throw std::runtime_error("+++ Geant4DetectorConstructionSequence::materials: Access not possible. Geometry is not yet converted!");
0189 }
0190 
0191 /// Access to the converted elements
0192 const Geant4GeometryMaps::ElementMap& Geant4DetectorConstructionSequence::elements() const  {
0193   Geant4GeometryInfo* p = Geant4Mapping::instance().ptr();
0194   if ( p ) return p->g4Elements;
0195   throw std::runtime_error("+++ Geant4DetectorConstructionSequence::elements: Access not possible. Geometry is not yet converted!");
0196 }
0197 
0198 
0199 
0200