Bugfix in AliPoints2Memory
[u/mrichter/AliRoot.git] / AliGeant4 / AliTrackingAction.cxx
CommitLineData
676fb573 1// $Id$
2// Category: event
3//
7005154f 4// Author: I. Hrivnacova
5//
6// Class AliTrackingAction
7// -----------------------
676fb573 8// See the class description in the header file.
9
10#include "AliTrackingAction.h"
4240c1e8 11#include "AliTrackInformation.h"
676fb573 12#include "AliRun.h"
13#include "AliGlobals.h"
14#include "TG4StepManager.h"
c8bc1274 15#include "TG4PhysicsManager.h"
e7057a42 16#include "TG4ParticlesManager.h"
676fb573 17
676fb573 18#include <G4Track.hh>
4240c1e8 19#include <G4TrackVector.hh>
20#include <G4VUserTrackInformation.hh>
21#include <G4TrackingManager.hh>
22#include <G4SteppingManager.hh>
78ca1e9c 23#include <G4UImanager.hh>
676fb573 24
676fb573 25// static data members
26AliTrackingAction* AliTrackingAction::fgInstance = 0;
27
78ca1e9c 28//_____________________________________________________________________________
676fb573 29AliTrackingAction::AliTrackingAction()
297e1c5f 30 : AliVerbose("trackingAction",2),
31 fPrimaryTrackID(0),
aafc96be 32 fNewVerboseLevel(0),
33 fNewVerboseTrackID(-1),
676fb573 34 fSavePrimaries(true),
7005154f 35 fTrackCounter(0),
36 fMessenger(this)
676fb573 37{
38//
39 if (fgInstance) {
40 AliGlobals::Exception("AliTrackingAction constructed twice.");
41 }
42
676fb573 43 fgInstance = this;
44}
45
78ca1e9c 46//_____________________________________________________________________________
7005154f 47AliTrackingAction::AliTrackingAction(const AliTrackingAction& right)
297e1c5f 48 : AliVerbose("trackingAction"),
49 fMessenger(this) {
676fb573 50//
51 AliGlobals::Exception("AliTrackingAction is protected from copying.");
52}
53
78ca1e9c 54//_____________________________________________________________________________
676fb573 55AliTrackingAction::~AliTrackingAction() {
56//
676fb573 57}
58
59// operators
60
78ca1e9c 61//_____________________________________________________________________________
676fb573 62AliTrackingAction&
63AliTrackingAction::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
4240c1e8 73// private methods
74
78ca1e9c 75//_____________________________________________________________________________
4240c1e8 76AliTrackInformation* AliTrackingAction::GetTrackInformation(
77 const G4Track* track,
78 const G4String& method) const
79{
80// Returns user track information.
81// ---
82
7005154f 83#ifdef TGEANT4_DEBUG
4240c1e8 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;
7005154f 96#else
97 return (AliTrackInformation*)track->GetUserInformation();
98#endif
4240c1e8 99}
100
676fb573 101// public methods
102
78ca1e9c 103//_____________________________________________________________________________
676fb573 104void AliTrackingAction::PrepareNewEvent()
105{
106// Called by G4 kernel at the beginning of event.
107// ---
108
3bfabcfc 109 fTrackCounter = 0;
110
676fb573 111 // set g4 stepping manager pointer
4240c1e8 112 TG4StepManager* stepManager = TG4StepManager::Instance();
113 stepManager->SetSteppingManager(fpTrackingManager->GetSteppingManager());
676fb573 114}
115
78ca1e9c 116//_____________________________________________________________________________
9bcb6317 117void AliTrackingAction::PreTrackingAction(const G4Track* aTrack)
676fb573 118{
119// Called by G4 kernel before starting tracking.
120// ---
121
676fb573 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) {
4240c1e8 127 // in AliRoot (from V3.0) track numbering starts from 0
128 trackIndex = trackID-1;
676fb573 129 }
130 else {
4240c1e8 131 trackIndex = gAlice->GetNtrack();
676fb573 132 }
4240c1e8 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);
676fb573 147
4240c1e8 148 // set current track number
149 gAlice->SetCurrentTrack(trackIndex);
676fb573 150
151 if (parentID == 0) {
4240c1e8 152 // finish previous primary track
153 FinishPrimaryTrack();
676fb573 154 fPrimaryTrackID = aTrack->GetTrackID();
887f9ee9 155
156 // begin this primary track
157 gAlice->BeginPrimary();
676fb573 158 }
159 else {
160 // save secondary particles info
4240c1e8 161 SaveTrack(aTrack);
9bcb6317 162 }
b17c5755 163
aafc96be 164 // verbose
165 if (trackID == fNewVerboseTrackID) {
166 G4String command = "/tracking/verbose ";
167 AliGlobals::AppendNumberToString(command, fNewVerboseLevel);
168 G4UImanager::GetUIpointer()->ApplyCommand(command);
169 }
170
4240c1e8 171 // aliroot pre track actions
b17c5755 172 gAlice->PreTrack();
676fb573 173}
174
78ca1e9c 175//_____________________________________________________________________________
9bcb6317 176void AliTrackingAction::PostTrackingAction(const G4Track* aTrack)
676fb573 177{
178// Called by G4 kernel after finishing tracking.
179// ---
180
3bfabcfc 181 fTrackCounter++;
4240c1e8 182
183 // set parent track particle index to all secondary tracks
184 G4TrackVector* secondaryTracks
185 = fpTrackingManager->GetSteppingManager()->GetSecondary();
186 if (secondaryTracks){
187 G4int i;
ff849423 188 for (i=0; i<secondaryTracks->size(); i++) {
4240c1e8 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
b17c5755 214 gAlice->PostTrack();
676fb573 215}
216
78ca1e9c 217//_____________________________________________________________________________
4240c1e8 218void AliTrackingAction::FinishPrimaryTrack()
676fb573 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
4240c1e8 226 if (fPrimaryTrackID>0) {
227
228 // verbose
297e1c5f 229 if (VerboseLevel() == 3) {
4240c1e8 230 G4cout << "$$$ Primary track " << fPrimaryTrackID << G4endl;
231 }
297e1c5f 232 else if (VerboseLevel() == 2 && fPrimaryTrackID % 10 == 0 ) {
4240c1e8 233 G4cout << "$$$ Primary track " << fPrimaryTrackID << G4endl;
234 }
297e1c5f 235 else if (VerboseLevel() == 1 && fPrimaryTrackID % 100 == 0 ) {
4240c1e8 236 G4cout << "$$$ Primary track " << fPrimaryTrackID << G4endl;
237 }
238
239 // aliroot finish primary track
240 gAlice->FinishPrimary();
241 }
242 fPrimaryTrackID = 0;
676fb573 243}
244
78ca1e9c 245//_____________________________________________________________________________
4240c1e8 246void AliTrackingAction::SaveTrack(const G4Track* track)
676fb573 247{
4240c1e8 248// Get all needed parameters from G4track and pass them
249// to AliRun::SetTrack() that creates corresponding TParticle
250// in the AliRun::fParticles array.
676fb573 251// ----
252
4240c1e8 253 // parent particle index
676fb573 254 G4int parentID = track->GetParentID();
4240c1e8 255 G4int motherIndex;
676fb573 256 if (parentID == 0) {
4240c1e8 257 motherIndex = -1;
676fb573 258 }
259 else {
4240c1e8 260 motherIndex
261 = GetTrackInformation(track,"SaveTrack")->GetParentParticleID();
262 }
676fb573 263
4240c1e8 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
e7057a42 271 G4int pdg
272 = TG4ParticlesManager::Instance()
273 ->GetPDGEncodingFast(track->GetDefinition());
676fb573 274
4240c1e8 275 // track kinematics
676fb573 276 G4ThreeVector momentum = track->GetMomentum();
4240c1e8 277
676fb573 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
4240c1e8 295 // production process
c8bc1274 296 AliMCProcess mcProcess;
297 const G4VProcess* kpProcess = track->GetCreatorProcess();
298 if (!kpProcess) {
299 mcProcess = kPPrimary;
300 }
301 else {
e7057a42 302 mcProcess = TG4PhysicsManager::Instance()->GetMCProcess(kpProcess);
c8bc1274 303 // distinguish kPDeltaRay from kPEnergyLoss
304 if (mcProcess == kPEnergyLoss) mcProcess = kPDeltaRay;
305 }
4240c1e8 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
676fb573 312}
313
78ca1e9c 314//_____________________________________________________________________________
aafc96be 315void 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
78ca1e9c 324//_____________________________________________________________________________
aafc96be 325void 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}