CMS 3D CMS Logo

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 //
12 
13 // system include files
14 #include <iostream>
15 
16 // user include files
20 
22 
23 #include "G4VProcess.hh"
24 
25 //#define DebugLog
26 //using namespace std;
27 
28 //
29 // constants, enums and typedefs
30 //
31 
32 //
33 // static data member definitions
34 //
35 
36 //
37 // constructors and destructor
38 //
39 SimTrackManager::SimTrackManager(bool iCollapsePrimaryVertices) :
40  m_trksForThisEvent(nullptr),m_nVertices(0),
41  m_collapsePrimaryVertices(iCollapsePrimaryVertices),
42  lastTrack(0),lastHist(0),theLHCTlink(nullptr){}
43 
44 
46 {
47  if ( m_trksForThisEvent != nullptr ) deleteTracks() ;
48 }
49 
50 //
51 // member functions
52 //
54 {
55  if (m_trksForThisEvent==nullptr) { m_trksForThisEvent = new TrackContainer(); }
56  else
57  {
58  for (unsigned int i = 0; i < m_trksForThisEvent->size(); i++) {
59  delete (*m_trksForThisEvent)[i];
60  }
61  delete m_trksForThisEvent;
63  }
66  std::vector<std::pair <int, int> >().swap(idsave);
67  ancestorList.clear();
68  lastTrack=0;
69  lastHist=0;
70 }
71 
73 {
74  for (unsigned int i = 0; i < m_trksForThisEvent->size(); i++) {
75  delete (*m_trksForThisEvent)[i];
76  }
77  delete m_trksForThisEvent;
78  m_trksForThisEvent = nullptr;
79 }
80 
83 {
84  using namespace std;
85  TrackWithHistory * trkH = trkWHist;
86  if (trkH == nullptr)
87  {
88  edm::LogError("SimTrackManager")
89  << " SimTrackManager::saveTrackAndItsBranch got 0 pointer ";
90  throw cms::Exception("SimTrackManager::saveTrackAndItsBranch")
91  << " cannot handle hits for tracking";
92  }
93  trkH->save();
94  unsigned int parent = trkH->parentID();
95 
96  TrackContainer::const_iterator tk_itr =
97  std::lower_bound((*m_trksForThisEvent).begin(),(*m_trksForThisEvent).end(),
99 
100  TrackWithHistory * tempTk = *tk_itr;
101 
102  if (tk_itr!=m_trksForThisEvent->end() && (*tk_itr)->trackID()==parent) {
103  saveTrackAndItsBranch(tempTk);
104  }
105 }
106 
108 {
110 
111  // fill the map with the final mother-daughter relationship
112  idsave.swap(ancestorList);
113  stable_sort(idsave.begin(),idsave.end());
114 
115  std::vector<std::pair<int,int> >().swap(ancestorList);
116 
117  // to get a backward compatible order
118  stable_sort(m_trksForThisEvent->begin(),m_trksForThisEvent->end(),trkIDLess());
119 
120  // to reset the GenParticle ID of a SimTrack to its pre-LHCTransport value
121  resetGenID();
122 
123  reallyStoreTracks(simEvent);
124 }
125 
127 {
128  // loop over the (now ordered) vector and really save the tracks
129 #ifdef DebugLog
130  LogDebug("SimTrackManager")
131  << "Inside the reallyStoreTracks method object to be stored = "
132  << m_trksForThisEvent->size();
133 #endif
134 
135  for (unsigned int it = 0; it < m_trksForThisEvent->size(); it++)
136  {
137  TrackWithHistory * trkH = (*m_trksForThisEvent)[it];
138  // at this stage there is one vertex per track,
139  // so the vertex id of track N is also N
140  int ivertex = -1;
141  int ig;
142 
143  math::XYZVectorD pm(0.,0.,0.);
144  unsigned int iParentID = trkH->parentID();
145  for(unsigned int iit = 0; iit < m_trksForThisEvent->size(); ++iit)
146  {
147  if((*m_trksForThisEvent)[iit]->trackID()==iParentID){
148  pm = (*m_trksForThisEvent)[iit]->momentum();
149  break;
150  }
151  }
152  ig = trkH->genParticleID();
153  ivertex = getOrCreateVertex(trkH,iParentID,simEvent);
154  std::map<uint32_t,std::pair<math::XYZVectorD,math::XYZTLorentzVectorD> >::const_iterator cit =
155  mapTkCaloStateInfo.find(trkH->trackID());
156  std::pair<math::XYZVectorD,math::XYZTLorentzVectorD> tcinfo;
157  if (cit != mapTkCaloStateInfo.end()){
158  tcinfo = cit->second;
159  }
160  simEvent->add(new G4SimTrack(trkH->trackID(),trkH->particleID(),
161  trkH->momentum(),trkH->totalEnergy(),
162  ivertex,ig,pm,tcinfo.first,tcinfo.second));
163  }
164 }
165 
167  G4SimEvent * simEvent)
168 {
169  int parent = iParentID;
170  int check = -1;
171 
172  for( std::vector<TrackWithHistory*>::const_iterator it = (*m_trksForThisEvent).begin();
173  it!= (*m_trksForThisEvent).end();it++)
174  {
175  if ((*it)->trackID() == uint32_t(parent)) {
176  check = 0;
177  break;
178  }
179  }
180 
181  if(check==-1) { parent = -1; }
182 
183  VertexMap::const_iterator iterator = m_vertexMap.find(parent);
184  if (iterator != m_vertexMap.end()) {
185 
186  // loop over saved vertices
187  for(unsigned int k=0; k<m_vertexMap[parent].size(); k++){
188  if(sqrt((trkH->vertexPosition()-(((m_vertexMap[parent])[k]).second)).Mag2())<0.001)
189  { return (((m_vertexMap[parent])[k]).first); }
190  }
191  }
192 
193  unsigned int ptype = 0;
194  const G4VProcess* pr = trkH->creatorProcess();
195  if(pr) { ptype = pr->GetProcessSubType(); }
196  simEvent->add(new G4SimVertex(trkH->vertexPosition(),trkH->globalTime(),parent,ptype));
198  m_nVertices++;
199  return (m_nVertices-1);
200 
201 }
202 
204 {
205  m_vertexMap.clear();
207  m_nVertices=0;
208 }
209 
211 {
212  mapTkCaloStateInfo.clear();
213  std::map<uint32_t,std::pair<math::XYZVectorD,math::XYZTLorentzVectorD > >().swap(mapTkCaloStateInfo);
214 }
215 
217 {
218  int idMother = id;
219  if(id > 0) {
220  unsigned int n = idsave.size();
221  if(0 < n) {
222  int jmax = n - 1;
223  int j, id1;
224 
225  // first loop forward
226  bool notFound = true;
227  for(j=0; j<=jmax; ++j) {
228  if((idsave[j]).first == idMother) {
229  id1 = (idsave[j]).second;
230  if(0 == id1 || id1 == idMother) { return id1; }
231  jmax = j - 1;
232  idMother = id1;
233  notFound = false;
234  break;
235  }
236  }
237  if(notFound) { return 0; }
238 
239  // recursive loop
240  do {
241 
242  notFound = true;
243  // search ID scan backward
244  for(j=jmax; j>=0; --j) {
245  if((idsave[j]).first == idMother) {
246  id1 = (idsave[j]).second;
247  if(0 == id1 || id1 == idMother) { return id1; }
248  jmax = j - 1;
249  idMother = id1;
250  notFound = false;
251  break;
252  }
253  }
254  if(notFound) {
255  // ID not in the list of saved track - look into ancestors
256  jmax = ancestorList.size()-1;
257  for(j=jmax; j>=0; --j) {
258  if((ancestorList[j]).first == idMother) {
259  idMother = (ancestorList[j]).second;
260  return idMother;
261  }
262  }
263  return 0;
264  }
265  } while (!notFound);
266  }
267  }
268  return idMother;
269 }
270 
272 {
273  if ( !ancestorList.empty() && lastHist > ancestorList.size() ) {
274  lastHist = ancestorList.size();
275  edm::LogError("SimTrackManager")
276  << " SimTrackManager::fillMotherList track index corrupted";
277  }
278  /*
279  std::cout << "### SimTrackManager::fillMotherList: "
280  << idsave.size() << " saved; ancestor: " << lastHist
281  << " " << ancestorList.size() << std::endl;
282  for (unsigned int i = 0; i< idsave.size(); ++i) {
283  std::cout << " ISV: Track ID = " << (idsave[i]).first
284  << " Mother ID = " << (idsave[i]).second << std::endl;
285  }
286  */
287  for (unsigned int n = lastHist; n < ancestorList.size(); n++) {
288 
289  int theMotherId = idSavedTrack((ancestorList[n]).first);
290  ancestorList[n].second = theMotherId;
291  /*
292  std::cout << " ANC: Track ID = " << (ancestorList[n]).first
293  << " Mother ID = " << (ancestorList[n]).second << std::endl;
294  */
295 #ifdef DebugLog
296  LogDebug("SimTrackManager") << "Track ID = " << (ancestorList[n]).first
297  << " Mother ID = " << (ancestorList[n]).second;
298 #endif
299  }
300 
301  lastHist = ancestorList.size();
302 
303  idsave.clear();
304 
305 }
306 
308 
309  if ((*m_trksForThisEvent).empty() && idsave.empty()) { return; }
310 
311 #ifdef DebugLog
312  LogDebug("SimTrackManager")
313  << "SimTrackManager::cleanTracksWithHistory has "
314  << idsave.size()
315  << " mother-daughter relationships stored with lastTrack = " << lastTrack;
316 #endif
317 
318  if ( lastTrack > 0 && lastTrack >= (*m_trksForThisEvent).size() ) {
319  lastTrack = 0;
320  edm::LogError("SimTrackManager")
321  << " SimTrackManager::cleanTracksWithHistory track index corrupted";
322  }
323 
324  stable_sort(m_trksForThisEvent->begin()+lastTrack,m_trksForThisEvent->end(),trkIDLess());
325 
326  stable_sort(idsave.begin(),idsave.end());
327 
328 #ifdef DebugLog
329  LogDebug("SimTrackManager")
330  << " SimTrackManager::cleanTracksWithHistory knows " << m_trksForThisEvent->size()
331  << " tracks with history before branching";
332  for (unsigned int it =0; it <(*m_trksForThisEvent).size(); it++) {
333  LogDebug("SimTrackManager")
334  << " 1 - Track in position " << it << " G4 track number "
335  << (*m_trksForThisEvent)[it]->trackID()
336  << " mother " << (*m_trksForThisEvent)[it]->parentID()
337  << " status " << (*m_trksForThisEvent)[it]->saved();
338  }
339 #endif
340 
341  for (unsigned int it = lastTrack; it < m_trksForThisEvent->size(); it++)
342  {
343  TrackWithHistory * t = (*m_trksForThisEvent)[it];
344  if (t->saved()) { saveTrackAndItsBranch(t); }
345  }
346  unsigned int num = lastTrack;
347  for (unsigned int it = lastTrack; it < m_trksForThisEvent->size(); it++)
348  {
349  TrackWithHistory * t = (*m_trksForThisEvent)[it];
350  int g4ID = t->trackID();
351  if (t->saved() == true)
352  {
353  if (it>num) (*m_trksForThisEvent)[num] = t;
354  num++;
355  for (unsigned int itr=0; itr<idsave.size(); itr++) {
356  if ((idsave[itr]).first == g4ID) {
357  (idsave[itr]).second = g4ID;
358  break;
359  }
360  }
361  }
362  else
363  {
364  delete t;
365  }
366  }
367 
368  (*m_trksForThisEvent).resize(num);
369 
370 #ifdef DebugLog
371  LogDebug("SimTrackManager")
372  << " AFTER CLEANING, I GET " << (*m_trksForThisEvent).size()
373  << " tracks to be saved persistently";
374  for (unsigned int it = 0; it < (*m_trksForThisEvent).size(); it++) {
375  LogDebug("SimTrackManager")
376  << " Track in position " << it
377  << " G4 track number " << (*m_trksForThisEvent)[it]->trackID()
378  << " mother " << (*m_trksForThisEvent)[it]->parentID()
379  << " Status " << (*m_trksForThisEvent)[it]->saved()
380  << " id " << (*m_trksForThisEvent)[it]->particleID()
381  << " E(MeV)= " << (*m_trksForThisEvent)[it]->totalEnergy();
382  }
383 #endif
384 
385  fillMotherList();
386 
387  lastTrack = (*m_trksForThisEvent).size();
388 }
389 
391 {
392  if ( theLHCTlink == nullptr ) return;
393 
394  for (unsigned int it = 0; it < m_trksForThisEvent->size(); it++)
395  {
396  TrackWithHistory * trkH = (*m_trksForThisEvent)[it];
397  int genParticleID_ = trkH->genParticleID();
398  if ( genParticleID_ == -1 ) { continue; }
399  else {
400  for ( unsigned int itrlink = 0; itrlink < (*theLHCTlink).size(); itrlink++ ) {
401  if ( (*theLHCTlink)[itrlink].afterHector() == genParticleID_ ) {
402  trkH->setGenParticleID( (*theLHCTlink)[itrlink].beforeHector() );
403  continue;
404  }
405  }
406  }
407  }
408 
409  theLHCTlink = nullptr;
410 
411 }
#define LogDebug(id)
int getOrCreateVertex(TrackWithHistory *, int, G4SimEvent *simEvent)
bool saved() const
double totalEnergy() const
SimTrackManager(bool iCollapsePrimaryVertices=false)
void setGenParticleID(int i)
#define nullptr
void cleanTkCaloStateInfoMap()
std::map< uint32_t, std::pair< math::XYZVectorD, math::XYZTLorentzVectorD > > mapTkCaloStateInfo
void swap(Association< C > &lhs, Association< C > &rhs)
Definition: Association.h:116
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:8
std::vector< TrackWithHistory * > TrackContainer
Definition: TrackContainer.h:8
T sqrt(T t)
Definition: SSEVec.h:18
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
int k[5][pyjets_maxn]
int parentID() const
const G4VProcess * creatorProcess() const
std::map< int, MapVertexPositionVector > MotherParticleToVertexMap
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
static const GlobalPoint notFound(0, 0, 0)
const edm::LHCTransportLinkContainer * theLHCTlink
void reallyStoreTracks(G4SimEvent *simEvent)
std::vector< std::pair< int, int > > ancestorList
const math::XYZVectorD & vertexPosition() const
def check(config)
Definition: trackerTree.py:14
int genParticleID() const