VMC Examples Version 6.6
Loading...
Searching...
No Matches
Ex03MCStack.cxx
Go to the documentation of this file.
1//------------------------------------------------
2// The Virtual Monte Carlo examples
3// Copyright (C) 2014 - 2018 Ivana Hrivnacova
4// All rights reserved.
5//
6// For the licensing terms see geant4_vmc/LICENSE.
7// Contact: root-vmc@cern.ch
8//-------------------------------------------------
9
10/// \file E03/src/Ex03MCStack.cxx
11/// \brief Implementation of the Ex03MCStack class
12///
13/// Geant4 ExampleN03 adapted to Virtual Monte Carlo
14///
15/// \date 06/03/2002
16/// \author I. Hrivnacova; IPN, Orsay
17
18#include <Riostream.h>
19#include <TClonesArray.h>
20#include <TError.h>
21#include <TParticle.h>
22
23#include "Ex03MCStack.h"
24
25using namespace std;
26
27/// \cond CLASSIMP
28ClassImp(Ex03MCStack)
29 /// \endcond
30
31 //_____________________________________________________________________________
32 Ex03MCStack::Ex03MCStack(Int_t size)
33 : fParticles(0), fCurrentTrack(-1), fNPrimary(0)
34{
35 /// Standard constructor
36 /// \param size The stack size
37
38 fParticles = new TClonesArray("TParticle", size);
39}
40
41//_____________________________________________________________________________
42Ex03MCStack::Ex03MCStack() : fParticles(0), fCurrentTrack(-1), fNPrimary(0)
43{
44 /// Default constructor
45}
46
47//_____________________________________________________________________________
49{
50 /// Destructor
51
52 if (fParticles) fParticles->Delete();
53 delete fParticles;
54}
55
56// private methods
57
58// public methods
59
60//_____________________________________________________________________________
61void Ex03MCStack::PushTrack(Int_t toBeDone, Int_t parent, Int_t pdg,
62 Double_t px, Double_t py, Double_t pz, Double_t e, Double_t vx, Double_t vy,
63 Double_t vz, Double_t tof, Double_t polx, Double_t poly, Double_t polz,
64 TMCProcess mech, Int_t& ntr, Double_t weight, Int_t is)
65{
66 /// Create a new particle and push into stack;
67 /// adds it to the particles array (fParticles) and if not done to the
68 /// stack (fStack).
69 /// Use TParticle::fMother[1] to store Track ID.
70 /// \param toBeDone 1 if particles should go to tracking, 0 otherwise
71 /// \param parent number of the parent track, -1 if track is primary
72 /// \param pdg PDG encoding
73 /// \param px particle momentum - x component [GeV/c]
74 /// \param py particle momentum - y component [GeV/c]
75 /// \param pz particle momentum - z component [GeV/c]
76 /// \param e total energy [GeV]
77 /// \param vx position - x component [cm]
78 /// \param vy position - y component [cm]
79 /// \param vz position - z component [cm]
80 /// \param tof time of flight [s]
81 /// \param polx polarization - x component
82 /// \param poly polarization - y component
83 /// \param polz polarization - z component
84 /// \param mech creator process VMC code
85 /// \param ntr track number (is filled by the stack
86 /// \param weight particle weight
87 /// \param is generation status code
88
89 const Int_t kFirstDaughter = -1;
90 const Int_t kLastDaughter = -1;
91
92 TClonesArray& particlesRef = *fParticles;
93 Int_t trackId = GetNtrack();
94 TParticle* particle = new (particlesRef[trackId]) TParticle(pdg, is, parent,
95 trackId, kFirstDaughter, kLastDaughter, px, py, pz, e, vx, vy, vz, tof);
96
97 particle->SetPolarisation(polx, poly, polz);
98 particle->SetWeight(weight);
99 particle->SetUniqueID(mech);
100
101 if (parent < 0) fNPrimary++;
102
103 if (toBeDone) fStack.push(particle);
104
105 ntr = GetNtrack() - 1;
106}
107
108//_____________________________________________________________________________
109TParticle* Ex03MCStack::PopNextTrack(Int_t& itrack)
110{
111 /// Get next particle for tracking from the stack.
112 /// \return The popped particle object
113 /// \param track The index of the popped track
114
115 itrack = -1;
116 if (fStack.empty()) return 0;
117
118 TParticle* particle = fStack.top();
119 fStack.pop();
120
121 if (!particle) return 0;
122
123 fCurrentTrack = particle->GetSecondMother();
124 itrack = fCurrentTrack;
125
126 return particle;
127}
128
129//_____________________________________________________________________________
130TParticle* Ex03MCStack::PopPrimaryForTracking(Int_t i)
131{
132 /// Return \em i -th particle in fParticles.
133 /// \return The popped primary particle object
134 /// \param i The index of primary particle to be popped
135
136 if (i < 0 || i >= fNPrimary)
137 Fatal("GetPrimaryForTracking", "Index out of range");
138
139 return (TParticle*)fParticles->At(i);
140}
141
142//_____________________________________________________________________________
143void Ex03MCStack::Print(Option_t* /*option*/) const
144{
145 /// Print info for all particles.
146
147 cout << "Ex03MCStack Info " << endl;
148 cout << "Total number of particles: " << GetNtrack() << endl;
149 cout << "Number of primary particles: " << GetNprimary() << endl;
150
151 for (Int_t i = 0; i < GetNtrack(); i++) GetParticle(i)->Print();
152}
153
154//_____________________________________________________________________________
156{
157 /// Delete contained particles, reset particles array and stack.
158
159 fCurrentTrack = -1;
160 fNPrimary = 0;
161 fParticles->Clear();
162}
163
164//_____________________________________________________________________________
165void Ex03MCStack::SetCurrentTrack(Int_t track)
166{
167 /// Set the current track number to a given value.
168 /// \param track The current track number
169
170 fCurrentTrack = track;
171}
172
173//_____________________________________________________________________________
174Int_t Ex03MCStack::GetNtrack() const
175{
176 /// \return The total number of all tracks.
177
178 return fParticles->GetEntriesFast();
179}
180
181//_____________________________________________________________________________
182Int_t Ex03MCStack::GetNprimary() const
183{
184 /// \return The total number of primary tracks.
185
186 return fNPrimary;
187}
188
189//_____________________________________________________________________________
190TParticle* Ex03MCStack::GetCurrentTrack() const
191{
192 /// \return The current track particle
193
194 TParticle* current = GetParticle(fCurrentTrack);
195
196 if (!current)
197 Warning("GetCurrentTrack", "Current track not found in the stack");
198
199 return current;
200}
201
202//_____________________________________________________________________________
204{
205 /// \return The current track number
206
207 return fCurrentTrack;
208}
209
210//_____________________________________________________________________________
212{
213 /// \return The current track parent ID.
214
215 TParticle* current = GetCurrentTrack();
216
217 if (current)
218 return current->GetFirstMother();
219 else
220 return -1;
221}
222
223//_____________________________________________________________________________
224TParticle* Ex03MCStack::GetParticle(Int_t id) const
225{
226 /// \return The \em id -th particle in fParticles
227 /// \param id The index of the particle to be returned
228
229 if (id < 0 || id >= fParticles->GetEntriesFast())
230 Fatal("GetParticle", "Index out of range");
231
232 return (TParticle*)fParticles->At(id);
233}
Implementation of the TVirtualMCStack interface.
Definition Ex03MCStack.h:36
virtual TParticle * PopNextTrack(Int_t &track)
virtual void SetCurrentTrack(Int_t track)
virtual TParticle * PopPrimaryForTracking(Int_t i)
virtual TParticle * GetCurrentTrack() const
virtual void PushTrack(Int_t toBeDone, Int_t parent, Int_t pdg, Double_t px, Double_t py, Double_t pz, Double_t e, Double_t vx, Double_t vy, Double_t vz, Double_t tof, Double_t polx, Double_t poly, Double_t polz, TMCProcess mech, Int_t &ntr, Double_t weight, Int_t is)
virtual Int_t GetNtrack() const
virtual Int_t GetCurrentTrackNumber() const
virtual void Print(Option_t *option="") const
Int_t fCurrentTrack
The current track number.
Definition Ex03MCStack.h:67
std::stack< TParticle * > fStack
The stack of particles (transient)
Definition Ex03MCStack.h:65
virtual ~Ex03MCStack()
TClonesArray * fParticles
The array of particle (persistent)
Definition Ex03MCStack.h:66
virtual Int_t GetNprimary() const
virtual Int_t GetCurrentParentTrackNumber() const
TParticle * GetParticle(Int_t id) const
Int_t fNPrimary
The number of primaries.
Definition Ex03MCStack.h:68