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