CMS 3D CMS Logo

Classes | Public Types | Public Member Functions | Private Member Functions | Private Attributes

SimTrackManager Class Reference

#include <SimG4Core/Application/interface/SimTrackManager.h>

List of all members.

Classes

class  StrictWeakOrdering

Public Types

typedef std::pair< int,
math::XYZVectorD
MapVertexPosition
 this map contains association between vertex number and position
typedef std::vector< std::pair
< int, math::XYZVectorD > > 
MapVertexPositionVector
typedef std::map< int,
MapVertexPositionVector
MotherParticleToVertexMap
typedef MotherParticleToVertexMap VertexMap

Public Member Functions

void addTkCaloStateInfo (uint32_t t, std::pair< math::XYZVectorD, math::XYZTLorentzVectorD > p)
void addTrack (TrackWithHistory *iTrack, bool inHistory, bool withAncestor)
void cleanTkCaloStateInfoMap ()
void cleanTracksWithHistory ()
void deleteTracks ()
int giveMotherNeeded (int i) const
void reset ()
void setCollapsePrimaryVertices (bool iSet)
void setLHCTransportLink (const edm::LHCTransportLinkContainer *thisLHCTlink)
 SimTrackManager (bool iCollapsePrimaryVertices=false)
void storeTracks (G4SimEvent *simEvent)
const TrackContainertrackContainer () const
bool trackExists (unsigned int i) const
virtual ~SimTrackManager ()

Private Member Functions

void cleanVertexMap ()
void fillMotherList ()
int getOrCreateVertex (TrackWithHistory *, int, G4SimEvent *simEvent)
int idSavedTrack (int) const
const SimTrackManageroperator= (const SimTrackManager &)
void reallyStoreTracks (G4SimEvent *simEvent)
void resetGenID ()
void saveTrackAndItsBranch (TrackWithHistory *)
 this saves a track and all its parents looping over the non ordered vector
 SimTrackManager (const SimTrackManager &)

Private Attributes

std::vector< std::pair< int,
int > > 
ancestorList
std::vector< std::pair< int,
int > > 
idsave
unsigned int lastHist
unsigned int lastTrack
bool m_collapsePrimaryVertices
int m_nVertices
bool m_SaveSimTracks
TrackContainerm_trksForThisEvent
MotherParticleToVertexMap m_vertexMap
std::map< uint32_t, std::pair
< math::XYZVectorD,
math::XYZTLorentzVectorD > > 
mapTkCaloStateInfo
const
edm::LHCTransportLinkContainer
theLHCTlink

Detailed Description

Description: Holds tracking information used by the sensitive detectors

Usage: <usage>

Definition at line 35 of file SimTrackManager.h.


Member Typedef Documentation

this map contains association between vertex number and position

Definition at line 46 of file SimTrackManager.h.

typedef std::vector<std::pair<int,math::XYZVectorD> > SimTrackManager::MapVertexPositionVector

Definition at line 47 of file SimTrackManager.h.

Definition at line 48 of file SimTrackManager.h.

Definition at line 49 of file SimTrackManager.h.


Constructor & Destructor Documentation

SimTrackManager::SimTrackManager ( bool  iCollapsePrimaryVertices = false)

Definition at line 37 of file SimTrackManager.cc.

SimTrackManager::~SimTrackManager ( ) [virtual]

Definition at line 43 of file SimTrackManager.cc.

References deleteTracks(), and m_trksForThisEvent.

{
  if ( m_trksForThisEvent != 0 ) deleteTracks() ;
}
SimTrackManager::SimTrackManager ( const SimTrackManager ) [private]

Member Function Documentation

void SimTrackManager::addTkCaloStateInfo ( uint32_t  t,
std::pair< math::XYZVectorD, math::XYZTLorentzVectorD p 
) [inline]

Definition at line 74 of file SimTrackManager.h.

References mapTkCaloStateInfo.

Referenced by EventAction::addTkCaloStateInfo().

                                                                                      {
    std::map<uint32_t,std::pair<math::XYZVectorD,math::XYZTLorentzVectorD> >::const_iterator it = 
      mapTkCaloStateInfo.find(t);
    
    if (it ==  mapTkCaloStateInfo.end())
      mapTkCaloStateInfo.insert(std::pair<uint32_t,std::pair<math::XYZVectorD,math::XYZTLorentzVectorD> >(t,p));
    
  }
