MUON + CheckCompiler
[u/mrichter/AliRoot.git] / MUON / MUONrec / AliMUONTrackReconstructorK.cxx
CommitLineData
8cc77df0 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
16/* $Id$ */
17
3d1463c8 18//-----------------------------------------------------------------------------
06ca6d7b 19/// \class AliMUONTrackReconstructorK
06ca6d7b 20///
21/// MUON track reconstructor using the kalman method
22///
23/// This class contains as data:
24/// - the parameters for the track reconstruction
25///
26/// It contains as methods, among others:
27/// - MakeTracks to build the tracks
28///
3d1463c8 29//-----------------------------------------------------------------------------
8cc77df0 30
8cc77df0 31#include "AliMUONTrackReconstructorK.h"
96ebe67e 32
8cc77df0 33#include "AliMUONConstants.h"
96ebe67e 34#include "AliMUONVCluster.h"
1fef78c8 35#include "AliMUONVClusterServer.h"
96ebe67e 36#include "AliMUONVClusterStore.h"
ea94c18b 37#include "AliMUONTrack.h"
38#include "AliMUONTrackParam.h"
39#include "AliMUONTrackExtrap.h"
5a240757 40#include "AliMUONRecoParam.h"
852fefb4 41#include "AliMUONGeometryTransformer.h"
8cde4af5 42
1fef78c8 43#include "AliMpArea.h"
44
8cc77df0 45#include "AliLog.h"
46
8cde4af5 47#include <Riostream.h>
ea94c18b 48#include <TMath.h>
49#include <TMatrixD.h>
ce8cd162 50#include <TClonesArray.h>
8cde4af5 51
cdd730d0 52using std::endl;
53using std::cout;
8cde4af5 54/// \cond CLASSIMP
8cc77df0 55ClassImp(AliMUONTrackReconstructorK) // Class implementation in ROOT context
8cde4af5 56/// \endcond
8cc77df0 57
019df241 58 //__________________________________________________________________________
852fefb4 59AliMUONTrackReconstructorK::AliMUONTrackReconstructorK(const AliMUONRecoParam* recoParam, AliMUONVClusterServer* clusterServer,
60 const AliMUONGeometryTransformer* transformer)
61 : AliMUONVTrackReconstructor(recoParam, clusterServer, transformer)
8cc77df0 62{
96ebe67e 63 /// Constructor
8cc77df0 64}
65
019df241 66 //__________________________________________________________________________
ea94c18b 67AliMUONTrackReconstructorK::~AliMUONTrackReconstructorK()
8cc77df0 68{
ea94c18b 69/// Destructor
70}
8cc77df0 71
72 //__________________________________________________________________________
89c8d66d 73Bool_t AliMUONTrackReconstructorK::MakeTrackCandidates(AliMUONVClusterStore& clusterStore)
8cc77df0 74{
89c8d66d 75 /// To make track candidates (assuming linear propagation if AliMUONRecoParam::MakeTrackCandidatesFast() return kTRUE):
ea94c18b 76 /// Start with segments station(1..) 4 or 5 then follow track in station 5 or 4.
89c8d66d 77 /// Good candidates are made of at least three clusters if both stations are requested (two otherwise).
78 /// Keep only best candidates or all of them according to the flag AliMUONRecoParam::TrackAllTracks().
b709ac13 79
208f139e 80 TClonesArray *segments;
89c8d66d 81 AliMUONObjectPair *segment;
ea94c18b 82 AliMUONTrack *track;
83 Int_t iCandidate = 0;
96ebe67e 84 Bool_t clusterFound;
8cc77df0 85
ea94c18b 86 AliDebug(1,"Enter MakeTrackCandidates");
8cc77df0 87
1fef78c8 88 // Unless we're doing combined tracking, we'll clusterize all stations at once
89 Int_t firstChamber(0);
90 Int_t lastChamber(9);
91
a0dc65b4 92 if (GetRecoParam()->CombineClusterTrackReco()) {
1fef78c8 93 // ... Here's the exception : ask the clustering to reconstruct
94 // clusters *only* in station 4 and 5 for combined tracking
95 firstChamber = 6;
96 }
97
98 for (Int_t i = firstChamber; i <= lastChamber; ++i )
99 {
35be7ed7 100 if (fClusterServer && GetRecoParam()->UseChamber(i)) fClusterServer->Clusterize(i, clusterStore, AliMpArea(), GetRecoParam());
1fef78c8 101 }
102
ea94c18b 103 // Loop over stations(1..) 5 and 4 and make track candidates
104 for (Int_t istat=4; istat>=3; istat--) {
105
208f139e 106 // Make segments in the station
9bf6860b 107 segments = MakeSegmentsBetweenChambers(clusterStore, 2*istat, 2*istat+1);
ea94c18b 108
109 // Loop over segments
89c8d66d 110 for (Int_t iSegment=0; iSegment<segments->GetEntriesFast(); iSegment++) {
ea94c18b 111 AliDebug(1,Form("Making primary candidate(1..) %d",++iCandidate));
89c8d66d 112 segment = (AliMUONObjectPair*) segments->UncheckedAt(iSegment);
ea94c18b 113
114 // Transform segments to tracks and put them at the end of fRecTracksPtr
89c8d66d 115 track = new ((*fRecTracksPtr)[fRecTracksPtr->GetLast()+1]) AliMUONTrack(segment,GetRecoParam()->GetBendingVertexDispersion());
ea94c18b 116 fNRecTracks++;
117
96ebe67e 118 // Look for compatible cluster(s) in the other station
89c8d66d 119 if (GetRecoParam()->MakeTrackCandidatesFast()) clusterFound = FollowLinearTrackInStation(*track, clusterStore, 7-istat);
120 else clusterFound = FollowTrackInStation(*track, clusterStore, 7-istat);
019df241 121
9bf6860b 122 // Remove track if no cluster found on a requested station
89c8d66d 123 // or abort tracking if there are too many candidates
6b191dea 124 if (GetRecoParam()->RequestStation(7-istat)) {
125 if (!clusterFound) {
126 fRecTracksPtr->Remove(track);
127 fNRecTracks--;
128 } else if (fNRecTracks > GetRecoParam()->GetMaxTrackCandidates()) {
87976389 129 AliError(Form("Too many track candidates (%d tracks). Stop tracking.", fNRecTracks));
6b191dea 130 return kFALSE;
131 }
132 } else {
133 if ((fNRecTracks + segments->GetEntriesFast() - iSegment - 1) > GetRecoParam()->GetMaxTrackCandidates()) {
4ea3f013 134 AliError(Form("Too many track candidates (%d tracks). Stop tracking.", fNRecTracks + segments->GetEntriesFast() - iSegment - 1));
6b191dea 135 return kFALSE;
136 }
ea94c18b 137 }
138
139 }
89c8d66d 140
ea94c18b 141 }
142
14bbe719 143 AliDebug(1,Form("Number of candidates before cleaning = %d",fNRecTracks));
144
89c8d66d 145 // Keep all different tracks if required
146 if (GetRecoParam()->TrackAllTracks()) RemoveIdenticalTracks();
ea94c18b 147
89c8d66d 148 // Retrace tracks using Kalman filter and select them if needed
149 Int_t nCurrentTracks = fRecTracksPtr->GetLast()+1;
150 for (Int_t iRecTrack = 0; iRecTrack < nCurrentTracks; iRecTrack++) {
151 track = (AliMUONTrack*) fRecTracksPtr->UncheckedAt(iRecTrack);
ea94c18b 152
89c8d66d 153 // skip empty slots
154 if(!track) continue;
155
4ea3f013 156 // retrace tracks using Kalman filter and remove the ones for which extrap failed or that are out of limits
157 if (!RetraceTrack(*track,kTRUE) || !IsAcceptable(*((AliMUONTrackParam*)track->GetTrackParamAtCluster()->First()))) {
6b191dea 158 fRecTracksPtr->Remove(track);
159 fNRecTracks--;
ea94c18b 160 }
161
162 }
163
89c8d66d 164 // Keep only the best tracks if required
165 if (!GetRecoParam()->TrackAllTracks()) RemoveDoubleTracks();
3e9a7a05 166 else fRecTracksPtr->Compress();
ea94c18b 167
168 AliDebug(1,Form("Number of good candidates = %d",fNRecTracks));
169
89c8d66d 170 return kTRUE;
171
8cc77df0 172}
173
ea94c18b 174 //__________________________________________________________________________
89c8d66d 175Bool_t AliMUONTrackReconstructorK::MakeMoreTrackCandidates(AliMUONVClusterStore& clusterStore)
9bf6860b 176{
89c8d66d 177 /// To make extra track candidates assuming linear propagation:
9bf6860b 178 /// clustering is supposed to be already done
179 /// Start with segments made of 1 cluster in each of the stations 4 and 5 then follow track in remaining chambers.
89c8d66d 180 /// Good candidates are made of at least three clusters if both stations are requested (two otherwise).
9bf6860b 181 /// Keep only best candidates or all of them according to the flag fgkTrackAllTracks.
182
183 TClonesArray *segments;
89c8d66d 184 AliMUONObjectPair *segment;
9bf6860b 185 AliMUONTrack *track;
186 Int_t iCandidate = 0, iCurrentTrack, nCurrentTracks;
187 Int_t initialNRecTracks = fNRecTracks;
188 Bool_t clusterFound;
189
190 AliDebug(1,"Enter MakeMoreTrackCandidates");
191
192 // Double loop over chambers in stations(1..) 4 and 5 to make track candidates
193 for (Int_t ich1 = 6; ich1 <= 7; ich1++) {
194 for (Int_t ich2 = 8; ich2 <= 9; ich2++) {
195
196 // Make segments between ch1 and ch2
197 segments = MakeSegmentsBetweenChambers(clusterStore, ich1, ich2);
198
89c8d66d 199 /// Remove segments already attached to a track
200 RemoveUsedSegments(*segments);
201
9bf6860b 202 // Loop over segments
89c8d66d 203 for (Int_t iSegment=0; iSegment<segments->GetEntriesFast(); iSegment++) {
9bf6860b 204 AliDebug(1,Form("Making primary candidate(1..) %d",++iCandidate));
89c8d66d 205 segment = (AliMUONObjectPair*) segments->UncheckedAt(iSegment);
9bf6860b 206
207 // Transform segments to tracks and put them at the end of fRecTracksPtr
208 iCurrentTrack = fRecTracksPtr->GetLast()+1;
89c8d66d 209 track = new ((*fRecTracksPtr)[iCurrentTrack]) AliMUONTrack(segment,GetRecoParam()->GetBendingVertexDispersion());
9bf6860b 210 fNRecTracks++;
211
212 // Look for compatible cluster(s) in the second chamber of station 5
89c8d66d 213 clusterFound = FollowLinearTrackInChamber(*track, clusterStore, 17-ich2);
9bf6860b 214
215 // skip the original track in case it has been removed
a0dc65b4 216 if (GetRecoParam()->TrackAllTracks() && clusterFound) iCurrentTrack++;
9bf6860b 217
218 // loop over every new tracks
219 nCurrentTracks = fRecTracksPtr->GetLast()+1;
220 while (iCurrentTrack < nCurrentTracks) {
221 track = (AliMUONTrack*) fRecTracksPtr->UncheckedAt(iCurrentTrack);
222
223 // Look for compatible cluster(s) in the second chamber of station 4
89c8d66d 224 FollowLinearTrackInChamber(*track, clusterStore, 13-ich1);
9bf6860b 225
226 iCurrentTrack++;
227 }
228
89c8d66d 229 // abort tracking if there are too many candidates
6b191dea 230 if ((fNRecTracks + segments->GetEntriesFast() - iSegment - 1) > GetRecoParam()->GetMaxTrackCandidates()) {
87976389 231 AliError(Form("Too many track candidates (%d tracks). Stop tracking.", fNRecTracks + segments->GetEntriesFast() - iSegment - 1));
89c8d66d 232 return kFALSE;
233 }
234
9bf6860b 235 }
236
9bf6860b 237 }
238 }
239
14bbe719 240 AliDebug(1,Form("Number of candidates before cleaning = %d",fNRecTracks));
241
89c8d66d 242 // Retrace tracks using Kalman filter (also compute track chi2) and select them
243 nCurrentTracks = fRecTracksPtr->GetLast()+1;
244 for (Int_t iRecTrack = initialNRecTracks; iRecTrack < nCurrentTracks; iRecTrack++) {
9bf6860b 245 track = (AliMUONTrack*) fRecTracksPtr->UncheckedAt(iRecTrack);
246
89c8d66d 247 // skip empty slots
248 if(!track) continue;
249
4ea3f013 250 // retrace tracks using Kalman filter and remove the ones for which extrap failed or that are out of limits
251 if (!RetraceTrack(*track,kTRUE) || !IsAcceptable(*((AliMUONTrackParam*)track->GetTrackParamAtCluster()->First()))) {
6b191dea 252 fRecTracksPtr->Remove(track);
253 fNRecTracks--;
9bf6860b 254 }
255
256 }
257
89c8d66d 258 // Keep only the best tracks if required
259 if (!GetRecoParam()->TrackAllTracks()) RemoveDoubleTracks();
260 else fRecTracksPtr->Compress();
9bf6860b 261
262 AliDebug(1,Form("Number of good candidates = %d",fNRecTracks));
263
89c8d66d 264 return kTRUE;
265
9bf6860b 266}
267
268 //__________________________________________________________________________
4ea3f013 269Bool_t AliMUONTrackReconstructorK::RetraceTrack(AliMUONTrack &trackCandidate, Bool_t resetSeed)
8cc77df0 270{
96ebe67e 271 /// Re-run the kalman filter from the most downstream cluster to the most uptream one
4ea3f013 272 /// Return kFALSE in case of failure (i.e. extrapolation problem)
ea94c18b 273 AliDebug(1,"Enter RetraceTrack");
274
7332f213 275 AliMUONTrackParam* lastTrackParam = (AliMUONTrackParam*) trackCandidate.GetTrackParamAtCluster()->Last();
ea94c18b 276
96ebe67e 277 // Reset the "seed" (= track parameters and their covariances at last cluster) if required
ea94c18b 278 if (resetSeed) {
7332f213 279
280 // parameters at last cluster
281 AliMUONVCluster* cluster2 = lastTrackParam->GetClusterPtr();
282 Double_t x2 = cluster2->GetX();
283 Double_t y2 = cluster2->GetY();
284 Double_t z2 = cluster2->GetZ();
285
286 // parameters at last but one cluster
287 AliMUONTrackParam* previousTrackParam = (AliMUONTrackParam*) trackCandidate.GetTrackParamAtCluster()->Before(lastTrackParam);
288 AliMUONVCluster* cluster1 = previousTrackParam->GetClusterPtr();
0e894e58 289 // make sure it is on the previous chamber (can have 2 clusters in the same chamber after "ComplementTrack")
290 if (cluster2->GetChamberId() == cluster1->GetChamberId()) {
291 previousTrackParam = (AliMUONTrackParam*) trackCandidate.GetTrackParamAtCluster()->Before(previousTrackParam);
292 cluster1 = previousTrackParam->GetClusterPtr();
293 }
7332f213 294 Double_t x1 = cluster1->GetX();
295 Double_t y1 = cluster1->GetY();
296 Double_t z1 = cluster1->GetZ();
297
298 // reset track parameters
299 Double_t dZ = z1 - z2;
300 lastTrackParam->SetNonBendingCoor(x2);
301 lastTrackParam->SetBendingCoor(y2);
302 lastTrackParam->SetZ(z2);
303 lastTrackParam->SetNonBendingSlope((x1 - x2) / dZ);
304 lastTrackParam->SetBendingSlope((y1 - y2) / dZ);
305 Double_t bendingImpact = y2 - z2 * lastTrackParam->GetBendingSlope();
9bf6860b 306 Double_t inverseBendingMomentum = 1. / AliMUONTrackExtrap::GetBendingMomentumFromImpactParam(bendingImpact);
307 lastTrackParam->SetInverseBendingMomentum(inverseBendingMomentum);
7332f213 308
309 // => Reset track parameter covariances at last cluster (as if the other clusters did not exist)
310 TMatrixD lastParamCov(5,5);
311 lastParamCov.Zero();
ea94c18b 312 // Non bending plane
7332f213 313 lastParamCov(0,0) = cluster2->GetErrX2();
89c8d66d 314 lastParamCov(0,1) = - cluster2->GetErrX2() / dZ;
315 lastParamCov(1,0) = lastParamCov(0,1);
316 lastParamCov(1,1) = ( 1000. * cluster1->GetErrX2() + cluster2->GetErrX2() ) / dZ / dZ;
ea94c18b 317 // Bending plane
7332f213 318 lastParamCov(2,2) = cluster2->GetErrY2();
89c8d66d 319 lastParamCov(2,3) = - cluster2->GetErrY2() / dZ;
320 lastParamCov(3,2) = lastParamCov(2,3);
321 lastParamCov(3,3) = ( 1000. * cluster1->GetErrY2() + cluster2->GetErrY2() ) / dZ / dZ;
9bf6860b 322 // Inverse bending momentum (vertex resolution + bending slope resolution + 10% error on dipole parameters+field)
89c8d66d 323 if (AliMUONTrackExtrap::IsFieldON()) {
324 lastParamCov(4,4) = ((GetRecoParam()->GetBendingVertexDispersion() *
325 GetRecoParam()->GetBendingVertexDispersion() +
326 (z1 * z1 * cluster2->GetErrY2() + z2 * z2 * 1000. * cluster1->GetErrY2()) / dZ / dZ) /
327 bendingImpact / bendingImpact + 0.1 * 0.1) * inverseBendingMomentum * inverseBendingMomentum;
328 lastParamCov(2,4) = z1 * cluster2->GetErrY2() * inverseBendingMomentum / bendingImpact / dZ;
329 lastParamCov(4,2) = lastParamCov(2,4);
330 lastParamCov(3,4) = - (z1 * cluster2->GetErrY2() + z2 * 1000. * cluster1->GetErrY2()) *
331 inverseBendingMomentum / bendingImpact / dZ / dZ;
332 lastParamCov(4,3) = lastParamCov(3,4);
333 } else lastParamCov(4,4) = inverseBendingMomentum*inverseBendingMomentum;
7332f213 334 lastTrackParam->SetCovariances(lastParamCov);
ea94c18b 335
336 // Reset the track chi2
7332f213 337 lastTrackParam->SetTrackChi2(0.);
ea94c18b 338
339 }
340
341 // Redo the tracking
4ea3f013 342 return RetracePartialTrack(trackCandidate, lastTrackParam);
ea94c18b 343
344}
8cc77df0 345
ea94c18b 346 //__________________________________________________________________________
4ea3f013 347Bool_t AliMUONTrackReconstructorK::RetracePartialTrack(AliMUONTrack &trackCandidate, const AliMUONTrackParam* startingTrackParam)
ea94c18b 348{
96ebe67e 349 /// Re-run the kalman filter from the cluster attached to startingTrackParam to the most uptream cluster
4ea3f013 350 /// Return kFALSE in case of failure (i.e. extrapolation problem)
ea94c18b 351 AliDebug(1,"Enter RetracePartialTrack");
352
353 // Printout for debuging
354 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
96ebe67e 355 cout << "RetracePartialTrack: track chi2 before re-tracking: " << trackCandidate.GetGlobalChi2() << endl;
ea94c18b 356 }
357
358 // Reset the track chi2
96ebe67e 359 trackCandidate.SetGlobalChi2(startingTrackParam->GetTrackChi2());
ea94c18b 360
96ebe67e 361 // loop over attached clusters until the first one and recompute track parameters and covariances using kalman filter
4ea3f013 362 Bool_t extrapStatus = kTRUE;
96ebe67e 363 Int_t expectedChamber = startingTrackParam->GetClusterPtr()->GetChamberId() - 1;
ea94c18b 364 Int_t currentChamber;
96ebe67e 365 Double_t addChi2TrackAtCluster;
366 AliMUONTrackParam* trackParamAtCluster = (AliMUONTrackParam*) trackCandidate.GetTrackParamAtCluster()->Before(startingTrackParam);
367 while (trackParamAtCluster) {
ea94c18b 368
369 // reset track parameters and their covariances
96ebe67e 370 trackParamAtCluster->SetParameters(startingTrackParam->GetParameters());
371 trackParamAtCluster->SetZ(startingTrackParam->GetZ());
372 trackParamAtCluster->SetCovariances(startingTrackParam->GetCovariances());
ea94c18b 373
374 // add MCS effect
4663da9f 375 AliMUONTrackExtrap::AddMCSEffect(trackParamAtCluster,AliMUONConstants::ChamberThicknessInX0(expectedChamber+1),-1.);
ea94c18b 376
377 // reset propagator for smoother
a0dc65b4 378 if (GetRecoParam()->UseSmoother()) trackParamAtCluster->ResetPropagator();
ea94c18b 379
9bf6860b 380 // add MCS in missing chambers if any
96ebe67e 381 currentChamber = trackParamAtCluster->GetClusterPtr()->GetChamberId();
ea94c18b 382 while (currentChamber < expectedChamber) {
383 // extrapolation to the missing chamber (update the propagator)
4ea3f013 384 if (!AliMUONTrackExtrap::ExtrapToZCov(trackParamAtCluster, AliMUONConstants::DefaultChamberZ(expectedChamber),
385 GetRecoParam()->UseSmoother())) extrapStatus = kFALSE;
ea94c18b 386 // add MCS effect
4663da9f 387 AliMUONTrackExtrap::AddMCSEffect(trackParamAtCluster,AliMUONConstants::ChamberThicknessInX0(expectedChamber),-1.);
ea94c18b 388 expectedChamber--;
389 }
390
96ebe67e 391 // extrapolation to the plane of the cluster attached to the current trackParamAtCluster (update the propagator)
4ea3f013 392 if (!AliMUONTrackExtrap::ExtrapToZCov(trackParamAtCluster, trackParamAtCluster->GetClusterPtr()->GetZ(),
393 GetRecoParam()->UseSmoother())) extrapStatus = kFALSE;
ea94c18b 394
a0dc65b4 395 if (GetRecoParam()->UseSmoother()) {
ea94c18b 396 // save extrapolated parameters for smoother
96ebe67e 397 trackParamAtCluster->SetExtrapParameters(trackParamAtCluster->GetParameters());
ea94c18b 398
399 // save extrapolated covariance matrix for smoother
96ebe67e 400 trackParamAtCluster->SetExtrapCovariances(trackParamAtCluster->GetCovariances());
ea94c18b 401 }
402
9bf6860b 403 // Compute new track parameters using kalman filter
96ebe67e 404 addChi2TrackAtCluster = RunKalmanFilter(*trackParamAtCluster);
ea94c18b 405
406 // Update the track chi2
96ebe67e 407 trackCandidate.SetGlobalChi2(trackCandidate.GetGlobalChi2() + addChi2TrackAtCluster);
408 trackParamAtCluster->SetTrackChi2(trackCandidate.GetGlobalChi2());
ea94c18b 409
96ebe67e 410 // prepare next step
411 expectedChamber = currentChamber - 1;
412 startingTrackParam = trackParamAtCluster;
413 trackParamAtCluster = (AliMUONTrackParam*) (trackCandidate.GetTrackParamAtCluster()->Before(startingTrackParam));
ea94c18b 414 }
415
416 // Printout for debuging
417 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
96ebe67e 418 cout << "RetracePartialTrack: track chi2 after re-tracking: " << trackCandidate.GetGlobalChi2() << endl;
ea94c18b 419 }
420
4ea3f013 421 // set global chi2 to max value in case of problem during track extrapolation
422 if (!extrapStatus) trackCandidate.SetGlobalChi2(2.*AliMUONTrack::MaxChi2());
423 return extrapStatus;
424
ea94c18b 425}
8cc77df0 426
ea94c18b 427 //__________________________________________________________________________
89c8d66d 428Bool_t AliMUONTrackReconstructorK::FollowTracks(AliMUONVClusterStore& clusterStore)
ea94c18b 429{
430 /// Follow tracks in stations(1..) 3, 2 and 1
431 AliDebug(1,"Enter FollowTracks");
432
433 AliMUONTrack *track;
434 Int_t currentNRecTracks;
ea94c18b 435
436 for (Int_t station = 2; station >= 0; station--) {
437
438 // Save the actual number of reconstructed track in case of
439 // tracks are added or suppressed during the tracking procedure
440 // !! Do not compress fRecTracksPtr until the end of the loop over tracks !!
441 currentNRecTracks = fNRecTracks;
442
443 for (Int_t iRecTrack = 0; iRecTrack <currentNRecTracks; iRecTrack++) {
444 AliDebug(1,Form("FollowTracks: track candidate(1..) %d", iRecTrack+1));
445
446 track = (AliMUONTrack*) fRecTracksPtr->UncheckedAt(iRecTrack);
447
96ebe67e 448 // Look for compatible cluster(s) in station(0..) "station"
9bf6860b 449 if (!FollowTrackInStation(*track, clusterStore, station)) {
450
451 // Try to recover track if required
a0dc65b4 452 if (GetRecoParam()->RecoverTracks()) {
9bf6860b 453
454 // work on a copy of the track if this station is not required
455 // to keep the case where no cluster is reconstructed as a possible candidate
a0dc65b4 456 if (!GetRecoParam()->RequestStation(station)) {
9bf6860b 457 track = new ((*fRecTracksPtr)[fRecTracksPtr->GetLast()+1]) AliMUONTrack(*track);
458 fNRecTracks++;
459 }
460
461 // try to recover
462 if (!RecoverTrack(*track, clusterStore, station)) {
463 // remove track if no cluster found
464 fRecTracksPtr->Remove(track);
465 fNRecTracks--;
466 }
467
a0dc65b4 468 } else if (GetRecoParam()->RequestStation(station)) {
9bf6860b 469 // remove track if no cluster found
470 fRecTracksPtr->Remove(track);
471 fNRecTracks--;
472 }
473
8cc77df0 474 }
ea94c18b 475
89c8d66d 476 // abort tracking if there are too many candidates
6b191dea 477 if (GetRecoParam()->RequestStation(station)) {
478 if (fNRecTracks > GetRecoParam()->GetMaxTrackCandidates()) {
87976389 479 AliError(Form("Too many track candidates (%d tracks). Stop tracking.", fNRecTracks));
6b191dea 480 return kFALSE;
481 }
482 } else {
483 if ((fNRecTracks + currentNRecTracks - iRecTrack - 1) > GetRecoParam()->GetMaxTrackCandidates()) {
87976389 484 AliError(Form("Too many track candidates (%d tracks). Stop tracking.", fNRecTracks + currentNRecTracks - iRecTrack - 1));
6b191dea 485 return kFALSE;
486 }
89c8d66d 487 }
488
8cc77df0 489 }
ea94c18b 490
491 fRecTracksPtr->Compress(); // this is essential before checking tracks
492
14bbe719 493 AliDebug(1,Form("In stations(1..) %d: Number of candidates before cleaning = %d",station+1,fNRecTracks));
494
ea94c18b 495 // Keep only the best tracks if required
a0dc65b4 496 if (!GetRecoParam()->TrackAllTracks()) RemoveDoubleTracks();
ea94c18b 497
14bbe719 498 AliDebug(1,Form("In stations(1..) %d: Number of good candidates = %d",station+1,fNRecTracks));
499
ea94c18b 500 }
501
89c8d66d 502 return kTRUE;
503
ea94c18b 504}
b0e4c598 505
ea94c18b 506 //__________________________________________________________________________
9bf6860b 507Bool_t AliMUONTrackReconstructorK::FollowTrackInChamber(AliMUONTrack &trackCandidate, AliMUONVClusterStore& clusterStore, Int_t nextChamber)
508{
509 /// Follow trackCandidate in chamber(0..) nextChamber and search for compatible cluster(s)
510 /// Keep all possibilities or only the best one(s) according to the flag fgkTrackAllTracks:
511 /// kTRUE: duplicate "trackCandidate" if there are several possibilities and add the new tracks at the end of
512 /// fRecTracksPtr to avoid conficts with other track candidates at this current stage of the tracking procedure.
513 /// Remove the obsolete "trackCandidate" at the end.
514 /// kFALSE: add only the best cluster(s) to the "trackCandidate". Try to add a couple of clusters in priority.
515 /// return kTRUE if new cluster(s) have been found (otherwise return kFALSE)
516 AliDebug(1,Form("Enter FollowTrackInChamber(1..) %d", nextChamber+1));
517
9bf6860b 518 Double_t chi2OfCluster;
a0dc65b4 519 Double_t maxChi2OfCluster = 2. * GetRecoParam()->GetSigmaCutForTracking() *
520 GetRecoParam()->GetSigmaCutForTracking(); // 2 because 2 quantities in chi2
9bf6860b 521 Double_t addChi2TrackAtCluster;
4ea3f013 522 Double_t bestAddChi2TrackAtCluster = AliMUONTrack::MaxChi2();
9bf6860b 523 Bool_t foundOneCluster = kFALSE;
524 AliMUONTrack *newTrack = 0x0;
525 AliMUONVCluster *cluster;
526 AliMUONTrackParam extrapTrackParamAtCh;
527 AliMUONTrackParam extrapTrackParamAtCluster;
528 AliMUONTrackParam bestTrackParamAtCluster;
529
530 // Get track parameters according to the propagation direction
531 if (nextChamber > 7) extrapTrackParamAtCh = *(AliMUONTrackParam*)trackCandidate.GetTrackParamAtCluster()->Last();
532 else extrapTrackParamAtCh = *(AliMUONTrackParam*)trackCandidate.GetTrackParamAtCluster()->First();
533
534 // Printout for debuging
535 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 2) || (AliLog::GetGlobalDebugLevel() >= 2)) {
536 cout<<endl<<"Track parameters and covariances at first cluster:"<<endl;
537 extrapTrackParamAtCh.GetParameters().Print();
538 extrapTrackParamAtCh.GetCovariances().Print();
539 }
540
541 // Add MCS effect
4663da9f 542 Int_t currentChamber = extrapTrackParamAtCh.GetClusterPtr()->GetChamberId();
543 AliMUONTrackExtrap::AddMCSEffect(&extrapTrackParamAtCh,AliMUONConstants::ChamberThicknessInX0(currentChamber),-1.);
9bf6860b 544
545 // reset propagator for smoother
a0dc65b4 546 if (GetRecoParam()->UseSmoother()) extrapTrackParamAtCh.ResetPropagator();
9bf6860b 547
548 // Add MCS in the missing chamber(s) if any
9bf6860b 549 while (currentChamber > nextChamber + 1) {
550 // extrapolation to the missing chamber
551 currentChamber--;
4ea3f013 552 if (!AliMUONTrackExtrap::ExtrapToZCov(&extrapTrackParamAtCh, AliMUONConstants::DefaultChamberZ(currentChamber),
553 GetRecoParam()->UseSmoother())) return kFALSE;
9bf6860b 554 // add MCS effect
4663da9f 555 AliMUONTrackExtrap::AddMCSEffect(&extrapTrackParamAtCh,AliMUONConstants::ChamberThicknessInX0(currentChamber),-1.);
9bf6860b 556 }
557
558 //Extrapolate trackCandidate to chamber
4ea3f013 559 if (!AliMUONTrackExtrap::ExtrapToZCov(&extrapTrackParamAtCh, AliMUONConstants::DefaultChamberZ(nextChamber),
560 GetRecoParam()->UseSmoother())) return kFALSE;
9bf6860b 561
562 // Printout for debuging
563 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 2) || (AliLog::GetGlobalDebugLevel() >= 2)) {
564 cout<<endl<<"Track parameters and covariances at first cluster extrapolated to z = "<<AliMUONConstants::DefaultChamberZ(nextChamber)<<":"<<endl;
565 extrapTrackParamAtCh.GetParameters().Print();
566 extrapTrackParamAtCh.GetCovariances().Print();
567 }
568
569 // Printout for debuging
570 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
571 cout << "FollowTrackInChamber: look for clusters in chamber(1..): " << nextChamber+1 << endl;
572 }
573
574 // Ask the clustering to reconstruct new clusters around the track position in the current chamber
575 // except for station 4 and 5 that are already entirely clusterized
a0dc65b4 576 if (GetRecoParam()->CombineClusterTrackReco()) {
9bf6860b 577 if (nextChamber < 6) AskForNewClustersInChamber(extrapTrackParamAtCh, clusterStore, nextChamber);
578 }
579
580 // Create iterators to loop over clusters in both chambers
581 TIter next(clusterStore.CreateChamberIterator(nextChamber,nextChamber));
582
583 // look for candidates in chamber
584 while ( ( cluster = static_cast<AliMUONVCluster*>(next()) ) ) {
585
586 // try to add the current cluster fast
587 if (!TryOneClusterFast(extrapTrackParamAtCh, cluster)) continue;
588
589 // try to add the current cluster accuratly
590 chi2OfCluster = TryOneCluster(extrapTrackParamAtCh, cluster, extrapTrackParamAtCluster,
a0dc65b4 591 GetRecoParam()->UseSmoother());
9bf6860b 592
593 // if good chi2 then consider to add cluster
594 if (chi2OfCluster < maxChi2OfCluster) {
9bf6860b 595
a0dc65b4 596 if (GetRecoParam()->UseSmoother()) {
9bf6860b 597 // save extrapolated parameters for smoother
598 extrapTrackParamAtCluster.SetExtrapParameters(extrapTrackParamAtCluster.GetParameters());
599
600 // save extrapolated covariance matrix for smoother
601 extrapTrackParamAtCluster.SetExtrapCovariances(extrapTrackParamAtCluster.GetCovariances());
602 }
603
604 // Compute new track parameters including new cluster using kalman filter
605 addChi2TrackAtCluster = RunKalmanFilter(extrapTrackParamAtCluster);
606
6b191dea 607 // skip track out of limits
608 if (!IsAcceptable(extrapTrackParamAtCluster)) continue;
89c8d66d 609
610 // remember a cluster was found
611 foundOneCluster = kTRUE;
612
613 // Printout for debuging
614 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
615 cout << "FollowTrackInChamber: found one cluster in chamber(1..): " << nextChamber+1
616 << " (Chi2 = " << chi2OfCluster << ")" << endl;
617 cluster->Print();
618 }
9bf6860b 619
a0dc65b4 620 if (GetRecoParam()->TrackAllTracks()) {
9bf6860b 621 // copy trackCandidate into a new track put at the end of fRecTracksPtr and add the new cluster
622 newTrack = new ((*fRecTracksPtr)[fRecTracksPtr->GetLast()+1]) AliMUONTrack(trackCandidate);
623 UpdateTrack(*newTrack,extrapTrackParamAtCluster,addChi2TrackAtCluster);
624 fNRecTracks++;
625
626 // Printout for debuging
627 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
628 cout << "FollowTrackInChamber: added one cluster in chamber(1..): " << nextChamber+1 << endl;
629 if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
630 }
631
632 } else if (addChi2TrackAtCluster < bestAddChi2TrackAtCluster) {
633 // keep track of the best cluster
634 bestAddChi2TrackAtCluster = addChi2TrackAtCluster;
635 bestTrackParamAtCluster = extrapTrackParamAtCluster;
636 }
637
638 }
639
640 }
641
642 // fill out the best track if required else clean up the fRecTracksPtr array
a0dc65b4 643 if (!GetRecoParam()->TrackAllTracks()) {
9bf6860b 644 if (foundOneCluster) {
645 UpdateTrack(trackCandidate,bestTrackParamAtCluster,bestAddChi2TrackAtCluster);
646
647 // Printout for debuging
648 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
649 cout << "FollowTrackInChamber: added the best cluster in chamber(1..): " << bestTrackParamAtCluster.GetClusterPtr()->GetChamberId()+1 << endl;
650 if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
651 }
652
653 } else return kFALSE;
654
655 } else if (foundOneCluster) {
656
657 // remove obsolete track
658 fRecTracksPtr->Remove(&trackCandidate);
659 fNRecTracks--;
660
661 } else return kFALSE;
662
663 return kTRUE;
664
665}
666
667 //__________________________________________________________________________
1fef78c8 668Bool_t AliMUONTrackReconstructorK::FollowTrackInStation(AliMUONTrack &trackCandidate, AliMUONVClusterStore& clusterStore, Int_t nextStation)
ea94c18b 669{
96ebe67e 670 /// Follow trackCandidate in station(0..) nextStation and search for compatible cluster(s)
ea94c18b 671 /// Keep all possibilities or only the best one(s) according to the flag fgkTrackAllTracks:
672 /// kTRUE: duplicate "trackCandidate" if there are several possibilities and add the new tracks at the end of
673 /// fRecTracksPtr to avoid conficts with other track candidates at this current stage of the tracking procedure.
674 /// Remove the obsolete "trackCandidate" at the end.
96ebe67e 675 /// kFALSE: add only the best cluster(s) to the "trackCandidate". Try to add a couple of clusters in priority.
676 /// return kTRUE if new cluster(s) have been found (otherwise return kFALSE)
ea94c18b 677 AliDebug(1,Form("Enter FollowTrackInStation(1..) %d", nextStation+1));
678
679 // Order the chamber according to the propagation direction (tracking starts with chamber 2):
680 // - nextStation == station(1...) 5 => forward propagation
681 // - nextStation < station(1...) 5 => backward propagation
682 Int_t ch1, ch2;
683 if (nextStation==4) {
684 ch1 = 2*nextStation+1;
685 ch2 = 2*nextStation;
686 } else {
687 ch1 = 2*nextStation;
688 ch2 = 2*nextStation+1;
689 }
690
96ebe67e 691 Double_t chi2OfCluster;
a0dc65b4 692 Double_t maxChi2OfCluster = 2. * GetRecoParam()->GetSigmaCutForTracking() *
693 GetRecoParam()->GetSigmaCutForTracking(); // 2 because 2 quantities in chi2
96ebe67e 694 Double_t addChi2TrackAtCluster1;
695 Double_t addChi2TrackAtCluster2;
4ea3f013 696 Double_t bestAddChi2TrackAtCluster1 = AliMUONTrack::MaxChi2();
697 Double_t bestAddChi2TrackAtCluster2 = AliMUONTrack::MaxChi2();
96ebe67e 698 Bool_t foundOneCluster = kFALSE;
699 Bool_t foundTwoClusters = kFALSE;
ea94c18b 700 AliMUONTrack *newTrack = 0x0;
96ebe67e 701 AliMUONVCluster *clusterCh1, *clusterCh2;
ea94c18b 702 AliMUONTrackParam extrapTrackParam;
7332f213 703 AliMUONTrackParam extrapTrackParamAtCh;
96ebe67e 704 AliMUONTrackParam extrapTrackParamAtCluster1;
705 AliMUONTrackParam extrapTrackParamAtCluster2;
706 AliMUONTrackParam bestTrackParamAtCluster1;
707 AliMUONTrackParam bestTrackParamAtCluster2;
708
7332f213 709 // Get track parameters according to the propagation direction
7332f213 710 if (nextStation==4) extrapTrackParamAtCh = *(AliMUONTrackParam*)trackCandidate.GetTrackParamAtCluster()->Last();
711 else extrapTrackParamAtCh = *(AliMUONTrackParam*)trackCandidate.GetTrackParamAtCluster()->First();
ea94c18b 712
9bf6860b 713 // Printout for debuging
714 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 2) || (AliLog::GetGlobalDebugLevel() >= 2)) {
715 cout<<endl<<"Track parameters and covariances at first cluster:"<<endl;
716 extrapTrackParamAtCh.GetParameters().Print();
717 extrapTrackParamAtCh.GetCovariances().Print();
718 }
719
ea94c18b 720 // Add MCS effect
4663da9f 721 Int_t currentChamber = extrapTrackParamAtCh.GetClusterPtr()->GetChamberId();
722 AliMUONTrackExtrap::AddMCSEffect(&extrapTrackParamAtCh,AliMUONConstants::ChamberThicknessInX0(currentChamber),-1.);
ea94c18b 723
724 // reset propagator for smoother
a0dc65b4 725 if (GetRecoParam()->UseSmoother()) extrapTrackParamAtCh.ResetPropagator();
ea94c18b 726
9bf6860b 727 // Add MCS in the missing chamber(s) if any
9bf6860b 728 while (ch1 < ch2 && currentChamber > ch2 + 1) {
ea94c18b 729 // extrapolation to the missing chamber
9bf6860b 730 currentChamber--;
4ea3f013 731 if (!AliMUONTrackExtrap::ExtrapToZCov(&extrapTrackParamAtCh, AliMUONConstants::DefaultChamberZ(currentChamber),
732 GetRecoParam()->UseSmoother())) return kFALSE;
ea94c18b 733 // add MCS effect
4663da9f 734 AliMUONTrackExtrap::AddMCSEffect(&extrapTrackParamAtCh,AliMUONConstants::ChamberThicknessInX0(currentChamber),-1.);
ea94c18b 735 }
736
737 //Extrapolate trackCandidate to chamber "ch2"
4ea3f013 738 if (!AliMUONTrackExtrap::ExtrapToZCov(&extrapTrackParamAtCh, AliMUONConstants::DefaultChamberZ(ch2),
739 GetRecoParam()->UseSmoother())) return kFALSE;
ea94c18b 740
741 // Printout for debuging
742 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 2) || (AliLog::GetGlobalDebugLevel() >= 2)) {
9bf6860b 743 cout<<endl<<"Track parameters and covariances at first cluster extrapolated to z = "<<AliMUONConstants::DefaultChamberZ(ch2)<<":"<<endl;
744 extrapTrackParamAtCh.GetParameters().Print();
019df241 745 extrapTrackParamAtCh.GetCovariances().Print();
ea94c18b 746 }
747
748 // Printout for debuging
749 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
96ebe67e 750 cout << "FollowTrackInStation: look for clusters in chamber(1..): " << ch2+1 << endl;
ea94c18b 751 }
752
1fef78c8 753 // Ask the clustering to reconstruct new clusters around the track position in the current station
754 // except for station 4 and 5 that are already entirely clusterized
a0dc65b4 755 if (GetRecoParam()->CombineClusterTrackReco()) {
1fef78c8 756 if (nextStation < 3) AskForNewClustersInStation(extrapTrackParamAtCh, clusterStore, nextStation);
757 }
758
4ea3f013 759 Int_t nClusters = clusterStore.GetSize();
760 Bool_t *clusterCh1Used = new Bool_t[nClusters];
761 for (Int_t i = 0; i < nClusters; i++) clusterCh1Used[i] = kFALSE;
762 Int_t iCluster1;
763
96ebe67e 764 // Create iterators to loop over clusters in both chambers
765 TIter nextInCh1(clusterStore.CreateChamberIterator(ch1,ch1));
766 TIter nextInCh2(clusterStore.CreateChamberIterator(ch2,ch2));
767
768 // look for candidates in chamber 2
769 while ( ( clusterCh2 = static_cast<AliMUONVCluster*>(nextInCh2()) ) ) {
ea94c18b 770
96ebe67e 771 // try to add the current cluster fast
772 if (!TryOneClusterFast(extrapTrackParamAtCh, clusterCh2)) continue;
019df241 773
96ebe67e 774 // try to add the current cluster accuratly
775 chi2OfCluster = TryOneCluster(extrapTrackParamAtCh, clusterCh2, extrapTrackParamAtCluster2,
a0dc65b4 776 GetRecoParam()->UseSmoother());
ea94c18b 777
96ebe67e 778 // if good chi2 then try to attach a cluster in the other chamber too
779 if (chi2OfCluster < maxChi2OfCluster) {
ea94c18b 780
a0dc65b4 781 if (GetRecoParam()->UseSmoother()) {
ea94c18b 782 // save extrapolated parameters for smoother
96ebe67e 783 extrapTrackParamAtCluster2.SetExtrapParameters(extrapTrackParamAtCluster2.GetParameters());
ea94c18b 784
785 // save extrapolated covariance matrix for smoother
96ebe67e 786 extrapTrackParamAtCluster2.SetExtrapCovariances(extrapTrackParamAtCluster2.GetCovariances());
ea94c18b 787 }
788
96ebe67e 789 // Compute new track parameters including "clusterCh2" using kalman filter
790 addChi2TrackAtCluster2 = RunKalmanFilter(extrapTrackParamAtCluster2);
ea94c18b 791
6b191dea 792 // skip track out of limits
793 if (!IsAcceptable(extrapTrackParamAtCluster2)) continue;
89c8d66d 794
795 // Printout for debuging
796 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
797 cout << "FollowTrackInStation: found one cluster in chamber(1..): " << ch2+1
798 << " (Chi2 = " << chi2OfCluster << ")" << endl;
799 clusterCh2->Print();
800 cout << " look for second clusters in chamber(1..): " << ch1+1 << " ..." << endl;
801 }
802
ea94c18b 803 // copy new track parameters for next step
96ebe67e 804 extrapTrackParam = extrapTrackParamAtCluster2;
ea94c18b 805
806 // add MCS effect
4663da9f 807 AliMUONTrackExtrap::AddMCSEffect(&extrapTrackParam,AliMUONConstants::ChamberThicknessInX0(ch2),-1.);
ea94c18b 808
809 // reset propagator for smoother
a0dc65b4 810 if (GetRecoParam()->UseSmoother()) extrapTrackParam.ResetPropagator();
ea94c18b 811
019df241 812 //Extrapolate track parameters to chamber "ch1"
4ea3f013 813 Bool_t normalExtrap = AliMUONTrackExtrap::ExtrapToZCov(&extrapTrackParam, AliMUONConstants::DefaultChamberZ(ch1),
814 GetRecoParam()->UseSmoother());
019df241 815
96ebe67e 816 // reset cluster iterator of chamber 1
817 nextInCh1.Reset();
818 iCluster1 = -1;
819
820 // look for second candidates in chamber 1
89c8d66d 821 Bool_t foundSecondCluster = kFALSE;
4ea3f013 822 if (normalExtrap) while ( ( clusterCh1 = static_cast<AliMUONVCluster*>(nextInCh1()) ) ) {
96ebe67e 823 iCluster1++;
ea94c18b 824
96ebe67e 825 // try to add the current cluster fast
826 if (!TryOneClusterFast(extrapTrackParam, clusterCh1)) continue;
019df241 827
96ebe67e 828 // try to add the current cluster accuratly
829 chi2OfCluster = TryOneCluster(extrapTrackParam, clusterCh1, extrapTrackParamAtCluster1,
a0dc65b4 830 GetRecoParam()->UseSmoother());
019df241 831
96ebe67e 832 // if good chi2 then consider to add the 2 clusters to the "trackCandidate"
833 if (chi2OfCluster < maxChi2OfCluster) {
019df241 834
a0dc65b4 835 if (GetRecoParam()->UseSmoother()) {
ea94c18b 836 // save extrapolated parameters for smoother
96ebe67e 837 extrapTrackParamAtCluster1.SetExtrapParameters(extrapTrackParamAtCluster1.GetParameters());
ea94c18b 838
839 // save extrapolated covariance matrix for smoother
96ebe67e 840 extrapTrackParamAtCluster1.SetExtrapCovariances(extrapTrackParamAtCluster1.GetCovariances());
ea94c18b 841 }
842
96ebe67e 843 // Compute new track parameters including "clusterCh1" using kalman filter
844 addChi2TrackAtCluster1 = RunKalmanFilter(extrapTrackParamAtCluster1);
ea94c18b 845
6b191dea 846 // skip track out of limits
847 if (!IsAcceptable(extrapTrackParamAtCluster1)) continue;
89c8d66d 848
849 // remember a second cluster was found
850 foundSecondCluster = kTRUE;
851 foundTwoClusters = kTRUE;
852
853 // Printout for debuging
854 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
855 cout << "FollowTrackInStation: found one cluster in chamber(1..): " << ch1+1
856 << " (Chi2 = " << chi2OfCluster << ")" << endl;
857 clusterCh1->Print();
858 }
9bf6860b 859
a0dc65b4 860 if (GetRecoParam()->TrackAllTracks()) {
96ebe67e 861 // copy trackCandidate into a new track put at the end of fRecTracksPtr and add the new clusters
ea94c18b 862 newTrack = new ((*fRecTracksPtr)[fRecTracksPtr->GetLast()+1]) AliMUONTrack(trackCandidate);
96ebe67e 863 UpdateTrack(*newTrack,extrapTrackParamAtCluster1,extrapTrackParamAtCluster2,addChi2TrackAtCluster1,addChi2TrackAtCluster2);
ea94c18b 864 fNRecTracks++;
865
96ebe67e 866 // Tag clusterCh1 as used
867 clusterCh1Used[iCluster1] = kTRUE;
ea94c18b 868
869 // Printout for debuging
870 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
96ebe67e 871 cout << "FollowTrackInStation: added two clusters in station(1..): " << nextStation+1 << endl;
ea94c18b 872 if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
873 }
874
96ebe67e 875 } else if (addChi2TrackAtCluster1+addChi2TrackAtCluster2 < bestAddChi2TrackAtCluster1+bestAddChi2TrackAtCluster2) {
876 // keep track of the best couple of clusters
877 bestAddChi2TrackAtCluster1 = addChi2TrackAtCluster1;
878 bestAddChi2TrackAtCluster2 = addChi2TrackAtCluster2;
879 bestTrackParamAtCluster1 = extrapTrackParamAtCluster1;
880 bestTrackParamAtCluster2 = extrapTrackParamAtCluster2;
ea94c18b 881 }
882
b0e4c598 883 }
ea94c18b 884
8cc77df0 885 }
ea94c18b 886
96ebe67e 887 // if no clusterCh1 found then consider to add clusterCh2 only
888 if (!foundSecondCluster) {
89c8d66d 889
890 // remember a cluster was found
96ebe67e 891 foundOneCluster = kTRUE;
ea94c18b 892
a0dc65b4 893 if (GetRecoParam()->TrackAllTracks()) {
96ebe67e 894 // copy trackCandidate into a new track put at the end of fRecTracksPtr and add the new cluster
ea94c18b 895 newTrack = new ((*fRecTracksPtr)[fRecTracksPtr->GetLast()+1]) AliMUONTrack(trackCandidate);
96ebe67e 896 UpdateTrack(*newTrack,extrapTrackParamAtCluster2,addChi2TrackAtCluster2);
ea94c18b 897 fNRecTracks++;
898
ea94c18b 899 // Printout for debuging
900 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
96ebe67e 901 cout << "FollowTrackInStation: added one cluster in chamber(1..): " << ch2+1 << endl;
ea94c18b 902 if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
903 }
904
96ebe67e 905 } else if (!foundTwoClusters && addChi2TrackAtCluster2 < bestAddChi2TrackAtCluster1) {
906 // keep track of the best single cluster except if a couple of clusters has already been found
907 bestAddChi2TrackAtCluster1 = addChi2TrackAtCluster2;
908 bestTrackParamAtCluster1 = extrapTrackParamAtCluster2;
ea94c18b 909 }
910
911 }
912
8cc77df0 913 }
ea94c18b 914
915 }
916
917 // look for candidates in chamber 1 not already attached to a track
96ebe67e 918 // if we want to keep all possible tracks or if no good couple of clusters has been found
a0dc65b4 919 if (GetRecoParam()->TrackAllTracks() || !foundTwoClusters) {
ea94c18b 920
ea94c18b 921 // add MCS effect for next step
4663da9f 922 AliMUONTrackExtrap::AddMCSEffect(&extrapTrackParamAtCh,AliMUONConstants::ChamberThicknessInX0(ch2),-1.);
019df241 923
924 //Extrapolate trackCandidate to chamber "ch1"
4ea3f013 925 Bool_t normalExtrap = AliMUONTrackExtrap::ExtrapToZCov(&extrapTrackParamAtCh, AliMUONConstants::DefaultChamberZ(ch1),
926 GetRecoParam()->UseSmoother());
019df241 927
9bf6860b 928 // Printout for debuging
929 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 2) || (AliLog::GetGlobalDebugLevel() >= 2)) {
930 cout<<endl<<"Track parameters and covariances at first cluster extrapolated to z = "<<AliMUONConstants::DefaultChamberZ(ch1)<<":"<<endl;
931 extrapTrackParamAtCh.GetParameters().Print();
932 extrapTrackParamAtCh.GetCovariances().Print();
933 }
934
935 // Printout for debuging
936 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
937 cout << "FollowTrackInStation: look for single clusters in chamber(1..): " << ch1+1 << endl;
938 }
939
96ebe67e 940 // reset cluster iterator of chamber 1
941 nextInCh1.Reset();
942 iCluster1 = -1;
943
944 // look for second candidates in chamber 1
4ea3f013 945 if (normalExtrap) while ( ( clusterCh1 = static_cast<AliMUONVCluster*>(nextInCh1()) ) ) {
96ebe67e 946 iCluster1++;
ea94c18b 947
96ebe67e 948 if (clusterCh1Used[iCluster1]) continue; // Skip clusters already used
ea94c18b 949
96ebe67e 950 // try to add the current cluster fast
951 if (!TryOneClusterFast(extrapTrackParamAtCh, clusterCh1)) continue;
019df241 952
96ebe67e 953 // try to add the current cluster accuratly
954 chi2OfCluster = TryOneCluster(extrapTrackParamAtCh, clusterCh1, extrapTrackParamAtCluster1,
a0dc65b4 955 GetRecoParam()->UseSmoother());
019df241 956
96ebe67e 957 // if good chi2 then consider to add clusterCh1
958 // We do not try to attach a cluster in the other chamber too since it has already been done above
959 if (chi2OfCluster < maxChi2OfCluster) {
019df241 960
a0dc65b4 961 if (GetRecoParam()->UseSmoother()) {
ea94c18b 962 // save extrapolated parameters for smoother
96ebe67e 963 extrapTrackParamAtCluster1.SetExtrapParameters(extrapTrackParamAtCluster1.GetParameters());
ea94c18b 964
965 // save extrapolated covariance matrix for smoother
96ebe67e 966 extrapTrackParamAtCluster1.SetExtrapCovariances(extrapTrackParamAtCluster1.GetCovariances());
ea94c18b 967 }
968
96ebe67e 969 // Compute new track parameters including "clusterCh1" using kalman filter
970 addChi2TrackAtCluster1 = RunKalmanFilter(extrapTrackParamAtCluster1);
ea94c18b 971
6b191dea 972 // skip track out of limits
973 if (!IsAcceptable(extrapTrackParamAtCluster1)) continue;
89c8d66d 974
975 // remember a cluster was found
976 foundOneCluster = kTRUE;
977
978 // Printout for debuging
979 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
980 cout << "FollowTrackInStation: found one cluster in chamber(1..): " << ch1+1
981 << " (Chi2 = " << chi2OfCluster << ")" << endl;
982 clusterCh1->Print();
983 }
9bf6860b 984
a0dc65b4 985 if (GetRecoParam()->TrackAllTracks()) {
96ebe67e 986 // copy trackCandidate into a new track put at the end of fRecTracksPtr and add the new cluster
ea94c18b 987 newTrack = new ((*fRecTracksPtr)[fRecTracksPtr->GetLast()+1]) AliMUONTrack(trackCandidate);
96ebe67e 988 UpdateTrack(*newTrack,extrapTrackParamAtCluster1,addChi2TrackAtCluster1);
ea94c18b 989 fNRecTracks++;
990
ea94c18b 991 // Printout for debuging
992 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
96ebe67e 993 cout << "FollowTrackInStation: added one cluster in chamber(1..): " << ch1+1 << endl;
ea94c18b 994 if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
995 }
996
96ebe67e 997 } else if (addChi2TrackAtCluster1 < bestAddChi2TrackAtCluster1) {
998 // keep track of the best single cluster except if a couple of clusters has already been found
999 bestAddChi2TrackAtCluster1 = addChi2TrackAtCluster1;
1000 bestTrackParamAtCluster1 = extrapTrackParamAtCluster1;
ea94c18b 1001 }
1002
1003 }
1004
8cc77df0 1005 }
ea94c18b 1006
1007 }
1008
1009 // fill out the best track if required else clean up the fRecTracksPtr array
a0dc65b4 1010 if (!GetRecoParam()->TrackAllTracks()) {
96ebe67e 1011 if (foundTwoClusters) {
1012 UpdateTrack(trackCandidate,bestTrackParamAtCluster1,bestTrackParamAtCluster2,bestAddChi2TrackAtCluster1,bestAddChi2TrackAtCluster2);
ea94c18b 1013
ea94c18b 1014 // Printout for debuging
1015 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
96ebe67e 1016 cout << "FollowTrackInStation: added the two best clusters in station(1..): " << nextStation+1 << endl;
ea94c18b 1017 if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
1018 }
1019
96ebe67e 1020 } else if (foundOneCluster) {
1021 UpdateTrack(trackCandidate,bestTrackParamAtCluster1,bestAddChi2TrackAtCluster1);
ea94c18b 1022
ea94c18b 1023 // Printout for debuging
1024 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
96ebe67e 1025 cout << "FollowTrackInStation: added the best cluster in chamber(1..): " << bestTrackParamAtCluster1.GetClusterPtr()->GetChamberId()+1 << endl;
ea94c18b 1026 if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
1027 }
1028
17773e2e 1029 } else {
96ebe67e 1030 delete [] clusterCh1Used;
17773e2e 1031 return kFALSE;
1032 }
9bf6860b 1033
96ebe67e 1034 } else if (foundOneCluster || foundTwoClusters) {
ea94c18b 1035
1036 // remove obsolete track
1037 fRecTracksPtr->Remove(&trackCandidate);
1038 fNRecTracks--;
1039
17773e2e 1040 } else {
96ebe67e 1041 delete [] clusterCh1Used;
17773e2e 1042 return kFALSE;
9bf6860b 1043 }
1044
96ebe67e 1045 delete [] clusterCh1Used;
ea94c18b 1046 return kTRUE;
1047
1048}
8cc77df0 1049
ea94c18b 1050 //__________________________________________________________________________
96ebe67e 1051Double_t AliMUONTrackReconstructorK::RunKalmanFilter(AliMUONTrackParam &trackParamAtCluster)
ea94c18b 1052{
96ebe67e 1053 /// Compute new track parameters and their covariances including new cluster using kalman filter
ea94c18b 1054 /// return the additional track chi2
1055 AliDebug(1,"Enter RunKalmanFilter");
1056
1057 // Get actual track parameters (p)
96ebe67e 1058 TMatrixD param(trackParamAtCluster.GetParameters());
ea94c18b 1059
96ebe67e 1060 // Get new cluster parameters (m)
1061 AliMUONVCluster *cluster = trackParamAtCluster.GetClusterPtr();
1062 TMatrixD clusterParam(5,1);
1063 clusterParam.Zero();
1064 clusterParam(0,0) = cluster->GetX();
1065 clusterParam(2,0) = cluster->GetY();
ea94c18b 1066
1067 // Compute the actual parameter weight (W)
96ebe67e 1068 TMatrixD paramWeight(trackParamAtCluster.GetCovariances());
ea94c18b 1069 if (paramWeight.Determinant() != 0) {
1070 paramWeight.Invert();
1071 } else {
1072 AliWarning(" Determinant = 0");
4ea3f013 1073 return 2.*AliMUONTrack::MaxChi2();
8cc77df0 1074 }
ea94c18b 1075
96ebe67e 1076 // Compute the new cluster weight (U)
1077 TMatrixD clusterWeight(5,5);
1078 clusterWeight.Zero();
1079 clusterWeight(0,0) = 1. / cluster->GetErrX2();
1080 clusterWeight(2,2) = 1. / cluster->GetErrY2();
8cc77df0 1081
ea94c18b 1082 // Compute the new parameters covariance matrix ( (W+U)^-1 )
96ebe67e 1083 TMatrixD newParamCov(paramWeight,TMatrixD::kPlus,clusterWeight);
ea94c18b 1084 if (newParamCov.Determinant() != 0) {
1085 newParamCov.Invert();
1086 } else {
1087 AliWarning(" Determinant = 0");
4ea3f013 1088 return 2.*AliMUONTrack::MaxChi2();
ea94c18b 1089 }
1090
1091 // Save the new parameters covariance matrix
96ebe67e 1092 trackParamAtCluster.SetCovariances(newParamCov);
ea94c18b 1093
1094 // Compute the new parameters (p' = ((W+U)^-1)U(m-p) + p)
96ebe67e 1095 TMatrixD tmp(clusterParam,TMatrixD::kMinus,param);
1096 TMatrixD tmp2(clusterWeight,TMatrixD::kMult,tmp); // U(m-p)
ea94c18b 1097 TMatrixD newParam(newParamCov,TMatrixD::kMult,tmp2); // ((W+U)^-1)U(m-p)
1098 newParam += param; // ((W+U)^-1)U(m-p) + p
1099
1100 // Save the new parameters
96ebe67e 1101 trackParamAtCluster.SetParameters(newParam);
ea94c18b 1102
1103 // Compute the additional chi2 (= ((p'-p)^-1)W(p'-p) + ((p'-m)^-1)U(p'-m))
1104 tmp = newParam; // p'
1105 tmp -= param; // (p'-p)
1106 TMatrixD tmp3(paramWeight,TMatrixD::kMult,tmp); // W(p'-p)
1107 TMatrixD addChi2Track(tmp,TMatrixD::kTransposeMult,tmp3); // ((p'-p)^-1)W(p'-p)
1108 tmp = newParam; // p'
96ebe67e 1109 tmp -= clusterParam; // (p'-m)
1110 TMatrixD tmp4(clusterWeight,TMatrixD::kMult,tmp); // U(p'-m)
ea94c18b 1111 addChi2Track += TMatrixD(tmp,TMatrixD::kTransposeMult,tmp4); // ((p'-p)^-1)W(p'-p) + ((p'-m)^-1)U(p'-m)
1112
1113 return addChi2Track(0,0);
1114
8cc77df0 1115}
1116
ea94c18b 1117 //__________________________________________________________________________
96ebe67e 1118void AliMUONTrackReconstructorK::UpdateTrack(AliMUONTrack &track, AliMUONTrackParam &trackParamAtCluster, Double_t addChi2)
8cc77df0 1119{
96ebe67e 1120 /// Add 1 cluster to the track candidate
ea94c18b 1121 /// Update chi2 of the track
1122
9bf6860b 1123 // Flag cluster as being (not) removable
a0dc65b4 1124 if (GetRecoParam()->RequestStation(trackParamAtCluster.GetClusterPtr()->GetChamberId()/2))
9bf6860b 1125 trackParamAtCluster.SetRemovable(kFALSE);
1126 else trackParamAtCluster.SetRemovable(kTRUE);
96ebe67e 1127 trackParamAtCluster.SetLocalChi2(0.); // --> Local chi2 not used
5f4ceff2 1128
96ebe67e 1129 // Update the track chi2 into trackParamAtCluster
1130 trackParamAtCluster.SetTrackChi2(track.GetGlobalChi2() + addChi2);
ea94c18b 1131
1132 // Update the chi2 of the new track
96ebe67e 1133 track.SetGlobalChi2(trackParamAtCluster.GetTrackChi2());
ea94c18b 1134
96ebe67e 1135 // Update array of TrackParamAtCluster
1136 track.AddTrackParamAtCluster(trackParamAtCluster,*(trackParamAtCluster.GetClusterPtr()));
ea94c18b 1137
1138}
8cc77df0 1139
ea94c18b 1140 //__________________________________________________________________________
96ebe67e 1141void AliMUONTrackReconstructorK::UpdateTrack(AliMUONTrack &track, AliMUONTrackParam &trackParamAtCluster1, AliMUONTrackParam &trackParamAtCluster2,
1142 Double_t addChi2AtCluster1, Double_t addChi2AtCluster2)
ea94c18b 1143{
96ebe67e 1144 /// Add 2 clusters to the track candidate (order is important)
ea94c18b 1145 /// Update track and local chi2
1146
96ebe67e 1147 // Update local chi2 at first cluster
1148 AliMUONVCluster* cluster1 = trackParamAtCluster1.GetClusterPtr();
1149 Double_t deltaX = trackParamAtCluster1.GetNonBendingCoor() - cluster1->GetX();
1150 Double_t deltaY = trackParamAtCluster1.GetBendingCoor() - cluster1->GetY();
1151 Double_t localChi2AtCluster1 = deltaX*deltaX / cluster1->GetErrX2() +
1152 deltaY*deltaY / cluster1->GetErrY2();
1153 trackParamAtCluster1.SetLocalChi2(localChi2AtCluster1);
ea94c18b 1154
96ebe67e 1155 // Flag first cluster as being removable
1156 trackParamAtCluster1.SetRemovable(kTRUE);
ea94c18b 1157
96ebe67e 1158 // Update local chi2 at second cluster
1159 AliMUONVCluster* cluster2 = trackParamAtCluster2.GetClusterPtr();
1160 AliMUONTrackParam extrapTrackParamAtCluster2(trackParamAtCluster1);
1161 AliMUONTrackExtrap::ExtrapToZ(&extrapTrackParamAtCluster2, trackParamAtCluster2.GetZ());
1162 deltaX = extrapTrackParamAtCluster2.GetNonBendingCoor() - cluster2->GetX();
1163 deltaY = extrapTrackParamAtCluster2.GetBendingCoor() - cluster2->GetY();
1164 Double_t localChi2AtCluster2 = deltaX*deltaX / cluster2->GetErrX2() +
1165 deltaY*deltaY / cluster2->GetErrY2();
1166 trackParamAtCluster2.SetLocalChi2(localChi2AtCluster2);
ea94c18b 1167
96ebe67e 1168 // Flag second cluster as being removable
1169 trackParamAtCluster2.SetRemovable(kTRUE);
ea94c18b 1170
96ebe67e 1171 // Update the track chi2 into trackParamAtCluster2
1172 trackParamAtCluster2.SetTrackChi2(track.GetGlobalChi2() + addChi2AtCluster2);
ea94c18b 1173
96ebe67e 1174 // Update the track chi2 into trackParamAtCluster1
1175 trackParamAtCluster1.SetTrackChi2(trackParamAtCluster2.GetTrackChi2() + addChi2AtCluster1);
ea94c18b 1176
1177 // Update the chi2 of the new track
96ebe67e 1178 track.SetGlobalChi2(trackParamAtCluster1.GetTrackChi2());
ea94c18b 1179
96ebe67e 1180 // Update array of trackParamAtCluster
1181 track.AddTrackParamAtCluster(trackParamAtCluster1,*cluster1);
1182 track.AddTrackParamAtCluster(trackParamAtCluster2,*cluster2);
ea94c18b 1183
1184}
8cc77df0 1185
ea94c18b 1186 //__________________________________________________________________________
1fef78c8 1187Bool_t AliMUONTrackReconstructorK::RecoverTrack(AliMUONTrack &trackCandidate, AliMUONVClusterStore& clusterStore, Int_t nextStation)
ea94c18b 1188{
1189 /// Try to recover the track candidate in the next station
96ebe67e 1190 /// by removing the worst of the two clusters attached in the current station
ea94c18b 1191 /// Return kTRUE if recovering succeeds
1192 AliDebug(1,"Enter RecoverTrack");
1193
96ebe67e 1194 // Do not try to recover track until we have attached cluster(s) on station(1..) 3
ea94c18b 1195 if (nextStation > 1) return kFALSE;
1196
96ebe67e 1197 Int_t worstClusterNumber = -1;
9bf6860b 1198 Double_t localChi2, worstLocalChi2 = -1.;
ea94c18b 1199
96ebe67e 1200 // Look for the cluster to remove
1201 for (Int_t clusterNumber = 0; clusterNumber < 2; clusterNumber++) {
1202 AliMUONTrackParam *trackParamAtCluster = (AliMUONTrackParam*)trackCandidate.GetTrackParamAtCluster()->UncheckedAt(clusterNumber);
ea94c18b 1203
9bf6860b 1204 // check if current cluster is in the previous station
1205 if (trackParamAtCluster->GetClusterPtr()->GetChamberId()/2 != nextStation+1) break;
1206
96ebe67e 1207 // check if current cluster is removable
1208 if (!trackParamAtCluster->IsRemovable()) return kFALSE;
ea94c18b 1209
9bf6860b 1210 // reset the current cluster as being not removable if it is on a required station
a0dc65b4 1211 if (GetRecoParam()->RequestStation(nextStation+1)) trackParamAtCluster->SetRemovable(kFALSE);
9bf6860b 1212
96ebe67e 1213 // Pick up cluster with the worst chi2
1214 localChi2 = trackParamAtCluster->GetLocalChi2();
1215 if (localChi2 > worstLocalChi2) {
1216 worstLocalChi2 = localChi2;
1217 worstClusterNumber = clusterNumber;
8cc77df0 1218 }
ea94c18b 1219 }
1220
9bf6860b 1221 // check if worst cluster found
1222 if (worstClusterNumber < 0) return kFALSE;
ea94c18b 1223
96ebe67e 1224 // Remove the worst cluster
1225 trackCandidate.RemoveTrackParamAtCluster((AliMUONTrackParam*)trackCandidate.GetTrackParamAtCluster()->UncheckedAt(worstClusterNumber));
ea94c18b 1226
96ebe67e 1227 // Re-calculate track parameters at the (new) first cluster
4ea3f013 1228 if (!RetracePartialTrack(trackCandidate,(AliMUONTrackParam*)trackCandidate.GetTrackParamAtCluster()->UncheckedAt(1))) return kFALSE;
ea94c18b 1229
6b191dea 1230 // skip track out of limits
1231 if (!IsAcceptable(*((AliMUONTrackParam*)trackCandidate.GetTrackParamAtCluster()->First()))) return kFALSE;
9bf6860b 1232
96ebe67e 1233 // Look for new cluster(s) in next station
1234 return FollowTrackInStation(trackCandidate, clusterStore, nextStation);
ea94c18b 1235
8cc77df0 1236}
1237
1238 //__________________________________________________________________________
ea94c18b 1239Bool_t AliMUONTrackReconstructorK::RunSmoother(AliMUONTrack &track)
8cc77df0 1240{
ea94c18b 1241 /// Compute new track parameters and their covariances using smoother
3304fa09 1242 AliDebug(1,"Enter UseSmoother");
ea94c18b 1243
96ebe67e 1244 AliMUONTrackParam *previousTrackParam = (AliMUONTrackParam*) track.GetTrackParamAtCluster()->First();
ea94c18b 1245
96ebe67e 1246 // Smoothed parameters and covariances at first cluster = filtered parameters and covariances
ea94c18b 1247 previousTrackParam->SetSmoothParameters(previousTrackParam->GetParameters());
1248 previousTrackParam->SetSmoothCovariances(previousTrackParam->GetCovariances());
d1e6fdee 1249
1250 AliMUONTrackParam *currentTrackParam = (AliMUONTrackParam*) track.GetTrackParamAtCluster()->After(previousTrackParam);
ea94c18b 1251
d1e6fdee 1252 // Save local chi2 at first cluster = last additional chi2 provided by Kalman
1253 previousTrackParam->SetLocalChi2(previousTrackParam->GetTrackChi2() - currentTrackParam->GetTrackChi2());
ea94c18b 1254
ff078154 1255 // if the track contains only 2 clusters simply copy the filtered parameters
1256 if (track.GetNClusters() == 2) {
1257 currentTrackParam->SetSmoothParameters(currentTrackParam->GetParameters());
1258 currentTrackParam->SetSmoothCovariances(currentTrackParam->GetCovariances());
1259 currentTrackParam->SetLocalChi2(currentTrackParam->GetTrackChi2());
1260 return kTRUE;
1261 }
1262
ea94c18b 1263 while (currentTrackParam) {
1264
1265 // Get variables
1266 const TMatrixD &extrapParameters = previousTrackParam->GetExtrapParameters(); // X(k+1 k)
1267 const TMatrixD &filteredParameters = currentTrackParam->GetParameters(); // X(k k)
1268 const TMatrixD &previousSmoothParameters = previousTrackParam->GetSmoothParameters(); // X(k+1 n)
1269 const TMatrixD &propagator = previousTrackParam->GetPropagator(); // F(k)
1270 const TMatrixD &extrapCovariances = previousTrackParam->GetExtrapCovariances(); // C(k+1 k)
1271 const TMatrixD &filteredCovariances = currentTrackParam->GetCovariances(); // C(k k)
1272 const TMatrixD &previousSmoothCovariances = previousTrackParam->GetSmoothCovariances(); // C(k+1 n)
1273
d1e6fdee 1274 // Compute smoother gain: A(k) = C(kk) * F(k)^t * (C(k+1 k))^-1
ea94c18b 1275 TMatrixD extrapWeight(extrapCovariances);
1276 if (extrapWeight.Determinant() != 0) {
1277 extrapWeight.Invert(); // (C(k+1 k))^-1
1278 } else {
1279 AliWarning(" Determinant = 0");
1280 return kFALSE;
1281 }
d1e6fdee 1282 TMatrixD smootherGain(filteredCovariances,TMatrixD::kMultTranspose,propagator); // C(kk) * F(k)^t
1283 smootherGain *= extrapWeight; // C(kk) * F(k)^t * (C(k+1 k))^-1
ea94c18b 1284
1285 // Compute smoothed parameters: X(k n) = X(k k) + A(k) * (X(k+1 n) - X(k+1 k))
1286 TMatrixD tmpParam(previousSmoothParameters,TMatrixD::kMinus,extrapParameters); // X(k+1 n) - X(k+1 k)
1287 TMatrixD smoothParameters(smootherGain,TMatrixD::kMult,tmpParam); // A(k) * (X(k+1 n) - X(k+1 k))
1288 smoothParameters += filteredParameters; // X(k k) + A(k) * (X(k+1 n) - X(k+1 k))
1289
1290 // Save smoothed parameters
1291 currentTrackParam->SetSmoothParameters(smoothParameters);
1292
1293 // Compute smoothed covariances: C(k n) = C(k k) + A(k) * (C(k+1 n) - C(k+1 k)) * (A(k))^t
1294 TMatrixD tmpCov(previousSmoothCovariances,TMatrixD::kMinus,extrapCovariances); // C(k+1 n) - C(k+1 k)
1295 TMatrixD tmpCov2(tmpCov,TMatrixD::kMultTranspose,smootherGain); // (C(k+1 n) - C(k+1 k)) * (A(k))^t
1296 TMatrixD smoothCovariances(smootherGain,TMatrixD::kMult,tmpCov2); // A(k) * (C(k+1 n) - C(k+1 k)) * (A(k))^t
1297 smoothCovariances += filteredCovariances; // C(k k) + A(k) * (C(k+1 n) - C(k+1 k)) * (A(k))^t
1298
1299 // Save smoothed covariances
1300 currentTrackParam->SetSmoothCovariances(smoothCovariances);
1301
96ebe67e 1302 // Compute smoothed residual: r(k n) = cluster - X(k n)
d1e6fdee 1303 AliMUONVCluster* cluster = currentTrackParam->GetClusterPtr();
ea94c18b 1304 TMatrixD smoothResidual(2,1);
1305 smoothResidual.Zero();
96ebe67e 1306 smoothResidual(0,0) = cluster->GetX() - smoothParameters(0,0);
1307 smoothResidual(1,0) = cluster->GetY() - smoothParameters(2,0);
ea94c18b 1308
96ebe67e 1309 // Compute weight of smoothed residual: W(k n) = (clusterCov - C(k n))^-1
ea94c18b 1310 TMatrixD smoothResidualWeight(2,2);
96ebe67e 1311 smoothResidualWeight(0,0) = cluster->GetErrX2() - smoothCovariances(0,0);
ea94c18b 1312 smoothResidualWeight(0,1) = - smoothCovariances(0,2);
1313 smoothResidualWeight(1,0) = - smoothCovariances(2,0);
96ebe67e 1314 smoothResidualWeight(1,1) = cluster->GetErrY2() - smoothCovariances(2,2);
ea94c18b 1315 if (smoothResidualWeight.Determinant() != 0) {
1316 smoothResidualWeight.Invert();
1317 } else {
1318 AliWarning(" Determinant = 0");
1319 return kFALSE;
1320 }
1321
1322 // Compute local chi2 = (r(k n))^t * W(k n) * r(k n)
1323 TMatrixD tmpChi2(smoothResidual,TMatrixD::kTransposeMult,smoothResidualWeight); // (r(k n))^t * W(k n)
1324 TMatrixD localChi2(tmpChi2,TMatrixD::kMult,smoothResidual); // (r(k n))^t * W(k n) * r(k n)
1325
1326 // Save local chi2
1327 currentTrackParam->SetLocalChi2(localChi2(0,0));
1328
1329 previousTrackParam = currentTrackParam;
96ebe67e 1330 currentTrackParam = (AliMUONTrackParam*) track.GetTrackParamAtCluster()->After(previousTrackParam);
ea94c18b 1331 }
1332
1333 return kTRUE;
1334
8cc77df0 1335}
1336
1337 //__________________________________________________________________________
89c8d66d 1338Bool_t AliMUONTrackReconstructorK::ComplementTracks(const AliMUONVClusterStore& clusterStore)
b709ac13 1339{
1340 /// Complete tracks by adding missing clusters (if there is an overlap between
89c8d66d 1341 /// two detection elements, the track may have two clusters in the same chamber).
1342 /// Recompute track parameters and covariances at each clusters.
4ea3f013 1343 /// Remove tracks getting abnormal (i.e. extrapolation failed...) after being complemented.
1344 /// Return kTRUE if one or more tracks have been complemented or removed.
b709ac13 1345 AliDebug(1,"Enter ComplementTracks");
1346
1347 Int_t chamberId, detElemId;
96ebe67e 1348 Double_t chi2OfCluster, addChi2TrackAtCluster, bestAddChi2TrackAtCluster;
a0dc65b4 1349 Double_t maxChi2OfCluster = 2. * GetRecoParam()->GetSigmaCutForTracking() *
1350 GetRecoParam()->GetSigmaCutForTracking(); // 2 because 2 quantities in chi2
89c8d66d 1351 Bool_t foundOneCluster, trackModified, hasChanged = kFALSE;
96ebe67e 1352 AliMUONVCluster *cluster;
1353 AliMUONTrackParam *trackParam, *previousTrackParam, *nextTrackParam, trackParamAtCluster, bestTrackParamAtCluster;
4ea3f013 1354 AliMUONTrack *nextTrack;
b709ac13 1355
b709ac13 1356 AliMUONTrack *track = (AliMUONTrack*) fRecTracksPtr->First();
1357 while (track) {
1358 trackModified = kFALSE;
1359
96ebe67e 1360 trackParam = (AliMUONTrackParam*)track->GetTrackParamAtCluster()->First();
b709ac13 1361 previousTrackParam = trackParam;
1362 while (trackParam) {
96ebe67e 1363 foundOneCluster = kFALSE;
4ea3f013 1364 bestAddChi2TrackAtCluster = AliMUONTrack::MaxChi2();
96ebe67e 1365 chamberId = trackParam->GetClusterPtr()->GetChamberId();
1366 detElemId = trackParam->GetClusterPtr()->GetDetElemId();
b709ac13 1367
1368 // prepare nextTrackParam before adding new cluster because of the sorting
96ebe67e 1369 nextTrackParam = (AliMUONTrackParam*)track->GetTrackParamAtCluster()->After(trackParam);
b709ac13 1370
96ebe67e 1371 // Create iterators to loop over clusters in current chamber
1372 TIter nextInCh(clusterStore.CreateChamberIterator(chamberId,chamberId));
b709ac13 1373
1374 // look for one second candidate in the same chamber
96ebe67e 1375 while ( ( cluster = static_cast<AliMUONVCluster*>(nextInCh()) ) ) {
b709ac13 1376
1377 // look for a cluster in another detection element
96ebe67e 1378 if (cluster->GetDetElemId() == detElemId) continue;
b709ac13 1379
96ebe67e 1380 // try to add the current cluster fast
1381 if (!TryOneClusterFast(*trackParam, cluster)) continue;
b709ac13 1382
96ebe67e 1383 // try to add the current cluster accurately
b709ac13 1384 // never use track parameters at last cluster because the covariance matrix is meaningless
96ebe67e 1385 if (nextTrackParam) chi2OfCluster = TryOneCluster(*trackParam, cluster, trackParamAtCluster);
1386 else chi2OfCluster = TryOneCluster(*previousTrackParam, cluster, trackParamAtCluster);
b709ac13 1387
1388 // if good chi2 then consider to add this cluster to the track
96ebe67e 1389 if (chi2OfCluster < maxChi2OfCluster) {
b709ac13 1390
96ebe67e 1391 // Compute local track parameters including current cluster using kalman filter
1392 addChi2TrackAtCluster = RunKalmanFilter(trackParamAtCluster);
b709ac13 1393
1394 // keep track of the best cluster
96ebe67e 1395 if (addChi2TrackAtCluster < bestAddChi2TrackAtCluster) {
1396 bestAddChi2TrackAtCluster = addChi2TrackAtCluster;
1397 bestTrackParamAtCluster = trackParamAtCluster;
1398 foundOneCluster = kTRUE;
b709ac13 1399 }
1400
1401 }
1402
1403 }
1404
1405 // add new cluster if any
96ebe67e 1406 if (foundOneCluster) {
0e894e58 1407
1408 // Printout for debuging
1409 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
1410 cout << "ComplementTracks: found one cluster in chamber(1..): " << chamberId+1 << endl;
1411 bestTrackParamAtCluster.GetClusterPtr()->Print();
ad3c6eda 1412 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 2) || (AliLog::GetGlobalDebugLevel() >= 2)) {
1413 cout<<endl<<"Track parameters and covariances at cluster:"<<endl;
1414 bestTrackParamAtCluster.GetParameters().Print();
1415 bestTrackParamAtCluster.GetCovariances().Print();
1416 }
0e894e58 1417 }
1418
7332f213 1419 trackParam->SetRemovable(kTRUE);
1420 bestTrackParamAtCluster.SetRemovable(kTRUE);
1421 track->AddTrackParamAtCluster(bestTrackParamAtCluster,*(bestTrackParamAtCluster.GetClusterPtr()));
b709ac13 1422 trackModified = kTRUE;
89c8d66d 1423 hasChanged = kTRUE;
b709ac13 1424 }
1425
1426 previousTrackParam = trackParam;
1427 trackParam = nextTrackParam;
1428 }
1429
4ea3f013 1430 // prepare next track
1431 nextTrack = (AliMUONTrack*) fRecTracksPtr->After(track);
1432
b709ac13 1433 // re-compute track parameters using kalman filter if needed
4ea3f013 1434 if (trackModified && !RetraceTrack(*track,kTRUE)) {
1435 AliWarning("track modified but problem occur during refitting --> remove track");
1436 fRecTracksPtr->Remove(track);
1437 fNRecTracks--;
1438 }
b709ac13 1439
4ea3f013 1440 track = nextTrack;
b709ac13 1441 }
1442
89c8d66d 1443 return hasChanged;
1444
b709ac13 1445}
1446
1447//__________________________________________________________________________
7332f213 1448void AliMUONTrackReconstructorK::ImproveTrack(AliMUONTrack &track)
8cc77df0 1449{
7332f213 1450 /// Improve the given track by removing removable clusters with local chi2 highter than the defined cut
1451 /// Removable clusters are identified by the method AliMUONTrack::TagRemovableClusters()
ea94c18b 1452 /// Recompute track parameters and covariances at the remaining clusters
4ea3f013 1453 /// and if something goes wrong (i.e. extrapolation failed...) set track chi2 to max value
7332f213 1454 AliDebug(1,"Enter ImproveTrack");
ea94c18b 1455
1456 Double_t localChi2, worstLocalChi2;
ff078154 1457 AliMUONTrackParam *trackParamAtCluster, *worstTrackParamAtCluster, *nextTrackParam, *next2nextTrackParam;
1458 Int_t nextChamber, next2nextChamber;
ea94c18b 1459 Bool_t smoothed;
a0dc65b4 1460 Double_t sigmaCut2 = GetRecoParam()->GetSigmaCutForImprovement() *
1461 GetRecoParam()->GetSigmaCutForImprovement();
ea94c18b 1462
7332f213 1463 while (!track.IsImproved()) {
ea94c18b 1464
7332f213 1465 // identify removable clusters
a0dc65b4 1466 track.TagRemovableClusters(GetRecoParam()->RequestedStationMask());
b709ac13 1467
7332f213 1468 // Run smoother if required
1469 smoothed = kFALSE;
a0dc65b4 1470 if (GetRecoParam()->UseSmoother()) smoothed = RunSmoother(track);
7332f213 1471
1472 // Use standard procedure to compute local chi2 if smoother not required or not working
1473 if (!smoothed) {
ea94c18b 1474
7332f213 1475 // Update track parameters and covariances
4ea3f013 1476 if (!track.UpdateCovTrackParamAtCluster()) {
1477 AliWarning("unable to update track parameters and covariances --> stop improvement");
1478 // restore the kalman parameters
1479 RetraceTrack(track,kTRUE);
1480 break;
1481 }
ea94c18b 1482
7332f213 1483 // Compute local chi2 of each clusters
1484 track.ComputeLocalChi2(kTRUE);
1485 }
1486
1487 // Look for the cluster to remove
1488 worstTrackParamAtCluster = 0x0;
ff078154 1489 worstLocalChi2 = -1.;
7332f213 1490 trackParamAtCluster = (AliMUONTrackParam*)track.GetTrackParamAtCluster()->First();
1491 while (trackParamAtCluster) {
ea94c18b 1492
7332f213 1493 // save parameters into smooth parameters in case of smoother did not work properly
a0dc65b4 1494 if (GetRecoParam()->UseSmoother() && !smoothed) {
7332f213 1495 trackParamAtCluster->SetSmoothParameters(trackParamAtCluster->GetParameters());
1496 trackParamAtCluster->SetSmoothCovariances(trackParamAtCluster->GetCovariances());
ea94c18b 1497 }
1498
7332f213 1499 // Pick up cluster with the worst chi2
1500 localChi2 = trackParamAtCluster->GetLocalChi2();
1501 if (localChi2 > worstLocalChi2) {
1502 worstLocalChi2 = localChi2;
1503 worstTrackParamAtCluster = trackParamAtCluster;
ea94c18b 1504 }
1505
7332f213 1506 trackParamAtCluster = (AliMUONTrackParam*)track.GetTrackParamAtCluster()->After(trackParamAtCluster);
1507 }
1508
7332f213 1509 // Check whether the worst chi2 is under requirement or not
1510 if (worstLocalChi2 < 2. * sigmaCut2) { // 2 because 2 quantities in chi2
1511 track.SetImproved(kTRUE);
1512 break;
1513 }
1514
1515 // if the worst cluster is not removable then stop improvement
ff078154 1516 if (!worstTrackParamAtCluster->IsRemovable()) {
1517 // restore the kalman parameters in case they have been lost
1518 if (!smoothed) RetraceTrack(track,kTRUE);
1519 break;
1520 }
7332f213 1521
1522 // get track parameters at cluster next to the one to be removed
1523 nextTrackParam = (AliMUONTrackParam*) track.GetTrackParamAtCluster()->After(worstTrackParamAtCluster);
1524
1525 // Remove the worst cluster
1526 track.RemoveTrackParamAtCluster(worstTrackParamAtCluster);
1527
1528 // Re-calculate track parameters
1529 // - from the cluster immediately downstream the one suppressed
1530 // - or from the begining - if parameters have been re-computed using the standard method (kalman parameters have been lost)
ff078154 1531 // - or if the removed cluster was used to compute the tracking seed
4ea3f013 1532 Bool_t normalExtrap;
ff078154 1533 if (smoothed && nextTrackParam) {
1534
1535 nextChamber = nextTrackParam->GetClusterPtr()->GetChamberId();
1536 next2nextTrackParam = nextTrackParam;
1537 do {
1538
1539 next2nextChamber = next2nextTrackParam->GetClusterPtr()->GetChamberId();
1540 next2nextTrackParam = (AliMUONTrackParam*) track.GetTrackParamAtCluster()->After(next2nextTrackParam);
1541
1542 } while (next2nextTrackParam && (next2nextChamber == nextChamber));
1543
4ea3f013 1544 if (next2nextChamber == nextChamber) normalExtrap = RetraceTrack(track,kTRUE);
1545 else normalExtrap = RetracePartialTrack(track,nextTrackParam);
ff078154 1546
4ea3f013 1547 } else normalExtrap = RetraceTrack(track,kTRUE);
1548
1549 // stop in case of extrapolation problem
1550 if (!normalExtrap) {
1551 AliWarning("track partially improved but problem occur during refitting --> stop improvement");
1552 break;
1553 }
7332f213 1554
1555 // Printout for debuging
1556 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
1557 cout << "ImproveTracks: track " << fRecTracksPtr->IndexOf(&track)+1 << " improved " << endl;
ea94c18b 1558 }
1559
ea94c18b 1560 }
1561
8cc77df0 1562}
1563
7332f213 1564//__________________________________________________________________________
4ea3f013 1565Bool_t AliMUONTrackReconstructorK::FinalizeTrack(AliMUONTrack &track)
b0e4c598 1566{
96ebe67e 1567 /// Update track parameters and covariances at each attached cluster
7332f213 1568 /// using smoother if required, if not already done
4ea3f013 1569 /// return kFALSE if the track cannot be extrapolated uo to the last chamber
b709ac13 1570
96ebe67e 1571 AliMUONTrackParam *trackParamAtCluster;
ea94c18b 1572 Bool_t smoothed = kFALSE;
1573
7332f213 1574 // update track parameters (using smoother if required) if not already done
4ea3f013 1575 if (track.IsImproved()) smoothed = GetRecoParam()->UseSmoother();
1576 else {
a0dc65b4 1577 if (GetRecoParam()->UseSmoother()) smoothed = RunSmoother(track);
d1e6fdee 1578 if (!smoothed) {
4ea3f013 1579 if (track.UpdateCovTrackParamAtCluster()) track.ComputeLocalChi2(kTRUE);
1580 else {
1581 AliWarning("finalization failed due to extrapolation problem");
1582 return kFALSE;
1583 }
d1e6fdee 1584 }
4ea3f013 1585 }
7332f213 1586
1587 // copy smoothed parameters and covariances if any
1588 if (smoothed) {
ea94c18b 1589
7332f213 1590 trackParamAtCluster = (AliMUONTrackParam*) (track.GetTrackParamAtCluster()->First());
1591 while (trackParamAtCluster) {
ea94c18b 1592
7332f213 1593 trackParamAtCluster->SetParameters(trackParamAtCluster->GetSmoothParameters());
1594 trackParamAtCluster->SetCovariances(trackParamAtCluster->GetSmoothCovariances());
ea94c18b 1595
7332f213 1596 trackParamAtCluster = (AliMUONTrackParam*) (track.GetTrackParamAtCluster()->After(trackParamAtCluster));
ea94c18b 1597 }
1598
b0e4c598 1599 }
7332f213 1600
4ea3f013 1601 return kTRUE;
1602
b0e4c598 1603}
ea94c18b 1604
7deb8eb0 1605 //__________________________________________________________________________
b1fea02e 1606Bool_t AliMUONTrackReconstructorK::RefitTrack(AliMUONTrack &track, Bool_t enableImprovement)
7deb8eb0 1607{
1608 /// re-fit the given track
89c8d66d 1609 AliDebug(1,"Enter RefitTrack");
7deb8eb0 1610
ff078154 1611 // check validity of the track (i.e. at least 2 chambers hit on stations 4 and 5)
1612 if (!track.IsValid(0)) {
1613 AliWarning("the track is not valid --> unable to refit");
7deb8eb0 1614 return kFALSE;
1615 }
1616
1617 // re-compute track parameters and covariances using Kalman filter
4ea3f013 1618 if (!RetraceTrack(track,kTRUE)) {
1619 AliWarning("bad track refitting due to extrapolation failure");
1620 return kFALSE;
1621 }
7deb8eb0 1622
1623 // Improve the reconstructed tracks if required
b1fea02e 1624 track.SetImproved(kFALSE);
a0dc65b4 1625 if (enableImprovement && GetRecoParam()->ImproveTracks()) ImproveTrack(track);
7deb8eb0 1626
1627 // Fill AliMUONTrack data members
4ea3f013 1628 if (track.GetGlobalChi2() < AliMUONTrack::MaxChi2()) return FinalizeTrack(track);
1629 else {
1630 AliWarning("track not finalized due to extrapolation failure");
1631 return kFALSE;
1632 }
7deb8eb0 1633
1634}
1635