Geant4  10.01
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 // 25/03/2014, Andrew Green
58 // Various changes to use the new G4GeneralParticleSourceData class, mostly
59 // just transparent wrappers around the thread safe object.
60 //
62 //
63 #include "G4Event.hh"
64 #include "Randomize.hh"
67 
69 
70 #include "G4Threading.hh"
71 #include "G4AutoLock.hh"
72 
73 namespace {
74  G4Mutex messangerInit = G4MUTEX_INITIALIZER;
75 }
76 
77 G4GeneralParticleSource::G4GeneralParticleSource() : multiple_vertex(false), flat_sampling(false),normalised(false),
78  theMessenger(0)
79 {
83 
84  //Messenger is special, only a worker should instantiate it. Singleton pattern
86  //Some initialization should be done only once
87  G4AutoLock l(&messangerInit);
88  static G4bool onlyOnce = false;
89  if ( !onlyOnce ) {
90  theMessenger->SetParticleGun(currentSource);
92  onlyOnce = true;
93  }
94 
95 }
96 
98 {
100 }
101 
103 {
104  normalised=false;
105  GPSData->Lock();
106  GPSData->AddASource(aV);
111  GPSData->Unlock();
112 }
113 
115 {
117  normalised=true;
118 }
119 
121 {
122  G4cout << "The number of particle sources is: " << GPSData->GetIntensityVectorSize() << G4endl;
123  for(G4int i=0; i<GPSData->GetIntensityVectorSize(); i++)
124  {
125  G4cout << "\tsource " << i << " intensity is: " << GPSData->GetIntensity(i) << G4endl;;
126  }
127 }
128 
130 {
131  G4int id = aV;
132  if ( id <= GPSData->GetIntensityVectorSize() )
133  {
134  currentSourceIdx = aV;
137  }
138  else
139  {
140  G4cout << " source index is invalid " << G4endl;
141  G4cout << " it shall be <= " << GPSData->GetIntensityVectorSize() << G4endl;
142  }
143 }
144 
146 {
147  GPSData->Lock();
149  GPSData->Unlock();
150  normalised = false;
151 }
152 
154 {
155  currentSourceIdx = -1;
156  currentSource = 0;
158  normalised=false;
159 }
160 
162 {
163  G4int id = aV;
164  if ( id <= GPSData->GetIntensityVectorSize() )
165  {
166  GPSData->DeleteASource(aV);
167  normalised=false;
168  }
169  else
170  {
171  G4cout << " source index is invalid " << G4endl;
172  G4cout << " it shall be <= " << GPSData->GetIntensityVectorSize() << G4endl;
173  }
174 }
175 
177 {
178  if (!multiple_vertex)
179  {
180  if (GPSData->GetIntensityVectorSize() > 1)
181  {
182  //Try to minimize locks
183  if (! normalised ) {
184  //According to local variable, normalization is needed
185  //Check with underlying (shared resource), another
186  //thread could have already normalized this
187  GPSData->Lock();
188  G4bool norm = GPSData->Normalised();
189  if (!norm) {
191  }
192  //This takes care of the case in which the local variable
193  //is False and the underlying source is.
195  GPSData->Unlock();
196  }
197  G4double rndm = G4UniformRand();
198  size_t i = 0 ;
199  if (!flat_sampling)
200  {
201  while ( rndm > GPSData->GetSourceProbability(i) ) i++;
203  }
204  else
205  {
206  i = size_t (GPSData->GetIntensityVectorSize()*rndm);
208  }
209  }
211  }
212  else
213  {
214  for (G4int i = 0; i < GPSData->GetIntensityVectorSize(); i++)
215  {
217  }
218  }
219 }
static G4GeneralParticleSourceMessenger * GetInstance(G4GeneralParticleSource *)
G4GeneralParticleSourceData * GPSData
G4SingleParticleSource * currentSource
int G4int
Definition: G4Types.hh:78
#define G4MUTEX_INITIALIZER
Definition: G4Threading.hh:163
#define G4UniformRand()
Definition: Randomize.hh:95
G4GLOB_DLL std::ostream G4cout
G4double GetSourceProbability(G4int idx) const
bool G4bool
Definition: G4Types.hh:79
G4double GetIntensity(G4int idx) const
void SetParticleGun(G4SingleParticleSource *fpg)
G4int G4Mutex
Definition: G4Threading.hh:161
G4SingleParticleSource * GetCurrentSource(G4int idx)
#define G4endl
Definition: G4ios.hh:61
void GeneratePrimaryVertex(G4Event *evt)
double G4double
Definition: G4Types.hh:76
G4GeneralParticleSourceMessenger * theMessenger
static G4GeneralParticleSourceData * Instance()