Create a list of positions and access it in TrackingAction

Hello everyone,

I am working on a simulation in which I need to create a list of certain vector values (it is NOT the position of the particle. It is just a list of vectors). I am creating this list in in RunAction.cc, but I would like to access it in the TrakingAction.cc and use those values to modify a particle position in the TrackingAction.

These are the for loops to create my list in RunAction:

    std::list<G4ThreeVector> DeltaList;       // create list                                                                               
    for(G4double i = -0.039; i < 0.039; i = i + 0.001)
    {
        for(G4double j = -0.039; j < 0.039; j = j + 0.001)
        {
            for(G4double k = -0.039; k < 0.039; k = k + 0.001)
            {
                Delta = G4ThreeVector(i*cm, j*cm, k*cm);   // Create the vector
                DeltaList.push_back(Delta);                         // Save the vectors in the list
            }
        }
    }

I searched how to access a list in other class for C++ and found this ( c# - Access List from another class - Stack Overflow. ):

public class MyClass {

private List<string> myList = new List<string>();

public List<string> GetList()
{
    return myList;
}

}

:::::::::::::::::::::::::::::::::::::::::::::::::::

public class CallingClass {

MyClass myClass = new MyClass();
public void GetList()
{
    List<string> calledList = myClass.GetList();
    ///More code here...
}

}

I tired to implement this on Geant4, but it is not working at all. Does anyone know how I could access this list in the TrackingAction?

Thank you,

A tidy way to communicate between classes in Geant4 is through the run manager. The run manager can give you pointers to your other user action classes (run action, event, stepping, tracking etc.). Keep in mind that it returns a pointer to the base Geant4 class and not the derived class, so you will have to cast from the base G4VUserRunAction down to your RunAction class.

From your Tracking Action you would call:

const RunAction* myRunAction = static_cast<const RunAction*>(G4RunManager::GetRunManager()->GetUserRunAction());

Then it would be up to you to define a public interface in your RunAction for retrieving the vector. In your RunAction header file it might look like this:

public:
  inline std::list<G4ThreeVector> GetVector() const { return DeltaList; };

Since the definition of GetVector() has been provided in the RunAction header file, you don’t have to modify the RunAction.cc source file. For simple functions that just return a class member, it is better to provide the definition in the header file to ensure that the compiler can inline the function for increased performance.

Additionally, note that the GetVector() function has been defined as const. This is important because the G4RunManager returns to you a pointer to const for your RunAction. If your GetVector() function was not const you would not be allowed to call it from the pointer you retrieved.

Joseph