Geant4  10.00.p03
G4GeneralParticleSource.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 //
27 //
28 // MODULE: G4GeneralParticleSource.cc
29 //
30 // Version: 2.0
31 // Date: 5/02/04
32 // Author: Fan Lei
33 // Organisation: QinetiQ ltd.
34 // Customer: ESA/ESTEC
35 //
36 // Documentation avaialable at http://reat.space.qinetiq.com/gps
37 // These include:
38 // User Requirement Document (URD)
39 // Software Specification Documents (SSD)
40 // Software User Manual (SUM): on-line version available
41 // Technical Note (TN) on the physics and algorithms
42 //
44 //
45 // CHANGE HISTORY
46 // --------------
47 //
48 // Version 2.0, 05/02/2004, Fan Lei, Created.
49 // After changes to version 1.1 as in Geant4 v6.0
50 // - Mutilple particle source definition
51 // - Re-structured commands
52 // - Split the task into smaller classes
53 //
54 // - old commonds have been retained for backward compatibility, will be
55 // removed in the future.
56 //
57 //
59 //
60 #include "G4Event.hh"
61 #include "Randomize.hh"
63 
65  : multiple_vertex(false), flat_sampling(false)
66 {
67  sourceVector.clear();
68  sourceIntensity.clear();
69  sourceProbability.clear();
71  sourceVector.push_back(currentSource);
72  sourceIntensity.push_back(1.);
73  currentSourceIdx = G4int(sourceVector.size() - 1);
75  theMessenger->SetParticleGun(currentSource);
77 }
78 
80 {
81  delete theMessenger;
82 }
83 
85 {
88  sourceVector.push_back(currentSource);
89  sourceIntensity.push_back(aV);
90  currentSourceIdx = G4int(sourceVector.size() - 1);
92 }
93 
95 {
96  G4double total = 0.;
97  size_t i = 0 ;
98  for (i = 0; i < sourceIntensity.size(); i++)
99  total += sourceIntensity[i] ;
100  //
101  sourceProbability.clear();
102  std::vector <G4double> sourceNormalizedIntensity;
103  sourceNormalizedIntensity.clear();
104 
105  sourceNormalizedIntensity.push_back(sourceIntensity[0]/total);
106  sourceProbability.push_back(sourceNormalizedIntensity[0]);
107 
108  for ( i = 1 ; i < sourceIntensity.size(); i++) {
109  sourceNormalizedIntensity.push_back(sourceIntensity[i]/total);
110  sourceProbability.push_back(sourceNormalizedIntensity[i] + sourceProbability[i-1]);
111  }
112 
113  // set source weights here based on sampling scheme (analog/flat) and intensities
114  for ( i = 0 ; i < sourceIntensity.size(); i++) {
115  if (!flat_sampling) {
116  sourceVector[i]->GetBiasRndm()->SetIntensityWeight(1.);
117  } else {
118  sourceVector[i]->GetBiasRndm()->SetIntensityWeight(sourceNormalizedIntensity[i]*sourceIntensity.size());
119  }
120  }
121 
122  normalised = true;
123 }
124 
126 {
127  G4cout << " The number of particle sources is " << sourceIntensity.size() << G4endl;
128  for (size_t i = 0 ; i < sourceIntensity.size(); i++)
129  G4cout << " source " << i << " intensity is " << sourceIntensity[i] << G4endl;
130 }
131 
133 {
134  size_t id = size_t (aV) ;
135  if ( id <= sourceIntensity.size() ) {
136  currentSourceIdx = aV;
139  //
140  } else {
141  G4cout << " source index is invalid " << G4endl;
142  G4cout << " it shall be <= " << sourceIntensity.size() << G4endl;
143  }
144 }
145 
147 {
149  normalised = false;
150 }
151 
153 {
154  currentSourceIdx = -1;
155  currentSource = 0;
156  sourceVector.clear();
157  sourceIntensity.clear();
158  sourceProbability.clear();
159 }
160 
162 {
163  size_t id = size_t (aV) ;
164  if ( id <= sourceIntensity.size() ) {
165  sourceVector.erase(sourceVector.begin()+aV);
166  sourceIntensity.erase(sourceIntensity.begin()+aV);
167  normalised = false ;
168  if (currentSourceIdx == aV ) {
169  if ( sourceIntensity.size() > 0 ) {
171  currentSourceIdx = 1;
172  } else {
173  currentSource = 0;
174  currentSourceIdx = -1;
175  }
176  }
177  } else {
178  G4cout << " source index is invalid " << G4endl;
179  G4cout << " it shall be <= " << sourceIntensity.size() << G4endl;
180  }
181 }
182 
184 {
185  if (!multiple_vertex){
186  if (sourceIntensity.size() > 1) {
188  G4double rndm = G4UniformRand();
189  size_t i = 0 ;
190  if (!flat_sampling) {
191  while ( rndm > sourceProbability[i] ) i++;
193  } else {
194  i = size_t (sourceIntensity.size()*rndm);
196  }
197  }
199  }
200  else {
201  for (size_t i = 0; i < sourceIntensity.size(); i++) {
202  sourceVector[i]->GeneratePrimaryVertex(evt);
203  }
204  }
205 }
std::vector< G4SingleParticleSource * > sourceVector
G4SingleParticleSource * currentSource
int G4int
Definition: G4Types.hh:78
#define G4UniformRand()
Definition: Randomize.hh:87
G4GLOB_DLL std::ostream G4cout
void SetParticleGun(G4SingleParticleSource *fpg)
G4double total(Particle const *const p1, Particle const *const p2)
std::vector< G4double > sourceIntensity
#define G4endl
Definition: G4ios.hh:61
std::vector< G4double > sourceProbability
double G4double
Definition: G4Types.hh:76
G4GeneralParticleSourceMessenger * theMessenger