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