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