CMS 3D CMS Logo

SimTrackManager.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:     Application
00004 // Class  :     SimTrackManager
00005 // 
00006 // Implementation:
00007 //     <Notes on implementation>
00008 //
00009 // Original Author:  
00010 //         Created:  Fri Nov 25 17:44:19 EST 2005
00011 // $Id: SimTrackManager.cc,v 1.16 2008/01/09 10:42:05 fambrogl Exp $
00012 //
00013 
00014 // system include files
00015 #include <iostream>
00016 
00017 // user include files
00018 #include "SimG4Core/Application/interface/SimTrackManager.h"
00019 #include "SimG4Core/Application/interface/G4SimTrack.h"
00020 #include "SimG4Core/Application/interface/G4SimVertex.h"
00021 
00022 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00023 
00024 
00025 //
00026 // constants, enums and typedefs
00027 //
00028 
00029 //
00030 // static data member definitions
00031 //
00032 
00033 //
00034 // constructors and destructor
00035 //
00036 SimTrackManager::SimTrackManager(bool iCollapsePrimaryVertices) :
00037   m_trksForThisEvent(0),m_nVertices(0),
00038   m_collapsePrimaryVertices(iCollapsePrimaryVertices)
00039 {
00040   idsave.reserve(1000);
00041   niteration=1;
00042   avgsizeidsave=0;
00043   sizeidsave=0;
00044   avgcalomapsize=0;
00045   calomapsize=0;
00046   avgvtxmapsize=0;
00047   vtxmapsize=0;
00048 }
00049 
00050 
00051 SimTrackManager::~SimTrackManager()
00052 {
00053    if ( m_trksForThisEvent != 0 ) deleteTracks() ;
00054 }
00055 
00056 //
00057 // assignment operators
00058 //
00059 // const SimTrackManager& SimTrackManager::operator=(const SimTrackManager& rhs)
00060 // {
00061 //   //An exception safe implementation is
00062 //   SimTrackManager temp(rhs);
00063 //   swap(rhs);
00064 //
00065 //   return *this;
00066 // }
00067 
00068 //
00069 // member functions
00070 //
00071 void SimTrackManager::reset()
00072 {
00073   if (m_trksForThisEvent==0) m_trksForThisEvent = new TrackContainer();
00074   else
00075     {
00076       for (unsigned int i = 0; i < m_trksForThisEvent->size(); i++) 
00077         delete (*m_trksForThisEvent)[i];
00078       delete m_trksForThisEvent;
00079         m_trksForThisEvent = new TrackContainer();
00080     }
00081   cleanVertexMap();
00082   cleanTkCaloStateInfoMap();
00083   avgsizeidsave+=idsave.size();
00084   sizeidsave=idsave.size();
00085   if(niteration>30){
00086     if(sizeidsave>(avgsizeidsave/niteration)*1.5){
00087       std::vector<int>().swap(idsave);
00088       idsave.reserve(1000);
00089     }else{
00090       idsave.clear();
00091     }
00092   }else{
00093     idsave.clear();
00094   }
00095   niteration++;
00096 }
00097 
00098 void SimTrackManager::deleteTracks()
00099 {
00100     for (unsigned int i = 0; i < m_trksForThisEvent->size(); i++) delete (*m_trksForThisEvent)[i];
00101     delete m_trksForThisEvent;
00102     m_trksForThisEvent = 0;
00103 }
00104 
00106 void SimTrackManager::saveTrackAndItsBranch(TrackWithHistory * trkWHist)
00107 {
00108     using namespace std;
00109     TrackWithHistory * trkH = trkWHist;
00110     if (trkH == 0)
00111     {
00112         edm::LogError("SimG4CoreApplication") << " SimTrackManager::saveTrackAndItsBranch got 0 pointer ";
00113         abort();
00114     }
00115     trkH->save();
00116     unsigned int parent = trkH->parentID();
00117     bool parentExists=false;
00118 
00119     TrackContainer::const_iterator tk_itr = std::lower_bound((*m_trksForThisEvent).begin(),(*m_trksForThisEvent).end(),
00120                                                       parent,SimTrackManager::StrictWeakOrdering());
00121     TrackWithHistory * tempTk = new TrackWithHistory(**tk_itr);
00122     if (tk_itr!=m_trksForThisEvent->end() && (*tk_itr)->trackID()==parent) { 
00123       parentExists=true;  
00124     }
00125 
00126     if (parentExists) saveTrackAndItsBranch(tempTk);
00127 
00128     delete tempTk;
00129 
00130 }
00131 
00132 void SimTrackManager::storeTracks(G4SimEvent* simEvent)
00133 {
00134     using namespace std;
00135 
00136     stable_sort(m_trksForThisEvent->begin(),m_trksForThisEvent->end(),trkIDLess());
00137     
00138     LogDebug("SimTrackManager")  << " SimTrackManager::storeTracks knows " << m_trksForThisEvent->size()
00139               << " tracks with history before branching";
00140     for (unsigned int it =0;  it <(*m_trksForThisEvent).size(); it++)
00141       LogDebug("SimTrackManager")   << " 1 - Track in position " << it << " G4 track number "
00142                  << (*m_trksForThisEvent)[it]->trackID()
00143                  << " mother " << (*m_trksForThisEvent)[it]->parentID()
00144                  << " status " << (*m_trksForThisEvent)[it]->saved();
00145 
00146     for (unsigned int i = 0; i < m_trksForThisEvent->size(); i++)
00147       {
00148         TrackWithHistory * t = (*m_trksForThisEvent)[i];
00149         if (t->saved()) saveTrackAndItsBranch(t);
00150       }
00151     
00152     // now eliminate from the vector the tracks with only history but not save
00153     unsigned int num = 0;
00154     for (unsigned int it = 0;  it < (*m_trksForThisEvent).size(); it++)
00155       {
00156         int g4ID = (*m_trksForThisEvent)[it]->trackID();
00157         if ((*m_trksForThisEvent)[it]->saved() == true)
00158           {
00159             if (it>num) (*m_trksForThisEvent)[num] = (*m_trksForThisEvent)[it];
00160             num++;
00161             idsave[g4ID] = g4ID;
00162           }
00163         else 
00164           {     
00165             delete (*m_trksForThisEvent)[it];
00166           }
00167       }
00168     
00169     (*m_trksForThisEvent).resize(num);
00170     
00171     LogDebug("SimTrackManager")  << " AFTER CLEANING, I GET " << (*m_trksForThisEvent).size()
00172               << " tracks to be saved persistently";
00173     for (unsigned int it = 0;  it < (*m_trksForThisEvent).size(); it++)
00174       LogDebug("SimTrackManager")   << " Track in position " << it
00175                  << " G4 track number " << (*m_trksForThisEvent)[it]->trackID()
00176                  << " mother " << (*m_trksForThisEvent)[it]->parentID()
00177                  << " Status " << (*m_trksForThisEvent)[it]->saved();
00178     
00179     reallyStoreTracks(simEvent);
00180 }
00181 
00182 void SimTrackManager::reallyStoreTracks(G4SimEvent * simEvent)
00183 {
00184     // loop over the (now ordered) vector and really save the tracks
00185   LogDebug("SimTrackManager")  << "Inside the reallyStoreTracks method object to be stored = " 
00186             << m_trksForThisEvent->size();
00187 
00188     for (unsigned int it = 0; it < m_trksForThisEvent->size(); it++)
00189     {
00190         TrackWithHistory * trkH = (*m_trksForThisEvent)[it];
00191         // at this stage there is one vertex per track, so the vertex id of track N is also N
00192         int ivertex = -1;
00193         int ig;
00194 
00195         math::XYZVectorD pm(0.,0.,0.);
00196         unsigned int iParentID = trkH->parentID();
00197         for(unsigned int iit = 0; iit < m_trksForThisEvent->size(); iit++)
00198           {
00199             if((*m_trksForThisEvent)[iit]->trackID()==iParentID){
00200               pm = (*m_trksForThisEvent)[iit]->momentum();
00201               break;
00202             }
00203           }
00204         ig = trkH->genParticleID();
00205         ivertex = getOrCreateVertex(trkH,iParentID,simEvent);
00206         std::map<uint32_t,std::pair<math::XYZVectorD,math::XYZTLorentzVectorD> >::const_iterator it = mapTkCaloStateInfo.find(trkH->trackID());
00207         std::pair<math::XYZVectorD,math::XYZTLorentzVectorD> tcinfo;
00208         if (it !=  mapTkCaloStateInfo.end()){
00209           tcinfo =  it->second;
00210         }
00211         simEvent->add(new G4SimTrack(trkH->trackID(),trkH->particleID(),
00212                                      trkH->momentum(),trkH->totalEnergy(),ivertex,ig,pm,tcinfo.first,tcinfo.second));
00213     }
00214 }
00215 
00216 int SimTrackManager::getOrCreateVertex(TrackWithHistory * trkH, int iParentID,
00217                                        G4SimEvent * simEvent){
00218 
00219   int parent = iParentID;
00220   int check = -1;
00221 
00222   for( std::vector<TrackWithHistory*>::const_iterator it = (*m_trksForThisEvent).begin(); 
00223        it!= (*m_trksForThisEvent).end();it++){
00224     if ((*it)->trackID() == uint32_t(parent)){
00225       check = 0;
00226       break;
00227     }
00228   }
00229 
00230   if(check==-1) parent = -1;
00231 
00232   VertexMap::const_iterator iterator = m_vertexMap.find(parent);
00233   if (iterator != m_vertexMap.end()){
00234     // loop over saved vertices
00235     for (unsigned int k=0; k<m_vertexMap[parent].size(); k++){
00236       if (sqrt((trkH->vertexPosition()-(((m_vertexMap[parent])[k]).second)).Mag2())<0.001)
00237         return (((m_vertexMap[parent])[k]).first);
00238     }
00239   }
00240   
00241   simEvent->add(new G4SimVertex(trkH->vertexPosition(),trkH->globalTime(),parent));
00242   m_vertexMap[parent].push_back(MapVertexPosition(m_nVertices,trkH->vertexPosition()));
00243   m_nVertices++;
00244   return (m_nVertices-1);
00245 
00246 }
00247 
00248 void SimTrackManager::cleanVertexMap() { 
00249   avgvtxmapsize+=m_vertexMap.size();
00250   vtxmapsize=m_vertexMap.size();
00251   m_vertexMap.clear();
00252   if(niteration>30){
00253     if(vtxmapsize>(avgvtxmapsize/niteration)*1.1){
00254       MotherParticleToVertexMap().swap(m_vertexMap);
00255     }
00256   }
00257   m_nVertices=0; 
00258 }
00259 
00260 void SimTrackManager::cleanTkCaloStateInfoMap() { 
00261   avgcalomapsize+=mapTkCaloStateInfo.size();
00262   calomapsize=mapTkCaloStateInfo.size();
00263   mapTkCaloStateInfo.clear();
00264   if(niteration>30){
00265     if(calomapsize>(avgcalomapsize/niteration)*1.1){
00266       std::map<uint32_t,std::pair<math::XYZVectorD,math::XYZTLorentzVectorD > >().swap(mapTkCaloStateInfo);
00267     }
00268   }
00269 }
00270 
00271 int SimTrackManager::idSavedTrack (int i) const
00272 {
00273   int id = 0;  
00274   if (i > 0) {
00275     id = idsave[i];
00276     if(id<0)
00277       id=0;
00278   }
00279   return id;
00280 }

Generated on Tue Jun 9 17:47:00 2009 for CMSSW by  doxygen 1.5.4