Geant4 VMC Version 6.6
Loading...
Searching...
No Matches
TG4StackPopper.cxx
Go to the documentation of this file.
1//------------------------------------------------
2// The Geant4 Virtual Monte Carlo package
3// Copyright (C) 2007 - 2014 Ivana Hrivnacova
4// All rights reserved.
5//
6// For the licensing terms see geant4_vmc/LICENSE.
7// Contact: root-vmc@cern.ch
8//-------------------------------------------------
9
14
15#include "TG4StackPopper.h"
16#include "TG4G3Units.h"
17#include "TG4ParticlesManager.h"
18#include "TG4TrackInformation.h"
19
20#include <G4IonTable.hh>
21#include <G4Track.hh>
22
23#include <TParticle.h>
24#include <TVirtualMC.h>
25#include <TVirtualMCStack.h>
26
28
29//_____________________________________________________________________________
30TG4StackPopper::TG4StackPopper(const G4String& processName)
31 : G4VProcess(processName, fUserDefined),
32 fMCStack(0),
33 fNofDoneTracks(0),
34 fDoExclusiveStep(false)
35{
37
38 if (fgInstance) {
39 TG4Globals::Exception("TG4StackPopper", "TG4StackPopper",
40 "Cannot create two instances of singleton.");
41 }
42
43 fgInstance = this;
44
45 // Set process sub type
46 SetProcessSubType(fStackPopper);
47
48 // Cache thread-local pointers
49 fMCStack = gMC->GetStack();
50}
51
52//_____________________________________________________________________________
59
60//
61// public methods
62//
63
64//_____________________________________________________________________________
66 const G4Track& /*track*/, G4double /*notUsed*/, G4ForceCondition* condition)
67{
70
71 *condition = InActivated;
72 if (HasPoppedTracks()) {
73 if (fDoExclusiveStep) {
74 *condition = ExclusivelyForced;
75 }
76 else {
77 *condition = StronglyForced;
78 }
79 }
80
81 return DBL_MAX;
82}
83
84//_____________________________________________________________________________
86 const G4Track& track, const G4Step& /*step*/)
87{
89
90 aParticleChange.Initialize(track);
91
92 if (fMCStack->GetNtrack() == fNofDoneTracks) return &aParticleChange;
93
94 Int_t currentTrackId = fMCStack->GetCurrentTrackNumber();
95 Int_t nofTracksToPop = fMCStack->GetNtrack() - fNofDoneTracks;
96 aParticleChange.SetNumberOfSecondaries(
97 aParticleChange.GetNumberOfSecondaries() + nofTracksToPop);
98
99 for (G4int i = 0; i < nofTracksToPop; ++i) {
100
101 // Pop particle from the stack
102 G4int itrack;
103 TParticle* particle = fMCStack->PopNextTrack(itrack);
105
106 if (!particle) {
108 "TG4StackPopper", "PostStepDoIt", "No particle popped from stack!");
109 return &aParticleChange;
110 }
111
112 // G4cout << "TG4StackPopper::PostStepDoIt: Popped particle = "
113 // << particle->GetName()
114 // << " trackID = "<< itrack << G4endl;
115
116 // Create dynamic particle
117 G4DynamicParticle* dynamicParticle =
119 if (!dynamicParticle) {
120 TG4Globals::Exception("TG4StackPopper", "PostStepDoIt",
121 "Conversion from Root particle -> G4 particle failed.");
122 }
123
124 // Define track
125
126 G4ThreeVector position =
128 G4double time = particle->T() * TG4G3Units::Time();
129
130 G4Track* secondaryTrack = new G4Track(dynamicParticle, time, position);
131
132 // set track information here to avoid saving track in the stack
133 // for the second time
134 TG4TrackInformation* trackInformation = new TG4TrackInformation(itrack);
135 // the track information is deleted together with its
136 // G4Track object
137 trackInformation->SetIsUserTrack(true);
138 trackInformation->SetPDGEncoding(particle->GetPdgCode());
139 secondaryTrack->SetUserInformation(trackInformation);
140
141 // Add track as a secondary
142 aParticleChange.AddSecondary(secondaryTrack);
143 }
144
145 // Set back current track number in the track
146 // (as stack may have changed it with popping particles)
147 fMCStack->SetCurrentTrack(currentTrackId);
148
149 // Set the kept track status if in exclusive step
150 if (fDoExclusiveStep) {
151 aParticleChange.ProposeTrackStatus(fTrackStatus);
152 fDoExclusiveStep = false;
153 }
154
155 return &aParticleChange;
156}
157
158//_____________________________________________________________________________
160{
162
164}
165
166//_____________________________________________________________________________
168{
171
172 fNofDoneTracks = fMCStack->GetNtrack();
173}
174
175//_____________________________________________________________________________
176void TG4StackPopper::SetDoExclusiveStep(G4TrackStatus trackStatus)
177{
179
180 fDoExclusiveStep = true;
181 fTrackStatus = trackStatus;
182}
183
184//_____________________________________________________________________________
186{
188
189 return (gMC->GetStack()->GetNtrack() != fNofDoneTracks);
190}
Definition of the TG4G3Units class.
Definition of the TG4ParticlesManager class.
Definition of the TG4StackPopper class.
Definition of the TG4TrackInformation class.
static G4double Time()
Definition TG4G3Units.h:93
static void Exception(const TString &className, const TString &methodName, const TString &text)
G4DynamicParticle * CreateDynamicParticle(const TParticle *particle) const
G4ThreeVector GetParticlePosition(const TParticle *particle) const
static TG4ParticlesManager * Instance()
The process which pops particles defined by user from the VMC stack and passes them to tracking.
G4bool HasPoppedTracks() const
void SetDoExclusiveStep(G4TrackStatus trackStatus)
static G4ThreadLocal TG4StackPopper * fgInstance
this instance
G4TrackStatus fTrackStatus
The track status to be restored after performing exclusive step.
TG4StackPopper(const G4String &processName="stackPopper")
virtual G4VParticleChange * PostStepDoIt(const G4Track &track, const G4Step &step)
TVirtualMCStack * fMCStack
Cached pointer to thread-local VMC stack.
G4int fNofDoneTracks
the counter for popped tracks
virtual G4double PostStepGetPhysicalInteractionLength(const G4Track &track, G4double previousStepSize, G4ForceCondition *condition)
virtual ~TG4StackPopper()
Defines additional track information.
void SetIsUserTrack(G4bool isUserTrack)
void SetPDGEncoding(G4int pdgEncoding)
@ fStackPopper