Geant4 VMC Version 6.7
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
TG4EventAction.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 "TG4EventAction.h"
16#include "TG4Globals.h"
17#include "TG4ParticlesManager.h"
18#include "TG4SDServices.h"
19#include "TG4StateManager.h"
20#include "TG4TrackManager.h"
21#include "TG4TrackingAction.h"
22
23#include <G4Version.hh>
24#if G4VERSION_NUMBER == 1100
25// Temporary work-around for bug in Cerenkov
26#include "TG4PhysicsManager.h"
27#endif
28
29#include <G4Event.hh>
30#include <G4Trajectory.hh>
31#include <G4TrajectoryContainer.hh>
32#include <G4UImanager.hh>
33#include <G4VVisManager.hh>
34#include <Randomize.hh>
35
36#include <RVersion.h>
37#include <TSystem.h>
38#include <TVirtualMC.h>
39#include <TVirtualMCApplication.h>
40#include <TVirtualMCSensitiveDetector.h>
41#include <TVirtualMCStack.h>
42
43#include <math.h>
44
45//_____________________________________________________________________________
47 : TG4Verbose("eventAction"),
48 fMessenger(this),
49 fTimer(),
51 fMCStack(0),
55 fPrintMemory(false),
56 fSaveRandomStatus(false),
58{
60}
61
62//_____________________________________________________________________________
67
68//
69// public methods
70//
71
72//_____________________________________________________________________________
82
83//_____________________________________________________________________________
84void TG4EventAction::BeginOfEventAction(const G4Event* event)
85{
87
88#if G4VERSION_NUMBER == 1100
89 // Temporary work-around for bug in Cerenkov
90 static G4ThreadLocal auto applyCerenkovFix = true;
91 if (applyCerenkovFix) {
92 TG4PhysicsManager::Instance()->ApplyCerenkovMaxBetaChangeValue();
93 applyCerenkovFix = false;
94 }
95#endif
96
97 // reset the tracks counters
98 fTrackingAction->PrepareNewEvent();
99
100 // fill primary particles in VMC stack if stack is empty
101 if (fMCStack->GetNtrack() == 0) {
102 if (VerboseLevel() > 0)
103 G4cout << "Filling VMC stack with primaries" << G4endl;
104
105 for (G4int iv = 0; iv < event->GetNumberOfPrimaryVertex(); iv++) {
106 G4PrimaryVertex* vertex = event->GetPrimaryVertex(iv);
107
108 for (G4int ip = 0; ip < vertex->GetNumberOfParticle(); ip++) {
109 G4PrimaryParticle* particle = vertex->GetPrimary(ip);
110 fTrackManager->PrimaryToStack(vertex, particle);
111 }
112 }
113 }
114
115 // save the event random number status per event
116 if (fSaveRandomStatus) {
117 G4UImanager::GetUIpointer()->ApplyCommand("/random/saveThisEvent");
118 if (VerboseLevel() > 0) G4cout << "Saving random status: " << G4endl;
119 CLHEP::HepRandom::showEngineStatus();
120 G4cout << G4endl;
121 }
122
123 if (VerboseLevel() > 0) {
124 G4cout << ">>> Event " << event->GetEventID() << G4endl;
125 fTimer.Start();
126 }
127}
128
129//_____________________________________________________________________________
130void TG4EventAction::EndOfEventAction(const G4Event* event)
131{
133
134 // finish the last primary track of the current event
135 // G4cout << "Finish primary from event action" << G4endl;
136 fTrackingAction->FinishPrimaryTrack();
137
138 if (VerboseLevel() > 1) {
139 G4cout << G4endl;
140 G4cout << ">>> End of Event " << event->GetEventID() << G4endl;
141 }
142
143 if (VerboseLevel() > 2) {
144 G4int nofPrimaryTracks = fMCStack->GetNprimary();
145 G4int nofSavedTracks = fMCStack->GetNtrack();
146
147 G4cout << " " << nofPrimaryTracks << " primary tracks processed."
148 << G4endl;
149 G4cout << " " << nofSavedTracks << " tracks saved." << G4endl;
150
151 G4int nofAllTracks = fTrackManager->GetNofTracks();
152 G4cout << " " << nofAllTracks << " all tracks processed." << G4endl;
153 }
154
155#if ROOT_VERSION_CODE >= ROOT_VERSION(6, 18, 0)
156 // VMC application end of event
157 fMCApplication->EndOfEvent();
158#endif
159
160 // User SDs finish event
161 if (TG4SDServices::Instance()->GetUserSDs()) {
162 for (auto& userSD : (*TG4SDServices::Instance()->GetUserSDs())) {
163 userSD->EndOfEvent();
164 }
165 }
166
167 // VMC application finish event
169 fMCApplication->FinishEvent();
170 }
171 fStateManager->SetNewState(kNotInApplication);
172
173 if (VerboseLevel() > 1) {
174 // print time
175 fTimer.Stop();
176 fTimer.Print();
177 }
178
179 if (fPrintMemory) {
180 ProcInfo_t procInfo;
181 gSystem->GetProcInfo(&procInfo);
182 G4cout << "Current memory usage: resident " << procInfo.fMemResident
183 << ", virtual " << procInfo.fMemVirtual << G4endl;
184 }
185}
Definition of the TG4EventAction 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 TG4StateManager class.
Definition of the TG4TrackManager class.
Definition of the TG4TrackingAction class.
G4bool fIsInterruptibleEvent
virtual void BeginOfEventAction(const G4Event *event)
G4bool fSaveRandomStatus
Control for saving random engine status for each event.
TG4TrackingAction * fTrackingAction
Cached pointer to thread-local tracking action.
TVirtualMCApplication * fMCApplication
Cached pointer to thread-local VMC application.
G4bool fPrintMemory
Control for printing memory usage.
TStopwatch fTimer
timer
TG4EventActionMessenger fMessenger
messenger
TVirtualMCStack * fMCStack
Cached pointer to thread-local VMC stack.
TG4StateManager * fStateManager
Cached pointer to thread-local state manager.
virtual void EndOfEventAction(const G4Event *event)
TG4TrackManager * fTrackManager
Cached pointer to thread-local track manager.
virtual ~TG4EventAction()
static TG4PhysicsManager * Instance()
static TG4SDServices * Instance()
static TG4StateManager * Instance()
static TG4TrackManager * Instance()
static TG4TrackingAction * Instance()
virtual G4int VerboseLevel() const
Definition TG4Verbose.h:78
TG4Verbose(const G4String &cmdName)
@ kNotInApplication
not in VMC application