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