]> git.uio.no Git - u/mrichter/AliRoot.git/blob - AliGeant4/AliPrimaryGeneratorAction.cxx
Initial version
[u/mrichter/AliRoot.git] / AliGeant4 / AliPrimaryGeneratorAction.cxx
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
15 #include "TG3Units.h"
16
17 #include <G4Event.hh>
18 #include <G4ParticleTable.hh>
19 #include <G4ParticleDefinition.hh>
20
21 #include <Randomize.hh>
22
23 #include <TParticle.h>
24
25 AliPrimaryGeneratorAction::AliPrimaryGeneratorAction()
26   : fGenerator(kAliGenerator),
27     fNofGunParticles(1),
28     fVerboseLevel(0)
29 {
30 //
31   fParticleGun = new AliParticleGun();
32   fMessenger = new AliPrimaryGeneratorMessenger(this);
33 }
34
35 AliPrimaryGeneratorAction::AliPrimaryGeneratorAction(
36                                     const AliPrimaryGeneratorAction& right) {
37 //                                  
38   AliGlobals::Exception(
39     "AliPrimaryGeneratorAction is protected from copying.");
40 }
41
42 AliPrimaryGeneratorAction::~AliPrimaryGeneratorAction() {
43 //
44   delete fMessenger;
45   delete fParticleGun;
46 }
47
48 // operators
49
50 AliPrimaryGeneratorAction& 
51 AliPrimaryGeneratorAction::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
64 void AliPrimaryGeneratorAction::ConstructGenerator()
65 {
66 // Constructs selected generator.
67 // ---
68
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       
85 void 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) { 
130     G4cout << "Geantino generator has been built." << G4endl; 
131   }
132
133             
134 void 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   }  
147   // fill AliRun::fParticleMap array 
148   generator->Generate();
149 }
150
151 void 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
163   //G4cout << " nofParticles: " <<  nofParticles << G4endl;
164   for( G4int i=0; i<nofParticles; i++ ) {    
165   
166     // get the particle from AliRun stack
167     TParticle* particle = gAlice->Particle(i);
168
169     // get particle definition from G4ParticleTable
170     G4int pdgEncoding = particle->GetPdgCode();
171     G4ParticleTable* particleTable 
172       = G4ParticleTable::GetParticleTable();                
173     G4ParticleDefinition* particleDefinition = 0;    
174     if (pdgEncoding != 0) 
175       particleDefinition = particleTable->FindParticle(pdgEncoding);
176     else {
177       G4String name = particle->GetName();
178       if (name == "Rootino")    
179         particleDefinition = particleTable->FindParticle("geantino");
180     }   
181   
182     if (particleDefinition==0) {
183       G4cout << "pdgEncoding: " << pdgEncoding << G4endl;
184       G4String text = 
185         "AliPrimaryGeneratorAction::GenerateAliGeneratorPrimaries:\n";
186       text = text + "   G4ParticleTable::FindParticle() failed.";
187       AliGlobals::Exception(text);
188     }   
189
190     // get/create vertex
191     G4ThreeVector position 
192       = G4ThreeVector(particle->Vx()*TG3Units::Length(),
193                       particle->Vy()*TG3Units::Length(),
194                       particle->Vz()*TG3Units::Length());
195     G4double time = particle->T()*TG3Units::Time(); 
196     G4PrimaryVertex* vertex;
197     if ( i==0 || position != previousPosition || time != previousTime ) {   
198       // create a new vertex 
199       // in case position and time of gun particle are different from 
200       // previous values
201       // (vertex objects are destroyed in G4EventManager::ProcessOneEvent()
202       // when event is deleted)  
203       vertex = new G4PrimaryVertex(position, time);
204       event->AddPrimaryVertex(vertex);
205
206       previousVertex = vertex;
207       previousPosition = position;
208       previousTime = time;
209     }
210     else 
211       vertex = previousVertex;
212
213     // create a primary particle and add it to the vertex
214     // (primaryParticle objects are destroyed in G4EventManager::ProcessOneEvent()
215     // when event and then vertex is deleted)
216     G4double px = particle->Px()*TG3Units::Energy();
217     G4double py = particle->Py()*TG3Units::Energy();
218     G4double pz = particle->Pz()*TG3Units::Energy();
219     G4PrimaryParticle* primaryParticle 
220       = new G4PrimaryParticle(particleDefinition, px, py, pz);
221       
222     // set polarization
223     TVector3 polarization;
224     particle->GetPolarisation(polarization);
225     primaryParticle
226       ->SetPolarization(polarization.X(), polarization.Y(), polarization.Z());    
227     
228     // add primary particle to the vertex
229     vertex->SetPrimary(primaryParticle);
230   }
231 }
232
233 // public methods
234
235 void AliPrimaryGeneratorAction::GeneratePrimaries(G4Event* event)
236 {
237 // Generates primary particles by the selected generator.
238 // ---
239
240   // reset AliRun
241   gAlice->BeginEvent();
242
243   // fill particle gun/particle array
244   ConstructGenerator();
245   
246   // generate primary vertices
247   if (fGenerator == kAliGenerator)  {
248     // use AliGenerator if set
249     GenerateAliGeneratorPrimaries(event);
250
251     // do not save primary particles
252     // (they would be stored twice)
253     AliTrackingAction* trackingAction
254       =  AliTrackingAction::Instance();
255     trackingAction->SetSavePrimaries(false);
256   }  
257   else {
258     // use particle gun otherwise
259     fParticleGun->GeneratePrimaryVertex(event);
260
261     // primary particles have to be saved
262     AliTrackingAction* trackingAction
263       =  AliTrackingAction::Instance();
264     trackingAction->SetSavePrimaries(true);
265   }  
266 }
267
268 void AliPrimaryGeneratorAction::SetGenerator(AliPrimaryGenerator generator)
269
270 // Sets generator.
271 // ---
272
273   fGenerator = generator; 
274 }
275
276 void AliPrimaryGeneratorAction::SetNofGunParticles(G4int nofParticles)
277
278 // Sets number of primary particles.
279 // This method is applied only to "gun" type generators
280 // (and not to AliGenerator from AliRoot).
281 // ---
282
283   fNofGunParticles = nofParticles;
284 }
285
286
287
288
289
290