void SimTrackManager::addTrack ( TrackWithHistory iTrack,
bool  inHistory,
bool  withAncestor 
) [inline]

Definition at line 67 of file SimTrackManager.h.

References ancestorList, idsave, m_trksForThisEvent, TrackWithHistory::parentID(), and TrackWithHistory::trackID().

Referenced by EventAction::addTrack().

                                                                             {
    std::pair<int, int> thePair(iTrack->trackID(),iTrack->parentID());
    idsave.push_back(thePair);
    if (inHistory) m_trksForThisEvent->push_back(iTrack);
    if (withAncestor) { std::pair<int,int> thisPair(iTrack->trackID(),0); ancestorList.push_back(thisPair); }
  }
void SimTrackManager::cleanTkCaloStateInfoMap ( )

Definition at line 210 of file SimTrackManager.cc.

References mapTkCaloStateInfo, and swap().

Referenced by EventAction::EndOfEventAction(), and reset().

                                              { 
  mapTkCaloStateInfo.clear();
  std::map<uint32_t,std::pair<math::XYZVectorD,math::XYZTLorentzVectorD > >().swap(mapTkCaloStateInfo);
}
void SimTrackManager::cleanTracksWithHistory ( )

Definition at line 250 of file SimTrackManager.cc.

References fillMotherList(), idsave, lastTrack, LogDebug, m_trksForThisEvent, TrackWithHistory::saved(), saveTrackAndItsBranch(), edm::second(), lumiQTWidget::t, and TrackWithHistory::trackID().

Referenced by EventAction::prepareForNewPrimary(), and storeTracks().

                                            {

  using namespace std;

  if ((*m_trksForThisEvent).size() == 0 && idsave.size() == 0) return;

#ifdef DebugLog
  LogDebug("SimTrackManager") << "SimTrackManager::cleanTracksWithHistory has " << idsave.size() 
                              << " mother-daughter relationships stored with lastTrack = " << lastTrack;
#endif

  if ( lastTrack > 0 && lastTrack >= (*m_trksForThisEvent).size() ) {
    lastTrack = 0;
    edm::LogError("SimTrackManager") << " SimTrackManager::cleanTracksWithHistory track index corrupted";
  }
  
  stable_sort(m_trksForThisEvent->begin()+lastTrack,m_trksForThisEvent->end(),trkIDLess());
  
  stable_sort(idsave.begin(),idsave.end());
  
#ifdef DebugLog
  LogDebug("SimTrackManager")  << " SimTrackManager::cleanTracksWithHistory knows " << m_trksForThisEvent->size()
                               << " tracks with history before branching";
  for (unsigned int it =0;  it <(*m_trksForThisEvent).size(); it++)
    LogDebug("SimTrackManager")   << " 1 - Track in position " << it << " G4 track number "
                                  << (*m_trksForThisEvent)[it]->trackID()
                                  << " mother " << (*m_trksForThisEvent)[it]->parentID()
                                  << " status " << (*m_trksForThisEvent)[it]->saved();
#endif  

  for (unsigned int it = lastTrack; it < m_trksForThisEvent->size(); it++)
    {
      TrackWithHistory * t = (*m_trksForThisEvent)[it];
      if (t->saved()) saveTrackAndItsBranch(t);
    }
  unsigned int num = lastTrack;
  for (unsigned int it = lastTrack; it < m_trksForThisEvent->size(); it++)
    {
      TrackWithHistory * t = (*m_trksForThisEvent)[it];
      int g4ID = t->trackID();
      if (t->saved() == true)
        {
          if (it>num) (*m_trksForThisEvent)[num] = t;
          num++;
          for (unsigned int itr=0; itr<idsave.size(); itr++) { 
            if ((idsave[itr]).first == g4ID) { 
              (idsave[itr]).second = g4ID; break; } 
          }
        }
      else 
        {       
          delete t;
        }
    }
  
  (*m_trksForThisEvent).resize(num);
  
#ifdef DebugLog
  LogDebug("SimTrackManager")  << " AFTER CLEANING, I GET " << (*m_trksForThisEvent).size()
                               << " tracks to be saved persistently";
  for (unsigned int it = 0;  it < (*m_trksForThisEvent).size(); it++)
    LogDebug("SimTrackManager")   << " Track in position " << it
                                  << " G4 track number " << (*m_trksForThisEvent)[it]->trackID()
                                  << " mother " << (*m_trksForThisEvent)[it]->parentID()
                                  << " Status " << (*m_trksForThisEvent)[it]->saved();
#endif  

  fillMotherList();

  lastTrack = (*m_trksForThisEvent).size();

}
void SimTrackManager::cleanVertexMap ( ) [private]

