337053251dc19ec07ff607de7b8747a3e3503812
[u/mrichter/AliRoot.git] / MUON / AliMUONTrackHitPattern.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 AliMUONTrackHitPattern
21 ///
22 /// This class propagates tracks to trigger chambers 
23 /// searching for matching trigger tracks and fired strips.
24 ///
25 /// To each track, a hit pattern for trigger chambers is set.
26 /// 
27 /// The main method is:
28 /// * ExecuteValidation
29 ///
30 ///  \author Diego Stocco
31 //-----------------------------------------------------------------------------
32
33
34 #include "AliMUONTrackHitPattern.h"
35
36 #include "AliMUONConstants.h"
37 #include "AliMUONVDigit.h"
38 #include "AliMUONDigitMaker.h"
39 #include "AliMUONDigitStoreV1.h"
40 #include "AliMUONGeometryTransformer.h"
41 #include "AliMUONLocalTrigger.h"
42 #include "AliMUONLocalTriggerBoard.h"
43 #include "AliMUONRecoParam.h"
44 #include "AliMUONTrack.h"
45 #include "AliMUONTrackExtrap.h"
46 #include "AliMUONTrackParam.h"
47 #include "AliMUONVTrackStore.h"
48 #include "AliMUONVTriggerStore.h"
49 #include "AliMpPad.h"
50 #include "AliMpSegmentation.h"
51 #include "AliMpVSegmentation.h"
52 #include "AliMpDEManager.h"
53 #include "AliMUONReconstructor.h"
54 #include "AliMUONTriggerTrack.h"
55 #include "AliMUONVTriggerTrackStore.h"
56
57 #include "AliMpConstants.h"
58
59 #include "AliLog.h"
60 #include "AliTracker.h"
61 #include "AliESDMuonTrack.h"
62
63 #include <Riostream.h>
64 #include <TArrayS.h>
65 #include <TClonesArray.h>
66 #include <TMath.h>
67 #include <TMatrixD.h>
68 #include <TROOT.h>
69 #include <TDirectory.h>
70 #include <TFile.h>
71 #include <TSystem.h>
72 #include <TVector2.h>
73
74 #include <cassert>
75
76 /// \cond CLASSIMP
77 ClassImp(AliMUONTrackHitPattern) // Class implementation in ROOT context
78 /// \endcond
79
80
81 //______________________________________________________________________________
82 AliMUONTrackHitPattern::AliMUONTrackHitPattern(const AliMUONRecoParam* recoParam,
83                                                const AliMUONGeometryTransformer& transformer,
84                                                const AliMUONDigitMaker& digitMaker)
85 : TObject(),
86 fkRecoParam(recoParam),
87 fkTransformer(transformer),
88 fkDigitMaker(digitMaker),
89 fkMaxDistance(99999.)
90 {
91     /// Default constructor
92     AliMUONTrackExtrap::SetField();
93 }
94
95
96 //______________________________________________________________________________
97 AliMUONTrackHitPattern::~AliMUONTrackHitPattern(void)
98 {
99   /// Destructor
100 }
101
102 //_____________________________________________________________________________
103 void AliMUONTrackHitPattern::CheckConstants() const
104 {
105 /// Check consistence of redefined constants 
106
107   assert(fgkNcathodes == AliMpConstants::NofCathodes());    
108   assert(fgkNchambers == AliMpConstants::NofTriggerChambers());    
109   assert(fgkNplanes == AliMpConstants::NofTriggerChambers() * fgkNcathodes);    
110 }
111
112
113 //______________________________________________________________________________
114 void AliMUONTrackHitPattern::ExecuteValidation(const AliMUONVTrackStore& trackStore,
115                                                const AliMUONVTriggerTrackStore& triggerTrackStore,
116                                                const AliMUONVTriggerStore& triggerStore) const
117 {
118   //
119   /// Main method:
120   /// Loops on reco tracks, extrapolates them to trigger chambers
121   /// and searches for matching trigger tracks and digits
122   //
123
124   AliMUONDigitStoreV1 digitStore;
125   fkDigitMaker.TriggerToDigitsStore(triggerStore,digitStore);
126
127   // Get the hit pattern for all trigger tracks
128   AliMUONTriggerTrack* triggerTrack;
129   TIter itTriggerTrack(triggerTrackStore.CreateIterator());
130   while ( ( triggerTrack = static_cast<AliMUONTriggerTrack*>(itTriggerTrack() ) ) ){
131     UShort_t pattern = GetHitPattern(triggerTrack, digitStore);
132     triggerTrack->SetHitsPatternInTrigCh(pattern);
133     AliDebug(1, Form("Hit pattern: hits 0x%x  slat %2i  board %3i  effFlag %i",
134                      pattern & 0xFF, AliESDMuonTrack::GetSlatOrInfo(pattern),
135                      triggerTrack->GetLoTrgNum(), AliESDMuonTrack::GetEffFlag(pattern)));
136   }
137
138   // Match tracker tracks with trigger tracks.
139   TIter itTrack(trackStore.CreateIterator());
140   AliMUONTrack* track;
141
142   const Int_t kFirstTrigCh = AliMUONConstants::NTrackingCh();
143
144   while ( ( track = static_cast<AliMUONTrack*>(itTrack()) ) )
145   { 
146     AliMUONTrackParam trackParam(*((AliMUONTrackParam*) (track->GetTrackParamAtCluster()->Last())));
147
148     ApplyMCSCorrections(trackParam);
149     AliMUONTrackExtrap::ExtrapToZCov(&trackParam, AliMUONConstants::DefaultChamberZ(kFirstTrigCh)); // extrap to 1st trigger chamber
150
151     AliMUONTriggerTrack *matchedTriggerTrack = MatchTriggerTrack(track, trackParam, triggerTrackStore, triggerStore);
152
153     // Copy trigger tracks hit pattern if there is matching,
154     // otherwise calculate the hit pattern directly from tracker track:
155     // the obtained pattern is good for check, but not good for efficiency determination.
156     UShort_t pattern = matchedTriggerTrack ?
157       matchedTriggerTrack->GetHitsPatternInTrigCh() : 
158       GetHitPattern(&trackParam, digitStore);
159
160     track->SetHitsPatternInTrigCh(pattern);
161   }
162 }
163
164
165 //______________________________________________________________________________
166 AliMUONTriggerTrack * 
167 AliMUONTrackHitPattern::MatchTriggerTrack(AliMUONTrack* track,
168                                           AliMUONTrackParam& trackParam,
169                                           const AliMUONVTriggerTrackStore& triggerTrackStore,
170                                           const AliMUONVTriggerStore& triggerStore) const
171 {
172   //
173   /// Match track with trigger track
174   //
175
176   Int_t matchTrigger = 0;
177   Int_t loTrgNum(-1);
178   TMatrixD paramDiff(3,1);
179   Double_t chi2;
180   Double_t chi2MatchTrigger = 0., minChi2MatchTrigger = 999.;
181   Int_t doubleMatch = -1; // Check if track matches 2 trigger tracks
182   Double_t doubleChi2 = -1.;
183   AliMUONTriggerTrack* doubleTriggerTrack = 0x0;
184   AliMUONTriggerTrack* matchedTriggerTrack = 0x0;
185     
186
187   // Covariance matrix 3x3 (X,Y,slopeY) for trigger tracks
188   TMatrixD trackCov(3,3);
189
190   AliMUONTriggerTrack *triggerTrack;
191   TIter itTriggerTrack(triggerTrackStore.CreateIterator());
192   while ( ( triggerTrack = static_cast<AliMUONTriggerTrack*>(itTriggerTrack() ) ) )
193   {
194     AliMUONTrackExtrap::LinearExtrapToZCov(&trackParam, triggerTrack->GetZ11());
195     const TMatrixD& kParamCov = trackParam.GetCovariances();
196     
197     Double_t xTrack = trackParam.GetNonBendingCoor();
198     Double_t yTrack = trackParam.GetBendingCoor();
199     Double_t ySlopeTrack = trackParam.GetBendingSlope();
200
201     paramDiff(0,0) = triggerTrack->GetX11() - xTrack;
202     paramDiff(1,0) = triggerTrack->GetY11() - yTrack;
203     paramDiff(2,0) = triggerTrack->GetSlopeY() - ySlopeTrack;
204
205     // Covariance matrix 3x3 (X,Y,slopeY) for tracker tracks
206     trackCov.Zero();
207     trackCov(0,0) = kParamCov(0,0);
208     trackCov(1,1) = kParamCov(2,2);
209     trackCov(2,2) = kParamCov(3,3);
210     trackCov(1,2) = kParamCov(2,3);
211     trackCov(2,1) = kParamCov(3,2);
212
213     // Covariance matrix 3x3 (X,Y,slopeY) for trigger tracks
214     TMatrixD trigCov(triggerTrack->GetCovariances());
215
216     TMatrixD sumCov(trackCov,TMatrixD::kPlus,trigCov);
217     if (sumCov.Determinant() != 0) {
218       sumCov.Invert();
219       
220       TMatrixD tmp(sumCov,TMatrixD::kMult,paramDiff);
221       TMatrixD chi2M(paramDiff,TMatrixD::kTransposeMult,tmp);
222       chi2 = chi2M(0,0);      
223     } else {
224       AliWarning(" Determinant = 0");
225       Double_t sigma2 = 0.;
226       chi2 = 0.;
227       for (Int_t iVar = 0; iVar < 3; iVar++) {
228         sigma2 = trackCov(iVar,iVar) + trigCov(iVar,iVar);
229         chi2 += paramDiff(iVar,0) * paramDiff(iVar,0) / sigma2;
230       }
231     }
232
233     chi2 /= 3.; // Normalized Chi2: 3 degrees of freedom (X,Y,slopeY)
234     if (chi2 < GetRecoParam()->GetMaxNormChi2MatchTrigger()) 
235     {
236       Bool_t isDoubleTrack = (TMath::Abs(chi2 - minChi2MatchTrigger)<1.);
237       if (chi2 < minChi2MatchTrigger && chi2 < GetRecoParam()->GetMaxNormChi2MatchTrigger()) 
238       {
239         if(isDoubleTrack)
240         {
241           doubleMatch = loTrgNum;
242           doubleChi2 = chi2MatchTrigger;
243           doubleTriggerTrack = matchedTriggerTrack;
244         }
245         minChi2MatchTrigger = chi2;
246         chi2MatchTrigger = chi2;
247         loTrgNum = triggerTrack->GetLoTrgNum();
248         matchedTriggerTrack = triggerTrack;
249         AliMUONLocalTrigger* locTrg = triggerStore.FindLocal(loTrgNum);
250         matchTrigger = 1;
251         if(locTrg->LoLpt()>0) matchTrigger = 2;
252         if(locTrg->LoHpt()>0) matchTrigger = 3;
253       }
254       else if(isDoubleTrack) 
255       {
256         doubleMatch = triggerTrack->GetLoTrgNum();
257         doubleChi2 = chi2;
258       }
259     }
260   }
261   if(doubleMatch>=0)
262   { // If two trigger tracks match, select the one passing more trigger cuts
263     AliDebug(1, Form("Two candidates found: %i and %i",loTrgNum,doubleMatch));
264     AliMUONLocalTrigger* locTrg1 = triggerStore.FindLocal(doubleMatch);
265     if((locTrg1->LoLpt()>0 && matchTrigger<2) || (locTrg1->LoHpt() && matchTrigger<3))
266     {
267       if(locTrg1->LoHpt()>0) matchTrigger=3;
268       else matchTrigger = 2;
269       loTrgNum = doubleMatch;
270       chi2MatchTrigger = doubleChi2;
271       matchedTriggerTrack = doubleTriggerTrack;
272     }
273   }
274     
275   track->SetMatchTrigger(matchTrigger);
276   track->SetChi2MatchTrigger(chi2MatchTrigger);
277
278   AliMUONLocalTrigger* locTrg = static_cast<AliMUONLocalTrigger*>(triggerStore.FindLocal(loTrgNum));
279
280   if (locTrg)
281   {    
282     track->SetLocalTrigger(locTrg->LoCircuit(),
283                            locTrg->LoStripX(),
284                            locTrg->LoStripY(),
285                            locTrg->GetDeviation(),
286                            locTrg->LoLpt(),
287                            locTrg->LoHpt(),
288                            locTrg->GetTriggerWithoutChamber());
289   }
290
291   return matchedTriggerTrack;
292 }
293
294
295 //______________________________________________________________________________
296 UShort_t AliMUONTrackHitPattern::GetHitPattern(AliMUONTriggerTrack* matchedTriggerTrack,
297                                                AliMUONVDigitStore& digitStore) const
298 {
299   //
300   /// Get hit pattern on trigger chambers for the current trigger track
301   //
302   UShort_t pattern = 0;
303   PerformTrigTrackMatch(pattern, matchedTriggerTrack, digitStore);
304   return pattern;
305 }
306
307
308 //______________________________________________________________________________
309 UShort_t AliMUONTrackHitPattern::GetHitPattern(AliMUONTrackParam* trackParam,
310                                                AliMUONVDigitStore& digitStore) const
311 {
312   //
313   /// Get hit pattern on trigger chambers for the current tracker track
314   //
315   UShort_t pattern = 0;
316   Bool_t isMatch[2];
317   const Int_t kNTrackingCh = AliMUONConstants::NTrackingCh();
318
319   for(Int_t ch=0; ch<4; ++ch)
320   {
321     Int_t iChamber = kNTrackingCh+ch;
322     AliMUONTrackExtrap::ExtrapToZCov(trackParam, AliMUONConstants::DefaultChamberZ(iChamber));
323     FindPadMatchingTrack(digitStore, *trackParam, isMatch, iChamber);
324     for(Int_t cath=0; cath<2; ++cath)
325     {
326       if(isMatch[cath]) AliESDMuonTrack::SetFiredChamber(pattern, cath, ch);
327     }
328   }
329
330   // pattern obtained by propagation of tracker track
331   // when it does not match the trigger.
332   AliESDMuonTrack::AddEffInfo(pattern, AliESDMuonTrack::kTrackerTrackPattern);
333
334   return pattern;
335 }
336
337 //______________________________________________________________________________
338 void 
339 AliMUONTrackHitPattern::ApplyMCSCorrections(AliMUONTrackParam& trackParam) const
340 {
341   //
342   /// Returns uncertainties on extrapolated position.
343   /// Takes into account Branson plane corrections in the iron wall.
344   //
345
346   const Float_t kZFilterOut = AliMUONConstants::MuonFilterZEnd();
347   const Float_t kFilterThickness = kZFilterOut-AliMUONConstants::MuonFilterZBeg(); // cm
348
349   AliMUONTrackExtrap::ExtrapToZCov(&trackParam, kZFilterOut); // Extrap to muon filter end
350   AliMUONTrackExtrap::AddMCSEffect(&trackParam, kFilterThickness, AliMUONConstants::MuonFilterX0()); // Add MCS effects
351   return;
352 }
353
354
355 //______________________________________________________________________________
356 void 
357 AliMUONTrackHitPattern::FindPadMatchingTrack(const AliMUONVDigitStore& digitStore,
358                                              const AliMUONTrackParam& trackParam,
359                                              Bool_t isMatch[2], Int_t iChamber) const
360 {
361     //
362     /// Given the tracker track position, searches for matching digits.
363     //
364
365     Float_t minMatchDist[2];
366
367     for(Int_t cath=0; cath<2; ++cath)
368     {
369       isMatch[cath]=kFALSE;
370       minMatchDist[cath]=fkMaxDistance/10.;
371     }
372
373     TIter next(digitStore.CreateIterator());
374     AliMUONVDigit* mDigit;
375
376     while ( ( mDigit = static_cast<AliMUONVDigit*>(next()) ) )
377     {
378       Int_t currDetElemId = mDigit->DetElemId();
379       Int_t currCh = AliMpDEManager::GetChamberId(currDetElemId);
380       if(currCh!=iChamber) continue;
381       Int_t cathode = mDigit->Cathode();
382       Int_t ix = mDigit->PadX();
383       Int_t iy = mDigit->PadY();
384       Float_t xpad, ypad, zpad;
385       const AliMpVSegmentation* seg = AliMpSegmentation::Instance()
386         ->GetMpSegmentation(currDetElemId,AliMp::GetCathodType(cathode));
387       
388       AliMpPad pad = seg->PadByIndices(ix,iy,kTRUE);
389       Float_t xlocal1 = pad.GetPositionX();
390       Float_t ylocal1 = pad.GetPositionY();
391       Float_t dpx = pad.GetDimensionX();
392       Float_t dpy = pad.GetDimensionY();
393
394       fkTransformer.Local2Global(currDetElemId, xlocal1, ylocal1, 0, xpad, ypad, zpad);
395       Float_t matchDist = MinDistanceFromPad(xpad, ypad, zpad, dpx, dpy, trackParam);
396       if(matchDist>minMatchDist[cathode])continue;
397       isMatch[cathode] = kTRUE;
398       if(isMatch[0] && isMatch[1]) break;
399       minMatchDist[cathode] = matchDist;
400     }
401 }
402
403
404 //______________________________________________________________________________
405 Float_t 
406 AliMUONTrackHitPattern::MinDistanceFromPad(Float_t xPad, Float_t yPad, Float_t zPad,
407                                            Float_t dpx, Float_t dpy, 
408                                            const AliMUONTrackParam& trackParam) const
409 {
410     //
411     /// Decides if the digit belongs to the tracker track.
412     //
413
414     AliMUONTrackParam trackParamAtPadZ(trackParam);
415     AliMUONTrackExtrap::LinearExtrapToZCov(&trackParamAtPadZ, zPad);
416
417     Float_t xTrackAtPad = trackParamAtPadZ.GetNonBendingCoor();
418     Float_t yTrackAtPad = trackParamAtPadZ.GetBendingCoor();
419
420     const Float_t kNSigma = GetRecoParam()->GetSigmaCutForTrigger();
421
422     const TMatrixD& kCovParam = trackParamAtPadZ.GetCovariances();
423     
424     Float_t sigmaX = TMath::Sqrt(kCovParam(0,0));
425     Float_t sigmaY = TMath::Sqrt(kCovParam(2,2));
426
427     Float_t maxDistX = kNSigma * sigmaX; // in cm
428     Float_t maxDistY = kNSigma * sigmaY; // in cm
429
430     Float_t deltaX = TMath::Abs(xPad-xTrackAtPad)-dpx;
431     Float_t deltaY = TMath::Abs(yPad-yTrackAtPad)-dpy;
432
433     Float_t matchDist = fkMaxDistance;
434     if(deltaX<=maxDistX && deltaY<=maxDistY) matchDist = TMath::Max(deltaX, deltaY);
435
436     return matchDist;
437 }
438
439
440 //_____________________________________________________________________________
441 Int_t AliMUONTrackHitPattern::FindPadMatchingTrig(const AliMUONVDigitStore& digitStore, Int_t &detElemId,
442                                                   Float_t coor[2], Bool_t isMatch[2],
443                                                   TArrayI nboard[2], TArrayF &zRealMatch, Float_t y11) const
444 {
445     //
446     /// Check slat and board number of digit matching trigger track
447     //
448
449     enum {kBending, kNonBending};
450
451     Float_t minMatchDist[fgkNcathodes];
452     Int_t padsInCheckArea[fgkNcathodes];
453
454     for(Int_t cath=0; cath<fgkNcathodes; cath++){
455         isMatch[cath] = kFALSE;
456         minMatchDist[cath] = fkMaxDistance/10.;
457         padsInCheckArea[cath] = 0;
458     }
459     Int_t iChamber = AliMpDEManager::GetChamberId(detElemId);
460     Int_t ch = iChamber-10;
461     Float_t oldDeltaZ = AliMUONConstants::DefaultChamberZ(iChamber) - AliMUONConstants::DefaultChamberZ(10);
462     Float_t y = coor[1];
463     Int_t iSlat = detElemId%100;
464     Int_t trigDigitBendPlane = -1;
465     Int_t foundDetElemId = detElemId;
466     Float_t foundZmatch=999.;
467     Float_t yCoorAtPadZ=999.;
468
469     TIter next(digitStore.CreateIterator());
470     AliMUONVDigit* mDigit;
471     Int_t idigit=0;
472     
473     while ( ( mDigit = static_cast<AliMUONVDigit*>(next()) ) )
474     {
475         idigit++;
476         Int_t currDetElemId = mDigit->DetElemId();
477         Int_t currCh = AliMpDEManager::GetChamberId(currDetElemId);
478         if(currCh!=iChamber) continue;
479         Int_t currSlat = currDetElemId%100;
480         Int_t slatDiff = TMath::Abs(currSlat-iSlat);
481         if(slatDiff>1 && slatDiff<17) continue; // Check neighbour slats
482         Int_t cathode = mDigit->Cathode();
483         Int_t ix = mDigit->PadX();
484         Int_t iy = mDigit->PadY();
485         Float_t xpad, ypad, zpad;
486         const AliMpVSegmentation* seg = AliMpSegmentation::Instance()
487             ->GetMpSegmentation(currDetElemId,AliMp::GetCathodType(cathode));
488
489         AliMpPad pad = seg->PadByIndices(ix,iy,kTRUE);
490         Float_t xlocal1 = pad.GetPositionX();
491         Float_t ylocal1 = pad.GetPositionY();
492         Float_t dpx = pad.GetDimensionX();
493         Float_t dpy = pad.GetDimensionY();
494
495         fkTransformer.Local2Global(currDetElemId, xlocal1, ylocal1, 0, xpad, ypad, zpad);
496         AliDebug(2, Form("\nDetElemId = %i  Cathode = %i  Pad = (%i,%i) = (%.2f,%.2f)  Dim = (%.2f,%.2f)  Track = (%.2f,%.2f)\n",currDetElemId,cathode,ix,iy,xpad,ypad,dpx,dpy,coor[0],coor[1]));
497         // searching track intersection with chambers (second approximation)
498         if(ch%2==1){
499             //if(iChamber%2==1){
500             Float_t deltaZ = zpad - zRealMatch[0];
501             y = (coor[1]-y11)*deltaZ/oldDeltaZ + y11;
502             if(TMath::Abs(y-coor[1])>0.1) AliDebug(3, Form("oldDeltaZ = %7.2f   newDeltaZ = %7.2f\toldY = %7.2f   new y = %7.2f\n",oldDeltaZ,deltaZ,coor[1],y));
503         }
504         Float_t matchDist = PadMatchTrack(xpad, ypad, dpx, dpy, coor[0], y);
505         if(matchDist<fkMaxDistance/2.) padsInCheckArea[cathode]++;
506         if(matchDist>minMatchDist[cathode])continue;
507         isMatch[cathode] = kTRUE;
508         minMatchDist[cathode] = matchDist;
509         foundDetElemId = currDetElemId;
510         foundZmatch=zpad;
511         yCoorAtPadZ=y;
512         if(cathode==kBending) trigDigitBendPlane = idigit;
513         for (Int_t loc=0; loc<pad.GetNofLocations(); loc++){
514             nboard[cathode][loc] = pad.GetLocalBoardId(loc);
515         }
516         for(Int_t loc=pad.GetNofLocations(); loc<fgkNlocations; loc++){
517             nboard[cathode][loc]=-1;
518         }
519     }
520
521     if(isMatch[kBending] || isMatch[kNonBending]){
522         detElemId = foundDetElemId;
523         zRealMatch[ch] = foundZmatch;
524         coor[1] = yCoorAtPadZ;
525     }
526
527     // If track matches many pads, it is not good for effciency determination.
528     // However we still want to calculate the hit pattern.
529     for(Int_t cath=0; cath<fgkNcathodes; cath++){
530       if(padsInCheckArea[cath]>2) {
531         AliDebug(1, Form("padsInCheckArea[%i] = %i\n",cath,padsInCheckArea[cath]));
532         return -500;
533       }
534     }
535
536     return trigDigitBendPlane;
537 }
538
539 //_____________________________________________________________________________
540 Float_t AliMUONTrackHitPattern::PadMatchTrack(Float_t xPad, Float_t yPad,
541                                                 Float_t dpx, Float_t dpy, 
542                                                 Float_t xTrackAtPad, Float_t yTrackAtPad) const
543 {
544     //
545     /// Decides if the digit belongs to the trigger track.
546     //
547
548   Float_t maxDist = GetRecoParam()->GetStripCutForTrigger() * 2. * TMath::Min(dpx,dpy); // cm
549   if(maxDist<2.) maxDist = 2.;
550   Float_t maxDistCheckArea = GetRecoParam()->GetMaxStripAreaForTrigger() * 2. *  TMath::Min(dpx,dpy); // cm
551
552     Float_t matchDist = fkMaxDistance;
553
554     Float_t deltaX = TMath::Abs(xPad-xTrackAtPad)-dpx;
555     Float_t deltaY = TMath::Abs(yPad-yTrackAtPad)-dpy;
556     Float_t maxDistX = maxDist;
557     Float_t maxDistY = maxDist;
558
559     if(deltaX<=maxDistX && deltaY<=maxDistY) matchDist = TMath::Max(deltaX, deltaY);
560     else if(deltaX<=maxDistCheckArea && deltaY<=maxDistCheckArea) matchDist = fkMaxDistance/5.;
561     return matchDist;
562 }
563
564
565 //_____________________________________________________________________________
566 Int_t AliMUONTrackHitPattern::DetElemIdFromPos(Float_t x, Float_t y, 
567                                                  Int_t chamber, Int_t cathode) const
568 {
569     //
570     /// Given the (x,y) position in the chamber,
571     /// it returns the corresponding slat
572     //
573
574     Int_t resultingDetElemId = -1;
575     AliMpDEIterator it;
576     Float_t minDist = 999.;
577     for ( it.First(chamber-1); ! it.IsDone(); it.Next() ){
578         Int_t detElemId = it.CurrentDEId();
579         Int_t ich = detElemId/100-10;
580         Float_t tolerance=0.2*((Float_t)ich);
581         Float_t currDist=9999.;
582
583         const AliMpVSegmentation* seg = 
584             AliMpSegmentation::Instance()
585             ->GetMpSegmentation(detElemId,AliMp::GetCathodType(cathode));
586         if (!seg) continue;
587
588         Float_t deltax = seg->GetDimensionX();
589         Float_t deltay = seg->GetDimensionY();
590         Float_t xlocal1 =  -deltax;
591         Float_t ylocal1 =  -deltay;
592         Float_t xlocal2 =  +deltax;
593         Float_t ylocal2 =  +deltay;
594         Float_t xg01, yg01, zg1, xg02, yg02, zg2;
595         fkTransformer.Local2Global(detElemId, xlocal1, ylocal1, 0, xg01, yg01, zg1);
596         fkTransformer.Local2Global(detElemId, xlocal2, ylocal2, 0, xg02, yg02, zg2);
597
598         Float_t xg1 = xg01, xg2 = xg02, yg1 = yg01, yg2 = yg02;
599
600         if(xg01>xg02){
601             xg1 = xg02;
602             xg2 = xg01;
603         }
604         if(yg01>yg02){
605             yg1 = yg02;
606             yg2 = yg01;
607         }
608
609         if(x>=xg1-tolerance && x<=xg2+tolerance && y>=yg1-tolerance && y<=yg2+tolerance){ // takes into account errors in extrapolation
610             if(y<yg1) currDist = yg1-y;
611             else if(y>yg2) currDist = y-yg2;
612             if(currDist<minDist) {
613                 resultingDetElemId = detElemId;
614                 minDist=currDist;
615                 continue;
616             }
617             resultingDetElemId = detElemId;
618             break;
619         }
620     } // loop on detElemId
621     return resultingDetElemId;
622 }
623
624
625 //_____________________________________________________________________________
626 void AliMUONTrackHitPattern::LocalBoardFromPos(Float_t x, Float_t y,
627                                                  Int_t detElemId, Int_t cathode,
628                                                  Int_t localBoard[4]) const
629 {
630     //
631     /// Given the (x,y) position in the chamber,
632     /// it returns the corresponding local board
633     //
634
635     for(Int_t loc=0; loc<fgkNlocations; loc++){
636         localBoard[loc]=-1;
637     }
638     Float_t xg, yg, zg;
639     fkTransformer.Local2Global(detElemId, 0., 0., 0., xg, yg, zg);
640     Float_t z = zg - (y-yg)*TMath::Tan(TMath::DegToRad()*AliMUONConstants::St345Inclination());
641     Float_t xl, yl, zl;
642     fkTransformer.Global2Local(detElemId, x, y, z, xl, yl, zl);
643     const AliMpVSegmentation* seg = 
644         AliMpSegmentation::Instance()
645           ->GetMpSegmentation(detElemId,AliMp::GetCathodType(cathode));
646     if ( ! seg ) return;
647     AliMpPad pad = seg->PadByPosition(xl,yl,kFALSE);
648     if ( ! pad.IsValid() ){
649       AliWarning(Form("Pad not found! DetElemId %i  position global (%f, %f, %f)  local (%f, %f, %f)\n", detElemId, x, y, z, xl, yl, zl));
650       return;
651     }
652
653     AliDebug(2, Form("\nWould match: DetElemId = %i  Pad = (%i,%i) = (%.2f,%.2f,%.2f)   Board %3i\n",detElemId,pad.GetIx(),pad.GetIy(),xl,yl,zl,pad.GetLocalBoardId(0)));
654
655     for (Int_t loc=0; loc<pad.GetNofLocations(); loc++){
656       localBoard[loc] = pad.GetLocalBoardId(loc);
657     }
658 }
659
660
661 //_____________________________________________________________________________
662 Bool_t AliMUONTrackHitPattern::PerformTrigTrackMatch(UShort_t &pattern,
663                                                      const AliMUONTriggerTrack* matchedTrigTrack,
664                                                      AliMUONVDigitStore& digitStore) const
665 {
666   //
667   /// It searches for matching digits around the trigger track.
668   //
669
670   enum {kBending, kNonBending};
671
672   Int_t chOrder[fgkNchambers] = {0,2,1,3};
673
674   TArrayF zRealMatch(fgkNchambers);
675
676   Bool_t isMatch[fgkNcathodes];
677   for(Int_t cath=0; cath<fgkNcathodes; cath++){
678     isMatch[cath] = kFALSE;
679   }
680
681   TArrayF zMeanChamber(fgkNchambers);
682   zMeanChamber[0] = matchedTrigTrack->GetZ11();
683   zMeanChamber[1] = matchedTrigTrack->GetZ11() + AliMUONConstants::DefaultChamberZ(11) - AliMUONConstants::DefaultChamberZ(10);
684   zMeanChamber[2] = matchedTrigTrack->GetZ21();
685   zMeanChamber[3] = matchedTrigTrack->GetZ21() + AliMUONConstants::DefaultChamberZ(13) - AliMUONConstants::DefaultChamberZ(12);
686
687   TArrayI digitPerTrack(fgkNcathodes);
688
689   Float_t trackIntersectCh[fgkNchambers][fgkNcathodes];
690
691   TArrayI triggeredDigits;
692   triggeredDigits.Set(fgkNchambers);
693   triggeredDigits.Reset(-1);
694
695   TArrayI trigScheme[fgkNcathodes];
696   TArrayI slatThatTriggered[fgkNcathodes];
697   for(Int_t cath=0; cath<fgkNcathodes; cath++){
698     trigScheme[cath].Set(fgkNchambers);
699     slatThatTriggered[cath].Set(fgkNchambers);
700   }
701
702   Int_t boardThatTriggered[fgkNchambers][fgkNcathodes][fgkNlocations];
703   TArrayI nboard[fgkNcathodes];
704   for(Int_t cath=0; cath<fgkNcathodes; cath++){
705     nboard[cath].Set(fgkNlocations);
706   }
707   Int_t ineffBoard[fgkNlocations];
708   for(Int_t loc=0; loc<fgkNlocations; loc++){
709     ineffBoard[loc] = -1;
710   }
711
712   digitPerTrack.Reset();
713   for(Int_t ch=0; ch<fgkNchambers; ch++){
714     zRealMatch[ch] = zMeanChamber[ch];
715     for(Int_t cath=0; cath<fgkNcathodes; cath++){
716       for(Int_t loc=0; loc<fgkNlocations; loc++){
717         boardThatTriggered[ch][cath][loc]=-1;
718       }
719     }
720   }
721
722   for(Int_t cath=0; cath<fgkNcathodes; cath++){
723     slatThatTriggered[cath].Reset(-1);
724     trigScheme[cath].Reset();
725   }
726
727   Bool_t isClearEvent = kTRUE;
728
729   Float_t y11 = matchedTrigTrack->GetY11();// y position (info from bending plane)
730   Float_t slopeX = matchedTrigTrack->GetSlopeX();
731   Float_t slopeY = matchedTrigTrack->GetSlopeY();
732
733   for(Int_t ch=0; ch<fgkNchambers; ch++) { // chamber loop
734     Int_t currCh = chOrder[ch];
735     AliDebug(3, Form("zMeanChamber[%i] = %.2f\tzRealMatch[0] = %.2f\n",currCh,zMeanChamber[currCh],zRealMatch[0]));
736
737     // searching track intersection with chambers (first approximation)
738     Float_t deltaZ = zMeanChamber[currCh] - zMeanChamber[0];
739     trackIntersectCh[currCh][0] = zMeanChamber[currCh] * slopeX;
740     trackIntersectCh[currCh][1] = y11 + deltaZ * slopeY;
741     Int_t detElemIdFromTrack = DetElemIdFromPos(trackIntersectCh[currCh][0], trackIntersectCh[currCh][1], 11+currCh, 0);
742     if(detElemIdFromTrack<0) {
743       AliDebug(1, "Warning: trigger track outside trigger chamber\n");
744       isClearEvent = kFALSE;
745
746       // track is rejected since the extrapolated track
747       // does not match a slat (border effects)
748       AliESDMuonTrack::AddEffInfo(pattern, AliESDMuonTrack::kTrackOutsideGeometry);
749       continue;
750     }
751                 
752     triggeredDigits[currCh] = FindPadMatchingTrig(digitStore, detElemIdFromTrack, trackIntersectCh[currCh], isMatch, nboard, zRealMatch, y11);
753
754     // if FindPadMatchingTrig = -500 => too many digits matching pad =>
755     //                               => Event not clear => Do not use for efficiency calculation
756     if(triggeredDigits[currCh]<-100){
757       isClearEvent = kFALSE;
758       // track is rejected since it matches many pads
759       AliESDMuonTrack::AddEffInfo(pattern, AliESDMuonTrack::kTrackMatchesManyPads);
760       AliDebug(1, Form("Warning: track = %p (%i) matches many pads. Rejected!\n",(void *)matchedTrigTrack, detElemIdFromTrack));
761     }
762
763     for(Int_t cath=0; cath<fgkNcathodes; cath++){
764       if(!isMatch[cath]) continue;
765       AliESDMuonTrack::SetFiredChamber(pattern, cath, currCh);
766       digitPerTrack[cath]++;
767       trigScheme[cath][currCh]++;
768       slatThatTriggered[cath][currCh] = detElemIdFromTrack;
769       for(Int_t loc=0; loc<fgkNlocations; loc++){
770         boardThatTriggered[currCh][cath][loc] = nboard[cath][loc];
771       }
772     }
773   } // end chamber loop
774
775   for(Int_t cath=0; cath<fgkNcathodes; cath++){
776     if(digitPerTrack[cath]<3) {
777       isClearEvent = kFALSE;
778       // track is rejected since the number of associated
779       // digits found is less than 3.
780       AliESDMuonTrack::AddEffInfo(pattern, AliESDMuonTrack::kTrackMatchesFewPads);
781       AliDebug(1, Form("Warning: found %i digits for trigger track cathode %i.\nRejecting event\n", digitPerTrack[cath],cath));
782     }
783   }
784
785   if(!isClearEvent) return kFALSE;
786
787   AliESDMuonTrack::EAliTriggerChPatternFlag goodForEff = AliESDMuonTrack::kBoardEff;
788
789   Int_t ineffSlat = -1;
790   Int_t ineffDetElId = -1;
791   Int_t firstSlat = slatThatTriggered[kBending][0]%100;
792   if(firstSlat<0) firstSlat = slatThatTriggered[kBending][1]%100;
793   Int_t firstBoard = boardThatTriggered[0][kBending][0];
794   if(firstBoard<0) firstBoard = boardThatTriggered[1][kBending][0];
795   for(Int_t ch=0; ch<fgkNchambers; ch++){
796     Bool_t isCurrChIneff = kFALSE;
797     Int_t currSlat = slatThatTriggered[kBending][ch]%100;
798     if(currSlat<0){
799       ineffDetElId = DetElemIdFromPos(trackIntersectCh[ch][0], trackIntersectCh[ch][1], 11+ch, kBending);
800       currSlat = ineffDetElId%100;
801       ineffSlat = currSlat;
802       isCurrChIneff = kTRUE;
803     }
804     if(currSlat!=firstSlat) {
805       AliESDMuonTrack::AddEffInfo(pattern,
806                                   AliESDMuonTrack::kCrossDifferentSlats,
807                                   AliESDMuonTrack::kChEff); // track crosses different slats
808       return kTRUE;
809     }
810     Bool_t atLeastOneLoc=kFALSE;
811     if(isCurrChIneff) LocalBoardFromPos(trackIntersectCh[ch][0], trackIntersectCh[ch][1], ineffDetElId, kBending, ineffBoard);
812     for(Int_t loc=0; loc<fgkNlocations; loc++){
813       Int_t currBoard = boardThatTriggered[ch][kBending][loc];
814       if(isCurrChIneff) currBoard = ineffBoard[loc];
815       if(currBoard==firstBoard){
816         atLeastOneLoc=kTRUE;
817         break;
818       }
819     }
820     if(!atLeastOneLoc) goodForEff = AliESDMuonTrack::kSlatEff;
821   } // end chamber loop
822   
823   AliESDMuonTrack::AddEffInfo(pattern, firstSlat, goodForEff);
824   return kTRUE;
825 }