]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONVTrackReconstructor.cxx
Compilation on Windows/Cygwin. Corrected dependences
[u/mrichter/AliRoot.git] / MUON / AliMUONVTrackReconstructor.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id$ */
17
18 //-----------------------------------------------------------------------------
19 /// \class AliMUONVTrackReconstructor
20 /// Virtual MUON track reconstructor in ALICE (class renamed from AliMUONEventReconstructor)
21 ///
22 /// This class contains as data a pointer to the array of reconstructed tracks
23 ///
24 /// It contains as methods, among others:
25 /// * EventReconstruct to build the muon tracks
26 /// * EventReconstructTrigger to build the trigger tracks
27 /// * ValidateTracksWithTrigger to match tracker/trigger tracks
28 ///
29 /// Several options and adjustable parameters are available for both KALMAN and ORIGINAL
30 /// tracking algorithms. They can be changed by using:
31 /// AliMUONRecoParam *muonRecoParam = AliMUONRecoParam::GetLow(High)FluxParam();
32 /// muonRecoParam->Set...(); // see methods in AliMUONRecoParam.h for details
33 /// AliMUONReconstructor::SetRecoParam(muonRecoParam);
34 ///
35 /// Main parameters and options are:
36 /// - *fgkSigmaToCutForTracking* : quality cut used to select new clusters to be
37 ///   attached to the track candidate and to select good tracks.
38 /// - *fgkMakeTrackCandidatesFast* : if this flag is set to 'true', the track candidates
39 ///   are made assuming linear propagation between stations 4 and 5.
40 /// - *fgkTrackAllTracks* : according to the value of this flag, in case that several
41 ///   new clusters pass the quality cut, either we consider all the possibilities
42 ///   (duplicating tracks) or we attach only the best cluster.
43 /// - *fgkRecoverTracks* : if this flag is set to 'true', we try to recover the tracks
44 ///   lost during the tracking by removing the worst of the 2 clusters attached in the
45 ///   previous station.
46 /// - *fgkImproveTracks* : if this flag is set to 'true', we try to improve the quality
47 ///   of the tracks at the end of the tracking by removing clusters that do not pass
48 ///   new quality cut (the track is removed is it does not contain enough cluster anymore).
49 /// - *fgkComplementTracks* : if this flag is set to 'true', we try to improve the quality
50 ///   of the tracks at the end of the tracking by adding potentially missing clusters
51 ///   (we may have 2 clusters in the same chamber because of the overlapping of detection
52 ///   elements, which is not handle by the tracking algorithm).
53 /// - *fgkSigmaToCutForImprovement* : quality cut used when we try to improve the
54 ///   quality of the tracks.
55 ///
56 ///  \author Philippe Pillot
57 //-----------------------------------------------------------------------------
58
59 #include "AliMUONVTrackReconstructor.h"
60
61 #include "AliMUONConstants.h"
62 #include "AliMUONObjectPair.h"
63 #include "AliMUONTriggerTrack.h"
64 #include "AliMUONTriggerCircuit.h"
65 #include "AliMUONLocalTrigger.h"
66 #include "AliMUONGlobalTrigger.h"
67 #include "AliMUONTrack.h"
68 #include "AliMUONTrackParam.h"
69 #include "AliMUONTrackExtrap.h"
70 #include "AliMUONTrackHitPattern.h"
71 #include "AliMUONVTrackStore.h"
72 #include "AliMUONVClusterStore.h"
73 #include "AliMUONVCluster.h"
74 #include "AliMUONVClusterServer.h"
75 #include "AliMUONVTriggerStore.h"
76 #include "AliMUONVTriggerTrackStore.h"
77 #include "AliMpDEManager.h"
78 #include "AliMpArea.h"
79
80 #include "AliLog.h"
81 #include "AliCodeTimer.h"
82 #include "AliTracker.h"
83
84 #include <TClonesArray.h>
85 #include <TMath.h>
86 #include <TMatrixD.h>
87 #include <TVector2.h>
88
89 #include <Riostream.h>
90
91 /// \cond CLASSIMP
92 ClassImp(AliMUONVTrackReconstructor) // Class implementation in ROOT context
93 /// \endcond
94
95   //__________________________________________________________________________
96 AliMUONVTrackReconstructor::AliMUONVTrackReconstructor(AliMUONVClusterServer& clusterServer)
97   : TObject(),
98     fRecTracksPtr(0x0),
99     fNRecTracks(0),
100   fClusterServer(clusterServer)
101 {
102   /// Constructor for class AliMUONVTrackReconstructor
103   
104   // Memory allocation for the TClonesArray of reconstructed tracks
105   fRecTracksPtr = new TClonesArray("AliMUONTrack", 100);
106   
107   // set the magnetic field for track extrapolations
108   const AliMagF* kField = AliTracker::GetFieldMap();
109   if (!kField) AliFatal("No field available");
110   AliMUONTrackExtrap::SetField(kField);
111 }
112
113   //__________________________________________________________________________
114 AliMUONVTrackReconstructor::~AliMUONVTrackReconstructor()
115 {
116   /// Destructor for class AliMUONVTrackReconstructor
117   delete fRecTracksPtr;
118 }
119
120   //__________________________________________________________________________
121 void AliMUONVTrackReconstructor::ResetTracks()
122 {
123   /// To reset the TClonesArray of reconstructed tracks
124   if (fRecTracksPtr) fRecTracksPtr->Clear("C");
125   fNRecTracks = 0;
126   return;
127 }
128
129   //__________________________________________________________________________
130 void AliMUONVTrackReconstructor::EventReconstruct(AliMUONVClusterStore& clusterStore, AliMUONVTrackStore& trackStore)
131 {
132   /// To reconstruct one event
133   AliDebug(1,"");
134   AliCodeTimerAuto("");
135   
136   // Reset array of tracks
137   ResetTracks();
138   
139   // Look for candidates from at least 3 aligned points in stations(1..) 4 and 5
140   MakeTrackCandidates(clusterStore);
141   
142   // Stop tracking if no candidate found
143   if (fRecTracksPtr->GetEntriesFast() == 0) return;
144   
145   // Follow tracks in stations(1..) 3, 2 and 1
146   FollowTracks(clusterStore);
147   
148   // Complement the reconstructed tracks
149   if (AliMUONReconstructor::GetRecoParam()->ComplementTracks()) ComplementTracks(clusterStore);
150   
151   // Improve the reconstructed tracks
152   if (AliMUONReconstructor::GetRecoParam()->ImproveTracks()) ImproveTracks();
153   
154   // Remove double tracks
155   RemoveDoubleTracks();
156   
157   // Fill AliMUONTrack data members
158   Finalize();
159   
160   // Add tracks to MUON data container 
161   for (Int_t i=0; i<fNRecTracks; ++i) 
162   {
163     AliMUONTrack * track = (AliMUONTrack*) fRecTracksPtr->At(i);
164     trackStore.Add(*track);
165   }
166 }
167
168   //__________________________________________________________________________
169 TClonesArray* AliMUONVTrackReconstructor::MakeSegmentsInStation(const AliMUONVClusterStore& clusterStore, Int_t station)
170 {
171   /// To make the list of segments in station(0..) "Station" from the list of clusters to be reconstructed.
172   /// Return a new TClonesArray of segments.
173   /// It is the responsibility of the user to delete it afterward.
174   AliDebug(1,Form("Enter MakeSegmentsPerStation (1..) %d",station+1));
175   
176   AliMUONVCluster *cluster1, *cluster2;
177   AliMUONObjectPair *segment;
178   Double_t bendingSlope = 0, impactParam = 0., bendingMomentum = 0.; // to avoid compilation warning
179   Int_t ch1 = 2 * station;
180   Int_t ch2 = ch1 + 1;
181   
182   // Create iterators to loop over clusters in both chambers
183   TIter nextInCh1(clusterStore.CreateChamberIterator(ch1,ch1));
184   TIter nextInCh2(clusterStore.CreateChamberIterator(ch2,ch2));
185   
186   // list of segments
187   TClonesArray *segments = new TClonesArray("AliMUONObjectPair", 100);
188   segments->SetOwner(kTRUE);
189   
190   // Loop over clusters in the first chamber of the station
191   while ( ( cluster1 = static_cast<AliMUONVCluster*>(nextInCh1()) ) ) {
192     
193     // reset cluster iterator of chamber 2
194     nextInCh2.Reset();
195     
196     // Loop over clusters in the second chamber of the station
197     while ( ( cluster2 = static_cast<AliMUONVCluster*>(nextInCh2()) ) ) {
198       
199       // bending slope
200       bendingSlope = (cluster1->GetY() - cluster2->GetY()) / (cluster1->GetZ() - cluster2->GetZ());
201       
202       // impact parameter
203       impactParam = cluster1->GetY() - cluster1->GetZ() * bendingSlope;
204      
205       // absolute value of bending momentum
206       bendingMomentum = TMath::Abs(AliMUONTrackExtrap::GetBendingMomentumFromImpactParam(impactParam));
207       
208       // check for bending momentum within tolerances
209       if ((bendingMomentum < AliMUONReconstructor::GetRecoParam()->GetMaxBendingMomentum()) &&
210           (bendingMomentum > AliMUONReconstructor::GetRecoParam()->GetMinBendingMomentum())) {
211         
212         // make new segment
213         segment = new ((*segments)[segments->GetLast()+1]) AliMUONObjectPair(cluster1, cluster2, kFALSE, kFALSE);
214         
215         // Printout for debug
216         if (AliLog::GetGlobalDebugLevel() > 1) {
217           cout << "segmentIndex(0...): " << segments->GetLast() << endl;
218           segment->Dump();
219           cout << "Cluster in first chamber" << endl;
220           cluster1->Print();
221           cout << "Cluster in second chamber" << endl;
222           cluster2->Print();
223         }
224         
225       }
226       
227     }
228     
229   }
230   
231   // Printout for debug
232   AliDebug(1,Form("Station: %d  NSegments:  %d ", station+1, segments->GetEntriesFast()));
233   
234   return segments;
235 }
236
237   //__________________________________________________________________________
238 void AliMUONVTrackReconstructor::RemoveIdenticalTracks()
239 {
240   /// To remove identical tracks:
241   /// Tracks are considered identical if they have all their clusters in common.
242   /// One keeps the track with the larger number of clusters if need be
243   AliMUONTrack *track1, *track2, *trackToRemove;
244   Int_t clustersInCommon, nClusters1, nClusters2;
245   Bool_t removedTrack1;
246   // Loop over first track of the pair
247   track1 = (AliMUONTrack*) fRecTracksPtr->First();
248   while (track1) {
249     removedTrack1 = kFALSE;
250     nClusters1 = track1->GetNClusters();
251     // Loop over second track of the pair
252     track2 = (AliMUONTrack*) fRecTracksPtr->After(track1);
253     while (track2) {
254       nClusters2 = track2->GetNClusters();
255       // number of clusters in common between two tracks
256       clustersInCommon = track1->ClustersInCommon(track2);
257       // check for identical tracks
258       if ((clustersInCommon == nClusters1) || (clustersInCommon == nClusters2)) {
259         // decide which track to remove
260         if (nClusters2 > nClusters1) {
261           // remove track1 and continue the first loop with the track next to track1
262           trackToRemove = track1;
263           track1 = (AliMUONTrack*) fRecTracksPtr->After(track1);
264           fRecTracksPtr->Remove(trackToRemove);
265           fRecTracksPtr->Compress(); // this is essential to retrieve the TClonesArray afterwards
266           fNRecTracks--;
267           removedTrack1 = kTRUE;
268           break;
269         } else {
270           // remove track2 and continue the second loop with the track next to track2
271           trackToRemove = track2;
272           track2 = (AliMUONTrack*) fRecTracksPtr->After(track2);
273           fRecTracksPtr->Remove(trackToRemove);
274           fRecTracksPtr->Compress(); // this is essential to retrieve the TClonesArray afterwards
275           fNRecTracks--;
276         }
277       } else track2 = (AliMUONTrack*) fRecTracksPtr->After(track2);
278     } // track2
279     if (removedTrack1) continue;
280     track1 = (AliMUONTrack*) fRecTracksPtr->After(track1);
281   } // track1
282   return;
283 }
284
285   //__________________________________________________________________________
286 void AliMUONVTrackReconstructor::RemoveDoubleTracks()
287 {
288   /// To remove double tracks:
289   /// Tracks are considered identical if more than half of the clusters of the track
290   /// which has the smaller number of clusters are in common with the other track.
291   /// Among two identical tracks, one keeps the track with the larger number of clusters
292   /// or, if these numbers are equal, the track with the minimum chi2.
293   AliMUONTrack *track1, *track2, *trackToRemove;
294   Int_t clustersInCommon, nClusters1, nClusters2;
295   Bool_t removedTrack1;
296   // Loop over first track of the pair
297   track1 = (AliMUONTrack*) fRecTracksPtr->First();
298   while (track1) {
299     removedTrack1 = kFALSE;
300     nClusters1 = track1->GetNClusters();
301     // Loop over second track of the pair
302     track2 = (AliMUONTrack*) fRecTracksPtr->After(track1);
303     while (track2) {
304       nClusters2 = track2->GetNClusters();
305       // number of clusters in common between two tracks
306       clustersInCommon = track1->ClustersInCommon(track2);
307       // check for identical tracks
308       if (((nClusters1 < nClusters2) && (2 * clustersInCommon > nClusters1)) || (2 * clustersInCommon > nClusters2)) {
309         // decide which track to remove
310         if ((nClusters1 > nClusters2) || ((nClusters1 == nClusters2) && (track1->GetGlobalChi2() <= track2->GetGlobalChi2()))) {
311           // remove track2 and continue the second loop with the track next to track2
312           trackToRemove = track2;
313           track2 = (AliMUONTrack*) fRecTracksPtr->After(track2);
314           fRecTracksPtr->Remove(trackToRemove);
315           fRecTracksPtr->Compress(); // this is essential to retrieve the TClonesArray afterwards
316           fNRecTracks--;
317         } else {
318           // else remove track1 and continue the first loop with the track next to track1
319           trackToRemove = track1;
320           track1 = (AliMUONTrack*) fRecTracksPtr->After(track1);
321           fRecTracksPtr->Remove(trackToRemove);
322           fRecTracksPtr->Compress(); // this is essential to retrieve the TClonesArray afterwards
323           fNRecTracks--;
324           removedTrack1 = kTRUE;
325           break;
326         }
327       } else track2 = (AliMUONTrack*) fRecTracksPtr->After(track2);
328     } // track2
329     if (removedTrack1) continue;
330     track1 = (AliMUONTrack*) fRecTracksPtr->After(track1);
331   } // track1
332   return;
333 }
334
335   //__________________________________________________________________________
336 void AliMUONVTrackReconstructor::AskForNewClustersInStation(const AliMUONTrackParam &trackParam,
337                                                             AliMUONVClusterStore& clusterStore, Int_t station)
338 {
339   /// Ask the clustering to reconstruct new clusters around the track candidate position
340   /// in the 2 chambers of the given station
341   
342   // maximum shift of the searching area due to distance between detection elements and the track slope
343   static const Double_t kgMaxShift = 2.; // 2 cm
344   
345   // extrapolate track parameters to the second chamber of the station
346   AliMUONTrackParam extrapTrackParam(trackParam);
347   AliMUONTrackExtrap::ExtrapToZ(&extrapTrackParam, AliMUONConstants::DefaultChamberZ(2*station+1));
348   
349   // build the searching area
350   TVector2 position(extrapTrackParam.GetNonBendingCoor(), extrapTrackParam.GetBendingCoor());
351   TVector2 dimensions(AliMUONReconstructor::GetRecoParam()->GetMaxNonBendingDistanceToTrack() + kgMaxShift,
352                       AliMUONReconstructor::GetRecoParam()->GetMaxBendingDistanceToTrack() + kgMaxShift);
353   AliMpArea area2(position, dimensions);
354   
355   // ask to cluterize in the given area of the given chamber
356   fClusterServer.Clusterize(2*station+1, clusterStore, area2);
357   
358   // extrapolate track parameters to the first chamber of the station
359   AliMUONTrackExtrap::ExtrapToZ(&extrapTrackParam, AliMUONConstants::DefaultChamberZ(2*station));
360   
361   // build the searching area
362   position.Set(extrapTrackParam.GetNonBendingCoor(), extrapTrackParam.GetBendingCoor());
363 //  dimensions.Set(AliMUONReconstructor::GetRecoParam()->GetMaxNonBendingDistanceToTrack() + kgMaxShift,
364 //               AliMUONReconstructor::GetRecoParam()->GetMaxBendingDistanceToTrack() + kgMaxShift);
365   AliMpArea area1(position, dimensions);
366   
367   // ask to cluterize in the given area of the given chamber
368   fClusterServer.Clusterize(2*station, clusterStore, area1);
369 }
370
371   //__________________________________________________________________________
372 Double_t AliMUONVTrackReconstructor::TryOneCluster(const AliMUONTrackParam &trackParam, AliMUONVCluster* cluster,
373                                                    AliMUONTrackParam &trackParamAtCluster, Bool_t updatePropagator)
374 {
375 /// Test the compatibility between the track and the cluster (using trackParam's covariance matrix):
376 /// return the corresponding Chi2
377 /// return trackParamAtCluster
378   
379   // extrapolate track parameters and covariances at the z position of the tested cluster
380   trackParamAtCluster = trackParam;
381   AliMUONTrackExtrap::ExtrapToZCov(&trackParamAtCluster, cluster->GetZ(), updatePropagator);
382   
383   // set pointer to cluster into trackParamAtCluster
384   trackParamAtCluster.SetClusterPtr(cluster);
385   
386   // Set differences between trackParam and cluster in the bending and non bending directions
387   Double_t dX = cluster->GetX() - trackParamAtCluster.GetNonBendingCoor();
388   Double_t dY = cluster->GetY() - trackParamAtCluster.GetBendingCoor();
389   
390   // Calculate errors and covariances
391   const TMatrixD& kParamCov = trackParamAtCluster.GetCovariances();
392   Double_t sigmaX2 = kParamCov(0,0) + cluster->GetErrX2();
393   Double_t sigmaY2 = kParamCov(2,2) + cluster->GetErrY2();
394   
395   // Compute chi2
396   return dX * dX / sigmaX2 + dY * dY / sigmaY2;
397   
398 }
399
400   //__________________________________________________________________________
401 Bool_t AliMUONVTrackReconstructor::TryOneClusterFast(const AliMUONTrackParam &trackParam, AliMUONVCluster* cluster)
402 {
403 /// Test the compatibility between the track and the cluster within a wide fix window
404 /// assuming linear propagation of the track:
405 /// return kTRUE if they are compatibles
406   
407   Double_t dZ = cluster->GetZ() - trackParam.GetZ();
408   Double_t dX = cluster->GetX() - (trackParam.GetNonBendingCoor() + trackParam.GetNonBendingSlope() * dZ);
409   Double_t dY = cluster->GetY() - (trackParam.GetBendingCoor() + trackParam.GetBendingSlope() * dZ);
410   
411   if (TMath::Abs(dX) > AliMUONReconstructor::GetRecoParam()->GetMaxNonBendingDistanceToTrack() ||
412       TMath::Abs(dY) > AliMUONReconstructor::GetRecoParam()->GetMaxBendingDistanceToTrack()) return kFALSE;
413   
414   return kTRUE;
415   
416 }
417
418   //__________________________________________________________________________
419 Double_t AliMUONVTrackReconstructor::TryTwoClustersFast(const AliMUONTrackParam &trackParamAtCluster1, AliMUONVCluster* cluster2,
420                                                         AliMUONTrackParam &trackParamAtCluster2)
421 {
422 /// Test the compatibility between the track and the 2 clusters together (using trackParam's covariance matrix)
423 /// assuming linear propagation between the two clusters:
424 /// return the corresponding Chi2 accounting for covariances between the 2 clusters
425 /// return trackParamAtCluster2
426   
427   // extrapolate linearly track parameters and covariances at the z position of the second cluster
428   trackParamAtCluster2 = trackParamAtCluster1;
429   AliMUONTrackExtrap::LinearExtrapToZ(&trackParamAtCluster2, cluster2->GetZ());
430   
431   // set pointer to cluster2 into trackParamAtCluster2
432   trackParamAtCluster2.SetClusterPtr(cluster2);
433   
434   // Set differences between track and clusters in the bending and non bending directions
435   AliMUONVCluster* cluster1 = trackParamAtCluster1.GetClusterPtr();
436   Double_t dX1 = cluster1->GetX() - trackParamAtCluster1.GetNonBendingCoor();
437   Double_t dX2 = cluster2->GetX() - trackParamAtCluster2.GetNonBendingCoor();
438   Double_t dY1 = cluster1->GetY() - trackParamAtCluster1.GetBendingCoor();
439   Double_t dY2 = cluster2->GetY() - trackParamAtCluster2.GetBendingCoor();
440   
441   // Calculate errors and covariances
442   const TMatrixD& kParamCov1 = trackParamAtCluster1.GetCovariances();
443   const TMatrixD& kParamCov2 = trackParamAtCluster2.GetCovariances();
444   Double_t dZ = trackParamAtCluster2.GetZ() - trackParamAtCluster1.GetZ();
445   Double_t sigma2X1 = kParamCov1(0,0) + cluster1->GetErrX2();
446   Double_t sigma2X2 = kParamCov2(0,0) + cluster2->GetErrX2();
447   Double_t covX1X2  = kParamCov1(0,0) + dZ * kParamCov1(0,1);
448   Double_t sigma2Y1 = kParamCov1(2,2) + cluster1->GetErrY2();
449   Double_t sigma2Y2 = kParamCov2(2,2) + cluster2->GetErrY2();
450   Double_t covY1Y2  = kParamCov1(2,2) + dZ * kParamCov1(2,3);
451   
452   // Compute chi2
453   Double_t detX = sigma2X1 * sigma2X2 - covX1X2 * covX1X2;
454   Double_t detY = sigma2Y1 * sigma2Y2 - covY1Y2 * covY1Y2;
455   if (detX == 0. || detY == 0.) return 1.e10;
456   return   (dX1 * dX1 * sigma2X2 + dX2 * dX2 * sigma2X1 - 2. * dX1 * dX2 * covX1X2) / detX
457          + (dY1 * dY1 * sigma2Y2 + dY2 * dY2 * sigma2Y1 - 2. * dY1 * dY2 * covY1Y2) / detY;
458   
459 }
460
461   //__________________________________________________________________________
462 Bool_t AliMUONVTrackReconstructor::FollowLinearTrackInStation(AliMUONTrack &trackCandidate, const AliMUONVClusterStore& clusterStore,
463                                                               Int_t nextStation)
464 {
465   /// Follow trackCandidate in station(0..) nextStation assuming linear propagation, and search for compatible cluster(s)
466   /// Keep all possibilities or only the best one(s) according to the flag fgkTrackAllTracks:
467   /// kTRUE:  duplicate "trackCandidate" if there are several possibilities and add the new tracks at the end of
468   ///         fRecTracksPtr to avoid conficts with other track candidates at this current stage of the tracking procedure.
469   ///         Remove the obsolete "trackCandidate" at the end.
470   /// kFALSE: add only the best cluster(s) to the "trackCandidate". Try to add a couple of clusters in priority.
471   /// return kTRUE if new cluster(s) have been found (otherwise return kFALSE)
472   AliDebug(1,Form("Enter FollowLinearTrackInStation(1..) %d", nextStation+1));
473   
474   // Order the chamber according to the propagation direction (tracking starts with chamber 2):
475   // - nextStation == station(1...) 5 => forward propagation
476   // - nextStation < station(1...) 5 => backward propagation
477   Int_t ch1, ch2;
478   if (nextStation==4) {
479     ch1 = 2*nextStation+1;
480     ch2 = 2*nextStation;
481   } else {
482     ch1 = 2*nextStation;
483     ch2 = 2*nextStation+1;
484   }
485   
486   Double_t chi2WithOneCluster = 1.e10;
487   Double_t chi2WithTwoClusters = 1.e10;
488   Double_t maxChi2WithOneCluster = 2. * AliMUONReconstructor::GetRecoParam()->GetSigmaCutForTracking() *
489                                         AliMUONReconstructor::GetRecoParam()->GetSigmaCutForTracking(); // 2 because 2 quantities in chi2
490   Double_t maxChi2WithTwoClusters = 4. * AliMUONReconstructor::GetRecoParam()->GetSigmaCutForTracking() *
491                                          AliMUONReconstructor::GetRecoParam()->GetSigmaCutForTracking(); // 4 because 4 quantities in chi2
492   Double_t bestChi2WithOneCluster = maxChi2WithOneCluster;
493   Double_t bestChi2WithTwoClusters = maxChi2WithTwoClusters;
494   Bool_t foundOneCluster = kFALSE;
495   Bool_t foundTwoClusters = kFALSE;
496   AliMUONTrack *newTrack = 0x0;
497   AliMUONVCluster *clusterCh1, *clusterCh2;
498   AliMUONTrackParam extrapTrackParamAtCluster1;
499   AliMUONTrackParam extrapTrackParamAtCluster2;
500   AliMUONTrackParam bestTrackParamAtCluster1;
501   AliMUONTrackParam bestTrackParamAtCluster2;
502   
503   Int_t nClusters = clusterStore.GetSize();
504   Bool_t *clusterCh1Used = new Bool_t[nClusters];
505   for (Int_t i = 0; i < nClusters; i++) clusterCh1Used[i] = kFALSE;
506   Int_t iCluster1;
507   
508   // Get track parameters
509   AliMUONTrackParam trackParam(*(AliMUONTrackParam*)trackCandidate.GetTrackParamAtCluster()->First());
510   
511   // Add MCS effect
512   AliMUONTrackExtrap::AddMCSEffect(&trackParam,AliMUONConstants::ChamberThicknessInX0(),1.);
513   
514   // Printout for debuging
515   if ((AliLog::GetDebugLevel("MUON","AliMUONVTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
516     cout << "FollowLinearTrackInStation: look for clusters in chamber(1..): " << ch2+1 << endl;
517   }
518   
519   // Create iterators to loop over clusters in both chambers
520   TIter nextInCh1(clusterStore.CreateChamberIterator(ch1,ch1));
521   TIter nextInCh2(clusterStore.CreateChamberIterator(ch2,ch2));
522   
523   // look for candidates in chamber 2
524   while ( ( clusterCh2 = static_cast<AliMUONVCluster*>(nextInCh2()) ) ) {
525     
526     // try to add the current cluster fast
527     if (!TryOneClusterFast(trackParam, clusterCh2)) continue;
528     
529     // try to add the current cluster accuratly
530     extrapTrackParamAtCluster2 = trackParam;
531     AliMUONTrackExtrap::LinearExtrapToZ(&extrapTrackParamAtCluster2, clusterCh2->GetZ());
532     chi2WithOneCluster = TryOneCluster(extrapTrackParamAtCluster2, clusterCh2, extrapTrackParamAtCluster2);
533     
534     // if good chi2 then try to attach a cluster in the other chamber too
535     if (chi2WithOneCluster < maxChi2WithOneCluster) {
536       Bool_t foundSecondCluster = kFALSE;
537       
538       // Printout for debuging
539       if ((AliLog::GetDebugLevel("MUON","AliMUONVTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
540         cout << "FollowLinearTrackInStation: found one cluster in chamber(1..): " << ch2+1
541              << " (Chi2 = " << chi2WithOneCluster << ")" << endl;
542         cout << "                      look for second clusters in chamber(1..): " << ch1+1 << " ..." << endl;
543       }
544       
545       // add MCS effect
546       AliMUONTrackExtrap::AddMCSEffect(&extrapTrackParamAtCluster2,AliMUONConstants::ChamberThicknessInX0(),1.);
547       
548       // reset cluster iterator of chamber 1
549       nextInCh1.Reset();
550       iCluster1 = -1;
551       
552       // look for second candidates in chamber 1
553       while ( ( clusterCh1 = static_cast<AliMUONVCluster*>(nextInCh1()) ) ) {
554         iCluster1++;
555         
556         // try to add the current cluster fast
557         if (!TryOneClusterFast(extrapTrackParamAtCluster2, clusterCh1)) continue;
558         
559         // try to add the current cluster in addition to the one found in the previous chamber
560         chi2WithTwoClusters = TryTwoClustersFast(extrapTrackParamAtCluster2, clusterCh1, extrapTrackParamAtCluster1);
561         
562         // if good chi2 then consider to add the 2 clusters to the "trackCandidate"
563         if (chi2WithTwoClusters < maxChi2WithTwoClusters) {
564           foundSecondCluster = kTRUE;
565           foundTwoClusters = kTRUE;
566           
567           // Printout for debuging
568           if ((AliLog::GetDebugLevel("MUON","AliMUONVTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
569             cout << "FollowLinearTrackInStation: found one cluster in chamber(1..): " << ch1+1
570                  << " (Chi2 = " << chi2WithTwoClusters << ")" << endl;
571           }
572           
573           if (AliMUONReconstructor::GetRecoParam()->TrackAllTracks()) {
574             // copy trackCandidate into a new track put at the end of fRecTracksPtr and add the new clusters
575             newTrack = new ((*fRecTracksPtr)[fRecTracksPtr->GetLast()+1]) AliMUONTrack(trackCandidate);
576             extrapTrackParamAtCluster1.SetRemovable(kTRUE);
577             newTrack->AddTrackParamAtCluster(extrapTrackParamAtCluster1,*clusterCh1);
578             extrapTrackParamAtCluster2.SetRemovable(kTRUE);
579             newTrack->AddTrackParamAtCluster(extrapTrackParamAtCluster2,*clusterCh2);
580             newTrack->GetTrackParamAtCluster()->Sort();
581             fNRecTracks++;
582             
583             // Tag clusterCh1 as used
584             clusterCh1Used[iCluster1] = kTRUE;
585             
586             // Printout for debuging
587             if ((AliLog::GetDebugLevel("MUON","AliMUONVTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
588               cout << "FollowLinearTrackInStation: added two clusters in station(1..): " << nextStation+1 << endl;
589               if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
590             }
591             
592           } else if (chi2WithTwoClusters < bestChi2WithTwoClusters) {
593             // keep track of the best couple of clusters
594             bestChi2WithTwoClusters = chi2WithTwoClusters;
595             bestTrackParamAtCluster1 = extrapTrackParamAtCluster1;
596             bestTrackParamAtCluster2 = extrapTrackParamAtCluster2;
597           }
598           
599         }
600         
601       }
602       
603       // if no cluster found in chamber1 then consider to add clusterCh2 only
604       if (!foundSecondCluster) {
605         foundOneCluster = kTRUE;
606         
607         if (AliMUONReconstructor::GetRecoParam()->TrackAllTracks()) {
608           // copy trackCandidate into a new track put at the end of fRecTracksPtr and add the new cluster
609           newTrack = new ((*fRecTracksPtr)[fRecTracksPtr->GetLast()+1]) AliMUONTrack(trackCandidate);
610           extrapTrackParamAtCluster2.SetRemovable(kFALSE);
611           newTrack->AddTrackParamAtCluster(extrapTrackParamAtCluster2,*clusterCh2);
612           newTrack->GetTrackParamAtCluster()->Sort();
613           fNRecTracks++;
614           
615           // Printout for debuging
616           if ((AliLog::GetDebugLevel("MUON","AliMUONVTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
617             cout << "FollowLinearTrackInStation: added one cluster in chamber(1..): " << ch2+1 << endl;
618             if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
619           }
620           
621         } else if (!foundTwoClusters && chi2WithOneCluster < bestChi2WithOneCluster) {
622           // keep track of the best cluster except if a couple of clusters has already been found
623           bestChi2WithOneCluster = chi2WithOneCluster;
624           bestTrackParamAtCluster1 = extrapTrackParamAtCluster2;
625         }
626         
627       }
628       
629     }
630     
631   }
632   
633   // look for candidates in chamber 1 not already attached to a track
634   // if we want to keep all possible tracks or if no good couple of clusters has been found
635   if (AliMUONReconstructor::GetRecoParam()->TrackAllTracks() || !foundTwoClusters) {
636     
637     // Printout for debuging
638     if ((AliLog::GetDebugLevel("MUON","AliMUONVTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
639       cout << "FollowLinearTrackInStation: look for single cluster in chamber(1..): " << ch1+1 << endl;
640     }
641     
642     //Extrapolate trackCandidate to chamber "ch2"
643     AliMUONTrackExtrap::LinearExtrapToZ(&trackParam, AliMUONConstants::DefaultChamberZ(ch2));
644     
645     // add MCS effect for next step
646     AliMUONTrackExtrap::AddMCSEffect(&trackParam,AliMUONConstants::ChamberThicknessInX0(),1.);
647       
648     // reset cluster iterator of chamber 1
649     nextInCh1.Reset();
650     iCluster1 = -1;
651     
652     // look for second candidates in chamber 1
653     while ( ( clusterCh1 = static_cast<AliMUONVCluster*>(nextInCh1()) ) ) {
654       iCluster1++;
655       
656       if (clusterCh1Used[iCluster1]) continue; // Skip clusters already used
657       
658       // try to add the current cluster fast
659       if (!TryOneClusterFast(trackParam, clusterCh1)) continue;
660         
661       // try to add the current cluster accuratly
662       extrapTrackParamAtCluster1 = trackParam;
663       AliMUONTrackExtrap::LinearExtrapToZ(&extrapTrackParamAtCluster1, clusterCh1->GetZ());
664       chi2WithOneCluster = TryOneCluster(extrapTrackParamAtCluster1, clusterCh1, extrapTrackParamAtCluster1);
665     
666       // if good chi2 then consider to add clusterCh1
667       // We do not try to attach a cluster in the other chamber too since it has already been done above
668       if (chi2WithOneCluster < maxChi2WithOneCluster) {
669         foundOneCluster = kTRUE;
670         
671         // Printout for debuging
672         if ((AliLog::GetDebugLevel("MUON","AliMUONVTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
673           cout << "FollowLinearTrackInStation: found one cluster in chamber(1..): " << ch1+1
674                << " (Chi2 = " << chi2WithOneCluster << ")" << endl;
675         }
676         
677         if (AliMUONReconstructor::GetRecoParam()->TrackAllTracks()) {
678           // copy trackCandidate into a new track put at the end of fRecTracksPtr and add the new cluster
679           newTrack = new ((*fRecTracksPtr)[fRecTracksPtr->GetLast()+1]) AliMUONTrack(trackCandidate);
680           extrapTrackParamAtCluster1.SetRemovable(kFALSE);
681           newTrack->AddTrackParamAtCluster(extrapTrackParamAtCluster1,*clusterCh1);
682           newTrack->GetTrackParamAtCluster()->Sort();
683           fNRecTracks++;
684           
685           // Printout for debuging
686           if ((AliLog::GetDebugLevel("MUON","AliMUONVTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
687             cout << "FollowLinearTrackInStation: added one cluster in chamber(1..): " << ch1+1 << endl;
688             if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
689           }
690           
691         } else if (chi2WithOneCluster < bestChi2WithOneCluster) {
692           // keep track of the best cluster except if a couple of clusters has already been found
693           bestChi2WithOneCluster = chi2WithOneCluster;
694           bestTrackParamAtCluster1 = extrapTrackParamAtCluster1;
695         }
696         
697       }
698       
699     }
700     
701   }
702   
703   // fill out the best track if required else clean up the fRecTracksPtr array
704   if (!AliMUONReconstructor::GetRecoParam()->TrackAllTracks()) {
705     if (foundTwoClusters) {
706       bestTrackParamAtCluster1.SetRemovable(kTRUE);
707       trackCandidate.AddTrackParamAtCluster(bestTrackParamAtCluster1,*(bestTrackParamAtCluster1.GetClusterPtr()));
708       bestTrackParamAtCluster2.SetRemovable(kTRUE);
709       trackCandidate.AddTrackParamAtCluster(bestTrackParamAtCluster2,*(bestTrackParamAtCluster2.GetClusterPtr()));
710       trackCandidate.GetTrackParamAtCluster()->Sort();
711       
712       // Printout for debuging
713       if ((AliLog::GetDebugLevel("MUON","AliMUONVTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
714         cout << "FollowLinearTrackInStation: added the two best clusters in station(1..): " << nextStation+1 << endl;
715         if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
716       }
717       
718     } else if (foundOneCluster) {
719       bestTrackParamAtCluster1.SetRemovable(kFALSE);
720       trackCandidate.AddTrackParamAtCluster(bestTrackParamAtCluster1,*(bestTrackParamAtCluster1.GetClusterPtr()));
721       trackCandidate.GetTrackParamAtCluster()->Sort();
722       
723       // Printout for debuging
724       if ((AliLog::GetDebugLevel("MUON","AliMUONVTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
725         cout << "FollowLinearTrackInStation: added the best cluster in chamber(1..): " << bestTrackParamAtCluster1.GetClusterPtr()->GetChamberId()+1 << endl;
726         if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
727       }
728       
729     } else {
730       delete [] clusterCh1Used;
731       return kFALSE;
732     }
733     
734   } else if (foundOneCluster || foundTwoClusters) {
735     
736     // remove obsolete track
737     fRecTracksPtr->Remove(&trackCandidate);
738     fNRecTracks--;
739     
740   } else {
741     delete [] clusterCh1Used;  
742     return kFALSE;
743   }
744   
745   delete [] clusterCh1Used;
746   return kTRUE;
747   
748 }
749
750   //__________________________________________________________________________
751 void AliMUONVTrackReconstructor::ValidateTracksWithTrigger(AliMUONVTrackStore& trackStore,
752                                                            const AliMUONVTriggerTrackStore& triggerTrackStore,
753                                                            const AliMUONVTriggerStore& triggerStore,
754                                                            const AliMUONTrackHitPattern& trackHitPattern)
755 {
756   /// Try to match track from tracking system with trigger track
757   AliCodeTimerAuto("");
758
759   trackHitPattern.ExecuteValidation(trackStore, triggerTrackStore, triggerStore);
760 }
761
762   //__________________________________________________________________________
763 void AliMUONVTrackReconstructor::EventReconstructTrigger(const AliMUONTriggerCircuit& circuit,
764                                                          const AliMUONVTriggerStore& triggerStore,
765                                                          AliMUONVTriggerTrackStore& triggerTrackStore)
766 {
767   /// To make the trigger tracks from Local Trigger
768   AliDebug(1, "");
769   AliCodeTimerAuto("");
770   
771   AliMUONGlobalTrigger* globalTrigger = triggerStore.Global();
772   
773   UChar_t gloTrigPat = 0;
774
775   if (globalTrigger)
776   {
777     gloTrigPat = globalTrigger->GetGlobalResponse();
778   }
779   
780   TIter next(triggerStore.CreateIterator());
781   AliMUONLocalTrigger* locTrg(0x0);
782
783   Float_t z11 = AliMUONConstants::DefaultChamberZ(10);
784   Float_t z21 = AliMUONConstants::DefaultChamberZ(12);
785       
786   AliMUONTriggerTrack triggerTrack;
787   
788   while ( ( locTrg = static_cast<AliMUONLocalTrigger*>(next()) ) )
789   {
790     Bool_t xTrig=kFALSE;
791     Bool_t yTrig=kFALSE;
792     
793     Int_t localBoardId = locTrg->LoCircuit();
794     if ( locTrg->LoSdev()==1 && locTrg->LoDev()==0 && 
795          locTrg->LoStripX()==0) xTrig=kFALSE; // no trigger in X
796     else xTrig=kTRUE;                         // trigger in X
797     if (locTrg->LoTrigY()==1 && 
798         locTrg->LoStripY()==15 ) yTrig = kFALSE; // no trigger in Y
799     else yTrig = kTRUE;                          // trigger in Y
800     
801     if (xTrig && yTrig) 
802     { // make Trigger Track if trigger in X and Y
803       
804       Float_t y11 = circuit.GetY11Pos(localBoardId, locTrg->LoStripX()); 
805       // need first to convert deviation to [0-30] 
806       // (see AliMUONLocalTriggerBoard::LocalTrigger)
807       Int_t deviation = locTrg->LoDev(); 
808       Int_t sign = 0;
809       if ( !locTrg->LoSdev() &&  deviation ) sign=-1;
810       if ( !locTrg->LoSdev() && !deviation ) sign= 0;
811       if (  locTrg->LoSdev() == 1 )          sign=+1;
812       deviation *= sign;
813       deviation += 15;
814       Int_t stripX21 = locTrg->LoStripX()+deviation+1;
815       Float_t y21 = circuit.GetY21Pos(localBoardId, stripX21);       
816       Float_t x11 = circuit.GetX11Pos(localBoardId, locTrg->LoStripY());
817       
818       AliDebug(1, Form(" MakeTriggerTrack %d %d %d %d %f %f %f \n",locTrg->LoCircuit(),
819                        locTrg->LoStripX(),locTrg->LoStripX()+locTrg->LoDev()+1,locTrg->LoStripY(),y11, y21, x11));
820       
821       Float_t thetax = TMath::ATan2( x11 , z11 );
822       Float_t thetay = TMath::ATan2( (y21-y11) , (z21-z11) );
823       
824       triggerTrack.SetX11(x11);
825       triggerTrack.SetY11(y11);
826       triggerTrack.SetThetax(thetax);
827       triggerTrack.SetThetay(thetay);
828       triggerTrack.SetGTPattern(gloTrigPat);
829       triggerTrack.SetLoTrgNum(localBoardId);
830       
831       triggerTrackStore.Add(triggerTrack);
832     } // board is fired 
833   } // end of loop on Local Trigger
834 }
835