1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
18 /// \class AliMUONTrackReconstructor
19 /// MUON track reconstructor using the original method
21 /// This class contains as data:
22 /// - the parameters for the track reconstruction
24 /// It contains as methods, among others:
25 /// - MakeTracks to build the tracks
28 #include "AliMUONTrackReconstructor.h"
30 #include "AliMUONConstants.h"
31 #include "AliMUONHitForRec.h"
32 #include "AliMUONTrack.h"
33 #include "AliMUONTrackParam.h"
34 #include "AliMUONTrackExtrap.h"
38 #include <Riostream.h>
42 // Functions to be minimized with Minuit
43 void TrackChi2(Int_t &nParam, Double_t *gradient, Double_t &chi2, Double_t *param, Int_t flag);
46 ClassImp(AliMUONTrackReconstructor) // Class implementation in ROOT context
49 //************* Parameters for reconstruction
50 const Double_t AliMUONTrackReconstructor::fgkBendingVertexDispersion = 10.;
51 const Double_t AliMUONTrackReconstructor::fgkNonBendingVertexDispersion = 10.;
54 //__________________________________________________________________________
55 AliMUONTrackReconstructor::AliMUONTrackReconstructor()
56 : AliMUONVTrackReconstructor()
58 /// Constructor for class AliMUONTrackReconstructor
59 AliInfo("*** Original tracking ***");
62 //__________________________________________________________________________
63 AliMUONTrackReconstructor::~AliMUONTrackReconstructor()
68 //__________________________________________________________________________
69 void AliMUONTrackReconstructor::MakeTrackCandidates()
71 /// To make track candidates:
72 /// Start with segments station(1..) 4 or 5 then follow track in station 5 or 4.
73 /// Good candidates are made of at least three hitForRec's.
74 /// Keep only best candidates or all of them according to the flag fgkTrackAllTracks.
75 TClonesArray *segments;
79 AliDebug(1,"Enter MakeTrackCandidates");
81 // Loop over stations(1..) 5 and 4 and make track candidates
82 for (Int_t istat=4; istat>=3; istat--) {
84 // Make segments in the station
85 segments = MakeSegmentsInStation(istat);
88 for (Int_t iseg=0; iseg<segments->GetEntriesFast(); iseg++)
90 AliDebug(1,Form("Making primary candidate(1..) %d",++iCandidate));
92 // Transform segments to tracks and put them at the end of fRecTracksPtr
93 track = new ((*fRecTracksPtr)[fRecTracksPtr->GetLast()+1]) AliMUONTrack((AliMUONObjectPair*)((*segments)[iseg]));
96 // Printout for debuging
97 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 2) || (AliLog::GetGlobalDebugLevel() >= 2))
99 cout<<endl<<"Track parameter covariances at first hit with multiple Coulomb scattering effects:"<<endl;
100 ((AliMUONTrackParam*) track->GetTrackParamAtHit()->First())->GetCovariances().Print();
103 // Look for compatible hitForRec(s) in the other station
104 if (!FollowTrackInStation(*track,7-istat)) {
105 fRecTracksPtr->Remove(track);
111 // delete the array of segments
115 fRecTracksPtr->Compress(); // this is essential before checking tracks
117 // Keep all different tracks or only the best ones as required
118 if (fgkTrackAllTracks) RemoveIdenticalTracks();
119 else RemoveDoubleTracks();
121 AliDebug(1,Form("Number of good candidates = %d",fNRecTracks));
125 //__________________________________________________________________________
126 void AliMUONTrackReconstructor::FollowTracks()
128 /// Follow tracks in stations(1..) 3, 2 and 1
129 AliDebug(1,"Enter FollowTracks");
131 AliMUONTrack *track, *nextTrack;
132 Int_t currentNRecTracks;
135 for (Int_t station = 2; station >= 0; station--) {
137 // Save the actual number of reconstructed track in case of
138 // tracks are added or suppressed during the tracking procedure
139 // !! Do not compress fRecTracksPtr until the end of the loop over tracks !!
140 currentNRecTracks = fNRecTracks;
142 for (Int_t iRecTrack = 0; iRecTrack <currentNRecTracks; iRecTrack++) {
143 AliDebug(1,Form("FollowTracks: track candidate(1..) %d", iRecTrack+1));
145 track = (AliMUONTrack*) fRecTracksPtr->UncheckedAt(iRecTrack);
148 // Do not take into account the multiple scattering to speed up the fit
149 // Calculate the track parameter covariance matrix
150 // If "station" is station(1..) 3 then use the vertex to better constrain the fit
152 SetVertexForFit(*track);
153 track->SetFitWithVertex(kTRUE);
154 } else track->SetFitWithVertex(kFALSE);
155 Fit(*track, kFALSE, kTRUE);
157 // Remove the track if the normalized chi2 is too high
158 if (track->GetNormalizedChi2() > fgkSigmaToCutForTracking * fgkSigmaToCutForTracking) {
159 fRecTracksPtr->Remove(track);
164 // Printout for debuging
165 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 2) || (AliLog::GetGlobalDebugLevel() >= 2)) {
166 cout<<endl<<"Track parameter covariances at first hit with multiple Coulomb scattering effects:"<<endl;
167 ((AliMUONTrackParam*) track->GetTrackParamAtHit()->First())->GetCovariances().Print();
170 // Look for compatible hitForRec in station(0..) "station"
171 hitFound = FollowTrackInStation(*track,station);
173 // Try to recover track if required
174 if (!hitFound && fgkRecoverTracks) hitFound = RecoverTrack(*track,station);
176 // remove track if no hit found
178 fRecTracksPtr->Remove(track);
184 // Compress fRecTracksPtr for the next step
185 fRecTracksPtr->Compress();
187 // Keep only the best tracks if required
188 if (!fgkTrackAllTracks) RemoveDoubleTracks();
192 // Last fit of track candidates with all station
193 // Take into account the multiple scattering and remove bad tracks
194 Int_t trackIndex = -1;
195 track = (AliMUONTrack*) fRecTracksPtr->First();
199 nextTrack = (AliMUONTrack*) fRecTracksPtr->After(track); // prepare next track
201 track->SetFitWithVertex(kFALSE); // just to be sure
202 Fit(*track, kTRUE, kTRUE);
204 // Printout for debuging
205 if (AliLog::GetGlobalDebugLevel() >= 3) {
206 cout << "FollowTracks: track candidate(0..) " << trackIndex << " after final fit" << endl;
207 track->RecursiveDump();
210 // Remove the track if the normalized chi2 is too high
211 if (track->GetNormalizedChi2() > fgkSigmaToCutForTracking * fgkSigmaToCutForTracking) {
212 fRecTracksPtr->Remove(track);
220 fRecTracksPtr->Compress();
224 //__________________________________________________________________________
225 Bool_t AliMUONTrackReconstructor::FollowTrackInStation(AliMUONTrack &trackCandidate, Int_t nextStation)
227 /// Follow trackCandidate in station(0..) nextStation and search for compatible HitForRec(s)
228 /// Keep all possibilities or only the best one(s) according to the flag fgkTrackAllTracks:
229 /// kTRUE: duplicate "trackCandidate" if there are several possibilities and add the new tracks at the end of
230 /// fRecTracksPtr to avoid conficts with other track candidates at this current stage of the tracking procedure.
231 /// Remove the obsolete "trackCandidate" at the end.
232 /// kFALSE: add only the best hit(s) to the "trackCandidate". Try to add a couple of hits in priority.
233 AliDebug(1,Form("Enter FollowTrackInStation(1..) %d", nextStation+1));
235 // Order the chamber according to the propagation direction (tracking starts with chamber 2):
236 // - nextStation == station(1...) 5 => forward propagation
237 // - nextStation < station(1...) 5 => backward propagation
239 if (nextStation==4) {
240 ch1 = 2*nextStation+1;
244 ch2 = 2*nextStation+1;
247 Double_t zCh2 = AliMUONConstants::DefaultChamberZ(ch2);
248 Double_t chi2WithOneHitForRec = 1.e10;
249 Double_t chi2WithTwoHitForRec = 1.e10;
250 Double_t maxChi2WithOneHitForRec = 2. * fgkSigmaToCutForTracking * fgkSigmaToCutForTracking; // 2 because 2 quantities in chi2
251 Double_t maxChi2WithTwoHitForRec = 4. * fgkSigmaToCutForTracking * fgkSigmaToCutForTracking; // 4 because 4 quantities in chi2
252 Double_t bestChi2WithOneHitForRec = maxChi2WithOneHitForRec;
253 Double_t bestChi2WithTwoHitForRec = maxChi2WithTwoHitForRec;
254 Bool_t foundOneHit = kFALSE;
255 Bool_t foundTwoHits = kFALSE;
256 AliMUONTrack *newTrack = 0x0;
257 AliMUONHitForRec *hitForRecCh1, *hitForRecCh2;
258 AliMUONTrackParam extrapTrackParamAtHit1;
259 AliMUONTrackParam extrapTrackParamAtHit2;
260 AliMUONTrackParam bestTrackParamAtHit1;
261 AliMUONTrackParam bestTrackParamAtHit2;
262 Bool_t *hitForRecCh1Used = new Bool_t[fNHitsForRecPerChamber[ch1]];
263 for (Int_t hit1 = 0; hit1 < fNHitsForRecPerChamber[ch1]; hit1++) hitForRecCh1Used[hit1] = kFALSE;
265 // Get track parameters and extrapolate them to chamber "ch2" to save computing time in the next steps
266 AliMUONTrackParam extrapTrackParamAtCh2(*(AliMUONTrackParam*)trackCandidate.GetTrackParamAtHit()->First());
269 AliMUONTrackExtrap::AddMCSEffect(&extrapTrackParamAtCh2,AliMUONConstants::ChamberThicknessInX0(),1.);
271 // Add MCS in the missing chamber if any (only 1 chamber can be missing according to tracking criteria)
272 if (ch1 < ch2 && extrapTrackParamAtCh2.GetHitForRecPtr()->GetChamberNumber() > ch2 + 1) {
273 // extrapolation to the missing chamber
274 AliMUONTrackExtrap::ExtrapToZCov(&extrapTrackParamAtCh2, AliMUONConstants::DefaultChamberZ(ch2 + 1));
276 AliMUONTrackExtrap::AddMCSEffect(&extrapTrackParamAtCh2,AliMUONConstants::ChamberThicknessInX0(),1.);
279 //Extrapolate trackCandidate to chamber "ch2"
280 AliMUONTrackExtrap::ExtrapToZCov(&extrapTrackParamAtCh2, zCh2);
282 // Printout for debuging
283 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 2) || (AliLog::GetGlobalDebugLevel() >= 2)) {
284 cout<<endl<<"Track parameter covariances at first hit extrapolated to z = "<<zCh2<<":"<<endl;
285 extrapTrackParamAtCh2.GetCovariances().Print();
288 // Printout for debuging
289 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
290 cout << "FollowTrackInStation: look for hits in chamber(1..): " << ch2+1 << endl;
293 // look for candidates in chamber 2
294 for (Int_t hit2 = 0; hit2 < fNHitsForRecPerChamber[ch2]; hit2++) {
296 hitForRecCh2 = (AliMUONHitForRec*) fHitsForRecPtr->UncheckedAt(fIndexOfFirstHitForRecPerChamber[ch2]+hit2);
298 // try to add the current hit
299 chi2WithOneHitForRec = TryOneHitForRec(extrapTrackParamAtCh2, hitForRecCh2, extrapTrackParamAtHit2);
301 // if good chi2 then try to attach a hitForRec in the other chamber too
302 if (chi2WithOneHitForRec < maxChi2WithOneHitForRec) {
303 Bool_t foundSecondHit = kFALSE;
305 // Printout for debuging
306 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
307 cout << "FollowTrackInStation: found one hit in chamber(1..): " << ch2+1
308 << " (Chi2 = " << chi2WithOneHitForRec << ")" << endl;
309 cout << " look for second hits in chamber(1..): " << ch1+1 << " ..." << endl;
312 // add MCS effect for next step
313 AliMUONTrackExtrap::AddMCSEffect(&extrapTrackParamAtHit2,AliMUONConstants::ChamberThicknessInX0(),1.);
315 for (Int_t hit1 = 0; hit1 < fNHitsForRecPerChamber[ch1]; hit1++) {
317 hitForRecCh1 = (AliMUONHitForRec*) fHitsForRecPtr->UncheckedAt(fIndexOfFirstHitForRecPerChamber[ch1]+hit1);
319 // try to add the current hit in addition to the one found on the previous chamber
320 chi2WithTwoHitForRec = TryTwoHitForRec(extrapTrackParamAtHit2, hitForRecCh1, extrapTrackParamAtHit1);
322 // if good chi2 then create a new track by adding the 2 hitForRec to the "trackCandidate"
323 if (chi2WithTwoHitForRec < maxChi2WithTwoHitForRec) {
324 foundSecondHit = kTRUE;
325 foundTwoHits = kTRUE;
327 // Printout for debuging
328 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
329 cout << "FollowTrackInStation: found second hit in chamber(1..): " << ch1+1
330 << " (Global Chi2 = " << chi2WithTwoHitForRec << ")" << endl;
333 if (fgkTrackAllTracks) {
334 // copy trackCandidate into a new track put at the end of fRecTracksPtr and add the new hitForRec's
335 newTrack = new ((*fRecTracksPtr)[fRecTracksPtr->GetLast()+1]) AliMUONTrack(trackCandidate);
336 UpdateTrack(*newTrack,extrapTrackParamAtHit1,extrapTrackParamAtHit2);
339 // Tag hitForRecCh1 as used
340 hitForRecCh1Used[hit1] = kTRUE;
342 // Printout for debuging
343 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
344 cout << "FollowTrackInStation: added two hits in station(1..): " << nextStation+1 << endl;
345 if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
348 } else if (chi2WithTwoHitForRec < bestChi2WithTwoHitForRec) {
349 // keep track of the best couple of hits
350 bestChi2WithTwoHitForRec = chi2WithTwoHitForRec;
351 bestTrackParamAtHit1 = extrapTrackParamAtHit1;
352 bestTrackParamAtHit2 = extrapTrackParamAtHit2;
359 // if no hitForRecCh1 found then consider to add hitForRecCh2 only
360 if (!foundSecondHit) {
363 if (fgkTrackAllTracks) {
364 // copy trackCandidate into a new track put at the end of fRecTracksPtr and add the new hitForRec's
365 newTrack = new ((*fRecTracksPtr)[fRecTracksPtr->GetLast()+1]) AliMUONTrack(trackCandidate);
366 UpdateTrack(*newTrack,extrapTrackParamAtHit2);
369 // Printout for debuging
370 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
371 cout << "FollowTrackInStation: added one hit in chamber(1..): " << ch2+1 << endl;
372 if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
375 } else if (!foundTwoHits && chi2WithOneHitForRec < bestChi2WithOneHitForRec) {
376 // keep track of the best single hitForRec except if a couple
377 // of hits has already been found (i.e. bestHitForRec2!=0x0)
378 bestChi2WithOneHitForRec = chi2WithOneHitForRec;
379 bestTrackParamAtHit1 = extrapTrackParamAtHit2;
388 // look for candidates in chamber 1 not already attached to a track
389 // if we want to keep all possible tracks or if no good couple of hitForRec has been found
390 if (fgkTrackAllTracks || !foundTwoHits) {
392 // Printout for debuging
393 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
394 cout << "FollowTrackInStation: look for single hits in chamber(1..): " << ch1+1 << endl;
397 // add MCS effect for next step
398 AliMUONTrackExtrap::AddMCSEffect(&extrapTrackParamAtCh2,AliMUONConstants::ChamberThicknessInX0(),1.);
400 for (Int_t hit1 = 0; hit1 < fNHitsForRecPerChamber[ch1]; hit1++) {
402 hitForRecCh1 = (AliMUONHitForRec*) fHitsForRecPtr->UncheckedAt(fIndexOfFirstHitForRecPerChamber[ch1]+hit1);
404 if (hitForRecCh1Used[hit1]) continue; // Skip hitForRec already used
406 // try to add the current hit
407 chi2WithOneHitForRec = TryOneHitForRec(extrapTrackParamAtCh2, hitForRecCh1, extrapTrackParamAtHit1);
409 // if good chi2 then consider to add hitForRecCh1
410 // We do not try to attach a hitForRec in the other chamber too since it has already been done above
411 if (chi2WithOneHitForRec < maxChi2WithOneHitForRec) {
414 // Printout for debuging
415 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
416 cout << "FollowTrackInStation: found one hit in chamber(1..): " << ch1+1
417 << " (Chi2 = " << chi2WithOneHitForRec << ")" << endl;
420 if (fgkTrackAllTracks) {
421 // copy trackCandidate into a new track put at the end of fRecTracksPtr and add the new hitForRec's
422 newTrack = new ((*fRecTracksPtr)[fRecTracksPtr->GetLast()+1]) AliMUONTrack(trackCandidate);
423 UpdateTrack(*newTrack,extrapTrackParamAtHit1);
426 // Printout for debuging
427 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
428 cout << "FollowTrackInStation: added one hit in chamber(1..): " << ch1+1 << endl;
429 if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
432 } else if (chi2WithOneHitForRec < bestChi2WithOneHitForRec) {
433 // keep track of the best single hitForRec except if a couple
434 // of hits has already been found (i.e. bestHitForRec1!=0x0)
435 bestChi2WithOneHitForRec = chi2WithOneHitForRec;
436 bestTrackParamAtHit1 = extrapTrackParamAtHit1;
445 // fill out the best track if required else clean up the fRecTracksPtr array
446 if (!fgkTrackAllTracks) {
448 UpdateTrack(trackCandidate,bestTrackParamAtHit1,bestTrackParamAtHit2);
450 // Printout for debuging
451 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
452 cout << "FollowTrackInStation: added the two best hits in station(1..): " << nextStation+1 << endl;
453 if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
456 } else if (foundOneHit) {
457 UpdateTrack(trackCandidate,bestTrackParamAtHit1);
459 // Printout for debuging
460 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
461 cout << "FollowTrackInStation: added the best hit in chamber(1..): " << bestTrackParamAtHit1.GetHitForRecPtr()->GetChamberNumber()+1 << endl;
462 if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
465 } else return kFALSE;
467 } else if (foundOneHit || foundTwoHits) {
469 // remove obsolete track
470 fRecTracksPtr->Remove(&trackCandidate);
473 } else return kFALSE;
479 //__________________________________________________________________________
480 Double_t AliMUONTrackReconstructor::TryTwoHitForRec(const AliMUONTrackParam &trackParamAtHit1, AliMUONHitForRec* hitForRec2, AliMUONTrackParam &trackParamAtHit2)
482 /// Test the compatibility between the track and the 2 hitForRec together (using trackParam's covariance matrix):
483 /// return the corresponding Chi2 accounting for covariances between the 2 hitForRec
484 /// return trackParamAtHit1 & 2
486 if (!trackParamAtHit1.CovariancesExist()) AliWarning(" track parameter covariance matrix does not exist");
487 AliMUONHitForRec* hitForRec1 = trackParamAtHit1.GetHitForRecPtr();
488 if (!hitForRec1) AliFatal(" no hitForRec attached to trackParamAtHit1");
490 // extrapolate track parameters at the z position of the second hit
491 trackParamAtHit2.SetParameters(trackParamAtHit1.GetParameters());
492 trackParamAtHit2.SetZ(trackParamAtHit1.GetZ());
493 AliMUONTrackExtrap::ExtrapToZ(&trackParamAtHit2, hitForRec2->GetZ());
495 // set pointer to hit2 into trackParamAtHit2
496 trackParamAtHit2.SetHitForRecPtr(hitForRec2);
498 // Set differences between track and the 2 hitForRec in the bending and non bending directions
500 dPos(0,0) = hitForRec1->GetNonBendingCoor() - trackParamAtHit1.GetNonBendingCoor();
501 dPos(1,0) = hitForRec1->GetBendingCoor() - trackParamAtHit1.GetBendingCoor();
502 dPos(2,0) = hitForRec2->GetNonBendingCoor() - trackParamAtHit2.GetNonBendingCoor();
503 dPos(3,0) = hitForRec2->GetBendingCoor() - trackParamAtHit2.GetBendingCoor();
505 // quick tests of hitForRec compatibility within a wide road of x*y = 1*1 cm2 to save computing time
506 if (TMath::Abs(dPos(0,0)) > fgkMaxTrackingDistanceNonBending ||
507 TMath::Abs(dPos(1,0)) > fgkMaxTrackingDistanceBending ||
508 TMath::Abs(dPos(2,0)) > fgkMaxTrackingDistanceNonBending ||
509 TMath::Abs(dPos(3,0)) > fgkMaxTrackingDistanceBending) return 1.e10;
511 // Calculate the error matrix from the track parameter covariances at first hitForRec
514 if (trackParamAtHit1.CovariancesExist()) {
515 // Save track parameters at first hitForRec
516 AliMUONTrackParam trackParamAtHit1Save(trackParamAtHit1);
517 TMatrixD paramAtHit1Save(trackParamAtHit1Save.GetParameters());
518 Double_t z1 = trackParamAtHit1Save.GetZ();
520 // Save track coordinates at second hitForRec
521 Double_t nonBendingCoor2 = trackParamAtHit2.GetNonBendingCoor();
522 Double_t bendingCoor2 = trackParamAtHit2.GetBendingCoor();
524 // add MCS effect at first hitForRec
525 AliMUONTrackExtrap::AddMCSEffect(&trackParamAtHit1Save,AliMUONConstants::ChamberThicknessInX0(),1.);
527 // Get the pointer to the parameter covariance matrix at first hitForRec
528 const TMatrixD& kParamCov = trackParamAtHit1Save.GetCovariances();
530 // Calculate the jacobian related to the transformation between track parameters
531 // at first hitForRec and track coordinates at the 2 hitForRec z-position
534 // first derivative at the first hitForRec:
535 jacob(0,0) = 1.; // dx1/dx
536 jacob(1,2) = 1.; // dy1/dy
537 // first derivative at the second hitForRec:
538 TMatrixD dParam(5,1);
539 for (Int_t i=0; i<5; i++) {
540 // Skip jacobian calculation for parameters with no associated error
541 if (kParamCov(i,i) == 0.) continue;
542 // Small variation of parameter i only
543 for (Int_t j=0; j<5; j++) {
545 dParam(j,0) = TMath::Sqrt(kParamCov(i,i));
546 if (j == 4) dParam(j,0) *= TMath::Sign(1.,-paramAtHit1Save(4,0)); // variation always in the same direction
547 } else dParam(j,0) = 0.;
550 // Set new track parameters at first hitForRec
551 trackParamAtHit1Save.SetParameters(paramAtHit1Save);
552 trackParamAtHit1Save.AddParameters(dParam);
553 trackParamAtHit1Save.SetZ(z1);
555 // Extrapolate new track parameters to the z position of the second hitForRec
556 AliMUONTrackExtrap::ExtrapToZ(&trackParamAtHit1Save,hitForRec2->GetZ());
558 // Calculate the jacobian
559 jacob(2,i) = (trackParamAtHit1Save.GetNonBendingCoor() - nonBendingCoor2) / dParam(i,0); // dx2/dParami
560 jacob(3,i) = (trackParamAtHit1Save.GetBendingCoor() - bendingCoor2 ) / dParam(i,0); // dy2/dParami
563 // Calculate the error matrix
564 TMatrixD tmp(jacob,TMatrixD::kMult,kParamCov);
565 error = TMatrixD(tmp,TMatrixD::kMultTranspose,jacob);
568 // Add hitForRec resolution to the error matrix
569 error(0,0) += hitForRec1->GetNonBendingReso2();
570 error(1,1) += hitForRec1->GetBendingReso2();
571 error(2,2) += hitForRec2->GetNonBendingReso2();
572 error(3,3) += hitForRec2->GetBendingReso2();
574 // invert the error matrix for Chi2 calculation
575 if (error.Determinant() != 0) {
578 AliWarning(" Determinant error=0");
582 // Compute the Chi2 value
583 TMatrixD tmp2(dPos,TMatrixD::kTransposeMult,error);
584 TMatrixD result(tmp2,TMatrixD::kMult,dPos);
590 //__________________________________________________________________________
591 void AliMUONTrackReconstructor::UpdateTrack(AliMUONTrack &track, AliMUONTrackParam &trackParamAtHit)
593 /// Add 1 hit to the track candidate
594 /// Update chi2 of the track
596 // Compute local chi2
597 AliMUONHitForRec* hit = trackParamAtHit.GetHitForRecPtr();
598 Double_t deltaX = trackParamAtHit.GetNonBendingCoor() - hit->GetNonBendingCoor();
599 Double_t deltaY = trackParamAtHit.GetBendingCoor() - hit->GetBendingCoor();
600 Double_t localChi2 = deltaX*deltaX / hit->GetNonBendingReso2() +
601 deltaY*deltaY / hit->GetBendingReso2();
603 // Update the chi2 of the new track
604 track.SetFitFMin(track.GetFitFMin() + localChi2);
606 // Update TrackParamAtHit
607 track.AddTrackParamAtHit(&trackParamAtHit,trackParamAtHit.GetHitForRecPtr());
608 track.GetTrackParamAtHit()->Sort();
612 //__________________________________________________________________________
613 void AliMUONTrackReconstructor::UpdateTrack(AliMUONTrack &track, AliMUONTrackParam &trackParamAtHit1, AliMUONTrackParam &trackParamAtHit2)
615 /// Add 2 hits to the track candidate
616 /// Update track and local chi2
618 // Update local chi2 at first hit
619 AliMUONHitForRec* hit1 = trackParamAtHit1.GetHitForRecPtr();
620 Double_t deltaX = trackParamAtHit1.GetNonBendingCoor() - hit1->GetNonBendingCoor();
621 Double_t deltaY = trackParamAtHit1.GetBendingCoor() - hit1->GetBendingCoor();
622 Double_t localChi2AtHit1 = deltaX*deltaX / hit1->GetNonBendingReso2() +
623 deltaY*deltaY / hit1->GetBendingReso2();
624 trackParamAtHit1.SetLocalChi2(localChi2AtHit1);
626 // Flag first hit as being removable
627 trackParamAtHit1.SetRemovable(kTRUE);
629 // Update local chi2 at second hit
630 AliMUONHitForRec* hit2 = trackParamAtHit2.GetHitForRecPtr();
631 deltaX = trackParamAtHit2.GetNonBendingCoor() - hit2->GetNonBendingCoor();
632 deltaY = trackParamAtHit2.GetBendingCoor() - hit2->GetBendingCoor();
633 Double_t localChi2AtHit2 = deltaX*deltaX / hit2->GetNonBendingReso2() +
634 deltaY*deltaY / hit2->GetBendingReso2();
635 trackParamAtHit2.SetLocalChi2(localChi2AtHit2);
637 // Flag first hit as being removable
638 trackParamAtHit2.SetRemovable(kTRUE);
640 // Update the chi2 of the new track
641 track.SetFitFMin(track.GetFitFMin() + localChi2AtHit1 + localChi2AtHit2);
643 // Update TrackParamAtHit
644 track.AddTrackParamAtHit(&trackParamAtHit1,trackParamAtHit1.GetHitForRecPtr());
645 track.AddTrackParamAtHit(&trackParamAtHit2,trackParamAtHit2.GetHitForRecPtr());
646 track.GetTrackParamAtHit()->Sort();
650 //__________________________________________________________________________
651 Bool_t AliMUONTrackReconstructor::RecoverTrack(AliMUONTrack &trackCandidate, Int_t nextStation)
653 /// Try to recover the track candidate in the next station
654 /// by removing the worst of the two hits attached in the current station
655 /// Return kTRUE if recovering succeeds
656 AliDebug(1,"Enter RecoverTrack");
658 // Do not try to recover track until we have attached hit(s) on station(1..) 3
659 if (nextStation > 1) return kFALSE;
661 Int_t worstHitNumber = -1;
662 Double_t localChi2, worstLocalChi2 = 0.;
664 // Look for the hit to remove
665 for (Int_t hitNumber = 0; hitNumber < 2; hitNumber++) {
666 AliMUONTrackParam *trackParamAtHit = (AliMUONTrackParam*)trackCandidate.GetTrackParamAtHit()->UncheckedAt(hitNumber);
668 // check if current hit is removable
669 if (!trackParamAtHit->IsRemovable()) return kFALSE;
671 // Pick up hit with the worst chi2
672 localChi2 = trackParamAtHit->GetLocalChi2();
673 if (localChi2 > worstLocalChi2) {
674 worstLocalChi2 = localChi2;
675 worstHitNumber = hitNumber;
679 // Reset best hit as being NOT removable
680 ((AliMUONTrackParam*)trackCandidate.GetTrackParamAtHit()->UncheckedAt((worstHitNumber+1)%2))->SetRemovable(kFALSE);
682 // Remove the worst hit
683 trackCandidate.RemoveTrackParamAtHit((AliMUONTrackParam*)trackCandidate.GetTrackParamAtHit()->UncheckedAt(worstHitNumber));
686 // Do not take into account the multiple scattering to speed up the fit
687 // Calculate the track parameter covariance matrix
688 trackCandidate.SetFitWithVertex(kFALSE); // To be sure
689 Fit(trackCandidate, kFALSE, kTRUE);
691 // Look for new hit(s) in next station
692 return FollowTrackInStation(trackCandidate,nextStation);
696 //__________________________________________________________________________
697 void AliMUONTrackReconstructor::SetVertexForFit(AliMUONTrack &trackCandidate)
699 /// Add the vertex as a measured hit to constrain the fit of the "trackCandidate"
700 /// Compute the vertex resolution from natural vertex dispersion and
701 /// multiple scattering effets according to trackCandidate path in absorber
702 /// It is necessary to account for multiple scattering effects here instead of during the fit of
703 /// the "trackCandidate" to do not influence the result by changing track resolution at vertex
704 AliDebug(1,"Enter SetVertexForFit");
706 Double_t nonBendingReso2 = fgkNonBendingVertexDispersion * fgkNonBendingVertexDispersion;
707 Double_t bendingReso2 = fgkBendingVertexDispersion * fgkBendingVertexDispersion;
708 // add multiple scattering effets
709 AliMUONTrackParam paramAtVertex(*((AliMUONTrackParam*)(trackCandidate.GetTrackParamAtHit()->First())));
710 paramAtVertex.DeleteCovariances(); // to be sure to account only for multiple scattering
711 AliMUONTrackExtrap::ExtrapToVertexUncorrected(¶mAtVertex,0.);
712 const TMatrixD& kParamCov = paramAtVertex.GetCovariances();
713 nonBendingReso2 += kParamCov(0,0);
714 bendingReso2 += kParamCov(2,2);
716 AliMUONHitForRec vertex; // Coordinates set to (0.,0.,0.) by default
717 vertex.SetNonBendingReso2(nonBendingReso2);
718 vertex.SetBendingReso2(bendingReso2);
719 trackCandidate.SetVertex(&vertex);
722 //__________________________________________________________________________
723 void AliMUONTrackReconstructor::Fit(AliMUONTrack &track, Bool_t includeMCS, Bool_t calcCov)
725 /// Fit the track "track" w/wo multiple Coulomb scattering according to "includeMCS".
727 Double_t benC, errorParam, invBenP, nonBenC, x, y;
728 AliMUONTrackParam *trackParam;
729 Double_t arg[1], fedm, errdef, fitFMin;
731 Int_t status, covStatus;
733 // Instantiate gMinuit if not already done
734 if (!gMinuit) gMinuit = new TMinuit(6);
735 // Clear MINUIT parameters
737 // Give the fitted track to MINUIT
738 gMinuit->SetObjectFit(&track);
739 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 2) || (AliLog::GetGlobalDebugLevel() >= 2)) {
740 // Define print level
742 gMinuit->mnexcm("SET PRI", arg, 1, status);
743 // Print covariance matrix
744 gMinuit->mnexcm("SHO COV", arg, 0, status);
747 gMinuit->mnexcm("SET PRI", arg, 1, status);
750 gMinuit->mnexcm("SET NOW", arg, 0, status);
753 //gMinuit->mnexcm("SET STR", arg, 1, status);
755 // set flag w/wo multiple scattering according to "includeMCS"
756 track.SetFitWithMCS(includeMCS);
758 // compute hit weights only once
759 if (!track.ComputeHitWeights()) {
760 AliWarning("cannot take into account the multiple scattering effects");
761 track.SetFitWithMCS(kFALSE);
765 // Set fitting function
766 gMinuit->SetFCN(TrackChi2);
768 // Set fitted parameters (!! The order is very important for the covariance matrix !!)
769 trackParam = (AliMUONTrackParam*) (track.GetTrackParamAtHit()->First());
770 // could be tried with no limits for the search (min=max=0) ????
771 // mandatory limits in non Bending to avoid NaN values of parameters
772 gMinuit->mnparm(0, "X", trackParam->GetNonBendingCoor(), 0.03, -500.0, 500.0, status);
773 gMinuit->mnparm(1, "NonBenS", trackParam->GetNonBendingSlope(), 0.001, -0.5, 0.5, status);
774 // mandatory limits in Bending to avoid NaN values of parameters
775 gMinuit->mnparm(2, "Y", trackParam->GetBendingCoor(), 0.10, -500.0, 500.0, status);
776 gMinuit->mnparm(3, "BenS", trackParam->GetBendingSlope(), 0.001, -0.5, 0.5, status);
777 gMinuit->mnparm(4, "InvBenP", trackParam->GetInverseBendingMomentum(), 0.003, -0.4, 0.4, status);
780 gMinuit->mnexcm("MIGRAD", arg, 0, status);
782 // Calculate the covariance matrix more accurately if required
783 if (calcCov) gMinuit->mnexcm("HESSE", arg, 0, status);
785 // get results into "invBenP", "benC", "nonBenC" ("x", "y")
786 gMinuit->GetParameter(0, x, errorParam);
787 trackParam->SetNonBendingCoor(x);
788 gMinuit->GetParameter(1, nonBenC, errorParam);
789 trackParam->SetNonBendingSlope(nonBenC);
790 gMinuit->GetParameter(2, y, errorParam);
791 trackParam->SetBendingCoor(y);
792 gMinuit->GetParameter(3, benC, errorParam);
793 trackParam->SetBendingSlope(benC);
794 gMinuit->GetParameter(4, invBenP, errorParam);
795 trackParam->SetInverseBendingMomentum(invBenP);
797 // global result of the fit
798 gMinuit->mnstat(fitFMin, fedm, errdef, npari, nparx, covStatus);
799 track.SetFitFMin(fitFMin);
801 // Get the covariance matrix if required
803 // Covariance matrix according to HESSE status
804 // If problem then keep only the diagonal terms (variances)
805 Double_t matrix[5][5];
806 gMinuit->mnemat(&matrix[0][0],5);
807 if (covStatus == 3) trackParam->SetCovariances(matrix);
808 else trackParam->SetVariances(matrix);
809 } else trackParam->DeleteCovariances();
813 //__________________________________________________________________________
814 void TrackChi2(Int_t & /*nParam*/, Double_t * /*gradient*/, Double_t &chi2, Double_t *param, Int_t /*flag*/)
816 /// Return the "Chi2" to be minimized with Minuit for track fitting.
817 /// Assumes that the track hits are sorted according to increasing Z.
818 /// Track parameters at each TrackHit are updated accordingly.
819 /// Vertex is used according to the flag "trackBeingFitted->GetFitWithVertex()".
820 /// Multiple Coulomb scattering is taken into account according to the flag "trackBeingFitted->GetFitWithMCS()".
822 AliMUONTrack *trackBeingFitted = (AliMUONTrack*) gMinuit->GetObjectFit();
823 AliMUONTrackParam* trackParamAtHit = (AliMUONTrackParam*) trackBeingFitted->GetTrackParamAtHit()->First();
825 chi2 = 0.; // initialize chi2
827 // update track parameters
828 trackParamAtHit->SetNonBendingCoor(param[0]);
829 trackParamAtHit->SetNonBendingSlope(param[1]);
830 trackParamAtHit->SetBendingCoor(param[2]);
831 trackParamAtHit->SetBendingSlope(param[3]);
832 trackParamAtHit->SetInverseBendingMomentum(param[4]);
833 trackBeingFitted->UpdateTrackParamAtHit();
835 // Take the vertex into account in the fit if required
836 if (trackBeingFitted->GetFitWithVertex()) {
837 AliMUONTrackParam paramAtVertex(*trackParamAtHit);
838 AliMUONTrackExtrap::ExtrapToZ(¶mAtVertex, 0.);
839 AliMUONHitForRec *vertex = trackBeingFitted->GetVertex();
841 cout<<"Error in TrackChi2: Want to use the vertex in tracking but it has not been created!!"<<endl;
844 dX = vertex->GetNonBendingCoor() - paramAtVertex.GetNonBendingCoor();
845 dY = vertex->GetBendingCoor() - paramAtVertex.GetBendingCoor();
846 chi2 += dX * dX / vertex->GetNonBendingReso2() + dY * dY / vertex->GetBendingReso2();
849 // compute chi2 w/wo multiple scattering
850 if (trackBeingFitted->GetFitWithMCS()) chi2 += trackBeingFitted->ComputeGlobalChi2(kTRUE);
851 else chi2 += trackBeingFitted->ComputeGlobalChi2(kFALSE);
855 //__________________________________________________________________________
856 void AliMUONTrackReconstructor::ImproveTracks()
858 /// Improve tracks by removing clusters with local chi2 highter than the defined cut
859 /// Recompute track parameters and covariances at the remaining clusters
860 AliDebug(1,"Enter ImproveTracks");
862 Double_t localChi2, worstLocalChi2;
864 AliMUONTrackParam *trackParamAtHit, *worstTrackParamAtHit;
866 // Remove double track to improve only "good" tracks
867 RemoveDoubleTracks();
869 AliMUONTrack *track = (AliMUONTrack*) fRecTracksPtr->First();
872 while (!track->IsImproved()) {
874 // Update track parameters and covariances
875 track->UpdateCovTrackParamAtHit();
877 // Compute local chi2 of each hits
878 track->ComputeLocalChi2(kTRUE);
880 // Look for the hit to remove
881 worstTrackParamAtHit = 0;
883 trackParamAtHit = (AliMUONTrackParam*) track->GetTrackParamAtHit()->First();
884 while (trackParamAtHit) {
886 // Pick up hit with the worst chi2
887 localChi2 = trackParamAtHit->GetLocalChi2();
888 if (localChi2 > worstLocalChi2) {
889 worstLocalChi2 = localChi2;
890 worstTrackParamAtHit = trackParamAtHit;
893 trackParamAtHit = (AliMUONTrackParam*) track->GetTrackParamAtHit()->After(trackParamAtHit);
896 // Check if bad hit found
897 if (!worstTrackParamAtHit) {
898 track->SetImproved(kTRUE);
902 // check whether the worst hit is removable or not
903 if (!worstTrackParamAtHit->IsRemovable()) {
904 track->SetImproved(kTRUE);
908 // Check whether the worst chi2 is under requirement or not
909 if (worstLocalChi2 < 2. * fgkSigmaToCutForImprovement * fgkSigmaToCutForImprovement) { // 2 because 2 quantities in chi2
910 track->SetImproved(kTRUE);
914 // Reset the second hit in the same station as the bad one as being NOT removable
915 worstChamber = worstTrackParamAtHit->GetHitForRecPtr()->GetChamberNumber();
916 if (worstChamber%2 == 0) ((AliMUONTrackParam*)track->GetTrackParamAtHit()->After(worstTrackParamAtHit))->SetRemovable(kFALSE);
917 else ((AliMUONTrackParam*)track->GetTrackParamAtHit()->Before(worstTrackParamAtHit))->SetRemovable(kFALSE);
919 // Remove the worst hit
920 track->RemoveTrackParamAtHit(worstTrackParamAtHit);
923 // Take into account the multiple scattering
924 // Calculate the track parameter covariance matrix
925 track->SetFitWithVertex(kFALSE); // To be sure
926 Fit(*track, kTRUE, kTRUE);
928 // Printout for debuging
929 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
930 cout << "ImproveTracks: track " << fRecTracksPtr->IndexOf(track)+1 << " improved " << endl;
935 track = (AliMUONTrack*) fRecTracksPtr->After(track);
940 //__________________________________________________________________________
941 void AliMUONTrackReconstructor::Finalize()
943 /// Fill AliMUONTrack's fHitForRecAtHit array
944 /// Recompute track parameters and covariances at each attached cluster from those at the first one
946 AliMUONTrackParam *trackParamAtHit;
948 track = (AliMUONTrack*) fRecTracksPtr->First();
951 // update track parameters if not already done
952 if (!track->IsImproved()) track->UpdateCovTrackParamAtHit();
954 trackParamAtHit = (AliMUONTrackParam*) (track->GetTrackParamAtHit()->First());
955 while (trackParamAtHit) {
957 // update array of track hit
958 track->AddHitForRecAtHit(trackParamAtHit->GetHitForRecPtr());
960 trackParamAtHit = (AliMUONTrackParam*) (track->GetTrackParamAtHit()->After(trackParamAtHit));
963 track = (AliMUONTrack*) fRecTracksPtr->After(track);