Always delete TObjArrays created by TString::Tokenize (Ruben)
[u/mrichter/AliRoot.git] / MUON / AliMUONTrackReconstructorK.cxx
CommitLineData
8cc77df0 1/**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3 * *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
6 * *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
15
16/* $Id$ */
17
3d1463c8 18//-----------------------------------------------------------------------------
06ca6d7b 19/// \class AliMUONTrackReconstructorK
06ca6d7b 20///
21/// MUON track reconstructor using the kalman method
22///
23/// This class contains as data:
24/// - the parameters for the track reconstruction
25///
26/// It contains as methods, among others:
27/// - MakeTracks to build the tracks
28///
3d1463c8 29//-----------------------------------------------------------------------------
8cc77df0 30
8cc77df0 31#include "AliMUONTrackReconstructorK.h"
96ebe67e 32
8cc77df0 33#include "AliMUONConstants.h"
96ebe67e 34#include "AliMUONVCluster.h"
1fef78c8 35#include "AliMUONVClusterServer.h"
96ebe67e 36#include "AliMUONVClusterStore.h"
ea94c18b 37#include "AliMUONTrack.h"
38#include "AliMUONTrackParam.h"
39#include "AliMUONTrackExtrap.h"
5a240757 40#include "AliMUONRecoParam.h"
8cde4af5 41
1fef78c8 42#include "AliMpArea.h"
43
8cc77df0 44#include "AliLog.h"
45
8cde4af5 46#include <Riostream.h>
ea94c18b 47#include <TMath.h>
48#include <TMatrixD.h>
ce8cd162 49#include <TClonesArray.h>
8cde4af5 50
cdd730d0 51using std::endl;
52using std::cout;
8cde4af5 53/// \cond CLASSIMP
8cc77df0 54ClassImp(AliMUONTrackReconstructorK) // Class implementation in ROOT context
8cde4af5 55/// \endcond
8cc77df0 56
019df241 57 //__________________________________________________________________________
a0dc65b4 58AliMUONTrackReconstructorK::AliMUONTrackReconstructorK(const AliMUONRecoParam* recoParam, AliMUONVClusterServer* clusterServer)
59 : AliMUONVTrackReconstructor(recoParam, clusterServer)
8cc77df0 60{
96ebe67e 61 /// Constructor
8cc77df0 62}
63
019df241 64 //__________________________________________________________________________
ea94c18b 65AliMUONTrackReconstructorK::~AliMUONTrackReconstructorK()
8cc77df0 66{
ea94c18b 67/// Destructor
68}
8cc77df0 69
70 //__________________________________________________________________________
89c8d66d 71Bool_t AliMUONTrackReconstructorK::MakeTrackCandidates(AliMUONVClusterStore& clusterStore)
8cc77df0 72{
89c8d66d 73 /// To make track candidates (assuming linear propagation if AliMUONRecoParam::MakeTrackCandidatesFast() return kTRUE):
ea94c18b 74 /// Start with segments station(1..) 4 or 5 then follow track in station 5 or 4.
89c8d66d 75 /// Good candidates are made of at least three clusters if both stations are requested (two otherwise).
76 /// Keep only best candidates or all of them according to the flag AliMUONRecoParam::TrackAllTracks().
b709ac13 77
208f139e 78 TClonesArray *segments;
89c8d66d 79 AliMUONObjectPair *segment;
ea94c18b 80 AliMUONTrack *track;
81 Int_t iCandidate = 0;
96ebe67e 82 Bool_t clusterFound;
8cc77df0 83
ea94c18b 84 AliDebug(1,"Enter MakeTrackCandidates");
8cc77df0 85
1fef78c8 86 // Unless we're doing combined tracking, we'll clusterize all stations at once
87 Int_t firstChamber(0);
88 Int_t lastChamber(9);
89
a0dc65b4 90 if (GetRecoParam()->CombineClusterTrackReco()) {
1fef78c8 91 // ... Here's the exception : ask the clustering to reconstruct
92 // clusters *only* in station 4 and 5 for combined tracking
93 firstChamber = 6;
94 }
95
96 for (Int_t i = firstChamber; i <= lastChamber; ++i )
97 {
35be7ed7 98 if (fClusterServer && GetRecoParam()->UseChamber(i)) fClusterServer->Clusterize(i, clusterStore, AliMpArea(), GetRecoParam());
1fef78c8 99 }
100
ea94c18b 101 // Loop over stations(1..) 5 and 4 and make track candidates
102 for (Int_t istat=4; istat>=3; istat--) {
103
208f139e 104 // Make segments in the station
9bf6860b 105 segments = MakeSegmentsBetweenChambers(clusterStore, 2*istat, 2*istat+1);
ea94c18b 106
107 // Loop over segments
89c8d66d 108 for (Int_t iSegment=0; iSegment<segments->GetEntriesFast(); iSegment++) {
ea94c18b 109 AliDebug(1,Form("Making primary candidate(1..) %d",++iCandidate));
89c8d66d 110 segment = (AliMUONObjectPair*) segments->UncheckedAt(iSegment);
ea94c18b 111
112 // Transform segments to tracks and put them at the end of fRecTracksPtr
89c8d66d 113 track = new ((*fRecTracksPtr)[fRecTracksPtr->GetLast()+1]) AliMUONTrack(segment,GetRecoParam()->GetBendingVertexDispersion());
ea94c18b 114 fNRecTracks++;
115
96ebe67e 116 // Look for compatible cluster(s) in the other station
89c8d66d 117 if (GetRecoParam()->MakeTrackCandidatesFast()) clusterFound = FollowLinearTrackInStation(*track, clusterStore, 7-istat);
118 else clusterFound = FollowTrackInStation(*track, clusterStore, 7-istat);
019df241 119
9bf6860b 120 // Remove track if no cluster found on a requested station
89c8d66d 121 // or abort tracking if there are too many candidates
6b191dea 122 if (GetRecoParam()->RequestStation(7-istat)) {
123 if (!clusterFound) {
124 fRecTracksPtr->Remove(track);
125 fNRecTracks--;
126 } else if (fNRecTracks > GetRecoParam()->GetMaxTrackCandidates()) {
87976389 127 AliError(Form("Too many track candidates (%d tracks). Stop tracking.", fNRecTracks));
6b191dea 128 return kFALSE;
129 }
130 } else {
131 if ((fNRecTracks + segments->GetEntriesFast() - iSegment - 1) > GetRecoParam()->GetMaxTrackCandidates()) {
4ea3f013 132 AliError(Form("Too many track candidates (%d tracks). Stop tracking.", fNRecTracks + segments->GetEntriesFast() - iSegment - 1));
6b191dea 133 return kFALSE;
134 }
ea94c18b 135 }
136
137 }
89c8d66d 138
ea94c18b 139 }
140
89c8d66d 141 // Keep all different tracks if required
142 if (GetRecoParam()->TrackAllTracks()) RemoveIdenticalTracks();
ea94c18b 143
89c8d66d 144 // Retrace tracks using Kalman filter and select them if needed
145 Int_t nCurrentTracks = fRecTracksPtr->GetLast()+1;
146 for (Int_t iRecTrack = 0; iRecTrack < nCurrentTracks; iRecTrack++) {
147 track = (AliMUONTrack*) fRecTracksPtr->UncheckedAt(iRecTrack);
ea94c18b 148
89c8d66d 149 // skip empty slots
150 if(!track) continue;
151
4ea3f013 152 // retrace tracks using Kalman filter and remove the ones for which extrap failed or that are out of limits
153 if (!RetraceTrack(*track,kTRUE) || !IsAcceptable(*((AliMUONTrackParam*)track->GetTrackParamAtCluster()->First()))) {
6b191dea 154 fRecTracksPtr->Remove(track);
155 fNRecTracks--;
ea94c18b 156 }
157
158 }
159
89c8d66d 160 // Keep only the best tracks if required
161 if (!GetRecoParam()->TrackAllTracks()) RemoveDoubleTracks();
3e9a7a05 162 else fRecTracksPtr->Compress();
ea94c18b 163
164 AliDebug(1,Form("Number of good candidates = %d",fNRecTracks));
165
89c8d66d 166 return kTRUE;
167
8cc77df0 168}
169
ea94c18b 170 //__________________________________________________________________________
89c8d66d 171Bool_t AliMUONTrackReconstructorK::MakeMoreTrackCandidates(AliMUONVClusterStore& clusterStore)
9bf6860b 172{
89c8d66d 173 /// To make extra track candidates assuming linear propagation:
9bf6860b 174 /// clustering is supposed to be already done
175 /// Start with segments made of 1 cluster in each of the stations 4 and 5 then follow track in remaining chambers.
89c8d66d 176 /// Good candidates are made of at least three clusters if both stations are requested (two otherwise).
9bf6860b 177 /// Keep only best candidates or all of them according to the flag fgkTrackAllTracks.
178
179 TClonesArray *segments;
89c8d66d 180 AliMUONObjectPair *segment;
9bf6860b 181 AliMUONTrack *track;
182 Int_t iCandidate = 0, iCurrentTrack, nCurrentTracks;
183 Int_t initialNRecTracks = fNRecTracks;
184 Bool_t clusterFound;
185
186 AliDebug(1,"Enter MakeMoreTrackCandidates");
187
188 // Double loop over chambers in stations(1..) 4 and 5 to make track candidates
189 for (Int_t ich1 = 6; ich1 <= 7; ich1++) {
190 for (Int_t ich2 = 8; ich2 <= 9; ich2++) {
191
192 // Make segments between ch1 and ch2
193 segments = MakeSegmentsBetweenChambers(clusterStore, ich1, ich2);
194
89c8d66d 195 /// Remove segments already attached to a track
196 RemoveUsedSegments(*segments);
197
9bf6860b 198 // Loop over segments
89c8d66d 199 for (Int_t iSegment=0; iSegment<segments->GetEntriesFast(); iSegment++) {
9bf6860b 200 AliDebug(1,Form("Making primary candidate(1..) %d",++iCandidate));
89c8d66d 201 segment = (AliMUONObjectPair*) segments->UncheckedAt(iSegment);
9bf6860b 202
203 // Transform segments to tracks and put them at the end of fRecTracksPtr
204 iCurrentTrack = fRecTracksPtr->GetLast()+1;
89c8d66d 205 track = new ((*fRecTracksPtr)[iCurrentTrack]) AliMUONTrack(segment,GetRecoParam()->GetBendingVertexDispersion());
9bf6860b 206 fNRecTracks++;
207
208 // Look for compatible cluster(s) in the second chamber of station 5
89c8d66d 209 clusterFound = FollowLinearTrackInChamber(*track, clusterStore, 17-ich2);
9bf6860b 210
211 // skip the original track in case it has been removed
a0dc65b4 212 if (GetRecoParam()->TrackAllTracks() && clusterFound) iCurrentTrack++;
9bf6860b 213
214 // loop over every new tracks
215 nCurrentTracks = fRecTracksPtr->GetLast()+1;
216 while (iCurrentTrack < nCurrentTracks) {
217 track = (AliMUONTrack*) fRecTracksPtr->UncheckedAt(iCurrentTrack);
218
219 // Look for compatible cluster(s) in the second chamber of station 4
89c8d66d 220 FollowLinearTrackInChamber(*track, clusterStore, 13-ich1);
9bf6860b 221
222 iCurrentTrack++;
223 }
224
89c8d66d 225 // abort tracking if there are too many candidates
6b191dea 226 if ((fNRecTracks + segments->GetEntriesFast() - iSegment - 1) > GetRecoParam()->GetMaxTrackCandidates()) {
87976389 227 AliError(Form("Too many track candidates (%d tracks). Stop tracking.", fNRecTracks + segments->GetEntriesFast() - iSegment - 1));
89c8d66d 228 return kFALSE;
229 }
230
9bf6860b 231 }
232
9bf6860b 233 }
234 }
235
89c8d66d 236 // Retrace tracks using Kalman filter (also compute track chi2) and select them
237 nCurrentTracks = fRecTracksPtr->GetLast()+1;
238 for (Int_t iRecTrack = initialNRecTracks; iRecTrack < nCurrentTracks; iRecTrack++) {
9bf6860b 239 track = (AliMUONTrack*) fRecTracksPtr->UncheckedAt(iRecTrack);
240
89c8d66d 241 // skip empty slots
242 if(!track) continue;
243
4ea3f013 244 // retrace tracks using Kalman filter and remove the ones for which extrap failed or that are out of limits
245 if (!RetraceTrack(*track,kTRUE) || !IsAcceptable(*((AliMUONTrackParam*)track->GetTrackParamAtCluster()->First()))) {
6b191dea 246 fRecTracksPtr->Remove(track);
247 fNRecTracks--;
9bf6860b 248 }
249
250 }
251
89c8d66d 252 // Keep only the best tracks if required
253 if (!GetRecoParam()->TrackAllTracks()) RemoveDoubleTracks();
254 else fRecTracksPtr->Compress();
9bf6860b 255
256 AliDebug(1,Form("Number of good candidates = %d",fNRecTracks));
257
89c8d66d 258 return kTRUE;
259
9bf6860b 260}
261
262 //__________________________________________________________________________
4ea3f013 263Bool_t AliMUONTrackReconstructorK::RetraceTrack(AliMUONTrack &trackCandidate, Bool_t resetSeed)
8cc77df0 264{
96ebe67e 265 /// Re-run the kalman filter from the most downstream cluster to the most uptream one
4ea3f013 266 /// Return kFALSE in case of failure (i.e. extrapolation problem)
ea94c18b 267 AliDebug(1,"Enter RetraceTrack");
268
7332f213 269 AliMUONTrackParam* lastTrackParam = (AliMUONTrackParam*) trackCandidate.GetTrackParamAtCluster()->Last();
ea94c18b 270
96ebe67e 271 // Reset the "seed" (= track parameters and their covariances at last cluster) if required
ea94c18b 272 if (resetSeed) {
7332f213 273
274 // parameters at last cluster
275 AliMUONVCluster* cluster2 = lastTrackParam->GetClusterPtr();
276 Double_t x2 = cluster2->GetX();
277 Double_t y2 = cluster2->GetY();
278 Double_t z2 = cluster2->GetZ();
279
280 // parameters at last but one cluster
281 AliMUONTrackParam* previousTrackParam = (AliMUONTrackParam*) trackCandidate.GetTrackParamAtCluster()->Before(lastTrackParam);
282 AliMUONVCluster* cluster1 = previousTrackParam->GetClusterPtr();
0e894e58 283 // make sure it is on the previous chamber (can have 2 clusters in the same chamber after "ComplementTrack")
284 if (cluster2->GetChamberId() == cluster1->GetChamberId()) {
285 previousTrackParam = (AliMUONTrackParam*) trackCandidate.GetTrackParamAtCluster()->Before(previousTrackParam);
286 cluster1 = previousTrackParam->GetClusterPtr();
287 }
7332f213 288 Double_t x1 = cluster1->GetX();
289 Double_t y1 = cluster1->GetY();
290 Double_t z1 = cluster1->GetZ();
291
292 // reset track parameters
293 Double_t dZ = z1 - z2;
294 lastTrackParam->SetNonBendingCoor(x2);
295 lastTrackParam->SetBendingCoor(y2);
296 lastTrackParam->SetZ(z2);
297 lastTrackParam->SetNonBendingSlope((x1 - x2) / dZ);
298 lastTrackParam->SetBendingSlope((y1 - y2) / dZ);
299 Double_t bendingImpact = y2 - z2 * lastTrackParam->GetBendingSlope();
9bf6860b 300 Double_t inverseBendingMomentum = 1. / AliMUONTrackExtrap::GetBendingMomentumFromImpactParam(bendingImpact);
301 lastTrackParam->SetInverseBendingMomentum(inverseBendingMomentum);
7332f213 302
303 // => Reset track parameter covariances at last cluster (as if the other clusters did not exist)
304 TMatrixD lastParamCov(5,5);
305 lastParamCov.Zero();
ea94c18b 306 // Non bending plane
7332f213 307 lastParamCov(0,0) = cluster2->GetErrX2();
89c8d66d 308 lastParamCov(0,1) = - cluster2->GetErrX2() / dZ;
309 lastParamCov(1,0) = lastParamCov(0,1);
310 lastParamCov(1,1) = ( 1000. * cluster1->GetErrX2() + cluster2->GetErrX2() ) / dZ / dZ;
ea94c18b 311 // Bending plane
7332f213 312 lastParamCov(2,2) = cluster2->GetErrY2();
89c8d66d 313 lastParamCov(2,3) = - cluster2->GetErrY2() / dZ;
314 lastParamCov(3,2) = lastParamCov(2,3);
315 lastParamCov(3,3) = ( 1000. * cluster1->GetErrY2() + cluster2->GetErrY2() ) / dZ / dZ;
9bf6860b 316 // Inverse bending momentum (vertex resolution + bending slope resolution + 10% error on dipole parameters+field)
89c8d66d 317 if (AliMUONTrackExtrap::IsFieldON()) {
318 lastParamCov(4,4) = ((GetRecoParam()->GetBendingVertexDispersion() *
319 GetRecoParam()->GetBendingVertexDispersion() +
320 (z1 * z1 * cluster2->GetErrY2() + z2 * z2 * 1000. * cluster1->GetErrY2()) / dZ / dZ) /
321 bendingImpact / bendingImpact + 0.1 * 0.1) * inverseBendingMomentum * inverseBendingMomentum;
322 lastParamCov(2,4) = z1 * cluster2->GetErrY2() * inverseBendingMomentum / bendingImpact / dZ;
323 lastParamCov(4,2) = lastParamCov(2,4);
324 lastParamCov(3,4) = - (z1 * cluster2->GetErrY2() + z2 * 1000. * cluster1->GetErrY2()) *
325 inverseBendingMomentum / bendingImpact / dZ / dZ;
326 lastParamCov(4,3) = lastParamCov(3,4);
327 } else lastParamCov(4,4) = inverseBendingMomentum*inverseBendingMomentum;
7332f213 328 lastTrackParam->SetCovariances(lastParamCov);
ea94c18b 329
330 // Reset the track chi2
7332f213 331 lastTrackParam->SetTrackChi2(0.);
ea94c18b 332
333 }
334
335 // Redo the tracking
4ea3f013 336 return RetracePartialTrack(trackCandidate, lastTrackParam);
ea94c18b 337
338}
8cc77df0 339
ea94c18b 340 //__________________________________________________________________________
4ea3f013 341Bool_t AliMUONTrackReconstructorK::RetracePartialTrack(AliMUONTrack &trackCandidate, const AliMUONTrackParam* startingTrackParam)
ea94c18b 342{
96ebe67e 343 /// Re-run the kalman filter from the cluster attached to startingTrackParam to the most uptream cluster
4ea3f013 344 /// Return kFALSE in case of failure (i.e. extrapolation problem)
ea94c18b 345 AliDebug(1,"Enter RetracePartialTrack");
346
347 // Printout for debuging
348 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
96ebe67e 349 cout << "RetracePartialTrack: track chi2 before re-tracking: " << trackCandidate.GetGlobalChi2() << endl;
ea94c18b 350 }
351
352 // Reset the track chi2
96ebe67e 353 trackCandidate.SetGlobalChi2(startingTrackParam->GetTrackChi2());
ea94c18b 354
96ebe67e 355 // loop over attached clusters until the first one and recompute track parameters and covariances using kalman filter
4ea3f013 356 Bool_t extrapStatus = kTRUE;
96ebe67e 357 Int_t expectedChamber = startingTrackParam->GetClusterPtr()->GetChamberId() - 1;
ea94c18b 358 Int_t currentChamber;
96ebe67e 359 Double_t addChi2TrackAtCluster;
360 AliMUONTrackParam* trackParamAtCluster = (AliMUONTrackParam*) trackCandidate.GetTrackParamAtCluster()->Before(startingTrackParam);
361 while (trackParamAtCluster) {
ea94c18b 362
363 // reset track parameters and their covariances
96ebe67e 364 trackParamAtCluster->SetParameters(startingTrackParam->GetParameters());
365 trackParamAtCluster->SetZ(startingTrackParam->GetZ());
366 trackParamAtCluster->SetCovariances(startingTrackParam->GetCovariances());
ea94c18b 367
368 // add MCS effect
4663da9f 369 AliMUONTrackExtrap::AddMCSEffect(trackParamAtCluster,AliMUONConstants::ChamberThicknessInX0(expectedChamber+1),-1.);
ea94c18b 370
371 // reset propagator for smoother
a0dc65b4 372 if (GetRecoParam()->UseSmoother()) trackParamAtCluster->ResetPropagator();
ea94c18b 373
9bf6860b 374 // add MCS in missing chambers if any
96ebe67e 375 currentChamber = trackParamAtCluster->GetClusterPtr()->GetChamberId();
ea94c18b 376 while (currentChamber < expectedChamber) {
377 // extrapolation to the missing chamber (update the propagator)
4ea3f013 378 if (!AliMUONTrackExtrap::ExtrapToZCov(trackParamAtCluster, AliMUONConstants::DefaultChamberZ(expectedChamber),
379 GetRecoParam()->UseSmoother())) extrapStatus = kFALSE;
ea94c18b 380 // add MCS effect
4663da9f 381 AliMUONTrackExtrap::AddMCSEffect(trackParamAtCluster,AliMUONConstants::ChamberThicknessInX0(expectedChamber),-1.);
ea94c18b 382 expectedChamber--;
383 }
384
96ebe67e 385 // extrapolation to the plane of the cluster attached to the current trackParamAtCluster (update the propagator)
4ea3f013 386 if (!AliMUONTrackExtrap::ExtrapToZCov(trackParamAtCluster, trackParamAtCluster->GetClusterPtr()->GetZ(),
387 GetRecoParam()->UseSmoother())) extrapStatus = kFALSE;
ea94c18b 388
a0dc65b4 389 if (GetRecoParam()->UseSmoother()) {
ea94c18b 390 // save extrapolated parameters for smoother
96ebe67e 391 trackParamAtCluster->SetExtrapParameters(trackParamAtCluster->GetParameters());
ea94c18b 392
393 // save extrapolated covariance matrix for smoother
96ebe67e 394 trackParamAtCluster->SetExtrapCovariances(trackParamAtCluster->GetCovariances());
ea94c18b 395 }
396
9bf6860b 397 // Compute new track parameters using kalman filter
96ebe67e 398 addChi2TrackAtCluster = RunKalmanFilter(*trackParamAtCluster);
ea94c18b 399
400 // Update the track chi2
96ebe67e 401 trackCandidate.SetGlobalChi2(trackCandidate.GetGlobalChi2() + addChi2TrackAtCluster);
402 trackParamAtCluster->SetTrackChi2(trackCandidate.GetGlobalChi2());
ea94c18b 403
96ebe67e 404 // prepare next step
405 expectedChamber = currentChamber - 1;
406 startingTrackParam = trackParamAtCluster;
407 trackParamAtCluster = (AliMUONTrackParam*) (trackCandidate.GetTrackParamAtCluster()->Before(startingTrackParam));
ea94c18b 408 }
409
410 // Printout for debuging
411 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
96ebe67e 412 cout << "RetracePartialTrack: track chi2 after re-tracking: " << trackCandidate.GetGlobalChi2() << endl;
ea94c18b 413 }
414
4ea3f013 415 // set global chi2 to max value in case of problem during track extrapolation
416 if (!extrapStatus) trackCandidate.SetGlobalChi2(2.*AliMUONTrack::MaxChi2());
417 return extrapStatus;
418
ea94c18b 419}
8cc77df0 420
ea94c18b 421 //__________________________________________________________________________
89c8d66d 422Bool_t AliMUONTrackReconstructorK::FollowTracks(AliMUONVClusterStore& clusterStore)
ea94c18b 423{
424 /// Follow tracks in stations(1..) 3, 2 and 1
425 AliDebug(1,"Enter FollowTracks");
426
427 AliMUONTrack *track;
428 Int_t currentNRecTracks;
ea94c18b 429
430 for (Int_t station = 2; station >= 0; station--) {
431
432 // Save the actual number of reconstructed track in case of
433 // tracks are added or suppressed during the tracking procedure
434 // !! Do not compress fRecTracksPtr until the end of the loop over tracks !!
435 currentNRecTracks = fNRecTracks;
436
437 for (Int_t iRecTrack = 0; iRecTrack <currentNRecTracks; iRecTrack++) {
438 AliDebug(1,Form("FollowTracks: track candidate(1..) %d", iRecTrack+1));
439
440 track = (AliMUONTrack*) fRecTracksPtr->UncheckedAt(iRecTrack);
441
96ebe67e 442 // Look for compatible cluster(s) in station(0..) "station"
9bf6860b 443 if (!FollowTrackInStation(*track, clusterStore, station)) {
444
445 // Try to recover track if required
a0dc65b4 446 if (GetRecoParam()->RecoverTracks()) {
9bf6860b 447
448 // work on a copy of the track if this station is not required
449 // to keep the case where no cluster is reconstructed as a possible candidate
a0dc65b4 450 if (!GetRecoParam()->RequestStation(station)) {
9bf6860b 451 track = new ((*fRecTracksPtr)[fRecTracksPtr->GetLast()+1]) AliMUONTrack(*track);
452 fNRecTracks++;
453 }
454
455 // try to recover
456 if (!RecoverTrack(*track, clusterStore, station)) {
457 // remove track if no cluster found
458 fRecTracksPtr->Remove(track);
459 fNRecTracks--;
460 }
461
a0dc65b4 462 } else if (GetRecoParam()->RequestStation(station)) {
9bf6860b 463 // remove track if no cluster found
464 fRecTracksPtr->Remove(track);
465 fNRecTracks--;
466 }
467
8cc77df0 468 }
ea94c18b 469
89c8d66d 470 // abort tracking if there are too many candidates
6b191dea 471 if (GetRecoParam()->RequestStation(station)) {
472 if (fNRecTracks > GetRecoParam()->GetMaxTrackCandidates()) {
87976389 473 AliError(Form("Too many track candidates (%d tracks). Stop tracking.", fNRecTracks));
6b191dea 474 return kFALSE;
475 }
476 } else {
477 if ((fNRecTracks + currentNRecTracks - iRecTrack - 1) > GetRecoParam()->GetMaxTrackCandidates()) {
87976389 478 AliError(Form("Too many track candidates (%d tracks). Stop tracking.", fNRecTracks + currentNRecTracks - iRecTrack - 1));
6b191dea 479 return kFALSE;
480 }
89c8d66d 481 }
482
8cc77df0 483 }
ea94c18b 484
485 fRecTracksPtr->Compress(); // this is essential before checking tracks
486
ea94c18b 487 // Keep only the best tracks if required
a0dc65b4 488 if (!GetRecoParam()->TrackAllTracks()) RemoveDoubleTracks();
ea94c18b 489
490 }
491
89c8d66d 492 return kTRUE;
493
ea94c18b 494}
b0e4c598 495
ea94c18b 496 //__________________________________________________________________________
9bf6860b 497Bool_t AliMUONTrackReconstructorK::FollowTrackInChamber(AliMUONTrack &trackCandidate, AliMUONVClusterStore& clusterStore, Int_t nextChamber)
498{
499 /// Follow trackCandidate in chamber(0..) nextChamber and search for compatible cluster(s)
500 /// Keep all possibilities or only the best one(s) according to the flag fgkTrackAllTracks:
501 /// kTRUE: duplicate "trackCandidate" if there are several possibilities and add the new tracks at the end of
502 /// fRecTracksPtr to avoid conficts with other track candidates at this current stage of the tracking procedure.
503 /// Remove the obsolete "trackCandidate" at the end.
504 /// kFALSE: add only the best cluster(s) to the "trackCandidate". Try to add a couple of clusters in priority.
505 /// return kTRUE if new cluster(s) have been found (otherwise return kFALSE)
506 AliDebug(1,Form("Enter FollowTrackInChamber(1..) %d", nextChamber+1));
507
9bf6860b 508 Double_t chi2OfCluster;
a0dc65b4 509 Double_t maxChi2OfCluster = 2. * GetRecoParam()->GetSigmaCutForTracking() *
510 GetRecoParam()->GetSigmaCutForTracking(); // 2 because 2 quantities in chi2
9bf6860b 511 Double_t addChi2TrackAtCluster;
4ea3f013 512 Double_t bestAddChi2TrackAtCluster = AliMUONTrack::MaxChi2();
9bf6860b 513 Bool_t foundOneCluster = kFALSE;
514 AliMUONTrack *newTrack = 0x0;
515 AliMUONVCluster *cluster;
516 AliMUONTrackParam extrapTrackParamAtCh;
517 AliMUONTrackParam extrapTrackParamAtCluster;
518 AliMUONTrackParam bestTrackParamAtCluster;
519
520 // Get track parameters according to the propagation direction
521 if (nextChamber > 7) extrapTrackParamAtCh = *(AliMUONTrackParam*)trackCandidate.GetTrackParamAtCluster()->Last();
522 else extrapTrackParamAtCh = *(AliMUONTrackParam*)trackCandidate.GetTrackParamAtCluster()->First();
523
524 // Printout for debuging
525 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 2) || (AliLog::GetGlobalDebugLevel() >= 2)) {
526 cout<<endl<<"Track parameters and covariances at first cluster:"<<endl;
527 extrapTrackParamAtCh.GetParameters().Print();
528 extrapTrackParamAtCh.GetCovariances().Print();
529 }
530
531 // Add MCS effect
4663da9f 532 Int_t currentChamber = extrapTrackParamAtCh.GetClusterPtr()->GetChamberId();
533 AliMUONTrackExtrap::AddMCSEffect(&extrapTrackParamAtCh,AliMUONConstants::ChamberThicknessInX0(currentChamber),-1.);
9bf6860b 534
535 // reset propagator for smoother
a0dc65b4 536 if (GetRecoParam()->UseSmoother()) extrapTrackParamAtCh.ResetPropagator();
9bf6860b 537
538 // Add MCS in the missing chamber(s) if any
9bf6860b 539 while (currentChamber > nextChamber + 1) {
540 // extrapolation to the missing chamber
541 currentChamber--;
4ea3f013 542 if (!AliMUONTrackExtrap::ExtrapToZCov(&extrapTrackParamAtCh, AliMUONConstants::DefaultChamberZ(currentChamber),
543 GetRecoParam()->UseSmoother())) return kFALSE;
9bf6860b 544 // add MCS effect
4663da9f 545 AliMUONTrackExtrap::AddMCSEffect(&extrapTrackParamAtCh,AliMUONConstants::ChamberThicknessInX0(currentChamber),-1.);
9bf6860b 546 }
547
548 //Extrapolate trackCandidate to chamber
4ea3f013 549 if (!AliMUONTrackExtrap::ExtrapToZCov(&extrapTrackParamAtCh, AliMUONConstants::DefaultChamberZ(nextChamber),
550 GetRecoParam()->UseSmoother())) return kFALSE;
9bf6860b 551
552 // Printout for debuging
553 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 2) || (AliLog::GetGlobalDebugLevel() >= 2)) {
554 cout<<endl<<"Track parameters and covariances at first cluster extrapolated to z = "<<AliMUONConstants::DefaultChamberZ(nextChamber)<<":"<<endl;
555 extrapTrackParamAtCh.GetParameters().Print();
556 extrapTrackParamAtCh.GetCovariances().Print();
557 }
558
559 // Printout for debuging
560 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
561 cout << "FollowTrackInChamber: look for clusters in chamber(1..): " << nextChamber+1 << endl;
562 }
563
564 // Ask the clustering to reconstruct new clusters around the track position in the current chamber
565 // except for station 4 and 5 that are already entirely clusterized
a0dc65b4 566 if (GetRecoParam()->CombineClusterTrackReco()) {
9bf6860b 567 if (nextChamber < 6) AskForNewClustersInChamber(extrapTrackParamAtCh, clusterStore, nextChamber);
568 }
569
570 // Create iterators to loop over clusters in both chambers
571 TIter next(clusterStore.CreateChamberIterator(nextChamber,nextChamber));
572
573 // look for candidates in chamber
574 while ( ( cluster = static_cast<AliMUONVCluster*>(next()) ) ) {
575
576 // try to add the current cluster fast
577 if (!TryOneClusterFast(extrapTrackParamAtCh, cluster)) continue;
578
579 // try to add the current cluster accuratly
580 chi2OfCluster = TryOneCluster(extrapTrackParamAtCh, cluster, extrapTrackParamAtCluster,
a0dc65b4 581 GetRecoParam()->UseSmoother());
9bf6860b 582
583 // if good chi2 then consider to add cluster
584 if (chi2OfCluster < maxChi2OfCluster) {
9bf6860b 585
a0dc65b4 586 if (GetRecoParam()->UseSmoother()) {
9bf6860b 587 // save extrapolated parameters for smoother
588 extrapTrackParamAtCluster.SetExtrapParameters(extrapTrackParamAtCluster.GetParameters());
589
590 // save extrapolated covariance matrix for smoother
591 extrapTrackParamAtCluster.SetExtrapCovariances(extrapTrackParamAtCluster.GetCovariances());
592 }
593
594 // Compute new track parameters including new cluster using kalman filter
595 addChi2TrackAtCluster = RunKalmanFilter(extrapTrackParamAtCluster);
596
6b191dea 597 // skip track out of limits
598 if (!IsAcceptable(extrapTrackParamAtCluster)) continue;
89c8d66d 599
600 // remember a cluster was found
601 foundOneCluster = kTRUE;
602
603 // Printout for debuging
604 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
605 cout << "FollowTrackInChamber: found one cluster in chamber(1..): " << nextChamber+1
606 << " (Chi2 = " << chi2OfCluster << ")" << endl;
607 cluster->Print();
608 }
9bf6860b 609
a0dc65b4 610 if (GetRecoParam()->TrackAllTracks()) {
9bf6860b 611 // copy trackCandidate into a new track put at the end of fRecTracksPtr and add the new cluster
612 newTrack = new ((*fRecTracksPtr)[fRecTracksPtr->GetLast()+1]) AliMUONTrack(trackCandidate);
613 UpdateTrack(*newTrack,extrapTrackParamAtCluster,addChi2TrackAtCluster);
614 fNRecTracks++;
615
616 // Printout for debuging
617 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
618 cout << "FollowTrackInChamber: added one cluster in chamber(1..): " << nextChamber+1 << endl;
619 if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
620 }
621
622 } else if (addChi2TrackAtCluster < bestAddChi2TrackAtCluster) {
623 // keep track of the best cluster
624 bestAddChi2TrackAtCluster = addChi2TrackAtCluster;
625 bestTrackParamAtCluster = extrapTrackParamAtCluster;
626 }
627
628 }
629
630 }
631
632 // fill out the best track if required else clean up the fRecTracksPtr array
a0dc65b4 633 if (!GetRecoParam()->TrackAllTracks()) {
9bf6860b 634 if (foundOneCluster) {
635 UpdateTrack(trackCandidate,bestTrackParamAtCluster,bestAddChi2TrackAtCluster);
636
637 // Printout for debuging
638 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
639 cout << "FollowTrackInChamber: added the best cluster in chamber(1..): " << bestTrackParamAtCluster.GetClusterPtr()->GetChamberId()+1 << endl;
640 if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
641 }
642
643 } else return kFALSE;
644
645 } else if (foundOneCluster) {
646
647 // remove obsolete track
648 fRecTracksPtr->Remove(&trackCandidate);
649 fNRecTracks--;
650
651 } else return kFALSE;
652
653 return kTRUE;
654
655}
656
657 //__________________________________________________________________________
1fef78c8 658Bool_t AliMUONTrackReconstructorK::FollowTrackInStation(AliMUONTrack &trackCandidate, AliMUONVClusterStore& clusterStore, Int_t nextStation)
ea94c18b 659{
96ebe67e 660 /// Follow trackCandidate in station(0..) nextStation and search for compatible cluster(s)
ea94c18b 661 /// Keep all possibilities or only the best one(s) according to the flag fgkTrackAllTracks:
662 /// kTRUE: duplicate "trackCandidate" if there are several possibilities and add the new tracks at the end of
663 /// fRecTracksPtr to avoid conficts with other track candidates at this current stage of the tracking procedure.
664 /// Remove the obsolete "trackCandidate" at the end.
96ebe67e 665 /// kFALSE: add only the best cluster(s) to the "trackCandidate". Try to add a couple of clusters in priority.
666 /// return kTRUE if new cluster(s) have been found (otherwise return kFALSE)
ea94c18b 667 AliDebug(1,Form("Enter FollowTrackInStation(1..) %d", nextStation+1));
668
669 // Order the chamber according to the propagation direction (tracking starts with chamber 2):
670 // - nextStation == station(1...) 5 => forward propagation
671 // - nextStation < station(1...) 5 => backward propagation
672 Int_t ch1, ch2;
673 if (nextStation==4) {
674 ch1 = 2*nextStation+1;
675 ch2 = 2*nextStation;
676 } else {
677 ch1 = 2*nextStation;
678 ch2 = 2*nextStation+1;
679 }
680
96ebe67e 681 Double_t chi2OfCluster;
a0dc65b4 682 Double_t maxChi2OfCluster = 2. * GetRecoParam()->GetSigmaCutForTracking() *
683 GetRecoParam()->GetSigmaCutForTracking(); // 2 because 2 quantities in chi2
96ebe67e 684 Double_t addChi2TrackAtCluster1;
685 Double_t addChi2TrackAtCluster2;
4ea3f013 686 Double_t bestAddChi2TrackAtCluster1 = AliMUONTrack::MaxChi2();
687 Double_t bestAddChi2TrackAtCluster2 = AliMUONTrack::MaxChi2();
96ebe67e 688 Bool_t foundOneCluster = kFALSE;
689 Bool_t foundTwoClusters = kFALSE;
ea94c18b 690 AliMUONTrack *newTrack = 0x0;
96ebe67e 691 AliMUONVCluster *clusterCh1, *clusterCh2;
ea94c18b 692 AliMUONTrackParam extrapTrackParam;
7332f213 693 AliMUONTrackParam extrapTrackParamAtCh;
96ebe67e 694 AliMUONTrackParam extrapTrackParamAtCluster1;
695 AliMUONTrackParam extrapTrackParamAtCluster2;
696 AliMUONTrackParam bestTrackParamAtCluster1;
697 AliMUONTrackParam bestTrackParamAtCluster2;
698
7332f213 699 // Get track parameters according to the propagation direction
7332f213 700 if (nextStation==4) extrapTrackParamAtCh = *(AliMUONTrackParam*)trackCandidate.GetTrackParamAtCluster()->Last();
701 else extrapTrackParamAtCh = *(AliMUONTrackParam*)trackCandidate.GetTrackParamAtCluster()->First();
ea94c18b 702
9bf6860b 703 // Printout for debuging
704 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 2) || (AliLog::GetGlobalDebugLevel() >= 2)) {
705 cout<<endl<<"Track parameters and covariances at first cluster:"<<endl;
706 extrapTrackParamAtCh.GetParameters().Print();
707 extrapTrackParamAtCh.GetCovariances().Print();
708 }
709
ea94c18b 710 // Add MCS effect
4663da9f 711 Int_t currentChamber = extrapTrackParamAtCh.GetClusterPtr()->GetChamberId();
712 AliMUONTrackExtrap::AddMCSEffect(&extrapTrackParamAtCh,AliMUONConstants::ChamberThicknessInX0(currentChamber),-1.);
ea94c18b 713
714 // reset propagator for smoother
a0dc65b4 715 if (GetRecoParam()->UseSmoother()) extrapTrackParamAtCh.ResetPropagator();
ea94c18b 716
9bf6860b 717 // Add MCS in the missing chamber(s) if any
9bf6860b 718 while (ch1 < ch2 && currentChamber > ch2 + 1) {
ea94c18b 719 // extrapolation to the missing chamber
9bf6860b 720 currentChamber--;
4ea3f013 721 if (!AliMUONTrackExtrap::ExtrapToZCov(&extrapTrackParamAtCh, AliMUONConstants::DefaultChamberZ(currentChamber),
722 GetRecoParam()->UseSmoother())) return kFALSE;
ea94c18b 723 // add MCS effect
4663da9f 724 AliMUONTrackExtrap::AddMCSEffect(&extrapTrackParamAtCh,AliMUONConstants::ChamberThicknessInX0(currentChamber),-1.);
ea94c18b 725 }
726
727 //Extrapolate trackCandidate to chamber "ch2"
4ea3f013 728 if (!AliMUONTrackExtrap::ExtrapToZCov(&extrapTrackParamAtCh, AliMUONConstants::DefaultChamberZ(ch2),
729 GetRecoParam()->UseSmoother())) return kFALSE;
ea94c18b 730
731 // Printout for debuging
732 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 2) || (AliLog::GetGlobalDebugLevel() >= 2)) {
9bf6860b 733 cout<<endl<<"Track parameters and covariances at first cluster extrapolated to z = "<<AliMUONConstants::DefaultChamberZ(ch2)<<":"<<endl;
734 extrapTrackParamAtCh.GetParameters().Print();
019df241 735 extrapTrackParamAtCh.GetCovariances().Print();
ea94c18b 736 }
737
738 // Printout for debuging
739 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
96ebe67e 740 cout << "FollowTrackInStation: look for clusters in chamber(1..): " << ch2+1 << endl;
ea94c18b 741 }
742
1fef78c8 743 // Ask the clustering to reconstruct new clusters around the track position in the current station
744 // except for station 4 and 5 that are already entirely clusterized
a0dc65b4 745 if (GetRecoParam()->CombineClusterTrackReco()) {
1fef78c8 746 if (nextStation < 3) AskForNewClustersInStation(extrapTrackParamAtCh, clusterStore, nextStation);
747 }
748
4ea3f013 749 Int_t nClusters = clusterStore.GetSize();
750 Bool_t *clusterCh1Used = new Bool_t[nClusters];
751 for (Int_t i = 0; i < nClusters; i++) clusterCh1Used[i] = kFALSE;
752 Int_t iCluster1;
753
96ebe67e 754 // Create iterators to loop over clusters in both chambers
755 TIter nextInCh1(clusterStore.CreateChamberIterator(ch1,ch1));
756 TIter nextInCh2(clusterStore.CreateChamberIterator(ch2,ch2));
757
758 // look for candidates in chamber 2
759 while ( ( clusterCh2 = static_cast<AliMUONVCluster*>(nextInCh2()) ) ) {
ea94c18b 760
96ebe67e 761 // try to add the current cluster fast
762 if (!TryOneClusterFast(extrapTrackParamAtCh, clusterCh2)) continue;
019df241 763
96ebe67e 764 // try to add the current cluster accuratly
765 chi2OfCluster = TryOneCluster(extrapTrackParamAtCh, clusterCh2, extrapTrackParamAtCluster2,
a0dc65b4 766 GetRecoParam()->UseSmoother());
ea94c18b 767
96ebe67e 768 // if good chi2 then try to attach a cluster in the other chamber too
769 if (chi2OfCluster < maxChi2OfCluster) {
ea94c18b 770
a0dc65b4 771 if (GetRecoParam()->UseSmoother()) {
ea94c18b 772 // save extrapolated parameters for smoother
96ebe67e 773 extrapTrackParamAtCluster2.SetExtrapParameters(extrapTrackParamAtCluster2.GetParameters());
ea94c18b 774
775 // save extrapolated covariance matrix for smoother
96ebe67e 776 extrapTrackParamAtCluster2.SetExtrapCovariances(extrapTrackParamAtCluster2.GetCovariances());
ea94c18b 777 }
778
96ebe67e 779 // Compute new track parameters including "clusterCh2" using kalman filter
780 addChi2TrackAtCluster2 = RunKalmanFilter(extrapTrackParamAtCluster2);
ea94c18b 781
6b191dea 782 // skip track out of limits
783 if (!IsAcceptable(extrapTrackParamAtCluster2)) continue;
89c8d66d 784
785 // Printout for debuging
786 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
787 cout << "FollowTrackInStation: found one cluster in chamber(1..): " << ch2+1
788 << " (Chi2 = " << chi2OfCluster << ")" << endl;
789 clusterCh2->Print();
790 cout << " look for second clusters in chamber(1..): " << ch1+1 << " ..." << endl;
791 }
792
ea94c18b 793 // copy new track parameters for next step
96ebe67e 794 extrapTrackParam = extrapTrackParamAtCluster2;
ea94c18b 795
796 // add MCS effect
4663da9f 797 AliMUONTrackExtrap::AddMCSEffect(&extrapTrackParam,AliMUONConstants::ChamberThicknessInX0(ch2),-1.);
ea94c18b 798
799 // reset propagator for smoother
a0dc65b4 800 if (GetRecoParam()->UseSmoother()) extrapTrackParam.ResetPropagator();
ea94c18b 801
019df241 802 //Extrapolate track parameters to chamber "ch1"
4ea3f013 803 Bool_t normalExtrap = AliMUONTrackExtrap::ExtrapToZCov(&extrapTrackParam, AliMUONConstants::DefaultChamberZ(ch1),
804 GetRecoParam()->UseSmoother());
019df241 805
96ebe67e 806 // reset cluster iterator of chamber 1
807 nextInCh1.Reset();
808 iCluster1 = -1;
809
810 // look for second candidates in chamber 1
89c8d66d 811 Bool_t foundSecondCluster = kFALSE;
4ea3f013 812 if (normalExtrap) while ( ( clusterCh1 = static_cast<AliMUONVCluster*>(nextInCh1()) ) ) {
96ebe67e 813 iCluster1++;
ea94c18b 814
96ebe67e 815 // try to add the current cluster fast
816 if (!TryOneClusterFast(extrapTrackParam, clusterCh1)) continue;
019df241 817
96ebe67e 818 // try to add the current cluster accuratly
819 chi2OfCluster = TryOneCluster(extrapTrackParam, clusterCh1, extrapTrackParamAtCluster1,
a0dc65b4 820 GetRecoParam()->UseSmoother());
019df241 821
96ebe67e 822 // if good chi2 then consider to add the 2 clusters to the "trackCandidate"
823 if (chi2OfCluster < maxChi2OfCluster) {
019df241 824
a0dc65b4 825 if (GetRecoParam()->UseSmoother()) {
ea94c18b 826 // save extrapolated parameters for smoother
96ebe67e 827 extrapTrackParamAtCluster1.SetExtrapParameters(extrapTrackParamAtCluster1.GetParameters());
ea94c18b 828
829 // save extrapolated covariance matrix for smoother
96ebe67e 830 extrapTrackParamAtCluster1.SetExtrapCovariances(extrapTrackParamAtCluster1.GetCovariances());
ea94c18b 831 }
832
96ebe67e 833 // Compute new track parameters including "clusterCh1" using kalman filter
834 addChi2TrackAtCluster1 = RunKalmanFilter(extrapTrackParamAtCluster1);
ea94c18b 835
6b191dea 836 // skip track out of limits
837 if (!IsAcceptable(extrapTrackParamAtCluster1)) continue;
89c8d66d 838
839 // remember a second cluster was found
840 foundSecondCluster = kTRUE;
841 foundTwoClusters = kTRUE;
842
843 // Printout for debuging
844 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
845 cout << "FollowTrackInStation: found one cluster in chamber(1..): " << ch1+1
846 << " (Chi2 = " << chi2OfCluster << ")" << endl;
847 clusterCh1->Print();
848 }
9bf6860b 849
a0dc65b4 850 if (GetRecoParam()->TrackAllTracks()) {
96ebe67e 851 // copy trackCandidate into a new track put at the end of fRecTracksPtr and add the new clusters
ea94c18b 852 newTrack = new ((*fRecTracksPtr)[fRecTracksPtr->GetLast()+1]) AliMUONTrack(trackCandidate);
96ebe67e 853 UpdateTrack(*newTrack,extrapTrackParamAtCluster1,extrapTrackParamAtCluster2,addChi2TrackAtCluster1,addChi2TrackAtCluster2);
ea94c18b 854 fNRecTracks++;
855
96ebe67e 856 // Tag clusterCh1 as used
857 clusterCh1Used[iCluster1] = kTRUE;
ea94c18b 858
859 // Printout for debuging
860 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
96ebe67e 861 cout << "FollowTrackInStation: added two clusters in station(1..): " << nextStation+1 << endl;
ea94c18b 862 if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
863 }
864
96ebe67e 865 } else if (addChi2TrackAtCluster1+addChi2TrackAtCluster2 < bestAddChi2TrackAtCluster1+bestAddChi2TrackAtCluster2) {
866 // keep track of the best couple of clusters
867 bestAddChi2TrackAtCluster1 = addChi2TrackAtCluster1;
868 bestAddChi2TrackAtCluster2 = addChi2TrackAtCluster2;
869 bestTrackParamAtCluster1 = extrapTrackParamAtCluster1;
870 bestTrackParamAtCluster2 = extrapTrackParamAtCluster2;
ea94c18b 871 }
872
b0e4c598 873 }
ea94c18b 874
8cc77df0 875 }
ea94c18b 876
96ebe67e 877 // if no clusterCh1 found then consider to add clusterCh2 only
878 if (!foundSecondCluster) {
89c8d66d 879
880 // remember a cluster was found
96ebe67e 881 foundOneCluster = kTRUE;
ea94c18b 882
a0dc65b4 883 if (GetRecoParam()->TrackAllTracks()) {
96ebe67e 884 // copy trackCandidate into a new track put at the end of fRecTracksPtr and add the new cluster
ea94c18b 885 newTrack = new ((*fRecTracksPtr)[fRecTracksPtr->GetLast()+1]) AliMUONTrack(trackCandidate);
96ebe67e 886 UpdateTrack(*newTrack,extrapTrackParamAtCluster2,addChi2TrackAtCluster2);
ea94c18b 887 fNRecTracks++;
888
ea94c18b 889 // Printout for debuging
890 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
96ebe67e 891 cout << "FollowTrackInStation: added one cluster in chamber(1..): " << ch2+1 << endl;
ea94c18b 892 if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
893 }
894
96ebe67e 895 } else if (!foundTwoClusters && addChi2TrackAtCluster2 < bestAddChi2TrackAtCluster1) {
896 // keep track of the best single cluster except if a couple of clusters has already been found
897 bestAddChi2TrackAtCluster1 = addChi2TrackAtCluster2;
898 bestTrackParamAtCluster1 = extrapTrackParamAtCluster2;
ea94c18b 899 }
900
901 }
902
8cc77df0 903 }
ea94c18b 904
905 }
906
907 // look for candidates in chamber 1 not already attached to a track
96ebe67e 908 // if we want to keep all possible tracks or if no good couple of clusters has been found
a0dc65b4 909 if (GetRecoParam()->TrackAllTracks() || !foundTwoClusters) {
ea94c18b 910
ea94c18b 911 // add MCS effect for next step
4663da9f 912 AliMUONTrackExtrap::AddMCSEffect(&extrapTrackParamAtCh,AliMUONConstants::ChamberThicknessInX0(ch2),-1.);
019df241 913
914 //Extrapolate trackCandidate to chamber "ch1"
4ea3f013 915 Bool_t normalExtrap = AliMUONTrackExtrap::ExtrapToZCov(&extrapTrackParamAtCh, AliMUONConstants::DefaultChamberZ(ch1),
916 GetRecoParam()->UseSmoother());
019df241 917
9bf6860b 918 // Printout for debuging
919 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 2) || (AliLog::GetGlobalDebugLevel() >= 2)) {
920 cout<<endl<<"Track parameters and covariances at first cluster extrapolated to z = "<<AliMUONConstants::DefaultChamberZ(ch1)<<":"<<endl;
921 extrapTrackParamAtCh.GetParameters().Print();
922 extrapTrackParamAtCh.GetCovariances().Print();
923 }
924
925 // Printout for debuging
926 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
927 cout << "FollowTrackInStation: look for single clusters in chamber(1..): " << ch1+1 << endl;
928 }
929
96ebe67e 930 // reset cluster iterator of chamber 1
931 nextInCh1.Reset();
932 iCluster1 = -1;
933
934 // look for second candidates in chamber 1
4ea3f013 935 if (normalExtrap) while ( ( clusterCh1 = static_cast<AliMUONVCluster*>(nextInCh1()) ) ) {
96ebe67e 936 iCluster1++;
ea94c18b 937
96ebe67e 938 if (clusterCh1Used[iCluster1]) continue; // Skip clusters already used
ea94c18b 939
96ebe67e 940 // try to add the current cluster fast
941 if (!TryOneClusterFast(extrapTrackParamAtCh, clusterCh1)) continue;
019df241 942
96ebe67e 943 // try to add the current cluster accuratly
944 chi2OfCluster = TryOneCluster(extrapTrackParamAtCh, clusterCh1, extrapTrackParamAtCluster1,
a0dc65b4 945 GetRecoParam()->UseSmoother());
019df241 946
96ebe67e 947 // if good chi2 then consider to add clusterCh1
948 // We do not try to attach a cluster in the other chamber too since it has already been done above
949 if (chi2OfCluster < maxChi2OfCluster) {
019df241 950
a0dc65b4 951 if (GetRecoParam()->UseSmoother()) {
ea94c18b 952 // save extrapolated parameters for smoother
96ebe67e 953 extrapTrackParamAtCluster1.SetExtrapParameters(extrapTrackParamAtCluster1.GetParameters());
ea94c18b 954
955 // save extrapolated covariance matrix for smoother
96ebe67e 956 extrapTrackParamAtCluster1.SetExtrapCovariances(extrapTrackParamAtCluster1.GetCovariances());
ea94c18b 957 }
958
96ebe67e 959 // Compute new track parameters including "clusterCh1" using kalman filter
960 addChi2TrackAtCluster1 = RunKalmanFilter(extrapTrackParamAtCluster1);
ea94c18b 961
6b191dea 962 // skip track out of limits
963 if (!IsAcceptable(extrapTrackParamAtCluster1)) continue;
89c8d66d 964
965 // remember a cluster was found
966 foundOneCluster = kTRUE;
967
968 // Printout for debuging
969 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
970 cout << "FollowTrackInStation: found one cluster in chamber(1..): " << ch1+1
971 << " (Chi2 = " << chi2OfCluster << ")" << endl;
972 clusterCh1->Print();
973 }
9bf6860b 974
a0dc65b4 975 if (GetRecoParam()->TrackAllTracks()) {
96ebe67e 976 // copy trackCandidate into a new track put at the end of fRecTracksPtr and add the new cluster
ea94c18b 977 newTrack = new ((*fRecTracksPtr)[fRecTracksPtr->GetLast()+1]) AliMUONTrack(trackCandidate);
96ebe67e 978 UpdateTrack(*newTrack,extrapTrackParamAtCluster1,addChi2TrackAtCluster1);
ea94c18b 979 fNRecTracks++;
980
ea94c18b 981 // Printout for debuging
982 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
96ebe67e 983 cout << "FollowTrackInStation: added one cluster in chamber(1..): " << ch1+1 << endl;
ea94c18b 984 if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
985 }
986
96ebe67e 987 } else if (addChi2TrackAtCluster1 < bestAddChi2TrackAtCluster1) {
988 // keep track of the best single cluster except if a couple of clusters has already been found
989 bestAddChi2TrackAtCluster1 = addChi2TrackAtCluster1;
990 bestTrackParamAtCluster1 = extrapTrackParamAtCluster1;
ea94c18b 991 }
992
993 }
994
8cc77df0 995 }
ea94c18b 996
997 }
998
999 // fill out the best track if required else clean up the fRecTracksPtr array
a0dc65b4 1000 if (!GetRecoParam()->TrackAllTracks()) {
96ebe67e 1001 if (foundTwoClusters) {
1002 UpdateTrack(trackCandidate,bestTrackParamAtCluster1,bestTrackParamAtCluster2,bestAddChi2TrackAtCluster1,bestAddChi2TrackAtCluster2);
ea94c18b 1003
ea94c18b 1004 // Printout for debuging
1005 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
96ebe67e 1006 cout << "FollowTrackInStation: added the two best clusters in station(1..): " << nextStation+1 << endl;
ea94c18b 1007 if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
1008 }
1009
96ebe67e 1010 } else if (foundOneCluster) {
1011 UpdateTrack(trackCandidate,bestTrackParamAtCluster1,bestAddChi2TrackAtCluster1);
ea94c18b 1012
ea94c18b 1013 // Printout for debuging
1014 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
96ebe67e 1015 cout << "FollowTrackInStation: added the best cluster in chamber(1..): " << bestTrackParamAtCluster1.GetClusterPtr()->GetChamberId()+1 << endl;
ea94c18b 1016 if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
1017 }
1018
17773e2e 1019 } else {
96ebe67e 1020 delete [] clusterCh1Used;
17773e2e 1021 return kFALSE;
1022 }
9bf6860b 1023
96ebe67e 1024 } else if (foundOneCluster || foundTwoClusters) {
ea94c18b 1025
1026 // remove obsolete track
1027 fRecTracksPtr->Remove(&trackCandidate);
1028 fNRecTracks--;
1029
17773e2e 1030 } else {
96ebe67e 1031 delete [] clusterCh1Used;
17773e2e 1032 return kFALSE;
9bf6860b 1033 }
1034
96ebe67e 1035 delete [] clusterCh1Used;
ea94c18b 1036 return kTRUE;
1037
1038}
8cc77df0 1039
ea94c18b 1040 //__________________________________________________________________________
96ebe67e 1041Double_t AliMUONTrackReconstructorK::RunKalmanFilter(AliMUONTrackParam &trackParamAtCluster)
ea94c18b 1042{
96ebe67e 1043 /// Compute new track parameters and their covariances including new cluster using kalman filter
ea94c18b 1044 /// return the additional track chi2
1045 AliDebug(1,"Enter RunKalmanFilter");
1046
1047 // Get actual track parameters (p)
96ebe67e 1048 TMatrixD param(trackParamAtCluster.GetParameters());
ea94c18b 1049
96ebe67e 1050 // Get new cluster parameters (m)
1051 AliMUONVCluster *cluster = trackParamAtCluster.GetClusterPtr();
1052 TMatrixD clusterParam(5,1);
1053 clusterParam.Zero();
1054 clusterParam(0,0) = cluster->GetX();
1055 clusterParam(2,0) = cluster->GetY();
ea94c18b 1056
1057 // Compute the actual parameter weight (W)
96ebe67e 1058 TMatrixD paramWeight(trackParamAtCluster.GetCovariances());
ea94c18b 1059 if (paramWeight.Determinant() != 0) {
1060 paramWeight.Invert();
1061 } else {
1062 AliWarning(" Determinant = 0");
4ea3f013 1063 return 2.*AliMUONTrack::MaxChi2();
8cc77df0 1064 }
ea94c18b 1065
96ebe67e 1066 // Compute the new cluster weight (U)
1067 TMatrixD clusterWeight(5,5);
1068 clusterWeight.Zero();
1069 clusterWeight(0,0) = 1. / cluster->GetErrX2();
1070 clusterWeight(2,2) = 1. / cluster->GetErrY2();
8cc77df0 1071
ea94c18b 1072 // Compute the new parameters covariance matrix ( (W+U)^-1 )
96ebe67e 1073 TMatrixD newParamCov(paramWeight,TMatrixD::kPlus,clusterWeight);
ea94c18b 1074 if (newParamCov.Determinant() != 0) {
1075 newParamCov.Invert();
1076 } else {
1077 AliWarning(" Determinant = 0");
4ea3f013 1078 return 2.*AliMUONTrack::MaxChi2();
ea94c18b 1079 }
1080
1081 // Save the new parameters covariance matrix
96ebe67e 1082 trackParamAtCluster.SetCovariances(newParamCov);
ea94c18b 1083
1084 // Compute the new parameters (p' = ((W+U)^-1)U(m-p) + p)
96ebe67e 1085 TMatrixD tmp(clusterParam,TMatrixD::kMinus,param);
1086 TMatrixD tmp2(clusterWeight,TMatrixD::kMult,tmp); // U(m-p)
ea94c18b 1087 TMatrixD newParam(newParamCov,TMatrixD::kMult,tmp2); // ((W+U)^-1)U(m-p)
1088 newParam += param; // ((W+U)^-1)U(m-p) + p
1089
1090 // Save the new parameters
96ebe67e 1091 trackParamAtCluster.SetParameters(newParam);
ea94c18b 1092
1093 // Compute the additional chi2 (= ((p'-p)^-1)W(p'-p) + ((p'-m)^-1)U(p'-m))
1094 tmp = newParam; // p'
1095 tmp -= param; // (p'-p)
1096 TMatrixD tmp3(paramWeight,TMatrixD::kMult,tmp); // W(p'-p)
1097 TMatrixD addChi2Track(tmp,TMatrixD::kTransposeMult,tmp3); // ((p'-p)^-1)W(p'-p)
1098 tmp = newParam; // p'
96ebe67e 1099 tmp -= clusterParam; // (p'-m)
1100 TMatrixD tmp4(clusterWeight,TMatrixD::kMult,tmp); // U(p'-m)
ea94c18b 1101 addChi2Track += TMatrixD(tmp,TMatrixD::kTransposeMult,tmp4); // ((p'-p)^-1)W(p'-p) + ((p'-m)^-1)U(p'-m)
1102
1103 return addChi2Track(0,0);
1104
8cc77df0 1105}
1106
ea94c18b 1107 //__________________________________________________________________________
96ebe67e 1108void AliMUONTrackReconstructorK::UpdateTrack(AliMUONTrack &track, AliMUONTrackParam &trackParamAtCluster, Double_t addChi2)
8cc77df0 1109{
96ebe67e 1110 /// Add 1 cluster to the track candidate
ea94c18b 1111 /// Update chi2 of the track
1112
9bf6860b 1113 // Flag cluster as being (not) removable
a0dc65b4 1114 if (GetRecoParam()->RequestStation(trackParamAtCluster.GetClusterPtr()->GetChamberId()/2))
9bf6860b 1115 trackParamAtCluster.SetRemovable(kFALSE);
1116 else trackParamAtCluster.SetRemovable(kTRUE);
96ebe67e 1117 trackParamAtCluster.SetLocalChi2(0.); // --> Local chi2 not used
5f4ceff2 1118
96ebe67e 1119 // Update the track chi2 into trackParamAtCluster
1120 trackParamAtCluster.SetTrackChi2(track.GetGlobalChi2() + addChi2);
ea94c18b 1121
1122 // Update the chi2 of the new track
96ebe67e 1123 track.SetGlobalChi2(trackParamAtCluster.GetTrackChi2());
ea94c18b 1124
96ebe67e 1125 // Update array of TrackParamAtCluster
1126 track.AddTrackParamAtCluster(trackParamAtCluster,*(trackParamAtCluster.GetClusterPtr()));
ea94c18b 1127
1128}
8cc77df0 1129
ea94c18b 1130 //__________________________________________________________________________
96ebe67e 1131void AliMUONTrackReconstructorK::UpdateTrack(AliMUONTrack &track, AliMUONTrackParam &trackParamAtCluster1, AliMUONTrackParam &trackParamAtCluster2,
1132 Double_t addChi2AtCluster1, Double_t addChi2AtCluster2)
ea94c18b 1133{
96ebe67e 1134 /// Add 2 clusters to the track candidate (order is important)
ea94c18b 1135 /// Update track and local chi2
1136
96ebe67e 1137 // Update local chi2 at first cluster
1138 AliMUONVCluster* cluster1 = trackParamAtCluster1.GetClusterPtr();
1139 Double_t deltaX = trackParamAtCluster1.GetNonBendingCoor() - cluster1->GetX();
1140 Double_t deltaY = trackParamAtCluster1.GetBendingCoor() - cluster1->GetY();
1141 Double_t localChi2AtCluster1 = deltaX*deltaX / cluster1->GetErrX2() +
1142 deltaY*deltaY / cluster1->GetErrY2();
1143 trackParamAtCluster1.SetLocalChi2(localChi2AtCluster1);
ea94c18b 1144
96ebe67e 1145 // Flag first cluster as being removable
1146 trackParamAtCluster1.SetRemovable(kTRUE);
ea94c18b 1147
96ebe67e 1148 // Update local chi2 at second cluster
1149 AliMUONVCluster* cluster2 = trackParamAtCluster2.GetClusterPtr();
1150 AliMUONTrackParam extrapTrackParamAtCluster2(trackParamAtCluster1);
1151 AliMUONTrackExtrap::ExtrapToZ(&extrapTrackParamAtCluster2, trackParamAtCluster2.GetZ());
1152 deltaX = extrapTrackParamAtCluster2.GetNonBendingCoor() - cluster2->GetX();
1153 deltaY = extrapTrackParamAtCluster2.GetBendingCoor() - cluster2->GetY();
1154 Double_t localChi2AtCluster2 = deltaX*deltaX / cluster2->GetErrX2() +
1155 deltaY*deltaY / cluster2->GetErrY2();
1156 trackParamAtCluster2.SetLocalChi2(localChi2AtCluster2);
ea94c18b 1157
96ebe67e 1158 // Flag second cluster as being removable
1159 trackParamAtCluster2.SetRemovable(kTRUE);
ea94c18b 1160
96ebe67e 1161 // Update the track chi2 into trackParamAtCluster2
1162 trackParamAtCluster2.SetTrackChi2(track.GetGlobalChi2() + addChi2AtCluster2);
ea94c18b 1163
96ebe67e 1164 // Update the track chi2 into trackParamAtCluster1
1165 trackParamAtCluster1.SetTrackChi2(trackParamAtCluster2.GetTrackChi2() + addChi2AtCluster1);
ea94c18b 1166
1167 // Update the chi2 of the new track
96ebe67e 1168 track.SetGlobalChi2(trackParamAtCluster1.GetTrackChi2());
ea94c18b 1169
96ebe67e 1170 // Update array of trackParamAtCluster
1171 track.AddTrackParamAtCluster(trackParamAtCluster1,*cluster1);
1172 track.AddTrackParamAtCluster(trackParamAtCluster2,*cluster2);
ea94c18b 1173
1174}
8cc77df0 1175
ea94c18b 1176 //__________________________________________________________________________
1fef78c8 1177Bool_t AliMUONTrackReconstructorK::RecoverTrack(AliMUONTrack &trackCandidate, AliMUONVClusterStore& clusterStore, Int_t nextStation)
ea94c18b 1178{
1179 /// Try to recover the track candidate in the next station
96ebe67e 1180 /// by removing the worst of the two clusters attached in the current station
ea94c18b 1181 /// Return kTRUE if recovering succeeds
1182 AliDebug(1,"Enter RecoverTrack");
1183
96ebe67e 1184 // Do not try to recover track until we have attached cluster(s) on station(1..) 3
ea94c18b 1185 if (nextStation > 1) return kFALSE;
1186
96ebe67e 1187 Int_t worstClusterNumber = -1;
9bf6860b 1188 Double_t localChi2, worstLocalChi2 = -1.;
ea94c18b 1189
96ebe67e 1190 // Look for the cluster to remove
1191 for (Int_t clusterNumber = 0; clusterNumber < 2; clusterNumber++) {
1192 AliMUONTrackParam *trackParamAtCluster = (AliMUONTrackParam*)trackCandidate.GetTrackParamAtCluster()->UncheckedAt(clusterNumber);
ea94c18b 1193
9bf6860b 1194 // check if current cluster is in the previous station
1195 if (trackParamAtCluster->GetClusterPtr()->GetChamberId()/2 != nextStation+1) break;
1196
96ebe67e 1197 // check if current cluster is removable
1198 if (!trackParamAtCluster->IsRemovable()) return kFALSE;
ea94c18b 1199
9bf6860b 1200 // reset the current cluster as being not removable if it is on a required station
a0dc65b4 1201 if (GetRecoParam()->RequestStation(nextStation+1)) trackParamAtCluster->SetRemovable(kFALSE);
9bf6860b 1202
96ebe67e 1203 // Pick up cluster with the worst chi2
1204 localChi2 = trackParamAtCluster->GetLocalChi2();
1205 if (localChi2 > worstLocalChi2) {
1206 worstLocalChi2 = localChi2;
1207 worstClusterNumber = clusterNumber;
8cc77df0 1208 }
ea94c18b 1209 }
1210
9bf6860b 1211 // check if worst cluster found
1212 if (worstClusterNumber < 0) return kFALSE;
ea94c18b 1213
96ebe67e 1214 // Remove the worst cluster
1215 trackCandidate.RemoveTrackParamAtCluster((AliMUONTrackParam*)trackCandidate.GetTrackParamAtCluster()->UncheckedAt(worstClusterNumber));
ea94c18b 1216
96ebe67e 1217 // Re-calculate track parameters at the (new) first cluster
4ea3f013 1218 if (!RetracePartialTrack(trackCandidate,(AliMUONTrackParam*)trackCandidate.GetTrackParamAtCluster()->UncheckedAt(1))) return kFALSE;
ea94c18b 1219
6b191dea 1220 // skip track out of limits
1221 if (!IsAcceptable(*((AliMUONTrackParam*)trackCandidate.GetTrackParamAtCluster()->First()))) return kFALSE;
9bf6860b 1222
96ebe67e 1223 // Look for new cluster(s) in next station
1224 return FollowTrackInStation(trackCandidate, clusterStore, nextStation);
ea94c18b 1225
8cc77df0 1226}
1227
1228 //__________________________________________________________________________
ea94c18b 1229Bool_t AliMUONTrackReconstructorK::RunSmoother(AliMUONTrack &track)
8cc77df0 1230{
ea94c18b 1231 /// Compute new track parameters and their covariances using smoother
3304fa09 1232 AliDebug(1,"Enter UseSmoother");
ea94c18b 1233
96ebe67e 1234 AliMUONTrackParam *previousTrackParam = (AliMUONTrackParam*) track.GetTrackParamAtCluster()->First();
ea94c18b 1235
96ebe67e 1236 // Smoothed parameters and covariances at first cluster = filtered parameters and covariances
ea94c18b 1237 previousTrackParam->SetSmoothParameters(previousTrackParam->GetParameters());
1238 previousTrackParam->SetSmoothCovariances(previousTrackParam->GetCovariances());
d1e6fdee 1239
1240 AliMUONTrackParam *currentTrackParam = (AliMUONTrackParam*) track.GetTrackParamAtCluster()->After(previousTrackParam);
ea94c18b 1241
d1e6fdee 1242 // Save local chi2 at first cluster = last additional chi2 provided by Kalman
1243 previousTrackParam->SetLocalChi2(previousTrackParam->GetTrackChi2() - currentTrackParam->GetTrackChi2());
ea94c18b 1244
ff078154 1245 // if the track contains only 2 clusters simply copy the filtered parameters
1246 if (track.GetNClusters() == 2) {
1247 currentTrackParam->SetSmoothParameters(currentTrackParam->GetParameters());
1248 currentTrackParam->SetSmoothCovariances(currentTrackParam->GetCovariances());
1249 currentTrackParam->SetLocalChi2(currentTrackParam->GetTrackChi2());
1250 return kTRUE;
1251 }
1252
ea94c18b 1253 while (currentTrackParam) {
1254
1255 // Get variables
1256 const TMatrixD &extrapParameters = previousTrackParam->GetExtrapParameters(); // X(k+1 k)
1257 const TMatrixD &filteredParameters = currentTrackParam->GetParameters(); // X(k k)
1258 const TMatrixD &previousSmoothParameters = previousTrackParam->GetSmoothParameters(); // X(k+1 n)
1259 const TMatrixD &propagator = previousTrackParam->GetPropagator(); // F(k)
1260 const TMatrixD &extrapCovariances = previousTrackParam->GetExtrapCovariances(); // C(k+1 k)
1261 const TMatrixD &filteredCovariances = currentTrackParam->GetCovariances(); // C(k k)
1262 const TMatrixD &previousSmoothCovariances = previousTrackParam->GetSmoothCovariances(); // C(k+1 n)
1263
d1e6fdee 1264 // Compute smoother gain: A(k) = C(kk) * F(k)^t * (C(k+1 k))^-1
ea94c18b 1265 TMatrixD extrapWeight(extrapCovariances);
1266 if (extrapWeight.Determinant() != 0) {
1267 extrapWeight.Invert(); // (C(k+1 k))^-1
1268 } else {
1269 AliWarning(" Determinant = 0");
1270 return kFALSE;
1271 }
d1e6fdee 1272 TMatrixD smootherGain(filteredCovariances,TMatrixD::kMultTranspose,propagator); // C(kk) * F(k)^t
1273 smootherGain *= extrapWeight; // C(kk) * F(k)^t * (C(k+1 k))^-1
ea94c18b 1274
1275 // Compute smoothed parameters: X(k n) = X(k k) + A(k) * (X(k+1 n) - X(k+1 k))
1276 TMatrixD tmpParam(previousSmoothParameters,TMatrixD::kMinus,extrapParameters); // X(k+1 n) - X(k+1 k)
1277 TMatrixD smoothParameters(smootherGain,TMatrixD::kMult,tmpParam); // A(k) * (X(k+1 n) - X(k+1 k))
1278 smoothParameters += filteredParameters; // X(k k) + A(k) * (X(k+1 n) - X(k+1 k))
1279
1280 // Save smoothed parameters
1281 currentTrackParam->SetSmoothParameters(smoothParameters);
1282
1283 // Compute smoothed covariances: C(k n) = C(k k) + A(k) * (C(k+1 n) - C(k+1 k)) * (A(k))^t
1284 TMatrixD tmpCov(previousSmoothCovariances,TMatrixD::kMinus,extrapCovariances); // C(k+1 n) - C(k+1 k)
1285 TMatrixD tmpCov2(tmpCov,TMatrixD::kMultTranspose,smootherGain); // (C(k+1 n) - C(k+1 k)) * (A(k))^t
1286 TMatrixD smoothCovariances(smootherGain,TMatrixD::kMult,tmpCov2); // A(k) * (C(k+1 n) - C(k+1 k)) * (A(k))^t
1287 smoothCovariances += filteredCovariances; // C(k k) + A(k) * (C(k+1 n) - C(k+1 k)) * (A(k))^t
1288
1289 // Save smoothed covariances
1290 currentTrackParam->SetSmoothCovariances(smoothCovariances);
1291
96ebe67e 1292 // Compute smoothed residual: r(k n) = cluster - X(k n)
d1e6fdee 1293 AliMUONVCluster* cluster = currentTrackParam->GetClusterPtr();
ea94c18b 1294 TMatrixD smoothResidual(2,1);
1295 smoothResidual.Zero();
96ebe67e 1296 smoothResidual(0,0) = cluster->GetX() - smoothParameters(0,0);
1297 smoothResidual(1,0) = cluster->GetY() - smoothParameters(2,0);
ea94c18b 1298
96ebe67e 1299 // Compute weight of smoothed residual: W(k n) = (clusterCov - C(k n))^-1
ea94c18b 1300 TMatrixD smoothResidualWeight(2,2);
96ebe67e 1301 smoothResidualWeight(0,0) = cluster->GetErrX2() - smoothCovariances(0,0);
ea94c18b 1302 smoothResidualWeight(0,1) = - smoothCovariances(0,2);
1303 smoothResidualWeight(1,0) = - smoothCovariances(2,0);
96ebe67e 1304 smoothResidualWeight(1,1) = cluster->GetErrY2() - smoothCovariances(2,2);
ea94c18b 1305 if (smoothResidualWeight.Determinant() != 0) {
1306 smoothResidualWeight.Invert();
1307 } else {
1308 AliWarning(" Determinant = 0");
1309 return kFALSE;
1310 }
1311
1312 // Compute local chi2 = (r(k n))^t * W(k n) * r(k n)
1313 TMatrixD tmpChi2(smoothResidual,TMatrixD::kTransposeMult,smoothResidualWeight); // (r(k n))^t * W(k n)
1314 TMatrixD localChi2(tmpChi2,TMatrixD::kMult,smoothResidual); // (r(k n))^t * W(k n) * r(k n)
1315
1316 // Save local chi2
1317 currentTrackParam->SetLocalChi2(localChi2(0,0));
1318
1319 previousTrackParam = currentTrackParam;
96ebe67e 1320 currentTrackParam = (AliMUONTrackParam*) track.GetTrackParamAtCluster()->After(previousTrackParam);
ea94c18b 1321 }
1322
1323 return kTRUE;
1324
8cc77df0 1325}
1326
1327 //__________________________________________________________________________
89c8d66d 1328Bool_t AliMUONTrackReconstructorK::ComplementTracks(const AliMUONVClusterStore& clusterStore)
b709ac13 1329{
1330 /// Complete tracks by adding missing clusters (if there is an overlap between
89c8d66d 1331 /// two detection elements, the track may have two clusters in the same chamber).
1332 /// Recompute track parameters and covariances at each clusters.
4ea3f013 1333 /// Remove tracks getting abnormal (i.e. extrapolation failed...) after being complemented.
1334 /// Return kTRUE if one or more tracks have been complemented or removed.
b709ac13 1335 AliDebug(1,"Enter ComplementTracks");
1336
1337 Int_t chamberId, detElemId;
96ebe67e 1338 Double_t chi2OfCluster, addChi2TrackAtCluster, bestAddChi2TrackAtCluster;
a0dc65b4 1339 Double_t maxChi2OfCluster = 2. * GetRecoParam()->GetSigmaCutForTracking() *
1340 GetRecoParam()->GetSigmaCutForTracking(); // 2 because 2 quantities in chi2
89c8d66d 1341 Bool_t foundOneCluster, trackModified, hasChanged = kFALSE;
96ebe67e 1342 AliMUONVCluster *cluster;
1343 AliMUONTrackParam *trackParam, *previousTrackParam, *nextTrackParam, trackParamAtCluster, bestTrackParamAtCluster;
4ea3f013 1344 AliMUONTrack *nextTrack;
b709ac13 1345
b709ac13 1346 AliMUONTrack *track = (AliMUONTrack*) fRecTracksPtr->First();
1347 while (track) {
1348 trackModified = kFALSE;
1349
96ebe67e 1350 trackParam = (AliMUONTrackParam*)track->GetTrackParamAtCluster()->First();
b709ac13 1351 previousTrackParam = trackParam;
1352 while (trackParam) {
96ebe67e 1353 foundOneCluster = kFALSE;
4ea3f013 1354 bestAddChi2TrackAtCluster = AliMUONTrack::MaxChi2();
96ebe67e 1355 chamberId = trackParam->GetClusterPtr()->GetChamberId();
1356 detElemId = trackParam->GetClusterPtr()->GetDetElemId();
b709ac13 1357
1358 // prepare nextTrackParam before adding new cluster because of the sorting
96ebe67e 1359 nextTrackParam = (AliMUONTrackParam*)track->GetTrackParamAtCluster()->After(trackParam);
b709ac13 1360
96ebe67e 1361 // Create iterators to loop over clusters in current chamber
1362 TIter nextInCh(clusterStore.CreateChamberIterator(chamberId,chamberId));
b709ac13 1363
1364 // look for one second candidate in the same chamber
96ebe67e 1365 while ( ( cluster = static_cast<AliMUONVCluster*>(nextInCh()) ) ) {
b709ac13 1366
1367 // look for a cluster in another detection element
96ebe67e 1368 if (cluster->GetDetElemId() == detElemId) continue;
b709ac13 1369
96ebe67e 1370 // try to add the current cluster fast
1371 if (!TryOneClusterFast(*trackParam, cluster)) continue;
b709ac13 1372
96ebe67e 1373 // try to add the current cluster accurately
b709ac13 1374 // never use track parameters at last cluster because the covariance matrix is meaningless
96ebe67e 1375 if (nextTrackParam) chi2OfCluster = TryOneCluster(*trackParam, cluster, trackParamAtCluster);
1376 else chi2OfCluster = TryOneCluster(*previousTrackParam, cluster, trackParamAtCluster);
b709ac13 1377
1378 // if good chi2 then consider to add this cluster to the track
96ebe67e 1379 if (chi2OfCluster < maxChi2OfCluster) {
b709ac13 1380
96ebe67e 1381 // Compute local track parameters including current cluster using kalman filter
1382 addChi2TrackAtCluster = RunKalmanFilter(trackParamAtCluster);
b709ac13 1383
1384 // keep track of the best cluster
96ebe67e 1385 if (addChi2TrackAtCluster < bestAddChi2TrackAtCluster) {
1386 bestAddChi2TrackAtCluster = addChi2TrackAtCluster;
1387 bestTrackParamAtCluster = trackParamAtCluster;
1388 foundOneCluster = kTRUE;
b709ac13 1389 }
1390
1391 }
1392
1393 }
1394
1395 // add new cluster if any
96ebe67e 1396 if (foundOneCluster) {
0e894e58 1397
1398 // Printout for debuging
1399 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
1400 cout << "ComplementTracks: found one cluster in chamber(1..): " << chamberId+1 << endl;
1401 bestTrackParamAtCluster.GetClusterPtr()->Print();
ad3c6eda 1402 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 2) || (AliLog::GetGlobalDebugLevel() >= 2)) {
1403 cout<<endl<<"Track parameters and covariances at cluster:"<<endl;
1404 bestTrackParamAtCluster.GetParameters().Print();
1405 bestTrackParamAtCluster.GetCovariances().Print();
1406 }
0e894e58 1407 }
1408
7332f213 1409 trackParam->SetRemovable(kTRUE);
1410 bestTrackParamAtCluster.SetRemovable(kTRUE);
1411 track->AddTrackParamAtCluster(bestTrackParamAtCluster,*(bestTrackParamAtCluster.GetClusterPtr()));
b709ac13 1412 trackModified = kTRUE;
89c8d66d 1413 hasChanged = kTRUE;
b709ac13 1414 }
1415
1416 previousTrackParam = trackParam;
1417 trackParam = nextTrackParam;
1418 }
1419
4ea3f013 1420 // prepare next track
1421 nextTrack = (AliMUONTrack*) fRecTracksPtr->After(track);
1422
b709ac13 1423 // re-compute track parameters using kalman filter if needed
4ea3f013 1424 if (trackModified && !RetraceTrack(*track,kTRUE)) {
1425 AliWarning("track modified but problem occur during refitting --> remove track");
1426 fRecTracksPtr->Remove(track);
1427 fNRecTracks--;
1428 }
b709ac13 1429
4ea3f013 1430 track = nextTrack;
b709ac13 1431 }
1432
89c8d66d 1433 return hasChanged;
1434
b709ac13 1435}
1436
1437//__________________________________________________________________________
7332f213 1438void AliMUONTrackReconstructorK::ImproveTrack(AliMUONTrack &track)
8cc77df0 1439{
7332f213 1440 /// Improve the given track by removing removable clusters with local chi2 highter than the defined cut
1441 /// Removable clusters are identified by the method AliMUONTrack::TagRemovableClusters()
ea94c18b 1442 /// Recompute track parameters and covariances at the remaining clusters
4ea3f013 1443 /// and if something goes wrong (i.e. extrapolation failed...) set track chi2 to max value
7332f213 1444 AliDebug(1,"Enter ImproveTrack");
ea94c18b 1445
1446 Double_t localChi2, worstLocalChi2;
ff078154 1447 AliMUONTrackParam *trackParamAtCluster, *worstTrackParamAtCluster, *nextTrackParam, *next2nextTrackParam;
1448 Int_t nextChamber, next2nextChamber;
ea94c18b 1449 Bool_t smoothed;
a0dc65b4 1450 Double_t sigmaCut2 = GetRecoParam()->GetSigmaCutForImprovement() *
1451 GetRecoParam()->GetSigmaCutForImprovement();
ea94c18b 1452
7332f213 1453 while (!track.IsImproved()) {
ea94c18b 1454
7332f213 1455 // identify removable clusters
a0dc65b4 1456 track.TagRemovableClusters(GetRecoParam()->RequestedStationMask());
b709ac13 1457
7332f213 1458 // Run smoother if required
1459 smoothed = kFALSE;
a0dc65b4 1460 if (GetRecoParam()->UseSmoother()) smoothed = RunSmoother(track);
7332f213 1461
1462 // Use standard procedure to compute local chi2 if smoother not required or not working
1463 if (!smoothed) {
ea94c18b 1464
7332f213 1465 // Update track parameters and covariances
4ea3f013 1466 if (!track.UpdateCovTrackParamAtCluster()) {
1467 AliWarning("unable to update track parameters and covariances --> stop improvement");
1468 // restore the kalman parameters
1469 RetraceTrack(track,kTRUE);
1470 break;
1471 }
ea94c18b 1472
7332f213 1473 // Compute local chi2 of each clusters
1474 track.ComputeLocalChi2(kTRUE);
1475 }
1476
1477 // Look for the cluster to remove
1478 worstTrackParamAtCluster = 0x0;
ff078154 1479 worstLocalChi2 = -1.;
7332f213 1480 trackParamAtCluster = (AliMUONTrackParam*)track.GetTrackParamAtCluster()->First();
1481 while (trackParamAtCluster) {
ea94c18b 1482
7332f213 1483 // save parameters into smooth parameters in case of smoother did not work properly
a0dc65b4 1484 if (GetRecoParam()->UseSmoother() && !smoothed) {
7332f213 1485 trackParamAtCluster->SetSmoothParameters(trackParamAtCluster->GetParameters());
1486 trackParamAtCluster->SetSmoothCovariances(trackParamAtCluster->GetCovariances());
ea94c18b 1487 }
1488
7332f213 1489 // Pick up cluster with the worst chi2
1490 localChi2 = trackParamAtCluster->GetLocalChi2();
1491 if (localChi2 > worstLocalChi2) {
1492 worstLocalChi2 = localChi2;
1493 worstTrackParamAtCluster = trackParamAtCluster;
ea94c18b 1494 }
1495
7332f213 1496 trackParamAtCluster = (AliMUONTrackParam*)track.GetTrackParamAtCluster()->After(trackParamAtCluster);
1497 }
1498
7332f213 1499 // Check whether the worst chi2 is under requirement or not
1500 if (worstLocalChi2 < 2. * sigmaCut2) { // 2 because 2 quantities in chi2
1501 track.SetImproved(kTRUE);
1502 break;
1503 }
1504
1505 // if the worst cluster is not removable then stop improvement
ff078154 1506 if (!worstTrackParamAtCluster->IsRemovable()) {
1507 // restore the kalman parameters in case they have been lost
1508 if (!smoothed) RetraceTrack(track,kTRUE);
1509 break;
1510 }
7332f213 1511
1512 // get track parameters at cluster next to the one to be removed
1513 nextTrackParam = (AliMUONTrackParam*) track.GetTrackParamAtCluster()->After(worstTrackParamAtCluster);
1514
1515 // Remove the worst cluster
1516 track.RemoveTrackParamAtCluster(worstTrackParamAtCluster);
1517
1518 // Re-calculate track parameters
1519 // - from the cluster immediately downstream the one suppressed
1520 // - or from the begining - if parameters have been re-computed using the standard method (kalman parameters have been lost)
ff078154 1521 // - or if the removed cluster was used to compute the tracking seed
4ea3f013 1522 Bool_t normalExtrap;
ff078154 1523 if (smoothed && nextTrackParam) {
1524
1525 nextChamber = nextTrackParam->GetClusterPtr()->GetChamberId();
1526 next2nextTrackParam = nextTrackParam;
1527 do {
1528
1529 next2nextChamber = next2nextTrackParam->GetClusterPtr()->GetChamberId();
1530 next2nextTrackParam = (AliMUONTrackParam*) track.GetTrackParamAtCluster()->After(next2nextTrackParam);
1531
1532 } while (next2nextTrackParam && (next2nextChamber == nextChamber));
1533
4ea3f013 1534 if (next2nextChamber == nextChamber) normalExtrap = RetraceTrack(track,kTRUE);
1535 else normalExtrap = RetracePartialTrack(track,nextTrackParam);
ff078154 1536
4ea3f013 1537 } else normalExtrap = RetraceTrack(track,kTRUE);
1538
1539 // stop in case of extrapolation problem
1540 if (!normalExtrap) {
1541 AliWarning("track partially improved but problem occur during refitting --> stop improvement");
1542 break;
1543 }
7332f213 1544
1545 // Printout for debuging
1546 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
1547 cout << "ImproveTracks: track " << fRecTracksPtr->IndexOf(&track)+1 << " improved " << endl;
ea94c18b 1548 }
1549
ea94c18b 1550 }
1551
8cc77df0 1552}
1553
7332f213 1554//__________________________________________________________________________
4ea3f013 1555Bool_t AliMUONTrackReconstructorK::FinalizeTrack(AliMUONTrack &track)
b0e4c598 1556{
96ebe67e 1557 /// Update track parameters and covariances at each attached cluster
7332f213 1558 /// using smoother if required, if not already done
4ea3f013 1559 /// return kFALSE if the track cannot be extrapolated uo to the last chamber
b709ac13 1560
96ebe67e 1561 AliMUONTrackParam *trackParamAtCluster;
ea94c18b 1562 Bool_t smoothed = kFALSE;
1563
7332f213 1564 // update track parameters (using smoother if required) if not already done
4ea3f013 1565 if (track.IsImproved()) smoothed = GetRecoParam()->UseSmoother();
1566 else {
a0dc65b4 1567 if (GetRecoParam()->UseSmoother()) smoothed = RunSmoother(track);
d1e6fdee 1568 if (!smoothed) {
4ea3f013 1569 if (track.UpdateCovTrackParamAtCluster()) track.ComputeLocalChi2(kTRUE);
1570 else {
1571 AliWarning("finalization failed due to extrapolation problem");
1572 return kFALSE;
1573 }
d1e6fdee 1574 }
4ea3f013 1575 }
7332f213 1576
1577 // copy smoothed parameters and covariances if any
1578 if (smoothed) {
ea94c18b 1579
7332f213 1580 trackParamAtCluster = (AliMUONTrackParam*) (track.GetTrackParamAtCluster()->First());
1581 while (trackParamAtCluster) {
ea94c18b 1582
7332f213 1583 trackParamAtCluster->SetParameters(trackParamAtCluster->GetSmoothParameters());
1584 trackParamAtCluster->SetCovariances(trackParamAtCluster->GetSmoothCovariances());
ea94c18b 1585
7332f213 1586 trackParamAtCluster = (AliMUONTrackParam*) (track.GetTrackParamAtCluster()->After(trackParamAtCluster));
ea94c18b 1587 }
1588
b0e4c598 1589 }
7332f213 1590
4ea3f013 1591 return kTRUE;
1592
b0e4c598 1593}
ea94c18b 1594
7deb8eb0 1595 //__________________________________________________________________________
b1fea02e 1596Bool_t AliMUONTrackReconstructorK::RefitTrack(AliMUONTrack &track, Bool_t enableImprovement)
7deb8eb0 1597{
1598 /// re-fit the given track
89c8d66d 1599 AliDebug(1,"Enter RefitTrack");
7deb8eb0 1600
ff078154 1601 // check validity of the track (i.e. at least 2 chambers hit on stations 4 and 5)
1602 if (!track.IsValid(0)) {
1603 AliWarning("the track is not valid --> unable to refit");
7deb8eb0 1604 return kFALSE;
1605 }
1606
1607 // re-compute track parameters and covariances using Kalman filter
4ea3f013 1608 if (!RetraceTrack(track,kTRUE)) {
1609 AliWarning("bad track refitting due to extrapolation failure");
1610 return kFALSE;
1611 }
7deb8eb0 1612
1613 // Improve the reconstructed tracks if required
b1fea02e 1614 track.SetImproved(kFALSE);
a0dc65b4 1615 if (enableImprovement && GetRecoParam()->ImproveTracks()) ImproveTrack(track);
7deb8eb0 1616
1617 // Fill AliMUONTrack data members
4ea3f013 1618 if (track.GetGlobalChi2() < AliMUONTrack::MaxChi2()) return FinalizeTrack(track);
1619 else {
1620 AliWarning("track not finalized due to extrapolation failure");
1621 return kFALSE;
1622 }
7deb8eb0 1623
1624}
1625