Error in EventActionClass

This is my EventAction class code:

MyEventAction::MyEventAction(MyRunAction* runAction) : G4UserEventAction(),
//fEdep = 0.;


void MyEventAction::BeginOfEventAction(const G4Event*)
fEdep = 0.;

void MyEventAction::EndOfEventAction(const G4Event* event)

// Get event ID
G4int eventID = event->GetEventID();

// Get position of last step
const G4PrimaryVertex* primaryVertex = event->GetPrimaryVertex(0);
const G4PrimaryParticle* primaryParticle = primaryVertex->GetPrimary(0);
const G4Track* primaryTrack = primaryParticle->GetTrack();
G4ThreeVector position;
const G4Step* lastStep = primaryTrack->GetStep();
if (lastStep) {
    position = lastStep->GetPostStepPoint()->GetPosition() / mm;
    const G4VPhysicalVolume* volume = lastStep->GetPreStepPoint()->GetTouchableHandle()->GetVolume();
    G4cout << "Particle last located in " << volume->GetName() << G4endl;

// Print event information
G4cout << "Event " << eventID << ": deposited energy = "
    << fEdep / MeV << " MeV, last position = "
    << position << " mm" << G4endl;

// Save event information in run action
fRunAction->AddEventInfo(eventID, fEdep, position);


The error I’m not able to resolve is -

C:\geant4\simulations\trials\Path\src\,52): error C2039: ‘GetTrack’: is not a member of ‘G4PrimaryParticle’ [C:\geant4\simulations\trials\Path\build\sim.vcxproj]

Anyone please help me what am I doing wrong in the code and how to correct it

If you look at the $G4INCLUDE/G4PrimaryParticle.hh file, you will see that it does not contain any function named “GetTrack()”, which is exactly what the error message is telling you. Skim through the .hh file and see if there is a function that might be appropriate to do what you want.

I can certainly tell you that what you are trying to do isn’t going to be right in general. A single event can have many tracks in it. If you want to collect information about individual steps or tracks, then you need to do that in a Stepping Action. If you want to you could create a container class that is shared between SteppingAction and EventAction, where the SteppingAction fills stuff in, and EventAction pulls stuff out (and empties it for the next event!).

I’ve updated my code like this:

void MyEventAction::EndOfEventAction(const G4Event* event)
// Retrieve the proton trajectory data
G4TrajectoryContainer* trajectoryContainer = event->GetTrajectoryContainer();
if (!trajectoryContainer) return;

G4int n_trajectories = trajectoryContainer->entries();
if (n_trajectories == 0) return;

G4VTrajectory* trajectory = (*trajectoryContainer)[n_trajectories - 1];

G4int n_points = trajectory->GetPointEntries();
if (n_points == 0) return;
G4ThreeVector position = trajectory->GetPoint(n_points - 1)->GetPosition();

this is only the endofRunAction function of the eventaction class.
Am I going in the right direction now?

If all you need is position information, then I think that should work. If you need energy deposition or particle ID, then you’ll need to use a SteppingAction to collect it.

Is there any way to collect energy information in this class?
And I’m facing one more problem in G4AnalysisManager to plot the data. Root files are not generated after compilation. What I’m doing is creating a NTuple in Runaction class and writing position details in that but output files are not generated. Should I do analysis in any other class or runaction class is fine?

No. You need a SteppingAction, or a SensitiveDetector class, or scorers (use any or all of those as keywords to search the documentation).

I’m not sure what you mean by this. ROOT files will be produced after running the executable, not after compiling the code.

In your RunAction, do you tell the AnalysisManager to save the output to a file, or not? If you don’t tell it, nothing will happen.

I’m writing to code to first Write file and then Close. It will not get updated by Writing?then How to save the update and save the file?

If you’ve got Write and Close in there, that’s what you need. Check some of the examples and see if you’re doing what they do.

I’ve successfully genrated the root file at the beginofRunAction burt it is not getting updated at the endofRunAction. What could be the possible reason for that?

How to add time information in it?

Same as for any other information that is specific to a track.

I’m attaching my EventAction and RunAction files below. Can you please take a look at them and tell me why these errors are coming and how to resolve them?
Error in TFile::ReadBuffer: error reading all requested bytes from file output.root, got 218 of 300
Error in TFile::Init: output.root failed to read the file type data.

I’ve checked everything in my knowledge. (2.0 KB) (1.2 KB)

I’m calculating energy deposit at each proton position like this in my trackingAction class.
void MyTrackingAction::PostUserTrackingAction(const G4Track* track)
// Check if the particle is a proton
const G4ParticleDefinition* particle = track->GetDefinition();
if (particle != G4Proton::ProtonDefinition()) return;

// Retrieve the position of the particle and print it to the console
G4ThreeVector position = track->GetPosition();
const G4Step* step = track->GetStep();
G4double energy = step->GetTotalEnergyDeposit();

//G4cout << "Proton position: (" << position.x() / cm << ", "
//    << position.y() / cm << ", " << position.z() / cm << ") cm" <<G4endl;
G4cout << position.x() / cm << ", "
    << position.y() / cm << ", " << position.z() / cm <<"," << energy << G4endl;

Is it the right way?

No. You’re getting the energy deposit for only the last step of the proton. Maybe it only takes one step, and you’re fine. But maybe it takes ten or twenty steps, in which case you’re completely wrong.

If you want to accumulate information for many steps, then you should be using a SteppingAction. You could accumulate the information for a track within your SteppingAction, for example into a HitsCollection for the event, and access that information later in your EventAction in order to store it; or you could store the information from individual steps directly into the N-tuple as part of your SteppingAction.

The latter method means you could do things like get totals per event, or make cuts based on positon, etc., in your analysis code.

I’ve added stepping action class to get energy deposit at every step and I’m printing that in the above tracking action class. Can you please verify whether I’m doing it right or not this time?
Here is the code of my stepping action and tracking action class: (828 Bytes) (1.1 KB)

Am I implementing it wrong again?

  1. You’re creating a new local instance of your tracking action. That’s a different object than the original one you registered with the run manager.

  2. You don’t need the tracking action at all if you want to record individual hits (energy deposits). You only need the stepping action, and you need to register it to the run manager for all worker threads in your UserActionInitialization.

Okay so I should only use stepping action class and there is no need for a tracking class. I’ve updated the stepping action class and removed tracking action class. I’ve added this line to get the position of each event in stepping action class. Is it right?
G4ThreeVector position = step->GetPostStepPoint()->GetPosition();

That’s exactly right.

Thanku so much for your ongoing support.
I’ve one more ques to ask what if I only want to see inelastic coulomb scattering, coulomb scattering, and non-elastic nuclear interactions of proton in the medium. I’m trying to add these processes in place of standard physics list options given. Can you guide me how to add these processes in the physicslist only?