]> git.uio.no Git - u/mrichter/AliRoot.git/blob - AliGeant4/AliTrackingAction.cxx
Bugfix in AliPoints2Memory
[u/mrichter/AliRoot.git] / AliGeant4 / AliTrackingAction.cxx
1 // $Id$
2 // Category: event
3 //
4 // Author: I. Hrivnacova
5 //
6 // Class AliTrackingAction
7 // -----------------------
8 // See the class description in the header file.
9
10 #include "AliTrackingAction.h"
11 #include "AliTrackInformation.h"
12 #include "AliRun.h"
13 #include "AliGlobals.h"  
14 #include "TG4StepManager.h"
15 #include "TG4PhysicsManager.h"
16 #include "TG4ParticlesManager.h"
17
18 #include <G4Track.hh>
19 #include <G4TrackVector.hh>
20 #include <G4VUserTrackInformation.hh>
21 #include <G4TrackingManager.hh>
22 #include <G4SteppingManager.hh>
23 #include <G4UImanager.hh>
24
25 // static data members
26 AliTrackingAction* AliTrackingAction::fgInstance = 0;
27
28 //_____________________________________________________________________________
29 AliTrackingAction::AliTrackingAction()
30   : AliVerbose("trackingAction",2),
31     fPrimaryTrackID(0),
32     fNewVerboseLevel(0),
33     fNewVerboseTrackID(-1),
34     fSavePrimaries(true),
35     fTrackCounter(0),
36     fMessenger(this)
37 {
38 //
39   if (fgInstance) { 
40     AliGlobals::Exception("AliTrackingAction constructed twice."); 
41   }
42
43   fgInstance = this;
44 }
45
46 //_____________________________________________________________________________
47 AliTrackingAction::AliTrackingAction(const AliTrackingAction& right) 
48   : AliVerbose("trackingAction"),
49     fMessenger(this) {
50 //
51   AliGlobals::Exception("AliTrackingAction is protected from copying.");
52 }
53
54 //_____________________________________________________________________________
55 AliTrackingAction::~AliTrackingAction() {
56 //
57 }
58
59 // operators
60
61 //_____________________________________________________________________________
62 AliTrackingAction& 
63 AliTrackingAction::operator=(const AliTrackingAction &right)
64 {
65   // check assignement to self
66   if (this == &right) return *this;
67   
68   AliGlobals::Exception("AliTrackingAction is protected from assigning.");
69
70   return *this;
71 }
72
73 // private methods
74
75 //_____________________________________________________________________________
76 AliTrackInformation* AliTrackingAction::GetTrackInformation(
77                                            const G4Track* track,
78                                            const G4String& method) const
79 {
80 // Returns user track information.
81 // ---
82  
83 #ifdef TGEANT4_DEBUG
84   G4VUserTrackInformation* trackInfo = track->GetUserInformation();
85   if (!trackInfo) return 0;  
86
87   AliTrackInformation* aliTrackInfo
88     = dynamic_cast<AliTrackInformation*>(trackInfo);
89   if (!aliTrackInfo) { 
90      G4String text = "AliTrackingAction::" + method + ":\n";
91      text = text + "   Unknown track information type";
92      AliGlobals::Exception(text);
93   }
94   
95   return aliTrackInfo;
96 #else  
97   return (AliTrackInformation*)track->GetUserInformation();
98 #endif  
99 }    
100   
101 // public methods
102
103 //_____________________________________________________________________________
104 void AliTrackingAction::PrepareNewEvent()
105 {
106 // Called by G4 kernel at the beginning of event.
107 // ---
108
109   fTrackCounter = 0;
110
111   // set g4 stepping manager pointer
112   TG4StepManager* stepManager = TG4StepManager::Instance();
113   stepManager->SetSteppingManager(fpTrackingManager->GetSteppingManager());
114 }
115
116 //_____________________________________________________________________________
117 void AliTrackingAction::PreTrackingAction(const G4Track* aTrack)
118 {
119 // Called by G4 kernel before starting tracking.
120 // ---
121
122   // track index in the particles array
123   G4int trackID = aTrack->GetTrackID();
124   G4int parentID = aTrack->GetParentID();
125   Int_t trackIndex;
126   if (parentID==0) { 
127     // in AliRoot (from V3.0) track numbering starts from 0
128     trackIndex = trackID-1; 
129   } 
130   else { 
131     trackIndex = gAlice->GetNtrack();
132   }
133   
134   // set track index to track information
135   AliTrackInformation* trackInfo
136     = GetTrackInformation(aTrack, "PreTrackingAction");
137   if (!trackInfo) {
138     // create track information and set it to G4Track
139     // if it does not yet exist
140     trackInfo = new AliTrackInformation(trackIndex);
141     fpTrackingManager->SetUserTrackInformation(trackInfo);
142         // the track information is deleted together with its
143         // G4Track object  
144   }
145   else
146     trackInfo->SetTrackParticleID(trackIndex);  
147
148   // set current track number
149   gAlice->SetCurrentTrack(trackIndex);
150
151   if (parentID == 0) {  
152     // finish previous primary track
153     FinishPrimaryTrack();
154     fPrimaryTrackID = aTrack->GetTrackID();
155     
156     // begin this primary track
157     gAlice->BeginPrimary();
158   }
159   else { 
160     // save secondary particles info 
161     SaveTrack(aTrack);
162   }
163   
164   // verbose
165   if (trackID == fNewVerboseTrackID) {
166       G4String command = "/tracking/verbose ";
167       AliGlobals::AppendNumberToString(command, fNewVerboseLevel);
168       G4UImanager::GetUIpointer()->ApplyCommand(command);
169   }    
170
171   // aliroot pre track actions
172   gAlice->PreTrack();
173 }
174
175 //_____________________________________________________________________________
176 void AliTrackingAction::PostTrackingAction(const G4Track* aTrack)
177 {
178 // Called by G4 kernel after finishing tracking.
179 // ---
180
181   fTrackCounter++;
182   
183   // set parent track particle index to all secondary tracks 
184   G4TrackVector* secondaryTracks 
185     = fpTrackingManager->GetSteppingManager()->GetSecondary();
186   if (secondaryTracks){
187     G4int i;
188     for (i=0; i<secondaryTracks->size(); i++) {
189       G4Track* track = (*secondaryTracks)[i]; 
190
191       if (track->GetUserInformation()) {
192         // this should never happen
193         G4String text = "AliTrackingAction::PostTrackingAction:\n";
194         text = text + "    Inconsistent track information."; 
195         AliGlobals::Exception(text);
196       } 
197       
198       // get parent track index
199       AliTrackInformation* aliParentInfo
200         = GetTrackInformation(aTrack, "PostTrackingAction");
201       G4int parentParticleID 
202         = aliParentInfo->GetTrackParticleID();
203
204       // create track information and set it to the G4Track
205       AliTrackInformation* trackInfo 
206         = new AliTrackInformation(-1, parentParticleID);
207       track->SetUserInformation(trackInfo);
208         // the track information is deleted together with its
209         // G4Track object  
210     }   
211   }
212       
213   // aliroot post track actions
214   gAlice->PostTrack();
215 }
216
217 //_____________________________________________________________________________
218 void AliTrackingAction::FinishPrimaryTrack()
219 {
220 // Calls AliRun::PurifyKine and fills trees of hits
221 // after finishing tracking of each primary track.
222 // !! This method has to be also called from AlEventAction::EndOfEventAction() 
223 // for storing the last primary track of the current event.
224 // --- 
225
226   if (fPrimaryTrackID>0) {
227
228     // verbose
229     if (VerboseLevel() == 3) { 
230       G4cout << "$$$ Primary track " << fPrimaryTrackID << G4endl;
231     } 
232     else if (VerboseLevel() == 2 &&  fPrimaryTrackID % 10 == 0 ) {
233       G4cout << "$$$ Primary track " << fPrimaryTrackID  << G4endl;
234     } 
235     else if (VerboseLevel() == 1 &&  fPrimaryTrackID % 100 == 0 ) {
236       G4cout << "$$$ Primary track " << fPrimaryTrackID  << G4endl;
237     } 
238
239     // aliroot finish primary track         
240     gAlice->FinishPrimary();
241   }
242   fPrimaryTrackID = 0;
243 }  
244
245 //_____________________________________________________________________________
246 void AliTrackingAction::SaveTrack(const G4Track* track)
247 {
248 // Get all needed parameters from G4track and pass them
249 // to AliRun::SetTrack() that creates corresponding TParticle
250 // in the AliRun::fParticles array.
251 // ----
252
253   // parent particle index 
254   G4int parentID = track->GetParentID();
255   G4int motherIndex;
256   if (parentID == 0) { 
257     motherIndex = -1; 
258   }
259   else {
260     motherIndex 
261       = GetTrackInformation(track,"SaveTrack")->GetParentParticleID();
262   }
263      
264   //G4cout << "SaveTrack: TrackID = " << track->GetTrackID()
265   //       << "  Parent ID = " << track->GetParentID() 
266   //       << "  Index = " << gAlice->CurrentTrack() 
267   //       << "  Parent Index = " << motherIndex
268   //       << G4endl;
269
270   // PDG code
271   G4int pdg 
272     = TG4ParticlesManager::Instance()
273       ->GetPDGEncodingFast(track->GetDefinition());
274
275   // track kinematics  
276   G4ThreeVector momentum = track->GetMomentum(); 
277   
278   G4double px = momentum.x()/GeV;
279   G4double py = momentum.y()/GeV;
280   G4double pz = momentum.z()/GeV;
281   G4double e = track->GetTotalEnergy()/GeV;
282
283   G4ThreeVector position = track->GetPosition(); 
284   G4double vx = position.x()/cm;
285   G4double vy = position.y()/cm;
286   G4double vz = position.z()/cm;
287   // time of production - check if ekvivalent with G4
288   G4double t = track->GetGlobalTime();
289
290   G4ThreeVector polarization = track->GetPolarization(); 
291   G4double polX = polarization.x();
292   G4double polY = polarization.y();
293   G4double polZ = polarization.z();
294
295   // production process
296   AliMCProcess mcProcess;  
297   const G4VProcess* kpProcess = track->GetCreatorProcess();
298   if (!kpProcess) {
299     mcProcess = kPPrimary;
300   }
301   else {  
302     mcProcess = TG4PhysicsManager::Instance()->GetMCProcess(kpProcess);  
303     // distinguish kPDeltaRay from kPEnergyLoss  
304     if (mcProcess == kPEnergyLoss) mcProcess = kPDeltaRay;
305   }  
306   
307   G4int ntr;
308   // create particle 
309   gAlice->SetTrack(1, motherIndex, pdg, px, py, pz, e, vx, vy, vz, t,
310                    polX, polY, polZ, mcProcess, ntr);  
311                    
312 }
313
314 //_____________________________________________________________________________
315 void AliTrackingAction::SetNewVerboseLevel(G4int level)
316
317 // Set the new verbose level that will be set when the track with 
318 // specified track ID (fNewVerboseTrackID) starts tracking.
319 // ---
320
321   fNewVerboseLevel = level;  
322 }
323
324 //_____________________________________________________________________________
325 void AliTrackingAction::SetNewVerboseTrackID(G4int trackID)
326
327 // Set the trackID for which the new verbose level (fNewVerboseLevel)
328 // will be applied.
329 // ---
330
331   fNewVerboseTrackID = trackID; 
332 }