Definition at line 204 of file SimTrackManager.cc.

References m_nVertices, and m_vertexMap.

Referenced by reset().

void SimTrackManager::deleteTracks ( )

Definition at line 81 of file SimTrackManager.cc.

References i, and m_trksForThisEvent.

Referenced by EventAction::EndOfEventAction(), and ~SimTrackManager().

{
  for (unsigned int i = 0; i < m_trksForThisEvent->size(); i++) delete (*m_trksForThisEvent)[i];
  delete m_trksForThisEvent;
  m_trksForThisEvent = 0;
}
void SimTrackManager::fillMotherList ( ) [private]

Definition at line 228 of file SimTrackManager.cc.

References ancestorList, first, idsave, idSavedTrack(), lastHist, LogDebug, and n.

Referenced by cleanTracksWithHistory().

                                     {

  if ( ancestorList.size() > 0 && lastHist > ancestorList.size() ) {
    lastHist = ancestorList.size();
    edm::LogError("SimTrackManager") << " SimTrackManager::fillMotherList track index corrupted";
  }

  for (unsigned int n = lastHist; n < ancestorList.size(); n++) { 
    
    int theMotherId = idSavedTrack((ancestorList[n]).first);
    ancestorList[n].second = theMotherId;
#ifdef DebugLog
    LogDebug("SimTrackManager")  << "Track ID = " << (ancestorList[n]).first << " Mother ID = " << (ancestorList[n]).second;
#endif    
  }

  lastHist = ancestorList.size();

  idsave.clear();

}
int SimTrackManager::getOrCreateVertex ( TrackWithHistory trkH,
int  iParentID,
G4SimEvent simEvent 
) [private]

Definition at line 172 of file SimTrackManager.cc.

References G4SimEvent::add(), CastorDataFrameFilter_impl::check(), first, TrackWithHistory::globalTime(), gen::k, m_nVertices, m_vertexMap, dbtoconf::parent, mathSSE::sqrt(), and TrackWithHistory::vertexPosition().

Referenced by reallyStoreTracks().

                                                             {
  
  int parent = iParentID;
  int check = -1;
  
  for( std::vector<TrackWithHistory*>::const_iterator it = (*m_trksForThisEvent).begin(); 
       it!= (*m_trksForThisEvent).end();it++){
    if ((*it)->trackID() == uint32_t(parent)){
      check = 0;
      break;
    }
  }
  
  if(check==-1) parent = -1;
  
  VertexMap::const_iterator iterator = m_vertexMap.find(parent);
  if (iterator != m_vertexMap.end()){
    // loop over saved vertices
    for (unsigned int k=0; k<m_vertexMap[parent].size(); k++){
      if (sqrt((trkH->vertexPosition()-(((m_vertexMap[parent])[k]).second)).Mag2())<0.001)
        return (((m_vertexMap[parent])[k]).first);
    }
  }
  
  simEvent->add(new G4SimVertex(trkH->vertexPosition(),trkH->globalTime(),parent));
  m_vertexMap[parent].push_back(MapVertexPosition(m_nVertices,trkH->vertexPosition()));
  m_nVertices++;
  return (m_nVertices-1);
  
}
int SimTrackManager::giveMotherNeeded ( int  i) const [inline]

Definition at line 85 of file SimTrackManager.h.

References i, idsave, and edm::second().

Referenced by CaloSD::saveHit().

                                    { 
    int theResult = 0;
    for (unsigned int itr=0; itr<idsave.size(); itr++) { if ((idsave[itr]).first == i) { theResult = (idsave[itr]).second; break; } }
    return theResult ; 
  }
int SimTrackManager::idSavedTrack ( int  i) const [private]

Definition at line 215 of file SimTrackManager.cc.

References i, errorMatrix2Lands_multiChannel::id, idsave, and edm::second().

Referenced by fillMotherList().

{

  int id = 0;  
  if (i > 0) {
    for (unsigned int itr=0; itr<idsave.size(); itr++) { if ((idsave[itr]).first == i) { id = (idsave[itr]).second; break; } }
      if (id != i) return idSavedTrack(id);
      id = i;
  }
  return id;
}
const SimTrackManager& SimTrackManager::operator= ( const SimTrackManager ) [private]
void SimTrackManager::reallyStoreTracks ( G4SimEvent simEvent) [private]

