VMC Examples Version 6.6
Loading...
Searching...
No Matches
Ex03cMCStack.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 Ex03cMCStack.cxx
11/// \brief Implementation of the Ex03cMCStack class
12///
13/// Geant4 ExampleN03 adapted to Virtual Monte Carlo
14///
15/// \date 21/08/2019
16/// \author Benedikt Volkel, CERN
17
18#include <Riostream.h>
19#include <TClonesArray.h>
20#include <TError.h>
21#include <TParticle.h>
22
23#include "TMCManager.h"
24
25#include "Ex03cMCStack.h"
26
27using namespace std;
28
29/// \cond CLASSIMP
30ClassImp(Ex03cMCStack)
31 /// \endcond
32
33 //_____________________________________________________________________________
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//_____________________________________________________________________________
44Ex03cMCStack::Ex03cMCStack() : 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 Ex03cMCStack::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 and push into stack;
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 /// Forward to the TMCManager in case of multi-run
110 TMCManager* mgr = TMCManager::Instance();
111 if (mgr) {
112 mgr->ForwardTrack(toBeDone, ntr, parent, particle);
113 }
114}
115
116//_____________________________________________________________________________
117TParticle* Ex03cMCStack::PopNextTrack(Int_t& itrack)
118{
119 /// Get next particle for tracking from the stack.
120 /// \return The popped particle object
121 /// \param track The index of the popped track
122
123 itrack = -1;
124 if (fStack.empty()) return 0;
125
126 TParticle* particle = fStack.top();
127 fStack.pop();
128
129 if (!particle) return 0;
130
131 fCurrentTrack = particle->GetSecondMother();
132 itrack = fCurrentTrack;
133
134 return particle;
135}
136
137//_____________________________________________________________________________
139{
140 /// Return \em i -th particle in fParticles.
141 /// \return The popped primary particle object
142 /// \param i The index of primary particle to be popped
143
144 if (i < 0 || i >= fNPrimary)
145 Fatal("GetPrimaryForTracking", "Index out of range");
146
147 return (TParticle*)fParticles->At(i);
148}
149
150//_____________________________________________________________________________
151void Ex03cMCStack::Print(Option_t* /*option*/) const
152{
153 /// Print info for all particles.
154
155 cout << "Ex03cMCStack Info " << endl;
156 cout << "Total number of particles: " << GetNtrack() << endl;
157 cout << "Number of primary particles: " << GetNprimary() << endl;
158
159 for (Int_t i = 0; i < GetNtrack(); i++) GetParticle(i)->Print();
160}
161
162//_____________________________________________________________________________
164{
165 /// Delete contained particles, reset particles array and stack.
166
167 fCurrentTrack = -1;
168 fNPrimary = 0;
169 fParticles->Clear();
170}
171
172//_____________________________________________________________________________
174{
175 /// Set the current track number to a given value.
176 /// \param track The current track number
177
178 fCurrentTrack = track;
179}
180
181//_____________________________________________________________________________
183{
184 /// \return The total number of all tracks.
185
186 return fParticles->GetEntriesFast();
187}
188
189//_____________________________________________________________________________
191{
192 /// \return The total number of primary tracks.
193
194 return fNPrimary;
195}
196
197//_____________________________________________________________________________
199{
200 /// \return The current track particle
201
202 TParticle* current = GetParticle(fCurrentTrack);
203
204 if (!current)
205 Warning("GetCurrentTrack", "Current track not found in the stack");
206
207 return current;
208}
209
210//_____________________________________________________________________________
212{
213 /// \return The current track number
214
215 return fCurrentTrack;
216}
217
218//_____________________________________________________________________________
220{
221 /// \return The current track parent ID.
222
223 TParticle* current = GetCurrentTrack();
224
225 if (current)
226 return current->GetFirstMother();
227 else
228 return -1;
229}
230
231//_____________________________________________________________________________
232TParticle* Ex03cMCStack::GetParticle(Int_t id) const
233{
234 /// \return The \em id -th particle in fParticles
235 /// \param id The index of the particle to be returned
236
237 if (id < 0 || id >= fParticles->GetEntriesFast())
238 Fatal("GetParticle", "Index out of range");
239
240 return (TParticle*)fParticles->At(id);
241}
Definition of the Ex03cMCStack class.
Implementation of the TVirtualMCStack interface.
std::stack< TParticle * > fStack
The stack of particles (transient)
virtual TParticle * GetCurrentTrack() const
virtual Int_t GetCurrentParentTrackNumber() const
TClonesArray * fParticles
The array of particle (persistent)
virtual Int_t GetCurrentTrackNumber() const
Int_t fCurrentTrack
The current track number.
virtual TParticle * PopPrimaryForTracking(Int_t i)
virtual void SetCurrentTrack(Int_t track)
TParticle * GetParticle(Int_t id) const
virtual void Print(Option_t *option="") 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)
Int_t fNPrimary
The number of primaries.
virtual Int_t GetNprimary() const
virtual Int_t GetNtrack() const
virtual TParticle * PopNextTrack(Int_t &track)
virtual ~Ex03cMCStack()