LDMX-Software / SimCore

Integration of Geant4 simulation framework into a more generalized event processing framework.
2 stars 0 forks source link

Vertex Smearing is not being Seeded Correctly #53

Closed tomeichlersmith closed 3 years ago

tomeichlersmith commented 3 years ago

@bryngemark found this while comparing inclusive overlay files and the sim hits in the tagger trigger pad. The tagger trigger pad is really close to the primary vertex, so that is why we were able to observe this bug there.

The primary generator action has its own random number generator that it seeds when it is constructed: https://github.com/LDMX-Software/SimCore/blob/0a05e1d9ca7a745080e1c0cf436a99f95d0ebe30/src/SimCore/PrimaryGeneratorAction.cxx#L38-L39

The run manager creates the primary generator action in RunManager::Initialize:

https://github.com/LDMX-Software/SimCore/blob/0a05e1d9ca7a745080e1c0cf436a99f95d0ebe30/src/SimCore/RunManager.cxx#L124-L125

which is called in Simulator::onProcessStart:

https://github.com/LDMX-Software/SimCore/blob/0a05e1d9ca7a745080e1c0cf436a99f95d0ebe30/src/SimCore/Simulator.cxx#L327-L328

But the random seed is not set until Simulator::onNewRun:

https://github.com/LDMX-Software/SimCore/blob/0a05e1d9ca7a745080e1c0cf436a99f95d0ebe30/src/SimCore/Simulator.cxx#L283-L291

So I think that the primary generator action (and only the primary generator action) is not being seeded correctly. This is a bug that should be "easy" to fix because we just need to move the seeding of the PGA to after the seeding of Geant4 (since the PGA uses G4's CLHEP seed). The reason we haven't seen this before is because this only affects primary vertex smearing between runs.

A simple solution is to use Geant4's internal random generator (which uses the seed after it is set in onNewRun). Basically, we would remove the random_ member variable of the Primary Generator Action, and instead use G4UniformRand() (which still needs to be scaled). i.e. replace

https://github.com/LDMX-Software/SimCore/blob/0a05e1d9ca7a745080e1c0cf436a99f95d0ebe30/src/SimCore/PrimaryGeneratorAction.cxx#L140-L142

with

        double x0_f = G4UniformRand()*2*IPWidthX - IPWidthX + x0_i;
        double y0_f = G4UniformRand()*2*IPWidthY - IPWidthY + y0_i;
        double z0_f = G4UniformRand()*2*IPWidthZ - IPWidthZ + z0_i;