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