Problem 2225

Summary: SetParticleMomentumDirection adds primary particles to G4ParticleGun
Product: Geant4 Reporter: Ligia Diana Pinto de Almeida Amorim <LDianaAmorim>
Component: eventAssignee: asai
Status: RESOLVED INVALID    
Severity: normal    
Priority: P4    
Version: other   
Hardware: Apple   
OS: Mac OS X   

Description Ligia Diana Pinto de Almeida Amorim 2020-02-10 21:46:27 CET
Hi,
I didn't find a matching/connected issue.
But I am not very familiar with Geant4, so sorry if I misunderstood them.

I am trying to send a fixed nr of particles, with random properties(energy / momentum), using the G4ParticleGun class.

In my user class that inherits from G4VUserPrimaryGeneratorAction I am doing:

G4int n_particle = 1;
    beam  = new G4ParticleGun(n_particle);
    
    G4ParticleTable *particle_table = G4ParticleTable::GetParticleTable();
    G4ParticleDefinition *electron
    = particle_table->FindParticle("e-");
    beam->SetParticleDefinition(electron);

And this leads to the generation of a nr of primary electrons that corresponds to the nr of events that I set in my main function through:
    runManager->BeamOn(in.nevents);

However, when I start specifying the properties of each particle, for example with the line below:

    beam->SetParticleMomentumDirection(G4ThreeVector(0.,0.,1.));

I get many more primary particles than the number of events (and they don't vary linearly, for low number of events).

I was wondering if there is a way for me to send the same nr of particles as the nr of events (i.e. only one primary particle per event), but with specific properties.

Thank you,
Diana
Comment 1 Ligia Diana Pinto de Almeida Amorim 2020-02-10 22:07:02 CET
Just as an update, if I add the line below:

    beam->SetParticleEnergy(rand_n[0]);

To the initially working section described in the previous message, I still get the correct number of primary particles.

But if I also add:

    beam->SetParticlePosition(G4ThreeVector(
                                rand_n[1],rand_n[2],rand_n[3]));

Then the number of primary particles seems random.

I think I might not be understanding what position and momentum settings are supposed to do according to the G4ParticleGun class. But I read their definition and still can't figure out how to make them not introduce additional primary particles.
Comment 2 asai 2020-02-11 17:36:36 CET
You may want to consult to one of our examples that demonstrate the use of UserPrimaryGeneratorAction. For example,
https://geant4.kek.jp/lxr/source/examples/basic/B3/B3a/src/B3PrimaryGeneratorAction.cc

For further questions, please use our discussion forum
https://geant4-forum.web.cern.ch
Comment 3 Ligia Diana Pinto de Almeida Amorim 2020-02-11 18:51:03 CET
Thank you very much for your reply.
I tried to follow exactly that example.

And I noticed that changing the momentum direction from (1.0,0.0,0.0) to (0.0,0.0,1.0) led to the generation of additional primary particles (instead of the same nr as the nr of events). So I corrected that direction in my code.

But, whenever I specify the random position, just as in the example, I get additional primary particles being created.
Do you have any suggestions on how I can find the source of this issue?

Thank you very much for your support,
Diana