How to use instance variable in my method

Hello there,

I have a problem with the use of an instance variable in a certain method for a few days.

I made a method “AddDose” that have in argument the deposit energy, the mass and the weight of the interaction to get those values calculated in TrackerSD and to use them in RunAction where this method is defined. For that, I made a RunAction object “fRunAction” that take in argument runAction to assign the values (energy, mass, weight) to this object.

The problem is that when i try to introduce an instance variable in the AddDose method (in the RunAction class), I receive the following error message when I launch the beam :

CAUGHT SIGNAL: 11 ### address: 0x43, signal = SIGSEGV, value = 11, description = segmentation violation. Address not mapped to object.

Backtrace:
[PID=15433, TID=0][ 0/11]> ./A1_s1(+0x110d2) [0x55bf78b090d2]
[PID=15433, TID=0][ 1/11]> ./A1_s1(+0x154e5) [0x55bf78b0d4e5]
[PID=15433, TID=0][ 2/11]> /home/vanhee/Programmes/Geant4.11.0.0/Geant4-Install/lib/libG4digits_hits.so(_ZN13G4SDStructure9TerminateEP15G4HCofThisEvent+0x3da) [0x7f4669649c6a]
[PID=15433, TID=0][ 3/11]> /home/vanhee/Programmes/Geant4.11.0.0/Geant4-Install/lib/libG4event.so(_ZN14G4EventManager12DoProcessingEP7G4Event+0x6ea) [0x7f466b86dada]
[PID=15433, TID=0][ 4/11]> /home/vanhee/Programmes/Geant4.11.0.0/Geant4-Install/lib/libG4run.so(_ZN18G4WorkerRunManager11DoEventLoopEiPKci+0x1dc) [0x7f466b9337fc]
[PID=15433, TID=0][ 5/11]> /home/vanhee/Programmes/Geant4.11.0.0/Geant4-Install/lib/libG4run.so(_ZN12G4RunManager6BeamOnEiPKci+0x5e) [0x7f466b924b6e]
[PID=15433, TID=0][ 6/11]> /home/vanhee/Programmes/Geant4.11.0.0/Geant4-Install/lib/libG4run.so(_ZN18G4WorkerRunManager6DoWorkEv+0x38c) [0x7f466b936ddc]
[PID=15433, TID=0][ 7/11]> /home/vanhee/Programmes/Geant4.11.0.0/Geant4-Install/lib/libG4run.so(_ZN20G4MTRunManagerKernel11StartThreadEP14G4WorkerThread+0x19b) [0x7f466b95a60b]
[PID=15433, TID=0][ 8/11]> /usr/lib/libstdc++.so.6(+0xd54d4) [0x7f4668d164d4]
[PID=15433, TID=0][ 9/11]> /usr/lib/libc.so.6(+0x8d5c2) [0x7f46689c15c2]
[PID=15433, TID=0][10/11]> /usr/lib/libc.so.6(clone+0x44) [0x7f4668a46584]

: Segmentation fault (Address not mapped to object [0x43])
Abandon (core dumped)

My method is defined like this (in RunAction.cc) :

void RunAction::AddDose(G4double epsilon,G4double weight_bis,G4double mass)
{
G4double tempo1 = (epsilon/mass)*weight_bis;// works with the local variable “tempo1”
tempo2 =(epsilon/mass)*weight_bis;// doesn’t work with the instance variable “tempo2”
}

Could someone explain me why i can’t use this instance variable in this method ?

The function isn’t the problem. You’re trying to call the function using a null pointer. Look at the code where you call the function, and make sure that the “RunAction” pointer is set properly to the correct object.

Thanks you ! You were right. The object “runAction” created in “ActionInitialization” was not the same I used in argument of my object “fRunAction” in the “TrackerSD” where I need the values of energy, mass and weight. Now i can use instance variables in my function “AddDose(edep,mass,weight)”.
But now, I have an other problem. I want to use the instance variable “dose”, calculated in the “AddDose” function, in the “EndOfRunAction”. The problem is that the dose give (with G4cout …) 0 in the EndOfRunAction but the correct answer in the AddDose method.
Could you tell me what am I doing wrong ?

Are you accumulating the result of each call to AddDose() to the class data member? Or are you replacing the data member value each time?

I’m sorry for the long response time (I was sick until now).
I made both and it doesn’t work.
First I tried like this (in RunAction.cc):

void RunAction::AddDose(G4double epsilon,G4double weight_bis,G4double mass,G4int i_radial,G4int j_theta)

{
epsxwgt = ((epsilon/joule)/(mass/kg))*weight_bis;
epsxwgt_acc = epsxwgt_acc + epsxwgt;

weight_acc = weight_acc + weight_bis;
}

In RunAction.hh:

class RunAction : public G4UserRunAction

{

public:

void AddDose ( … )

G4double weight_acc;
G4double epsxwgt;
G4double epsxwgt_acc;

}

Then i tried with accumulables like this (in RunAction.cc) :

void RunAction::BeginOfRunAction(const G4Run* aRun)
{

G4AccumulableManager* accumulableManager = G4AccumulableManager::Instance();
for (int i=0;i<=1000;i++)
{

epsxwgt_acc.push_back(0);  
accumulableManager->RegisterAccumulable(epsxwgt_acc[i]);
weight_acc.push_back(0);  
accumulableManager->RegisterAccumulable(weight_acc[i]);

}

}
void RunAction::AddDose(G4double epsilon,G4double weight_bis,G4double mass,G4int i_radial,G4int j_theta)
{

weight_acc[0] = weight_acc[0].GetValue() + weight_bis;

epsxwgt = ((epsilon/joule)/(mass/kg))*weight_bis;

epsxwgt_acc[0] = epsxwgt_acc[0].GetValue() + epsxwgt;
}

In RunAction.hh :

class RunAction : public G4UserRunAction

{

public:

void AddDose ( … )

std::vector<G4Accumulable> epsxwgt_acc;
std::vector<G4Accumulable> weight_acc;
std::vector<G4Accumulable> dose;
G4double epsxwgt;
}

In both cases, when I try to use those variables in the method “EndOfRunAction”, it gives me 0.

You must be running a multithreaded job. Each worker thread has its own, independent instance of RunAction. The data members of those instances do not, of course, communicate with the other independent instances. EndOfRunAction() gets called for the worker threads as well as the master thread. The master thread instance, of course, never collected any data, so it’s all zeroes.

Yes, I’m using the multithread but it doesn’t work even if I run single thread (with the macro /run/numberOfThreads 1). Is it normal ?
How do I make the master thread collect the data ?