bug fixed
[u/mrichter/AliRoot.git] / MUON / AliMUONTrackReconstructor.cxx
CommitLineData
a9e2aefa 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
cc87ebcd 16/* $Id$ */
17
3d1463c8 18//-----------------------------------------------------------------------------
7945aae7 19/// \class AliMUONTrackReconstructor
20/// MUON track reconstructor using the original method
21///
22/// This class contains as data:
23/// - the parameters for the track reconstruction
24///
25/// It contains as methods, among others:
26/// - MakeTracks to build the tracks
3d1463c8 27//-----------------------------------------------------------------------------
a9e2aefa 28
29f1b13a 29#include "AliMUONTrackReconstructor.h"
7ec3b9cf 30
29fc2c86 31#include "AliMUONConstants.h"
96ebe67e 32#include "AliMUONVCluster.h"
1fef78c8 33#include "AliMUONVClusterServer.h"
96ebe67e 34#include "AliMUONVClusterStore.h"
a9e2aefa 35#include "AliMUONTrack.h"
37827b29 36#include "AliMUONTrackParam.h"
37#include "AliMUONTrackExtrap.h"
5a240757 38#include "AliMUONRecoParam.h"
b709ac13 39
1fef78c8 40#include "AliMpArea.h"
41
8c343c7c 42#include "AliLog.h"
af743f54 43
208f139e 44#include <TMinuit.h>
af743f54 45#include <Riostream.h>
ea94c18b 46#include <TMath.h>
af743f54 47#include <TMatrixD.h>
a9e2aefa 48
de2cd600 49// Functions to be minimized with Minuit
ea94c18b 50void TrackChi2(Int_t &nParam, Double_t *gradient, Double_t &chi2, Double_t *param, Int_t flag);
de2cd600 51
7945aae7 52/// \cond CLASSIMP
29f1b13a 53ClassImp(AliMUONTrackReconstructor) // Class implementation in ROOT context
7945aae7 54/// \endcond
a9e2aefa 55
019df241 56 //__________________________________________________________________________
a0dc65b4 57AliMUONTrackReconstructor::AliMUONTrackReconstructor(const AliMUONRecoParam* recoParam, AliMUONVClusterServer* clusterServer)
58 : AliMUONVTrackReconstructor(recoParam,clusterServer)
a9e2aefa 59{
96ebe67e 60 /// Constructor
a9e2aefa 61}
9cbdf048 62
019df241 63 //__________________________________________________________________________
ea94c18b 64AliMUONTrackReconstructor::~AliMUONTrackReconstructor()
a9e2aefa 65{
ea94c18b 66/// Destructor
67}
276c44b7 68
69 //__________________________________________________________________________
89c8d66d 70Bool_t AliMUONTrackReconstructor::MakeTrackCandidates(AliMUONVClusterStore& clusterStore)
de2cd600 71{
019df241 72 /// To make track candidates (assuming linear propagation if the flag fgkMakeTrackCandidatesFast is set to kTRUE):
208f139e 73 /// Start with segments station(1..) 4 or 5 then follow track in station 5 or 4.
96ebe67e 74 /// Good candidates are made of at least three clusters.
208f139e 75 /// Keep only best candidates or all of them according to the flag fgkTrackAllTracks.
b709ac13 76
208f139e 77 TClonesArray *segments;
208f139e 78 AliMUONTrack *track;
208f139e 79 Int_t iCandidate = 0;
96ebe67e 80 Bool_t clusterFound;
208f139e 81
de2cd600 82 AliDebug(1,"Enter MakeTrackCandidates");
de2cd600 83
3a7acd03 84 // Unless we're doing combined tracking, we'll clusterize all stations at once
85 Int_t firstChamber(0);
86 Int_t lastChamber(9);
87
a0dc65b4 88 if (GetRecoParam()->CombineClusterTrackReco()) {
3a7acd03 89 // ... Here's the exception : ask the clustering to reconstruct
90 // clusters *only* in station 4 and 5 for combined tracking
91 firstChamber = 6;
92 }
93
94 for (Int_t i = firstChamber; i <= lastChamber; ++i )
95 {
35be7ed7 96 if (fClusterServer && GetRecoParam()->UseChamber(i)) fClusterServer->Clusterize(i, clusterStore, AliMpArea(), GetRecoParam());
1fef78c8 97 }
98
208f139e 99 // Loop over stations(1..) 5 and 4 and make track candidates
ea94c18b 100 for (Int_t istat=4; istat>=3; istat--) {
101
208f139e 102 // Make segments in the station
9bf6860b 103 segments = MakeSegmentsBetweenChambers(clusterStore, 2*istat, 2*istat+1);
ea94c18b 104
208f139e 105 // Loop over segments
7ec3b9cf 106 for (Int_t iseg=0; iseg<segments->GetEntriesFast(); iseg++)
107 {
208f139e 108 AliDebug(1,Form("Making primary candidate(1..) %d",++iCandidate));
ea94c18b 109
208f139e 110 // Transform segments to tracks and put them at the end of fRecTracksPtr
a0dc65b4 111 track = new ((*fRecTracksPtr)[fRecTracksPtr->GetLast()+1]) AliMUONTrack((AliMUONObjectPair*)((*segments)[iseg]),GetRecoParam()->GetBendingVertexDispersion());
a9e2aefa 112 fNRecTracks++;
ea94c18b 113
96ebe67e 114 // Look for compatible cluster(s) in the other station
a0dc65b4 115 if (GetRecoParam()->MakeTrackCandidatesFast())
96ebe67e 116 clusterFound = FollowLinearTrackInStation(*track, clusterStore, 7-istat);
117 else clusterFound = FollowTrackInStation(*track, clusterStore, 7-istat);
019df241 118
89c8d66d 119 // Remove track if no cluster found on a requested station
120 // or abort tracking if there are too many candidates
6b191dea 121 if (GetRecoParam()->RequestStation(7-istat)) {
122 if (!clusterFound) {
123 fRecTracksPtr->Remove(track);
124 fNRecTracks--;
125 } else if (fNRecTracks > GetRecoParam()->GetMaxTrackCandidates()) {
4ea3f013 126 AliError(Form("Too many track candidates (%d tracks). Stop tracking.", fNRecTracks));
6b191dea 127 delete segments;
128 return kFALSE;
129 }
130 } else {
131 if ((fNRecTracks + segments->GetEntriesFast() - iseg - 1) > GetRecoParam()->GetMaxTrackCandidates()) {
4ea3f013 132 AliError(Form("Too many track candidates (%d tracks). Stop tracking.", fNRecTracks + segments->GetEntriesFast() - iseg - 1));
6b191dea 133 delete segments;
134 return kFALSE;
135 }
ea94c18b 136 }
137
a9e2aefa 138 }
ea94c18b 139
208f139e 140 // delete the array of segments
141 delete segments;
142 }
ea94c18b 143
208f139e 144 // Keep all different tracks or only the best ones as required
a0dc65b4 145 if (GetRecoParam()->TrackAllTracks()) RemoveIdenticalTracks();
208f139e 146 else RemoveDoubleTracks();
147
148 AliDebug(1,Form("Number of good candidates = %d",fNRecTracks));
149
89c8d66d 150 return kTRUE;
151
a9e2aefa 152}
153
154 //__________________________________________________________________________
89c8d66d 155Bool_t AliMUONTrackReconstructor::MakeMoreTrackCandidates(AliMUONVClusterStore& clusterStore)
9bf6860b 156{
157 /// To make extra track candidates (assuming linear propagation if the flag fgkMakeTrackCandidatesFast is set to kTRUE):
158 /// clustering is supposed to be already done
159 /// Start with segments made of 1 cluster in each of the stations 4 and 5 then follow track in remaining chambers.
160 /// Good candidates are made of at least three clusters if both station are requested (two otherwise).
161 /// Keep only best candidates or all of them according to the flag fgkTrackAllTracks.
162
163 TClonesArray *segments;
89c8d66d 164 AliMUONObjectPair *segment;
9bf6860b 165 AliMUONTrack *track;
166 Int_t iCandidate = 0, iCurrentTrack, nCurrentTracks;
167 Bool_t clusterFound;
168
169 AliDebug(1,"Enter MakeMoreTrackCandidates");
170
171 // Double loop over chambers in stations(1..) 4 and 5 to make track candidates
172 for (Int_t ich1 = 6; ich1 <= 7; ich1++) {
173 for (Int_t ich2 = 8; ich2 <= 9; ich2++) {
174
175 // Make segments in the station
176 segments = MakeSegmentsBetweenChambers(clusterStore, ich1, ich2);
177
89c8d66d 178 /// Remove segments already attached to a track
179 RemoveUsedSegments(*segments);
180
9bf6860b 181 // Loop over segments
89c8d66d 182 for (Int_t iSegment=0; iSegment<segments->GetEntriesFast(); iSegment++)
9bf6860b 183 {
184 AliDebug(1,Form("Making primary candidate(1..) %d",++iCandidate));
89c8d66d 185 segment = (AliMUONObjectPair*) segments->UncheckedAt(iSegment);
9bf6860b 186
187 // Transform segments to tracks and put them at the end of fRecTracksPtr
188 iCurrentTrack = fRecTracksPtr->GetLast()+1;
89c8d66d 189 track = new ((*fRecTracksPtr)[iCurrentTrack]) AliMUONTrack(segment,GetRecoParam()->GetBendingVertexDispersion());
9bf6860b 190 fNRecTracks++;
191
192 // Look for compatible cluster(s) in the second chamber of station 5
89c8d66d 193 clusterFound = FollowLinearTrackInChamber(*track, clusterStore, 17-ich2);
9bf6860b 194
195 // skip the original track in case it has been removed
a0dc65b4 196 if (GetRecoParam()->TrackAllTracks() && clusterFound) iCurrentTrack++;
9bf6860b 197
198 // loop over every new tracks
199 nCurrentTracks = fRecTracksPtr->GetLast()+1;
200 while (iCurrentTrack < nCurrentTracks) {
201 track = (AliMUONTrack*) fRecTracksPtr->UncheckedAt(iCurrentTrack);
202
203 // Look for compatible cluster(s) in the second chamber of station 4
89c8d66d 204 FollowLinearTrackInChamber(*track, clusterStore, 13-ich1);
9bf6860b 205
206 iCurrentTrack++;
207 }
208
89c8d66d 209 // abort tracking if there are too many candidates
6b191dea 210 if ((fNRecTracks + segments->GetEntriesFast() - iSegment - 1) > GetRecoParam()->GetMaxTrackCandidates()) {
4ea3f013 211 AliError(Form("Too many track candidates (%d tracks). Stop tracking.", fNRecTracks + segments->GetEntriesFast() - iSegment - 1));
89c8d66d 212 delete segments;
213 return kFALSE;
214 }
215
9bf6860b 216 }
217
218 // delete the array of segments
219 delete segments;
220 }
221 }
222
89c8d66d 223 // Keep only the best tracks if required
224 if (!GetRecoParam()->TrackAllTracks()) RemoveDoubleTracks();
225 else fRecTracksPtr->Compress();
9bf6860b 226
227 AliDebug(1,Form("Number of good candidates = %d",fNRecTracks));
228
89c8d66d 229 return kTRUE;
230
9bf6860b 231}
232
233 //__________________________________________________________________________
89c8d66d 234Bool_t AliMUONTrackReconstructor::FollowTracks(AliMUONVClusterStore& clusterStore)
a9e2aefa 235{
2457f726 236 /// Follow tracks in stations(1..) 3, 2 and 1
208f139e 237 AliDebug(1,"Enter FollowTracks");
238
04b5ea16 239 AliMUONTrack *track, *nextTrack;
b709ac13 240 AliMUONTrackParam *trackParam, *nextTrackParam;
af743f54 241 Int_t currentNRecTracks;
208f139e 242
a0dc65b4 243 Double_t sigmaCut2 = GetRecoParam()->GetSigmaCutForTracking() *
244 GetRecoParam()->GetSigmaCutForTracking();
3304fa09 245
208f139e 246 for (Int_t station = 2; station >= 0; station--) {
ea94c18b 247
208f139e 248 // Save the actual number of reconstructed track in case of
249 // tracks are added or suppressed during the tracking procedure
250 // !! Do not compress fRecTracksPtr until the end of the loop over tracks !!
af743f54 251 currentNRecTracks = fNRecTracks;
ea94c18b 252
af743f54 253 for (Int_t iRecTrack = 0; iRecTrack <currentNRecTracks; iRecTrack++) {
208f139e 254 AliDebug(1,Form("FollowTracks: track candidate(1..) %d", iRecTrack+1));
ea94c18b 255
208f139e 256 track = (AliMUONTrack*) fRecTracksPtr->UncheckedAt(iRecTrack);
ea94c18b 257
208f139e 258 // Fit the track:
259 // Do not take into account the multiple scattering to speed up the fit
260 // Calculate the track parameter covariance matrix
9bf6860b 261 // If there is no cluster out of station 4 or 5 then use the vertex to better constrain the fit
262 if (((AliMUONTrackParam*) track->GetTrackParamAtCluster()->First())->GetClusterPtr()->GetChamberId() > 5)
263 Fit(*track, kFALSE, kTRUE, kTRUE);
96ebe67e 264 else Fit(*track, kFALSE, kFALSE, kTRUE);
ea94c18b 265
6b191dea 266 // remove tracks out of limits
267 if (!IsAcceptable(*((AliMUONTrackParam*)track->GetTrackParamAtCluster()->First()))) {
268 fRecTracksPtr->Remove(track);
269 fNRecTracks--;
270 continue;
89c8d66d 271 }
272
273 // remove track if the normalized chi2 is too high
274 if (track->GetNormalizedChi2() > sigmaCut2) {
9bf6860b 275 fRecTracksPtr->Remove(track);
89c8d66d 276 fNRecTracks--;
277 continue;
208f139e 278 }
ea94c18b 279
b709ac13 280 // save parameters from fit into smoothed parameters to complete track afterward
a0dc65b4 281 if (GetRecoParam()->ComplementTracks()) {
b709ac13 282
283 if (station==2) { // save track parameters on stations 4 and 5
284
285 // extrapolate track parameters and covariances at each cluster
4ea3f013 286 // remove the track in case of failure
287 if (!track->UpdateCovTrackParamAtCluster()) {
288 fRecTracksPtr->Remove(track);
289 fNRecTracks--;
290 continue;
291 }
b709ac13 292
293 // save them
96ebe67e 294 trackParam = (AliMUONTrackParam*) track->GetTrackParamAtCluster()->First();
b709ac13 295 while (trackParam) {
296 trackParam->SetSmoothParameters(trackParam->GetParameters());
297 trackParam->SetSmoothCovariances(trackParam->GetCovariances());
96ebe67e 298 trackParam = (AliMUONTrackParam*) track->GetTrackParamAtCluster()->After(trackParam);
b709ac13 299 }
300
301 } else { // or save track parameters on last station only
302
96ebe67e 303 trackParam = (AliMUONTrackParam*) track->GetTrackParamAtCluster()->First();
9bf6860b 304 if (trackParam->GetClusterPtr()->GetChamberId() < 2*(station+2)) {
b709ac13 305
9bf6860b 306 // save parameters from fit
307 trackParam->SetSmoothParameters(trackParam->GetParameters());
308 trackParam->SetSmoothCovariances(trackParam->GetCovariances());
b709ac13 309
9bf6860b 310 // save parameters extrapolated to the second chamber of the same station if it has been hit
311 nextTrackParam = (AliMUONTrackParam*) track->GetTrackParamAtCluster()->After(trackParam);
312 if (nextTrackParam->GetClusterPtr()->GetChamberId() < 2*(station+2)) {
313
314 // reset parameters and covariances
315 nextTrackParam->SetParameters(trackParam->GetParameters());
316 nextTrackParam->SetZ(trackParam->GetZ());
317 nextTrackParam->SetCovariances(trackParam->GetCovariances());
318
319 // extrapolate them to the z of the corresponding cluster
4ea3f013 320 // remove the track in case of failure
321 if (!AliMUONTrackExtrap::ExtrapToZCov(nextTrackParam, nextTrackParam->GetClusterPtr()->GetZ())) {
322 fRecTracksPtr->Remove(track);
323 fNRecTracks--;
324 continue;
325 }
9bf6860b 326
327 // save them
328 nextTrackParam->SetSmoothParameters(nextTrackParam->GetParameters());
329 nextTrackParam->SetSmoothCovariances(nextTrackParam->GetCovariances());
330
331 }
b709ac13 332
333 }
334
335 }
336
337 }
338
96ebe67e 339 // Look for compatible cluster(s) in station(0..) "station"
9bf6860b 340 if (!FollowTrackInStation(*track, clusterStore, station)) {
341
342 // Try to recover track if required
a0dc65b4 343 if (GetRecoParam()->RecoverTracks()) {
9bf6860b 344
345 // work on a copy of the track if this station is not required
346 // to keep the case where no cluster is reconstructed as a possible candidate
a0dc65b4 347 if (!GetRecoParam()->RequestStation(station)) {
9bf6860b 348 track = new ((*fRecTracksPtr)[fRecTracksPtr->GetLast()+1]) AliMUONTrack(*track);
349 fNRecTracks++;
350 }
351
352 // try to recover
353 if (!RecoverTrack(*track, clusterStore, station)) {
354 // remove track if no cluster found
355 fRecTracksPtr->Remove(track);
356 fNRecTracks--;
357 }
358
a0dc65b4 359 } else if (GetRecoParam()->RequestStation(station)) {
9bf6860b 360 // remove track if no cluster found
361 fRecTracksPtr->Remove(track);
362 fNRecTracks--;
363 }
364
ea94c18b 365 }
366
89c8d66d 367 // abort tracking if there are too many candidates
368 if (fNRecTracks > GetRecoParam()->GetMaxTrackCandidates()) {
4ea3f013 369 AliError(Form("Too many track candidates (%d tracks). Stop tracking.", fNRecTracks));
89c8d66d 370 return kFALSE;
371 }
372
208f139e 373 }
ea94c18b 374
208f139e 375 // Compress fRecTracksPtr for the next step
376 fRecTracksPtr->Compress();
ea94c18b 377
208f139e 378 // Keep only the best tracks if required
a0dc65b4 379 if (!GetRecoParam()->TrackAllTracks()) RemoveDoubleTracks();
ea94c18b 380
208f139e 381 }
382
96ebe67e 383 // Last fit of track candidates with all stations
208f139e 384 // Take into account the multiple scattering and remove bad tracks
385 Int_t trackIndex = -1;
04b5ea16 386 track = (AliMUONTrack*) fRecTracksPtr->First();
04b5ea16 387 while (track) {
ea94c18b 388
04b5ea16 389 trackIndex++;
390 nextTrack = (AliMUONTrack*) fRecTracksPtr->After(track); // prepare next track
ea94c18b 391
96ebe67e 392 Fit(*track, kTRUE, kFALSE, kTRUE);
ea94c18b 393
208f139e 394 // Printout for debuging
395 if (AliLog::GetGlobalDebugLevel() >= 3) {
396 cout << "FollowTracks: track candidate(0..) " << trackIndex << " after final fit" << endl;
a9e2aefa 397 track->RecursiveDump();
208f139e 398 }
ea94c18b 399
208f139e 400 // Remove the track if the normalized chi2 is too high
3304fa09 401 if (track->GetNormalizedChi2() > sigmaCut2) {
208f139e 402 fRecTracksPtr->Remove(track);
403 fNRecTracks--;
6d9a2d45 404 track = nextTrack;
405 continue;
a9e2aefa 406 }
ea94c18b 407
b709ac13 408 // save parameters from fit into smoothed parameters to complete track afterward
a0dc65b4 409 if (GetRecoParam()->ComplementTracks()) {
b709ac13 410
96ebe67e 411 trackParam = (AliMUONTrackParam*) track->GetTrackParamAtCluster()->First();
9bf6860b 412 if (trackParam->GetClusterPtr()->GetChamberId() < 2) {
b709ac13 413
9bf6860b 414 // save parameters from fit
415 trackParam->SetSmoothParameters(trackParam->GetParameters());
416 trackParam->SetSmoothCovariances(trackParam->GetCovariances());
b709ac13 417
9bf6860b 418 // save parameters extrapolated to the second chamber of the same station if it has been hit
419 nextTrackParam = (AliMUONTrackParam*) track->GetTrackParamAtCluster()->After(trackParam);
420 if (nextTrackParam->GetClusterPtr()->GetChamberId() < 2) {
421
422 // reset parameters and covariances
423 nextTrackParam->SetParameters(trackParam->GetParameters());
424 nextTrackParam->SetZ(trackParam->GetZ());
425 nextTrackParam->SetCovariances(trackParam->GetCovariances());
426
427 // extrapolate them to the z of the corresponding cluster
4ea3f013 428 // remove the track in case of failure
429 if (!AliMUONTrackExtrap::ExtrapToZCov(nextTrackParam, nextTrackParam->GetClusterPtr()->GetZ())) {
430 fRecTracksPtr->Remove(track);
431 fNRecTracks--;
432 track = nextTrack;
433 continue;
434 }
9bf6860b 435
436 // save them
437 nextTrackParam->SetSmoothParameters(nextTrackParam->GetParameters());
438 nextTrackParam->SetSmoothCovariances(nextTrackParam->GetCovariances());
439
440 }
b709ac13 441
442 }
443
444 }
445
208f139e 446 track = nextTrack;
ea94c18b 447
208f139e 448 }
ea94c18b 449
208f139e 450 fRecTracksPtr->Compress();
451
89c8d66d 452 return kTRUE;
453
208f139e 454}
1a38e749 455
208f139e 456 //__________________________________________________________________________
9bf6860b 457Bool_t AliMUONTrackReconstructor::FollowTrackInChamber(AliMUONTrack &trackCandidate, AliMUONVClusterStore& clusterStore, Int_t nextChamber)
458{
459 /// Follow trackCandidate in chamber(0..) nextChamber and search for compatible cluster(s)
460 /// Keep all possibilities or only the best one(s) according to the flag fgkTrackAllTracks:
461 /// kTRUE: duplicate "trackCandidate" if there are several possibilities and add the new tracks at the end of
462 /// fRecTracksPtr to avoid conficts with other track candidates at this current stage of the tracking procedure.
463 /// Remove the obsolete "trackCandidate" at the end.
464 /// kFALSE: add only the best cluster(s) to the "trackCandidate". Try to add a couple of clusters in priority.
465 AliDebug(1,Form("Enter FollowTrackInChamber(1..) %d", nextChamber+1));
466
4ea3f013 467 Double_t chi2WithOneCluster = AliMUONTrack::MaxChi2();
a0dc65b4 468 Double_t maxChi2WithOneCluster = 2. * GetRecoParam()->GetSigmaCutForTracking() *
469 GetRecoParam()->GetSigmaCutForTracking(); // 2 because 2 quantities in chi2
9bf6860b 470 Double_t bestChi2WithOneCluster = maxChi2WithOneCluster;
471 Bool_t foundOneCluster = kFALSE;
472 AliMUONTrack *newTrack = 0x0;
473 AliMUONVCluster *cluster;
474 AliMUONTrackParam extrapTrackParam;
475 AliMUONTrackParam extrapTrackParamAtCh;
476 AliMUONTrackParam extrapTrackParamAtCluster;
477 AliMUONTrackParam bestTrackParamAtCluster;
478
479 // Get track parameters according to the propagation direction
480 if (nextChamber > 7) extrapTrackParamAtCh = *(AliMUONTrackParam*)trackCandidate.GetTrackParamAtCluster()->Last();
481 else extrapTrackParamAtCh = *(AliMUONTrackParam*)trackCandidate.GetTrackParamAtCluster()->First();
482
483 // Printout for debuging
484 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 2) || (AliLog::GetGlobalDebugLevel() >= 2)) {
485 cout<<endl<<"Track parameters and covariances at first cluster:"<<endl;
486 extrapTrackParamAtCh.GetParameters().Print();
487 extrapTrackParamAtCh.GetCovariances().Print();
488 }
489
490 // Add MCS effect
4663da9f 491 Int_t currentChamber = extrapTrackParamAtCh.GetClusterPtr()->GetChamberId();
492 AliMUONTrackExtrap::AddMCSEffect(&extrapTrackParamAtCh,AliMUONConstants::ChamberThicknessInX0(currentChamber),-1.);
9bf6860b 493
494 // Add MCS in the missing chamber(s) if any
9bf6860b 495 while (currentChamber > nextChamber + 1) {
496 // extrapolation to the missing chamber
497 currentChamber--;
4ea3f013 498 if (!AliMUONTrackExtrap::ExtrapToZCov(&extrapTrackParamAtCh, AliMUONConstants::DefaultChamberZ(currentChamber))) return kFALSE;
9bf6860b 499 // add MCS effect
4663da9f 500 AliMUONTrackExtrap::AddMCSEffect(&extrapTrackParamAtCh,AliMUONConstants::ChamberThicknessInX0(currentChamber),-1.);
9bf6860b 501 }
502
503 //Extrapolate trackCandidate to chamber
4ea3f013 504 if (!AliMUONTrackExtrap::ExtrapToZCov(&extrapTrackParamAtCh, AliMUONConstants::DefaultChamberZ(nextChamber))) return kFALSE;
9bf6860b 505
506 // Printout for debuging
507 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 2) || (AliLog::GetGlobalDebugLevel() >= 2)) {
508 cout<<endl<<"Track parameters and covariances at first cluster extrapolated to z = "<<AliMUONConstants::DefaultChamberZ(nextChamber)<<":"<<endl;
509 extrapTrackParamAtCh.GetParameters().Print();
510 extrapTrackParamAtCh.GetCovariances().Print();
511 }
512
513 // Printout for debuging
514 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
515 cout << "FollowTrackInStation: look for clusters in chamber(1..): " << nextChamber+1 << endl;
516 }
517
518 // Ask the clustering to reconstruct new clusters around the track position in the current chamber
519 // except for station 4 and 5 that are already entirely clusterized
a0dc65b4 520 if (GetRecoParam()->CombineClusterTrackReco()) {
9bf6860b 521 if (nextChamber < 6) AskForNewClustersInChamber(extrapTrackParamAtCh, clusterStore, nextChamber);
522 }
523
524 // Create iterators to loop over clusters in both chambers
525 TIter next(clusterStore.CreateChamberIterator(nextChamber,nextChamber));
526
527 // look for cluster in chamber
528 while ( ( cluster = static_cast<AliMUONVCluster*>(next()) ) ) {
529
530 // try to add the current cluster fast
531 if (!TryOneClusterFast(extrapTrackParamAtCh, cluster)) continue;
532
533 // try to add the current cluster accuratly
534 chi2WithOneCluster = TryOneCluster(extrapTrackParamAtCh, cluster, extrapTrackParamAtCluster);
535
536 // if good chi2 then consider to add cluster
537 if (chi2WithOneCluster < maxChi2WithOneCluster) {
538 foundOneCluster = kTRUE;
539
540 // Printout for debuging
541 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
542 cout << "FollowTrackInStation: found one cluster in chamber(1..): " << nextChamber+1
543 << " (Chi2 = " << chi2WithOneCluster << ")" << endl;
544 cluster->Print();
545 }
546
a0dc65b4 547 if (GetRecoParam()->TrackAllTracks()) {
9bf6860b 548 // copy trackCandidate into a new track put at the end of fRecTracksPtr and add the new cluster
549 newTrack = new ((*fRecTracksPtr)[fRecTracksPtr->GetLast()+1]) AliMUONTrack(trackCandidate);
550 UpdateTrack(*newTrack,extrapTrackParamAtCluster);
551 fNRecTracks++;
552
553 // Printout for debuging
554 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
555 cout << "FollowTrackInStation: added one cluster in chamber(1..): " << nextChamber+1 << endl;
556 if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
557 }
558
559 } else if (chi2WithOneCluster < bestChi2WithOneCluster) {
560 // keep track of the best single cluster except if a couple of clusters has already been found
561 bestChi2WithOneCluster = chi2WithOneCluster;
562 bestTrackParamAtCluster = extrapTrackParamAtCluster;
563 }
564
565 }
566
567 }
568
569 // fill out the best track if required else clean up the fRecTracksPtr array
a0dc65b4 570 if (!GetRecoParam()->TrackAllTracks()) {
9bf6860b 571 if (foundOneCluster) {
572 UpdateTrack(trackCandidate,bestTrackParamAtCluster);
573
574 // Printout for debuging
575 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
576 cout << "FollowTrackInStation: added the best cluster in chamber(1..): " << bestTrackParamAtCluster.GetClusterPtr()->GetChamberId()+1 << endl;
577 if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
578 }
579
580 } else return kFALSE;
581
582 } else if (foundOneCluster) {
583
584 // remove obsolete track
585 fRecTracksPtr->Remove(&trackCandidate);
586 fNRecTracks--;
587
588 } else return kFALSE;
589
590 return kTRUE;
591
592}
593
594 //__________________________________________________________________________
1fef78c8 595Bool_t AliMUONTrackReconstructor::FollowTrackInStation(AliMUONTrack &trackCandidate, AliMUONVClusterStore& clusterStore, Int_t nextStation)
208f139e 596{
96ebe67e 597 /// Follow trackCandidate in station(0..) nextStation and search for compatible cluster(s)
208f139e 598 /// Keep all possibilities or only the best one(s) according to the flag fgkTrackAllTracks:
599 /// kTRUE: duplicate "trackCandidate" if there are several possibilities and add the new tracks at the end of
600 /// fRecTracksPtr to avoid conficts with other track candidates at this current stage of the tracking procedure.
601 /// Remove the obsolete "trackCandidate" at the end.
96ebe67e 602 /// kFALSE: add only the best cluster(s) to the "trackCandidate". Try to add a couple of clusters in priority.
208f139e 603 AliDebug(1,Form("Enter FollowTrackInStation(1..) %d", nextStation+1));
604
ea94c18b 605 // Order the chamber according to the propagation direction (tracking starts with chamber 2):
606 // - nextStation == station(1...) 5 => forward propagation
607 // - nextStation < station(1...) 5 => backward propagation
608 Int_t ch1, ch2;
609 if (nextStation==4) {
610 ch1 = 2*nextStation+1;
611 ch2 = 2*nextStation;
612 } else {
613 ch1 = 2*nextStation;
614 ch2 = 2*nextStation+1;
615 }
616
4ea3f013 617 Double_t chi2WithOneCluster = AliMUONTrack::MaxChi2();
618 Double_t chi2WithTwoClusters = AliMUONTrack::MaxChi2();
a0dc65b4 619 Double_t maxChi2WithOneCluster = 2. * GetRecoParam()->GetSigmaCutForTracking() *
620 GetRecoParam()->GetSigmaCutForTracking(); // 2 because 2 quantities in chi2
621 Double_t maxChi2WithTwoClusters = 4. * GetRecoParam()->GetSigmaCutForTracking() *
622 GetRecoParam()->GetSigmaCutForTracking(); // 4 because 4 quantities in chi2
96ebe67e 623 Double_t bestChi2WithOneCluster = maxChi2WithOneCluster;
624 Double_t bestChi2WithTwoClusters = maxChi2WithTwoClusters;
625 Bool_t foundOneCluster = kFALSE;
626 Bool_t foundTwoClusters = kFALSE;
208f139e 627 AliMUONTrack *newTrack = 0x0;
96ebe67e 628 AliMUONVCluster *clusterCh1, *clusterCh2;
019df241 629 AliMUONTrackParam extrapTrackParam;
7332f213 630 AliMUONTrackParam extrapTrackParamAtCh;
96ebe67e 631 AliMUONTrackParam extrapTrackParamAtCluster1;
632 AliMUONTrackParam extrapTrackParamAtCluster2;
633 AliMUONTrackParam bestTrackParamAtCluster1;
634 AliMUONTrackParam bestTrackParamAtCluster2;
635
7332f213 636 // Get track parameters according to the propagation direction
637 if (nextStation==4) extrapTrackParamAtCh = *(AliMUONTrackParam*)trackCandidate.GetTrackParamAtCluster()->Last();
638 else extrapTrackParamAtCh = *(AliMUONTrackParam*)trackCandidate.GetTrackParamAtCluster()->First();
ea94c18b 639
9bf6860b 640 // Printout for debuging
641 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 2) || (AliLog::GetGlobalDebugLevel() >= 2)) {
642 cout<<endl<<"Track parameters and covariances at first cluster:"<<endl;
643 extrapTrackParamAtCh.GetParameters().Print();
644 extrapTrackParamAtCh.GetCovariances().Print();
645 }
646
ea94c18b 647 // Add MCS effect
4663da9f 648 Int_t currentChamber = extrapTrackParamAtCh.GetClusterPtr()->GetChamberId();
649 AliMUONTrackExtrap::AddMCSEffect(&extrapTrackParamAtCh,AliMUONConstants::ChamberThicknessInX0(currentChamber),-1.);
ea94c18b 650
9bf6860b 651 // Add MCS in the missing chamber(s) if any
9bf6860b 652 while (ch1 < ch2 && currentChamber > ch2 + 1) {
ea94c18b 653 // extrapolation to the missing chamber
9bf6860b 654 currentChamber--;
4ea3f013 655 if (!AliMUONTrackExtrap::ExtrapToZCov(&extrapTrackParamAtCh, AliMUONConstants::DefaultChamberZ(currentChamber))) return kFALSE;
ea94c18b 656 // add MCS effect
4663da9f 657 AliMUONTrackExtrap::AddMCSEffect(&extrapTrackParamAtCh,AliMUONConstants::ChamberThicknessInX0(currentChamber),-1.);
ea94c18b 658 }
659
660 //Extrapolate trackCandidate to chamber "ch2"
4ea3f013 661 if (!AliMUONTrackExtrap::ExtrapToZCov(&extrapTrackParamAtCh, AliMUONConstants::DefaultChamberZ(ch2))) return kFALSE;
ea94c18b 662
208f139e 663 // Printout for debuging
664 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 2) || (AliLog::GetGlobalDebugLevel() >= 2)) {
9bf6860b 665 cout<<endl<<"Track parameters and covariances at first cluster extrapolated to z = "<<AliMUONConstants::DefaultChamberZ(ch2)<<":"<<endl;
666 extrapTrackParamAtCh.GetParameters().Print();
019df241 667 extrapTrackParamAtCh.GetCovariances().Print();
208f139e 668 }
ea94c18b 669
208f139e 670 // Printout for debuging
671 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
96ebe67e 672 cout << "FollowTrackInStation: look for clusters in chamber(1..): " << ch2+1 << endl;
208f139e 673 }
ea94c18b 674
1fef78c8 675 // Ask the clustering to reconstruct new clusters around the track position in the current station
676 // except for station 4 and 5 that are already entirely clusterized
a0dc65b4 677 if (GetRecoParam()->CombineClusterTrackReco()) {
1fef78c8 678 if (nextStation < 3) AskForNewClustersInStation(extrapTrackParamAtCh, clusterStore, nextStation);
679 }
680
4ea3f013 681 Int_t nClusters = clusterStore.GetSize();
682 Bool_t *clusterCh1Used = new Bool_t[nClusters];
683 for (Int_t i = 0; i < nClusters; i++) clusterCh1Used[i] = kFALSE;
684 Int_t iCluster1;
685
96ebe67e 686 // Create iterators to loop over clusters in both chambers
687 TIter nextInCh1(clusterStore.CreateChamberIterator(ch1,ch1));
688 TIter nextInCh2(clusterStore.CreateChamberIterator(ch2,ch2));
689
690 // look for candidates in chamber 2
691 while ( ( clusterCh2 = static_cast<AliMUONVCluster*>(nextInCh2()) ) ) {
ea94c18b 692
96ebe67e 693 // try to add the current cluster fast
694 if (!TryOneClusterFast(extrapTrackParamAtCh, clusterCh2)) continue;
019df241 695
96ebe67e 696 // try to add the current cluster accuratly
697 chi2WithOneCluster = TryOneCluster(extrapTrackParamAtCh, clusterCh2, extrapTrackParamAtCluster2);
ea94c18b 698
96ebe67e 699 // if good chi2 then try to attach a cluster in the other chamber too
700 if (chi2WithOneCluster < maxChi2WithOneCluster) {
701 Bool_t foundSecondCluster = kFALSE;
ea94c18b 702
208f139e 703 // Printout for debuging
704 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
96ebe67e 705 cout << "FollowTrackInStation: found one cluster in chamber(1..): " << ch2+1
706 << " (Chi2 = " << chi2WithOneCluster << ")" << endl;
9bf6860b 707 clusterCh2->Print();
96ebe67e 708 cout << " look for second clusters in chamber(1..): " << ch1+1 << " ..." << endl;
a9e2aefa 709 }
ea94c18b 710
711 // add MCS effect for next step
4663da9f 712 AliMUONTrackExtrap::AddMCSEffect(&extrapTrackParamAtCluster2,AliMUONConstants::ChamberThicknessInX0(ch2),-1.);
ea94c18b 713
019df241 714 // copy new track parameters for next step
96ebe67e 715 extrapTrackParam = extrapTrackParamAtCluster2;
019df241 716
717 //Extrapolate track parameters to chamber "ch1"
4ea3f013 718 Bool_t normalExtrap = AliMUONTrackExtrap::ExtrapToZ(&extrapTrackParam, AliMUONConstants::DefaultChamberZ(ch1));
019df241 719
96ebe67e 720 // reset cluster iterator of chamber 1
721 nextInCh1.Reset();
722 iCluster1 = -1;
723
724 // look for second candidates in chamber 1
4ea3f013 725 if (normalExtrap) while ( ( clusterCh1 = static_cast<AliMUONVCluster*>(nextInCh1()) ) ) {
96ebe67e 726 iCluster1++;
ea94c18b 727
96ebe67e 728 // try to add the current cluster fast
729 if (!TryOneClusterFast(extrapTrackParam, clusterCh1)) continue;
019df241 730
4ea3f013 731 // try to add the current cluster accurately
96ebe67e 732 chi2WithTwoClusters = TryTwoClusters(extrapTrackParamAtCluster2, clusterCh1, extrapTrackParamAtCluster1);
ea94c18b 733
96ebe67e 734 // if good chi2 then create a new track by adding the 2 clusters to the "trackCandidate"
735 if (chi2WithTwoClusters < maxChi2WithTwoClusters) {
736 foundSecondCluster = kTRUE;
737 foundTwoClusters = kTRUE;
ea94c18b 738
739 // Printout for debuging
740 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
96ebe67e 741 cout << "FollowTrackInStation: found second cluster in chamber(1..): " << ch1+1
9bf6860b 742 << " (Global Chi2 = " << chi2WithTwoClusters << ")" << endl;
743 clusterCh1->Print();
ea94c18b 744 }
745
a0dc65b4 746 if (GetRecoParam()->TrackAllTracks()) {
96ebe67e 747 // copy trackCandidate into a new track put at the end of fRecTracksPtr and add the new clusters
ea94c18b 748 newTrack = new ((*fRecTracksPtr)[fRecTracksPtr->GetLast()+1]) AliMUONTrack(trackCandidate);
96ebe67e 749 UpdateTrack(*newTrack,extrapTrackParamAtCluster1,extrapTrackParamAtCluster2);
208f139e 750 fNRecTracks++;
ea94c18b 751
96ebe67e 752 // Tag clusterCh1 as used
753 clusterCh1Used[iCluster1] = kTRUE;
ea94c18b 754
208f139e 755 // Printout for debuging
756 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
96ebe67e 757 cout << "FollowTrackInStation: added two clusters in station(1..): " << nextStation+1 << endl;
208f139e 758 if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
a9e2aefa 759 }
ea94c18b 760
96ebe67e 761 } else if (chi2WithTwoClusters < bestChi2WithTwoClusters) {
762 // keep track of the best couple of clusters
763 bestChi2WithTwoClusters = chi2WithTwoClusters;
764 bestTrackParamAtCluster1 = extrapTrackParamAtCluster1;
765 bestTrackParamAtCluster2 = extrapTrackParamAtCluster2;
208f139e 766 }
ea94c18b 767
a9e2aefa 768 }
ea94c18b 769
d0bfce8d 770 }
ea94c18b 771
96ebe67e 772 // if no clusterCh1 found then consider to add clusterCh2 only
773 if (!foundSecondCluster) {
774 foundOneCluster = kTRUE;
ea94c18b 775
a0dc65b4 776 if (GetRecoParam()->TrackAllTracks()) {
96ebe67e 777 // copy trackCandidate into a new track put at the end of fRecTracksPtr and add the new cluster
ea94c18b 778 newTrack = new ((*fRecTracksPtr)[fRecTracksPtr->GetLast()+1]) AliMUONTrack(trackCandidate);
96ebe67e 779 UpdateTrack(*newTrack,extrapTrackParamAtCluster2);
208f139e 780 fNRecTracks++;
ea94c18b 781
208f139e 782 // Printout for debuging
783 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
96ebe67e 784 cout << "FollowTrackInStation: added one cluster in chamber(1..): " << ch2+1 << endl;
208f139e 785 if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
786 }
ea94c18b 787
96ebe67e 788 } else if (!foundTwoClusters && chi2WithOneCluster < bestChi2WithOneCluster) {
789 // keep track of the best single cluster except if a couple of clusters has already been found
790 bestChi2WithOneCluster = chi2WithOneCluster;
791 bestTrackParamAtCluster1 = extrapTrackParamAtCluster2;
208f139e 792 }
ea94c18b 793
d0bfce8d 794 }
ea94c18b 795
208f139e 796 }
ea94c18b 797
208f139e 798 }
ea94c18b 799
208f139e 800 // look for candidates in chamber 1 not already attached to a track
96ebe67e 801 // if we want to keep all possible tracks or if no good couple of clusters has been found
a0dc65b4 802 if (GetRecoParam()->TrackAllTracks() || !foundTwoClusters) {
ea94c18b 803
ea94c18b 804 // add MCS effect for next step
4663da9f 805 AliMUONTrackExtrap::AddMCSEffect(&extrapTrackParamAtCh,AliMUONConstants::ChamberThicknessInX0(ch2),-1.);
019df241 806
807 //Extrapolate trackCandidate to chamber "ch1"
4ea3f013 808 Bool_t normalExtrap = AliMUONTrackExtrap::ExtrapToZCov(&extrapTrackParamAtCh, AliMUONConstants::DefaultChamberZ(ch1));
019df241 809
9bf6860b 810 // Printout for debuging
811 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 2) || (AliLog::GetGlobalDebugLevel() >= 2)) {
812 cout<<endl<<"Track parameters and covariances at first cluster extrapolated to z = "<<AliMUONConstants::DefaultChamberZ(ch1)<<":"<<endl;
813 extrapTrackParamAtCh.GetParameters().Print();
814 extrapTrackParamAtCh.GetCovariances().Print();
815 }
816
817 // Printout for debuging
818 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
819 cout << "FollowTrackInStation: look for single clusters in chamber(1..): " << ch1+1 << endl;
820 }
821
96ebe67e 822 // reset cluster iterator of chamber 1
823 nextInCh1.Reset();
824 iCluster1 = -1;
825
826 // look for second candidates in chamber 1
4ea3f013 827 if (normalExtrap) while ( ( clusterCh1 = static_cast<AliMUONVCluster*>(nextInCh1()) ) ) {
96ebe67e 828 iCluster1++;
ea94c18b 829
96ebe67e 830 if (clusterCh1Used[iCluster1]) continue; // Skip cluster already used
ea94c18b 831
96ebe67e 832 // try to add the current cluster fast
833 if (!TryOneClusterFast(extrapTrackParamAtCh, clusterCh1)) continue;
019df241 834
96ebe67e 835 // try to add the current cluster accuratly
836 chi2WithOneCluster = TryOneCluster(extrapTrackParamAtCh, clusterCh1, extrapTrackParamAtCluster1);
9bf6860b 837
96ebe67e 838 // if good chi2 then consider to add clusterCh1
839 // We do not try to attach a cluster in the other chamber too since it has already been done above
840 if (chi2WithOneCluster < maxChi2WithOneCluster) {
841 foundOneCluster = kTRUE;
9bf6860b 842
ea94c18b 843 // Printout for debuging
844 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
96ebe67e 845 cout << "FollowTrackInStation: found one cluster in chamber(1..): " << ch1+1
846 << " (Chi2 = " << chi2WithOneCluster << ")" << endl;
9bf6860b 847 clusterCh1->Print();
ea94c18b 848 }
849
a0dc65b4 850 if (GetRecoParam()->TrackAllTracks()) {
96ebe67e 851 // copy trackCandidate into a new track put at the end of fRecTracksPtr and add the new cluster
ea94c18b 852 newTrack = new ((*fRecTracksPtr)[fRecTracksPtr->GetLast()+1]) AliMUONTrack(trackCandidate);
96ebe67e 853 UpdateTrack(*newTrack,extrapTrackParamAtCluster1);
208f139e 854 fNRecTracks++;
ea94c18b 855
856 // Printout for debuging
208f139e 857 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
96ebe67e 858 cout << "FollowTrackInStation: added one cluster in chamber(1..): " << ch1+1 << endl;
208f139e 859 if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
860 }
ea94c18b 861
96ebe67e 862 } else if (chi2WithOneCluster < bestChi2WithOneCluster) {
863 // keep track of the best single cluster except if a couple of clusters has already been found
864 bestChi2WithOneCluster = chi2WithOneCluster;
865 bestTrackParamAtCluster1 = extrapTrackParamAtCluster1;
208f139e 866 }
ea94c18b 867
d0bfce8d 868 }
ea94c18b 869
208f139e 870 }
ea94c18b 871
208f139e 872 }
ea94c18b 873
208f139e 874 // fill out the best track if required else clean up the fRecTracksPtr array
a0dc65b4 875 if (!GetRecoParam()->TrackAllTracks()) {
96ebe67e 876 if (foundTwoClusters) {
877 UpdateTrack(trackCandidate,bestTrackParamAtCluster1,bestTrackParamAtCluster2);
ea94c18b 878
208f139e 879 // Printout for debuging
880 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
96ebe67e 881 cout << "FollowTrackInStation: added the two best clusters in station(1..): " << nextStation+1 << endl;
208f139e 882 if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
a9e2aefa 883 }
ea94c18b 884
96ebe67e 885 } else if (foundOneCluster) {
886 UpdateTrack(trackCandidate,bestTrackParamAtCluster1);
ea94c18b 887
208f139e 888 // Printout for debuging
889 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
96ebe67e 890 cout << "FollowTrackInStation: added the best cluster in chamber(1..): " << bestTrackParamAtCluster1.GetClusterPtr()->GetChamberId()+1 << endl;
208f139e 891 if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
04b5ea16 892 }
ea94c18b 893
dd230855 894 } else {
96ebe67e 895 delete [] clusterCh1Used;
dd230855 896 return kFALSE;
897 }
ea94c18b 898
96ebe67e 899 } else if (foundOneCluster || foundTwoClusters) {
ea94c18b 900
901 // remove obsolete track
902 fRecTracksPtr->Remove(&trackCandidate);
903 fNRecTracks--;
904
dd230855 905 } else {
96ebe67e 906 delete [] clusterCh1Used;
dd230855 907 return kFALSE;
908 }
ea94c18b 909
96ebe67e 910 delete [] clusterCh1Used;
ea94c18b 911 return kTRUE;
912
913}
914
915 //__________________________________________________________________________
96ebe67e 916Double_t AliMUONTrackReconstructor::TryTwoClusters(const AliMUONTrackParam &trackParamAtCluster1, AliMUONVCluster* cluster2,
917 AliMUONTrackParam &trackParamAtCluster2)
ea94c18b 918{
96ebe67e 919/// Test the compatibility between the track and the 2 clusters together (using trackParam's covariance matrix):
920/// return the corresponding Chi2 accounting for covariances between the 2 clusters
921/// return trackParamAtCluster1 & 2
ea94c18b 922
96ebe67e 923 // extrapolate track parameters at the z position of the second cluster (no need to extrapolate the covariances)
4ea3f013 924 // and set pointer to cluster into trackParamAtCluster
96ebe67e 925 trackParamAtCluster2.SetParameters(trackParamAtCluster1.GetParameters());
926 trackParamAtCluster2.SetZ(trackParamAtCluster1.GetZ());
96ebe67e 927 trackParamAtCluster2.SetClusterPtr(cluster2);
4ea3f013 928 if (!AliMUONTrackExtrap::ExtrapToZ(&trackParamAtCluster2, cluster2->GetZ())) return 2.*AliMUONTrack::MaxChi2();
ea94c18b 929
96ebe67e 930 // Set differences between track and the 2 clusters in the bending and non bending directions
931 AliMUONVCluster* cluster1 = trackParamAtCluster1.GetClusterPtr();
ea94c18b 932 TMatrixD dPos(4,1);
96ebe67e 933 dPos(0,0) = cluster1->GetX() - trackParamAtCluster1.GetNonBendingCoor();
934 dPos(1,0) = cluster1->GetY() - trackParamAtCluster1.GetBendingCoor();
935 dPos(2,0) = cluster2->GetX() - trackParamAtCluster2.GetNonBendingCoor();
936 dPos(3,0) = cluster2->GetY() - trackParamAtCluster2.GetBendingCoor();
ea94c18b 937
96ebe67e 938 // Calculate the error matrix from the track parameter covariances at first cluster
ea94c18b 939 TMatrixD error(4,4);
940 error.Zero();
96ebe67e 941 if (trackParamAtCluster1.CovariancesExist()) {
942 // Save track parameters at first cluster
943 TMatrixD paramAtCluster1Save(trackParamAtCluster1.GetParameters());
944
945 // Save track coordinates at second cluster
946 Double_t nonBendingCoor2 = trackParamAtCluster2.GetNonBendingCoor();
947 Double_t bendingCoor2 = trackParamAtCluster2.GetBendingCoor();
ea94c18b 948
96ebe67e 949 // copy track parameters at first cluster for jacobian calculation
950 AliMUONTrackParam trackParam(trackParamAtCluster1);
ea94c18b 951
96ebe67e 952 // Get the pointer to the parameter covariance matrix at first cluster
953 const TMatrixD& kParamCov = trackParam.GetCovariances();
ea94c18b 954
955 // Calculate the jacobian related to the transformation between track parameters
96ebe67e 956 // at first cluster and track coordinates at the 2 cluster z-positions
ea94c18b 957 TMatrixD jacob(4,5);
958 jacob.Zero();
96ebe67e 959 // first derivative at the first cluster:
ea94c18b 960 jacob(0,0) = 1.; // dx1/dx
961 jacob(1,2) = 1.; // dy1/dy
96ebe67e 962 // first derivative at the second cluster:
ea94c18b 963 TMatrixD dParam(5,1);
6b191dea 964 Double_t direction[5] = {-1.,-1.,1.,1.,-1.};
ea94c18b 965 for (Int_t i=0; i<5; i++) {
966 // Skip jacobian calculation for parameters with no associated error
967 if (kParamCov(i,i) == 0.) continue;
968 // Small variation of parameter i only
969 for (Int_t j=0; j<5; j++) {
970 if (j==i) {
971 dParam(j,0) = TMath::Sqrt(kParamCov(i,i));
6b191dea 972 dParam(j,0) *= TMath::Sign(1.,direction[j]*paramAtCluster1Save(j,0)); // variation always in the same direction
ea94c18b 973 } else dParam(j,0) = 0.;
974 }
975
96ebe67e 976 // Set new track parameters at first cluster
977 trackParam.SetParameters(paramAtCluster1Save);
978 trackParam.AddParameters(dParam);
979 trackParam.SetZ(cluster1->GetZ());
ea94c18b 980
96ebe67e 981 // Extrapolate new track parameters to the z position of the second cluster
4ea3f013 982 if (!AliMUONTrackExtrap::ExtrapToZ(&trackParam,cluster2->GetZ())) return 2.*AliMUONTrack::MaxChi2();
ea94c18b 983
984 // Calculate the jacobian
96ebe67e 985 jacob(2,i) = (trackParam.GetNonBendingCoor() - nonBendingCoor2) / dParam(i,0); // dx2/dParami
986 jacob(3,i) = (trackParam.GetBendingCoor() - bendingCoor2 ) / dParam(i,0); // dy2/dParami
208f139e 987 }
ea94c18b 988
989 // Calculate the error matrix
990 TMatrixD tmp(jacob,TMatrixD::kMult,kParamCov);
991 error = TMatrixD(tmp,TMatrixD::kMultTranspose,jacob);
992 }
993
96ebe67e 994 // Add cluster resolution to the error matrix
995 error(0,0) += cluster1->GetErrX2();
996 error(1,1) += cluster1->GetErrY2();
997 error(2,2) += cluster2->GetErrX2();
998 error(3,3) += cluster2->GetErrY2();
ea94c18b 999
1000 // invert the error matrix for Chi2 calculation
1001 if (error.Determinant() != 0) {
1002 error.Invert();
208f139e 1003 } else {
ea94c18b 1004 AliWarning(" Determinant error=0");
4ea3f013 1005 return 2.*AliMUONTrack::MaxChi2();
208f139e 1006 }
1007
ea94c18b 1008 // Compute the Chi2 value
1009 TMatrixD tmp2(dPos,TMatrixD::kTransposeMult,error);
1010 TMatrixD result(tmp2,TMatrixD::kMult,dPos);
1011
1012 return result(0,0);
1013
1014}
1015
1016 //__________________________________________________________________________
96ebe67e 1017void AliMUONTrackReconstructor::UpdateTrack(AliMUONTrack &track, AliMUONTrackParam &trackParamAtCluster)
ea94c18b 1018{
96ebe67e 1019 /// Add 1 cluster to the track candidate
ea94c18b 1020 /// Update chi2 of the track
1021
1022 // Compute local chi2
96ebe67e 1023 AliMUONVCluster* cluster = trackParamAtCluster.GetClusterPtr();
1024 Double_t deltaX = trackParamAtCluster.GetNonBendingCoor() - cluster->GetX();
1025 Double_t deltaY = trackParamAtCluster.GetBendingCoor() - cluster->GetY();
1026 Double_t localChi2 = deltaX*deltaX / cluster->GetErrX2() +
1027 deltaY*deltaY / cluster->GetErrY2();
ea94c18b 1028
96ebe67e 1029 // Flag cluster as being not removable
a0dc65b4 1030 if (GetRecoParam()->RequestStation(cluster->GetChamberId()/2))
9bf6860b 1031 trackParamAtCluster.SetRemovable(kFALSE);
1032 else trackParamAtCluster.SetRemovable(kTRUE);
96ebe67e 1033 trackParamAtCluster.SetLocalChi2(0.); // --> Local chi2 not used
5f4ceff2 1034
ea94c18b 1035 // Update the chi2 of the new track
96ebe67e 1036 track.SetGlobalChi2(track.GetGlobalChi2() + localChi2);
ea94c18b 1037
96ebe67e 1038 // Update TrackParamAtCluster
1039 track.AddTrackParamAtCluster(trackParamAtCluster,*cluster);
ea94c18b 1040
04b5ea16 1041}
1042
1043 //__________________________________________________________________________
96ebe67e 1044void AliMUONTrackReconstructor::UpdateTrack(AliMUONTrack &track, AliMUONTrackParam &trackParamAtCluster1, AliMUONTrackParam &trackParamAtCluster2)
ea94c18b 1045{
96ebe67e 1046 /// Add 2 clusters to the track candidate
ea94c18b 1047 /// Update track and local chi2
1048
96ebe67e 1049 // Update local chi2 at first cluster
1050 AliMUONVCluster* cluster1 = trackParamAtCluster1.GetClusterPtr();
1051 Double_t deltaX = trackParamAtCluster1.GetNonBendingCoor() - cluster1->GetX();
1052 Double_t deltaY = trackParamAtCluster1.GetBendingCoor() - cluster1->GetY();
1053 Double_t localChi2AtCluster1 = deltaX*deltaX / cluster1->GetErrX2() +
1054 deltaY*deltaY / cluster1->GetErrY2();
1055 trackParamAtCluster1.SetLocalChi2(localChi2AtCluster1);
ea94c18b 1056
96ebe67e 1057 // Flag first cluster as being removable
1058 trackParamAtCluster1.SetRemovable(kTRUE);
ea94c18b 1059
96ebe67e 1060 // Update local chi2 at second cluster
1061 AliMUONVCluster* cluster2 = trackParamAtCluster2.GetClusterPtr();
1062 deltaX = trackParamAtCluster2.GetNonBendingCoor() - cluster2->GetX();
1063 deltaY = trackParamAtCluster2.GetBendingCoor() - cluster2->GetY();
1064 Double_t localChi2AtCluster2 = deltaX*deltaX / cluster2->GetErrX2() +
1065 deltaY*deltaY / cluster2->GetErrY2();
1066 trackParamAtCluster2.SetLocalChi2(localChi2AtCluster2);
ea94c18b 1067
96ebe67e 1068 // Flag first cluster as being removable
1069 trackParamAtCluster2.SetRemovable(kTRUE);
ea94c18b 1070
1071 // Update the chi2 of the new track
96ebe67e 1072 track.SetGlobalChi2(track.GetGlobalChi2() + localChi2AtCluster1 + localChi2AtCluster2);
ea94c18b 1073
96ebe67e 1074 // Update TrackParamAtCluster
1075 track.AddTrackParamAtCluster(trackParamAtCluster1,*cluster1);
1076 track.AddTrackParamAtCluster(trackParamAtCluster2,*cluster2);
ea94c18b 1077
1078}
1079
1080 //__________________________________________________________________________
1fef78c8 1081Bool_t AliMUONTrackReconstructor::RecoverTrack(AliMUONTrack &trackCandidate, AliMUONVClusterStore& clusterStore, Int_t nextStation)
ea94c18b 1082{
1083 /// Try to recover the track candidate in the next station
96ebe67e 1084 /// by removing the worst of the two clusters attached in the current station
ea94c18b 1085 /// Return kTRUE if recovering succeeds
1086 AliDebug(1,"Enter RecoverTrack");
1087
96ebe67e 1088 // Do not try to recover track until we have attached cluster(s) on station(1..) 3
ea94c18b 1089 if (nextStation > 1) return kFALSE;
1090
96ebe67e 1091 Int_t worstClusterNumber = -1;
9bf6860b 1092 Double_t localChi2, worstLocalChi2 = -1.;
ea94c18b 1093
96ebe67e 1094 // Look for the cluster to remove
1095 for (Int_t clusterNumber = 0; clusterNumber < 2; clusterNumber++) {
1096 AliMUONTrackParam *trackParamAtCluster = (AliMUONTrackParam*)trackCandidate.GetTrackParamAtCluster()->UncheckedAt(clusterNumber);
ea94c18b 1097
9bf6860b 1098 // check if current cluster is in the previous station
1099 if (trackParamAtCluster->GetClusterPtr()->GetChamberId()/2 != nextStation+1) break;
1100
96ebe67e 1101 // check if current cluster is removable
1102 if (!trackParamAtCluster->IsRemovable()) return kFALSE;
ea94c18b 1103
9bf6860b 1104 // reset the current cluster as beig not removable if it is on a required station
a0dc65b4 1105 if (GetRecoParam()->RequestStation(nextStation+1)) trackParamAtCluster->SetRemovable(kFALSE);
9bf6860b 1106
96ebe67e 1107 // Pick up cluster with the worst chi2
1108 localChi2 = trackParamAtCluster->GetLocalChi2();
ea94c18b 1109 if (localChi2 > worstLocalChi2) {
1110 worstLocalChi2 = localChi2;
96ebe67e 1111 worstClusterNumber = clusterNumber;
ea94c18b 1112 }
9bf6860b 1113
ea94c18b 1114 }
1115
9bf6860b 1116 // check if worst cluster found
1117 if (worstClusterNumber < 0) return kFALSE;
ea94c18b 1118
96ebe67e 1119 // Remove the worst cluster
1120 trackCandidate.RemoveTrackParamAtCluster((AliMUONTrackParam*)trackCandidate.GetTrackParamAtCluster()->UncheckedAt(worstClusterNumber));
ea94c18b 1121
1122 // Re-fit the track:
1123 // Do not take into account the multiple scattering to speed up the fit
1124 // Calculate the track parameter covariance matrix
96ebe67e 1125 Fit(trackCandidate, kFALSE, kFALSE, kTRUE);
ea94c18b 1126
4ea3f013 1127 // skip track if the normalized chi2 is too high
1128 if (trackCandidate.GetNormalizedChi2() > GetRecoParam()->GetSigmaCutForTracking() * GetRecoParam()->GetSigmaCutForTracking()) return kFALSE;
1129
6b191dea 1130 // skip track out of limits
1131 if (!IsAcceptable(*((AliMUONTrackParam*)trackCandidate.GetTrackParamAtCluster()->First()))) return kFALSE;
89c8d66d 1132
96ebe67e 1133 // Look for new cluster(s) in next station
1134 return FollowTrackInStation(trackCandidate,clusterStore,nextStation);
ea94c18b 1135
1136}
1137
1138 //__________________________________________________________________________
96ebe67e 1139void AliMUONTrackReconstructor::SetVertexErrXY2ForFit(AliMUONTrack &trackCandidate)
de2cd600 1140{
96ebe67e 1141 /// Compute the vertex resolution square from natural vertex dispersion and
208f139e 1142 /// multiple scattering effets according to trackCandidate path in absorber
1143 /// It is necessary to account for multiple scattering effects here instead of during the fit of
1144 /// the "trackCandidate" to do not influence the result by changing track resolution at vertex
1145 AliDebug(1,"Enter SetVertexForFit");
de2cd600 1146
a0dc65b4 1147 Double_t nonBendingReso2 = GetRecoParam()->GetNonBendingVertexDispersion() *
1148 GetRecoParam()->GetNonBendingVertexDispersion();
1149 Double_t bendingReso2 = GetRecoParam()->GetBendingVertexDispersion() *
1150 GetRecoParam()->GetBendingVertexDispersion();
96ebe67e 1151
208f139e 1152 // add multiple scattering effets
96ebe67e 1153 AliMUONTrackParam paramAtVertex(*((AliMUONTrackParam*)(trackCandidate.GetTrackParamAtCluster()->First())));
208f139e 1154 paramAtVertex.DeleteCovariances(); // to be sure to account only for multiple scattering
4ea3f013 1155 if (!AliMUONTrackExtrap::ExtrapToZ(&paramAtVertex,AliMUONConstants::AbsZEnd())) {
1156 nonBendingReso2 = 0.;
1157 bendingReso2 = 0.;
1158 } else {
1159 AliMUONTrackExtrap::ExtrapToVertexUncorrected(&paramAtVertex,0.);
1160 const TMatrixD& kParamCov = paramAtVertex.GetCovariances();
1161 nonBendingReso2 += kParamCov(0,0);
1162 bendingReso2 += kParamCov(2,2);
1163 }
96ebe67e 1164
1165 // Set the vertex resolution square
1166 trackCandidate.SetVertexErrXY2(nonBendingReso2,bendingReso2);
208f139e 1167}
1168
1169 //__________________________________________________________________________
96ebe67e 1170void AliMUONTrackReconstructor::Fit(AliMUONTrack &track, Bool_t includeMCS, Bool_t fitWithVertex, Bool_t calcCov)
208f139e 1171{
96ebe67e 1172 /// Fit the track
1173 /// w/wo multiple Coulomb scattering according to "includeMCS".
1174 /// w/wo constraining the vertex according to "fitWithVertex".
1175 /// calculating or not the covariance matrix according to "calcCov".
7332f213 1176 AliDebug(1,"Enter Fit");
de2cd600 1177
208f139e 1178 Double_t benC, errorParam, invBenP, nonBenC, x, y;
de2cd600 1179 AliMUONTrackParam *trackParam;
96ebe67e 1180 Double_t arg[1], fedm, errdef, globalChi2;
208f139e 1181 Int_t npari, nparx;
1182 Int_t status, covStatus;
1183
8cde4af5 1184 // Instantiate gMinuit if not already done
1185 if (!gMinuit) gMinuit = new TMinuit(6);
208f139e 1186 // Clear MINUIT parameters
1187 gMinuit->mncler();
1188 // Give the fitted track to MINUIT
ea94c18b 1189 gMinuit->SetObjectFit(&track);
208f139e 1190 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 2) || (AliLog::GetGlobalDebugLevel() >= 2)) {
1191 // Define print level
1192 arg[0] = 1;
1193 gMinuit->mnexcm("SET PRI", arg, 1, status);
1194 // Print covariance matrix
1195 gMinuit->mnexcm("SHO COV", arg, 0, status);
1196 } else {
1197 arg[0] = -1;
1198 gMinuit->mnexcm("SET PRI", arg, 1, status);
1199 }
de2cd600 1200 // No warnings
208f139e 1201 gMinuit->mnexcm("SET NOW", arg, 0, status);
1202 // Define strategy
1203 //arg[0] = 2;
1204 //gMinuit->mnexcm("SET STR", arg, 1, status);
1205
ea94c18b 1206 // set flag w/wo multiple scattering according to "includeMCS"
96ebe67e 1207 track.FitWithMCS(includeMCS);
ea94c18b 1208 if (includeMCS) {
96ebe67e 1209 // compute cluster weights only once
4ea3f013 1210 if (!track.UpdateTrackParamAtCluster() || !track.ComputeClusterWeights()) {
ea94c18b 1211 AliWarning("cannot take into account the multiple scattering effects");
96ebe67e 1212 track.FitWithMCS(kFALSE);
ea94c18b 1213 }
1214 }
1215
96ebe67e 1216 track.FitWithVertex(fitWithVertex);
1217 if (fitWithVertex) SetVertexErrXY2ForFit(track);
1218
ea94c18b 1219 // Set fitting function
1220 gMinuit->SetFCN(TrackChi2);
208f139e 1221
1222 // Set fitted parameters (!! The order is very important for the covariance matrix !!)
9bf6860b 1223 // Mandatory limits to avoid NaN values of parameters
96ebe67e 1224 trackParam = (AliMUONTrackParam*) (track.GetTrackParamAtCluster()->First());
a0dc65b4 1225 Double_t maxIBM = 1. / GetRecoParam()->GetMinBendingMomentum();
208f139e 1226 gMinuit->mnparm(0, "X", trackParam->GetNonBendingCoor(), 0.03, -500.0, 500.0, status);
9bf6860b 1227 gMinuit->mnparm(1, "NonBenS", trackParam->GetNonBendingSlope(), 0.001, -1., 1., status);
208f139e 1228 gMinuit->mnparm(2, "Y", trackParam->GetBendingCoor(), 0.10, -500.0, 500.0, status);
9bf6860b 1229 gMinuit->mnparm(3, "BenS", trackParam->GetBendingSlope(), 0.001, -1.5, 1.5, status);
1230 gMinuit->mnparm(4, "InvBenP", trackParam->GetInverseBendingMomentum(), 0.003, -maxIBM, maxIBM, status);
208f139e 1231
de2cd600 1232 // minimization
208f139e 1233 gMinuit->mnexcm("MIGRAD", arg, 0, status);
1234
1235 // Calculate the covariance matrix more accurately if required
1236 if (calcCov) gMinuit->mnexcm("HESSE", arg, 0, status);
8cde4af5 1237
de2cd600 1238 // get results into "invBenP", "benC", "nonBenC" ("x", "y")
208f139e 1239 gMinuit->GetParameter(0, x, errorParam);
1240 trackParam->SetNonBendingCoor(x);
1241 gMinuit->GetParameter(1, nonBenC, errorParam);
de2cd600 1242 trackParam->SetNonBendingSlope(nonBenC);
208f139e 1243 gMinuit->GetParameter(2, y, errorParam);
1244 trackParam->SetBendingCoor(y);
1245 gMinuit->GetParameter(3, benC, errorParam);
1246 trackParam->SetBendingSlope(benC);
1247 gMinuit->GetParameter(4, invBenP, errorParam);
1248 trackParam->SetInverseBendingMomentum(invBenP);
1249
de2cd600 1250 // global result of the fit
96ebe67e 1251 gMinuit->mnstat(globalChi2, fedm, errdef, npari, nparx, covStatus);
1252 track.SetGlobalChi2(globalChi2);
208f139e 1253
1254 // Get the covariance matrix if required
1255 if (calcCov) {
1256 // Covariance matrix according to HESSE status
1257 // If problem then keep only the diagonal terms (variances)
1258 Double_t matrix[5][5];
1259 gMinuit->mnemat(&matrix[0][0],5);
1260 if (covStatus == 3) trackParam->SetCovariances(matrix);
1261 else trackParam->SetVariances(matrix);
ea94c18b 1262 } else trackParam->DeleteCovariances();
208f139e 1263
de2cd600 1264}
1265
1266 //__________________________________________________________________________
ea94c18b 1267void TrackChi2(Int_t & /*nParam*/, Double_t * /*gradient*/, Double_t &chi2, Double_t *param, Int_t /*flag*/)
de2cd600 1268{
ea94c18b 1269 /// Return the "Chi2" to be minimized with Minuit for track fitting.
96ebe67e 1270 /// Assumes that the trackParamAtCluster are sorted according to increasing Z.
1271 /// Track parameters at each cluster are updated accordingly.
ea94c18b 1272 /// Vertex is used according to the flag "trackBeingFitted->GetFitWithVertex()".
1273 /// Multiple Coulomb scattering is taken into account according to the flag "trackBeingFitted->GetFitWithMCS()".
208f139e 1274
1275 AliMUONTrack *trackBeingFitted = (AliMUONTrack*) gMinuit->GetObjectFit();
96ebe67e 1276 AliMUONTrackParam* trackParamAtCluster = (AliMUONTrackParam*) trackBeingFitted->GetTrackParamAtCluster()->First();
208f139e 1277 Double_t dX, dY;
ea94c18b 1278 chi2 = 0.; // initialize chi2
208f139e 1279
ea94c18b 1280 // update track parameters
96ebe67e 1281 trackParamAtCluster->SetNonBendingCoor(param[0]);
1282 trackParamAtCluster->SetNonBendingSlope(param[1]);
1283 trackParamAtCluster->SetBendingCoor(param[2]);
1284 trackParamAtCluster->SetBendingSlope(param[3]);
1285 trackParamAtCluster->SetInverseBendingMomentum(param[4]);
4ea3f013 1286 if (!trackBeingFitted->UpdateTrackParamAtCluster()) {
1287 chi2 = 2.*AliMUONTrack::MaxChi2();
1288 return;
1289 }
208f139e 1290
1291 // Take the vertex into account in the fit if required
96ebe67e 1292 if (trackBeingFitted->FitWithVertex()) {
1293 Double_t nonBendingReso2,bendingReso2;
1294 trackBeingFitted->GetVertexErrXY2(nonBendingReso2,bendingReso2);
4ea3f013 1295 AliMUONTrackParam paramAtVertex(*trackParamAtCluster);
1296 if (nonBendingReso2 != 0. && bendingReso2 != 0. && AliMUONTrackExtrap::ExtrapToZ(&paramAtVertex, 0.)) { // vextex position = (0,0,0)
96ebe67e 1297 dX = paramAtVertex.GetNonBendingCoor();
1298 dY = paramAtVertex.GetBendingCoor();
1299 chi2 += dX * dX / nonBendingReso2 + dY * dY / bendingReso2;
4ea3f013 1300 } else {
1301 chi2 = 2.*AliMUONTrack::MaxChi2();
1302 return;
208f139e 1303 }
de2cd600 1304 }
208f139e 1305
ea94c18b 1306 // compute chi2 w/wo multiple scattering
96ebe67e 1307 chi2 += trackBeingFitted->ComputeGlobalChi2(trackBeingFitted->FitWithMCS());
b709ac13 1308
1309}
1310
1311 //__________________________________________________________________________
89c8d66d 1312Bool_t AliMUONTrackReconstructor::ComplementTracks(const AliMUONVClusterStore& clusterStore)
b709ac13 1313{
1314 /// Complete tracks by adding missing clusters (if there is an overlap between
89c8d66d 1315 /// two detection elements, the track may have two clusters in the same chamber).
1316 /// Re-fit track parameters and covariances.
1317 /// Return kTRUE if one or more tracks have been complemented.
b709ac13 1318 AliDebug(1,"Enter ComplementTracks");
1319
1320 Int_t chamberId, detElemId;
96ebe67e 1321 Double_t chi2OfCluster, bestChi2OfCluster;
a0dc65b4 1322 Double_t sigmaCut2 = GetRecoParam()->GetSigmaCutForTracking() *
1323 GetRecoParam()->GetSigmaCutForTracking();
89c8d66d 1324 Bool_t foundOneCluster, trackModified, hasChanged = kFALSE;
96ebe67e 1325 AliMUONVCluster* cluster;
1326 AliMUONTrackParam *trackParam, *nextTrackParam, copyOfTrackParam, trackParamAtCluster, bestTrackParamAtCluster;
b709ac13 1327
b709ac13 1328 AliMUONTrack *track = (AliMUONTrack*) fRecTracksPtr->First();
1329 while (track) {
1330 trackModified = kFALSE;
1331
96ebe67e 1332 trackParam = (AliMUONTrackParam*)track->GetTrackParamAtCluster()->First();
b709ac13 1333 while (trackParam) {
96ebe67e 1334 foundOneCluster = kFALSE;
1335 bestChi2OfCluster = 2. * sigmaCut2; // 2 because 2 quantities in chi2
1336 chamberId = trackParam->GetClusterPtr()->GetChamberId();
1337 detElemId = trackParam->GetClusterPtr()->GetDetElemId();
b709ac13 1338
1339 // prepare nextTrackParam before adding new cluster because of the sorting
96ebe67e 1340 nextTrackParam = (AliMUONTrackParam*)track->GetTrackParamAtCluster()->After(trackParam);
b709ac13 1341
1342 // recover track parameters from local fit and put them into a copy of trackParam
1343 copyOfTrackParam.SetZ(trackParam->GetZ());
1344 copyOfTrackParam.SetParameters(trackParam->GetSmoothParameters());
1345 copyOfTrackParam.SetCovariances(trackParam->GetSmoothCovariances());
1346
96ebe67e 1347 // Create iterators to loop over clusters in current chamber
1348 TIter nextInCh(clusterStore.CreateChamberIterator(chamberId,chamberId));
1349
b709ac13 1350 // look for one second candidate in the same chamber
96ebe67e 1351 while ( ( cluster = static_cast<AliMUONVCluster*>(nextInCh()) ) ) {
b709ac13 1352
1353 // look for a cluster in another detection element
96ebe67e 1354 if (cluster->GetDetElemId() == detElemId) continue;
b709ac13 1355
96ebe67e 1356 // try to add the current cluster fast
1357 if (!TryOneClusterFast(copyOfTrackParam, cluster)) continue;
b709ac13 1358
96ebe67e 1359 // try to add the current cluster accurately
1360 chi2OfCluster = TryOneCluster(copyOfTrackParam, cluster, trackParamAtCluster);
b709ac13 1361
1362 // if better chi2 then prepare to add this cluster to the track
96ebe67e 1363 if (chi2OfCluster < bestChi2OfCluster) {
1364 bestChi2OfCluster = chi2OfCluster;
1365 bestTrackParamAtCluster = trackParamAtCluster;
1366 foundOneCluster = kTRUE;
b709ac13 1367 }
1368
1369 }
1370
1371 // add new cluster if any
96ebe67e 1372 if (foundOneCluster) {
0e894e58 1373
1374 // Printout for debuging
1375 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
1376 cout << "ComplementTracks: found one cluster in chamber(1..): " << chamberId+1 << endl;
1377 bestTrackParamAtCluster.GetClusterPtr()->Print();
1378 cout<<endl<<"Track parameters and covariances at cluster:"<<endl;
1379 bestTrackParamAtCluster.GetParameters().Print();
1380 bestTrackParamAtCluster.GetCovariances().Print();
1381 }
1382
7332f213 1383 trackParam->SetRemovable(kTRUE);
1384 bestTrackParamAtCluster.SetRemovable(kTRUE);
1385 track->AddTrackParamAtCluster(bestTrackParamAtCluster,*(bestTrackParamAtCluster.GetClusterPtr()));
b709ac13 1386 trackModified = kTRUE;
89c8d66d 1387 hasChanged = kTRUE;
b709ac13 1388 }
1389
1390 trackParam = nextTrackParam;
1391 }
1392
1393 // re-fit track parameters if needed
96ebe67e 1394 if (trackModified) Fit(*track, kTRUE, kFALSE, kTRUE);
b709ac13 1395
1396 track = (AliMUONTrack*) fRecTracksPtr->After(track);
1397 }
ea94c18b 1398
89c8d66d 1399 return hasChanged;
1400
de2cd600 1401}
1402
1403 //__________________________________________________________________________
7332f213 1404void AliMUONTrackReconstructor::ImproveTrack(AliMUONTrack &track)
de2cd600 1405{
7332f213 1406 /// Improve the given track by removing clusters with local chi2 highter than the defined cut
ea94c18b 1407 /// Recompute track parameters and covariances at the remaining clusters
7332f213 1408 AliDebug(1,"Enter ImproveTrack");
208f139e 1409
ea94c18b 1410 Double_t localChi2, worstLocalChi2;
7332f213 1411 AliMUONTrackParam *trackParamAtCluster, *worstTrackParamAtCluster;
a0dc65b4 1412 Double_t sigmaCut2 = GetRecoParam()->GetSigmaCutForImprovement() *
1413 GetRecoParam()->GetSigmaCutForImprovement();
208f139e 1414
7332f213 1415 while (!track.IsImproved()) {
ea94c18b 1416
7332f213 1417 // identify removable clusters
a0dc65b4 1418 track.TagRemovableClusters(GetRecoParam()->RequestedStationMask());
b709ac13 1419
7332f213 1420 // Update track parameters and covariances
4ea3f013 1421 if (!track.UpdateCovTrackParamAtCluster()) {
1422 AliWarning("unable to update track parameters and covariances --> stop improvement");
1423 break;
1424 }
7332f213 1425
1426 // Compute local chi2 of each clusters
1427 track.ComputeLocalChi2(kTRUE);
1428
1429 // Look for the cluster to remove
1430 worstTrackParamAtCluster = NULL;
1431 worstLocalChi2 = 0.;
1432 trackParamAtCluster = (AliMUONTrackParam*) track.GetTrackParamAtCluster()->First();
1433 while (trackParamAtCluster) {
ea94c18b 1434
7332f213 1435 // Pick up cluster with the worst chi2
1436 localChi2 = trackParamAtCluster->GetLocalChi2();
1437 if (localChi2 > worstLocalChi2) {
1438 worstLocalChi2 = localChi2;
1439 worstTrackParamAtCluster = trackParamAtCluster;
ea94c18b 1440 }
1441
7332f213 1442 trackParamAtCluster = (AliMUONTrackParam*) track.GetTrackParamAtCluster()->After(trackParamAtCluster);
1443 }
1444
1445 // Check if worst cluster found
1446 if (!worstTrackParamAtCluster) {
1447 AliWarning("Bad local chi2 values?");
1448 break;
1449 }
1450
1451 // Check whether the worst chi2 is under requirement or not
1452 if (worstLocalChi2 < 2. * sigmaCut2) { // 2 because 2 quantities in chi2
1453 track.SetImproved(kTRUE);
1454 break;
1455 }
1456
1457 // if the worst cluster is not removable then stop improvement
1458 if (!worstTrackParamAtCluster->IsRemovable()) break;
1459
1460 // Remove the worst cluster
1461 track.RemoveTrackParamAtCluster(worstTrackParamAtCluster);
1462
1463 // Re-fit the track:
1464 // Take into account the multiple scattering
1465 // Calculate the track parameter covariance matrix
1466 Fit(track, kTRUE, kFALSE, kTRUE);
1467
1468 // Printout for debuging
1469 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
1470 cout << "ImproveTracks: track " << fRecTracksPtr->IndexOf(&track)+1 << " improved " << endl;
de2cd600 1471 }
ea94c18b 1472
de2cd600 1473 }
1474
de2cd600 1475}
1476
208f139e 1477 //__________________________________________________________________________
4ea3f013 1478Bool_t AliMUONTrackReconstructor::FinalizeTrack(AliMUONTrack &track)
b8dc484b 1479{
7332f213 1480 /// Recompute track parameters and covariances at each attached cluster
1481 /// from those at the first one, if not already done
1482 AliDebug(1,"Enter FinalizeTrack");
4ea3f013 1483 if (!track.IsImproved() && !track.UpdateCovTrackParamAtCluster()) {
1484 AliWarning("finalization failed due to extrapolation problem");
1485 return kFALSE;
1486 }
1487 return kTRUE;
a9e2aefa 1488}
1489
9bf6860b 1490 //__________________________________________________________________________
b1fea02e 1491Bool_t AliMUONTrackReconstructor::RefitTrack(AliMUONTrack &track, Bool_t enableImprovement)
7deb8eb0 1492{
1493 /// re-fit the given track
1494
1495 // check validity of the track
ad3c6eda 1496 if (track.GetNClusters() < 3) {
7deb8eb0 1497 AliWarning("the track does not contain enough clusters --> unable to refit");
1498 return kFALSE;
1499 }
1500
1501 // reset the seed (i.e. parameters at first cluster) before fitting
1502 AliMUONTrackParam* firstTrackParam = (AliMUONTrackParam*) track.GetTrackParamAtCluster()->First();
1503 if (firstTrackParam->GetInverseBendingMomentum() == 0.) {
1504 AliWarning("track parameters at first chamber are not initialized --> unable to refit");
1505 return kFALSE;
1506 }
1507
1508 // compute track parameters at each cluster from parameters at the first one
1509 // necessary to compute multiple scattering effect during refitting
4ea3f013 1510 if (!track.UpdateTrackParamAtCluster()) {
1511 AliWarning("bad track refitting due to extrapolation failure");
1512 return kFALSE;
1513 }
7deb8eb0 1514
1515 // Re-fit the track:
1516 // Take into account the multiple scattering
1517 // Calculate the track parameter covariance matrix
1518 Fit(track, kTRUE, kFALSE, kTRUE);
1519
1520 // Improve the reconstructed tracks if required
b1fea02e 1521 track.SetImproved(kFALSE);
a0dc65b4 1522 if (enableImprovement && GetRecoParam()->ImproveTracks()) ImproveTrack(track);
7deb8eb0 1523
1524 // Fill AliMUONTrack data members
4ea3f013 1525 if (track.GetGlobalChi2() < AliMUONTrack::MaxChi2()) return FinalizeTrack(track);
1526 else {
1527 AliWarning("track not finalized due to extrapolation failure");
1528 return kFALSE;
1529 }
7deb8eb0 1530
1531}
1532