Geant4  10.03.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4GeneralParticleSourceData.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 //
29 // GEANT4 Class file
30 //
31 //
32 // File name: G4GeneralParticleSourceData.cc
33 //
34 // Author: Andrew Green
35 //
36 // Creation date: 20 Mar 2014
37 //
38 // Modifications:
39 // 24/03/2014
40 // Fixed a bug whereby there was a data race for ownership of the "currentSource"
41 // member data. This has been resolved by returning a pointer to the G4SPS from
42 // the source vector. Tested up to 4 threads, and works fine; may need some further
43 // locking in the G4SPS code.
44 //
45 //
46 // Class Description:
47 // This class uses the singleton pattern to create a single copy of the data
48 // needed for the G4GPS class. As yet, only the largest parts have been split
49 // off.
50 //
51 //
52 
54 #include "G4Threading.hh"
55 #include "G4AutoLock.hh"
56 
57 
58 
59 
60 namespace
61 {
62  G4Mutex singMutex = G4MUTEX_INITIALIZER; //Protects singleton access
63 }
64 
65 //G4GeneralParticleSourceData* G4GeneralParticleSourceData::theInstance = 0;
66 
67 G4GeneralParticleSourceData::G4GeneralParticleSourceData() :
68  multiple_vertex(false) ,flat_sampling(false),
69  normalised(false),currentSourceIdx(0)
70 {
72 
73  sourceVector.clear();
74  sourceIntensity.clear();
75  sourceProbability.clear();
76 
77  currentSource = new G4SingleParticleSource();
78  sourceVector.push_back(currentSource);
79  sourceIntensity.push_back(1.);
80 
81 }
82 
83 G4GeneralParticleSourceData::~G4GeneralParticleSourceData()
84 {
86  for ( std::vector<G4SingleParticleSource*>::const_iterator it = sourceVector.begin() ;
87  it != sourceVector.end() ; ++it ) { delete *it; }
88  sourceVector.clear();
89 }
90 
91 
93 {
94  G4AutoLock lock(&singMutex);
96  return &instance;
97 }
98 
100 {
101  G4double total = 0.;
102  size_t i = 0 ;
103  for (i = 0; i < sourceIntensity.size(); i++)
104  {
105  total += sourceIntensity[i] ;
106  }
107  sourceProbability.clear();
108  std::vector <G4double> sourceNormalizedIntensity;
109  sourceNormalizedIntensity.clear();
110 
111  sourceNormalizedIntensity.push_back(sourceIntensity[0]/total);
112  sourceProbability.push_back(sourceNormalizedIntensity[0]);
113 
114  for ( i = 1 ; i < sourceIntensity.size(); i++)
115  {
116  sourceNormalizedIntensity.push_back(sourceIntensity[i]/total);
117  sourceProbability.push_back(sourceNormalizedIntensity[i] + sourceProbability[i-1]);
118  }
119 
120  // set source weights here based on sampling scheme (analog/flat) and intensities
121  for ( i = 0 ; i < sourceIntensity.size(); i++)
122  {
123  if (!flat_sampling)
124  {
126  }
127  else
128  {
129  this->GetCurrentSource(i)->GetBiasRndm()->SetIntensityWeight(sourceNormalizedIntensity[i]*sourceIntensity.size());
130  }
131  }
132 
133  normalised = true;
134 }
135 
137 {
138  sourceIntensity.at(currentSourceIdx) = intensity;
139  normalised = false;
140 }
141 
143 {
144  currentSource = new G4SingleParticleSource();
145  sourceVector.push_back(currentSource);
146  sourceIntensity.push_back(intensity);
147  currentSourceIdx = sourceVector.size() - 1;
148  normalised = false;
149 }
150 
152 {
153  delete sourceVector[idx];
154  sourceVector.erase(sourceVector.begin() + idx);
155  sourceIntensity.erase(sourceIntensity.begin()+idx);
156  normalised = false ;
157  if (currentSourceIdx == idx )
158  {
159  if ( this->GetIntensityVectorSize() > 0 )
160  {
161  currentSource = this->GetCurrentSource(0);
162  currentSourceIdx = 0;
163  }
164  else
165  {
166  currentSource = NULL;
167  currentSourceIdx = -1;
168  }
169  }
170 
171 }
172 
174 {
175  currentSourceIdx = -1;
176  currentSource = NULL;
177  for ( std::vector<G4SingleParticleSource*>::iterator it = sourceVector.begin();
178  it != sourceVector.end() ; ++it ) { delete *it; }
179  sourceVector.clear();
180  sourceIntensity.clear();
181  normalised = false;
182 }
183 
185 {
186  for ( std::vector<G4SingleParticleSource*>::iterator it = sourceVector.begin();
187  it != sourceVector.end() ; ++it ) {
188  (*it)->SetVerbosity(vl);
189 
190  }
191 
192 }
193 
195 {
196  currentSource = sourceVector[idx];
197  currentSourceIdx = idx;
198  return sourceVector[idx];
199 }
200 
202 {
203  G4MUTEXLOCK(&mutex);
204 }
205 
207 {
209 }
#define G4MUTEXUNLOCK
Definition: G4Threading.hh:180
#define G4MUTEXINIT(mutex)
Definition: G4Threading.hh:177
G4SingleParticleSource * GetCurrentSource() const
int G4int
Definition: G4Types.hh:78
#define G4MUTEX_INITIALIZER
Definition: G4Threading.hh:175
G4SPSRandomGenerator * GetBiasRndm() const
#define G4MUTEXLOCK
Definition: G4Threading.hh:179
G4int G4Mutex
Definition: G4Threading.hh:173
G4double total(Particle const *const p1, Particle const *const p2)
void SetIntensityWeight(G4double weight)
static MCTruthManager * instance
double G4double
Definition: G4Types.hh:76
#define G4MUTEXDESTROY(mutex)
Definition: G4Threading.hh:178
static G4GeneralParticleSourceData * Instance()