]> git.uio.no Git - u/mrichter/AliRoot.git/blob - AliGeant4/AliPrimaryGeneratorAction.cxx
corrected command guidance
[u/mrichter/AliRoot.git] / AliGeant4 / AliPrimaryGeneratorAction.cxx
1 // $Id$
2 // Category: run
3 //
4 // Author: I. Hrivnacova
5 //
6 // Class AliPrimaryGeneratorAction
7 // -------------------------------
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
19 #include "TG4G3Units.h"
20
21 #include <G4Event.hh>
22 #include <G4ParticleTable.hh>
23 #include <G4ParticleDefinition.hh>
24
25 #include <Randomize.hh>
26
27 #include <TParticle.h>
28
29 //_____________________________________________________________________________
30 AliPrimaryGeneratorAction::AliPrimaryGeneratorAction()
31   : fGenerator(kAliGenerator),
32     fNofGunParticles(1),
33     fVerboseLevel(0),
34     fMessenger(this),
35     fParticleGun() {
36 //
37 }
38
39 //_____________________________________________________________________________
40 AliPrimaryGeneratorAction::~AliPrimaryGeneratorAction() {
41 //
42 }
43
44 // private methods
45
46 //_____________________________________________________________________________
47 void AliPrimaryGeneratorAction::ConstructGenerator()
48 {
49 // Constructs selected generator.
50 // ---
51
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       
68 //_____________________________________________________________________________
69 void AliPrimaryGeneratorAction::ConstructGeantinoGenerator(G4bool isCharged)
70 {
71 // Geantino with random momentum direction
72 // (the default generator).
73 // ---
74
75   // reset gun
76   fParticleGun.Reset();     
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
111     fParticleGun.AddParticle(gunParticle);     
112   } 
113   if (fVerboseLevel>1) { 
114     G4cout << "Geantino generator has been built." << G4endl; 
115   }
116
117             
118 //_____________________________________________________________________________
119 void 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   }  
132   // fill AliRun::fParticleMap array 
133   generator->Generate();
134 }
135
136 //_____________________________________________________________________________
137 void 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
149   //G4cout << " nofParticles: " <<  nofParticles << G4endl;
150   for( G4int i=0; i<nofParticles; i++ ) {    
151   
152     // get the particle from AliRun stack
153     TParticle* particle = gAlice->Particle(i);
154
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       } 
172   
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());    
218     
219       // add primary particle to the vertex
220       vertex->SetPrimary(primaryParticle);
221     }   
222   }
223 }
224
225 // public methods
226
227 //_____________________________________________________________________________
228 void 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
240   if (fGenerator == kAliGenerator)  {
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();
248     if (trackingAction) trackingAction->SetSavePrimaries(false);
249   }  
250   else {
251     // use particle gun otherwise
252     fParticleGun.GeneratePrimaryVertex(event);
253
254     // primary particles have to be saved
255     AliTrackingAction* trackingAction
256       =  AliTrackingAction::Instance();
257     if (trackingAction) trackingAction->SetSavePrimaries(true);
258   }  
259 }
260
261 //_____________________________________________________________________________
262 void AliPrimaryGeneratorAction::SetGenerator(AliPrimaryGenerator generator)
263
264 // Sets generator.
265 // ---
266
267   fGenerator = generator; 
268 }
269
270 //_____________________________________________________________________________
271 void 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 }