Geant4 VMC Version 6.6
Loading...
Searching...
No Matches
TG4RunAction.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 <G4Timer.hh>
16// in order to avoid the odd dependency for the
17// times system function this include must be the first
18
19#include "TG4Globals.h"
20#include "TG4VRegionsManager.h"
21#include "TG4RunAction.h"
22#include "TGeant4.h"
23
24#include <G4AutoLock.hh>
25#include <G4Electron.hh>
26#include <G4Positron.hh>
27#include <G4Run.hh>
28#include <G4SystemOfUnits.hh>
29#include <G4Threading.hh>
30#include <G4Transportation.hh>
31#include <G4Types.hh>
32#include <G4UImanager.hh>
33#include <Randomize.hh>
34
35#include <TObjArray.h>
36
37// mutex in a file scope
38
39namespace
40{
41
42#ifdef G4MULTITHREADED
43// Mutex to lock master application when merging data
44G4Mutex mergeMutex = G4MUTEX_INITIALIZER;
45#endif
46
47G4Transportation* FindTransportation(
48 const G4ParticleDefinition* particleDefinition)
49{
50 const auto* processManager = particleDefinition->GetProcessManager();
51 return dynamic_cast<G4Transportation*>(
52 processManager->GetProcess("Transportation"));
53}
54
55} // namespace
56
57const G4String TG4RunAction::fgkDefaultRandomStatusFile = "currentRun.rndm";
58
59//_____________________________________________________________________________
62 TG4Verbose("runAction"),
63 fMessenger(this),
64 fCrossSectionManager(),
65 fTimer(0),
66 fRunID(-1),
67 fSaveRandomStatus(false),
68 fReadRandomStatus(false),
69 fRandomStatusFile(fgkDefaultRandomStatusFile),
70 fThresholdWarningEnergy(-1.0),
71 fThresholdImportantEnergy(-1.0),
72 fNumberOfThresholdTrials(0)
73{
75
76 if (VerboseLevel() > 2)
77 G4cout << "TG4RunAction::TG4RunAction " << this << G4endl;
78
79 fTimer = new G4Timer;
80}
81
82//_____________________________________________________________________________
84{
86
87 if (VerboseLevel() > 2)
88 G4cout << "TG4RunAction::~TG4RunAction " << this << G4endl;
89
90 delete fTimer;
91}
92
93//
94// private methods
95//
96
97//_____________________________________________________________________________
99 const G4ParticleDefinition* particleDefinition)
100{
101 // Nothing to be done if no parameters change
104 return;
105 }
106
107 auto* transportation = FindTransportation(particleDefinition);
108 if (!transportation) {
109 TG4Globals::Warning("TG4RunAction", "ChangeLooperParameters",
110 "Cannot set parameters. Transportation process not found.");
111 return;
112 }
113
114 if (fThresholdWarningEnergy >= 0.) {
115 if (VerboseLevel() > 2) {
116 G4cout << "ChangeLooperParameters: ThresholdWarningEnergy [keV] = "
117 << fThresholdWarningEnergy / keV << G4endl;
118 }
119 transportation->SetThresholdWarningEnergy(fThresholdWarningEnergy);
120 }
121
122 if (fThresholdImportantEnergy >= 0.) {
123 if (VerboseLevel() > 2) {
124 G4cout << "ChangeLooperParameters: ThresholdImportantEnergy [keV] = "
125 << fThresholdImportantEnergy / keV << G4endl;
126 }
127 transportation->SetThresholdImportantEnergy(fThresholdImportantEnergy);
128 }
129
130 if (fNumberOfThresholdTrials > 0) {
131 if (VerboseLevel() > 2) {
132 G4cout << "ChangeLooperParameters: NumberOfThresholdTrials = "
133 << fNumberOfThresholdTrials << G4endl;
134 }
135 transportation->SetThresholdTrials(fNumberOfThresholdTrials);
136 }
137}
138
139//_____________________________________________________________________________
141{
143
144 auto transportation = FindTransportation(G4Electron::Definition());
145 if (transportation) {
146 G4cout << "ThresholdWarningEnergy [MeV] = "
147 << transportation->GetThresholdWarningEnergy() / MeV << G4endl;
148 G4cout << "ThresholdImportantEnergy [MeV] = "
149 << transportation->GetThresholdImportantEnergy() / MeV << G4endl;
150 G4cout << "NumberOfThresholdTrials = "
151 << transportation->GetThresholdTrials() << G4endl;
152 }
153}
154
155//
156// public methods
157//
158
159//_____________________________________________________________________________
160void TG4RunAction::BeginOfRunAction(const G4Run* run)
161{
163
164 fRunID++;
165
166 if (VerboseLevel() > 0) {
167 G4cout << "### Run " << run->GetRunID() << " start." << G4endl;
168 }
169
170 auto regionsManager = TG4VRegionsManager::Instance();
171 if (regionsManager != nullptr) {
172 if (regionsManager->IsCheck()) {
173 regionsManager->CheckRegions();
174 }
175 if (regionsManager->IsPrint()) {
176 regionsManager->PrintRegions(G4cout);
177 }
178 if (regionsManager->IsSave()) {
179 regionsManager->SaveRegions();
180 }
181 }
182
183 // activate random number status
184 if (fSaveRandomStatus) {
185 G4UImanager::GetUIpointer()->ApplyCommand("/random/setSavingFlag true");
186 if (VerboseLevel() > 0)
187 G4cout << "Activated saving random status " << G4endl;
188 CLHEP::HepRandom::showEngineStatus();
189 G4cout << G4endl;
190 }
191
192 if (fReadRandomStatus) {
193 // restore event random number status from a file
194 CLHEP::HepRandom::showEngineStatus();
195 G4String command("/random/resetEngineFrom ");
196 command += fRandomStatusFile;
197 G4UImanager::GetUIpointer()->ApplyCommand(command.data());
198 if (VerboseLevel() > 0) {
199 G4cout << "Resetting random engine from " << fRandomStatusFile << G4endl;
200 CLHEP::HepRandom::showEngineStatus();
201 G4cout << G4endl;
202 }
203 }
204
205 // set looper thresholds parameters
206 // (only if defaults are overriden by user)
207 ChangeLooperParameters(G4Electron::Electron());
208 ChangeLooperParameters(G4Positron::Positron());
209
210 // Print looping threshold parameters
211 if (VerboseLevel() > 1) {
213 }
214
215 fTimer->Start();
216}
217
218//_____________________________________________________________________________
219void TG4RunAction::EndOfRunAction(const G4Run* run)
220{
222
223#ifdef G4MULTITHREADED
224 if (! IsMaster()) {
225 // Merge user application data collected on workers to master
226 G4AutoLock lm(&mergeMutex);
228 TVirtualMCApplication::Instance());
229 lm.unlock();
230 }
231#endif
232
235 }
236
237 fTimer->Stop();
238
239 if (VerboseLevel() > 0) {
240 G4cout << "Time of this run: " << *fTimer << G4endl;
241 G4cout << "Number of events processed: " << run->GetNumberOfEvent()
242 << G4endl;
243 }
244}
Definition of the TG4Globals class and basic container types.
Definition of the TG4RunAction class.
Definition of the TG4VRegionsManager class.
Definition of the TGeant4 class.
static void Warning(const TString &className, const TString &methodName, const TString &text)
virtual void BeginOfRunAction(const G4Run *run)
G4String fRandomStatusFile
random engine status file name
G4bool fReadRandomStatus
control for reading random engine status
G4int fNumberOfThresholdTrials
Number of trials to propagate a looping track.
virtual ~TG4RunAction()
G4int fRunID
run ID
G4double fThresholdWarningEnergy
Energy threshold for warnings about killing looping tracks.
TG4CrossSectionManager fCrossSectionManager
cross section manager
G4double fThresholdImportantEnergy
virtual void EndOfRunAction(const G4Run *run)
G4bool fSaveRandomStatus
control for saving random engine status
void PrintLooperParameters() const
void ChangeLooperParameters(const G4ParticleDefinition *particleDefinition)
G4Timer * fTimer
G4Timer.
static const G4String fgkDefaultRandomStatusFile
default name of the random engine status file to be read in
static TG4VRegionsManager * Instance()
Return the singleton instance.
Base class for defining the verbose level and a common messenger.
Definition TG4Verbose.h:36
virtual G4int VerboseLevel() const
Definition TG4Verbose.h:78
static TVirtualMCApplication * MasterApplicationInstance()