File indexing completed on 2025-01-18 09:14:25
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #include <DDG4/Geant4GeneratorWrapper.h>
0016
0017 #include <DD4hep/InstanceCount.h>
0018 #include <DD4hep/Plugins.h>
0019 #include <DD4hep/Printout.h>
0020 #include <DDG4/Geant4Context.h>
0021 #include <DDG4/Geant4Primary.h>
0022 #include <DDG4/Geant4InputHandling.h>
0023
0024
0025 #include <G4Event.hh>
0026 #include <G4PrimaryVertex.hh>
0027 #include <G4PrimaryParticle.hh>
0028 #include <G4VPrimaryGenerator.hh>
0029
0030
0031 #include <stdexcept>
0032 #include <set>
0033
0034 using namespace dd4hep::sim;
0035
0036
0037 Geant4GeneratorWrapper::Geant4GeneratorWrapper(Geant4Context* ctxt, const std::string& nam)
0038 : Geant4GeneratorAction(ctxt,nam)
0039 {
0040 declareProperty("Uses", m_generatorType);
0041 declareProperty("Mask", m_mask = 1);
0042 InstanceCount::increment(this);
0043 }
0044
0045
0046 Geant4GeneratorWrapper::~Geant4GeneratorWrapper() {
0047 detail::deletePtr(m_generator);
0048 InstanceCount::decrement(this);
0049 }
0050
0051 G4VPrimaryGenerator* Geant4GeneratorWrapper::generator() {
0052 if ( 0 == m_generator ) {
0053 m_generator = PluginService::Create<G4VPrimaryGenerator*>(m_generatorType);
0054 if ( 0 == m_generator ) {
0055 PluginDebug dbg;
0056 m_generator = PluginService::Create<G4VPrimaryGenerator*>(m_generatorType);
0057 if ( !m_generator ) {
0058 except("Geant4GeneratorWrapper: FATAL Failed to create G4VPrimaryGenerator of type %s.",
0059 m_generatorType.c_str());
0060 }
0061 }
0062 }
0063 return m_generator;
0064 }
0065
0066
0067 void Geant4GeneratorWrapper::operator()(G4Event* event) {
0068 Geant4PrimaryEvent* prim = context()->event().extension<Geant4PrimaryEvent>();
0069 Geant4PrimaryMap* primaryMap = context()->event().extension<Geant4PrimaryMap>();
0070 std::set<G4PrimaryVertex*> primaries;
0071
0072
0073 generator()->GeneratePrimaryVertex(event);
0074
0075
0076 for(G4PrimaryVertex* v=event->GetPrimaryVertex(); v; v=v->GetNext())
0077 primaries.insert(v);
0078
0079
0080 Geant4PrimaryInteraction* inter = createPrimary(m_mask, primaryMap, primaries);
0081 prim->add(m_mask, inter);
0082 }