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