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