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