Hi, there is function ParticleCountPerLayer
in my nbRun.cc file which I am calling from stepping action.
It get’s called and it’s storing data in map. It also print the data from map in same function.
But when I try to print data in another function endOfRun
it does not give any output. It prints map size 0.
code snippet:
void nbRun::ParticleCountPerLayer(G4String layerName, G4String particleName)
{
// create iterator instance of map
map<G4String, vector<particleDataPerLayer>>::iterator it = fParticleDataPerLayerMap.find(layerName);
// if we do not find layerName
if (fParticleDataPerLayerMap.find(layerName) == fParticleDataPerLayerMap.end()) {
// push struct in vector
particleDataPerLayer obj;
obj.fParticleName = particleName;
obj.fcount = 1;
particleDataVector.push_back(obj); // initialize vector with particle name and count
// store in map
fParticleDataPerLayerMap[layerName] = particleDataVector;
// fParticleDataPerLayerMap[layerName] = particleDataVector;
}
// we already have this layerName in our map
else {
G4int flag = 0;
G4int index = -1;
vector<particleDataPerLayer> v = fParticleDataPerLayerMap.at(layerName);
// search if you have this particle already in your vector
for (auto i = begin (v); i != end (v); ++i) {
if(i->fParticleName == particleName)
{
flag = 1;
index = i - v.begin();
break;
}
}
if(flag == 1)
{
// this particle is already present in the vector
v[index].fcount++;
// fParticleDataPerLayerMap[layerName] = v;
}
else
{
// this particle is not present in vector
// push struct in vector
particleDataPerLayer obj;
obj.fParticleName = particleName;
obj.fcount = 1;
v.push_back(obj);
// fParticleDataPerLayerMap[layerName] = v;
}
// if (it != fParticleDataPerLayerMap.end()) it->second = v;
}
G4cout<<fParticleDataPerLayerMap[layerName].size()<<G4endl;
}
Here is my nbRun.hh file where I declared variables:
struct particleDataPerLayer {
G4int fcount;
G4String fParticleName;
};
// dynamic vector of structs
vector<particleDataPerLayer> particleDataVector;
// map(k=layerName, v=vector containing information about all particles)
std::map<G4String,vector<particleDataPerLayer>> fParticleDataPerLayerMap;