CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SimTrackManager.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: Application
4 // Class : SimTrackManager
5 //
6 // Implementation:
7 // <Notes on implementation>
8 //
9 // Original Author:
10 // Created: Fri Nov 25 17:44:19 EST 2005
11 // $Id: SimTrackManager.cc,v 1.21 2012/04/02 12:24:43 davidlt Exp $
12 //
13 
14 // system include files
15 #include <iostream>
16 
17 // user include files
21 
23 
24 //#define DebugLog
25 
26 //
27 // constants, enums and typedefs
28 //
29 
30 //
31 // static data member definitions
32 //
33 
34 //
35 // constructors and destructor
36 //
37 SimTrackManager::SimTrackManager(bool iCollapsePrimaryVertices) :
38  m_trksForThisEvent(0),m_nVertices(0),
39  m_collapsePrimaryVertices(iCollapsePrimaryVertices),
40  lastTrack(0),lastHist(0),theLHCTlink(0){}
41 
42 
44 {
45  if ( m_trksForThisEvent != 0 ) deleteTracks() ;
46 }
47 
48 //
49 // assignment operators
50 //
51 // const SimTrackManager& SimTrackManager::operator=(const SimTrackManager& rhs)
52 // {
53 // //An exception safe implementation is
54 // SimTrackManager temp(rhs);
55 // swap(rhs);
56 //
57 // return *this;
58 // }
59 
60 //
61 // member functions
62 //
64 {
66  else
67  {
68  for (unsigned int i = 0; i < m_trksForThisEvent->size(); i++)
69  delete (*m_trksForThisEvent)[i];
70  delete m_trksForThisEvent;
72  }
75  std::vector<std::pair <int, int> >().swap(idsave);
76  ancestorList.clear();
77  lastTrack=0;
78  lastHist=0;
79 }
80 
82 {
83  for (unsigned int i = 0; i < m_trksForThisEvent->size(); i++) delete (*m_trksForThisEvent)[i];
84  delete m_trksForThisEvent;
86 }
87 
90 {
91  using namespace std;
92  TrackWithHistory * trkH = trkWHist;
93  if (trkH == 0)
94  {
95  edm::LogError("SimTrackManager") << " SimTrackManager::saveTrackAndItsBranch got 0 pointer ";
96  abort();
97  }
98  trkH->save();
99  unsigned int parent = trkH->parentID();
100  bool parentExists=false;
101 
102  TrackContainer::const_iterator tk_itr = std::lower_bound((*m_trksForThisEvent).begin(),(*m_trksForThisEvent).end(),
104 
105  TrackWithHistory * tempTk = *tk_itr;
106  // TrackWithHistory * tempTk = new TrackWithHistory(**tk_itr);
107  if (tk_itr!=m_trksForThisEvent->end() && (*tk_itr)->trackID()==parent) {
108  parentExists=true;
109  }
110 
111  if (parentExists) saveTrackAndItsBranch(tempTk);
112 
113  // delete tempTk;
114 
115 }
116 
118 {
120 
121  // fill the map with the final mother-daughter relationship
122  idsave.swap(ancestorList);
123  stable_sort(idsave.begin(),idsave.end());
124 
125  std::vector<std::pair<int,int> >().swap(ancestorList);
126 
127  // to get a backward compatible order
128  stable_sort(m_trksForThisEvent->begin(),m_trksForThisEvent->end(),trkIDLess());
129 
130  // to reset the GenParticle ID of a SimTrack to its pre-LHCTransport value
131  resetGenID();
132 
133  reallyStoreTracks(simEvent);
134 }
135 
137 {
138  // loop over the (now ordered) vector and really save the tracks
139 #ifdef DebugLog
140  LogDebug("SimTrackManager") << "Inside the reallyStoreTracks method object to be stored = "
141  << m_trksForThisEvent->size();
142 #endif
143 
144  for (unsigned int it = 0; it < m_trksForThisEvent->size(); it++)
145  {
146  TrackWithHistory * trkH = (*m_trksForThisEvent)[it];
147  // at this stage there is one vertex per track, so the vertex id of track N is also N
148  int ivertex = -1;
149  int ig;
150 
151  math::XYZVectorD pm(0.,0.,0.);
152  unsigned int iParentID = trkH->parentID();
153  for(unsigned int iit = 0; iit < m_trksForThisEvent->size(); iit++)
154  {
155  if((*m_trksForThisEvent)[iit]->trackID()==iParentID){
156  pm = (*m_trksForThisEvent)[iit]->momentum();
157  break;
158  }
159  }
160  ig = trkH->genParticleID();
161  ivertex = getOrCreateVertex(trkH,iParentID,simEvent);
162  std::map<uint32_t,std::pair<math::XYZVectorD,math::XYZTLorentzVectorD> >::const_iterator cit = mapTkCaloStateInfo.find(trkH->trackID());
163  std::pair<math::XYZVectorD,math::XYZTLorentzVectorD> tcinfo;
164  if (cit != mapTkCaloStateInfo.end()){
165  tcinfo = cit->second;
166  }
167  simEvent->add(new G4SimTrack(trkH->trackID(),trkH->particleID(),
168  trkH->momentum(),trkH->totalEnergy(),ivertex,ig,pm,tcinfo.first,tcinfo.second));
169  }
170 }
171 
173  G4SimEvent * simEvent){
174 
175  int parent = iParentID;
176  int check = -1;
177 
178  for( std::vector<TrackWithHistory*>::const_iterator it = (*m_trksForThisEvent).begin();
179  it!= (*m_trksForThisEvent).end();it++){
180  if ((*it)->trackID() == uint32_t(parent)){
181  check = 0;
182  break;
183  }
184  }
185 
186  if(check==-1) parent = -1;
187 
188  VertexMap::const_iterator iterator = m_vertexMap.find(parent);
189  if (iterator != m_vertexMap.end()){
190  // loop over saved vertices
191  for (unsigned int k=0; k<m_vertexMap[parent].size(); k++){
192  if (sqrt((trkH->vertexPosition()-(((m_vertexMap[parent])[k]).second)).Mag2())<0.001)
193  return (((m_vertexMap[parent])[k]).first);
194  }
195  }
196 
197  simEvent->add(new G4SimVertex(trkH->vertexPosition(),trkH->globalTime(),parent));
199  m_nVertices++;
200  return (m_nVertices-1);
201 
202 }
203 
205  m_vertexMap.clear();
207  m_nVertices=0;
208 }
209 
211  mapTkCaloStateInfo.clear();
212  std::map<uint32_t,std::pair<math::XYZVectorD,math::XYZTLorentzVectorD > >().swap(mapTkCaloStateInfo);
213 }
214 
216 {
217 
218  int id = 0;
219  if (i > 0) {
220  for (unsigned int itr=0; itr<idsave.size(); itr++) { if ((idsave[itr]).first == i) { id = (idsave[itr]).second; break; } }
221  if (id != i) return idSavedTrack(id);
222  id = i;
223  }
224  return id;
225 }
226 
227 
229 
230  if ( ancestorList.size() > 0 && lastHist > ancestorList.size() ) {
231  lastHist = ancestorList.size();
232  edm::LogError("SimTrackManager") << " SimTrackManager::fillMotherList track index corrupted";
233  }
234 
235  for (unsigned int n = lastHist; n < ancestorList.size(); n++) {
236 
237  int theMotherId = idSavedTrack((ancestorList[n]).first);
238  ancestorList[n].second = theMotherId;
239 #ifdef DebugLog
240  LogDebug("SimTrackManager") << "Track ID = " << (ancestorList[n]).first << " Mother ID = " << (ancestorList[n]).second;
241 #endif
242  }
243 
244  lastHist = ancestorList.size();
245 
246  idsave.clear();
247 
248 }
249 
251 
252  using namespace std;
253 
254  if ((*m_trksForThisEvent).size() == 0 && idsave.size() == 0) return;
255 
256 #ifdef DebugLog
257  LogDebug("SimTrackManager") << "SimTrackManager::cleanTracksWithHistory has " << idsave.size()
258  << " mother-daughter relationships stored with lastTrack = " << lastTrack;
259 #endif
260 
261  if ( lastTrack > 0 && lastTrack >= (*m_trksForThisEvent).size() ) {
262  lastTrack = 0;
263  edm::LogError("SimTrackManager") << " SimTrackManager::cleanTracksWithHistory track index corrupted";
264  }
265 
266  stable_sort(m_trksForThisEvent->begin()+lastTrack,m_trksForThisEvent->end(),trkIDLess());
267 
268  stable_sort(idsave.begin(),idsave.end());
269 
270 #ifdef DebugLog
271  LogDebug("SimTrackManager") << " SimTrackManager::cleanTracksWithHistory knows " << m_trksForThisEvent->size()
272  << " tracks with history before branching";
273  for (unsigned int it =0; it <(*m_trksForThisEvent).size(); it++)
274  LogDebug("SimTrackManager") << " 1 - Track in position " << it << " G4 track number "
275  << (*m_trksForThisEvent)[it]->trackID()
276  << " mother " << (*m_trksForThisEvent)[it]->parentID()
277  << " status " << (*m_trksForThisEvent)[it]->saved();
278 #endif
279 
280  for (unsigned int it = lastTrack; it < m_trksForThisEvent->size(); it++)
281  {
282  TrackWithHistory * t = (*m_trksForThisEvent)[it];
283  if (t->saved()) saveTrackAndItsBranch(t);
284  }
285  unsigned int num = lastTrack;
286  for (unsigned int it = lastTrack; it < m_trksForThisEvent->size(); it++)
287  {
288  TrackWithHistory * t = (*m_trksForThisEvent)[it];
289  int g4ID = t->trackID();
290  if (t->saved() == true)
291  {
292  if (it>num) (*m_trksForThisEvent)[num] = t;
293  num++;
294  for (unsigned int itr=0; itr<idsave.size(); itr++) {
295  if ((idsave[itr]).first == g4ID) {
296  (idsave[itr]).second = g4ID; break; }
297  }
298  }
299  else
300  {
301  delete t;
302  }
303  }
304 
305  (*m_trksForThisEvent).resize(num);
306 
307 #ifdef DebugLog
308  LogDebug("SimTrackManager") << " AFTER CLEANING, I GET " << (*m_trksForThisEvent).size()
309  << " tracks to be saved persistently";
310  for (unsigned int it = 0; it < (*m_trksForThisEvent).size(); it++)
311  LogDebug("SimTrackManager") << " Track in position " << it
312  << " G4 track number " << (*m_trksForThisEvent)[it]->trackID()
313  << " mother " << (*m_trksForThisEvent)[it]->parentID()
314  << " Status " << (*m_trksForThisEvent)[it]->saved();
315 #endif
316 
317  fillMotherList();
318 
319  lastTrack = (*m_trksForThisEvent).size();
320 
321 }
322 
324 
325  if ( theLHCTlink == 0 ) return;
326 
327  for (unsigned int it = 0; it < m_trksForThisEvent->size(); it++)
328  {
329  TrackWithHistory * trkH = (*m_trksForThisEvent)[it];
330  int genParticleID_ = trkH->genParticleID();
331  if ( genParticleID_ == -1 ) { continue; }
332  else {
333  for ( unsigned int itrlink = 0; itrlink < (*theLHCTlink).size(); itrlink++ ) {
334  if ( (*theLHCTlink)[itrlink].afterHector() == genParticleID_ ) {
335  trkH->setGenParticleID( (*theLHCTlink)[itrlink].beforeHector() );
336  continue;
337  }
338  }
339  }
340  }
341 
342  theLHCTlink = 0;
343 
344 }
#define LogDebug(id)
void swap(ora::Record &rh, ora::Record &lh)
Definition: Record.h:70
int i
Definition: DBlmapReader.cc:9
int getOrCreateVertex(TrackWithHistory *, int, G4SimEvent *simEvent)
list parent
Definition: dbtoconf.py:74
bool saved() const
double totalEnergy() const
SimTrackManager(bool iCollapsePrimaryVertices=false)
void setGenParticleID(int i)
void cleanTkCaloStateInfoMap()
std::map< uint32_t, std::pair< math::XYZVectorD, math::XYZTLorentzVectorD > > mapTkCaloStateInfo
unsigned int lastHist
virtual ~SimTrackManager()
TrackContainer * m_trksForThisEvent
U second(std::pair< T, U > const &p)
void add(G4SimTrack *t)
Definition: G4SimEvent.h:35
ROOT::Math::DisplacementVector3D< ROOT::Math::Cartesian3D< double > > XYZVectorD
spatial vector with cartesian internal representation
Definition: Vector3D.h:9
std::vector< TrackWithHistory * > TrackContainer
Definition: TrackContainer.h:8
T sqrt(T t)
Definition: SSEVec.h:46
bool check(const DataFrame &df, bool capcheck, bool dvercheck)
std::vector< std::pair< int, int > > idsave
unsigned int trackID() const
void cleanTracksWithHistory()
std::pair< int, math::XYZVectorD > MapVertexPosition
this map contains association between vertex number and position
MotherParticleToVertexMap m_vertexMap
int particleID() const
void storeTracks(G4SimEvent *simEvent)
int idSavedTrack(int) const
bool first
Definition: L1TdeRCT.cc:94
int k[5][pyjets_maxn]
int parentID() const
std::map< int, MapVertexPositionVector > MotherParticleToVertexMap
long long int num
Definition: procUtils.cc:71
const math::XYZVectorD & momentum() const
void saveTrackAndItsBranch(TrackWithHistory *)
this saves a track and all its parents looping over the non ordered vector
unsigned int lastTrack
double globalTime() const
const edm::LHCTransportLinkContainer * theLHCTlink
void reallyStoreTracks(G4SimEvent *simEvent)
std::vector< std::pair< int, int > > ancestorList
const math::XYZVectorD & vertexPosition() const
int genParticleID() const