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