source: trunk/examples/advanced/radiation_monitor/generator/src/RadmonPrimaryGeneratorAction.cc @ 1321

Last change on this file since 1321 was 807, checked in by garnier, 16 years ago

update

File size: 6.4 KB
Line 
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// File name:     RadmonPrimaryGeneratorAction.cc
28// Creation date: Oct 2005
29// Main author:   Riccardo Capra <capra@ge.infn.it>
30//
31// Id:            $Id: RadmonPrimaryGeneratorAction.cc,v 1.2.2.2 2006/06/29 16:16:41 gunter Exp $
32// Tag:           $Name: geant4-09-01-patch-02 $
33//
34
35// Include files
36#include "RadmonPrimaryGeneratorAction.hh"
37#include "RadmonVGeneratorLayout.hh"
38#include "RadmonVGeneratorsFactory.hh"
39#include "RadmonVGenerator.hh"
40#include "Randomize.hh"
41#include "G4Geantino.hh"
42
43#include "G4UnitsTable.hh"
44
45                                                RadmonPrimaryGeneratorAction :: RadmonPrimaryGeneratorAction(RadmonVGeneratorLayout * layout, RadmonVGeneratorsFactory * factory)
46:
47 particlesGun(1),
48 generatorLayout(layout),
49 generatorsFactory(factory),
50 needUpdate(true)
51{
52 if (layout==0)
53  G4Exception("RadmonPrimaryGeneratorAction::RadmonPrimaryGeneratorAction: layout==0.");
54
55 if (factory==0)
56  G4Exception("RadmonPrimaryGeneratorAction::RadmonPrimaryGeneratorAction: factory==0.");
57
58 layout->AttachObserver(this);
59}
60 
61 
62 
63                                                RadmonPrimaryGeneratorAction :: ~RadmonPrimaryGeneratorAction()
64{
65 generatorLayout->DetachObserver(this);
66 
67 CleanUp();
68
69 delete generatorsFactory;
70}
71
72
73
74
75
76void                                            RadmonPrimaryGeneratorAction :: GeneratePrimaries(G4Event * anEvent)
77{
78 if (needUpdate)
79 {
80  Update();
81  needUpdate=false;
82 }
83 
84 G4ThreeVector zero(0., 0. ,0.);
85 particlesGun.SetParticlePosition(zero);
86 particlesGun.SetParticleTime(0.);
87 particlesGun.SetParticleDefinition(G4Geantino::GeantinoDefinition());
88 particlesGun.SetParticleEnergy(0.*MeV);
89 particlesGun.SetParticleMomentumDirection(G4ParticleMomentum(0., 0., 1.));
90 particlesGun.SetParticlePolarization(zero);
91 particlesGun.SetParticleCharge(0.);
92 particlesGun.SetNumberOfParticles(1);
93 
94 const RadmonVGeneratorLayout * const layout(generatorLayout);
95
96 G4String sourceLabel;
97 G4int i(layout->GetNSources());
98 G4double total(0.);
99
100 while (i>0)
101 {
102  i--;
103 
104  sourceLabel=layout->GetSourceLabel(i);
105  total+=layout->GetRelativeSourceIntensity(sourceLabel);
106 }
107 
108 if (total<=0.)
109 {
110  particlesGun.GeneratePrimaryVertex(anEvent);
111  return;
112 }
113
114 total*=G4UniformRand();
115 
116 G4double intensity;
117 
118 i=layout->GetNSources();
119 while (i>0)
120 {
121  i--;
122 
123  sourceLabel=layout->GetSourceLabel(i);
124  intensity=layout->GetRelativeSourceIntensity(sourceLabel);
125  if (total < intensity)
126   break;
127 
128  total-=intensity;
129 }
130 
131 const G4int n(layout->GetNSourceAlgorithms(sourceLabel));
132 
133 for (i=0; i<n; i++)
134 {
135  G4String algorithmLabel(layout->GetSourceAlgorithmLabel(sourceLabel, i));
136
137  GeneratorsMap::iterator j(generatorsMap.find(MakeKey(sourceLabel, algorithmLabel)));
138 
139  if (j!=generatorsMap.end())
140   if (j->second)
141   {
142    j->second->ConvolveParticleGun(particlesGun);
143   }
144 }
145
146// G4cout << G4BestUnit(particlesGun.GetParticlePosition(), "Length") << ", " << particlesGun.GetParticleDefinition()->GetParticleName() << ", " << G4BestUnit(particlesGun.GetParticleMomentumDirection()*particlesGun.GetParticleEnergy(), "Energy") << G4endl;
147
148 particlesGun.GeneratePrimaryVertex(anEvent);
149}
150
151
152
153
154
155void                                            RadmonPrimaryGeneratorAction :: OnLayoutChange()
156{
157 needUpdate=true; 
158}
159
160
161
162
163
164inline void                                     RadmonPrimaryGeneratorAction :: Update(void)
165{
166 CleanUp();
167 generatorsMap.clear();
168 
169 const RadmonVGeneratorLayout * const layout(generatorLayout);
170 
171 G4int i(layout->GetNSources());
172 
173 while (i>0)
174 {
175  i--;
176 
177  G4String sourceLabel(layout->GetSourceLabel(i));
178 
179  G4int j(layout->GetNSourceAlgorithms(sourceLabel));
180 
181  while (j>0)
182  {
183   j--;
184   
185   G4String algorithmLabel(layout->GetSourceAlgorithmLabel(sourceLabel, j));
186   
187   RadmonVGenerator * generator(generatorsFactory->GetGenerator(layout->GetSourceAlgorithmType(sourceLabel, algorithmLabel)));
188   
189   if (generator)
190   {
191    generatorsMap[MakeKey(sourceLabel, algorithmLabel)]=generator;
192
193    G4int k(layout->GetSourceAlgorithmNAttributes(sourceLabel, algorithmLabel));
194   
195    while (k>0)
196    {
197     k--;
198     
199     G4String attribute(layout->GetSourceAlgorithmAttributeName(sourceLabel, algorithmLabel, k));
200     G4String value(layout->GetSourceAlgorithmAttribute(sourceLabel, algorithmLabel, attribute));
201   
202     generator->SetGeneratorAttribute(attribute, value);
203    }
204   }
205  }
206 }
207}
208
209
210
211inline void                                     RadmonPrimaryGeneratorAction :: CleanUp(void)
212{
213 GeneratorsMap::iterator i(generatorsMap.begin());
214 const GeneratorsMap::iterator end(generatorsMap.end());
215 
216 while (i!=end)
217 {
218  delete i->second;
219  i++;
220 }
221}
222
223
224
225inline G4String                                 RadmonPrimaryGeneratorAction :: MakeKey(const G4String & source, const G4String & algorithm)
226{
227 G4String key(source);
228 key+="|==|";
229 key+=algorithm;
230
231 return key;     
232}
Note: See TracBrowser for help on using the repository browser.