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