Geant4  10.01.p01
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() : flat_sampling(false), normalised(false),currentSourceIdx(0)
68 {
70 
71  sourceVector.clear();
72  sourceIntensity.clear();
73  sourceProbability.clear();
74 
76  sourceVector.push_back(currentSource);
77  sourceIntensity.push_back(1.);
78 
79 }
80 
81 
83 {
85  for ( std::vector<G4SingleParticleSource*>::const_iterator it = sourceVector.begin() ;
86  it != sourceVector.end() ; ++it ) { delete *it; }
87  sourceVector.clear();
88 }
89 
90 
92 {
93  G4AutoLock lock(&singMutex);
95  return &instance;
96  // if(theInstance == NULL)
97  // {
98  // theInstance = new G4GeneralParticleSourceData();
99  // }
100  // return theInstance;
101 }
102 
104 {
105  G4double total = 0.;
106  size_t i = 0 ;
107  for (i = 0; i < sourceIntensity.size(); i++)
108  {
109  total += sourceIntensity[i] ;
110  }
111  sourceProbability.clear();
112  std::vector <G4double> sourceNormalizedIntensity;
113  sourceNormalizedIntensity.clear();
114 
115  sourceNormalizedIntensity.push_back(sourceIntensity[0]/total);
116  sourceProbability.push_back(sourceNormalizedIntensity[0]);
117 
118  for ( i = 1 ; i < sourceIntensity.size(); i++)
119  {
120  sourceNormalizedIntensity.push_back(sourceIntensity[i]/total);
121  sourceProbability.push_back(sourceNormalizedIntensity[i] + sourceProbability[i-1]);
122  }
123 
124  // set source weights here based on sampling scheme (analog/flat) and intensities
125  for ( i = 0 ; i < sourceIntensity.size(); i++)
126  {
127  if (!flat_sampling)
128  {
130  }
131  else
132  {
133  this->GetCurrentSource(i)->GetBiasRndm()->SetIntensityWeight(sourceNormalizedIntensity[i]*sourceIntensity.size());
134  }
135  }
136 
137  normalised = true;
138 }
139 
141 {
142  sourceIntensity.at(currentSourceIdx) = intensity;
143  normalised = false;
144 }
145 
147 {
149  sourceVector.push_back(currentSource);
150  sourceIntensity.push_back(intensity);
151  currentSourceIdx = sourceVector.size() - 1;
152  normalised = false;
153 }
154 
156 {
157  delete sourceVector[idx];
158  sourceVector.erase(sourceVector.begin() + idx);
159  sourceIntensity.erase(sourceIntensity.begin()+idx);
160  normalised = false ;
161  if (currentSourceIdx == idx )
162  {
163  if ( this->GetIntensityVectorSize() > 0 )
164  {
165  currentSource = this->GetCurrentSource(0);
166  currentSourceIdx = 0;
167  }
168  else
169  {
170  currentSource = NULL;
171  currentSourceIdx = -1;
172  }
173  }
174 
175 }
176 
178 {
179  currentSourceIdx = -1;
180  currentSource = NULL;
181  sourceVector.clear();
182  sourceIntensity.clear();
183 }
184 
185 
187 {
189  currentSourceIdx = idx;
190  return sourceVector[idx];
191 }
192 
194 {
195  G4MUTEXLOCK(&mutex);
196 }
197 
199 {
201 }
#define G4MUTEXUNLOCK
Definition: G4Threading.hh:176
#define G4MUTEXINIT(mutex)
Definition: G4Threading.hh:173
Andrea Dotti Feb 2015 Important: This is a shared class between threads.
G4SingleParticleSource * GetCurrentSource() const
int G4int
Definition: G4Types.hh:78
#define G4MUTEX_INITIALIZER
Definition: G4Threading.hh:171
G4SPSRandomGenerator * GetBiasRndm() const
#define G4MUTEXLOCK
Definition: G4Threading.hh:175
G4int G4Mutex
Definition: G4Threading.hh:169
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:174
std::vector< G4SingleParticleSource * > sourceVector
static G4GeneralParticleSourceData * Instance()