Geant4 9.6.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
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//
26///////////////////////////////////////////////////////////////////////////////
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//
43///////////////////////////////////////////////////////////////////////////////
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//
58///////////////////////////////////////////////////////////////////////////////
59//
60#include "G4Event.hh"
61#include "Randomize.hh"
63
65 : multiple_vertex(false), flat_sampling(false), weight_change(1.)
66{
67 sourceVector.clear();
68 sourceIntensity.clear();
69 sourceProbability.clear();
70 currentSource = new G4SingleParticleSource();
71 sourceVector.push_back(currentSource);
72 sourceIntensity.push_back(1.);
73 currentSourceIdx = G4int(sourceVector.size() - 1);
74 theMessenger = new G4GeneralParticleSourceMessenger(this);
75 theMessenger->SetParticleGun(currentSource);
76 IntensityNormalization();
77}
78
80{
81 delete theMessenger;
82}
83
85{
86 currentSource = new G4SingleParticleSource();
87 theMessenger->SetParticleGun(currentSource);
88 sourceVector.push_back(currentSource);
89 sourceIntensity.push_back(aV);
90 currentSourceIdx = G4int(sourceVector.size() - 1);
91 IntensityNormalization();
92}
93
94void G4GeneralParticleSource::IntensityNormalization()
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;
137 currentSource = sourceVector[id];
138 theMessenger->SetParticleGun(currentSource);
139 //
140 } else {
141 G4cout << " source index is invalid " << G4endl;
142 G4cout << " it shall be <= " << sourceIntensity.size() << G4endl;
143 }
144}
145
147{
148 sourceIntensity[currentSourceIdx] = aV;
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 ) {
170 currentSource = sourceVector[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) {
187 if (!normalised) IntensityNormalization();
188 G4double rndm = G4UniformRand();
189 size_t i = 0 ;
190 if (!flat_sampling) {
191 while ( rndm > sourceProbability[i] ) i++;
192 (currentSource = sourceVector[i]);
193 } else {
194 i = size_t (sourceIntensity.size()*rndm);
195 currentSource = sourceVector[i];
196 }
197 }
198 currentSource-> GeneratePrimaryVertex(evt);
199 }
200 else {
201 for (size_t i = 0; i < sourceIntensity.size(); i++) {
202 sourceVector[i]->GeneratePrimaryVertex(evt);
203 }
204 }
205}
double G4double
Definition: G4Types.hh:64
int G4int
Definition: G4Types.hh:66
#define G4endl
Definition: G4ios.hh:52
G4DLLIMPORT std::ostream G4cout
#define G4UniformRand()
Definition: Randomize.hh:53
void SetParticleGun(G4SingleParticleSource *fpg)