From 7c3e775f8af85eee009a09cdbd1353bb4e413ad0 Mon Sep 17 00:00:00 2001 From: Andre Sailer Date: Fri, 15 Dec 2023 11:15:32 +0100 Subject: [PATCH 1/4] Geant4PrimaryHandling: fix issue with multiple vertices in Geant4 GeneralParticleSource --- DDG4/src/Geant4GeneratorWrapper.cpp | 6 ++++-- DDG4/src/Geant4InputHandling.cpp | 5 +++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/DDG4/src/Geant4GeneratorWrapper.cpp b/DDG4/src/Geant4GeneratorWrapper.cpp index 7eb09b3f5..b79bed2f6 100644 --- a/DDG4/src/Geant4GeneratorWrapper.cpp +++ b/DDG4/src/Geant4GeneratorWrapper.cpp @@ -78,10 +78,12 @@ void Geant4GeneratorWrapper::operator()(G4Event* event) { generator()->GeneratePrimaryVertex(event); /// Add all the missing interactions (primary vertices) to the primary event record. + int maskCounter = 100000 + m_mask; for(G4PrimaryVertex* gv=event->GetPrimaryVertex(); gv; gv=gv->GetNext()) { if ( primaries.find(gv) == primaries.end() ) { - Geant4PrimaryInteraction* inter = createPrimary(m_mask, primaryMap, gv); - prim->add(m_mask, inter); + Geant4PrimaryInteraction* inter = createPrimary(maskCounter, primaryMap, gv); + prim->add(maskCounter, inter); + maskCounter += 1; } } } diff --git a/DDG4/src/Geant4InputHandling.cpp b/DDG4/src/Geant4InputHandling.cpp index fdc1468f5..e75a4f26f 100644 --- a/DDG4/src/Geant4InputHandling.cpp +++ b/DDG4/src/Geant4InputHandling.cpp @@ -180,13 +180,14 @@ static void appendInteraction(const Geant4Action* caller, } Geant4PrimaryInteraction::VertexMap::iterator ivfnd, iv, ivend; for( iv=input->vertices.begin(), ivend=input->vertices.end(); iv != ivend; ++iv ) { - ivfnd = output->vertices.find((*iv).first) ; //(*iv).second->mask); + int theMask = input->mask; + ivfnd = output->vertices.find(theMask); if ( ivfnd != output->vertices.end() ) { caller->abortRun("Duplicate primary interaction identifier!", "Cannot handle 2 interactions with identical identifiers!"); } for(Geant4Vertex* vtx : (*iv).second ) - output->vertices[(*iv).first].emplace_back( vtx->addRef() ); + output->vertices[theMask].emplace_back( vtx->addRef() ); } } From dea8e5c635dc012aed77970957833902ef52dc6b Mon Sep 17 00:00:00 2001 From: Andre Sailer Date: Fri, 15 Dec 2023 13:13:22 +0100 Subject: [PATCH 2/4] Geant4InputHandling: refactor one of the createPrimary to deal with multiple sources from GPS --- DDG4/include/DDG4/Geant4InputHandling.h | 2 +- DDG4/src/Geant4GeneratorWrapper.cpp | 12 +++--------- DDG4/src/Geant4InputHandling.cpp | 17 +++++++++-------- 3 files changed, 13 insertions(+), 18 deletions(-) diff --git a/DDG4/include/DDG4/Geant4InputHandling.h b/DDG4/include/DDG4/Geant4InputHandling.h index 3a889b6a5..2498b541e 100644 --- a/DDG4/include/DDG4/Geant4InputHandling.h +++ b/DDG4/include/DDG4/Geant4InputHandling.h @@ -42,7 +42,7 @@ namespace dd4hep { Geant4Particle* createPrimary(int particle_id, const Geant4Vertex* v, const G4PrimaryParticle* g4p); /// Create a DDG4 interaction record from a Geant4 interaction defined by a primary vertex - Geant4PrimaryInteraction* createPrimary(int mask, Geant4PrimaryMap* pm, const G4PrimaryVertex* gv); + Geant4PrimaryInteraction* createPrimary(int mask, Geant4PrimaryMap* pm, std::setconst& primaries); /// Initialize the generation of one event int generationInitialization(const Geant4Action* caller,const Geant4Context* context); diff --git a/DDG4/src/Geant4GeneratorWrapper.cpp b/DDG4/src/Geant4GeneratorWrapper.cpp index b79bed2f6..b40bec032 100644 --- a/DDG4/src/Geant4GeneratorWrapper.cpp +++ b/DDG4/src/Geant4GeneratorWrapper.cpp @@ -77,13 +77,7 @@ void Geant4GeneratorWrapper::operator()(G4Event* event) { // Now generate the new interaction generator()->GeneratePrimaryVertex(event); - /// Add all the missing interactions (primary vertices) to the primary event record. - int maskCounter = 100000 + m_mask; - for(G4PrimaryVertex* gv=event->GetPrimaryVertex(); gv; gv=gv->GetNext()) { - if ( primaries.find(gv) == primaries.end() ) { - Geant4PrimaryInteraction* inter = createPrimary(maskCounter, primaryMap, gv); - prim->add(maskCounter, inter); - maskCounter += 1; - } - } + // Add all the missing interactions (primary vertices) to the primary event record. + Geant4PrimaryInteraction* inter = createPrimary(m_mask, primaryMap, primaries); + prim->add(m_mask, inter); } diff --git a/DDG4/src/Geant4InputHandling.cpp b/DDG4/src/Geant4InputHandling.cpp index e75a4f26f..a741179a9 100644 --- a/DDG4/src/Geant4InputHandling.cpp +++ b/DDG4/src/Geant4InputHandling.cpp @@ -121,18 +121,19 @@ static void collectPrimaries(Geant4PrimaryMap* pm, Geant4PrimaryInteraction* dd4hep::sim::createPrimary(int mask, Geant4PrimaryMap* pm, - const G4PrimaryVertex* gv) + std::setconst& primaries) { Geant4PrimaryInteraction* interaction = new Geant4PrimaryInteraction(); - Geant4Vertex* v = createPrimary(gv); - int vid = int(interaction->vertices.size()); interaction->locked = true; interaction->mask = mask; - v->mask = mask; - interaction->vertices[vid].emplace_back(v); - - for (G4PrimaryParticle *gp = gv->GetPrimary(); gp; gp = gp->GetNext() ) - collectPrimaries(pm, interaction, v, gp); + for (auto const& gv: primaries) { + Geant4Vertex* v = createPrimary(gv); + v->mask = mask; + interaction->vertices[mask].emplace_back(v); + for (G4PrimaryParticle *gp = gv->GetPrimary(); gp; gp = gp->GetNext()) { + collectPrimaries(pm, interaction, v, gp); + } + } return interaction; } From 424f39ceab0761d21a43865f9809b4d382ac5797 Mon Sep 17 00:00:00 2001 From: Andre Sailer Date: Fri, 15 Dec 2023 13:16:55 +0100 Subject: [PATCH 3/4] Geant4InputHandling: collectPrimaries: use the mask to place the daughter vertices rather than size of the map Now every place where interaction->vertices map is being used, the mask is used as the key to the map, instead of something else --- DDG4/src/Geant4InputHandling.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/DDG4/src/Geant4InputHandling.cpp b/DDG4/src/Geant4InputHandling.cpp index a741179a9..279db4b6a 100644 --- a/DDG4/src/Geant4InputHandling.cpp +++ b/DDG4/src/Geant4InputHandling.cpp @@ -103,14 +103,13 @@ static void collectPrimaries(Geant4PrimaryMap* pm, if ( dau ) { Geant4Vertex* dv = new Geant4Vertex(*particle_origine); - int vid = int(interaction->vertices.size()); PropertyMask reason(p->reason); reason.set(G4PARTICLE_HAS_SECONDARIES); dv->mask = mask; dv->in.insert(p->id); - interaction->vertices[vid].emplace_back(dv) ; + interaction->vertices[mask].emplace_back(dv) ; for(; dau; dau = dau->GetNext()) collectPrimaries(pm, interaction, dv, dau); From fbb91b145042be6efcc8a6a4c3c41b8dcc6311bc Mon Sep 17 00:00:00 2001 From: Andre Sailer Date: Fri, 15 Dec 2023 17:04:40 +0100 Subject: [PATCH 4/4] Geant4GeneratorWrapper: collect vertices before putting them in the set Geant4InputHandling: prevent adding same particle more than once --- DDG4/src/Geant4GeneratorWrapper.cpp | 6 +++--- DDG4/src/Geant4InputHandling.cpp | 5 +++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/DDG4/src/Geant4GeneratorWrapper.cpp b/DDG4/src/Geant4GeneratorWrapper.cpp index b40bec032..858300e5f 100644 --- a/DDG4/src/Geant4GeneratorWrapper.cpp +++ b/DDG4/src/Geant4GeneratorWrapper.cpp @@ -70,13 +70,13 @@ void Geant4GeneratorWrapper::operator()(G4Event* event) { Geant4PrimaryMap* primaryMap = context()->event().extension(); set primaries; + // Now generate the new interaction + generator()->GeneratePrimaryVertex(event); + /// Collect all existing interactions (primary vertices) for(G4PrimaryVertex* v=event->GetPrimaryVertex(); v; v=v->GetNext()) primaries.insert(v); - // Now generate the new interaction - generator()->GeneratePrimaryVertex(event); - // Add all the missing interactions (primary vertices) to the primary event record. Geant4PrimaryInteraction* inter = createPrimary(m_mask, primaryMap, primaries); prim->add(m_mask, inter); diff --git a/DDG4/src/Geant4InputHandling.cpp b/DDG4/src/Geant4InputHandling.cpp index 279db4b6a..84c1a7a81 100644 --- a/DDG4/src/Geant4InputHandling.cpp +++ b/DDG4/src/Geant4InputHandling.cpp @@ -88,6 +88,11 @@ static void collectPrimaries(Geant4PrimaryMap* pm, Geant4Vertex* particle_origine, G4PrimaryParticle* gp) { + //if the particle is in the map, we do not have to do anything + if ( pm->get(gp) ) { + return; + } + int pid = int(interaction->particles.size()); Geant4Particle* p = createPrimary(pid,particle_origine,gp); G4PrimaryParticle* dau = gp->GetDaughter();