]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONVTrackReconstructor.cxx
Added decription of available options to tune track reconstruction
[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 ///
20 /// \class AliMUONVTrackReconstructor
21 /// Virtual MUON track reconstructor in ALICE (class renamed from AliMUONEventReconstructor)
22 ///
23 /// This class contains as data:
24 /// * a pointer to the array of hits to be reconstructed (the event)
25 /// * a pointer to the array of segments made with these hits inside each station
26 /// * a pointer to the array of reconstructed tracks
27 ///
28 /// It contains as methods, among others:
29 /// * EventReconstruct to build the muon tracks
30 /// * EventReconstructTrigger to build the trigger tracks
31 ///
32 /// Several options and adjustable parameters are available for both Kalman and Original
33 /// tracking algorithms (hard coded for the moment in AliMUONVTrackReconstructor.cxx):
34 /// - *fgkSigmaToCutForTracking* : quality cut used to select new clusters to be
35 ///   attached to the track candidate and to select good tracks.
36 /// - *fgkTrackAllTracks* : according to the value of this flag, in case that several
37 ///   new clusters pass the quality cut, either we consider all the possibilities
38 ///   (duplicating tracks) or we attach only the best cluster.
39 /// - *fgkRecoverTracks* : if this flag is set to 'true', we try to recover the tracks
40 ///   lost during the tracking by removing the worst of the 2 clusters attached in the
41 ///   previous station.
42 /// - *fgkImproveTracks* : if this flag is set to 'true', we try to improve the quality
43 ///   of the tracks at the end of the tracking by removing clusters that do not pass
44 ///   new quality cut (within the limit that we must keep at least one cluster per
45 ///   the station).
46 /// - *fgkSigmaToCutForImprovement* : quality cut used when we try to improve the
47 ///   quality of the tracks.
48 ///
49 ///  \author Philippe Pillot
50 ///
51 ////////////////////////////////////
52
53 #include "AliMUONVTrackReconstructor.h"
54
55 #include "AliMUONConstants.h"
56 #include "AliMUONRawCluster.h"
57 #include "AliMUONHitForRec.h"
58 #include "AliMUONObjectPair.h"
59 #include "AliMUONTriggerTrack.h"
60 #include "AliMUONTriggerCircuit.h"
61 #include "AliMUONLocalTrigger.h"
62 #include "AliMUONGlobalTrigger.h"
63 #include "AliMUONTrack.h"
64 #include "AliMUONTrackParam.h"
65 #include "AliMUONTrackExtrap.h"
66 #include "AliMUONTrackHitPattern.h"
67 #include "AliMUONVTrackStore.h"
68 #include "AliMUONVClusterStore.h"
69 #include "AliMUONRawCluster.h"
70 #include "AliMUONVTriggerStore.h"
71 #include "AliMUONVTriggerTrackStore.h"
72 #include "AliMpDEManager.h"
73
74 #include "AliLog.h"
75 #include "AliTracker.h"
76
77 #include <TClonesArray.h>
78 #include <TMath.h>
79 #include <TMatrixD.h>
80
81 #include <Riostream.h>
82
83 /// \cond CLASSIMP
84 ClassImp(AliMUONVTrackReconstructor) // Class implementation in ROOT context
85 /// \endcond
86
87 //************* Defaults parameters for reconstruction
88 const Double_t AliMUONVTrackReconstructor::fgkDefaultMinBendingMomentum = 3.0;
89 const Double_t AliMUONVTrackReconstructor::fgkDefaultMaxBendingMomentum = 3000.0;
90 const Double_t AliMUONVTrackReconstructor::fgkDefaultMaxNormChi2MatchTrigger = 16.0;
91
92 const Double_t AliMUONVTrackReconstructor::fgkSigmaToCutForTracking = 6.0;
93 const Double_t AliMUONVTrackReconstructor::fgkSigmaToCutForImprovement = 4.0;
94 const Bool_t   AliMUONVTrackReconstructor::fgkTrackAllTracks = kTRUE;
95 const Double_t AliMUONVTrackReconstructor::fgkMaxTrackingDistanceBending    = 2.;
96 const Double_t AliMUONVTrackReconstructor::fgkMaxTrackingDistanceNonBending = 2.;
97 const Bool_t   AliMUONVTrackReconstructor::fgkRecoverTracks = kTRUE;
98 const Bool_t   AliMUONVTrackReconstructor::fgkImproveTracks = kTRUE;
99
100
101   //__________________________________________________________________________
102 AliMUONVTrackReconstructor::AliMUONVTrackReconstructor()
103   : TObject(),
104     fMinBendingMomentum(fgkDefaultMinBendingMomentum),
105     fMaxBendingMomentum(fgkDefaultMaxBendingMomentum),
106     fMaxNormChi2MatchTrigger(fgkDefaultMaxNormChi2MatchTrigger),
107     fHitsForRecPtr(0x0),
108     fNHitsForRec(0),
109     fNHitsForRecPerChamber(0x0),
110     fIndexOfFirstHitForRecPerChamber(0x0),
111     fRecTracksPtr(0x0),
112     fNRecTracks(0)
113 {
114   /// Constructor for class AliMUONVTrackReconstructor
115   fNHitsForRecPerChamber = new Int_t[AliMUONConstants::NTrackingCh()];
116   fIndexOfFirstHitForRecPerChamber = new Int_t[AliMUONConstants::NTrackingCh()];
117
118   // Memory allocation for the TClonesArray of hits for reconstruction
119   // Is 10000 the right size ????
120   fHitsForRecPtr = new TClonesArray("AliMUONHitForRec", 10000);
121   
122   // Memory allocation for the TClonesArray of reconstructed tracks
123   fRecTracksPtr = new TClonesArray("AliMUONTrack", 10);
124   
125   // set the magnetic field for track extrapolations
126   const AliMagF* kField = AliTracker::GetFieldMap();
127   if (!kField) AliFatal("No field available");
128   AliMUONTrackExtrap::SetField(kField);
129 }
130
131   //__________________________________________________________________________
132 AliMUONVTrackReconstructor::~AliMUONVTrackReconstructor()
133 {
134   /// Destructor for class AliMUONVTrackReconstructor
135   delete [] fNHitsForRecPerChamber;
136   delete [] fIndexOfFirstHitForRecPerChamber;
137   delete fHitsForRecPtr;
138   delete fRecTracksPtr;
139 }
140
141   //__________________________________________________________________________
142 void AliMUONVTrackReconstructor::ResetHitsForRec()
143 {
144   /// To reset the TClonesArray of HitsForRec,
145   /// and the number of HitForRec and the index of the first HitForRec per chamber
146   if (fHitsForRecPtr) fHitsForRecPtr->Clear("C");
147   fNHitsForRec = 0;
148   for (Int_t ch = 0; ch < AliMUONConstants::NTrackingCh(); ch++) {
149     fNHitsForRecPerChamber[ch] = 0;
150     fIndexOfFirstHitForRecPerChamber[ch] = 0;
151   }
152 }
153
154   //__________________________________________________________________________
155 void AliMUONVTrackReconstructor::ResetTracks()
156 {
157   /// To reset the TClonesArray of reconstructed tracks
158   if (fRecTracksPtr) fRecTracksPtr->Clear("C");
159   fNRecTracks = 0;
160   return;
161 }
162
163   //__________________________________________________________________________
164 void AliMUONVTrackReconstructor::EventReconstruct(const AliMUONVClusterStore& clusterStore, AliMUONVTrackStore& trackStore)
165 {
166   /// To reconstruct one event
167   AliDebug(1,"");
168   
169   ResetTracks();
170   ResetHitsForRec();
171   AddHitsForRecFromRawClusters(clusterStore);
172   MakeTracks();
173
174   // Add tracks to MUON data container 
175   for (Int_t i=0; i<fNRecTracks; ++i) 
176   {
177     AliMUONTrack * track = (AliMUONTrack*) fRecTracksPtr->At(i);
178     trackStore.Add(*track);
179   }
180 }
181
182   //__________________________________________________________________________
183 void AliMUONVTrackReconstructor::AddHitsForRecFromRawClusters(const AliMUONVClusterStore& clusterStore)
184 {
185   /// Build internal array of hit for rec from clusterStore
186   
187   TIter next(clusterStore.CreateIterator());
188   AliMUONRawCluster* clus(0x0);
189   Int_t iclus(0);
190   
191   while ( ( clus = static_cast<AliMUONRawCluster*>(next()) ) )
192   {
193     // new AliMUONHitForRec from raw cluster
194     // and increment number of AliMUONHitForRec's (total and in chamber)
195     AliMUONHitForRec* hitForRec = new ((*fHitsForRecPtr)[fNHitsForRec]) AliMUONHitForRec(clus);
196     fNHitsForRec++;
197     // more information into HitForRec
198     hitForRec->SetBendingReso2(clus->GetErrY() * clus->GetErrY());
199     hitForRec->SetNonBendingReso2(clus->GetErrX() * clus->GetErrX());
200     //  original raw cluster
201     Int_t ch = AliMpDEManager::GetChamberId(clus->GetDetElemId());
202     hitForRec->SetChamberNumber(ch);
203     hitForRec->SetHitNumber(iclus);
204     // Z coordinate of the raw cluster (cm)
205     hitForRec->SetZ(clus->GetZ(0));
206     if (AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 3) {
207       cout << "Chamber " << ch <<" raw cluster  " << iclus << " : " << endl;
208       clus->Print("full");
209       cout << "AliMUONHitForRec number (1...): " << fNHitsForRec << endl;
210       hitForRec->Print("full");
211     }
212     ++iclus;
213   } // end of chamber loop
214   
215   SortHitsForRecWithIncreasingChamber(); 
216   
217   AliDebug(1,"End of AddHitsForRecFromRawClusters");
218   
219   if (AliLog::GetGlobalDebugLevel() > 0) 
220   {
221     AliDebug(1, Form("NHitsForRec: %d",fNHitsForRec));
222     for (Int_t ch = 0; ch < AliMUONConstants::NTrackingCh(); ch++) 
223     {
224       AliDebug(1, Form("Chamber(0...): %d",ch));
225       AliDebug(1, Form("NHitsForRec: %d", fNHitsForRecPerChamber[ch]));
226       AliDebug(1, Form("Index(first HitForRec): %d", fIndexOfFirstHitForRecPerChamber[ch]));
227       for (Int_t hit = fIndexOfFirstHitForRecPerChamber[ch];
228            hit < fIndexOfFirstHitForRecPerChamber[ch] + fNHitsForRecPerChamber[ch];
229            hit++) {
230         AliDebug(1, Form("HitForRec index(0...): %d",hit));
231         ((*fHitsForRecPtr)[hit])->Dump();
232       }
233     }
234   }
235 }
236
237   //__________________________________________________________________________
238 void AliMUONVTrackReconstructor::SortHitsForRecWithIncreasingChamber()
239 {
240   /// Sort HitsForRec's in increasing order with respect to chamber number.
241   /// Uses the function "Compare".
242   /// Update the information for HitsForRec per chamber too.
243   Int_t ch, nhits, prevch;
244   fHitsForRecPtr->Sort();
245   for (ch = 0; ch < AliMUONConstants::NTrackingCh(); ch++) {
246     fNHitsForRecPerChamber[ch] = 0;
247     fIndexOfFirstHitForRecPerChamber[ch] = 0;
248   }
249   prevch = 0; // previous chamber
250   nhits = 0; // number of hits in current chamber
251   // Loop over HitsForRec
252   for (Int_t hit = 0; hit < fNHitsForRec; hit++) {
253     // chamber number (0...)
254     ch = ((AliMUONHitForRec*)  ((*fHitsForRecPtr)[hit]))->GetChamberNumber();
255     // increment number of hits in current chamber
256     (fNHitsForRecPerChamber[ch])++;
257     // update index of first HitForRec in current chamber
258     // if chamber number different from previous one
259     if (ch != prevch) {
260       fIndexOfFirstHitForRecPerChamber[ch] = hit;
261       prevch = ch;
262     }
263   }
264   return;
265 }
266
267   //__________________________________________________________________________
268 void AliMUONVTrackReconstructor::MakeTracks()
269 {
270   /// To make the tracks from the list of segments and points in all stations
271   AliDebug(1,"Enter MakeTracks");
272   // Look for candidates from at least 3 aligned points in stations(1..) 4 and 5
273   MakeTrackCandidates();
274   if (fRecTracksPtr->GetEntriesFast() == 0) return;
275   // Follow tracks in stations(1..) 3, 2 and 1
276   FollowTracks();
277   // Improve the reconstructed tracks
278   if (fgkImproveTracks) ImproveTracks();
279   // Remove double tracks
280   RemoveDoubleTracks();
281   // Fill AliMUONTrack data members
282   Finalize();
283 }
284
285   //__________________________________________________________________________
286 TClonesArray* AliMUONVTrackReconstructor::MakeSegmentsInStation(Int_t station)
287 {
288   /// To make the list of segments in station(0..) "Station" from the list of hits to be reconstructed.
289   /// Return a new TClonesArray of segments.
290   /// It is the responsibility of the user to delete it afterward.
291   AliDebug(1,Form("Enter MakeSegmentsPerStation (0...) %d",station));
292   
293   AliMUONHitForRec *hit1Ptr, *hit2Ptr;
294   AliMUONObjectPair *segment;
295   Double_t bendingSlope = 0, impactParam = 0., bendingMomentum = 0.; // to avoid compilation warning
296                                                                      // first and second chambers (0...) in the station
297   Int_t ch1 = 2 * station;
298   Int_t ch2 = ch1 + 1;
299   
300   // list of segments
301   TClonesArray *segments = new TClonesArray("AliMUONObjectPair", fNHitsForRecPerChamber[ch2]);
302   segments->SetOwner(kTRUE);
303   
304   // Loop over HitForRec's in the first chamber of the station
305   for (Int_t hit1 = fIndexOfFirstHitForRecPerChamber[ch1];
306        hit1 < fIndexOfFirstHitForRecPerChamber[ch1] + fNHitsForRecPerChamber[ch1];
307        hit1++) 
308   {
309     // pointer to the HitForRec
310     hit1Ptr = (AliMUONHitForRec*) ((*fHitsForRecPtr)[hit1]);
311     // Loop over HitsForRec's in the second chamber of the station
312     for (Int_t hit2 = fIndexOfFirstHitForRecPerChamber[ch2];
313          hit2 < fIndexOfFirstHitForRecPerChamber[ch2] + fNHitsForRecPerChamber[ch2];
314          hit2++) 
315     {
316       // pointer to the HitForRec
317       hit2Ptr = (AliMUONHitForRec*) ((*fHitsForRecPtr)[hit2]);
318       if ( hit1Ptr->GetZ() - hit2Ptr->GetZ() != 0. ) 
319       {
320         // bending slope
321         bendingSlope = (hit1Ptr->GetBendingCoor() - hit2Ptr->GetBendingCoor()) / (hit1Ptr->GetZ() - hit2Ptr->GetZ());
322         // impact parameter
323         impactParam = hit1Ptr->GetBendingCoor() - hit1Ptr->GetZ() * bendingSlope;
324         // absolute value of bending momentum
325         bendingMomentum = TMath::Abs(AliMUONTrackExtrap::GetBendingMomentumFromImpactParam(impactParam));
326       } else 
327       {
328         AliWarning("hit1Ptr->GetZ() = hit2Ptr->GetZ(): no segment created");
329         continue;
330       }   
331       // check for bending momentum within tolerances
332       if ((bendingMomentum < fMaxBendingMomentum) && (bendingMomentum > fMinBendingMomentum)) 
333       {
334         // make new segment
335         segment = new ((*segments)[segments->GetLast()+1]) AliMUONObjectPair(hit1Ptr, hit2Ptr, kFALSE, kFALSE);
336         if (AliLog::GetGlobalDebugLevel() > 1) {
337           cout << "segmentIndex(0...): " << segments->GetLast() << endl;
338           segment->Dump();
339           cout << "HitForRec in first chamber" << endl;
340           hit1Ptr->Dump();
341           cout << "HitForRec in second chamber" << endl;
342           hit2Ptr->Dump();
343         }
344       }
345     } //for (Int_t hit2
346   } // for (Int_t hit1...
347   AliDebug(1,Form("Station: %d  NSegments:  %d ", station, segments->GetEntriesFast()));
348   return segments;
349 }
350
351   //__________________________________________________________________________
352 void AliMUONVTrackReconstructor::RemoveIdenticalTracks()
353 {
354   /// To remove identical tracks:
355   /// Tracks are considered identical if they have all their hits in common.
356   /// One keeps the track with the larger number of hits if need be
357   AliMUONTrack *track1, *track2, *trackToRemove;
358   Int_t hitsInCommon, nHits1, nHits2;
359   Bool_t removedTrack1;
360   // Loop over first track of the pair
361   track1 = (AliMUONTrack*) fRecTracksPtr->First();
362   while (track1) {
363     removedTrack1 = kFALSE;
364     nHits1 = track1->GetNTrackHits();
365     // Loop over second track of the pair
366     track2 = (AliMUONTrack*) fRecTracksPtr->After(track1);
367     while (track2) {
368       nHits2 = track2->GetNTrackHits();
369       // number of hits in common between two tracks
370       hitsInCommon = track1->HitsInCommon(track2);
371       // check for identical tracks
372       if ((hitsInCommon == nHits1) || (hitsInCommon == nHits2)) {
373         // decide which track to remove
374         if (nHits2 > nHits1) {
375           // remove track1 and continue the first loop with the track next to track1
376           trackToRemove = track1;
377           track1 = (AliMUONTrack*) fRecTracksPtr->After(track1);
378           fRecTracksPtr->Remove(trackToRemove);
379           fRecTracksPtr->Compress(); // this is essential to retrieve the TClonesArray afterwards
380           fNRecTracks--;
381           removedTrack1 = kTRUE;
382           break;
383         } else {
384           // remove track2 and continue the second loop with the track next to track2
385           trackToRemove = track2;
386           track2 = (AliMUONTrack*) fRecTracksPtr->After(track2);
387           fRecTracksPtr->Remove(trackToRemove);
388           fRecTracksPtr->Compress(); // this is essential to retrieve the TClonesArray afterwards
389           fNRecTracks--;
390         }
391       } else track2 = (AliMUONTrack*) fRecTracksPtr->After(track2);
392     } // track2
393     if (removedTrack1) continue;
394     track1 = (AliMUONTrack*) fRecTracksPtr->After(track1);
395   } // track1
396   return;
397 }
398
399   //__________________________________________________________________________
400 void AliMUONVTrackReconstructor::RemoveDoubleTracks()
401 {
402   /// To remove double tracks:
403   /// Tracks are considered identical if more than half of the hits of the track
404   /// which has the smaller number of hits are in common with the other track.
405   /// Among two identical tracks, one keeps the track with the larger number of hits
406   /// or, if these numbers are equal, the track with the minimum chi2.
407   AliMUONTrack *track1, *track2, *trackToRemove;
408   Int_t hitsInCommon, nHits1, nHits2;
409   Bool_t removedTrack1;
410   // Loop over first track of the pair
411   track1 = (AliMUONTrack*) fRecTracksPtr->First();
412   while (track1) {
413     removedTrack1 = kFALSE;
414     nHits1 = track1->GetNTrackHits();
415     // Loop over second track of the pair
416     track2 = (AliMUONTrack*) fRecTracksPtr->After(track1);
417     while (track2) {
418       nHits2 = track2->GetNTrackHits();
419       // number of hits in common between two tracks
420       hitsInCommon = track1->HitsInCommon(track2);
421       // check for identical tracks
422       if (((nHits1 < nHits2) && (2 * hitsInCommon > nHits1)) || (2 * hitsInCommon > nHits2)) {
423         // decide which track to remove
424         if ((nHits1 > nHits2) || ((nHits1 == nHits2) && (track1->GetFitFMin() <= track2->GetFitFMin()))) {
425           // remove track2 and continue the second loop with the track next to track2
426           trackToRemove = track2;
427           track2 = (AliMUONTrack*) fRecTracksPtr->After(track2);
428           fRecTracksPtr->Remove(trackToRemove);
429           fRecTracksPtr->Compress(); // this is essential to retrieve the TClonesArray afterwards
430           fNRecTracks--;
431         } else {
432           // else remove track1 and continue the first loop with the track next to track1
433           trackToRemove = track1;
434           track1 = (AliMUONTrack*) fRecTracksPtr->After(track1);
435           fRecTracksPtr->Remove(trackToRemove);
436           fRecTracksPtr->Compress(); // this is essential to retrieve the TClonesArray afterwards
437           fNRecTracks--;
438           removedTrack1 = kTRUE;
439           break;
440         }
441       } else track2 = (AliMUONTrack*) fRecTracksPtr->After(track2);
442     } // track2
443     if (removedTrack1) continue;
444     track1 = (AliMUONTrack*) fRecTracksPtr->After(track1);
445   } // track1
446   return;
447 }
448
449   //__________________________________________________________________________
450 Double_t AliMUONVTrackReconstructor::TryOneHitForRec(const AliMUONTrackParam &trackParam, AliMUONHitForRec* hitForRec,
451                                                      AliMUONTrackParam &trackParamAtHit, Bool_t updatePropagator)
452 {
453 /// Test the compatibility between the track and the hitForRec (using trackParam's covariance matrix):
454 /// return the corresponding Chi2
455 /// return trackParamAtHit
456   
457   if (!trackParam.CovariancesExist()) AliWarning(" track parameter covariance matrix does not exist");
458   
459   // extrapolate track parameters and covariances at the z position of the tested hit
460   if (&trackParam != &trackParamAtHit) {
461     trackParamAtHit = trackParam;
462     AliMUONTrackExtrap::ExtrapToZCov(&trackParamAtHit, hitForRec->GetZ(), updatePropagator);
463   }
464   
465   // set pointer to hit into trackParamAtHit
466   trackParamAtHit.SetHitForRecPtr(hitForRec);
467   
468   // Set differences between trackParam and hitForRec in the bending and non bending directions
469   TMatrixD dPos(2,1);
470   dPos(0,0) = hitForRec->GetNonBendingCoor() - trackParamAtHit.GetNonBendingCoor();
471   dPos(1,0) = hitForRec->GetBendingCoor() - trackParamAtHit.GetBendingCoor();
472   
473   // quick test of hitForRec compatibility within a wide road of x*y = 10*1 cm2 to save computing time
474   if (TMath::Abs(dPos(0,0)) > fgkMaxTrackingDistanceNonBending ||
475       TMath::Abs(dPos(1,0)) > fgkMaxTrackingDistanceBending) return 1.e10;
476   
477   // Set the error matrix from trackParam covariances and hitForRec resolution
478   const TMatrixD& kParamCov = trackParamAtHit.GetCovariances();
479   TMatrixD error(2,2);
480   error(0,0) = kParamCov(0,0) + hitForRec->GetNonBendingReso2();
481   error(0,1) = kParamCov(0,2);
482   error(1,0) = kParamCov(2,0);
483   error(1,1) = kParamCov(2,2) + hitForRec->GetBendingReso2();
484   
485   // Invert the error matrix for Chi2 calculation
486   if (error.Determinant() != 0) {
487     error.Invert();
488   } else {
489     AliWarning(" Determinant error=0");
490     return 1.e10;
491   }
492   
493   // Compute the Chi2 value
494   TMatrixD tmp(dPos,TMatrixD::kTransposeMult,error);
495   TMatrixD result(tmp,TMatrixD::kMult,dPos);
496   
497   return result(0,0);
498   
499 }
500
501   //__________________________________________________________________________
502 void AliMUONVTrackReconstructor::ValidateTracksWithTrigger(AliMUONVTrackStore& trackStore,
503                                                            const AliMUONVTriggerTrackStore& triggerTrackStore,
504                                                            const AliMUONVTriggerStore& triggerStore,
505                                                            const AliMUONTrackHitPattern& trackHitPattern)
506 {
507   /// Try to match track from tracking system with trigger track
508   static const Double_t kDistSigma[3]={1,1,0.02}; // sigma of distributions (trigger-track) X,Y,slopeY
509   
510   Int_t matchTrigger;
511   Int_t loTrgNum(-1);
512   Double_t distTriggerTrack[3];
513   Double_t xTrack, yTrack, ySlopeTrack, chi2MatchTrigger, minChi2MatchTrigger, chi2;
514
515   TIter itTrack(trackStore.CreateIterator());
516   AliMUONTrack* track;
517   
518   while ( ( track = static_cast<AliMUONTrack*>(itTrack()) ) )
519   {
520     matchTrigger = 0;
521     chi2MatchTrigger = 0.;
522     loTrgNum = -1;
523     Int_t doubleMatch=-1; // Check if track matches 2 trigger tracks
524     Double_t doubleChi2 = -1.;
525     
526     AliMUONTrackParam trackParam(*((AliMUONTrackParam*) (track->GetTrackParamAtHit()->Last())));
527     AliMUONTrackExtrap::ExtrapToZ(&trackParam, AliMUONConstants::DefaultChamberZ(10)); // extrap to 1st trigger chamber
528     
529     xTrack = trackParam.GetNonBendingCoor();
530     yTrack = trackParam.GetBendingCoor();
531     ySlopeTrack = trackParam.GetBendingSlope();
532     minChi2MatchTrigger = 999.;
533     
534     AliMUONTriggerTrack *triggerTrack;
535     TIter itTriggerTrack(triggerTrackStore.CreateIterator());
536     while ( ( triggerTrack = static_cast<AliMUONTriggerTrack*>(itTriggerTrack() ) ) )
537     {
538       distTriggerTrack[0] = (triggerTrack->GetX11()-xTrack)/kDistSigma[0];
539       distTriggerTrack[1] = (triggerTrack->GetY11()-yTrack)/kDistSigma[1];
540       distTriggerTrack[2] = (TMath::Tan(triggerTrack->GetThetay())-ySlopeTrack)/kDistSigma[2];
541       chi2 = 0.;
542       for (Int_t iVar = 0; iVar < 3; iVar++) chi2 += distTriggerTrack[iVar]*distTriggerTrack[iVar];
543       chi2 /= 3.; // Normalized Chi2: 3 degrees of freedom (X,Y,slopeY)
544       if (chi2 < fMaxNormChi2MatchTrigger) 
545       {
546         Bool_t isDoubleTrack = (TMath::Abs(chi2 - minChi2MatchTrigger)<1.);
547         if (chi2 < minChi2MatchTrigger && chi2 < fMaxNormChi2MatchTrigger) 
548         {
549           if(isDoubleTrack)
550           {
551             doubleMatch = loTrgNum;
552             doubleChi2 = chi2MatchTrigger;
553           }
554           minChi2MatchTrigger = chi2;
555           chi2MatchTrigger = chi2;
556           loTrgNum = triggerTrack->GetLoTrgNum();
557           AliMUONLocalTrigger* locTrg = triggerStore.FindLocal(loTrgNum);
558           matchTrigger=1;
559           if(locTrg->LoLpt()>0)matchTrigger=2;
560           if(locTrg->LoHpt()>0)matchTrigger=3;
561         }
562         else if(isDoubleTrack) 
563         {
564           doubleMatch = triggerTrack->GetLoTrgNum();
565           doubleChi2 = chi2;
566         }
567       }
568     }
569     if(doubleMatch>=0)
570     { // If two trigger tracks match, select the one passing more trigger cuts
571       AliDebug(1, Form("Two candidates found: %i and %i",loTrgNum,doubleMatch));
572       AliMUONLocalTrigger* locTrg1 = triggerStore.FindLocal(doubleMatch);
573       if((locTrg1->LoLpt()>0 && matchTrigger<2) || (locTrg1->LoHpt() && matchTrigger<3))
574       {
575         if(locTrg1->LoHpt()>0)matchTrigger=3;
576         else matchTrigger=2;
577         loTrgNum = doubleMatch;
578         chi2MatchTrigger=doubleChi2;
579       }
580     }
581     
582     track->SetMatchTrigger(matchTrigger);
583     track->SetLoTrgNum(loTrgNum);
584     track->SetChi2MatchTrigger(chi2MatchTrigger);
585     
586     AliMUONLocalTrigger* locTrg = static_cast<AliMUONLocalTrigger*>(triggerStore.FindLocal(loTrgNum));
587     
588     if (locTrg)
589     {    
590       track->SetLocalTrigger(locTrg->LoCircuit(),
591                              locTrg->LoStripX(),
592                              locTrg->LoStripY(),
593                              locTrg->LoDev(),
594                              locTrg->LoLpt(),
595                              locTrg->LoHpt());
596     }    
597   }  
598   
599   trackHitPattern.GetHitPattern(trackStore,triggerStore);
600 }
601
602   //__________________________________________________________________________
603 void AliMUONVTrackReconstructor::EventReconstructTrigger(const AliMUONTriggerCircuit& circuit,
604                                                          const AliMUONVTriggerStore& triggerStore,
605                                                          AliMUONVTriggerTrackStore& triggerTrackStore)
606 {
607   /// To make the trigger tracks from Local Trigger
608   AliDebug(1, "");
609   
610   AliMUONGlobalTrigger* globalTrigger = triggerStore.Global();
611   
612   UChar_t gloTrigPat = 0;
613
614   if (globalTrigger)
615   {
616     gloTrigPat = globalTrigger->GetGlobalResponse();
617   }
618   
619   TIter next(triggerStore.CreateIterator());
620   AliMUONLocalTrigger* locTrg(0x0);
621
622   Float_t z11 = AliMUONConstants::DefaultChamberZ(10);
623   Float_t z21 = AliMUONConstants::DefaultChamberZ(12);
624       
625   AliMUONTriggerTrack triggerTrack;
626   
627   while ( ( locTrg = static_cast<AliMUONLocalTrigger*>(next()) ) )
628   {
629     Bool_t xTrig=kFALSE;
630     Bool_t yTrig=kFALSE;
631     
632     Int_t localBoardId = locTrg->LoCircuit();
633     if ( locTrg->LoSdev()==1 && locTrg->LoDev()==0 && 
634          locTrg->LoStripX()==0) xTrig=kFALSE; // no trigger in X
635     else xTrig=kTRUE;                         // trigger in X
636     if (locTrg->LoTrigY()==1 && 
637         locTrg->LoStripY()==15 ) yTrig = kFALSE; // no trigger in Y
638     else yTrig = kTRUE;                          // trigger in Y
639     
640     if (xTrig && yTrig) 
641     { // make Trigger Track if trigger in X and Y
642       
643       Float_t y11 = circuit.GetY11Pos(localBoardId, locTrg->LoStripX()); 
644       // need first to convert deviation to [0-30] 
645       // (see AliMUONLocalTriggerBoard::LocalTrigger)
646       Int_t deviation = locTrg->LoDev(); 
647       Int_t sign = 0;
648       if ( !locTrg->LoSdev() &&  deviation ) sign=-1;
649       if ( !locTrg->LoSdev() && !deviation ) sign= 0;
650       if (  locTrg->LoSdev() == 1 )          sign=+1;
651       deviation *= sign;
652       deviation += 15;
653       Int_t stripX21 = locTrg->LoStripX()+deviation+1;
654       Float_t y21 = circuit.GetY21Pos(localBoardId, stripX21);       
655       Float_t x11 = circuit.GetX11Pos(localBoardId, locTrg->LoStripY());
656       
657       AliDebug(1, Form(" MakeTriggerTrack %d %d %d %d %f %f %f \n",locTrg->LoCircuit(),
658                        locTrg->LoStripX(),locTrg->LoStripX()+locTrg->LoDev()+1,locTrg->LoStripY(),y11, y21, x11));
659       
660       Float_t thetax = TMath::ATan2( x11 , z11 );
661       Float_t thetay = TMath::ATan2( (y21-y11) , (z21-z11) );
662       
663       triggerTrack.SetX11(x11);
664       triggerTrack.SetY11(y11);
665       triggerTrack.SetThetax(thetax);
666       triggerTrack.SetThetay(thetay);
667       triggerTrack.SetGTPattern(gloTrigPat);
668       triggerTrack.SetLoTrgNum(localBoardId);
669       
670       triggerTrackStore.Add(triggerTrack);
671     } // board is fired 
672   } // end of loop on Local Trigger
673 }
674