Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Geant4PrimaryHandling: fix issue with multiple vertices in Geant4 GeneralParticleSource #1205

Merged
merged 4 commits into from
Dec 15, 2023

Conversation

andresailer
Copy link
Member

@andresailer andresailer commented Dec 15, 2023

BEGINRELEASENOTES

ENDRELEASENOTES

This is a little bit hacky, but as long as no-one goes over 10k primary sources we are OK.

There are actually two issues: One already pointed out in #1204
which is triggered in

       prim->add(m_mask, inter);

Because we cannot add multiple vertices with the same mask

And with the m_mask replaced by an increasing maskCounter, we run into

/// Import a Geant4 interaction defined by a primary vertex into a DDG4 interaction record
Geant4PrimaryInteraction*
dd4hep::sim::createPrimary(int mask,
Geant4PrimaryMap* pm,
const G4PrimaryVertex* gv)
{
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);
return interaction;
}

Where vid is always 0, because we take the length of a just created vector.

Better fixes would probably to switch the loops around in the Geant4GeneratorWrapper to create one interaction, which has multiple vertices, instead of multiple interactions with one vertex each.

But I want to see if these changes cause any other test failures as well, so I already open the PR

@MarkusFrankATcernch
Copy link
Contributor

MarkusFrankATcernch commented Dec 15, 2023

@andresailer
This solution may destroys entirely the MC history mechanism as soon as there are multiple interactions.
For the particle provenance every source must have it's own mask, independent of the primaries of each source.
If this is not the case, any later relationship between hits and particles for e.g. spill-over or multiple interactions
is just void.

-- Why is it not possible to add multiple primary vertices per interaction ?
I cannot really see why.
-- If every GPS has its own mask, which it MUST have, this should work out just fine. Each GPS will be associated to a primary interaction and all particle contributions after simulation can be clearly disentangled.

Please do not merge.

@andresailer
Copy link
Member Author

Hi @MarkusFrankATcernch

Thanks for your comments! But could you be more specific about where in the code you see an issue?

For the particle provenance every source must have it's own mask, independent of the primaries of each source.

Every source still has its own mask, just that the GPS one has multiple masks now, or did I mess something up with my changes there?

If this is not the case, any later relationship between hits and particles for e.g. spill-over or multiple interactions is just void.

There are still checks in the code that prevent multiple interactions with the same mask from happening.

Why is it not possible to add multiple primary vertices per interaction? I cannot really see why.

Just more re-writing of the code, which has possibly other implications elsewhere.

If every GPS has its own mask, which it MUST have, this should work out just fine. Each GPS will be associated to a primary interaction and all particle contributions after simulation can be clearly disentangled.

Maybe we need to define what we understand with GPS? The DD4hep incarnation of the GPS, or the Geant4 setup for the GPS? In the example macro from #1205 There is one Geant4 GPS with two different sources.

Please do not merge.

No worries!

Copy link

github-actions bot commented Dec 15, 2023

Test Results

       6 files         6 suites   7h 1m 39s ⏱️
   356 tests    356 ✔️ 0 💤 0
1 058 runs  1 058 ✔️ 0 💤 0

Results for commit fbb91b1.

♻️ This comment has been updated with latest results.

…hter 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
@MarkusFrankATcernch
Copy link
Contributor

@andresailer
I had some chat with Alvaro. So the result of these was:

  • He wanted to shoot two(or more) close particles into the detector ans see which separation he can resolve.
  • To solve this he constructed for Geant4 a combined particle source being the union of two individual particle sources.
  • If he would have done this purely inside dd4hep, he would not even have seen this deficiency.

But he did not....

  • In dd4hep the combined source appears as one single source giving multiple primary vertices. However, since it produces (unforeseen by me initially) multiple primary vertices, these get all attached to a primary interaction with the same mask. This effectively caused the exception.
  • From the dd4hep philosophy one should assign multiple primary vertices of one source (even if this source is the union of other sources) to one primary interaction. Here is actually a bug present in dd4hep.

Now that I have all this explained and wanted to have a cross-check on the code, I see that you have already implemented all this according to the ideas above :-).....

@andresailer
Copy link
Member Author

But something broke in t_Persist_CLICSiD_Geant4_LONGTEST

Geant4InputHandling: prevent adding same particle more than once
@andresailer andresailer merged commit ec32498 into AIDASoft:master Dec 15, 2023
14 checks passed
@andresailer andresailer deleted the fixGPS branch December 15, 2023 18:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DDSim crashes when GPS use more than 1 source at once
2 participants