ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4StackManager.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4StackManager.cc
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 //
27 //
28 //
29 // Last Modification : 09/Dec/96 M.Asai
30 //
31 
32 #include "G4StackManager.hh"
33 #include "G4StackingMessenger.hh"
34 #include "G4VTrajectory.hh"
35 #include "evmandefs.hh"
36 #include "G4ios.hh"
37 
39 :userStackingAction(0),verboseLevel(0),numberOfAdditionalWaitingStacks(0)
40 {
42 #ifdef G4_USESMARTSTACK
44  // G4cout<<"+++ G4StackManager uses G4SmartTrackStack. +++"<<G4endl;
45 #else
46  urgentStack = new G4TrackStack(5000);
47 // G4cout<<"+++ G4StackManager uses ordinary G4TrackStack. +++"<<G4endl;
48 #endif
49  waitingStack = new G4TrackStack(1000);
50  postponeStack = new G4TrackStack(1000);
51 }
52 
54 {
56 
57 #ifdef G4VERBOSE
58  if(verboseLevel>0)
59  {
60  G4cout << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << G4endl;
61  G4cout << " Maximum number of tracks in the urgent stack : " << urgentStack->GetMaxNTrack() << G4endl;
62  G4cout << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << G4endl;
63  }
64 #endif
65  delete urgentStack;
66  delete waitingStack;
67  delete postponeStack;
68  delete theMessenger;
70  for(int i=0;i<numberOfAdditionalWaitingStacks;i++) {
71  delete additionalWaitingStacks[i];
72  }
73  }
74 }
75 
76 const G4StackManager & G4StackManager::operator=
77 (const G4StackManager &) { return *this; }
79 const{ return false; }
81 const{ return true; }
82 
83 #include "G4ParticleDefinition.hh"
84 #include "G4VProcess.hh"
85 
86 //Needed for temporal service
87 #include "G4ParticleTable.hh"
88 #include "G4ProcessManager.hh"
89 #include "G4ProcessVector.hh"
90 
92 {
93  const G4ParticleDefinition* pd = newTrack->GetParticleDefinition();
94  if(pd->GetParticleDefinitionID() < 0)
95  {
97  ED << "A track without proper process manager is pushed into the track stack.\n"
98  << " Particle name : " << pd->GetParticleName() << " -- ";
99  if(newTrack->GetParentID()<0)
100  { ED << "created by a primary particle generator."; }
101  else
102  {
103  const G4VProcess* vp = newTrack->GetCreatorProcess();
104  if(vp)
105  { ED << "created by " << vp->GetProcessName() << "."; }
106  else
107  { ED << "creaded by unknown process."; }
108  }
109  G4Exception("G4StackManager::PushOneTrack","Event10051",
110  FatalException,ED);
111  delete newTrack;
112  return GetNUrgentTrack();
113  }
114 
115  G4ClassificationOfNewTrack classification = DefaultClassification( newTrack );
116  if(userStackingAction)
117  { classification = userStackingAction->ClassifyNewTrack( newTrack ); }
118 
119  if(classification==fKill) // delete newTrack without stacking
120  {
121 #ifdef G4VERBOSE
122  if( verboseLevel > 1 )
123  {
124  G4cout << " ---> G4Track " << newTrack << " (trackID "
125  << newTrack->GetTrackID() << ", parentID "
126  << newTrack->GetParentID() << ") is not to be stored." << G4endl;
127  }
128 #endif
129  delete newTrack;
130  delete newTrajectory;
131  }
132  else
133  {
134  G4StackedTrack newStackedTrack( newTrack, newTrajectory );
135  switch (classification)
136  {
137  case fUrgent:
138  urgentStack->PushToStack( newStackedTrack );
139  break;
140  case fWaiting:
141  waitingStack->PushToStack( newStackedTrack );
142  break;
143  case fPostpone:
144  postponeStack->PushToStack( newStackedTrack );
145  break;
146  default:
147  G4int i = classification - 10;
150  ED << "invalid classification " << classification << G4endl;
151  G4Exception("G4StackManager::PushOneTrack","Event0051",
152  FatalException,ED);
153  } else {
154  additionalWaitingStacks[i-1]->PushToStack( newStackedTrack );
155  }
156  break;
157  }
158  }
159 
160  return GetNUrgentTrack();
161 }
162 
163 
165 {
166 #ifdef G4VERBOSE
167  if( verboseLevel > 1 )
168  {
169  G4cout << "### pop requested out of "
170  << GetNUrgentTrack() << " stacked tracks." << G4endl;
171  }
172 #endif
173 
174  while( GetNUrgentTrack() == 0 )
175  {
176 #ifdef G4VERBOSE
177  if( verboseLevel > 1 ) G4cout << "### " << GetNWaitingTrack()
178  << " waiting tracks are re-classified to" << G4endl;
179 #endif
182  for(int i=0;i<numberOfAdditionalWaitingStacks;i++) {
183  if(i==0) {
184  additionalWaitingStacks[0]->TransferTo(waitingStack);
185  } else {
187  }
188  }
189  }
191 #ifdef G4VERBOSE
192  if( verboseLevel > 1 ) G4cout << " " << GetNUrgentTrack()
193  << " urgent tracks and " << GetNWaitingTrack()
194  << " waiting tracks." << G4endl;
195 #endif
196  if( ( GetNUrgentTrack()==0 ) && ( GetNWaitingTrack()==0 ) ) return 0;
197  }
198 
199  G4StackedTrack selectedStackedTrack = urgentStack->PopFromStack();
200  G4Track * selectedTrack = selectedStackedTrack.GetTrack();
201  *newTrajectory = selectedStackedTrack.GetTrajectory();
202 
203 #ifdef G4VERBOSE
204  if( verboseLevel > 2 )
205  {
206  G4cout << "Selected G4StackedTrack : " << &selectedStackedTrack
207  << " with G4Track " << selectedStackedTrack.GetTrack()
208  << " (trackID " << selectedStackedTrack.GetTrack()->GetTrackID()
209  << ", parentID " << selectedStackedTrack.GetTrack()->GetParentID()
210  << ")" << G4endl;
211  }
212 #endif
213 
214  return selectedTrack;
215 }
216 
218 {
219  G4StackedTrack aStackedTrack;
220  G4TrackStack tmpStack;
221 
222  if( !userStackingAction ) return;
223  if( GetNUrgentTrack() == 0 ) return;
224 
225  urgentStack->TransferTo(&tmpStack);
226  while( tmpStack.GetNTrack() > 0 )
227  {
228  aStackedTrack=tmpStack.PopFromStack();
229  G4ClassificationOfNewTrack classification =
230  userStackingAction->ClassifyNewTrack( aStackedTrack.GetTrack() );
231  switch (classification)
232  {
233  case fKill:
234  delete aStackedTrack.GetTrack();
235  delete aStackedTrack.GetTrajectory();
236  break;
237  case fUrgent:
238  urgentStack->PushToStack( aStackedTrack );
239  break;
240  case fWaiting:
241  waitingStack->PushToStack( aStackedTrack );
242  break;
243  case fPostpone:
244  postponeStack->PushToStack( aStackedTrack );
245  break;
246  default:
247  G4int i = classification - 10;
250  ED << "invalid classification " << classification << G4endl;
251  G4Exception("G4StackManager::ReClassify","Event0052",
252  FatalException,ED);
253  } else {
254  additionalWaitingStacks[i-1]->PushToStack( aStackedTrack );
255  }
256  break;
257  }
258  }
259 }
260 
262 {
264 
265  urgentStack->clearAndDestroy(); // Set the urgentStack in a defined state. Not doing it would affect reproducibility.
266 
267  G4int n_passedFromPrevious = 0;
268 
269  if( GetNPostponedTrack() > 0 )
270  {
271 #ifdef G4VERBOSE
272  if( verboseLevel > 1 )
273  {
275  << " postponed tracked are now shifted to the stack." << G4endl;
276  }
277 #endif
278 
279  G4StackedTrack aStackedTrack;
280  G4TrackStack tmpStack;
281 
282  postponeStack->TransferTo(&tmpStack);
283 
284  while( tmpStack.GetNTrack() > 0 )
285  {
286  aStackedTrack=tmpStack.PopFromStack();
287  G4Track* aTrack = aStackedTrack.GetTrack();
288  aTrack->SetParentID(-1);
289  G4ClassificationOfNewTrack classification;
291  { classification = userStackingAction->ClassifyNewTrack( aTrack ); }
292  else
293  { classification = DefaultClassification( aTrack ); }
294 
295  if(classification==fKill)
296  {
297  delete aTrack;
298  delete aStackedTrack.GetTrajectory();
299  }
300  else
301  {
302  aTrack->SetTrackID(-(++n_passedFromPrevious));
303  switch (classification)
304  {
305  case fUrgent:
306  urgentStack->PushToStack( aStackedTrack );
307  break;
308  case fWaiting:
309  waitingStack->PushToStack( aStackedTrack );
310  break;
311  case fPostpone:
312  postponeStack->PushToStack( aStackedTrack );
313  break;
314  default:
315  G4int i = classification - 10;
318  ED << "invalid classification " << classification << G4endl;
319  G4Exception("G4StackManager::PrepareNewEvent","Event0053",
320  FatalException,ED);
321  } else {
322  additionalWaitingStacks[i-1]->PushToStack( aStackedTrack );
323  }
324  break;
325  }
326  }
327  }
328  }
329 
330  return n_passedFromPrevious;
331 }
332 
334 {
336  {
337  for(int i=numberOfAdditionalWaitingStacks;i<iAdd;i++)
338  {
339  G4TrackStack* newStack = new G4TrackStack;
340  additionalWaitingStacks.push_back(newStack);
341  }
343  }
344  else if (iAdd < numberOfAdditionalWaitingStacks)
345  {
346  for(int i=numberOfAdditionalWaitingStacks;i>iAdd;i--)
347  {
348  delete additionalWaitingStacks[i];
349  }
350  }
351 }
352 
354 {
355  if(origin==destination) return;
356  if(origin==fKill) return;
357  G4TrackStack* originStack = 0;
358  switch(origin)
359  {
360  case fUrgent:
361  originStack = 0;
362  break;
363  case fWaiting:
364  originStack = waitingStack;
365  break;
366  case fPostpone:
367  originStack = postponeStack;
368  break;
369  default:
370  int i = origin - 10;
372  break;
373  }
374 
375  if(destination==fKill)
376  {
377  if(originStack)
378  { originStack->clearAndDestroy(); }
379  else
381  }
382  else
383  {
384  G4TrackStack* targetStack = 0;
385  switch(destination)
386  {
387  case fUrgent:
388  targetStack = 0;
389  break;
390  case fWaiting:
391  targetStack = waitingStack;
392  break;
393  case fPostpone:
394  targetStack = postponeStack;
395  break;
396  default:
397  int i = destination - 10;
399  break;
400  }
401  if(originStack)
402  {
403  if(targetStack)
404  { originStack->TransferTo(targetStack); }
405  else
406  { originStack->TransferTo(urgentStack); }
407  }
408  else
409  { urgentStack->TransferTo(targetStack); }
410  }
411  return;
412 }
413 
415 {
416  if(origin==destination) return;
417  if(origin==fKill) return;
418  G4TrackStack* originStack = 0;
419  switch(origin)
420  {
421  case fUrgent:
422  originStack = 0;
423  break;
424  case fWaiting:
425  originStack = waitingStack;
426  break;
427  case fPostpone:
428  originStack = postponeStack;
429  break;
430  default:
431  int i = origin - 10;
433  break;
434  }
435 
436  G4StackedTrack aStackedTrack;
437  if(destination==fKill)
438  {
439  if( originStack && originStack->GetNTrack() ) {
440  aStackedTrack = originStack->PopFromStack();
441  delete aStackedTrack.GetTrack();
442  delete aStackedTrack.GetTrajectory();
443  }
444  else if (urgentStack->GetNTrack() ) {
445  aStackedTrack = urgentStack->PopFromStack();
446  delete aStackedTrack.GetTrack();
447  delete aStackedTrack.GetTrajectory();
448  }
449  }
450  else
451  {
452  G4TrackStack* targetStack = 0;
453  switch(destination)
454  {
455  case fUrgent:
456  targetStack = 0;
457  break;
458  case fWaiting:
459  targetStack = waitingStack;
460  break;
461  case fPostpone:
462  targetStack = postponeStack;
463  break;
464  default:
465  int i = destination - 10;
467  break;
468  }
469  if(originStack && originStack->GetNTrack()) {
470  aStackedTrack = originStack->PopFromStack();
471  if(targetStack) { targetStack->PushToStack(aStackedTrack); }
472  else { urgentStack->PushToStack(aStackedTrack); }
473  }
474  else if(urgentStack->GetNTrack()) {
475  aStackedTrack = urgentStack->PopFromStack();
476  if(targetStack) { targetStack->PushToStack(aStackedTrack); }
477  else { urgentStack->PushToStack(aStackedTrack); }
478  }
479  }
480  return;
481 }
482 
484 {
487  for(int i=1;i<=numberOfAdditionalWaitingStacks;i++) {ClearWaitingStack(i);}
488 }
489 
491 {
493 }
494 
496 {
497  if(i==0) {
499  } else {
500  if(i<=numberOfAdditionalWaitingStacks) additionalWaitingStacks[i-1]->clearAndDestroy();
501  }
502 }
503 
505 {
507 }
508 
510 {
512  for(int i=1;i<=numberOfAdditionalWaitingStacks;i++) {n += additionalWaitingStacks[i-1]->GetNTrack();}
513  return n;
514 }
515 
517 {
518  return urgentStack->GetNTrack();
519 }
520 
522 {
523  if(i==0) { return waitingStack->GetNTrack(); }
524  else {
525  if(i<=numberOfAdditionalWaitingStacks) { return additionalWaitingStacks[i-1]->GetNTrack();}
526  }
527  return 0;
528 }
529 
531 {
532  return postponeStack->GetNTrack();
533 }
534 
536 {
538 }
539 
541 {
544 }
545 
547 {
548  G4ClassificationOfNewTrack classification = fUrgent;
549  if( aTrack->GetTrackStatus() == fPostponeToNextEvent )
550  { classification = fPostpone; }
551  return classification;
552 }
553 
554 
555 
556