35#define INCLXX_IN_GEANT4_MODE 1
50 loadedStoppingTime(0.),
63 const long ID = p->
getID();
67 if(particleAvatarConnections.find(ID)==particleAvatarConnections.end()) {
68 std::vector<long> *aIDs =
new std::vector<long>;
69 particleAvatarConnections[ID] = aIDs;
75 avatars[a->
getID()]=a;
76 avatarList.push_back(a);
79 for(
ParticleIter i = pList.begin(); i != pList.end(); ++i) {
82 connectParticleAndAvatar((*i)->getID(), a->
getID());
93 avatars[a->
getID()]=a;
94 avatarList.push_back(a);
97 for(
ParticleIter i = pList.begin(); i != pList.end(); ++i) {
102 if(particles.find((*i)->getID()) == particles.end()) {
103 ERROR(
"Avatar was added before related particles. This is probably a bug." << std::endl);
107 connectParticleAndAvatar((*i)->getID(), a->
getID());
113 incoming.push_back(p);
116 void Store::connectParticleAndAvatar(
long particleID,
long avatarID) {
117 std::map<long, std::vector<long>* >::const_iterator iter = particleAvatarConnections.find(particleID);
119 if(iter!=particleAvatarConnections.end()) {
120 std::vector<long> *p = iter->second;
121 p->push_back(avatarID);
123 std::vector<long> *p =
new std::vector<long>;
124 p->push_back(avatarID);
125 particleAvatarConnections[particleID]=p;
129 void Store::removeAvatarFromParticle(
long particleID,
long avatarID) {
130 std::vector<long>* theseAvatars = particleAvatarConnections.find(particleID)->second;
131 std::vector<long>* newAvatars =
new std::vector<long>();
132 for(std::vector<long>::const_iterator iter = theseAvatars->begin();
133 iter != theseAvatars->end(); ++iter) {
134 if((*iter) != avatarID) {
135 newAvatars->push_back((*iter));
139 particleAvatarConnections[particleID] = newAvatars;
142 void Store::removeAvatarByID(
long ID) {
144 IAvatar *avatar = avatars.find(ID)->second;
145 ParticleList particlesRelatedToAvatar = avatar->getParticles();
147 = particlesRelatedToAvatar.begin();
148 particleIDiter != particlesRelatedToAvatar.end(); ++particleIDiter) {
149 removeAvatarFromParticle((*particleIDiter)->getID(), ID);
152#ifdef INCL_AVATAR_SEARCH_INCLSort
154 std::list<IAvatarIter>::iterator it=binaryIterSearch(avatars.find(ID)->second);
155 if(it != avatarIterList.end())
156 avatarIterList.erase(it);
160 avatarList.remove(avatar);
165 std::vector<long> temp_aIDs;
166 std::vector<long> *aIDs = particleAvatarConnections.find(particleID)->second;
167 for(std::vector<long>::iterator i = aIDs->begin();
168 i != aIDs->end(); ++i) {
169 temp_aIDs.push_back((*i));
172 for(std::vector<long>::iterator i = temp_aIDs.begin();
173 i != temp_aIDs.end(); ++i) {
174 IAvatar *tmpAvatar = avatars.find(*i)->second;
175 removeAvatarByID((*i));
180#ifdef INCL_AVATAR_SEARCH_INCLSort
181 std::list<IAvatarIter>::iterator Store::binaryIterSearch(
IAvatar const *
const avatar) {
182 std::list<IAvatarIter>::iterator it;
183 std::iterator_traits<std::list<IAvatarIter>::iterator>::difference_type count, step;
184 std::list<IAvatarIter>::iterator first = avatarIterList.begin();
185 std::list<IAvatarIter>::iterator last = avatarIterList.end();
187 count = distance(first,last);
190 it = first; step=count/2; advance(it,step);
191 if ((**it)->getTime()>avatarTime)
192 { first=++it; count-=step+1; }
195 if(first!=last && (**first)->getID()==avatar->
getID())
202 void Store::removeAvatarsFromParticle(
long ID) {
203 std::vector<long> *relatedAvatars = particleAvatarConnections.find(ID)->second;
204 for(std::vector<long>::const_iterator i = relatedAvatars->begin();
205 i != relatedAvatars->end(); ++i) {
206 removeAvatarByID((*i));
208 delete relatedAvatars;
209 particleAvatarConnections.erase(ID);
213 if(avatarList.empty())
return NULL;
215#ifdef INCL_AVATAR_SEARCH_FullSort
222 IAvatar *avatar = avatarList.front();
224#elif defined(INCL_AVATAR_SEARCH_INCLSort)
234 if(avatarIterList.empty())
235 best = avatarList.begin();
237 best = avatarIterList.back();
238 G4double bestTime = (*best)->getTime();
241 for(++a; a!=avatarList.end(); ++a)
242 if((*a)->getTime() < bestTime) {
244 bestTime = (*best)->getTime();
245 avatarIterList.push_back(best);
249#elif defined(INCL_AVATAR_SEARCH_MinElement)
252 IAvatar *avatar = *(std::min_element(avatarList.begin(), avatarList.end(),
256#error Unrecognized INCL_AVATAR_SEARCH. Allowed values are: FullSort, INCLSort, MinElement.
259 removeAvatarByID(avatar->
getID());
264 for(std::map<long, Particle*>::iterator particleIter
266 particleIter != particles.end(); ++particleIter) {
267 (*particleIter).second->propagate(step);
274 inside.remove(particles.find(ID)->second);
276 delete particleAvatarConnections.find(ID)->second;
277 particleAvatarConnections.erase(ID);
283 Particle *
const toDelete = particles.find(ID)->second;
284 inside.remove(toDelete);
295 WARN(
"Store::getParticipants is probably slow..." << std::endl);
297 for(std::map<long, Particle*>::iterator iter = particles.begin();
298 iter != particles.end(); ++iter) {
299 if((*iter).second->isParticipant()) {
300 result.push_back((*iter).second);
307 WARN(
"Store::getSpectators is probably slow..." << std::endl);
309 for(std::map<long, Particle*>::iterator iter = particles.begin();
310 iter != particles.end(); ++iter) {
311 if(!(*iter).second->isParticipant()) {
312 result.push_back((*iter).second);
319 for(std::map<long, IAvatar*>::iterator iter = avatars.begin();
320 iter != avatars.end(); ++iter) {
321 delete (*iter).second;
324 for(std::map<
long, std::vector<long>*>::iterator iter = particleAvatarConnections.begin();
325 iter != particleAvatarConnections.end(); ++iter) {
326 delete (*iter).second;
329 particleAvatarConnections.clear();
336 for(
ParticleIter ip=inside.begin(); ip!=inside.end(); ++ip) {
337 std::vector<long> *aIDs =
new std::vector<long>;
338 particleAvatarConnections[(*ip)->getID()] = aIDs;
346 for(std::map<long, Particle*>::iterator iter = particles.begin();
347 iter != particles.end(); ++iter) {
348 delete (*iter).second;
354 if( incoming.size() != 0 ) {
355 WARN(
"Incoming list is not empty when Store::clear() is called" << std::endl);
359#ifdef INCL_AVATAR_SEARCH_INCLSort
360 avatarIterList.clear();
366 for(
ParticleIter iter = outgoing.begin(); iter != outgoing.end(); ++iter) {
367 if((*iter)->isCluster()) {
370#ifdef INCLXX_IN_GEANT4_MODE
383 G4int projectileA, projectileZ, A, Z;
385 G4int ID, type, isParticipant;
389 std::ifstream in(filename.c_str());
390 in >> projectileA >> projectileZ >> A >> Z >> stoppingTime >> cutNN;
393 loadedStoppingTime = stoppingTime;
398 in >> ID >> type >> isParticipant >> x >> y >> z >> px >> py >> pz >> E >> v;
399 if(!in.good())
break;
406 else if(type == -1) {
411 FATAL(
"Unrecognized particle type while loading particles; type=" << type << std::endl);
418 if(isParticipant == 1) {
429 std::stringstream ss;
431 for(
ParticleIter i = inside.begin(); i != inside.end(); ++i) {
432 if((*i)->getType() ==
Proton) {
436 if((*i)->getType() ==
Neutron) {
442 ss <<
"0 0 " << A <<
" " << Z <<
" "
444 <<
"0.0" << std::endl;
446 for(
ParticleIter i = inside.begin(); i != inside.end(); ++i) {
447 G4int ID = (*i)->getID();
449 if((*i)->getType() ==
Proton) {
452 if((*i)->getType() ==
Neutron) {
456 G4int isParticipant = 0;
457 if((*i)->isParticipant()) {
461 G4double x = (*i)->getPosition().getX();
462 G4double y = (*i)->getPosition().getY();
463 G4double z = (*i)->getPosition().getZ();
465 G4double px = (*i)->getMomentum().getX();
466 G4double py = (*i)->getMomentum().getY();
467 G4double pz = (*i)->getMomentum().getZ();
468 G4double V = (*i)->getPotentialEnergy();
470 ss << ID <<
" " << type <<
" " << isParticipant <<
" "
471 << x <<
" " << y <<
" " << z <<
" "
472 << px <<
" " << py <<
" " << pz <<
" "
473 << E <<
" " << V << std::endl;
480 std::ofstream out(filename.c_str());
486 std::stringstream ss;
488 for(i = avatarList.begin(); i != avatarList.end(); ++i) {
489 ss << (*i)->toString() << std::endl;
496 for(i = avatarList.begin(); i != avatarList.end(); ++i)
void incrementCascading()
virtual ParticleList getParticles() const =0
void setPotentialEnergy(G4double v)
Set the particle potential energy.
virtual void makeParticipant()
void particleHasBeenDestroyed(long)
void addParticleEntryAvatars(IAvatarList const &al)
Add one ParticleEntry avatar.
void timeStep(G4double step)
IAvatar * findSmallestTime()
Store(Config const *const config)
void initialiseParticleAvatarConnections()
Initialise the particleAvatarConnections map.
void particleHasBeenEjected(long)
std::string printAvatars()
void particleHasBeenUpdated(long)
void addIncomingParticle(Particle *const p)
void particleHasEntered(Particle *const particle)
Move a particle from incoming to inside.
std::string printParticleConfiguration()
void writeParticles(std::string filename)
G4bool containsCollisions() const
static G4bool avatarComparisonPredicate(IAvatar *lhs, IAvatar *rhs)
Comparison predicate for avatars.
void loadParticles(std::string filename)
void addParticleEntryAvatar(IAvatar *a)
Add one ParticleEntry avatar.
void removeFromIncoming(Particle *const p)
ParticleList getSpectators()
ParticleList getParticipants()
std::list< IAvatar * >::const_iterator IAvatarIter
std::list< IAvatar * > IAvatarList
std::list< G4INCL::Particle * > ParticleList
std::list< G4INCL::Particle * >::const_iterator ParticleIter