Geant4  10.02.p02
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 #include "G4UnitsTable.hh"
68 
70 
71 #include "G4Threading.hh"
72 #include "G4AutoLock.hh"
73 
74 namespace {
75  G4Mutex messangerInit = G4MUTEX_INITIALIZER;
76 }
77 
79  theMessenger(0)
80 {
82  //currentSource = GPSData->GetCurrentSource();
83  //currentSourceIdx = G4int(GPSData->GetSourceVectorSize() - 1);
84 
85  //Messenger is special, only a worker should instantiate it. Singleton pattern
87  //Some initialization should be done only once
88  G4AutoLock l(&messangerInit);
89  static G4bool onlyOnce = false;
90  if ( !onlyOnce ) {
93  onlyOnce = true;
94  }
95 }
96 
98 {
100 }
101 
103 {
104  GPSData->Lock();
105  GPSData->AddASource(aV);
107  //TODO: But do we really normalize here after each source?
109  GPSData->Unlock();
110 }
111 
113 {
116 }
117 
119 {
120  G4cout << "The number of particle sources is: " << GPSData->GetIntensityVectorSize() << G4endl;
121  G4cout <<" Multiple Vertex sources: "<<GPSData->GetMultipleVertex();
122  G4cout <<" Flat Sampling flag: "<<GPSData->GetFlatSampling()<<G4endl;
123  const G4int currentIdx = GPSData->GetCurrentSourceIdx();
124  for(G4int i=0; i<GPSData->GetIntensityVectorSize(); i++)
125  {
126  G4cout << "\tsource " << i << " with intensity: " << GPSData->GetIntensity(i) << G4endl;
127  const G4SingleParticleSource* thisSrc = GPSData->GetCurrentSource(i);
128  G4cout <<" \t\tNum Particles: "<<thisSrc->GetNumberOfParticles()<<"; Particle type: "<<thisSrc->GetParticleDefinition()->GetParticleName()<<G4endl;
129  G4cout <<" \t\tEnergy: "<<G4BestUnit(thisSrc->GetParticleEnergy(),"Energy")<<G4endl;
130  G4cout <<" \t\tDirection: "<<thisSrc->GetAngDist()->GetDirection()<<"; Position: ";
131  G4cout <<G4BestUnit(thisSrc->GetPosDist()->GetCentreCoords(),"Length")<<G4endl;
132  G4cout <<" \t\tAngular Distribution: "<<thisSrc->GetAngDist()->GetDistType()<<G4endl;
133  G4cout <<" \t\tEnergy Distribution: "<<thisSrc->GetEneDist()->GetEnergyDisType()<<G4endl;
134  G4cout <<" \t\tPosition Distribution Type: "<<thisSrc->GetPosDist()->GetPosDisType();
135  G4cout <<"; Position Shape: "<<thisSrc->GetPosDist()->GetPosDisShape()<<G4endl;
136  }
137  //Set back previous source
138  GPSData->GetCurrentSource(currentIdx);
139 }
140 
142 {
143  G4int id = aV;
144  if ( id < GPSData->GetIntensityVectorSize() )
145  {
146  //currentSourceIdx = aV;
147  //currentSource = GPSData->GetCurrentSource(id);
149  }
150  else
151  {
153  msg<<"Trying to set source to index "<<aV<<" but only "<<GPSData->GetIntensityVectorSize()<<" sources are defined.";
154  G4Exception("G4GeneralParticleSoruce::SetCurrentSourceto","G4GPS004",FatalException,msg);
155  }
156 }
157 
159 {
160  GPSData->Lock();
162  GPSData->Unlock();
164 }
165 
167 {
170 }
171 
173 {
174  G4int id = aV;
175  if ( id <= GPSData->GetIntensityVectorSize() )
176  {
177  GPSData->DeleteASource(aV);
179  }
180  else
181  {
182  G4cout << " source index is invalid " << G4endl;
183  G4cout << " it shall be <= " << GPSData->GetIntensityVectorSize() << G4endl;
184  }
185 }
186 
188 {
189  if (!GPSData->GetMultipleVertex())
190  {
191  G4SingleParticleSource* currentSource = GPSData->GetCurrentSource();
192  if (GPSData->GetIntensityVectorSize() > 1)
193  {
194  //Try to minimize locks
195  if (! normalised ) {
196  //According to local variable, normalization is needed
197  //Check with underlying shared resource, another
198  //thread could have already normalized this
199  GPSData->Lock();
200  G4bool norm = GPSData->Normalised();
201  if (!norm) {
203  }
204  //This takes care of the case in which the local variable
205  //is False and the underlying resource is true.
207  GPSData->Unlock();
208  }
209  G4double rndm = G4UniformRand();
210  size_t i = 0 ;
211  if (! GPSData->GetFlatSampling() )
212  {
213  while ( rndm > GPSData->GetSourceProbability(i) ) i++;
214  currentSource = GPSData->GetCurrentSource(i);
215  }
216  else
217  {
218  i = size_t (GPSData->GetIntensityVectorSize()*rndm);
219  currentSource = GPSData->GetCurrentSource(i);
220  }
221  }
222  currentSource->GeneratePrimaryVertex(evt);
223  }
224  else
225  {
226  for (G4int i = 0; i < GPSData->GetIntensityVectorSize(); i++)
227  {
229  }
230  }
231 }
static G4GeneralParticleSourceMessenger * GetInstance(G4GeneralParticleSource *)
G4SPSAngDistribution * GetAngDist() const
G4SPSEneDistribution * GetEneDist() const
G4GeneralParticleSourceData * GPSData
G4SPSPosDistribution * GetPosDist() const
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
G4String GetPosDisShape() const
Andrea Dotti Feb 2015 Important: This is a shared class between threads.
#define G4BestUnit(a, b)
#define G4_USE_G4BESTUNIT_FOR_VERBOSE 1
int G4int
Definition: G4Types.hh:78
#define G4MUTEX_INITIALIZER
Definition: G4Threading.hh:175
const G4String & GetParticleName() const
#define G4UniformRand()
Definition: Randomize.hh:97
G4GLOB_DLL std::ostream G4cout
G4ThreeVector GetCentreCoords() const
G4double GetSourceProbability(G4int idx) const
bool G4bool
Definition: G4Types.hh:79
G4double GetIntensity(G4int idx) const
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
void SetParticleGun(G4SingleParticleSource *fpg)
G4int G4Mutex
Definition: G4Threading.hh:173
G4SingleParticleSource * GetCurrentSource(G4int idx)
G4String GetPosDisType() const
#define G4endl
Definition: G4ios.hh:61
G4double GetParticleEnergy() const
void GeneratePrimaryVertex(G4Event *evt)
double G4double
Definition: G4Types.hh:76
G4GeneralParticleSourceMessenger * theMessenger
G4ParticleDefinition * GetParticleDefinition() const
static G4GeneralParticleSourceData * Instance()