Definition at line 136 of file SimTrackManager.cc.

References G4SimEvent::add(), TrackWithHistory::genParticleID(), getOrCreateVertex(), LogDebug, m_trksForThisEvent, mapTkCaloStateInfo, TrackWithHistory::momentum(), TrackWithHistory::parentID(), TrackWithHistory::particleID(), TrackWithHistory::totalEnergy(), and TrackWithHistory::trackID().

Referenced by storeTracks().

{
  // loop over the (now ordered) vector and really save the tracks
#ifdef DebugLog
  LogDebug("SimTrackManager")  << "Inside the reallyStoreTracks method object to be stored = " 
                               << m_trksForThisEvent->size();
#endif 
 
  for (unsigned int it = 0; it < m_trksForThisEvent->size(); it++)
    {
      TrackWithHistory * trkH = (*m_trksForThisEvent)[it];
      // at this stage there is one vertex per track, so the vertex id of track N is also N
      int ivertex = -1;
      int ig;
      
      math::XYZVectorD pm(0.,0.,0.);
      unsigned int iParentID = trkH->parentID();
      for(unsigned int iit = 0; iit < m_trksForThisEvent->size(); iit++)
        {
          if((*m_trksForThisEvent)[iit]->trackID()==iParentID){
            pm = (*m_trksForThisEvent)[iit]->momentum();
            break;
          }
        }
      ig = trkH->genParticleID();
      ivertex = getOrCreateVertex(trkH,iParentID,simEvent);
      std::map<uint32_t,std::pair<math::XYZVectorD,math::XYZTLorentzVectorD> >::const_iterator cit = mapTkCaloStateInfo.find(trkH->trackID());
      std::pair<math::XYZVectorD,math::XYZTLorentzVectorD> tcinfo;
      if (cit !=  mapTkCaloStateInfo.end()){
        tcinfo = cit->second;
      }
      simEvent->add(new G4SimTrack(trkH->trackID(),trkH->particleID(),
                                   trkH->momentum(),trkH->totalEnergy(),ivertex,ig,pm,tcinfo.first,tcinfo.second));
    }
}
void SimTrackManager::reset ( void  )

Definition at line 63 of file SimTrackManager.cc.

References ancestorList, cleanTkCaloStateInfoMap(), cleanVertexMap(), i, idsave, lastHist, lastTrack, m_trksForThisEvent, and swap().

Referenced by EventAction::BeginOfEventAction().

{
  if (m_trksForThisEvent==0) m_trksForThisEvent = new TrackContainer();
  else
    {
      for (unsigned int i = 0; i < m_trksForThisEvent->size(); i++) 
        delete (*m_trksForThisEvent)[i];
      delete m_trksForThisEvent;
      m_trksForThisEvent = new TrackContainer();
    }
  cleanVertexMap();
  cleanTkCaloStateInfoMap();
  std::vector<std::pair <int, int> >().swap(idsave);
  ancestorList.clear();
  lastTrack=0;
  lastHist=0;
}
void SimTrackManager::resetGenID ( ) [private]

Definition at line 323 of file SimTrackManager.cc.

References TrackWithHistory::genParticleID(), m_trksForThisEvent, TrackWithHistory::setGenParticleID(), and theLHCTlink.

Referenced by storeTracks().

                                 {

  if ( theLHCTlink == 0 ) return;

  for  (unsigned int it = 0; it < m_trksForThisEvent->size(); it++)
    {
      TrackWithHistory * trkH = (*m_trksForThisEvent)[it];
      int genParticleID_ = trkH->genParticleID();
      if ( genParticleID_ == -1 ) { continue; }
      else {
        for ( unsigned int itrlink = 0; itrlink < (*theLHCTlink).size(); itrlink++ ) {
          if ( (*theLHCTlink)[itrlink].afterHector() == genParticleID_ ) {
            trkH->setGenParticleID( (*theLHCTlink)[itrlink].beforeHector() );
            continue;
          }
        }
      }
    }

  theLHCTlink = 0;

}
void SimTrackManager::saveTrackAndItsBranch ( TrackWithHistory trkWHist) [private]

this saves a track and all its parents looping over the non ordered vector

