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