I’m not 100% familiar with G4Accumulable
so what follows should be taken as hints rather than definitive!
If the quantities to be accumulated are all of the same type, e.g. G4int
, then it’s possible to use a std::vector
(*):
// This could be in the class that manages the accumulables
std::vector<G4Accumulable<G4int>> PhotonCount;
it could then be initialised, e.g., in the body of a constructor as
G4AccumulableManager* accumulableManager = G4AccumulableManager::Instance();
for (size_t i = 0 ; i < 20 ; ++i)
{
PhotonCount.emplace_back(0); // assuming the initial value is 0
accumulableManager->RegisterAccumulable(PhotonCount[i]);
}
(*) One gotcha with G4Accumulable
is that it has a delete
d default constructor, so a plain C array or std::array
would need to store pointers. That can be done, but requires more memory management and code.
The other option could be to use the CreateAccumulable/GetAccumulable
member functions of G4AccumulableManager
. Again, assuming the quantities are all the same type, then these could be created by assuming no other accumulables:
G4AccumulableManager* accumulableManager = G4AccumulableManager::Instance();
for (size_t i = 0 ; i < 20 ; ++i)
{
accumulableManager->CreateAccumulable<G4int>(0);
}
The no other accumulables is required because the integer id they get in the G4AccumulableManager
is based on the order of addition. So if you’d already created N
accumulables, the ones above would have ids starting from N
rather than zero.
Accumulables created this way can be retrieved by:
G4AccumulableManager* accumulableManager = G4AccumulableManager::Instance();
for (size_t i = 0 ; i < 20 ; ++i)
{
G4Accumulable<G4int>* a = accumulableManager->GetAccumulable<G4int>(i);
// Do something with retrieved accumulable
}
There are a few more adaptions possible to the above if you need the accumulables to have a named id (via G4String
). The Application Developer Guide section on Accumulables covers these.