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