Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // 26 << 27 #include <typeinfo> << 28 << 29 #include "G4CollisionManager.hh" 26 #include "G4CollisionManager.hh" 30 #include "G4SystemOfUnits.hh" << 31 #include "G4HadronicException.hh" 27 #include "G4HadronicException.hh" 32 #include "G4CollisionInitialState.hh" 28 #include "G4CollisionInitialState.hh" 33 #include "G4BCAction.hh" << 34 29 35 G4CollisionManager::G4CollisionManager() 30 G4CollisionManager::G4CollisionManager() 36 { 31 { 37 theCollisionList = new G4ListOfCollisions; 32 theCollisionList = new G4ListOfCollisions; 38 } 33 } 39 34 40 35 41 G4CollisionManager::~G4CollisionManager() 36 G4CollisionManager::~G4CollisionManager() 42 { 37 { 43 ClearAndDestroy(); 38 ClearAndDestroy(); 44 delete theCollisionList; 39 delete theCollisionList; 45 } 40 } 46 41 47 42 48 void G4CollisionManager::AddCollision(G4double 43 void G4CollisionManager::AddCollision(G4double time, G4KineticTrack * proj, 49 G4KineticTrack * target) 44 G4KineticTrack * target) 50 45 51 { 46 { 52 if ( time < DBL_MAX ) 47 if ( time < DBL_MAX ) 53 { 48 { 54 G4CollisionInitialState * 49 G4CollisionInitialState * 55 collision = new G4CollisionInitialState 50 collision = new G4CollisionInitialState(time, proj, target); 56 theCollisionList->push_back(collision 51 theCollisionList->push_back(collision); 57 } else { 52 } else { 58 G4cerr << "G4Scatterer invalid TimeTo 53 G4cerr << "G4Scatterer invalid TimeTo Interaction : " << time; 59 G4cerr <<" projectile "<<proj->Get 54 G4cerr <<" projectile "<<proj->Get4Momentum()<<" " 60 <<proj->Get 55 <<proj->GetDefinition()->GetParticleName()<<G4endl; 61 if (target) G4cerr <<" target 56 if (target) G4cerr <<" target " 62 <<target->Get4Momentum()<<" " 57 <<target->Get4Momentum()<<" " 63 <<target->GetD 58 <<target->GetDefinition()->GetParticleName()<<G4endl; 64 G4cerr <<"G4Scatterer error message e 59 G4cerr <<"G4Scatterer error message end"<< G4endl; 65 throw G4HadronicException(__FILE__, __LINE_ 60 throw G4HadronicException(__FILE__, __LINE__, "G4Scatterer::AddCollision()"); 66 } 61 } 67 } 62 } 68 63 69 64 70 void G4CollisionManager::RemoveCollision(G4Col 65 void G4CollisionManager::RemoveCollision(G4CollisionInitialState * collision) 71 { 66 { 72 theCollisionList->erase(std::find(theCollisi 67 theCollisionList->erase(std::find(theCollisionList->begin(), 73 theCollisionList->end(), 68 theCollisionList->end(), 74 collision)); 69 collision)); 75 delete collision; 70 delete collision; 76 collision = NULL; // prevent future use of t 71 collision = NULL; // prevent future use of the pointer 77 } 72 } 78 73 79 74 80 void G4CollisionManager::RemoveTracksCollision 75 void G4CollisionManager::RemoveTracksCollisions(G4KineticTrackVector * toBeCaned) 81 { 76 { 82 if(toBeCaned == NULL) 77 if(toBeCaned == NULL) 83 return; 78 return; 84 if(toBeCaned->empty()) 79 if(toBeCaned->empty()) 85 return; 80 return; 86 81 87 G4CollisionInitialState * collision; 82 G4CollisionInitialState * collision; 88 std::vector<G4CollisionInitialState *>::iter 83 std::vector<G4CollisionInitialState *>::iterator collIter, collIter2; 89 std::vector<G4KineticTrack *>::iterator trac 84 std::vector<G4KineticTrack *>::iterator trackIter; 90 G4ListOfCollisions toRemove; 85 G4ListOfCollisions toRemove; 91 86 92 for(collIter = theCollisionList->begin(); 87 for(collIter = theCollisionList->begin(); 93 collIter != theCollisionList->end(); col 88 collIter != theCollisionList->end(); collIter++) 94 { 89 { 95 collision = *collIter; 90 collision = *collIter; 96 G4KineticTrackVector & targets = collision 91 G4KineticTrackVector & targets = collision->GetTargetCollection(); 97 G4bool getNextCollision = false; 92 G4bool getNextCollision = false; 98 for(trackIter = toBeCaned->begin(); trackI 93 for(trackIter = toBeCaned->begin(); trackIter != toBeCaned->end(); ++trackIter) 99 { 94 { 100 if((collision->GetTarget() == *trackIter 95 if((collision->GetTarget() == *trackIter) || 101 (collision->GetPrimary() == *trackIter)) 96 (collision->GetPrimary() == *trackIter)) 102 { // cannot remove the collision from t 97 { // cannot remove the collision from the list inside the loop. Save and do it later 103 toRemove.push_back(collision); 98 toRemove.push_back(collision); 104 break; // exit from the "trackIter" loop 99 break; // exit from the "trackIter" loop 105 } 100 } 106 for(size_t tcount=0; tcount<targets.size 101 for(size_t tcount=0; tcount<targets.size(); tcount++) 107 { 102 { 108 if(targets[tcount] == *trackIter) 103 if(targets[tcount] == *trackIter) 109 { 104 { 110 toRemove.push_back(collision); 105 toRemove.push_back(collision); 111 getNextCollision = true; 106 getNextCollision = true; 112 break; 107 break; 113 } 108 } 114 } 109 } 115 if(getNextCollision) break; 110 if(getNextCollision) break; 116 } 111 } 117 } 112 } 118 113 119 // now remove the collisions 114 // now remove the collisions 120 for(collIter = toRemove.begin(); collIter != 115 for(collIter = toRemove.begin(); collIter != toRemove.end(); ++collIter) 121 { 116 { 122 collision = *collIter; 117 collision = *collIter; 123 collIter2 = std::find(theCollisionList->be 118 collIter2 = std::find(theCollisionList->begin(), 124 theCollisionList->end(), collision); 119 theCollisionList->end(), collision); 125 theCollisionList->erase(collIter2); // re 120 theCollisionList->erase(collIter2); // remove from list... 126 delete collision; // .. 121 delete collision; // ...and delete the collision 127 } 122 } 128 } 123 } 129 124 130 125 131 void G4CollisionManager::ClearAndDestroy() 126 void G4CollisionManager::ClearAndDestroy() 132 { 127 { 133 std::vector<G4CollisionInitialState *>::iter 128 std::vector<G4CollisionInitialState *>::iterator i; 134 for(i = theCollisionList->begin(); i != theC 129 for(i = theCollisionList->begin(); i != theCollisionList->end(); ++i) 135 delete *i; 130 delete *i; 136 theCollisionList->clear(); 131 theCollisionList->clear(); 137 } 132 } 138 133 139 134 140 G4CollisionInitialState * G4CollisionManager:: 135 G4CollisionInitialState * G4CollisionManager::GetNextCollision() 141 { 136 { 142 G4CollisionInitialState * theNext=0; 137 G4CollisionInitialState * theNext=0; 143 G4double nextTime = DBL_MAX; 138 G4double nextTime = DBL_MAX; 144 std::vector<G4CollisionInitialState *>::iter 139 std::vector<G4CollisionInitialState *>::iterator i; 145 for(i = theCollisionList->begin(); i != theC 140 for(i = theCollisionList->begin(); i != theCollisionList->end(); ++i) 146 { 141 { 147 if(nextTime > (*i)->GetCollisionTime()) 142 if(nextTime > (*i)->GetCollisionTime()) 148 { 143 { 149 nextTime = (*i)->GetCollisionTime(); 144 nextTime = (*i)->GetCollisionTime(); 150 theNext = *i; 145 theNext = *i; 151 } 146 } 152 } 147 } 153 #ifdef debug_G4CollisionManager 148 #ifdef debug_G4CollisionManager 154 if(theNext == 0 && theCollisionList->size()! 149 if(theNext == 0 && theCollisionList->size()!=0) 155 { 150 { 156 G4double debugTime = DBL_MAX; 151 G4double debugTime = DBL_MAX; 157 G4cerr <<"G4CollisionManager::GetNextColli 152 G4cerr <<"G4CollisionManager::GetNextCollision - Fatal"<<G4endl; 158 G4cerr <<" number of collisions left "<<th 153 G4cerr <<" number of collisions left "<<theCollisionList->size()<<G4endl; 159 for(i = theCollisionList->begin(); i != th 154 for(i = theCollisionList->begin(); i != theCollisionList->end(); ++i) 160 { 155 { 161 G4cerr <<" Time to collision "<<(*i)->Ge 156 G4cerr <<" Time to collision "<<(*i)->GetCollisionTime()<<" "<<G4endl; 162 G4cerr <<" projectile "<<(*i)->GetPri 157 G4cerr <<" projectile "<<(*i)->GetPrimary()->Get4Momentum()<<" " 163 <<(*i)->GetPri 158 <<(*i)->GetPrimary()->GetDefinition()->GetParticleName()<<G4endl; 164 if ((*i)->GetTarget()) G4cerr <<" tar 159 if ((*i)->GetTarget()) G4cerr <<" target "<<(*i)->GetTarget()->Get4Momentum()<<" " 165 <<(*i)->GetTar 160 <<(*i)->GetTarget()->GetDefinition()->GetParticleName()<<G4endl; 166 } 161 } 167 G4cerr <<"G4CollisionManager::GetNextColli 162 G4cerr <<"G4CollisionManager::GetNextCollision - End of message"<<G4endl; 168 } 163 } 169 #endif 164 #endif 170 165 171 return theNext; 166 return theNext; 172 } 167 } 173 168 174 169 175 void G4CollisionManager::Print() 170 void G4CollisionManager::Print() 176 { 171 { 177 std::vector<G4CollisionInitialState *>::iter 172 std::vector<G4CollisionInitialState *>::iterator i; 178 << 173 179 G4cout << "CollisionManager: " << theCollisi 174 G4cout << "CollisionManager: " << theCollisionList->size() 180 << " entries at " << theCollisionList << G4 175 << " entries at " << theCollisionList << G4endl; 181 G4CollisionInitialState * collision; 176 G4CollisionInitialState * collision; 182 for(i = theCollisionList->begin(); i != theC 177 for(i = theCollisionList->begin(); i != theCollisionList->end(); ++i) 183 { 178 { 184 collision = *i; 179 collision = *i; 185 G4int tgtPdg=collision->GetTarget() ? << 186 collision->GetTarget()->GetDefinition()-> << 187 const G4BCAction &action= *collision->GetGe << 188 G4cout << " collision " << collision << " 180 G4cout << " collision " << collision << " time: " 189 << collision->GetCollisionTime()/second < 181 << collision->GetCollisionTime()/second << " proj: " 190 << collision->GetPrimary() << "/pdg=" << 182 << collision->GetPrimary() << " trgt: " 191 << collision->GetPrimary()->GetDefinition << 183 << collision->GetTarget() << G4endl; 192 << " trgt: " << 193 << collision->GetTarget() << "/pdg=" << 194 << tgtPdg << 195 << " Collision type: "<< typeid(action).n << 196 << G4endl; << 197 } 184 } 198 } 185 } 199 186