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