Geant4 11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4ImportanceAlgorithm.cc
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26// G4ImportanceAlgorithm implementation
27//
28// Author: Michael Dressel (CERN), 2002
29// ----------------------------------------------------------------------
30
31#include "G4Types.hh"
32#include "Randomize.hh"
33#include "G4Threading.hh"
34#include "G4AutoLock.hh"
35
37
38#include <sstream>
39
40namespace
41{
42 G4Mutex ImportanceMutex = G4MUTEX_INITIALIZER;
43}
44
47 G4double ipost,
48 G4double init_w) const
49{
50 G4AutoLock l(&ImportanceMutex);
52 if (ipost>0.)
53 {
54 if (!(ipre>0.))
55 {
56 Error("Calculate() - ipre==0.");
57 }
58 G4double ipre_over_ipost = ipre/ipost;
59 if ((ipre_over_ipost<0.25 || ipre_over_ipost> 4) && !fWarned)
60 {
61 std::ostringstream os;
62 os << "Calculate() - ipre_over_ipost ! in [0.25, 4]." << G4endl
63 << "ipre_over_ipost = " << ipre_over_ipost << ".";
64 Warning(os.str());
65 fWarned = true;
66 if (ipre_over_ipost<=0)
67 {
68 Error("Calculate() - ipre_over_ipost<=0.");
69 }
70 }
71 if (init_w<=0.)
72 {
73 Error("Calculate() - iniitweight<= 0. found!");
74 }
75
76 // default geometrical splitting
77 // in integer mode
78 // for ipre_over_ipost <= 1
79 G4double inv = 1./ipre_over_ipost;
80 nw.fN = static_cast<G4int>(inv);
81 nw.fW = init_w * ipre_over_ipost;
82
83 // geometrical splitting for double mode
84 if (ipre_over_ipost<1)
85 {
86 if ( static_cast<G4double>(nw.fN) != inv)
87 {
88 // double mode
89 // probability p for splitting into n+1 tracks
90 G4double p = inv - nw.fN;
91 // get a random number out of [0,1)
93 if (r<p)
94 {
95 ++nw.fN;
96 }
97 }
98 }
99 else if (ipre_over_ipost>1) // russian roulette
100 {
101 // probabiity for killing track
102 G4double p = 1-inv;
103 // get a random number out of [0,1)
105 if (r<p)
106 {
107 // kill track
108 nw.fN = 0;
109 nw.fW = 0;
110 }
111 else
112 {
113 nw.fN = 1;
114 }
115 }
116 }
117 l.unlock();
118
119 return nw;
120}
121
122void G4ImportanceAlgorithm::Error(const G4String& msg) const
123{
124 G4Exception("G4ImportanceAlgorithm::Error()",
125 "GeomBias0002", FatalException, msg);
126}
127
128void G4ImportanceAlgorithm::Warning(const G4String& msg) const
129{
130 G4Exception("G4ImportanceAlgorithm::Warning()",
131 "GeomBias1001", JustWarning, msg);
132}
G4TemplateAutoLock< G4Mutex > G4AutoLock
@ JustWarning
@ FatalException
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
#define G4MUTEX_INITIALIZER
std::mutex G4Mutex
double G4double
Definition G4Types.hh:83
int G4int
Definition G4Types.hh:85
#define G4endl
Definition G4ios.hh:67
#define G4UniformRand()
Definition Randomize.hh:52
G4Nsplit_Weight Calculate(G4double ipre, G4double ipost, G4double init_w) const override
G4Nsplit_Weight is a class (struct) used by importance sampling. It contains the number of tracks a m...