Energy Spectrum

Hi everyone,
I would like to know if I can download the energy spectrum of a particular particle inside a code in G4 and how I can do this. Can I also put together , in the same run , the spectra of more particles ? And is there an example doing this ?
Thank you in advance.

1 Like

I am interesting . I also don’t know how to extract energy spectrum

1 Like

You’ll need to modify steppingaction.cc. Basically what I did in the TestEM7 example was, when crossing a boundary, I record the energy, position and momenta for each particle and write it in a CSV file. This resultant csv I run through a matlab script which analyses the spectra. Gimme a shout if you’d like me to send you the modified file.
image

1 Like

Hi, may I ask you how do you implement the condition at the boundary? With getstepstatus() I still get all the particles and not only the ones crossing the boundary.
Thank you so much.

//My modification. Spits out the energy of the particle as it enters the absorber.

G4StepPoint* prePoint = step->GetPreStepPoint();

G4StepPoint* postPoint = step->GetPostStepPoint();

G4VPhysicalVolume* prePhysVol = prePoint->GetPhysicalVolume();

G4VPhysicalVolume* postPhysVol = postPoint->GetPhysicalVolume();

if (prePhysVol != postPhysVol)

{

G4double KE = track->GetKineticEnergy();

//G4ThreeVector Position = track->GetPosition();    

//G4ThreeVector Momentum = track->GetMomentum();

if (KE > 1.0)

{

  G4String postPhysVolName = postPhysVol->GetName();

  if (postPhysVolName == "Absorber")

  {

    if (writefile)

    {

      G4ThreeVector Position = track->GetPosition();    

      G4ThreeVector Momentum = track->GetMomentum();

      // Not a mistype. Beam is rotated in the macro file and enters on the x axis so x = z and vice versa. 

      writefile << Position.z() << "," << Momentum.z() << "," << Position.y() << "," << Momentum.y() << "," << Position.x() << "," << Momentum.x() << "," << KE << G4endl;

    }

  }

}

}

1 Like

Thank you , but I’d like to know if I can download an energy spectrum of a particle on Geant4 and use it for primary generator . I also would like to know if I can use more tha one particle in a single run . Thank you so much

I have done this but the code isn’t too flash I’m afraid. Again it takes modification of the code. That code would read a csv file generated in my above reply and inject that beam at the position at which the csv file recorded the beam.

By one particle I assume you mean one species of particle? If this is the case I have only done it for protons.

1 Like

Ok thank you so much , sorry, for ‘’ more particles’’ I meaned more than one specie , protons with neutrons or others . But Thank you

Off-hand, I think you might be able to.

In my code posted above you have “G4ThreeVector Position = track->GetPosition();”. From track you might be able to determine the particle species. See if there’s a member function that allows you to do this. If there is you can either a) add an additional column in the csv file and sort it out later or b) write several files. One for each species.

track->GetParticleDefinition() gets you the const G4ParticleDefinition* pointer.
From G4PD, you can either use GetParticleName() and store a string into your N-tuple, or you can use GetPDGEncoding() to get the numeric code for the particle.

Please look at $G4INCUDE/G4Track.hh to see all of the information you can retrieve for the track, and $G4INCLUDE/G4ParticleDefinition.hh for all the particle type information that’s available.