Geant4  10.03.p03
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 //
27 // $Id: G4ImportanceAlgorithm.cc 105146 2017-07-14 07:58:24Z gcosmo $
28 //
29 // ----------------------------------------------------------------------
30 // GEANT 4 class source file
31 //
32 // G4ImportanceAlgorithm.cc
33 //
34 // ----------------------------------------------------------------------
35 
36 #include "G4Types.hh"
37 #include <sstream>
38 #include "Randomize.hh"
39 #include "G4Threading.hh"
40 
41 #include "G4ImportanceAlgorithm.hh"
42 
43 #ifdef G4MULTITHREADED
44 G4Mutex G4ImportanceAlgorithm::ImportanceMutex = G4MUTEX_INITIALIZER;
45 #endif
46 
48 {
49 }
50 
52 {
53 }
54 
57  G4double ipost,
58  G4double init_w) const
59 {
60 
61 #ifdef G4MULTITHREADED
62  G4MUTEXLOCK(&G4ImportanceAlgorithm::ImportanceMutex);
63 #endif
64 
65  G4Nsplit_Weight nw = {0,0};
66  if (ipost>0.){
67  if (!(ipre>0.)){
68  Error("Calculate() - ipre==0.");
69  }
70  G4double ipre_over_ipost = ipre/ipost;
71  if ((ipre_over_ipost<0.25 || ipre_over_ipost> 4) && !fWorned) {
72  std::ostringstream os;
73  os << "Calculate() - ipre_over_ipost ! in [0.25, 4]." << G4endl
74  << "ipre_over_ipost = " << ipre_over_ipost << ".";
75  Warning(os.str());
76  fWorned = true;
77  if (ipre_over_ipost<=0) {
78  Error("Calculate() - ipre_over_ipost<=0.");
79  }
80  }
81  if (init_w<=0.) {
82  Error("Calculate() - iniitweight<= 0. found!");
83  }
84 
85  // default geometrical splitting
86  // in integer mode
87  // for ipre_over_ipost <= 1
88  G4double inv = 1./ipre_over_ipost;
89  nw.fN = static_cast<G4int>(inv);
90  nw.fW = init_w * ipre_over_ipost;
91 
92  // geometrical splitting for double mode
93  if (ipre_over_ipost<1) {
94  if ( static_cast<G4double>(nw.fN) != inv) {
95  // double mode
96  // probability p for splitting into n+1 tracks
97  G4double p = inv - nw.fN;
98  // get a random number out of [0,1)
99  G4double r = G4UniformRand();
100  if (r<p) {
101  nw.fN++;
102  }
103  }
104  }
105  // ipre_over_ipost > 1
106  // russian roulett
107  else if (ipre_over_ipost>1) {
108  // probabiity for killing track
109  G4double p = 1-inv;
110  // get a random number out of [0,1)
111  G4double r = G4UniformRand();
112  if (r<p) {
113  // kill track
114  nw.fN = 0;
115  nw.fW = 0;
116  }
117  else {
118  nw.fN = 1;
119  }
120  }
121  }
122 #ifdef G4MULTITHREADED
123  G4MUTEXUNLOCK(&G4ImportanceAlgorithm::ImportanceMutex);
124 #endif
125  return nw;
126 }
127 
128 void G4ImportanceAlgorithm::Error(const G4String &msg) const
129 {
130  G4Exception("G4ImportanceAlgorithm::Error()",
131  "GeomBias0002", FatalException, msg);
132 }
133 
134 void G4ImportanceAlgorithm::Warning(const G4String &msg) const
135 {
136  G4Exception("G4ImportanceAlgorithm::Warning()",
137  "GeomBias1001", JustWarning, msg);
138 }
#define G4MUTEXUNLOCK
Definition: G4Threading.hh:180
virtual G4Nsplit_Weight Calculate(G4double ipre, G4double ipost, G4double init_w) const
const char * p
Definition: xmltok.h:285
int G4int
Definition: G4Types.hh:78
#define G4MUTEX_INITIALIZER
Definition: G4Threading.hh:175
#define G4UniformRand()
Definition: Randomize.hh:97
#define G4MUTEXLOCK
Definition: G4Threading.hh:179
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
G4int G4Mutex
Definition: G4Threading.hh:173
#define G4endl
Definition: G4ios.hh:61
double G4double
Definition: G4Types.hh:76