]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONVTrackReconstructor.cxx
- AliMUONRecoParam.cxx:
[u/mrichter/AliRoot.git] / MUON / AliMUONVTrackReconstructor.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 AliMUONVTrackReconstructor
20 /// Virtual MUON track reconstructor in ALICE (class renamed from AliMUONEventReconstructor)
21 ///
22 /// This class contains as data a pointer to the array of reconstructed tracks
23 ///
24 /// It contains as methods, among others:
25 /// * EventReconstruct to build the muon tracks
26 /// * EventReconstructTrigger to build the trigger tracks
27 /// * ValidateTracksWithTrigger to match tracker/trigger tracks
28 ///
29 /// Several options and adjustable parameters are available for both KALMAN and ORIGINAL
30 /// tracking algorithms. They can be changed through the AliMUONRecoParam object
31 /// set in the reconstruction macro or read from the CDB
32 /// (see methods in AliMUONRecoParam.h file for details)
33 ///
34 /// Main parameters and options are:
35 /// - *fgkSigmaToCutForTracking* : quality cut used to select new clusters to be
36 ///   attached to the track candidate and to select good tracks.
37 /// - *fgkMakeTrackCandidatesFast* : if this flag is set to 'true', the track candidates
38 ///   are made assuming linear propagation between stations 4 and 5.
39 /// - *fgkTrackAllTracks* : according to the value of this flag, in case that several
40 ///   new clusters pass the quality cut, either we consider all the possibilities
41 ///   (duplicating tracks) or we attach only the best cluster.
42 /// - *fgkRecoverTracks* : if this flag is set to 'true', we try to recover the tracks
43 ///   lost during the tracking by removing the worst of the 2 clusters attached in the
44 ///   previous station.
45 /// - *fgkImproveTracks* : if this flag is set to 'true', we try to improve the quality
46 ///   of the tracks at the end of the tracking by removing clusters that do not pass
47 ///   new quality cut (the track is removed is it does not contain enough cluster anymore).
48 /// - *fgkComplementTracks* : if this flag is set to 'true', we try to improve the quality
49 ///   of the tracks at the end of the tracking by adding potentially missing clusters
50 ///   (we may have 2 clusters in the same chamber because of the overlapping of detection
51 ///   elements, which is not handle by the tracking algorithm).
52 /// - *fgkSigmaToCutForImprovement* : quality cut used when we try to improve the
53 ///   quality of the tracks.
54 ///
55 ///  \author Philippe Pillot
56 //-----------------------------------------------------------------------------
57
58 #include "AliMUONVTrackReconstructor.h"
59
60 #include "AliMUONConstants.h"
61 #include "AliMUONObjectPair.h"
62 #include "AliMUONTriggerTrack.h"
63 #include "AliMUONTriggerCircuit.h"
64 #include "AliMUONLocalTrigger.h"
65 #include "AliMUONGlobalTrigger.h"
66 #include "AliMUONTrack.h"
67 #include "AliMUONTrackParam.h"
68 #include "AliMUONTrackExtrap.h"
69 #include "AliMUONTrackHitPattern.h"
70 #include "AliMUONVTrackStore.h"
71 #include "AliMUONVClusterStore.h"
72 #include "AliMUONVCluster.h"
73 #include "AliMUONVClusterServer.h"
74 #include "AliMUONVTriggerStore.h"
75 #include "AliMUONVTriggerTrackStore.h"
76 #include "AliMpDEManager.h"
77 #include "AliMpArea.h"
78
79 #include "AliLog.h"
80 #include "AliCodeTimer.h"
81 #include "AliTracker.h"
82
83 #include <TClonesArray.h>
84 #include <TMath.h>
85 #include <TMatrixD.h>
86 #include <TVector2.h>
87
88 #include <Riostream.h>
89
90 /// \cond CLASSIMP
91 ClassImp(AliMUONVTrackReconstructor) // Class implementation in ROOT context
92 /// \endcond
93
94   //__________________________________________________________________________
95 AliMUONVTrackReconstructor::AliMUONVTrackReconstructor(const AliMUONRecoParam* recoParam,
96                                                        AliMUONVClusterServer* clusterServer)
97 : TObject(),
98 fRecTracksPtr(0x0),
99 fNRecTracks(0),
100 fClusterServer(clusterServer),
101 fRecoParam(recoParam)
102 {
103   /// Constructor for class AliMUONVTrackReconstructor
104   /// WARNING: if clusterServer=0x0, no clusterization will be possible at this level
105   
106   // Memory allocation for the TClonesArray of reconstructed tracks
107   fRecTracksPtr = new TClonesArray("AliMUONTrack", 100);
108   
109   // set the magnetic field for track extrapolations
110   const AliMagF* kField = AliTracker::GetFieldMap();
111   if (!kField) AliFatal("No field available");
112   AliMUONTrackExtrap::SetField(kField);
113 }
114
115   //__________________________________________________________________________
116 AliMUONVTrackReconstructor::~AliMUONVTrackReconstructor()
117 {
118   /// Destructor for class AliMUONVTrackReconstructor
119   delete fRecTracksPtr;
120 }
121
122   //__________________________________________________________________________
123 void AliMUONVTrackReconstructor::ResetTracks()
124 {
125   /// To reset the TClonesArray of reconstructed tracks
126   if (fRecTracksPtr) fRecTracksPtr->Clear("C");
127   fNRecTracks = 0;
128   return;
129 }
130
131   //__________________________________________________________________________
132 void AliMUONVTrackReconstructor::EventReconstruct(AliMUONVClusterStore& clusterStore, AliMUONVTrackStore& trackStore)
133 {
134   /// To reconstruct one event
135   AliDebug(1,"");
136   AliCodeTimerAuto("");
137   
138   // Reset array of tracks
139   ResetTracks();
140   
141   // Look for candidates from clusters in stations(1..) 4 and 5
142   MakeTrackCandidates(clusterStore);
143   
144   // Look for extra candidates from clusters in stations(1..) 4 and 5
145   if (GetRecoParam()->MakeMoreTrackCandidates()) MakeMoreTrackCandidates(clusterStore);
146   
147   // Stop tracking if no candidate found
148   if (fRecTracksPtr->GetEntriesFast() == 0) return;
149   
150   // Follow tracks in stations(1..) 3, 2 and 1
151   FollowTracks(clusterStore);
152   
153   // Complement the reconstructed tracks
154   if (GetRecoParam()->ComplementTracks()) ComplementTracks(clusterStore);
155   
156   // Improve the reconstructed tracks
157   if (GetRecoParam()->ImproveTracks()) ImproveTracks();
158   
159   // Remove double tracks
160   RemoveDoubleTracks();
161   
162   // Fill AliMUONTrack data members
163   Finalize();
164   
165   // Add tracks to MUON data container 
166   for (Int_t i=0; i<fNRecTracks; ++i) 
167   {
168     AliMUONTrack * track = (AliMUONTrack*) fRecTracksPtr->At(i);
169     track->SetUniqueID(i+1);
170     trackStore.Add(*track);
171   }
172 }
173
174   //__________________________________________________________________________
175 TClonesArray* AliMUONVTrackReconstructor::MakeSegmentsBetweenChambers(const AliMUONVClusterStore& clusterStore, Int_t ch1, Int_t ch2)
176 {
177   /// To make the list of segments from the list of clusters in the 2 given chambers.
178   /// Return a new TClonesArray of segments.
179   /// It is the responsibility of the user to delete it afterward.
180   AliDebug(1,Form("Enter MakeSegmentsBetweenChambers (1..) %d-%d", ch1+1, ch2+1));
181   
182   AliMUONVCluster *cluster1, *cluster2;
183   AliMUONObjectPair *segment;
184   Double_t nonBendingSlope = 0, bendingSlope = 0, impactParam = 0., bendingMomentum = 0.; // to avoid compilation warning
185   
186   // Create iterators to loop over clusters in both chambers
187   TIter nextInCh1(clusterStore.CreateChamberIterator(ch1,ch1));
188   TIter nextInCh2(clusterStore.CreateChamberIterator(ch2,ch2));
189   
190   // list of segments
191   TClonesArray *segments = new TClonesArray("AliMUONObjectPair", 100);
192   
193   // Loop over clusters in the first chamber of the station
194   while ( ( cluster1 = static_cast<AliMUONVCluster*>(nextInCh1()) ) ) {
195     
196     // reset cluster iterator of chamber 2
197     nextInCh2.Reset();
198     
199     // Loop over clusters in the second chamber of the station
200     while ( ( cluster2 = static_cast<AliMUONVCluster*>(nextInCh2()) ) ) {
201       
202       // non bending slope
203       nonBendingSlope = (cluster1->GetX() - cluster2->GetX()) / (cluster1->GetZ() - cluster2->GetZ());
204       
205       // check if non bending slope is within tolerances
206       if (TMath::Abs(nonBendingSlope) > GetRecoParam()->GetMaxNonBendingSlope()) continue;
207       
208       // bending slope
209       bendingSlope = (cluster1->GetY() - cluster2->GetY()) / (cluster1->GetZ() - cluster2->GetZ());
210       
211       // check the bending momentum of the bending slope depending if the field is ON or OFF
212       if (AliMUONTrackExtrap::IsFieldON()) {
213         
214         // impact parameter
215         impactParam = cluster1->GetY() - cluster1->GetZ() * bendingSlope;
216         
217         // absolute value of bending momentum
218         bendingMomentum = TMath::Abs(AliMUONTrackExtrap::GetBendingMomentumFromImpactParam(impactParam));
219         
220         // check if bending momentum is within tolerances
221         if (bendingMomentum < GetRecoParam()->GetMinBendingMomentum() ||
222             bendingMomentum > GetRecoParam()->GetMaxBendingMomentum()) continue;
223         
224       } else {
225         
226         // check if non bending slope is within tolerances
227         if (TMath::Abs(bendingSlope) > GetRecoParam()->GetMaxBendingSlope()) continue;
228       
229       }
230       // make new segment
231       segment = new ((*segments)[segments->GetLast()+1]) AliMUONObjectPair(cluster1, cluster2, kFALSE, kFALSE);
232       
233       // Printout for debug
234       if (AliLog::GetGlobalDebugLevel() > 1) {
235         cout << "segmentIndex(0...): " << segments->GetLast() << endl;
236         segment->Dump();
237         cout << "Cluster in first chamber" << endl;
238         cluster1->Print();
239         cout << "Cluster in second chamber" << endl;
240         cluster2->Print();
241       }
242       
243     }
244     
245   }
246   
247   // Printout for debug
248   AliDebug(1,Form("chambers%d-%d: NSegments =  %d ", ch1+1, ch2+1, segments->GetEntriesFast()));
249   
250   return segments;
251 }
252
253   //__________________________________________________________________________
254 void AliMUONVTrackReconstructor::RemoveIdenticalTracks()
255 {
256   /// To remove identical tracks:
257   /// Tracks are considered identical if they have all their clusters in common.
258   /// One keeps the track with the larger number of clusters if need be
259   AliMUONTrack *track1, *track2, *trackToRemove;
260   Int_t clustersInCommon, nClusters1, nClusters2;
261   Bool_t removedTrack1;
262   // Loop over first track of the pair
263   track1 = (AliMUONTrack*) fRecTracksPtr->First();
264   while (track1) {
265     removedTrack1 = kFALSE;
266     nClusters1 = track1->GetNClusters();
267     // Loop over second track of the pair
268     track2 = (AliMUONTrack*) fRecTracksPtr->After(track1);
269     while (track2) {
270       nClusters2 = track2->GetNClusters();
271       // number of clusters in common between two tracks
272       clustersInCommon = track1->ClustersInCommon(track2);
273       // check for identical tracks
274       if ((clustersInCommon == nClusters1) || (clustersInCommon == nClusters2)) {
275         // decide which track to remove
276         if (nClusters2 > nClusters1) {
277           // remove track1 and continue the first loop with the track next to track1
278           trackToRemove = track1;
279           track1 = (AliMUONTrack*) fRecTracksPtr->After(track1);
280           fRecTracksPtr->Remove(trackToRemove);
281           fRecTracksPtr->Compress(); // this is essential to retrieve the TClonesArray afterwards
282           fNRecTracks--;
283           removedTrack1 = kTRUE;
284           break;
285         } else {
286           // remove track2 and continue the second loop with the track next to track2
287           trackToRemove = track2;
288           track2 = (AliMUONTrack*) fRecTracksPtr->After(track2);
289           fRecTracksPtr->Remove(trackToRemove);
290           fRecTracksPtr->Compress(); // this is essential to retrieve the TClonesArray afterwards
291           fNRecTracks--;
292         }
293       } else track2 = (AliMUONTrack*) fRecTracksPtr->After(track2);
294     } // track2
295     if (removedTrack1) continue;
296     track1 = (AliMUONTrack*) fRecTracksPtr->After(track1);
297   } // track1
298   return;
299 }
300
301   //__________________________________________________________________________
302 void AliMUONVTrackReconstructor::RemoveDoubleTracks()
303 {
304   /// To remove double tracks:
305   /// Tracks are considered identical if more than half of the clusters of the track
306   /// which has the smaller number of clusters are in common with the other track.
307   /// Among two identical tracks, one keeps the track with the larger number of clusters
308   /// or, if these numbers are equal, the track with the minimum chi2.
309   AliMUONTrack *track1, *track2, *trackToRemove;
310   Int_t clustersInCommon, nClusters1, nClusters2;
311   Bool_t removedTrack1;
312   // Loop over first track of the pair
313   track1 = (AliMUONTrack*) fRecTracksPtr->First();
314   while (track1) {
315     removedTrack1 = kFALSE;
316     nClusters1 = track1->GetNClusters();
317     // Loop over second track of the pair
318     track2 = (AliMUONTrack*) fRecTracksPtr->After(track1);
319     while (track2) {
320       nClusters2 = track2->GetNClusters();
321       // number of clusters in common between two tracks
322       clustersInCommon = track1->ClustersInCommon(track2);
323       // check for identical tracks
324       if (((nClusters1 < nClusters2) && (2 * clustersInCommon > nClusters1)) || (2 * clustersInCommon > nClusters2)) {
325         // decide which track to remove
326         if ((nClusters1 > nClusters2) || ((nClusters1 == nClusters2) && (track1->GetGlobalChi2() <= track2->GetGlobalChi2()))) {
327           // remove track2 and continue the second loop with the track next to track2
328           trackToRemove = track2;
329           track2 = (AliMUONTrack*) fRecTracksPtr->After(track2);
330           fRecTracksPtr->Remove(trackToRemove);
331           fRecTracksPtr->Compress(); // this is essential to retrieve the TClonesArray afterwards
332           fNRecTracks--;
333         } else {
334           // else remove track1 and continue the first loop with the track next to track1
335           trackToRemove = track1;
336           track1 = (AliMUONTrack*) fRecTracksPtr->After(track1);
337           fRecTracksPtr->Remove(trackToRemove);
338           fRecTracksPtr->Compress(); // this is essential to retrieve the TClonesArray afterwards
339           fNRecTracks--;
340           removedTrack1 = kTRUE;
341           break;
342         }
343       } else track2 = (AliMUONTrack*) fRecTracksPtr->After(track2);
344     } // track2
345     if (removedTrack1) continue;
346     track1 = (AliMUONTrack*) fRecTracksPtr->After(track1);
347   } // track1
348   return;
349 }
350
351   //__________________________________________________________________________
352 void AliMUONVTrackReconstructor::AskForNewClustersInChamber(const AliMUONTrackParam &trackParam,
353                                                             AliMUONVClusterStore& clusterStore, Int_t chamber)
354 {
355   /// Ask the clustering to reconstruct new clusters around the track candidate position
356   
357   // check if the current chamber is useable
358   if (!fClusterServer || !GetRecoParam()->UseChamber(chamber)) return;
359   
360   // maximum distance between the center of the chamber and a detection element
361   // (accounting for the inclination of the chamber)
362   static const Double_t kMaxDZ = 15.; // 15 cm
363   
364   // extrapolate track parameters to the chamber
365   AliMUONTrackParam extrapTrackParam(trackParam);
366   AliMUONTrackExtrap::ExtrapToZCov(&extrapTrackParam, AliMUONConstants::DefaultChamberZ(chamber));
367   
368   // build the searching area using the track resolution and the maximum-distance-to-track value
369   const TMatrixD& kParamCov = extrapTrackParam.GetCovariances();
370   Double_t errX2 = kParamCov(0,0) + kMaxDZ * kMaxDZ * kParamCov(1,1) + 2. * kMaxDZ * TMath::Abs(kParamCov(0,1));
371   Double_t errY2 = kParamCov(2,2) + kMaxDZ * kMaxDZ * kParamCov(3,3) + 2. * kMaxDZ * TMath::Abs(kParamCov(2,3));
372   Double_t dX = TMath::Abs(trackParam.GetNonBendingSlope()) * kMaxDZ +
373                 GetRecoParam()->GetMaxNonBendingDistanceToTrack() +
374                 GetRecoParam()->GetSigmaCutForTracking() * TMath::Sqrt(errX2);
375   Double_t dY = TMath::Abs(trackParam.GetBendingSlope()) * kMaxDZ +
376                 GetRecoParam()->GetMaxBendingDistanceToTrack() +
377                 GetRecoParam()->GetSigmaCutForTracking() * TMath::Sqrt(errY2);
378   TVector2 dimensions(dX, dY);
379   TVector2 position(extrapTrackParam.GetNonBendingCoor(), extrapTrackParam.GetBendingCoor());
380   AliMpArea area(position, dimensions);
381   
382   // ask to cluterize in the given area of the given chamber
383   fClusterServer->Clusterize(chamber, clusterStore, area);
384   
385 }
386
387   //__________________________________________________________________________
388 void AliMUONVTrackReconstructor::AskForNewClustersInStation(const AliMUONTrackParam &trackParam,
389                                                             AliMUONVClusterStore& clusterStore, Int_t station)
390 {
391   /// Ask the clustering to reconstruct new clusters around the track candidate position
392   /// in the 2 chambers of the given station
393   AskForNewClustersInChamber(trackParam, clusterStore, 2*station+1);
394   AskForNewClustersInChamber(trackParam, clusterStore, 2*station);
395 }
396
397   //__________________________________________________________________________
398 Double_t AliMUONVTrackReconstructor::TryOneCluster(const AliMUONTrackParam &trackParam, AliMUONVCluster* cluster,
399                                                    AliMUONTrackParam &trackParamAtCluster, Bool_t updatePropagator)
400 {
401 /// Test the compatibility between the track and the cluster (using trackParam's covariance matrix):
402 /// return the corresponding Chi2
403 /// return trackParamAtCluster
404   
405   // extrapolate track parameters and covariances at the z position of the tested cluster
406   trackParamAtCluster = trackParam;
407   AliMUONTrackExtrap::ExtrapToZCov(&trackParamAtCluster, cluster->GetZ(), updatePropagator);
408   
409   // set pointer to cluster into trackParamAtCluster
410   trackParamAtCluster.SetClusterPtr(cluster);
411   
412   // Set differences between trackParam and cluster in the bending and non bending directions
413   Double_t dX = cluster->GetX() - trackParamAtCluster.GetNonBendingCoor();
414   Double_t dY = cluster->GetY() - trackParamAtCluster.GetBendingCoor();
415   
416   // Calculate errors and covariances
417   const TMatrixD& kParamCov = trackParamAtCluster.GetCovariances();
418   Double_t sigmaX2 = kParamCov(0,0) + cluster->GetErrX2();
419   Double_t sigmaY2 = kParamCov(2,2) + cluster->GetErrY2();
420   
421   // Compute chi2
422   return dX * dX / sigmaX2 + dY * dY / sigmaY2;
423   
424 }
425
426   //__________________________________________________________________________
427 Bool_t AliMUONVTrackReconstructor::TryOneClusterFast(const AliMUONTrackParam &trackParam, AliMUONVCluster* cluster)
428 {
429 /// Test the compatibility between the track and the cluster
430 /// given the track resolution + the maximum-distance-to-track value
431 /// and assuming linear propagation of the track:
432 /// return kTRUE if they are compatibles
433   
434   Double_t dZ = cluster->GetZ() - trackParam.GetZ();
435   Double_t dX = cluster->GetX() - (trackParam.GetNonBendingCoor() + trackParam.GetNonBendingSlope() * dZ);
436   Double_t dY = cluster->GetY() - (trackParam.GetBendingCoor() + trackParam.GetBendingSlope() * dZ);
437   const TMatrixD& kParamCov = trackParam.GetCovariances();
438   Double_t errX2 = kParamCov(0,0) + dZ * dZ * kParamCov(1,1) + 2. * dZ * kParamCov(0,1);
439   Double_t errY2 = kParamCov(2,2) + dZ * dZ * kParamCov(3,3) + 2. * dZ * kParamCov(2,3);
440
441   Double_t dXmax = GetRecoParam()->GetSigmaCutForTracking() * TMath::Sqrt(errX2) +
442                    GetRecoParam()->GetMaxNonBendingDistanceToTrack();
443   Double_t dYmax = GetRecoParam()->GetSigmaCutForTracking() * TMath::Sqrt(errY2) +
444                    GetRecoParam()->GetMaxBendingDistanceToTrack();
445   
446   if (TMath::Abs(dX) > dXmax || TMath::Abs(dY) > dYmax) return kFALSE;
447   
448   return kTRUE;
449   
450 }
451
452   //__________________________________________________________________________
453 Double_t AliMUONVTrackReconstructor::TryTwoClustersFast(const AliMUONTrackParam &trackParamAtCluster1, AliMUONVCluster* cluster2,
454                                                         AliMUONTrackParam &trackParamAtCluster2)
455 {
456 /// Test the compatibility between the track and the 2 clusters together (using trackParam's covariance matrix)
457 /// assuming linear propagation between the two clusters:
458 /// return the corresponding Chi2 accounting for covariances between the 2 clusters
459 /// return trackParamAtCluster2
460   
461   // extrapolate linearly track parameters and covariances at the z position of the second cluster
462   trackParamAtCluster2 = trackParamAtCluster1;
463   AliMUONTrackExtrap::LinearExtrapToZ(&trackParamAtCluster2, cluster2->GetZ());
464   
465   // set pointer to cluster2 into trackParamAtCluster2
466   trackParamAtCluster2.SetClusterPtr(cluster2);
467   
468   // Set differences between track and clusters in the bending and non bending directions
469   AliMUONVCluster* cluster1 = trackParamAtCluster1.GetClusterPtr();
470   Double_t dX1 = cluster1->GetX() - trackParamAtCluster1.GetNonBendingCoor();
471   Double_t dX2 = cluster2->GetX() - trackParamAtCluster2.GetNonBendingCoor();
472   Double_t dY1 = cluster1->GetY() - trackParamAtCluster1.GetBendingCoor();
473   Double_t dY2 = cluster2->GetY() - trackParamAtCluster2.GetBendingCoor();
474   
475   // Calculate errors and covariances
476   const TMatrixD& kParamCov1 = trackParamAtCluster1.GetCovariances();
477   const TMatrixD& kParamCov2 = trackParamAtCluster2.GetCovariances();
478   Double_t dZ = trackParamAtCluster2.GetZ() - trackParamAtCluster1.GetZ();
479   Double_t sigma2X1 = kParamCov1(0,0) + cluster1->GetErrX2();
480   Double_t sigma2X2 = kParamCov2(0,0) + cluster2->GetErrX2();
481   Double_t covX1X2  = kParamCov1(0,0) + dZ * kParamCov1(0,1);
482   Double_t sigma2Y1 = kParamCov1(2,2) + cluster1->GetErrY2();
483   Double_t sigma2Y2 = kParamCov2(2,2) + cluster2->GetErrY2();
484   Double_t covY1Y2  = kParamCov1(2,2) + dZ * kParamCov1(2,3);
485   
486   // Compute chi2
487   Double_t detX = sigma2X1 * sigma2X2 - covX1X2 * covX1X2;
488   Double_t detY = sigma2Y1 * sigma2Y2 - covY1Y2 * covY1Y2;
489   if (detX == 0. || detY == 0.) return 1.e10;
490   return   (dX1 * dX1 * sigma2X2 + dX2 * dX2 * sigma2X1 - 2. * dX1 * dX2 * covX1X2) / detX
491          + (dY1 * dY1 * sigma2Y2 + dY2 * dY2 * sigma2Y1 - 2. * dY1 * dY2 * covY1Y2) / detY;
492   
493 }
494
495   //__________________________________________________________________________
496 Bool_t AliMUONVTrackReconstructor::FollowLinearTrackInChamber(AliMUONTrack &trackCandidate, const AliMUONVClusterStore& clusterStore,
497                                                               Int_t nextChamber)
498 {
499   /// Follow trackCandidate in chamber(0..) nextChamber assuming linear propagation, and search for compatible cluster(s)
500   /// Keep all possibilities or only the best one(s) according to the flag fgkTrackAllTracks:
501   /// kTRUE:  duplicate "trackCandidate" if there are several possibilities and add the new tracks at the end of
502   ///         fRecTracksPtr to avoid conficts with other track candidates at this current stage of the tracking procedure.
503   ///         Remove the obsolete "trackCandidate" at the end.
504   /// kFALSE: add only the best cluster(s) to the "trackCandidate". Try to add a couple of clusters in priority.
505   /// return kTRUE if new cluster(s) have been found (otherwise return kFALSE)
506   AliDebug(1,Form("Enter FollowLinearTrackInChamber(1..) %d", nextChamber+1));
507   
508   Double_t chi2WithOneCluster = 1.e10;
509   Double_t maxChi2WithOneCluster = 2. * GetRecoParam()->GetSigmaCutForTracking() *
510                                         GetRecoParam()->GetSigmaCutForTracking(); // 2 because 2 quantities in chi2
511   Double_t bestChi2WithOneCluster = maxChi2WithOneCluster;
512   Bool_t foundOneCluster = kFALSE;
513   AliMUONTrack *newTrack = 0x0;
514   AliMUONVCluster *cluster;
515   AliMUONTrackParam trackParam;
516   AliMUONTrackParam extrapTrackParamAtCluster;
517   AliMUONTrackParam bestTrackParamAtCluster;
518   
519   // Get track parameters according to the propagation direction
520   if (nextChamber > 7) trackParam = *(AliMUONTrackParam*)trackCandidate.GetTrackParamAtCluster()->Last();
521   else trackParam = *(AliMUONTrackParam*)trackCandidate.GetTrackParamAtCluster()->First();
522   
523   // Printout for debuging
524   if ((AliLog::GetDebugLevel("MUON","AliMUONVTrackReconstructor") >= 2) || (AliLog::GetGlobalDebugLevel() >= 2)) {
525     cout<<endl<<"Track parameters and covariances at first cluster:"<<endl;
526     trackParam.GetParameters().Print();
527     trackParam.GetCovariances().Print();
528   }
529   
530   // Add MCS effect
531   AliMUONTrackExtrap::AddMCSEffect(&trackParam,AliMUONConstants::ChamberThicknessInX0(),1.);
532   
533   // Printout for debuging
534   if ((AliLog::GetDebugLevel("MUON","AliMUONVTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
535     cout << "FollowLinearTrackInChamber: look for cluster in chamber(1..): " << nextChamber+1 << endl;
536   }
537   
538   // Create iterators to loop over clusters in chamber
539   TIter next(clusterStore.CreateChamberIterator(nextChamber,nextChamber));
540   
541   // look for candidates in chamber
542   while ( ( cluster = static_cast<AliMUONVCluster*>(next()) ) ) {
543     
544     // try to add the current cluster fast
545     if (!TryOneClusterFast(trackParam, cluster)) continue;
546     
547     // try to add the current cluster accuratly
548     extrapTrackParamAtCluster = trackParam;
549     AliMUONTrackExtrap::LinearExtrapToZ(&extrapTrackParamAtCluster, cluster->GetZ());
550     chi2WithOneCluster = TryOneCluster(extrapTrackParamAtCluster, cluster, extrapTrackParamAtCluster);
551     
552     // if good chi2 then consider to add cluster
553     if (chi2WithOneCluster < maxChi2WithOneCluster) {
554       foundOneCluster = kTRUE;
555       
556       // Printout for debuging
557       if ((AliLog::GetDebugLevel("MUON","AliMUONVTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
558         cout << "FollowLinearTrackInChamber: found one cluster in chamber(1..): " << nextChamber+1
559         << " (Chi2 = " << chi2WithOneCluster << ")" << endl;
560         cluster->Print();
561       }
562       
563       if (GetRecoParam()->TrackAllTracks()) {
564         // copy trackCandidate into a new track put at the end of fRecTracksPtr and add the new cluster
565         newTrack = new ((*fRecTracksPtr)[fRecTracksPtr->GetLast()+1]) AliMUONTrack(trackCandidate);
566         if (GetRecoParam()->RequestStation(nextChamber/2))
567           extrapTrackParamAtCluster.SetRemovable(kFALSE);
568         else extrapTrackParamAtCluster.SetRemovable(kTRUE);
569         newTrack->AddTrackParamAtCluster(extrapTrackParamAtCluster,*cluster);
570         fNRecTracks++;
571         
572         // Printout for debuging
573         if ((AliLog::GetDebugLevel("MUON","AliMUONVTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
574           cout << "FollowLinearTrackInChamber: added one cluster in chamber(1..): " << nextChamber+1 << endl;
575           if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
576         }
577         
578       } else if (chi2WithOneCluster < bestChi2WithOneCluster) {
579         // keep track of the best cluster
580         bestChi2WithOneCluster = chi2WithOneCluster;
581         bestTrackParamAtCluster = extrapTrackParamAtCluster;
582       }
583       
584     }
585     
586   }
587   
588   // fill out the best track if required else clean up the fRecTracksPtr array
589   if (!GetRecoParam()->TrackAllTracks()) {
590     if (foundOneCluster) {
591       if (GetRecoParam()->RequestStation(nextChamber/2))
592         bestTrackParamAtCluster.SetRemovable(kFALSE);
593       else bestTrackParamAtCluster.SetRemovable(kTRUE);
594       trackCandidate.AddTrackParamAtCluster(bestTrackParamAtCluster,*(bestTrackParamAtCluster.GetClusterPtr()));
595       
596       // Printout for debuging
597       if ((AliLog::GetDebugLevel("MUON","AliMUONVTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
598         cout << "FollowLinearTrackInChamber: added the best cluster in chamber(1..): " << bestTrackParamAtCluster.GetClusterPtr()->GetChamberId()+1 << endl;
599         if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
600       }
601       
602     } else return kFALSE;
603     
604   } else if (foundOneCluster) {
605     
606     // remove obsolete track
607     fRecTracksPtr->Remove(&trackCandidate);
608     fNRecTracks--;
609     
610   } else return kFALSE;
611   
612   return kTRUE;
613   
614 }
615
616 //__________________________________________________________________________
617 Bool_t AliMUONVTrackReconstructor::FollowLinearTrackInStation(AliMUONTrack &trackCandidate, const AliMUONVClusterStore& clusterStore,
618                                                               Int_t nextStation)
619 {
620   /// Follow trackCandidate in station(0..) nextStation assuming linear propagation, and search for compatible cluster(s)
621   /// Keep all possibilities or only the best one(s) according to the flag fgkTrackAllTracks:
622   /// kTRUE:  duplicate "trackCandidate" if there are several possibilities and add the new tracks at the end of
623   ///         fRecTracksPtr to avoid conficts with other track candidates at this current stage of the tracking procedure.
624   ///         Remove the obsolete "trackCandidate" at the end.
625   /// kFALSE: add only the best cluster(s) to the "trackCandidate". Try to add a couple of clusters in priority.
626   /// return kTRUE if new cluster(s) have been found (otherwise return kFALSE)
627   AliDebug(1,Form("Enter FollowLinearTrackInStation(1..) %d", nextStation+1));
628   
629   // Order the chamber according to the propagation direction (tracking starts with chamber 2):
630   // - nextStation == station(1...) 5 => forward propagation
631   // - nextStation < station(1...) 5 => backward propagation
632   Int_t ch1, ch2;
633   if (nextStation==4) {
634     ch1 = 2*nextStation+1;
635     ch2 = 2*nextStation;
636   } else {
637     ch1 = 2*nextStation;
638     ch2 = 2*nextStation+1;
639   }
640   
641   Double_t chi2WithOneCluster = 1.e10;
642   Double_t chi2WithTwoClusters = 1.e10;
643   Double_t maxChi2WithOneCluster = 2. * GetRecoParam()->GetSigmaCutForTracking() *
644                                         GetRecoParam()->GetSigmaCutForTracking(); // 2 because 2 quantities in chi2
645   Double_t maxChi2WithTwoClusters = 4. * GetRecoParam()->GetSigmaCutForTracking() *
646                                          GetRecoParam()->GetSigmaCutForTracking(); // 4 because 4 quantities in chi2
647   Double_t bestChi2WithOneCluster = maxChi2WithOneCluster;
648   Double_t bestChi2WithTwoClusters = maxChi2WithTwoClusters;
649   Bool_t foundOneCluster = kFALSE;
650   Bool_t foundTwoClusters = kFALSE;
651   AliMUONTrack *newTrack = 0x0;
652   AliMUONVCluster *clusterCh1, *clusterCh2;
653   AliMUONTrackParam trackParam;
654   AliMUONTrackParam extrapTrackParamAtCluster1;
655   AliMUONTrackParam extrapTrackParamAtCluster2;
656   AliMUONTrackParam bestTrackParamAtCluster1;
657   AliMUONTrackParam bestTrackParamAtCluster2;
658   
659   Int_t nClusters = clusterStore.GetSize();
660   Bool_t *clusterCh1Used = new Bool_t[nClusters];
661   for (Int_t i = 0; i < nClusters; i++) clusterCh1Used[i] = kFALSE;
662   Int_t iCluster1;
663   
664   // Get track parameters according to the propagation direction
665   if (nextStation==4) trackParam = *(AliMUONTrackParam*)trackCandidate.GetTrackParamAtCluster()->Last();
666   else trackParam = *(AliMUONTrackParam*)trackCandidate.GetTrackParamAtCluster()->First();
667   
668   // Printout for debuging
669   if ((AliLog::GetDebugLevel("MUON","AliMUONVTrackReconstructor") >= 2) || (AliLog::GetGlobalDebugLevel() >= 2)) {
670     cout<<endl<<"Track parameters and covariances at first cluster:"<<endl;
671     trackParam.GetParameters().Print();
672     trackParam.GetCovariances().Print();
673   }
674   
675   // Add MCS effect
676   AliMUONTrackExtrap::AddMCSEffect(&trackParam,AliMUONConstants::ChamberThicknessInX0(),1.);
677   
678   // Printout for debuging
679   if ((AliLog::GetDebugLevel("MUON","AliMUONVTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
680     cout << "FollowLinearTrackInStation: look for clusters in chamber(1..): " << ch2+1 << endl;
681   }
682   
683   // Create iterators to loop over clusters in both chambers
684   TIter nextInCh1(clusterStore.CreateChamberIterator(ch1,ch1));
685   TIter nextInCh2(clusterStore.CreateChamberIterator(ch2,ch2));
686   
687   // look for candidates in chamber 2
688   while ( ( clusterCh2 = static_cast<AliMUONVCluster*>(nextInCh2()) ) ) {
689     
690     // try to add the current cluster fast
691     if (!TryOneClusterFast(trackParam, clusterCh2)) continue;
692     
693     // try to add the current cluster accuratly
694     extrapTrackParamAtCluster2 = trackParam;
695     AliMUONTrackExtrap::LinearExtrapToZ(&extrapTrackParamAtCluster2, clusterCh2->GetZ());
696     chi2WithOneCluster = TryOneCluster(extrapTrackParamAtCluster2, clusterCh2, extrapTrackParamAtCluster2);
697     
698     // if good chi2 then try to attach a cluster in the other chamber too
699     if (chi2WithOneCluster < maxChi2WithOneCluster) {
700       Bool_t foundSecondCluster = kFALSE;
701       
702       // Printout for debuging
703       if ((AliLog::GetDebugLevel("MUON","AliMUONVTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
704         cout << "FollowLinearTrackInStation: found one cluster in chamber(1..): " << ch2+1
705              << " (Chi2 = " << chi2WithOneCluster << ")" << endl;
706         clusterCh2->Print();
707         cout << "                      look for second clusters in chamber(1..): " << ch1+1 << " ..." << endl;
708       }
709       
710       // add MCS effect
711       AliMUONTrackExtrap::AddMCSEffect(&extrapTrackParamAtCluster2,AliMUONConstants::ChamberThicknessInX0(),1.);
712       
713       // reset cluster iterator of chamber 1
714       nextInCh1.Reset();
715       iCluster1 = -1;
716       
717       // look for second candidates in chamber 1
718       while ( ( clusterCh1 = static_cast<AliMUONVCluster*>(nextInCh1()) ) ) {
719         iCluster1++;
720         
721         // try to add the current cluster fast
722         if (!TryOneClusterFast(extrapTrackParamAtCluster2, clusterCh1)) continue;
723         
724         // try to add the current cluster in addition to the one found in the previous chamber
725         chi2WithTwoClusters = TryTwoClustersFast(extrapTrackParamAtCluster2, clusterCh1, extrapTrackParamAtCluster1);
726         
727         // if good chi2 then consider to add the 2 clusters to the "trackCandidate"
728         if (chi2WithTwoClusters < maxChi2WithTwoClusters) {
729           foundSecondCluster = kTRUE;
730           foundTwoClusters = kTRUE;
731           
732           // Printout for debuging
733           if ((AliLog::GetDebugLevel("MUON","AliMUONVTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
734             cout << "FollowLinearTrackInStation: found one cluster in chamber(1..): " << ch1+1
735                  << " (Chi2 = " << chi2WithTwoClusters << ")" << endl;
736             clusterCh1->Print();
737           }
738           
739           if (GetRecoParam()->TrackAllTracks()) {
740             // copy trackCandidate into a new track put at the end of fRecTracksPtr and add the new clusters
741             newTrack = new ((*fRecTracksPtr)[fRecTracksPtr->GetLast()+1]) AliMUONTrack(trackCandidate);
742             extrapTrackParamAtCluster1.SetRemovable(kTRUE);
743             newTrack->AddTrackParamAtCluster(extrapTrackParamAtCluster1,*clusterCh1);
744             extrapTrackParamAtCluster2.SetRemovable(kTRUE);
745             newTrack->AddTrackParamAtCluster(extrapTrackParamAtCluster2,*clusterCh2);
746             fNRecTracks++;
747             
748             // Tag clusterCh1 as used
749             clusterCh1Used[iCluster1] = kTRUE;
750             
751             // Printout for debuging
752             if ((AliLog::GetDebugLevel("MUON","AliMUONVTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
753               cout << "FollowLinearTrackInStation: added two clusters in station(1..): " << nextStation+1 << endl;
754               if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
755             }
756             
757           } else if (chi2WithTwoClusters < bestChi2WithTwoClusters) {
758             // keep track of the best couple of clusters
759             bestChi2WithTwoClusters = chi2WithTwoClusters;
760             bestTrackParamAtCluster1 = extrapTrackParamAtCluster1;
761             bestTrackParamAtCluster2 = extrapTrackParamAtCluster2;
762           }
763           
764         }
765         
766       }
767       
768       // if no cluster found in chamber1 then consider to add clusterCh2 only
769       if (!foundSecondCluster) {
770         foundOneCluster = kTRUE;
771         
772         if (GetRecoParam()->TrackAllTracks()) {
773           // copy trackCandidate into a new track put at the end of fRecTracksPtr and add the new cluster
774           newTrack = new ((*fRecTracksPtr)[fRecTracksPtr->GetLast()+1]) AliMUONTrack(trackCandidate);
775           if (GetRecoParam()->RequestStation(nextStation))
776             extrapTrackParamAtCluster2.SetRemovable(kFALSE);
777           else extrapTrackParamAtCluster2.SetRemovable(kTRUE);
778           newTrack->AddTrackParamAtCluster(extrapTrackParamAtCluster2,*clusterCh2);
779           fNRecTracks++;
780           
781           // Printout for debuging
782           if ((AliLog::GetDebugLevel("MUON","AliMUONVTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
783             cout << "FollowLinearTrackInStation: added one cluster in chamber(1..): " << ch2+1 << endl;
784             if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
785           }
786           
787         } else if (!foundTwoClusters && chi2WithOneCluster < bestChi2WithOneCluster) {
788           // keep track of the best cluster except if a couple of clusters has already been found
789           bestChi2WithOneCluster = chi2WithOneCluster;
790           bestTrackParamAtCluster1 = extrapTrackParamAtCluster2;
791         }
792         
793       }
794       
795     }
796     
797   }
798   
799   // look for candidates in chamber 1 not already attached to a track
800   // if we want to keep all possible tracks or if no good couple of clusters has been found
801   if (GetRecoParam()->TrackAllTracks() || !foundTwoClusters) {
802     
803     // Printout for debuging
804     if ((AliLog::GetDebugLevel("MUON","AliMUONVTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
805       cout << "FollowLinearTrackInStation: look for single cluster in chamber(1..): " << ch1+1 << endl;
806     }
807     
808     //Extrapolate trackCandidate to chamber "ch2"
809     AliMUONTrackExtrap::LinearExtrapToZ(&trackParam, AliMUONConstants::DefaultChamberZ(ch2));
810     
811     // add MCS effect for next step
812     AliMUONTrackExtrap::AddMCSEffect(&trackParam,AliMUONConstants::ChamberThicknessInX0(),1.);
813     
814     // reset cluster iterator of chamber 1
815     nextInCh1.Reset();
816     iCluster1 = -1;
817     
818     // look for second candidates in chamber 1
819     while ( ( clusterCh1 = static_cast<AliMUONVCluster*>(nextInCh1()) ) ) {
820       iCluster1++;
821       
822       if (clusterCh1Used[iCluster1]) continue; // Skip clusters already used
823       
824       // try to add the current cluster fast
825       if (!TryOneClusterFast(trackParam, clusterCh1)) continue;
826       
827       // try to add the current cluster accuratly
828       extrapTrackParamAtCluster1 = trackParam;
829       AliMUONTrackExtrap::LinearExtrapToZ(&extrapTrackParamAtCluster1, clusterCh1->GetZ());
830       chi2WithOneCluster = TryOneCluster(extrapTrackParamAtCluster1, clusterCh1, extrapTrackParamAtCluster1);
831       
832       // if good chi2 then consider to add clusterCh1
833       // We do not try to attach a cluster in the other chamber too since it has already been done above
834       if (chi2WithOneCluster < maxChi2WithOneCluster) {
835         foundOneCluster = kTRUE;
836         
837         // Printout for debuging
838         if ((AliLog::GetDebugLevel("MUON","AliMUONVTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
839           cout << "FollowLinearTrackInStation: found one cluster in chamber(1..): " << ch1+1
840                << " (Chi2 = " << chi2WithOneCluster << ")" << endl;
841           clusterCh1->Print();
842         }
843         
844         if (GetRecoParam()->TrackAllTracks()) {
845           // copy trackCandidate into a new track put at the end of fRecTracksPtr and add the new cluster
846           newTrack = new ((*fRecTracksPtr)[fRecTracksPtr->GetLast()+1]) AliMUONTrack(trackCandidate);
847           if (GetRecoParam()->RequestStation(nextStation))
848             extrapTrackParamAtCluster1.SetRemovable(kFALSE);
849           else extrapTrackParamAtCluster1.SetRemovable(kTRUE);
850           newTrack->AddTrackParamAtCluster(extrapTrackParamAtCluster1,*clusterCh1);
851           fNRecTracks++;
852           
853           // Printout for debuging
854           if ((AliLog::GetDebugLevel("MUON","AliMUONVTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
855             cout << "FollowLinearTrackInStation: added one cluster in chamber(1..): " << ch1+1 << endl;
856             if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
857           }
858           
859         } else if (chi2WithOneCluster < bestChi2WithOneCluster) {
860           // keep track of the best cluster except if a couple of clusters has already been found
861           bestChi2WithOneCluster = chi2WithOneCluster;
862           bestTrackParamAtCluster1 = extrapTrackParamAtCluster1;
863         }
864         
865       }
866       
867     }
868     
869   }
870   
871   // fill out the best track if required else clean up the fRecTracksPtr array
872   if (!GetRecoParam()->TrackAllTracks()) {
873     if (foundTwoClusters) {
874       bestTrackParamAtCluster1.SetRemovable(kTRUE);
875       trackCandidate.AddTrackParamAtCluster(bestTrackParamAtCluster1,*(bestTrackParamAtCluster1.GetClusterPtr()));
876       bestTrackParamAtCluster2.SetRemovable(kTRUE);
877       trackCandidate.AddTrackParamAtCluster(bestTrackParamAtCluster2,*(bestTrackParamAtCluster2.GetClusterPtr()));
878       
879       // Printout for debuging
880       if ((AliLog::GetDebugLevel("MUON","AliMUONVTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
881         cout << "FollowLinearTrackInStation: added the two best clusters in station(1..): " << nextStation+1 << endl;
882         if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
883       }
884       
885     } else if (foundOneCluster) {
886       if (GetRecoParam()->RequestStation(nextStation))
887         bestTrackParamAtCluster1.SetRemovable(kFALSE);
888       else bestTrackParamAtCluster1.SetRemovable(kTRUE);
889       trackCandidate.AddTrackParamAtCluster(bestTrackParamAtCluster1,*(bestTrackParamAtCluster1.GetClusterPtr()));
890       
891       // Printout for debuging
892       if ((AliLog::GetDebugLevel("MUON","AliMUONVTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
893         cout << "FollowLinearTrackInStation: added the best cluster in chamber(1..): " << bestTrackParamAtCluster1.GetClusterPtr()->GetChamberId()+1 << endl;
894         if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
895       }
896       
897     } else {
898       delete [] clusterCh1Used;
899       return kFALSE;
900     }
901     
902   } else if (foundOneCluster || foundTwoClusters) {
903     
904     // remove obsolete track
905     fRecTracksPtr->Remove(&trackCandidate);
906     fNRecTracks--;
907     
908   } else {
909     delete [] clusterCh1Used;  
910     return kFALSE;
911   }
912   
913   delete [] clusterCh1Used;
914   return kTRUE;
915   
916 }
917
918 //__________________________________________________________________________
919 void AliMUONVTrackReconstructor::ImproveTracks()
920 {
921   /// Improve tracks by removing clusters with local chi2 highter than the defined cut
922   /// Recompute track parameters and covariances at the remaining clusters
923   AliDebug(1,"Enter ImproveTracks");
924   
925   AliMUONTrack *track, *nextTrack;
926   
927   track = (AliMUONTrack*) fRecTracksPtr->First();
928   while (track) {
929     
930     // prepare next track in case the actual track is suppressed
931     nextTrack = (AliMUONTrack*) fRecTracksPtr->After(track);
932     
933     ImproveTrack(*track);
934     
935     // remove track if improvement failed
936     if (!track->IsImproved()) {
937       fRecTracksPtr->Remove(track);
938       fNRecTracks--;
939     }
940     
941     track = nextTrack;
942   }
943   
944   // compress the array in case of some tracks have been removed
945   fRecTracksPtr->Compress();
946   
947 }
948
949 //__________________________________________________________________________
950 void AliMUONVTrackReconstructor::Finalize()
951 {
952   /// Recompute track parameters and covariances at each attached cluster from those at the first one
953   
954   AliMUONTrack *track;
955   
956   track = (AliMUONTrack*) fRecTracksPtr->First();
957   while (track) {
958     
959     FinalizeTrack(*track);
960     
961     track = (AliMUONTrack*) fRecTracksPtr->After(track);
962     
963   }
964   
965 }
966
967 //__________________________________________________________________________
968 void AliMUONVTrackReconstructor::ValidateTracksWithTrigger(AliMUONVTrackStore& trackStore,
969                                                            const AliMUONVTriggerTrackStore& triggerTrackStore,
970                                                            const AliMUONVTriggerStore& triggerStore,
971                                                            const AliMUONTrackHitPattern& trackHitPattern)
972 {
973   /// Try to match track from tracking system with trigger track
974   AliCodeTimerAuto("");
975
976   trackHitPattern.ExecuteValidation(trackStore, triggerTrackStore, triggerStore);
977 }
978
979   //__________________________________________________________________________
980 void AliMUONVTrackReconstructor::EventReconstructTrigger(const AliMUONTriggerCircuit& circuit,
981                                                          const AliMUONVTriggerStore& triggerStore,
982                                                          AliMUONVTriggerTrackStore& triggerTrackStore)
983 {
984   /// To make the trigger tracks from Local Trigger
985   AliDebug(1, "");
986   AliCodeTimerAuto("");
987   
988   AliMUONGlobalTrigger* globalTrigger = triggerStore.Global();
989   
990   UChar_t gloTrigPat = 0;
991
992   if (globalTrigger)
993   {
994     gloTrigPat = globalTrigger->GetGlobalResponse();
995   }
996   
997   TIter next(triggerStore.CreateIterator());
998   AliMUONLocalTrigger* locTrg(0x0);
999
1000   Float_t z11 = AliMUONConstants::DefaultChamberZ(10);
1001   Float_t z21 = AliMUONConstants::DefaultChamberZ(12);
1002       
1003   AliMUONTriggerTrack triggerTrack;
1004   
1005   while ( ( locTrg = static_cast<AliMUONLocalTrigger*>(next()) ) )
1006   {
1007     Bool_t xTrig=locTrg->IsTrigX();
1008     Bool_t yTrig=locTrg->IsTrigY();
1009     
1010     Int_t localBoardId = locTrg->LoCircuit();
1011     
1012     if (xTrig && yTrig) 
1013     { // make Trigger Track if trigger in X and Y
1014       
1015       Float_t y11 = circuit.GetY11Pos(localBoardId, locTrg->LoStripX()); 
1016       // need first to convert deviation to [0-30] 
1017       // (see AliMUONLocalTriggerBoard::LocalTrigger)
1018       Int_t deviation = locTrg->GetDeviation(); 
1019       Int_t stripX21 = locTrg->LoStripX()+deviation+1;
1020       Float_t y21 = circuit.GetY21Pos(localBoardId, stripX21);       
1021       Float_t x11 = circuit.GetX11Pos(localBoardId, locTrg->LoStripY());
1022       
1023       AliDebug(1, Form(" MakeTriggerTrack %d %d %d %d %f %f %f \n",locTrg->LoCircuit(),
1024                        locTrg->LoStripX(),locTrg->LoStripX()+locTrg->LoDev()+1,locTrg->LoStripY(),y11, y21, x11));
1025       
1026       Float_t thetax = TMath::ATan2( x11 , z11 );
1027       Float_t thetay = TMath::ATan2( (y21-y11) , (z21-z11) );
1028       
1029       triggerTrack.SetX11(x11);
1030       triggerTrack.SetY11(y11);
1031       triggerTrack.SetThetax(thetax);
1032       triggerTrack.SetThetay(thetay);
1033       triggerTrack.SetGTPattern(gloTrigPat);
1034       triggerTrack.SetLoTrgNum(localBoardId);
1035       
1036       triggerTrackStore.Add(triggerTrack);
1037     } // board is fired 
1038   } // end of loop on Local Trigger
1039 }
1040