4 // See the class description in the header file.
6 #include "AliTrackingAction.h"
7 #include "AliTrackingActionMessenger.h"
8 #include "AliSensitiveDetector.h"
10 #include "AliGlobals.h"
11 #include "TG4StepManager.h"
13 #include <G4TrackingManager.hh>
16 #include <G4SDManager.hh>
17 #include <G4VSensitiveDetector.hh>
18 #include <G4VHitsCollection.hh>
21 #include <TParticle.h>
23 // static data members
24 AliTrackingAction* AliTrackingAction::fgInstance = 0;
27 AliTrackingAction* AliTrackingAction::Instance() {
32 AliTrackingAction::AliTrackingAction()
40 fLastParticleIndex(-1)
44 AliGlobals::Exception("AliTrackingAction constructed twice.");
47 fMessenger = new AliTrackingActionMessenger(this);
51 AliTrackingAction::AliTrackingAction(const AliTrackingAction& right) {
53 AliGlobals::Exception("AliTrackingAction is protected from copying.");
56 AliTrackingAction::~AliTrackingAction() {
64 AliTrackingAction::operator=(const AliTrackingAction &right)
66 // check assignement to self
67 if (this == &right) return *this;
69 AliGlobals::Exception("AliTrackingAction is protected from assigning.");
76 void AliTrackingAction::PrepareNewEvent()
78 // Called by G4 kernel at the beginning of event.
84 if (!fParticles) fParticles = gAlice->Particles();
86 // set the particles and primaries counter to the
87 // number of tracks already stored in AliRun::fParticles
88 // (fParticles can already contain primary particles
89 // saved by AliGenerator)
90 G4int nofTracks = gAlice->GetNtrack();
91 fPrimariesCounter = nofTracks;
92 fParticlesCounter = nofTracks;
93 fLastParticleIndex = nofTracks-1;
95 // set g4 stepping manager pointer
96 G4SteppingManager* pG4StepManager
97 = fpTrackingManager->GetSteppingManager();
98 TG4StepManager* pStepManager = TG4StepManager::Instance();
99 pStepManager->SetSteppingManager(pG4StepManager);
102 void AliTrackingAction::PreTrackingAction(const G4Track* aTrack)
104 // Called by G4 kernel before starting tracking.
108 // track index in the particles array
109 G4int trackID = aTrack->GetTrackID();
110 G4int parentID = aTrack->GetParentID();
113 trackIndex = trackID;
116 trackIndex = GetNofSavedTracks();
119 // in AliRoot (from V3.0) track numbering starts from 0
120 gAlice->SetCurrentTrack(--trackIndex);
123 // save primary track
124 SaveAndDestroyTrack();
125 fPrimaryTrackID = aTrack->GetTrackID();
128 // save secondary particles info
129 // improve this later with retrieving the generation process
130 // (primary particles are stored
131 // by AlStackingAction in ClassifyNewTrack() method)
132 G4String origin = "secondary";
133 SaveParticle(aTrack, origin);
137 void AliTrackingAction::PostTrackingAction(const G4Track* aTrack)
139 // Called by G4 kernel after finishing tracking.
145 void AliTrackingAction::SaveAndDestroyTrack()
147 // Calls AliRun::PurifyKine and fills trees of hits
148 // after finishing tracking of each primary track.
149 // !! This method has to be also called from AlEventAction::EndOfEventAction()
150 // for storing the last primary track of the current event.
153 if (fPrimaryTrackID>0)
155 if (fVerboseLevel == 3) {
156 G4cout << "$$$ Primary track " << fPrimaryTrackID << G4endl;
158 else if ( fVerboseLevel == 2 && fPrimaryTrackID % 10 == 0 ) {
159 G4cout << "$$$ Primary track " << fPrimaryTrackID << G4endl;
161 else if ( fVerboseLevel == 1 && fPrimaryTrackID % 100 == 0 ) {
162 G4cout << "$$$ Primary track " << fPrimaryTrackID << G4endl;
167 = gAlice->PurifyKine(fLastParticleIndex, fParticlesCounter);
168 G4int nofPuredSavedTracks
169 = gAlice->GetNtrack();
170 fLastParticleIndex = lastSavedTrack;
171 fParticlesCounter = nofPuredSavedTracks;
173 if(gAlice->TreeH()) gAlice->TreeH()->Fill();
177 fLastParticleIndex = fPrimariesCounter-1;
182 void AliTrackingAction::SaveParticle(const G4Track* track,
183 G4String processName)
185 // Converts G4track to TParticle and saves it in AliRun::fParticles
192 G4int firstDaughter = -1;
193 G4int lastDaughter = -1;
194 G4int parentID = track->GetParentID();
196 G4int motherIndex2 = -1;
202 // set first/last child for already defined particles
203 motherIndex1 = GetParticleIndex(parentID);
206 = dynamic_cast<TParticle*>(fParticles->UncheckedAt(motherIndex1));
208 if (parent->GetFirstDaughter()<0)
209 parent->SetFirstDaughter(fParticlesCounter);
210 parent->SetLastDaughter(fParticlesCounter);
213 AliGlobals::Exception(
214 "AliTrackingAction::SaveParticle: Unknown particle type");
218 // ?? status of particle
219 // temporarily used for storing trackID from G4
220 G4int ks = track->GetTrackID();
223 // is this G3 ekvivalent ?? - check
224 G4int pdg = track->GetDefinition()->GetPDGEncoding();
227 G4ThreeVector momentum = track->GetMomentum();
228 G4double px = momentum.x()/GeV;
229 G4double py = momentum.y()/GeV;
230 G4double pz = momentum.z()/GeV;
231 G4double e = track->GetTotalEnergy()/GeV;
233 G4ThreeVector position = track->GetPosition();
234 G4double vx = position.x()/cm;
235 G4double vy = position.y()/cm;
236 G4double vz = position.z()/cm;
237 // time of production - check if ekvivalent with G4
238 G4double t = track->GetGlobalTime();
240 G4ThreeVector polarization = track->GetPolarization();
241 G4double polX = polarization.x();
242 G4double polY = polarization.y();
243 G4double polZ = polarization.z();
246 TClonesArray& theCollectionRef = *fParticles;
247 G4int nofParticles = theCollectionRef.GetEntriesFast();
249 = new(theCollectionRef[nofParticles])
250 TParticle(pdg, ks, motherIndex1, motherIndex1,
251 firstDaughter, lastDaughter, px, py, pz, e, vx, vy, vz, t);
252 particle->SetPolarisation(polX, polY, polZ);
253 particle->SetBit(kKeepBit, false);
256 G4int AliTrackingAction::GetParticleIndex(G4int trackID)
258 // Converts the trackID into the index of the particle
259 // in AliRun::fParticles array.
262 if (trackID <= fPrimariesCounter) {
266 for (G4int i=fLastParticleIndex+1; i<fParticlesCounter; i++) {
269 = (TParticle*)fParticles->UncheckedAt(i);
270 if (trackID == particle->GetStatusCode()) return i;
273 AliGlobals::Exception("AliTrackingAction::GetParticleIndex() has failed.");