G4CollisionManager.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 
00027 #include <typeinfo>
00028 
00029 #include "G4CollisionManager.hh"
00030 #include "G4SystemOfUnits.hh"
00031 #include "G4HadronicException.hh"
00032 #include "G4CollisionInitialState.hh"
00033 #include "G4BCAction.hh"
00034 
00035 G4CollisionManager::G4CollisionManager()
00036 {
00037   theCollisionList = new G4ListOfCollisions;
00038 }
00039 
00040 
00041 G4CollisionManager::~G4CollisionManager()
00042 {
00043   ClearAndDestroy();
00044   delete theCollisionList;
00045 }
00046 
00047 
00048 void G4CollisionManager::AddCollision(G4double time, G4KineticTrack * proj,
00049                                       G4KineticTrack * target)
00050 
00051 {
00052       if ( time < DBL_MAX )
00053       {
00054          G4CollisionInitialState *
00055              collision = new G4CollisionInitialState(time, proj, target);
00056          theCollisionList->push_back(collision);
00057       } else {
00058          G4cerr << "G4Scatterer invalid TimeTo Interaction : " << time;
00059          G4cerr <<"    projectile "<<proj->Get4Momentum()<<" "
00060                                    <<proj->GetDefinition()->GetParticleName()<<G4endl;
00061          if (target) G4cerr <<"    target     "
00062                                 <<target->Get4Momentum()<<" "
00063                                 <<target->GetDefinition()->GetParticleName()<<G4endl;
00064          G4cerr <<"G4Scatterer error message end"<< G4endl;
00065          throw G4HadronicException(__FILE__, __LINE__, "G4Scatterer::AddCollision()");
00066       }
00067 }
00068 
00069 
00070 void G4CollisionManager::RemoveCollision(G4CollisionInitialState * collision)
00071 {
00072   theCollisionList->erase(std::find(theCollisionList->begin(),
00073                                       theCollisionList->end(),
00074                                       collision));
00075   delete collision;
00076   collision = NULL; // prevent future use of the pointer
00077 }
00078 
00079 
00080 void G4CollisionManager::RemoveTracksCollisions(G4KineticTrackVector * toBeCaned)
00081 {
00082   if(toBeCaned == NULL)
00083     return;
00084   if(toBeCaned->empty())
00085     return;
00086 
00087   G4CollisionInitialState * collision;
00088   std::vector<G4CollisionInitialState *>::iterator collIter, collIter2;
00089   std::vector<G4KineticTrack *>::iterator trackIter;
00090   G4ListOfCollisions toRemove;
00091 
00092   for(collIter = theCollisionList->begin();
00093       collIter != theCollisionList->end(); collIter++)
00094   {
00095     collision = *collIter;
00096     G4KineticTrackVector & targets = collision->GetTargetCollection();
00097     G4bool getNextCollision = false;
00098     for(trackIter = toBeCaned->begin(); trackIter != toBeCaned->end(); ++trackIter)
00099     {
00100       if((collision->GetTarget() == *trackIter) ||
00101          (collision->GetPrimary() == *trackIter))
00102       {  // cannot remove the collision from the list inside the loop. Save and do it later
00103         toRemove.push_back(collision);
00104         break;  // exit from the "trackIter" loop
00105       }
00106       for(size_t tcount=0; tcount<targets.size(); tcount++)
00107       {
00108         if(targets[tcount] == *trackIter)
00109         {
00110           toRemove.push_back(collision);
00111           getNextCollision = true;
00112           break;
00113         }
00114       }
00115       if(getNextCollision) break;
00116     }
00117   }
00118 
00119   // now remove the collisions
00120   for(collIter = toRemove.begin(); collIter != toRemove.end(); ++collIter)
00121   {
00122     collision = *collIter;
00123     collIter2 = std::find(theCollisionList->begin(),
00124                             theCollisionList->end(), collision);
00125     theCollisionList->erase(collIter2);  // remove from list...
00126     delete collision;                    // ...and delete the collision
00127   }
00128 }
00129 
00130 
00131 void G4CollisionManager::ClearAndDestroy()
00132 {
00133   std::vector<G4CollisionInitialState *>::iterator i;
00134   for(i = theCollisionList->begin(); i != theCollisionList->end(); ++i)
00135     delete *i;
00136   theCollisionList->clear();
00137 }
00138 
00139 
00140 G4CollisionInitialState * G4CollisionManager::GetNextCollision()
00141 {
00142   G4CollisionInitialState * theNext=0;
00143   G4double nextTime = DBL_MAX;
00144   std::vector<G4CollisionInitialState *>::iterator i;
00145   for(i = theCollisionList->begin(); i != theCollisionList->end(); ++i)
00146   {
00147     if(nextTime > (*i)->GetCollisionTime())
00148     {
00149       nextTime = (*i)->GetCollisionTime();
00150       theNext = *i;
00151     }
00152   }
00153   #ifdef debug_G4CollisionManager
00154   if(theNext == 0 && theCollisionList->size()!=0)
00155   {
00156     G4double debugTime = DBL_MAX;
00157     G4cerr <<"G4CollisionManager::GetNextCollision - Fatal"<<G4endl;
00158     G4cerr <<" number of collisions left "<<theCollisionList->size()<<G4endl;
00159     for(i = theCollisionList->begin(); i != theCollisionList->end(); ++i)
00160     {
00161       G4cerr <<" Time to collision "<<(*i)->GetCollisionTime()<<" "<<G4endl;
00162       G4cerr <<"    projectile "<<(*i)->GetPrimary()->Get4Momentum()<<" "
00163                                 <<(*i)->GetPrimary()->GetDefinition()->GetParticleName()<<G4endl;
00164       if ((*i)->GetTarget()) G4cerr <<"    target     "<<(*i)->GetTarget()->Get4Momentum()<<" "
00165                                 <<(*i)->GetTarget()->GetDefinition()->GetParticleName()<<G4endl;
00166     }
00167     G4cerr <<"G4CollisionManager::GetNextCollision - End of message"<<G4endl;
00168   }
00169   #endif
00170 
00171   return theNext;
00172 }
00173 
00174 
00175 void G4CollisionManager::Print()
00176 {
00177   std::vector<G4CollisionInitialState *>::iterator i;
00178 
00179   G4cout << "CollisionManager: " << theCollisionList->size()
00180          << " entries at " << theCollisionList << G4endl;
00181   G4CollisionInitialState * collision;
00182   for(i = theCollisionList->begin(); i != theCollisionList->end(); ++i)
00183   {
00184     collision = *i;
00185     G4int tgtPdg=collision->GetTarget() ?
00186            collision->GetTarget()->GetDefinition()->GetPDGEncoding() : 0;
00187     G4cout << "  collision " << collision << " time: "
00188            << collision->GetCollisionTime()/second << " proj: "
00189            << collision->GetPrimary() << "/pdg="
00190            << collision->GetPrimary()->GetDefinition()->GetPDGEncoding()
00191            << " trgt: "
00192            << collision->GetTarget() << "/pdg="
00193            << tgtPdg
00194            << " Collision type: "<< typeid(*collision->GetGenerator()).name()
00195            << G4endl;
00196   }
00197 }

Generated on Mon May 27 17:47:54 2013 for Geant4 by  doxygen 1.4.7