]> git.uio.no Git - u/mrichter/AliRoot.git/blame - AliGeant4/AliPrimaryGeneratorAction.cxx
two levels of verbose introduced; AliHeader.h include added
[u/mrichter/AliRoot.git] / AliGeant4 / AliPrimaryGeneratorAction.cxx
CommitLineData
676fb573 1// $Id$
2// Category: run
3//
4// See the class description in the header file.
5
6#include "AliPrimaryGeneratorAction.h"
7#include "AliPrimaryGeneratorMessenger.h"
8#include "AliTrackingAction.h"
9#include "AliParticleGun.h"
10#include "AliGunParticle.h"
11#include "AliGlobals.h"
12#include "AliRun.h"
13#include "AliGenerator.h"
14
b7d1552b 15#include "TG4G3Units.h"
676fb573 16
17#include <G4Event.hh>
18#include <G4ParticleTable.hh>
19#include <G4ParticleDefinition.hh>
20
21#include <Randomize.hh>
22
23#include <TParticle.h>
24
25AliPrimaryGeneratorAction::AliPrimaryGeneratorAction()
26 : fGenerator(kAliGenerator),
27 fNofGunParticles(1),
87ec4f69 28 fVerboseLevel(0)
676fb573 29{
30//
31 fParticleGun = new AliParticleGun();
32 fMessenger = new AliPrimaryGeneratorMessenger(this);
33}
34
35AliPrimaryGeneratorAction::AliPrimaryGeneratorAction(
36 const AliPrimaryGeneratorAction& right) {
37//
38 AliGlobals::Exception(
39 "AliPrimaryGeneratorAction is protected from copying.");
40}
41
42AliPrimaryGeneratorAction::~AliPrimaryGeneratorAction() {
43//
44 delete fMessenger;
45 delete fParticleGun;
46}
47
48// operators
49
50AliPrimaryGeneratorAction&
51AliPrimaryGeneratorAction::operator=(const AliPrimaryGeneratorAction &right)
52{
53 // check assignement to self
54 if (this == &right) return *this;
55
56 AliGlobals::Exception(
57 "AliPrimaryGeneratorAction is protected from assigning.");
58
59 return *this;
60}
61
62// private methods
63
64void AliPrimaryGeneratorAction::ConstructGenerator()
65{
66// Constructs selected generator.
67// ---
68
676fb573 69 switch (fGenerator) {
70 case kGun:
71 // gun is constructed interactively
72 return;
73 case kGeantino:
74 ConstructGeantinoGenerator(false);
75 return;
76 case kChargedGeantino:
77 ConstructGeantinoGenerator(true);
78 return;
79 case kAliGenerator:
80 ConstructAliGenerator();
81 return;
82 }
83}
84
85void AliPrimaryGeneratorAction::ConstructGeantinoGenerator(G4bool isCharged)
86{
87// Geantino with random momentum direction
88// (the default generator).
89// ---
90
91 // reset gun
92 fParticleGun->Reset();
93
94 G4ParticleTable* particleTable
95 = G4ParticleTable::GetParticleTable();
96
97 for (G4int i=0; i< fNofGunParticles; i++)
98 {
99 G4ParticleDefinition* particleDef = 0;
100 if (!isCharged)
101 particleDef = particleTable->FindParticle("geantino");
102 else
103 particleDef = particleTable->FindParticle("chargedgeantino");
104
105 if (!particleDef) {
106 G4String text = "AliPrimaryGeneratorAction::GenerateGeantino:\n";
107 text = text + " G4ParticleTable::FindParticle() failed.";
108 AliGlobals::Exception(text);
109 }
110
111 G4double rn[3];
112 RandFlat::shootArray(3,rn);
113 G4double px=rn[0];
114 G4double py=rn[1];
115 G4double pz=rn[2];
116 G4ThreeVector momentumDir(px, py, pz);
117
118 G4double energy = 1.*GeV;
119 G4ThreeVector position(0.,0.,0.);
120 G4double time = 0.;
121 G4ThreeVector polarization(0.,0.,0.);
122
123 AliGunParticle * gunParticle
124 = new AliGunParticle(particleDef, momentumDir, energy, position, time,
125 polarization);
126
127 fParticleGun->AddParticle(gunParticle);
128 }
129 if (fVerboseLevel>1) {
5f1d09c5 130 G4cout << "Geantino generator has been built." << G4endl;
676fb573 131 }
132}
133
134void AliPrimaryGeneratorAction::ConstructAliGenerator()
135{
136// Generator from AliRoot
137// AliGenerator::Generate() fills the AliRun::fParticles array.
138// ---
139
140 // check if AliGenerator is set
141 AliGenerator* generator = gAlice->Generator();
142 if (!generator) {
143 G4String text = "AliPrimaryGeneratorAction::ConstructGenerator:\n";
144 text = text + " No AliGenerator is defined in gAlice.";
145 AliGlobals::Exception(text);
146 }
87ec4f69 147 // fill AliRun::fParticleMap array
676fb573 148 generator->Generate();
676fb573 149}
150
151void AliPrimaryGeneratorAction::GenerateAliGeneratorPrimaries(G4Event* event)
152{
153// Creates a new G4PrimaryVertex objects for each TParticle
154// in fParticles array.
155// ---
156
157 G4PrimaryVertex* previousVertex = 0;
158 G4ThreeVector previousPosition = G4ThreeVector();
159 G4double previousTime = 0.;
160
161 G4int nofParticles = gAlice->GetNtrack();
162 // add verbose
5f1d09c5 163 //G4cout << " nofParticles: " << nofParticles << G4endl;
676fb573 164 for( G4int i=0; i<nofParticles; i++ ) {
165
87ec4f69 166 // get the particle from AliRun stack
167 TParticle* particle = gAlice->Particle(i);
168
b7d1552b 169
170 if (!particle->TestBit(kDoneBit)) {
171 // only particles that didn't die (decay) in primary generator
172 // will be transformed to G4 objects
173
174 // get particle definition from G4ParticleTable
175 G4int pdgEncoding = particle->GetPdgCode();
176 G4ParticleTable* particleTable
177 = G4ParticleTable::GetParticleTable();
178 G4ParticleDefinition* particleDefinition = 0;
179 if (pdgEncoding != 0)
180 particleDefinition = particleTable->FindParticle(pdgEncoding);
181 else {
182 G4String name = particle->GetName();
183 if (name == "Rootino")
184 particleDefinition = particleTable->FindParticle("geantino");
185 }
676fb573 186
b7d1552b 187 if (particleDefinition==0) {
188 G4cout << "pdgEncoding: " << pdgEncoding << G4endl;
189 G4String text =
190 "AliPrimaryGeneratorAction::GenerateAliGeneratorPrimaries:\n";
191 text = text + " G4ParticleTable::FindParticle() failed.";
192 AliGlobals::Exception(text);
193 }
194
195 // get/create vertex
196 G4ThreeVector position
197 = G4ThreeVector(particle->Vx()*TG4G3Units::Length(),
198 particle->Vy()*TG4G3Units::Length(),
199 particle->Vz()*TG4G3Units::Length());
200 G4double time = particle->T()*TG4G3Units::Time();
201 G4PrimaryVertex* vertex;
202 if ( i==0 || position != previousPosition || time != previousTime ) {
203 // create a new vertex
204 // in case position and time of gun particle are different from
205 // previous values
206 // (vertex objects are destroyed in G4EventManager::ProcessOneEvent()
207 // when event is deleted)
208 vertex = new G4PrimaryVertex(position, time);
209 event->AddPrimaryVertex(vertex);
210
211 previousVertex = vertex;
212 previousPosition = position;
213 previousTime = time;
214 }
215 else
216 vertex = previousVertex;
217
218 // create a primary particle and add it to the vertex
219 // (primaryParticle objects are destroyed in G4EventManager::ProcessOneEvent()
220 // when event and then vertex is deleted)
221 G4double px = particle->Px()*TG4G3Units::Energy();
222 G4double py = particle->Py()*TG4G3Units::Energy();
223 G4double pz = particle->Pz()*TG4G3Units::Energy();
224 G4PrimaryParticle* primaryParticle
225 = new G4PrimaryParticle(particleDefinition, px, py, pz);
226
227 // set polarization
228 TVector3 polarization;
229 particle->GetPolarisation(polarization);
230 primaryParticle
231 ->SetPolarization(polarization.X(), polarization.Y(), polarization.Z());
676fb573 232
b7d1552b 233 // add primary particle to the vertex
234 vertex->SetPrimary(primaryParticle);
235 }
676fb573 236 }
237}
238
239// public methods
240
241void AliPrimaryGeneratorAction::GeneratePrimaries(G4Event* event)
242{
243// Generates primary particles by the selected generator.
244// ---
245
246 // reset AliRun
247 gAlice->BeginEvent();
248
249 // fill particle gun/particle array
250 ConstructGenerator();
251
252 // generate primary vertices
87ec4f69 253 if (fGenerator == kAliGenerator) {
676fb573 254 // use AliGenerator if set
255 GenerateAliGeneratorPrimaries(event);
256
257 // do not save primary particles
258 // (they would be stored twice)
259 AliTrackingAction* trackingAction
260 = AliTrackingAction::Instance();
261 trackingAction->SetSavePrimaries(false);
262 }
263 else {
264 // use particle gun otherwise
265 fParticleGun->GeneratePrimaryVertex(event);
266
267 // primary particles have to be saved
268 AliTrackingAction* trackingAction
269 = AliTrackingAction::Instance();
270 trackingAction->SetSavePrimaries(true);
271 }
272}
273
274void AliPrimaryGeneratorAction::SetGenerator(AliPrimaryGenerator generator)
275{
276// Sets generator.
277// ---
278
279 fGenerator = generator;
280}
281
282void AliPrimaryGeneratorAction::SetNofGunParticles(G4int nofParticles)
283{
284// Sets number of primary particles.
285// This method is applied only to "gun" type generators
286// (and not to AliGenerator from AliRoot).
287// ---
288
289 fNofGunParticles = nofParticles;
290}
291
292
293
294
295
296