4 // See the class description in the header file.
6 #include "AliTrackingAction.h"
7 #include "AliTrackingActionMessenger.h"
8 #include "AliTrackInformation.h"
10 #include "AliGlobals.h"
11 #include "TG4StepManager.h"
12 #include "TG4PhysicsManager.h"
15 #include <G4TrackVector.hh>
16 #include <G4VUserTrackInformation.hh>
17 #include <G4TrackingManager.hh>
18 #include <G4SteppingManager.hh>
19 #include <G4UImanager.hh>
21 // static data members
22 AliTrackingAction* AliTrackingAction::fgInstance = 0;
24 //_____________________________________________________________________________
25 AliTrackingAction::AliTrackingAction()
29 fNewVerboseTrackID(-1),
35 AliGlobals::Exception("AliTrackingAction constructed twice.");
38 fMessenger = new AliTrackingActionMessenger(this);
42 //_____________________________________________________________________________
43 AliTrackingAction::AliTrackingAction(const AliTrackingAction& right) {
45 AliGlobals::Exception("AliTrackingAction is protected from copying.");
48 //_____________________________________________________________________________
49 AliTrackingAction::~AliTrackingAction() {
56 //_____________________________________________________________________________
58 AliTrackingAction::operator=(const AliTrackingAction &right)
60 // check assignement to self
61 if (this == &right) return *this;
63 AliGlobals::Exception("AliTrackingAction is protected from assigning.");
70 //_____________________________________________________________________________
71 AliTrackInformation* AliTrackingAction::GetTrackInformation(
73 const G4String& method) const
75 // Returns user track information.
78 G4VUserTrackInformation* trackInfo = track->GetUserInformation();
79 if (!trackInfo) return 0;
81 AliTrackInformation* aliTrackInfo
82 = dynamic_cast<AliTrackInformation*>(trackInfo);
84 G4String text = "AliTrackingAction::" + method + ":\n";
85 text = text + " Unknown track information type";
86 AliGlobals::Exception(text);
94 //_____________________________________________________________________________
95 void AliTrackingAction::PrepareNewEvent()
97 // Called by G4 kernel at the beginning of event.
102 // set g4 stepping manager pointer
103 TG4StepManager* stepManager = TG4StepManager::Instance();
104 stepManager->SetSteppingManager(fpTrackingManager->GetSteppingManager());
107 //_____________________________________________________________________________
108 void AliTrackingAction::PreTrackingAction(const G4Track* aTrack)
110 // Called by G4 kernel before starting tracking.
113 // track index in the particles array
114 G4int trackID = aTrack->GetTrackID();
115 G4int parentID = aTrack->GetParentID();
118 // in AliRoot (from V3.0) track numbering starts from 0
119 trackIndex = trackID-1;
122 trackIndex = gAlice->GetNtrack();
125 // set track index to track information
126 AliTrackInformation* trackInfo
127 = GetTrackInformation(aTrack, "PreTrackingAction");
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
137 trackInfo->SetTrackParticleID(trackIndex);
139 // set current track number
140 gAlice->SetCurrentTrack(trackIndex);
143 // finish previous primary track
144 FinishPrimaryTrack();
145 fPrimaryTrackID = aTrack->GetTrackID();
147 // begin this primary track
148 gAlice->BeginPrimary();
151 // save secondary particles info
156 if (trackID == fNewVerboseTrackID) {
157 G4String command = "/tracking/verbose ";
158 AliGlobals::AppendNumberToString(command, fNewVerboseLevel);
159 G4UImanager::GetUIpointer()->ApplyCommand(command);
162 // aliroot pre track actions
166 //_____________________________________________________________________________
167 void AliTrackingAction::PostTrackingAction(const G4Track* aTrack)
169 // Called by G4 kernel after finishing tracking.
174 // set parent track particle index to all secondary tracks
175 G4TrackVector* secondaryTracks
176 = fpTrackingManager->GetSteppingManager()->GetSecondary();
177 if (secondaryTracks){
179 for (i=0; i<secondaryTracks->size(); i++) {
180 G4Track* track = (*secondaryTracks)[i];
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);
189 // get parent track index
190 AliTrackInformation* aliParentInfo
191 = GetTrackInformation(aTrack, "PostTrackingAction");
192 G4int parentParticleID
193 = aliParentInfo->GetTrackParticleID();
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
204 // aliroot post track actions
208 //_____________________________________________________________________________
209 void AliTrackingAction::FinishPrimaryTrack()
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.
217 if (fPrimaryTrackID>0) {
220 if (fVerboseLevel == 3) {
221 G4cout << "$$$ Primary track " << fPrimaryTrackID << G4endl;
223 else if ( fVerboseLevel == 2 && fPrimaryTrackID % 10 == 0 ) {
224 G4cout << "$$$ Primary track " << fPrimaryTrackID << G4endl;
226 else if ( fVerboseLevel == 1 && fPrimaryTrackID % 100 == 0 ) {
227 G4cout << "$$$ Primary track " << fPrimaryTrackID << G4endl;
230 // aliroot finish primary track
231 gAlice->FinishPrimary();
236 //_____________________________________________________________________________
237 void AliTrackingAction::SaveTrack(const G4Track* track)
239 // Get all needed parameters from G4track and pass them
240 // to AliRun::SetTrack() that creates corresponding TParticle
241 // in the AliRun::fParticles array.
244 // parent particle index
245 G4int parentID = track->GetParentID();
252 = GetTrackInformation(track,"SaveTrack")->GetParentParticleID();
255 //G4cout << "SaveTrack: TrackID = " << track->GetTrackID()
256 // << " Parent ID = " << track->GetParentID()
257 // << " Index = " << gAlice->CurrentTrack()
258 // << " Parent Index = " << motherIndex
262 G4int pdg = track->GetDefinition()->GetPDGEncoding();
265 G4ThreeVector momentum = track->GetMomentum();
267 G4double px = momentum.x()/GeV;
268 G4double py = momentum.y()/GeV;
269 G4double pz = momentum.z()/GeV;
270 G4double e = track->GetTotalEnergy()/GeV;
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();
279 G4ThreeVector polarization = track->GetPolarization();
280 G4double polX = polarization.x();
281 G4double polY = polarization.y();
282 G4double polZ = polarization.z();
284 // production process
285 AliMCProcess mcProcess;
286 const G4VProcess* kpProcess = track->GetCreatorProcess();
288 mcProcess = kPPrimary;
291 TG4PhysicsManager* pPhysicsManager = TG4PhysicsManager::Instance();
292 mcProcess = pPhysicsManager->GetMCProcess(kpProcess);
293 // distinguish kPDeltaRay from kPEnergyLoss
294 if (mcProcess == kPEnergyLoss) mcProcess = kPDeltaRay;
299 gAlice->SetTrack(1, motherIndex, pdg, px, py, pz, e, vx, vy, vz, t,
300 polX, polY, polZ, mcProcess, ntr);
304 //_____________________________________________________________________________
305 void AliTrackingAction::SetNewVerboseLevel(G4int level)
307 // Set the new verbose level that will be set when the track with
308 // specified track ID (fNewVerboseTrackID) starts tracking.
311 fNewVerboseLevel = level;
314 //_____________________________________________________________________________
315 void AliTrackingAction::SetNewVerboseTrackID(G4int trackID)
317 // Set the trackID for which the new verbose level (fNewVerboseLevel)
321 fNewVerboseTrackID = trackID;