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