Feeding Information within Primary Generator Action

Hi,

This is probably a reflection of my C++ knowledge, but I was hoping to feed in information from the Primary Generator Action constructor (which records an array of particle information on a row by row basis) into the GeneratePrimaries() function. However, because the entire array (e.g. every row) is imported into the Constructor at once, I’m unsure how to access it in the GeneratePrimaries() function in a way that is suitable, as from my understanding, this function runs on a particle-by-particle basis. How do I access information from the constructor row-by-row and therefore assign it particle-by-particle? Thank you!

Are you talking about a specific Geant4 example? If so, which one? Or are you looking at a Geant4 application which someone else has given you to work with?

This sounds like you’re trying to use a phase space file, in case you don’t know the terminology. Essentially you have a list of particles, each with their own particle type, position, energy, momentum direction etc. and you want to fire each into your geometry, if I’m understanding correctly.

The simplest way to do this is to read the text or binary file that contains this particle information in the PrimaryGeneratorAction’s constructor into multiple vectors for each particle quantity you want to use, or create a vector for each particle and put each of those in another vector. If you declare these vectors outside of the PrimaryGeneratorAction’s constructor (in the header) they will persist so long as the PrimaryGeneratorAction persists. Also declare an integer that will correspond to the vector’s position, which should be 0 at first.

Then in the GeneratePrimaries method you read the particle information from the vectors and just set the event to have those particle qualities. At the end of the GeneratePrimaries you want to increment that integer.

In your main, or in a run macro, you want to set beamOn to be the number of particles in your phase space file.

That’s the simplest way I can think of, there’s certainly improvements, but hopefully that will get you started.