Geant4 VMC Version 6.6
Loading...
Searching...
No Matches
TG4TrackingAction.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
15#include "TG4TrackingAction.h"
16#include "TG4GeometryServices.h"
17#include "TG4Globals.h"
18#include "TG4ParticlesManager.h"
19#include "TG4PhysicsManager.h"
20#include "TG4SDServices.h"
23#include "TG4StackPopper.h"
24#include "TG4StepManager.h"
25#include "TG4TrackInformation.h"
26#include "TG4TrackManager.h"
27
28#include <TMCParticleStatus.h>
29#include <TMCProcess.h>
30#include <TVirtualMC.h>
31#include <TVirtualMCApplication.h>
32
33#include <G4TrackVector.hh>
34#include <G4TrackingManager.hh>
35#include <G4UImanager.hh>
36
37// static data members
39
40//_____________________________________________________________________________
43 TG4Verbose("trackingAction", 2),
44 fMessenger(this),
45 fSpecialControls(0),
46 fTrackManager(0),
47 fMCApplication(0),
48 fMCStack(0),
49 fStepManager(0),
50 fStackPopper(0),
51 fPrimaryTrackID(0),
52 fCurrentTrackID(0),
53 fTrackSaveControl(kDoNotSave),
54 fOverwriteLastTrack(false),
55 fNewVerboseLevel(0),
56 fNewVerboseTrackID(-1),
57 fDoFinishPrimary(true)
58{
60
61 if (fgInstance) {
62 TG4Globals::Exception("TG4TrackingAction", "TG4TrackingAction",
63 "Cannot create two instances of singleton.");
64 }
65
67
68 fgInstance = this;
69}
70
71//_____________________________________________________________________________
80
81//
82// private methods
83//
84
85//_____________________________________________________________________________
86void TG4TrackingAction::UserProcessHits(const G4Track* /*track*/)
87{
91
92 G4VPhysicalVolume* pv = fStepManager->GetCurrentPhysicalVolume();
93
94 if (!pv) {
95 // was exception
97 "TG4TrackingAction", "UserProcessHits", "Cannot locate track vertex.");
98 return;
99 }
100
101#ifdef MCDEBUG
103 pv->GetLogicalVolume()->GetSensitiveDetector());
104
105 if (tsd) tsd->ProcessHitsOnTrackStart();
106#else
108 (TG4SensitiveDetector*)pv->GetLogicalVolume()->GetSensitiveDetector();
109
110 if (tsd) tsd->ProcessHitsOnTrackStart();
111#endif
112}
113
114//_____________________________________________________________________________
116{
119
120 if ((VerboseLevel() == 1 && fPrimaryTrackID % 100 == 0) ||
121 (VerboseLevel() == 2 && fPrimaryTrackID % 10 == 0) ||
122 VerboseLevel() == 3) {
123
124 G4cout << "$$$X Primary track " << fPrimaryTrackID << G4endl;
125 }
126}
127
128//
129// public methods
130//
131
132//_____________________________________________________________________________
134{
136
137 fMCApplication = TVirtualMCApplication::Instance();
140
142}
143
144//_____________________________________________________________________________
146{
148
149 // set g4 stepping manager pointer and world volume
150 fStepManager->SetSteppingManager(fpTrackingManager->GetSteppingManager());
151
152 fTrackManager->SetG4TrackingManager(fpTrackingManager);
153
156 else
157 fTrackManager->SetNofTracks(fMCStack->GetNtrack());
158
159 fCurrentTrackID = 0;
160}
161
162//_____________________________________________________________________________
164{
166
167 // do not call this function more than once
168 if (track->GetTrackID() == fCurrentTrackID) return;
169
170 // keep this track number for the check above
171 fCurrentTrackID = track->GetTrackID();
172
173 G4bool isFirstStep = (track->GetCurrentStepNumber() == 0);
174
175 // finish previous primary track first
176 if (track->GetParentID() == 0 && isFirstStep) {
178 }
179
180 // initialize special controls manager
182
183 // reset stack popper (if activated
185
186 // set track information
187 TG4TrackInformation* trackInfo =
189
191
192 TMCParticleStatus* particleStatus =
193 const_cast<TMCParticleStatus*>(trackInfo->GetInitialTrackStatus());
194
195 if (isFirstStep) {
196 // set step manager status
197 fStepManager->SetStep((G4Track*)track, kVertex);
198 fStepManager->SetInitialVMCTrackStatus(particleStatus);
199
200 if (track->GetParentID() == 0) {
201 fPrimaryTrackID = track->GetTrackID();
202
203 // begin this primary track if the VMC particle is actually a primary and
204 // only if it has initial step number == 0
205 if (!particleStatus ||
206 (particleStatus->fStepNumber == 0 && particleStatus->fParentId < 0)) {
207 fMCStack->SetCurrentTrack(trackInfo->GetTrackParticleID());
208 fMCApplication->BeginPrimary();
209 }
210
211 fDoFinishPrimary = true;
212 if (particleStatus && particleStatus->fParentId > -1) {
213 // Flag that to be a
214 fDoFinishPrimary = false;
215 }
216
217 // set saving flag
219 }
220 else {
221 // set saving flag
223 ? kDoNotSave
225 }
226
227 // save track in stack
230
231 // Notify a stack popper (if activated) about saving this secondary
233 }
234 }
235
236 fMCStack->SetCurrentTrack(trackInfo->GetTrackParticleID());
237
238 // verbose
239 if (track->GetTrackID() == fNewVerboseTrackID) {
240 fpTrackingManager->SetVerboseLevel(fNewVerboseLevel);
241 }
242
243 // VMC application pre track action
244 if (isFirstStep) {
245 if (!particleStatus || particleStatus->fStepNumber == 0) {
246 fMCApplication->PreTrack();
247 }
248
249 // call pre-tracking action of derived class
250 PreTrackingAction(track);
251
252 if (track->GetTrackStatus() != fStopAndKill) {
253 // Let sensitive detector process vertex step
254 UserProcessHits(track);
255 }
256 }
257}
258
259//_____________________________________________________________________________
261{
263
264#ifdef STACK_WITH_KEEP_FLAG
265 // Remember whether this track should be kept in the stack
266 // or can be overwritten:
267 // the track will not be overwritten if it was flagged in the stack
268 // to be kept or if it has produced any secondary particles.
269 fOverwriteLastTrack = (!fMCStack->GetKeepCurrentTrack()) &&
270 (!fpTrackingManager->GimmeSecondaries() ||
271 fpTrackingManager->GimmeSecondaries()->size() == 0);
272 // Experimental code with flagging tracks in stack for overwrite;
273 // not yet available in distribution
274#else
275 fOverwriteLastTrack = false;
276#endif
277
278 // restore processes activation
281
282 // set back max step limit if it has been modified on fly by user
284
285 // set parent track particle index to the secondary tracks
287
288 // restore particle lifetime if it was modified by user
290
291 // Do this only if the track was not interrupted but either stopped or for all
292 // other reasons the transport has been finished.
293 auto trackInfo = fTrackManager->GetTrackInformation(track);
294 if (track->GetTrackStatus() != fSuspend && !trackInfo->IsInterrupt()) {
295
296 // VMC application post track action
297 fMCApplication->PostTrack();
298
299 // call post-tracking action of derived class
300 PostTrackingAction(track);
301 }
302 const TMCParticleStatus* particleStatus = trackInfo->GetInitialTrackStatus();
303 // If this is an interrupted primary, when we pick up the next primary,
304 // this one is not finished yet...
305 if (particleStatus && particleStatus->fParentId < 0 &&
306 trackInfo->IsInterrupt()) {
307 fDoFinishPrimary = false;
308 }
309}
310
311//_____________________________________________________________________________
313{
318 // ---
319
320 if (fPrimaryTrackID > 0) {
321
322 // set special step manager status
323 // not in both stepping, vertex stage
324 G4Track* noTrack = 0;
325 fStepManager->SetStep(noTrack, kVertex);
326
327 // verbose
328 Verbose();
329
330 // VMC application finish primary track
331 if (fDoFinishPrimary) {
332 fMCApplication->FinishPrimary();
333 fDoFinishPrimary = false;
334 }
335 }
336
337 fPrimaryTrackID = 0;
338}
339
340//_____________________________________________________________________________
342{
345
346 fNewVerboseLevel = level;
347}
348
349//_____________________________________________________________________________
351{
354
355 fNewVerboseTrackID = trackID;
356}
357
358//_____________________________________________________________________________
360{
362
363 return fNewVerboseLevel;
364}
365
366//_____________________________________________________________________________
368{
370
371 return fNewVerboseTrackID;
372}
373
374//_____________________________________________________________________________
Definition of the TG4GeometryServices class.
Definition of the TG4Globals class and basic container types.
Definition of the TG4ParticlesManager class.
Definition of the TG4PhysicsManager class.
Definition of the TG4SDServices class.
Definition of the TG4SensitiveDetector class.
Definition of the TG4SpecialControlsV2 class.
Definition of the TG4StackPopper class.
Definition of the TG4StepManager class.
@ kVertex
in track vertex
Definition of the TG4TrackInformation class.
Definition of the TG4TrackManager class.
Definition of the TG4TrackingAction class.
static void Warning(const TString &className, const TString &methodName, const TString &text)
static void Exception(const TString &className, const TString &methodName, const TString &text)
TG4SensitiveDetector * GetSensitiveDetector(G4VSensitiveDetector *sd) const
static TG4SDServices * Instance()
Sensitive detector class for calling a user defined stepping function.
virtual void ProcessHitsOnTrackStart()
void StartTrack(const G4Track *track)
static TG4StackPopper * Instance()
void SetSteppingManager(G4SteppingManager *manager)
static TG4StepManager * Instance()
void SetStep(G4Step *step, TG4StepStatus status)
G4VPhysicalVolume * GetCurrentPhysicalVolume() const
TG4Limits * GetLimitsModifiedOnFly() const
void SetInitialVMCTrackStatus(TMCParticleStatus *status)
Defines additional track information.
const TMCParticleStatus * GetInitialTrackStatus() const
G4int GetTrackParticleID() const
The class for storing G4 tracks in VMC sack.
TG4TrackSaveControl GetTrackSaveControl() const
void SetG4TrackingManager(G4TrackingManager *trackingManager)
void SetBackPDGLifetime(const G4Track *aTrack)
TG4TrackInformation * GetTrackInformation(const G4Track *track) const
void SetParentToTrackInformation(const G4Track *aTrack)
G4bool IsUserTrack(const G4Track *track) const
void TrackToStack(const G4Track *track, G4bool overWrite=false)
void SetNofTracks(G4int nofTracks)
TG4TrackInformation * SetTrackInformation(const G4Track *aTrack, G4bool overWrite=false)
Actions at the beginnig and at the end of track.
G4int fPrimaryTrackID
current primary track ID
virtual void PostTrackingAction(const G4Track *aTrack)
void SetNewVerboseLevel(G4int level)
G4bool fOverwriteLastTrack
control of saving current track
static G4ThreadLocal TG4TrackingAction * fgInstance
this instance
TG4TrackManager * GetTrackManager() const
G4int fCurrentTrackID
current track ID
G4int fNewVerboseTrackID
track ID for which the new verbose level is applied
virtual void PreUserTrackingAction(const G4Track *aTrack)
G4int GetNewVerboseLevel() const
TG4StepManager * fStepManager
Cached pointer to thread-local step manager.
TG4TrackManager * fTrackManager
track manager
G4int fNewVerboseLevel
new /tracking/verbose level
void UserProcessHits(const G4Track *aTrack)
G4int GetNewVerboseTrackID() const
virtual void PreTrackingAction(const G4Track *aTrack)
TG4TrackSaveControl fTrackSaveControl
control of saving secondary tracks
virtual void PrepareNewEvent()
TG4StackPopper * fStackPopper
Cached pointer to thread-local stack popper.
TVirtualMCApplication * fMCApplication
Cached pointer to thread-local VMC application.
void SetNewVerboseTrackID(G4int trackID)
virtual void PostUserTrackingAction(const G4Track *aTrack)
TVirtualMCStack * fMCStack
Cached pointer to thread-local VMC stack.
TG4SpecialControlsV2 * fSpecialControls
the special controls manager
Base class for defining the verbose level and a common messenger.
Definition TG4Verbose.h:36
virtual G4int VerboseLevel() const
Definition TG4Verbose.h:78
@ kSaveInPreTrack
save in pre-track
@ kDoNotSave
do not save
@ fStackPopper