VMC Examples Version 6.6
Loading...
Searching...
No Matches
Ex02MCStack.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 Ex02MCStack.cxx
11/// \brief Implementation of the Ex02MCStack class
12///
13/// Geant4 ExampleN02 adapted to Virtual Monte Carlo
14///
15/// \date 21/04/2002
16/// \author I. Hrivnacova; IPN, Orsay
17
18#include "Ex02MCStack.h"
19
20#include <TError.h>
21#include <TObjArray.h>
22#include <TParticle.h>
23
24#include <iostream>
25
26using namespace std;
27
28/// \cond CLASSIMP
29ClassImp(Ex02MCStack)
30 /// \endcond
31
32 //_____________________________________________________________________________
34 : fParticles(0), fCurrentTrack(-1), fNPrimary(0), fObjectNumber(0)
35{
36 /// Standard constructor
37 /// \param size The stack size
38
39 fParticles = new TObjArray(size);
40}
41
42//_____________________________________________________________________________
44 : fParticles(0), fCurrentTrack(-1), fNPrimary(0), fObjectNumber(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 Ex02MCStack::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 /// \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 TParticle* particleDef = new TParticle(pdg, is, parent, -1, kFirstDaughter,
94 kLastDaughter, px, py, pz, e, vx, vy, vz, tof);
95
96 particleDef->SetPolarisation(polx, poly, polz);
97 particleDef->SetWeight(weight);
98 particleDef->SetUniqueID(mech);
99
100 Ex02Particle* mother = 0;
101 if (parent >= 0)
102 mother = GetParticle(parent);
103 else
104 fNPrimary++;
105
106 Ex02Particle* particle = new Ex02Particle(GetNtrack(), particleDef, mother);
107 if (mother) mother->AddDaughter(particle);
108 fParticles->Add(particle);
109
110 if (toBeDone) fStack.push(particle);
111
112 ntr = GetNtrack() - 1;
113}
114
115//_____________________________________________________________________________
116TParticle* Ex02MCStack::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 Ex02Particle* particle = fStack.top();
126 fStack.pop();
127
128 if (!particle) return 0;
129
130 itrack = particle->GetID();
131 fCurrentTrack = itrack;
132
133 return particle->GetParticle();
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 ((Ex02Particle*)fParticles->At(i))->GetParticle();
147}
148
149//_____________________________________________________________________________
150void Ex02MCStack::Print(Option_t* /*option*/) const
151{
152 /// Print info for all particles.
153
154 cout << "Ex02MCStack 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++) {
159 GetParticle(i)->Print();
160 // GetParticle(i)->PrintDaughters();
161 }
162}
163
164//_____________________________________________________________________________
166{
167 /// Delete contained particles, reset particles array and stack.
168
169 // reset fStack
170 fCurrentTrack = -1;
171 fNPrimary = 0;
172 // fParticles->Delete();
173 fParticles->Clear();
174
175 // Restore Object count
176 // To save space in the table keeping track of all referenced objects
177 // we assume that our events do not address each other. We reset the
178 // object count to what it was at the beginning of the event
179 TProcessID::SetObjectCount(fObjectNumber);
180}
181
182//_____________________________________________________________________________
184{
185 /// Set the current track number to a given value.
186 /// \param track The current track number
187
188 fCurrentTrack = track;
189}
190
191//_____________________________________________________________________________
193{
194 /// Set the current value of Root object counter into fObjectNumber.
195 /// Tis value will be restored in Reset.
196
197 fObjectNumber = TProcessID::GetObjectCount();
198}
199
200//_____________________________________________________________________________
202{
203 /// \return The total number of all tracks.
204
205 return fParticles->GetEntriesFast();
206}
207
208//_____________________________________________________________________________
210{
211 /// \return The total number of primary tracks.
212
213 return fNPrimary;
214}
215
216//_____________________________________________________________________________
218{
219 /// \return The current track particle
220
222
223 if (current)
224 return current->GetParticle();
225 else
226 return 0;
227}
228
229//_____________________________________________________________________________
231{
232 /// \return The current track number
233
234 return fCurrentTrack;
235}
236
237//_____________________________________________________________________________
239{
240 /// \return The current track parent ID.
241
243
244 if (!current) return -1;
245
246 Ex02Particle* mother = current->GetMother();
247
248 if (!mother) return -1;
249
250 return mother->GetID();
251}
252
253//_____________________________________________________________________________
255{
256 /// \return The \em id -th particle in fParticles
257 /// \param id The index of the particle to be returned
258
259 if (id < 0 || id >= fParticles->GetEntriesFast())
260 Fatal("GetParticle", "Index out of range");
261
262 return (Ex02Particle*)fParticles->At(id);
263}
Definition of the Ex02MCStack class.
Implementation of the TVirtualMCStack interface.
Definition Ex02MCStack.h:33
virtual ~Ex02MCStack()
virtual void Print(Option_t *option="") const
virtual TParticle * GetCurrentTrack() const
virtual void SetCurrentTrack(Int_t track)
Int_t fCurrentTrack
The current track number.
Definition Ex02MCStack.h:65
virtual TParticle * PopPrimaryForTracking(Int_t i)
virtual Int_t GetNtrack() const
virtual TParticle * PopNextTrack(Int_t &track)
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)
Ex02Particle * GetParticle(Int_t id) const
void SetObjectNumber()
virtual Int_t GetCurrentTrackNumber() const
Int_t fObjectNumber
The Root object number counter.
Definition Ex02MCStack.h:67
virtual Int_t GetCurrentParentTrackNumber() const
virtual Int_t GetNprimary() const
std::stack< Ex02Particle * > fStack
The stack of particles (transient)
Definition Ex02MCStack.h:63
Int_t fNPrimary
The number of primaries.
Definition Ex02MCStack.h:66
TObjArray * fParticles
The array of particle (persistent)
Definition Ex02MCStack.h:64
Extended TParticle with pointers to mother and daughter particles.
Int_t GetID() const
virtual void Print(Option_t *option="") const
Ex02Particle * GetMother() const
TParticle * GetParticle() const
void AddDaughter(Ex02Particle *particle)