Definition at line 89 of file SimTrackManager.cc.

References m_trksForThisEvent, and dbtoconf::parent.

Referenced by cleanTracksWithHistory().

{
  using namespace std;
  TrackWithHistory * trkH = trkWHist;
  if (trkH == 0)
    {
      edm::LogError("SimTrackManager") << " SimTrackManager::saveTrackAndItsBranch got 0 pointer ";
      abort();
    }
  trkH->save();
  unsigned int parent = trkH->parentID();
  bool parentExists=false;
  
  TrackContainer::const_iterator tk_itr = std::lower_bound((*m_trksForThisEvent).begin(),(*m_trksForThisEvent).end(),
                                                           parent,SimTrackManager::StrictWeakOrdering());

  TrackWithHistory * tempTk = *tk_itr;
  //  TrackWithHistory * tempTk = new TrackWithHistory(**tk_itr);
  if (tk_itr!=m_trksForThisEvent->end() && (*tk_itr)->trackID()==parent) { 
    parentExists=true;  
  }
  
  if (parentExists) saveTrackAndItsBranch(tempTk);
  
  //  delete tempTk;
  
}
void SimTrackManager::setCollapsePrimaryVertices ( bool  iSet) [inline]

Definition at line 82 of file SimTrackManager.h.

References m_collapsePrimaryVertices.

Referenced by EventAction::EventAction().

void SimTrackManager::setLHCTransportLink ( const edm::LHCTransportLinkContainer thisLHCTlink) [inline]

Definition at line 100 of file SimTrackManager.h.

References theLHCTlink.

{ theLHCTlink = thisLHCTlink; }
void SimTrackManager::storeTracks ( G4SimEvent simEvent)

Definition at line 117 of file SimTrackManager.cc.

References ancestorList, cleanTracksWithHistory(), idsave, m_trksForThisEvent, reallyStoreTracks(), resetGenID(), and swap().

Referenced by EventAction::EndOfEventAction().

{
  cleanTracksWithHistory();

  // fill the map with the final mother-daughter relationship
  idsave.swap(ancestorList);
  stable_sort(idsave.begin(),idsave.end());

  std::vector<std::pair<int,int> >().swap(ancestorList);

  // to get a backward compatible order
  stable_sort(m_trksForThisEvent->begin(),m_trksForThisEvent->end(),trkIDLess());

  // to reset the GenParticle ID of a SimTrack to its pre-LHCTransport value
  resetGenID();

  reallyStoreTracks(simEvent);
}
const TrackContainer* SimTrackManager::trackContainer ( ) const [inline]

Definition at line 55 of file SimTrackManager.h.

References m_trksForThisEvent.

Referenced by EventAction::trackContainer(), and CaloSD::update().

                                                { 
    return m_trksForThisEvent; 
  }
bool SimTrackManager::trackExists ( unsigned int  i) const [inline]

Definition at line 90 of file SimTrackManager.h.

References i, and m_trksForThisEvent.

Referenced by CaloSD::saveHit().

                                         {
    bool flag = false;
    for (unsigned int itr=0; itr<(*m_trksForThisEvent).size(); ++itr) {
      if ((*m_trksForThisEvent)[itr]->trackID() == i) {
        flag = true; break;
      }
    }
    return flag;
  }

Member Data Documentation

std::vector<std::pair<int, int> > SimTrackManager::ancestorList [private]

Definition at line 126 of file SimTrackManager.h.

Referenced by addTrack(), fillMotherList(), reset(), and storeTracks().

std::vector< std::pair<int, int> > SimTrackManager::idsave [private]
unsigned int SimTrackManager::lastHist [private]

Definition at line 129 of file SimTrackManager.h.

Referenced by fillMotherList(), and reset().

unsigned int SimTrackManager::lastTrack [private]

Definition at line 128 of file SimTrackManager.h.

Referenced by cleanTracksWithHistory(), and reset().

Definition at line 122 of file SimTrackManager.h.

Referenced by setCollapsePrimaryVertices().

Definition at line 121 of file SimTrackManager.h.

Referenced by cleanVertexMap(), and getOrCreateVertex().

Definition at line 119 of file SimTrackManager.h.

Definition at line 120 of file SimTrackManager.h.

Referenced by cleanVertexMap(), and getOrCreateVertex().

Definition at line 131 of file SimTrackManager.h.

Referenced by resetGenID(), and setLHCTransportLink().