VMC Examples Version 6.8
Loading...
Searching...
No Matches
Ex03dMCStack.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/Ex03dMCStack.cxx
11/// \brief Implementation of the Ex03dMCStack class
12///
13/// Geant4 ExampleN03 adapted to Virtual Monte Carlo
14///
15/// \date 07/07/2026
16/// \author Radoslaw Karabowicz; GSI
17
18#include <Riostream.h>
19#include <TError.h>
20#include <TParticle.h>
21
22#include "Ex03dMCStack.h"
23#include "TMCRootManager.h"
24
25using namespace std;
26
27/// \cond CLASSIMP
28ClassImp(Ex03dMCStack)
29 /// \endcond
30
31 //_____________________________________________________________________________
33{
34 /// Standard constructor
35 /// \param size The stack size
36}
37
38//_____________________________________________________________________________
40{
41 /// Default constructor
42}
43
44//_____________________________________________________________________________
46{
47 /// Destructor
48
49 delete fParticles;
50}
51
52// private methods
53
54// public methods
56{
57 TMCRootManager::Instance()->Register("particles", fExParticles);
58}
59
60//_____________________________________________________________________________
61void Ex03dMCStack::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 Int_t trackId = GetNtrack();
93 fParticles->push_back(TParticle(pdg, is, parent, trackId, kFirstDaughter,
94 kLastDaughter, px, py, pz, e, vx, vy, vz, tof));
95 auto particle = &fParticles->back();
96
97 particle->SetPolarisation(polx, poly, polz);
98 particle->SetWeight(weight);
99 particle->SetUniqueID(mech);
100
101 fExParticles->push_back(Ex03dParticle(pdg, is, parent, trackId,
102 kFirstDaughter, kLastDaughter, px, py, pz, e, vx, vy, vz, tof));
103 auto exParticle = &fExParticles->back();
104
105 exParticle->SetPolarisation(polx, poly, polz);
106 exParticle->SetWeight(weight);
107
108 if (parent < 0) fNPrimary++;
109
110 if (toBeDone) fStack.push(particle);
111
112 ntr = GetNtrack() - 1;
113}
114
115//_____________________________________________________________________________
116TParticle* Ex03dMCStack::PopNextTrack(Int_t& itrack)
117{
118 /// Get next particle for tracking from the stack.
119 /// \return The popped particle object
120 /// \param track The index of the popped track
121
122 itrack = -1;
123 if (fStack.empty()) return 0;
124
125 TParticle* particle = fStack.top();
126 fStack.pop();
127
128 if (!particle) return 0;
129
130 fCurrentTrack = particle->GetSecondMother();
131 itrack = fCurrentTrack;
132
133 return particle;
134}
135
136//_____________________________________________________________________________
138{
139 /// Return \em i -th particle in fParticles.
140 /// \return The popped primary particle object
141 /// \param i The index of primary particle to be popped
142
143 if (i < 0 || i >= fNPrimary)
144 Fatal("GetPrimaryForTracking", "Index out of range");
145
146 return &(*fParticles)[i];
147}
148
149//_____________________________________________________________________________
150void Ex03dMCStack::Print(Option_t* /*option*/) const
151{
152 /// Print info for all particles.
153
154 cout << "Ex03dMCStack Info " << endl;
155 cout << "Total number of particles: " << GetNtrack() << endl;
156 cout << "Number of primary particles: " << GetNprimary() << endl;
157
158 for (Int_t i = 0; i < GetNtrack(); i++) GetParticle(i)->Print();
159}
160
161//_____________________________________________________________________________
163{
164 /// Delete contained particles, reset particles array and stack.
165
166 fCurrentTrack = -1;
167 fNPrimary = 0;
168 fParticles->clear();
169 fExParticles->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 return fParticles->size();
186}
187
188//_____________________________________________________________________________
190{
191 /// \return The total number of primary tracks.
192
193 return fNPrimary;
194}
195
196//_____________________________________________________________________________
198{
199 /// \return The current track particle
200
201 TParticle* current = GetParticle(fCurrentTrack);
202
203 if (!current)
204 Warning("GetCurrentTrack", "Current track not found in the stack");
205
206 return current;
207}
208
209//_____________________________________________________________________________
211{
212 /// \return The current track number
213
214 return fCurrentTrack;
215}
216
217//_____________________________________________________________________________
219{
220 /// \return The current track parent ID.
221
222 TParticle* current = GetCurrentTrack();
223
224 if (current)
225 return current->GetFirstMother();
226 else
227 return -1;
228}
229
230//_____________________________________________________________________________
231TParticle* Ex03dMCStack::GetParticle(Int_t id) const
232{
233 /// \return The \em id -th particle in fParticles
234 /// \param id The index of the particle to be returned
235
236 if (id < 0 || id >= fParticles->size())
237 Fatal("GetParticle", "Index out of range");
238
239 return &(*fParticles)[id];
240}
Implementation of the TVirtualMCStack interface.
virtual Int_t GetNprimary() const
virtual Int_t GetCurrentParentTrackNumber() const
virtual TParticle * PopPrimaryForTracking(Int_t i)
virtual Int_t GetNtrack() const
virtual TParticle * GetCurrentTrack() const
virtual Int_t GetCurrentTrackNumber() const
virtual void Print(Option_t *option="") const
Ex03dMCStack(Int_t size)
std::vector< TParticle > * fParticles
< The stack of particles (transient)
virtual void SetCurrentTrack(Int_t track)
virtual ~Ex03dMCStack()
TParticle * GetParticle(Int_t id) const
std::vector< Ex03dParticle > * fExParticles
< The vector of particle (persistent)
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 TParticle * PopNextTrack(Int_t &track)
Int_t fNPrimary
< The current track number
std::stack< TParticle * > fStack
Int_t fCurrentTrack
< The vector of particle (persistent)
Replacement of the TParticle.