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 // Flag hit as being not removable
604 trackParamAtHit.SetRemovable(kFALSE);
605 trackParamAtHit.SetLocalChi2(0.); // --> Local chi2 not used
607 // Update the chi2 of the new track
608 track.SetFitFMin(track.GetFitFMin() + localChi2);
610 // Update TrackParamAtHit
611 track.AddTrackParamAtHit(&trackParamAtHit,trackParamAtHit.GetHitForRecPtr());
612 track.GetTrackParamAtHit()->Sort();
616 //__________________________________________________________________________
617 void AliMUONTrackReconstructor::UpdateTrack(AliMUONTrack &track, AliMUONTrackParam &trackParamAtHit1, AliMUONTrackParam &trackParamAtHit2)
619 /// Add 2 hits to the track candidate
620 /// Update track and local chi2
622 // Update local chi2 at first hit
623 AliMUONHitForRec* hit1 = trackParamAtHit1.GetHitForRecPtr();
624 Double_t deltaX = trackParamAtHit1.GetNonBendingCoor() - hit1->GetNonBendingCoor();
625 Double_t deltaY = trackParamAtHit1.GetBendingCoor() - hit1->GetBendingCoor();
626 Double_t localChi2AtHit1 = deltaX*deltaX / hit1->GetNonBendingReso2() +
627 deltaY*deltaY / hit1->GetBendingReso2();
628 trackParamAtHit1.SetLocalChi2(localChi2AtHit1);
630 // Flag first hit as being removable
631 trackParamAtHit1.SetRemovable(kTRUE);
633 // Update local chi2 at second hit
634 AliMUONHitForRec* hit2 = trackParamAtHit2.GetHitForRecPtr();
635 deltaX = trackParamAtHit2.GetNonBendingCoor() - hit2->GetNonBendingCoor();
636 deltaY = trackParamAtHit2.GetBendingCoor() - hit2->GetBendingCoor();
637 Double_t localChi2AtHit2 = deltaX*deltaX / hit2->GetNonBendingReso2() +
638 deltaY*deltaY / hit2->GetBendingReso2();
639 trackParamAtHit2.SetLocalChi2(localChi2AtHit2);
641 // Flag first hit as being removable
642 trackParamAtHit2.SetRemovable(kTRUE);
644 // Update the chi2 of the new track
645 track.SetFitFMin(track.GetFitFMin() + localChi2AtHit1 + localChi2AtHit2);
647 // Update TrackParamAtHit
648 track.AddTrackParamAtHit(&trackParamAtHit1,trackParamAtHit1.GetHitForRecPtr());
649 track.AddTrackParamAtHit(&trackParamAtHit2,trackParamAtHit2.GetHitForRecPtr());
650 track.GetTrackParamAtHit()->Sort();
654 //__________________________________________________________________________
655 Bool_t AliMUONTrackReconstructor::RecoverTrack(AliMUONTrack &trackCandidate, Int_t nextStation)
657 /// Try to recover the track candidate in the next station
658 /// by removing the worst of the two hits attached in the current station
659 /// Return kTRUE if recovering succeeds
660 AliDebug(1,"Enter RecoverTrack");
662 // Do not try to recover track until we have attached hit(s) on station(1..) 3
663 if (nextStation > 1) return kFALSE;
665 Int_t worstHitNumber = -1;
666 Double_t localChi2, worstLocalChi2 = 0.;
668 // Look for the hit to remove
669 for (Int_t hitNumber = 0; hitNumber < 2; hitNumber++) {
670 AliMUONTrackParam *trackParamAtHit = (AliMUONTrackParam*)trackCandidate.GetTrackParamAtHit()->UncheckedAt(hitNumber);
672 // check if current hit is removable
673 if (!trackParamAtHit->IsRemovable()) return kFALSE;
675 // Pick up hit with the worst chi2
676 localChi2 = trackParamAtHit->GetLocalChi2();
677 if (localChi2 > worstLocalChi2) {
678 worstLocalChi2 = localChi2;
679 worstHitNumber = hitNumber;
683 // Reset best hit as being NOT removable
684 ((AliMUONTrackParam*)trackCandidate.GetTrackParamAtHit()->UncheckedAt((worstHitNumber+1)%2))->SetRemovable(kFALSE);
686 // Remove the worst hit
687 trackCandidate.RemoveTrackParamAtHit((AliMUONTrackParam*)trackCandidate.GetTrackParamAtHit()->UncheckedAt(worstHitNumber));
690 // Do not take into account the multiple scattering to speed up the fit
691 // Calculate the track parameter covariance matrix
692 trackCandidate.SetFitWithVertex(kFALSE); // To be sure
693 Fit(trackCandidate, kFALSE, kTRUE);
695 // Look for new hit(s) in next station
696 return FollowTrackInStation(trackCandidate,nextStation);
700 //__________________________________________________________________________
701 void AliMUONTrackReconstructor::SetVertexForFit(AliMUONTrack &trackCandidate)
703 /// Add the vertex as a measured hit to constrain the fit of the "trackCandidate"
704 /// Compute the vertex resolution from natural vertex dispersion and
705 /// multiple scattering effets according to trackCandidate path in absorber
706 /// It is necessary to account for multiple scattering effects here instead of during the fit of
707 /// the "trackCandidate" to do not influence the result by changing track resolution at vertex
708 AliDebug(1,"Enter SetVertexForFit");
710 Double_t nonBendingReso2 = fgkNonBendingVertexDispersion * fgkNonBendingVertexDispersion;
711 Double_t bendingReso2 = fgkBendingVertexDispersion * fgkBendingVertexDispersion;
712 // add multiple scattering effets
713 AliMUONTrackParam paramAtVertex(*((AliMUONTrackParam*)(trackCandidate.GetTrackParamAtHit()->First())));
714 paramAtVertex.DeleteCovariances(); // to be sure to account only for multiple scattering
715 AliMUONTrackExtrap::ExtrapToVertexUncorrected(¶mAtVertex,0.);
716 const TMatrixD& kParamCov = paramAtVertex.GetCovariances();
717 nonBendingReso2 += kParamCov(0,0);
718 bendingReso2 += kParamCov(2,2);
720 AliMUONHitForRec vertex; // Coordinates set to (0.,0.,0.) by default
721 vertex.SetNonBendingReso2(nonBendingReso2);
722 vertex.SetBendingReso2(bendingReso2);
723 trackCandidate.SetVertex(&vertex);
726 //__________________________________________________________________________
727 void AliMUONTrackReconstructor::Fit(AliMUONTrack &track, Bool_t includeMCS, Bool_t calcCov)
729 /// Fit the track "track" w/wo multiple Coulomb scattering according to "includeMCS".
731 Double_t benC, errorParam, invBenP, nonBenC, x, y;
732 AliMUONTrackParam *trackParam;
733 Double_t arg[1], fedm, errdef, fitFMin;
735 Int_t status, covStatus;
737 // Instantiate gMinuit if not already done
738 if (!gMinuit) gMinuit = new TMinuit(6);
739 // Clear MINUIT parameters
741 // Give the fitted track to MINUIT
742 gMinuit->SetObjectFit(&track);
743 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 2) || (AliLog::GetGlobalDebugLevel() >= 2)) {
744 // Define print level
746 gMinuit->mnexcm("SET PRI", arg, 1, status);
747 // Print covariance matrix
748 gMinuit->mnexcm("SHO COV", arg, 0, status);
751 gMinuit->mnexcm("SET PRI", arg, 1, status);
754 gMinuit->mnexcm("SET NOW", arg, 0, status);
757 //gMinuit->mnexcm("SET STR", arg, 1, status);
759 // set flag w/wo multiple scattering according to "includeMCS"
760 track.SetFitWithMCS(includeMCS);
762 // compute hit weights only once
763 if (!track.ComputeHitWeights()) {
764 AliWarning("cannot take into account the multiple scattering effects");
765 track.SetFitWithMCS(kFALSE);
769 // Set fitting function
770 gMinuit->SetFCN(TrackChi2);
772 // Set fitted parameters (!! The order is very important for the covariance matrix !!)
773 trackParam = (AliMUONTrackParam*) (track.GetTrackParamAtHit()->First());
774 // could be tried with no limits for the search (min=max=0) ????
775 // mandatory limits in non Bending to avoid NaN values of parameters
776 gMinuit->mnparm(0, "X", trackParam->GetNonBendingCoor(), 0.03, -500.0, 500.0, status);
777 gMinuit->mnparm(1, "NonBenS", trackParam->GetNonBendingSlope(), 0.001, -0.5, 0.5, status);
778 // mandatory limits in Bending to avoid NaN values of parameters
779 gMinuit->mnparm(2, "Y", trackParam->GetBendingCoor(), 0.10, -500.0, 500.0, status);
780 gMinuit->mnparm(3, "BenS", trackParam->GetBendingSlope(), 0.001, -0.5, 0.5, status);
781 gMinuit->mnparm(4, "InvBenP", trackParam->GetInverseBendingMomentum(), 0.003, -0.4, 0.4, status);
784 gMinuit->mnexcm("MIGRAD", arg, 0, status);
786 // Calculate the covariance matrix more accurately if required
787 if (calcCov) gMinuit->mnexcm("HESSE", arg, 0, status);
789 // get results into "invBenP", "benC", "nonBenC" ("x", "y")
790 gMinuit->GetParameter(0, x, errorParam);
791 trackParam->SetNonBendingCoor(x);
792 gMinuit->GetParameter(1, nonBenC, errorParam);
793 trackParam->SetNonBendingSlope(nonBenC);
794 gMinuit->GetParameter(2, y, errorParam);
795 trackParam->SetBendingCoor(y);
796 gMinuit->GetParameter(3, benC, errorParam);
797 trackParam->SetBendingSlope(benC);
798 gMinuit->GetParameter(4, invBenP, errorParam);
799 trackParam->SetInverseBendingMomentum(invBenP);
801 // global result of the fit
802 gMinuit->mnstat(fitFMin, fedm, errdef, npari, nparx, covStatus);
803 track.SetFitFMin(fitFMin);
805 // Get the covariance matrix if required
807 // Covariance matrix according to HESSE status
808 // If problem then keep only the diagonal terms (variances)
809 Double_t matrix[5][5];
810 gMinuit->mnemat(&matrix[0][0],5);
811 if (covStatus == 3) trackParam->SetCovariances(matrix);
812 else trackParam->SetVariances(matrix);
813 } else trackParam->DeleteCovariances();
817 //__________________________________________________________________________
818 void TrackChi2(Int_t & /*nParam*/, Double_t * /*gradient*/, Double_t &chi2, Double_t *param, Int_t /*flag*/)
820 /// Return the "Chi2" to be minimized with Minuit for track fitting.
821 /// Assumes that the track hits are sorted according to increasing Z.
822 /// Track parameters at each TrackHit are updated accordingly.
823 /// Vertex is used according to the flag "trackBeingFitted->GetFitWithVertex()".
824 /// Multiple Coulomb scattering is taken into account according to the flag "trackBeingFitted->GetFitWithMCS()".
826 AliMUONTrack *trackBeingFitted = (AliMUONTrack*) gMinuit->GetObjectFit();
827 AliMUONTrackParam* trackParamAtHit = (AliMUONTrackParam*) trackBeingFitted->GetTrackParamAtHit()->First();
829 chi2 = 0.; // initialize chi2
831 // update track parameters
832 trackParamAtHit->SetNonBendingCoor(param[0]);
833 trackParamAtHit->SetNonBendingSlope(param[1]);
834 trackParamAtHit->SetBendingCoor(param[2]);
835 trackParamAtHit->SetBendingSlope(param[3]);
836 trackParamAtHit->SetInverseBendingMomentum(param[4]);
837 trackBeingFitted->UpdateTrackParamAtHit();
839 // Take the vertex into account in the fit if required
840 if (trackBeingFitted->GetFitWithVertex()) {
841 AliMUONTrackParam paramAtVertex(*trackParamAtHit);
842 AliMUONTrackExtrap::ExtrapToZ(¶mAtVertex, 0.);
843 AliMUONHitForRec *vertex = trackBeingFitted->GetVertex();
845 cout<<"Error in TrackChi2: Want to use the vertex in tracking but it has not been created!!"<<endl;
848 dX = vertex->GetNonBendingCoor() - paramAtVertex.GetNonBendingCoor();
849 dY = vertex->GetBendingCoor() - paramAtVertex.GetBendingCoor();
850 chi2 += dX * dX / vertex->GetNonBendingReso2() + dY * dY / vertex->GetBendingReso2();
853 // compute chi2 w/wo multiple scattering
854 if (trackBeingFitted->GetFitWithMCS()) chi2 += trackBeingFitted->ComputeGlobalChi2(kTRUE);
855 else chi2 += trackBeingFitted->ComputeGlobalChi2(kFALSE);
859 //__________________________________________________________________________
860 void AliMUONTrackReconstructor::ImproveTracks()
862 /// Improve tracks by removing clusters with local chi2 highter than the defined cut
863 /// Recompute track parameters and covariances at the remaining clusters
864 AliDebug(1,"Enter ImproveTracks");
866 Double_t localChi2, worstLocalChi2;
868 AliMUONTrackParam *trackParamAtHit, *worstTrackParamAtHit;
870 // Remove double track to improve only "good" tracks
871 RemoveDoubleTracks();
873 AliMUONTrack *track = (AliMUONTrack*) fRecTracksPtr->First();
876 while (!track->IsImproved()) {
878 // Update track parameters and covariances
879 track->UpdateCovTrackParamAtHit();
881 // Compute local chi2 of each hits
882 track->ComputeLocalChi2(kTRUE);
884 // Look for the hit to remove
885 worstTrackParamAtHit = 0;
887 trackParamAtHit = (AliMUONTrackParam*) track->GetTrackParamAtHit()->First();
888 while (trackParamAtHit) {
890 // Pick up hit with the worst chi2
891 localChi2 = trackParamAtHit->GetLocalChi2();
892 if (localChi2 > worstLocalChi2) {
893 worstLocalChi2 = localChi2;
894 worstTrackParamAtHit = trackParamAtHit;
897 trackParamAtHit = (AliMUONTrackParam*) track->GetTrackParamAtHit()->After(trackParamAtHit);
900 // Check if bad hit found
901 if (!worstTrackParamAtHit) {
902 track->SetImproved(kTRUE);
906 // check whether the worst hit is removable or not
907 if (!worstTrackParamAtHit->IsRemovable()) {
908 track->SetImproved(kTRUE);
912 // Check whether the worst chi2 is under requirement or not
913 if (worstLocalChi2 < 2. * fgkSigmaToCutForImprovement * fgkSigmaToCutForImprovement) { // 2 because 2 quantities in chi2
914 track->SetImproved(kTRUE);
918 // Reset the second hit in the same station as the bad one as being NOT removable
919 worstChamber = worstTrackParamAtHit->GetHitForRecPtr()->GetChamberNumber();
920 if (worstChamber%2 == 0) ((AliMUONTrackParam*)track->GetTrackParamAtHit()->After(worstTrackParamAtHit))->SetRemovable(kFALSE);
921 else ((AliMUONTrackParam*)track->GetTrackParamAtHit()->Before(worstTrackParamAtHit))->SetRemovable(kFALSE);
923 // Remove the worst hit
924 track->RemoveTrackParamAtHit(worstTrackParamAtHit);
927 // Take into account the multiple scattering
928 // Calculate the track parameter covariance matrix
929 track->SetFitWithVertex(kFALSE); // To be sure
930 Fit(*track, kTRUE, kTRUE);
932 // Printout for debuging
933 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
934 cout << "ImproveTracks: track " << fRecTracksPtr->IndexOf(track)+1 << " improved " << endl;
939 track = (AliMUONTrack*) fRecTracksPtr->After(track);
944 //__________________________________________________________________________
945 void AliMUONTrackReconstructor::Finalize()
947 /// Fill AliMUONTrack's fHitForRecAtHit array
948 /// Recompute track parameters and covariances at each attached cluster from those at the first one
950 AliMUONTrackParam *trackParamAtHit;
952 track = (AliMUONTrack*) fRecTracksPtr->First();
955 // update track parameters if not already done
956 if (!track->IsImproved()) track->UpdateCovTrackParamAtHit();
958 trackParamAtHit = (AliMUONTrackParam*) (track->GetTrackParamAtHit()->First());
959 while (trackParamAtHit) {
961 // update array of track hit
962 track->AddHitForRecAtHit(trackParamAtHit->GetHitForRecPtr());
964 trackParamAtHit = (AliMUONTrackParam*) (track->GetTrackParamAtHit()->After(trackParamAtHit));
967 track = (AliMUONTrack*) fRecTracksPtr->After(track);