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