Export results from UserSteppingAction()

I’d like to export some results (e.g., particle positions and momentum at each step) from UserSteppingAction() to a file output.
I tried to use an ofstream variable defined as global in a separate header file; however, it generated several errors.
Could anyone please tell what’s the best way or the standard practice to export results from UserSteppingAction()?

Have a look at G4cout during a Run with a GUI.

Thank you for the posting. I think it’s related to my question, but doesn’t quite answer it.

Here is my problem. There are some results available in UserSteppingAction(), which I’d like to export to a file.

First, I tried to define the file object as a global variable so that it can be accessed from any files in the project. However, this was somewhat trickier than I originally thought, which I believe is related to the way the functions are called in Geant4.

Next, I declared (& defined) a file object (std::ofstream myfile) in BeginOfEventAction() and closed it in EndOfEventAction(), so that I can generate one output file for each event (this will contain the information of all the steps during the event). The problem is to access to the file object (std::ofstream myfile) from UserSteppingAction(), which is in a different file.

This must be a simple task in a general C++ project. However, it becomes quite tricky when all those Geant4 classes and functions are involved.

1 Like

I was able to do this by changing exampleB1 a bit.

  • In the EventAction class, I defined a vector (let’s call it ‘stepInfo’) and a member function (let’s call it AddStep()) to add a new element into it.
  • To add new step information in UserSteppingAction(), the member function was called using fEventAction->AddStep().

It becomes particularly easy if you have SteppingAction.cc file defined separately. Looking at other example’s implementation of SteppingAction is the best way as you figured out yourself.

Thank you very much for the suggestion!

It looks that G4VSteppingVerbose also provides a nice way to print out the stepping information. For implementation, see the ‘extended/radioactivedecay’ example.