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