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