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