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"
12 #include <G4TrackingManager.hh>
15 #include <G4SDManager.hh>
16 #include <G4VHitsCollection.hh>
18 #include <TParticle.h>
20 // static data members
21 AliTrackingAction* AliTrackingAction::fgInstance = 0;
24 AliTrackingAction* AliTrackingAction::Instance() {
29 AliTrackingAction::AliTrackingAction()
36 fLastParticleIndex(-1)
40 AliGlobals::Exception("AliTrackingAction constructed twice.");
43 fMessenger = new AliTrackingActionMessenger(this);
47 AliTrackingAction::AliTrackingAction(const AliTrackingAction& right) {
49 AliGlobals::Exception("AliTrackingAction is protected from copying.");
52 AliTrackingAction::~AliTrackingAction() {
60 AliTrackingAction::operator=(const AliTrackingAction &right)
62 // check assignement to self
63 if (this == &right) return *this;
65 AliGlobals::Exception("AliTrackingAction is protected from assigning.");
72 void AliTrackingAction::PrepareNewEvent()
74 // Called by G4 kernel at the beginning of event.
78 if (!fParticles) fParticles = gAlice->Particles();
80 // set the particles and primaries counter to the
81 // number of tracks already stored in AliRun::fParticles
82 // (fParticles can already contain primary particles
83 // saved by AliGenerator)
84 G4int nofTracks = gAlice->GetNtrack();
85 fPrimariesCounter = nofTracks;
86 fParticlesCounter = nofTracks;
87 fLastParticleIndex = nofTracks-1;
89 // set g4 stepping manager pointer
90 G4SteppingManager* pG4StepManager
91 = fpTrackingManager->GetSteppingManager();
92 TG4StepManager* pStepManager = TG4StepManager::Instance();
93 pStepManager->SetSteppingManager(pG4StepManager);
96 void AliTrackingAction::PreUserTrackingAction(const G4Track* aTrack)
98 // Called by G4 kernel before starting tracking.
102 // track index in the particles array
103 G4int trackID = aTrack->GetTrackID();
104 G4int parentID = aTrack->GetParentID();
107 trackIndex = trackID;
110 trackIndex = GetNofSavedTracks();
113 // in AliRoot (from V3.0) track numbering starts from 0
114 gAlice->SetCurrentTrack(--trackIndex);
117 // save primary track
118 SaveAndDestroyTrack();
119 fPrimaryTrackID = aTrack->GetTrackID();
122 // save secondary particles info
123 // improve this later with retrieving the generation process
124 // (primary particles are stored
125 // by AlStackingAction in ClassifyNewTrack() method)
126 G4String origin = "secondary";
127 SaveParticle(aTrack, origin);
131 void AliTrackingAction::PostUserTrackingAction(const G4Track* aTrack)
133 // Called by G4 kernel after finishing tracking.
136 G4String particleName
137 = aTrack->GetDynamicParticle()->GetDefinition()->GetParticleName();
138 if (particleName == "opticalphoton") {
139 G4cout << "$$$ Track " << aTrack->GetTrackID()
140 << " is optical photon." << endl;
144 void AliTrackingAction::SaveAndDestroyTrack()
146 // Calls AliRun::PurifyKine and fills trees of hits
147 // after finishing tracking of each primary track.
148 // !! This method has to be also called from AlEventAction::EndOfEventAction()
149 // for storing the last primary track of the current event.
152 if (fPrimaryTrackID>0)
154 if (fVerboseLevel == 3) {
155 G4cout << "$$$ Primary track " << fPrimaryTrackID << endl;
157 else if ( fVerboseLevel == 2 && fPrimaryTrackID % 10 == 0 ) {
158 G4cout << "$$$ Primary track " << fPrimaryTrackID << endl;
160 else if ( fVerboseLevel == 1 && fPrimaryTrackID % 100 == 0 ) {
161 G4cout << "$$$ Primary track " << fPrimaryTrackID << endl;
166 = gAlice->PurifyKine(fLastParticleIndex, fParticlesCounter);
167 G4int nofPuredSavedTracks
168 = gAlice->GetNtrack();
169 fLastParticleIndex = lastSavedTrack;
170 fParticlesCounter = nofPuredSavedTracks;
172 if(gAlice->TreeH()) gAlice->TreeH()->Fill();
176 fLastParticleIndex = fPrimariesCounter-1;
181 void AliTrackingAction::SaveParticle(const G4Track* track,
182 G4String processName)
184 // Converts G4track to TParticle and saves it in AliRun::fParticles
191 G4int firstDaughter = -1;
192 G4int lastDaughter = -1;
193 G4int parentID = track->GetParentID();
195 G4int motherIndex2 = -1;
201 // set first/last child for already defined particles
202 motherIndex1 = GetParticleIndex(parentID);
205 = dynamic_cast<TParticle*>(fParticles->UncheckedAt(motherIndex1));
207 if (parent->GetFirstDaughter()<0)
208 parent->SetFirstDaughter(fParticlesCounter);
209 parent->SetLastDaughter(fParticlesCounter);
212 AliGlobals::Exception(
213 "AliTrackingAction::SaveParticle: Unknown particle type");
217 // ?? status of particle
218 // temporarily used for storing trackID from G4
219 G4int ks = track->GetTrackID();
222 // is this G3 ekvivalent ?? - check
223 G4int pdg = track->GetDefinition()->GetPDGEncoding();
226 G4ThreeVector momentum = track->GetMomentum();
227 G4double px = momentum.x()/GeV;
228 G4double py = momentum.y()/GeV;
229 G4double pz = momentum.z()/GeV;
230 G4double e = track->GetTotalEnergy()/GeV;
232 G4ThreeVector position = track->GetPosition();
233 G4double vx = position.x()/cm;
234 G4double vy = position.y()/cm;
235 G4double vz = position.z()/cm;
236 // time of production - check if ekvivalent with G4
237 G4double t = track->GetGlobalTime();
239 G4ThreeVector polarization = track->GetPolarization();
240 G4double polX = polarization.x();
241 G4double polY = polarization.y();
242 G4double polZ = polarization.z();
245 TClonesArray& theCollectionRef = *fParticles;
246 G4int nofParticles = theCollectionRef.GetEntriesFast();
248 = new(theCollectionRef[nofParticles])
249 TParticle(pdg, ks, motherIndex1, motherIndex1,
250 firstDaughter, lastDaughter, px, py, pz, e, vx, vy, vz, t);
251 particle->SetPolarisation(polX, polY, polZ);
252 particle->SetBit(Keep_Bit, false);
255 G4int AliTrackingAction::GetParticleIndex(G4int trackID)
257 // Converts the trackID into the index of the particle
258 // in AliRun::fParticles array.
261 if (trackID <= fPrimariesCounter) {
265 for (G4int i=fLastParticleIndex+1; i<fParticlesCounter; i++) {
268 = (TParticle*)fParticles->UncheckedAt(i);
269 if (trackID == particle->GetStatusCode()) return i;
272 AliGlobals::Exception("AliTrackingAction::GetParticleIndex() has failed.");