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