4 // See the class description in the header file.
6 #include "AliTrackingAction.h"
7 #include "AliTrackingActionMessenger.h"
9 #include "AliGlobals.h"
10 #include "TG4StepManager.h"
11 #include "TG4PhysicsManager.h"
13 #include <G4TrackingManager.hh>
17 #include <TParticle.h>
18 #include <TClonesArray.h>
20 // static data members
21 AliTrackingAction* AliTrackingAction::fgInstance = 0;
24 AliTrackingAction* AliTrackingAction::Instance() {
29 AliTrackingAction::AliTrackingAction()
37 fLastParticleIndex(-1)
41 AliGlobals::Exception("AliTrackingAction constructed twice.");
44 fMessenger = new AliTrackingActionMessenger(this);
48 AliTrackingAction::AliTrackingAction(const AliTrackingAction& right) {
50 AliGlobals::Exception("AliTrackingAction is protected from copying.");
53 AliTrackingAction::~AliTrackingAction() {
61 AliTrackingAction::operator=(const AliTrackingAction &right)
63 // check assignement to self
64 if (this == &right) return *this;
66 AliGlobals::Exception("AliTrackingAction is protected from assigning.");
73 void AliTrackingAction::PrepareNewEvent()
75 // Called by G4 kernel at the beginning of event.
81 if (!fParticles) fParticles = gAlice->Particles();
83 // set the particles and primaries counter to the
84 // number of tracks already stored in AliRun::fParticles
85 // (fParticles can already contain primary particles
86 // saved by AliGenerator)
87 G4int nofTracks = gAlice->GetNtrack();
88 fPrimariesCounter = nofTracks;
89 fParticlesCounter = nofTracks;
90 fLastParticleIndex = nofTracks-1;
92 // set g4 stepping manager pointer
93 G4SteppingManager* pG4StepManager
94 = fpTrackingManager->GetSteppingManager();
95 TG4StepManager* pStepManager = TG4StepManager::Instance();
96 pStepManager->SetSteppingManager(pG4StepManager);
99 void AliTrackingAction::PreTrackingAction(const G4Track* aTrack)
101 // Called by G4 kernel before starting tracking.
105 // track index in the particles array
106 G4int trackID = aTrack->GetTrackID();
107 G4int parentID = aTrack->GetParentID();
110 trackIndex = trackID;
113 trackIndex = GetNofSavedTracks();
116 // in AliRoot (from V3.0) track numbering starts from 0
117 gAlice->SetCurrentTrack(--trackIndex);
120 // save primary track
121 SaveAndDestroyTrack();
122 fPrimaryTrackID = aTrack->GetTrackID();
125 // save secondary particles info
126 SaveParticle(aTrack);
132 void AliTrackingAction::PostTrackingAction(const G4Track* aTrack)
134 // Called by G4 kernel after finishing tracking.
142 void AliTrackingAction::SaveAndDestroyTrack()
144 // Calls AliRun::PurifyKine and fills trees of hits
145 // after finishing tracking of each primary track.
146 // !! This method has to be also called from AlEventAction::EndOfEventAction()
147 // for storing the last primary track of the current event.
150 if (fPrimaryTrackID>0)
152 if (fVerboseLevel == 3) {
153 G4cout << "$$$ Primary track " << fPrimaryTrackID << G4endl;
155 else if ( fVerboseLevel == 2 && fPrimaryTrackID % 10 == 0 ) {
156 G4cout << "$$$ Primary track " << fPrimaryTrackID << G4endl;
158 else if ( fVerboseLevel == 1 && fPrimaryTrackID % 100 == 0 ) {
159 G4cout << "$$$ Primary track " << fPrimaryTrackID << G4endl;
164 = gAlice->PurifyKine(fLastParticleIndex, fParticlesCounter);
165 G4int nofPuredSavedTracks
166 = gAlice->GetNtrack();
167 fLastParticleIndex = lastSavedTrack;
168 fParticlesCounter = nofPuredSavedTracks;
170 if(gAlice->TreeH()) gAlice->TreeH()->Fill();
174 fLastParticleIndex = fPrimariesCounter-1;
179 void AliTrackingAction::SaveParticle(const G4Track* track)
181 // Converts G4track to TParticle and saves it in AliRun::fParticles
188 G4int firstDaughter = -1;
189 G4int lastDaughter = -1;
190 G4int parentID = track->GetParentID();
192 G4int motherIndex2 = -1;
198 // set first/last child for already defined particles
199 motherIndex1 = GetParticleIndex(parentID);
202 = dynamic_cast<TParticle*>(fParticles->UncheckedAt(motherIndex1));
204 if (parent->GetFirstDaughter()<0)
205 parent->SetFirstDaughter(fParticlesCounter);
206 parent->SetLastDaughter(fParticlesCounter);
209 AliGlobals::Exception(
210 "AliTrackingAction::SaveParticle: Unknown particle type");
214 // ?? status of particle
215 // temporarily used for storing trackID from G4
216 G4int ks = track->GetTrackID();
219 // is this G3 ekvivalent ?? - check
220 G4int pdg = track->GetDefinition()->GetPDGEncoding();
223 G4ThreeVector momentum = track->GetMomentum();
224 G4double px = momentum.x()/GeV;
225 G4double py = momentum.y()/GeV;
226 G4double pz = momentum.z()/GeV;
227 G4double e = track->GetTotalEnergy()/GeV;
229 G4ThreeVector position = track->GetPosition();
230 G4double vx = position.x()/cm;
231 G4double vy = position.y()/cm;
232 G4double vz = position.z()/cm;
233 // time of production - check if ekvivalent with G4
234 G4double t = track->GetGlobalTime();
236 G4ThreeVector polarization = track->GetPolarization();
237 G4double polX = polarization.x();
238 G4double polY = polarization.y();
239 G4double polZ = polarization.z();
242 TClonesArray& theCollectionRef = *fParticles;
243 G4int nofParticles = theCollectionRef.GetEntriesFast();
245 = new(theCollectionRef[nofParticles])
246 TParticle(pdg, ks, motherIndex1, motherIndex1,
247 firstDaughter, lastDaughter, px, py, pz, e, vx, vy, vz, t);
248 particle->SetPolarisation(polX, polY, polZ);
249 particle->SetBit(kKeepBit, false);
251 // set production process
252 AliMCProcess mcProcess;
253 const G4VProcess* kpProcess = track->GetCreatorProcess();
255 mcProcess = kPPrimary;
258 TG4PhysicsManager* pPhysicsManager = TG4PhysicsManager::Instance();
259 mcProcess = pPhysicsManager->GetMCProcess(kpProcess);
260 // distinguish kPDeltaRay from kPEnergyLoss
261 if (mcProcess == kPEnergyLoss) mcProcess = kPDeltaRay;
263 particle->SetUniqueID(mcProcess);
266 G4int AliTrackingAction::GetParticleIndex(G4int trackID)
268 // Converts the trackID into the index of the particle
269 // in AliRun::fParticles array.
272 if (trackID <= fPrimariesCounter) {
276 for (G4int i=fLastParticleIndex+1; i<fParticlesCounter; i++) {
279 = (TParticle*)fParticles->UncheckedAt(i);
280 if (trackID == particle->GetStatusCode()) return i;
283 AliGlobals::Exception("AliTrackingAction::GetParticleIndex() has failed.");