Geant4 VMC Version 6.6
Loading...
Searching...
No Matches
TG4PrimaryGeneratorAction.cxx
Go to the documentation of this file.
1//------------------------------------------------
2// The Geant4 Virtual Monte Carlo package
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
14
16#include "TG4EventAction.h"
17#include "TG4G3Units.h"
18#include "TG4Globals.h"
19#include "TG4ParticlesManager.h"
21#include "TG4RunManager.h"
22#include "TG4StateManager.h"
23#include "TG4TrackManager.h"
24#include "TG4UserIon.h"
25
26#include <G4Event.hh>
27#include <G4IonTable.hh>
28#include <G4ParticleDefinition.hh>
29#include <G4ParticleTable.hh>
30#include <G4RunManager.hh>
31
32#include <TMCManagerStack.h>
33#include <TMCParticleStatus.h>
34#include <TParticle.h>
35#include <TVirtualMC.h>
36#include <TVirtualMCApplication.h>
37#include <TVirtualMCStack.h>
38
39// Moved after Root includes to avoid shadowed variables
40// generated from short units names
41#include <G4SystemOfUnits.hh>
42
43namespace
44{
45
46TG4EventAction* GetEventAction()
47{
48 return static_cast<TG4EventAction*>(const_cast<G4UserEventAction*>(
49 G4RunManager::GetRunManager()->GetUserEventAction()));
50}
51
52} // namespace
53
54//_____________________________________________________________________________
56 : TG4Verbose("primaryGeneratorAction"),
57 fMessenger(0),
58 fParticlesManager(0),
59 fTrackManager(0),
60 fMCStack(0),
61 fMCManagerStack(0),
62 fCached(false),
63 fSkipUnknownParticles(false)
64{
66
68}
69
70//_____________________________________________________________________________
77
78//
79// private methods
80//
81
82//_____________________________________________________________________________
83G4bool TG4PrimaryGeneratorAction::CheckVMCStack(TVirtualMCStack* mcStack) const
84{
85
86 if (!mcStack) {
88 "TG4PrimaryGeneratorAction", "CheckVMCStack", "Found nullptr for stack.");
89 return false;
90 }
91
92 if (mcStack->GetNtrack() <= 0) {
93 TG4Globals::Warning("TG4PrimaryGeneratorAction", "CheckVMCStack",
94 "No primary particles found on the stack.");
95 return false;
96 }
97 return true;
98}
99
100//_____________________________________________________________________________
102 const G4ParticleDefinition* particleDefinition,
103 const TParticle* particle) const
104{
105 if (!particleDefinition) {
106 TString text =
107 "TG4PrimaryGeneratorAction::CheckParticleDefinition() failed for ";
108 text += TString(particle->GetName());
109 text += " pdgEncoding=";
110 text += particle->GetPdgCode();
111 text += ".";
114 "TG4PrimaryGeneratorAction", "CheckParticleDefinition", text);
115 }
116 else {
118 "TG4PrimaryGeneratorAction", "CheckParticleDefinition", text);
119 }
120 return false;
121 }
122 return true;
123}
124
125//_____________________________________________________________________________
127 const G4ParticleDefinition* particleDefinition,
128 const TParticle* particle) const
129{
130 G4double charge = particleDefinition->GetPDGCharge();
131 if (G4IonTable::IsIon(particleDefinition) &&
132 particleDefinition->GetParticleName() != "proton") {
133 // Get dynamic charge defined by user
134 TG4UserIon* userIon =
135 fParticlesManager->GetUserIon(particle->GetName(), false);
136 if (userIon) charge = userIon->GetQ() * eplus;
137 }
138 return charge;
139}
140
141//_____________________________________________________________________________
143 G4PrimaryVertex* vertex, const G4ParticleDefinition* particleDefinition,
144 const G4ThreeVector& position, G4double time, const G4ThreeVector& momentum,
145 G4double energy, const G4ThreeVector& polarization, G4double charge,
146 G4double weight) const
147{
148 auto thisVertex = vertex;
149 auto primaryParticle = new G4PrimaryParticle(
150 particleDefinition, momentum.x(), momentum.y(), momentum.z(), energy);
151 primaryParticle->SetCharge(charge);
152 primaryParticle->SetWeight(weight);
153 primaryParticle->SetPolarization(polarization);
154 if (vertex == 0 || vertex->GetPosition() != position ||
155 time != vertex->GetT0()) {
156 // Create a new vertex in case position and time of particle are different
157 // from previous values
158 thisVertex = new G4PrimaryVertex(position, time);
159 event->AddPrimaryVertex(thisVertex);
160 }
161 thisVertex->SetPrimary(primaryParticle);
162 // Verbose
163 if (VerboseLevel() > 1) {
164 G4cout << "Add primary particle to vertex: " << G4endl;
165 primaryParticle->Print();
166 }
167 return thisVertex;
168}
169
170//_____________________________________________________________________________
172{
175
177
178 G4int nofParticles = fMCStack->GetNtrack();
179
180 if (VerboseLevel() > 1)
181 G4cout << "TG4PrimaryGeneratorAction::TransformPrimaries: " << nofParticles
182 << " particles" << G4endl;
183
184 G4PrimaryVertex* previousVertex = 0;
185
186 for (G4int i = 0; i < nofParticles; i++) {
187
188 // get the particle from the stack
189 TParticle* particle = fMCStack->PopPrimaryForTracking(i);
190
191 if (particle) {
192
193 // Pass this particle Id (in the VMC stack) to Track manager
195
196 // Get particle definition from TG4ParticlesManager
197 G4ParticleDefinition* particleDefinition =
199
200 if (!CheckParticleDefinition(particleDefinition, particle)) {
201 continue;
202 }
203
204 // Current particle's position and time
205 G4ThreeVector position = fParticlesManager->GetParticlePosition(particle);
206 G4double time = particle->T() * TG4G3Units::Time();
207
208 // Current particle's momentum and energy
209 G4ThreeVector momentum = fParticlesManager->GetParticleMomentum(particle);
210 G4double energy = particle->Energy() * TG4G3Units::Energy();
211
212 // Particle's charge, weight and polarization
213 G4double charge = GetProperCharge(particleDefinition, particle);
214 G4double weight = particle->GetWeight();
215 TVector3 polarization;
216 particle->GetPolarisation(polarization);
217 G4ThreeVector g4Polarization(
218 polarization.X(), polarization.Y(), polarization.Z());
219
220 // Create new G4PrimaryParticle and add to G4PrimaryVertex.
221 previousVertex =
222 AddParticleToVertex(event, previousVertex, particleDefinition, position,
223 time, momentum, energy, g4Polarization, charge, weight);
224 }
225 }
226}
227
228//_____________________________________________________________________________
230{
233
234 // The TMCManagerStack has additional info on the current track status,
235 // e.g. kinematics and geometry state.
236 // Calling TG4PrimaryGeneratorAction::TransformTracks implies that
237 // it exists, it is the same as the TVirtualMCStack above
238
240
241 if (VerboseLevel() > 1)
242 G4cout << "TG4PrimaryGeneratorAction::TransformTracks: "
243 << fMCManagerStack->GetNtrack() << " particles" << G4endl;
244
245 G4PrimaryVertex* previousVertex = 0;
246
247 const TParticle* particle = 0;
248
249 Int_t trackId = -1;
250
251 while ((particle = fMCManagerStack->PopNextTrack(trackId))) {
252
253 if (particle) {
254 const TMCParticleStatus* particleStatus =
255 fMCManagerStack->GetParticleStatus(trackId);
256
257 // Pass current status of the particle to the trackManager containing
258 // information about potential made steps and track length != 0 in case
259 // the track was transported before
260 fTrackManager->AddParticleStatus(particleStatus);
261
262 // Get particle definition from TG4ParticlesManager
263 G4ParticleDefinition* particleDefinition =
265
266 if (!CheckParticleDefinition(particleDefinition, particle)) {
267 continue;
268 }
269
270 // Current particle's position and time
271 const TLorentzVector& particlePosition = particleStatus->fPosition;
272 G4ThreeVector position(particlePosition.X() * TG4G3Units::Length(),
273 particlePosition.Y() * TG4G3Units::Length(),
274 particlePosition.Z() * TG4G3Units::Length());
275 G4double time = particlePosition.T() * TG4G3Units::Time();
276
277 // Current particle's momentum and energy
278 const TLorentzVector& particleMomentum = particleStatus->fMomentum;
279 G4ThreeVector momentum(particleMomentum.Px() * TG4G3Units::Energy(),
280 particleMomentum.Py() * TG4G3Units::Energy(),
281 particleMomentum.Pz() * TG4G3Units::Energy());
282 G4double energy = particleMomentum.Energy() * TG4G3Units::Energy();
283
284 // Particle's charge, weight and polarization
285 G4double charge = GetProperCharge(particleDefinition, particle);
286 G4double weight = particleStatus->fWeight;
287 const TVector3& polarization = particleStatus->fPolarization;
288 G4ThreeVector g4Polarization(
289 polarization.X(), polarization.Y(), polarization.Z());
290
291 // Create new G4PrimaryParticle and add to G4PrimaryVertex.
292 previousVertex =
293 AddParticleToVertex(event, previousVertex, particleDefinition, position,
294 time, momentum, energy, g4Polarization, charge, weight);
295 }
296 }
297}
298
299//
300// public methods
301//
302
303//_____________________________________________________________________________
305{
307
308 // Cache pointers to thread-local objects
310 TG4EventAction* eventAction = GetEventAction();
311 TVirtualMCApplication* mcApplication = TVirtualMCApplication::Instance();
312 TVirtualMC* vmc = gMC;
313
314 // Begin of event
316
317 if (!fCached) {
320 fMCManagerStack = vmc->GetManagerStack();
321 fCached = true;
322 }
323
324 // If TG4RunManager::IsInterruptibleEvent(), rely on BeginEvent() has been
325 // called already.
326 if (!eventAction->IsInterruptibleEvent()) {
327 mcApplication->BeginEvent();
328 }
329
330 // Update cached pointer to MC stack which can changed in some applications
331 // in MCApplication::BeginEvent()
332 fMCStack = vmc->GetStack();
333 runManager->CacheMCStack();
334
335 // Clear cached VMC stack info from former event
338
339 // Don't generate primaries if this is a complex interruptible event
340 if (!fMCManagerStack) {
341 // Generate primaries and fill the VMC stack
342 mcApplication->GeneratePrimaries();
343 TransformPrimaries(event);
344 }
345 else {
346 TransformTracks(event);
347 }
348}
Definition of the TG4EventAction class.
Definition of the TG4G3Units class.
Definition of the TG4Globals class and basic container types.
Definition of the TG4ParticlesManager class.
Definition of the TG4PrimaryGeneratorAction class.
Definition of the TG4PrimaryGeneratorMessenger class.
Definition of the TG4RunManager.h class.
Definition of the TG4StateManager class.
Definition of the TG4TrackManager class.
Definition of the TG4UserIon class.
Actions at the beginning and the end of event.
G4bool IsInterruptibleEvent() const
static G4double Energy()
Definition TG4G3Units.h:105
static G4double Length()
Definition TG4G3Units.h:81
static G4double Time()
Definition TG4G3Units.h:93
static void Warning(const TString &className, const TString &methodName, const TString &text)
static void Exception(const TString &className, const TString &methodName, const TString &text)
G4ParticleDefinition * GetParticleDefinition(const TParticle *particle, G4bool warn=true) const
G4ThreeVector GetParticlePosition(const TParticle *particle) const
TG4UserIon * GetUserIon(const G4String &ionName, G4bool warn=true) const
G4ThreeVector GetParticleMomentum(const TParticle *particle) const
static TG4ParticlesManager * Instance()
G4bool fSkipUnknownParticles
Option to skip particles which do not exist in Geant4.
G4bool CheckParticleDefinition(const G4ParticleDefinition *particleDefinition, const TParticle *particle) const
TG4ParticlesManager * fParticlesManager
Thread-local particles manager.
TG4PrimaryGeneratorMessenger * fMessenger
Messenger.
TVirtualMCStack * fMCStack
Thread-local stacks.
G4bool fCached
Flag whether thread-local variables have been cached.
G4PrimaryVertex * AddParticleToVertex(G4Event *event, G4PrimaryVertex *vertex, const G4ParticleDefinition *particleDefinition, const G4ThreeVector &position, G4double time, const G4ThreeVector &momentum, G4double energy, const G4ThreeVector &polarization, G4double charge, G4double weight) const
virtual void GeneratePrimaries(G4Event *event)
G4bool CheckVMCStack(TVirtualMCStack *stack) const
TG4TrackManager * fTrackManager
Thread-local track manager.
G4double GetProperCharge(const G4ParticleDefinition *particleDefinition, const TParticle *particle) const
Messenger class that defines commands for TG4PrimaryGeneratorAction.
Geant4 implementation of the TVirtualMC interface methods for access to Geant4 at run level.
static TG4RunManager * Instance()
void SetNewState(TG4ApplicationState state)
static TG4StateManager * Instance()
void AddParticleStatus(const TMCParticleStatus *particleStatus)
static TG4TrackManager * Instance()
void AddPrimaryParticleId(G4int id)
The class for user ion definition.
Definition TG4UserIon.h:31
G4int GetQ() const
Return the ion charge.
Definition TG4UserIon.h:50
Base class for defining the verbose level and a common messenger.
Definition TG4Verbose.h:36
virtual G4int VerboseLevel() const
Definition TG4Verbose.h:78
@ kInEvent
in event processing