4 // See the class description in the header file.
6 #include "AliPrimaryGeneratorAction.h"
7 #include "AliPrimaryGeneratorMessenger.h"
8 #include "AliTrackingAction.h"
9 #include "AliParticleGun.h"
10 #include "AliGunParticle.h"
11 #include "AliGlobals.h"
13 #include "AliGenerator.h"
18 #include <G4ParticleTable.hh>
19 #include <G4ParticleDefinition.hh>
21 #include <Randomize.hh>
23 #include <TParticle.h>
24 #include <TClonesArray.h>
26 AliPrimaryGeneratorAction::AliPrimaryGeneratorAction()
27 : fGenerator(kAliGenerator),
33 fParticleGun = new AliParticleGun();
34 fMessenger = new AliPrimaryGeneratorMessenger(this);
37 AliPrimaryGeneratorAction::AliPrimaryGeneratorAction(
38 const AliPrimaryGeneratorAction& right) {
40 AliGlobals::Exception(
41 "AliPrimaryGeneratorAction is protected from copying.");
44 AliPrimaryGeneratorAction::~AliPrimaryGeneratorAction() {
52 AliPrimaryGeneratorAction&
53 AliPrimaryGeneratorAction::operator=(const AliPrimaryGeneratorAction &right)
55 // check assignement to self
56 if (this == &right) return *this;
58 AliGlobals::Exception(
59 "AliPrimaryGeneratorAction is protected from assigning.");
66 void AliPrimaryGeneratorAction::ConstructGenerator()
68 // Constructs selected generator.
74 // gun is constructed interactively
77 ConstructGeantinoGenerator(false);
79 case kChargedGeantino:
80 ConstructGeantinoGenerator(true);
83 ConstructAliGenerator();
88 void AliPrimaryGeneratorAction::ConstructGeantinoGenerator(G4bool isCharged)
90 // Geantino with random momentum direction
91 // (the default generator).
95 fParticleGun->Reset();
97 G4ParticleTable* particleTable
98 = G4ParticleTable::GetParticleTable();
100 for (G4int i=0; i< fNofGunParticles; i++)
102 G4ParticleDefinition* particleDef = 0;
104 particleDef = particleTable->FindParticle("geantino");
106 particleDef = particleTable->FindParticle("chargedgeantino");
109 G4String text = "AliPrimaryGeneratorAction::GenerateGeantino:\n";
110 text = text + " G4ParticleTable::FindParticle() failed.";
111 AliGlobals::Exception(text);
115 RandFlat::shootArray(3,rn);
119 G4ThreeVector momentumDir(px, py, pz);
121 G4double energy = 1.*GeV;
122 G4ThreeVector position(0.,0.,0.);
124 G4ThreeVector polarization(0.,0.,0.);
126 AliGunParticle * gunParticle
127 = new AliGunParticle(particleDef, momentumDir, energy, position, time,
130 fParticleGun->AddParticle(gunParticle);
132 if (fVerboseLevel>1) {
133 G4cout << "Geantino generator has been built." << G4endl;
137 void AliPrimaryGeneratorAction::ConstructAliGenerator()
139 // Generator from AliRoot
140 // AliGenerator::Generate() fills the AliRun::fParticles array.
143 // check if AliGenerator is set
144 AliGenerator* generator = gAlice->Generator();
146 G4String text = "AliPrimaryGeneratorAction::ConstructGenerator:\n";
147 text = text + " No AliGenerator is defined in gAlice.";
148 AliGlobals::Exception(text);
150 // fill AliRun::fParticles array
151 generator->Generate();
152 fParticleArray = gAlice->Particles();
155 void AliPrimaryGeneratorAction::GenerateAliGeneratorPrimaries(G4Event* event)
157 // Creates a new G4PrimaryVertex objects for each TParticle
158 // in fParticles array.
161 G4PrimaryVertex* previousVertex = 0;
162 G4ThreeVector previousPosition = G4ThreeVector();
163 G4double previousTime = 0.;
165 G4int nofParticles = gAlice->GetNtrack();
167 //G4cout << " nofParticles: " << nofParticles << G4endl;
168 for( G4int i=0; i<nofParticles; i++ ) {
170 // get particle from TClonesArray
171 TObject* particleTObject
172 = fParticleArray->UncheckedAt(i);
174 = dynamic_cast<TParticle*>(particleTObject);
176 // check particle type
179 "AliPrimaryGeneratorAction::GenerateAliGeneratorPrimaries\n";
180 text = text + "Unknown particle type";
181 AliGlobals::Exception(text);
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);
192 G4String name = particle->GetName();
193 if (name == "Rootino")
194 particleDefinition = particleTable->FindParticle("geantino");
197 if (particleDefinition==0) {
198 G4cout << "pdgEncoding: " << pdgEncoding << G4endl;
200 "AliPrimaryGeneratorAction::GenerateAliGeneratorPrimaries:\n";
201 text = text + " G4ParticleTable::FindParticle() failed.";
202 AliGlobals::Exception(text);
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
216 // (vertex objects are destroyed in G4EventManager::ProcessOneEvent()
217 // when event is deleted)
218 vertex = new G4PrimaryVertex(position, time);
219 event->AddPrimaryVertex(vertex);
221 previousVertex = vertex;
222 previousPosition = position;
226 vertex = previousVertex;
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);
238 TVector3 polarization;
239 particle->GetPolarisation(polarization);
241 ->SetPolarization(polarization.X(), polarization.Y(), polarization.Z());
243 // add primary particle to the vertex
244 vertex->SetPrimary(primaryParticle);
250 void AliPrimaryGeneratorAction::GeneratePrimaries(G4Event* event)
252 // Generates primary particles by the selected generator.
256 gAlice->BeginEvent();
258 // fill particle gun/particle array
259 ConstructGenerator();
261 // generate primary vertices
262 if (fParticleArray) {
263 // use AliGenerator if set
264 GenerateAliGeneratorPrimaries(event);
266 // do not save primary particles
267 // (they would be stored twice)
268 AliTrackingAction* trackingAction
269 = AliTrackingAction::Instance();
270 trackingAction->SetSavePrimaries(false);
273 // use particle gun otherwise
274 fParticleGun->GeneratePrimaryVertex(event);
276 // primary particles have to be saved
277 AliTrackingAction* trackingAction
278 = AliTrackingAction::Instance();
279 trackingAction->SetSavePrimaries(true);
283 void AliPrimaryGeneratorAction::SetGenerator(AliPrimaryGenerator generator)
288 fGenerator = generator;
291 void AliPrimaryGeneratorAction::SetNofGunParticles(G4int nofParticles)
293 // Sets number of primary particles.
294 // This method is applied only to "gun" type generators
295 // (and not to AliGenerator from AliRoot).
298 fNofGunParticles = nofParticles;