]> git.uio.no Git - u/mrichter/AliRoot.git/blame - MUON/AliMUONTrackReconstructor.cxx
Removing clustering from here as it is now driven by the tracking, whatever the track...
[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"
33#include "AliMUONVClusterStore.h"
a9e2aefa 34#include "AliMUONTrack.h"
37827b29 35#include "AliMUONTrackParam.h"
36#include "AliMUONTrackExtrap.h"
b709ac13 37
8c343c7c 38#include "AliLog.h"
af743f54 39
208f139e 40#include <TMinuit.h>
af743f54 41#include <Riostream.h>
ea94c18b 42#include <TMath.h>
af743f54 43#include <TMatrixD.h>
a9e2aefa 44
de2cd600 45// Functions to be minimized with Minuit
ea94c18b 46void TrackChi2(Int_t &nParam, Double_t *gradient, Double_t &chi2, Double_t *param, Int_t flag);
de2cd600 47
7945aae7 48/// \cond CLASSIMP
29f1b13a 49ClassImp(AliMUONTrackReconstructor) // Class implementation in ROOT context
7945aae7 50/// \endcond
a9e2aefa 51
019df241 52 //__________________________________________________________________________
7ec3b9cf 53AliMUONTrackReconstructor::AliMUONTrackReconstructor()
54 : AliMUONVTrackReconstructor()
a9e2aefa 55{
96ebe67e 56 /// Constructor
a9e2aefa 57}
9cbdf048 58
019df241 59 //__________________________________________________________________________
ea94c18b 60AliMUONTrackReconstructor::~AliMUONTrackReconstructor()
a9e2aefa 61{
ea94c18b 62/// Destructor
63}
276c44b7 64
de2cd600 65 //__________________________________________________________________________
96ebe67e 66void AliMUONTrackReconstructor::MakeTrackCandidates(const AliMUONVClusterStore& clusterStore)
de2cd600 67{
019df241 68 /// To make track candidates (assuming linear propagation if the flag fgkMakeTrackCandidatesFast is set to kTRUE):
208f139e 69 /// Start with segments station(1..) 4 or 5 then follow track in station 5 or 4.
96ebe67e 70 /// Good candidates are made of at least three clusters.
208f139e 71 /// Keep only best candidates or all of them according to the flag fgkTrackAllTracks.
b709ac13 72
208f139e 73 TClonesArray *segments;
208f139e 74 AliMUONTrack *track;
208f139e 75 Int_t iCandidate = 0;
96ebe67e 76 Bool_t clusterFound;
208f139e 77
de2cd600 78 AliDebug(1,"Enter MakeTrackCandidates");
de2cd600 79
208f139e 80 // Loop over stations(1..) 5 and 4 and make track candidates
ea94c18b 81 for (Int_t istat=4; istat>=3; istat--) {
82
208f139e 83 // Make segments in the station
96ebe67e 84 segments = MakeSegmentsInStation(clusterStore,istat);
ea94c18b 85
208f139e 86 // Loop over segments
7ec3b9cf 87 for (Int_t iseg=0; iseg<segments->GetEntriesFast(); iseg++)
88 {
208f139e 89 AliDebug(1,Form("Making primary candidate(1..) %d",++iCandidate));
ea94c18b 90
208f139e 91 // Transform segments to tracks and put them at the end of fRecTracksPtr
ea94c18b 92 track = new ((*fRecTracksPtr)[fRecTracksPtr->GetLast()+1]) AliMUONTrack((AliMUONObjectPair*)((*segments)[iseg]));
a9e2aefa 93 fNRecTracks++;
ea94c18b 94
208f139e 95 // Printout for debuging
96ebe67e 96 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 2) || (AliLog::GetGlobalDebugLevel() >= 2)) {
97 cout<<endl<<"Track parameter covariances at first cluster:"<<endl;
98 ((AliMUONTrackParam*) track->GetTrackParamAtCluster()->First())->GetCovariances().Print();
208f139e 99 }
ea94c18b 100
96ebe67e 101 // Look for compatible cluster(s) in the other station
102 if (AliMUONReconstructor::GetRecoParam()->MakeTrackCandidatesFast())
103 clusterFound = FollowLinearTrackInStation(*track, clusterStore, 7-istat);
104 else clusterFound = FollowTrackInStation(*track, clusterStore, 7-istat);
019df241 105
96ebe67e 106 // Remove track if no cluster found
107 if (!clusterFound) {
ea94c18b 108 fRecTracksPtr->Remove(track);
109 fNRecTracks--;
110 }
111
a9e2aefa 112 }
ea94c18b 113
208f139e 114 // delete the array of segments
115 delete segments;
116 }
ea94c18b 117
208f139e 118 fRecTracksPtr->Compress(); // this is essential before checking tracks
119
120 // Keep all different tracks or only the best ones as required
3304fa09 121 if (AliMUONReconstructor::GetRecoParam()->TrackAllTracks()) RemoveIdenticalTracks();
208f139e 122 else RemoveDoubleTracks();
123
124 AliDebug(1,Form("Number of good candidates = %d",fNRecTracks));
125
a9e2aefa 126}
127
128 //__________________________________________________________________________
96ebe67e 129void AliMUONTrackReconstructor::FollowTracks(const AliMUONVClusterStore& clusterStore)
a9e2aefa 130{
2457f726 131 /// Follow tracks in stations(1..) 3, 2 and 1
208f139e 132 AliDebug(1,"Enter FollowTracks");
133
04b5ea16 134 AliMUONTrack *track, *nextTrack;
b709ac13 135 AliMUONTrackParam *trackParam, *nextTrackParam;
af743f54 136 Int_t currentNRecTracks;
96ebe67e 137 Bool_t clusterFound;
208f139e 138
3304fa09 139 Double_t sigmaCut2 = AliMUONReconstructor::GetRecoParam()->GetSigmaCutForTracking() *
140 AliMUONReconstructor::GetRecoParam()->GetSigmaCutForTracking();
141
208f139e 142 for (Int_t station = 2; station >= 0; station--) {
ea94c18b 143
208f139e 144 // Save the actual number of reconstructed track in case of
145 // tracks are added or suppressed during the tracking procedure
146 // !! Do not compress fRecTracksPtr until the end of the loop over tracks !!
af743f54 147 currentNRecTracks = fNRecTracks;
ea94c18b 148
af743f54 149 for (Int_t iRecTrack = 0; iRecTrack <currentNRecTracks; iRecTrack++) {
208f139e 150 AliDebug(1,Form("FollowTracks: track candidate(1..) %d", iRecTrack+1));
ea94c18b 151
208f139e 152 track = (AliMUONTrack*) fRecTracksPtr->UncheckedAt(iRecTrack);
ea94c18b 153
208f139e 154 // Fit the track:
155 // Do not take into account the multiple scattering to speed up the fit
156 // Calculate the track parameter covariance matrix
157 // If "station" is station(1..) 3 then use the vertex to better constrain the fit
96ebe67e 158 if (station==2) Fit(*track, kFALSE, kTRUE, kTRUE);
159 else Fit(*track, kFALSE, kFALSE, kTRUE);
ea94c18b 160
208f139e 161 // Remove the track if the normalized chi2 is too high
3304fa09 162 if (track->GetNormalizedChi2() > sigmaCut2) {
208f139e 163 fRecTracksPtr->Remove(track);
164 fNRecTracks--;
165 continue;
166 }
ea94c18b 167
b709ac13 168 // save parameters from fit into smoothed parameters to complete track afterward
3304fa09 169 if (AliMUONReconstructor::GetRecoParam()->ComplementTracks()) {
b709ac13 170
171 if (station==2) { // save track parameters on stations 4 and 5
172
173 // extrapolate track parameters and covariances at each cluster
96ebe67e 174 track->UpdateCovTrackParamAtCluster();
b709ac13 175
176 // save them
96ebe67e 177 trackParam = (AliMUONTrackParam*) track->GetTrackParamAtCluster()->First();
b709ac13 178 while (trackParam) {
179 trackParam->SetSmoothParameters(trackParam->GetParameters());
180 trackParam->SetSmoothCovariances(trackParam->GetCovariances());
96ebe67e 181 trackParam = (AliMUONTrackParam*) track->GetTrackParamAtCluster()->After(trackParam);
b709ac13 182 }
183
184 } else { // or save track parameters on last station only
185
186 // save parameters from fit
96ebe67e 187 trackParam = (AliMUONTrackParam*) track->GetTrackParamAtCluster()->First();
b709ac13 188 trackParam->SetSmoothParameters(trackParam->GetParameters());
189 trackParam->SetSmoothCovariances(trackParam->GetCovariances());
190
191 // save parameters extrapolated to the second chamber of the same station if it has been hit
96ebe67e 192 nextTrackParam = (AliMUONTrackParam*) track->GetTrackParamAtCluster()->After(trackParam);
193 if (nextTrackParam->GetClusterPtr()->GetChamberId() < 2*(station+2)) {
b709ac13 194
195 // reset parameters and covariances
196 nextTrackParam->SetParameters(trackParam->GetParameters());
197 nextTrackParam->SetZ(trackParam->GetZ());
198 nextTrackParam->SetCovariances(trackParam->GetCovariances());
199
200 // extrapolate them to the z of the corresponding cluster
96ebe67e 201 AliMUONTrackExtrap::ExtrapToZCov(nextTrackParam, nextTrackParam->GetClusterPtr()->GetZ());
b709ac13 202
203 // save them
204 nextTrackParam->SetSmoothParameters(nextTrackParam->GetParameters());
205 nextTrackParam->SetSmoothCovariances(nextTrackParam->GetCovariances());
206
207 }
208
209 }
210
211 }
212
208f139e 213 // Printout for debuging
214 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 2) || (AliLog::GetGlobalDebugLevel() >= 2)) {
96ebe67e 215 cout<<endl<<"Track parameter covariances at first cluster:"<<endl;
216 ((AliMUONTrackParam*) track->GetTrackParamAtCluster()->First())->GetCovariances().Print();
208f139e 217 }
ea94c18b 218
96ebe67e 219 // Look for compatible cluster(s) in station(0..) "station"
220 clusterFound = FollowTrackInStation(*track, clusterStore, station);
ea94c18b 221
222 // Try to recover track if required
96ebe67e 223 if (!clusterFound && AliMUONReconstructor::GetRecoParam()->RecoverTracks())
224 clusterFound = RecoverTrack(*track, clusterStore, station);
ea94c18b 225
96ebe67e 226 // remove track if no cluster found
227 if (!clusterFound) {
ea94c18b 228 fRecTracksPtr->Remove(track);
229 fNRecTracks--;
230 }
231
208f139e 232 }
ea94c18b 233
208f139e 234 // Compress fRecTracksPtr for the next step
235 fRecTracksPtr->Compress();
ea94c18b 236
208f139e 237 // Keep only the best tracks if required
3304fa09 238 if (!AliMUONReconstructor::GetRecoParam()->TrackAllTracks()) RemoveDoubleTracks();
ea94c18b 239
208f139e 240 }
241
96ebe67e 242 // Last fit of track candidates with all stations
208f139e 243 // Take into account the multiple scattering and remove bad tracks
244 Int_t trackIndex = -1;
04b5ea16 245 track = (AliMUONTrack*) fRecTracksPtr->First();
04b5ea16 246 while (track) {
ea94c18b 247
04b5ea16 248 trackIndex++;
249 nextTrack = (AliMUONTrack*) fRecTracksPtr->After(track); // prepare next track
ea94c18b 250
96ebe67e 251 Fit(*track, kTRUE, kFALSE, kTRUE);
ea94c18b 252
208f139e 253 // Printout for debuging
254 if (AliLog::GetGlobalDebugLevel() >= 3) {
255 cout << "FollowTracks: track candidate(0..) " << trackIndex << " after final fit" << endl;
a9e2aefa 256 track->RecursiveDump();
208f139e 257 }
ea94c18b 258
208f139e 259 // Remove the track if the normalized chi2 is too high
3304fa09 260 if (track->GetNormalizedChi2() > sigmaCut2) {
208f139e 261 fRecTracksPtr->Remove(track);
262 fNRecTracks--;
a9e2aefa 263 }
ea94c18b 264
b709ac13 265 // save parameters from fit into smoothed parameters to complete track afterward
3304fa09 266 if (AliMUONReconstructor::GetRecoParam()->ComplementTracks()) {
b709ac13 267
268 // save parameters from fit
96ebe67e 269 trackParam = (AliMUONTrackParam*) track->GetTrackParamAtCluster()->First();
b709ac13 270 trackParam->SetSmoothParameters(trackParam->GetParameters());
271 trackParam->SetSmoothCovariances(trackParam->GetCovariances());
272
273 // save parameters extrapolated to the second chamber of the same station if it has been hit
96ebe67e 274 nextTrackParam = (AliMUONTrackParam*) track->GetTrackParamAtCluster()->After(trackParam);
275 if (nextTrackParam->GetClusterPtr()->GetChamberId() < 2) {
b709ac13 276
277 // reset parameters and covariances
278 nextTrackParam->SetParameters(trackParam->GetParameters());
279 nextTrackParam->SetZ(trackParam->GetZ());
280 nextTrackParam->SetCovariances(trackParam->GetCovariances());
281
282 // extrapolate them to the z of the corresponding cluster
96ebe67e 283 AliMUONTrackExtrap::ExtrapToZCov(nextTrackParam, nextTrackParam->GetClusterPtr()->GetZ());
b709ac13 284
285 // save them
286 nextTrackParam->SetSmoothParameters(nextTrackParam->GetParameters());
287 nextTrackParam->SetSmoothCovariances(nextTrackParam->GetCovariances());
288
289 }
290
291 }
292
208f139e 293 track = nextTrack;
ea94c18b 294
208f139e 295 }
ea94c18b 296
208f139e 297 fRecTracksPtr->Compress();
298
299}
1a38e749 300
208f139e 301 //__________________________________________________________________________
96ebe67e 302Bool_t AliMUONTrackReconstructor::FollowTrackInStation(AliMUONTrack &trackCandidate, const AliMUONVClusterStore& clusterStore, Int_t nextStation)
208f139e 303{
96ebe67e 304 /// Follow trackCandidate in station(0..) nextStation and search for compatible cluster(s)
208f139e 305 /// Keep all possibilities or only the best one(s) according to the flag fgkTrackAllTracks:
306 /// kTRUE: duplicate "trackCandidate" if there are several possibilities and add the new tracks at the end of
307 /// fRecTracksPtr to avoid conficts with other track candidates at this current stage of the tracking procedure.
308 /// Remove the obsolete "trackCandidate" at the end.
96ebe67e 309 /// kFALSE: add only the best cluster(s) to the "trackCandidate". Try to add a couple of clusters in priority.
208f139e 310 AliDebug(1,Form("Enter FollowTrackInStation(1..) %d", nextStation+1));
311
ea94c18b 312 // Order the chamber according to the propagation direction (tracking starts with chamber 2):
313 // - nextStation == station(1...) 5 => forward propagation
314 // - nextStation < station(1...) 5 => backward propagation
315 Int_t ch1, ch2;
316 if (nextStation==4) {
317 ch1 = 2*nextStation+1;
318 ch2 = 2*nextStation;
319 } else {
320 ch1 = 2*nextStation;
321 ch2 = 2*nextStation+1;
322 }
323
96ebe67e 324 Double_t chi2WithOneCluster = 1.e10;
325 Double_t chi2WithTwoClusters = 1.e10;
326 Double_t maxChi2WithOneCluster = 2. * AliMUONReconstructor::GetRecoParam()->GetSigmaCutForTracking() *
327 AliMUONReconstructor::GetRecoParam()->GetSigmaCutForTracking(); // 2 because 2 quantities in chi2
328 Double_t maxChi2WithTwoClusters = 4. * AliMUONReconstructor::GetRecoParam()->GetSigmaCutForTracking() *
329 AliMUONReconstructor::GetRecoParam()->GetSigmaCutForTracking(); // 4 because 4 quantities in chi2
330 Double_t bestChi2WithOneCluster = maxChi2WithOneCluster;
331 Double_t bestChi2WithTwoClusters = maxChi2WithTwoClusters;
332 Bool_t foundOneCluster = kFALSE;
333 Bool_t foundTwoClusters = kFALSE;
208f139e 334 AliMUONTrack *newTrack = 0x0;
96ebe67e 335 AliMUONVCluster *clusterCh1, *clusterCh2;
019df241 336 AliMUONTrackParam extrapTrackParam;
96ebe67e 337 AliMUONTrackParam extrapTrackParamAtCluster1;
338 AliMUONTrackParam extrapTrackParamAtCluster2;
339 AliMUONTrackParam bestTrackParamAtCluster1;
340 AliMUONTrackParam bestTrackParamAtCluster2;
341
342 Int_t nClusters = clusterStore.GetSize();
343 Bool_t *clusterCh1Used = new Bool_t[nClusters];
344 for (Int_t i = 0; i < nClusters; i++) clusterCh1Used[i] = kFALSE;
345 Int_t iCluster1;
ea94c18b 346
019df241 347 // Get track parameters
96ebe67e 348 AliMUONTrackParam extrapTrackParamAtCh(*(AliMUONTrackParam*)trackCandidate.GetTrackParamAtCluster()->First());
ea94c18b 349
350 // Add MCS effect
019df241 351 AliMUONTrackExtrap::AddMCSEffect(&extrapTrackParamAtCh,AliMUONConstants::ChamberThicknessInX0(),1.);
ea94c18b 352
353 // Add MCS in the missing chamber if any (only 1 chamber can be missing according to tracking criteria)
96ebe67e 354 if (ch1 < ch2 && extrapTrackParamAtCh.GetClusterPtr()->GetChamberId() > ch2 + 1) {
ea94c18b 355 // extrapolation to the missing chamber
019df241 356 AliMUONTrackExtrap::ExtrapToZCov(&extrapTrackParamAtCh, AliMUONConstants::DefaultChamberZ(ch2 + 1));
ea94c18b 357 // add MCS effect
019df241 358 AliMUONTrackExtrap::AddMCSEffect(&extrapTrackParamAtCh,AliMUONConstants::ChamberThicknessInX0(),1.);
ea94c18b 359 }
360
361 //Extrapolate trackCandidate to chamber "ch2"
019df241 362 AliMUONTrackExtrap::ExtrapToZCov(&extrapTrackParamAtCh, AliMUONConstants::DefaultChamberZ(ch2));
ea94c18b 363
208f139e 364 // Printout for debuging
365 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 2) || (AliLog::GetGlobalDebugLevel() >= 2)) {
96ebe67e 366 cout<<endl<<"Track parameter covariances at first cluster extrapolated to z = "<<AliMUONConstants::DefaultChamberZ(ch2)<<":"<<endl;
019df241 367 extrapTrackParamAtCh.GetCovariances().Print();
208f139e 368 }
ea94c18b 369
208f139e 370 // Printout for debuging
371 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
96ebe67e 372 cout << "FollowTrackInStation: look for clusters in chamber(1..): " << ch2+1 << endl;
208f139e 373 }
ea94c18b 374
96ebe67e 375 // Create iterators to loop over clusters in both chambers
376 TIter nextInCh1(clusterStore.CreateChamberIterator(ch1,ch1));
377 TIter nextInCh2(clusterStore.CreateChamberIterator(ch2,ch2));
378
379 // look for candidates in chamber 2
380 while ( ( clusterCh2 = static_cast<AliMUONVCluster*>(nextInCh2()) ) ) {
ea94c18b 381
96ebe67e 382 // try to add the current cluster fast
383 if (!TryOneClusterFast(extrapTrackParamAtCh, clusterCh2)) continue;
019df241 384
96ebe67e 385 // try to add the current cluster accuratly
386 chi2WithOneCluster = TryOneCluster(extrapTrackParamAtCh, clusterCh2, extrapTrackParamAtCluster2);
ea94c18b 387
96ebe67e 388 // if good chi2 then try to attach a cluster in the other chamber too
389 if (chi2WithOneCluster < maxChi2WithOneCluster) {
390 Bool_t foundSecondCluster = kFALSE;
ea94c18b 391
208f139e 392 // Printout for debuging
393 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
96ebe67e 394 cout << "FollowTrackInStation: found one cluster in chamber(1..): " << ch2+1
395 << " (Chi2 = " << chi2WithOneCluster << ")" << endl;
396 cout << " look for second clusters in chamber(1..): " << ch1+1 << " ..." << endl;
a9e2aefa 397 }
ea94c18b 398
399 // add MCS effect for next step
96ebe67e 400 AliMUONTrackExtrap::AddMCSEffect(&extrapTrackParamAtCluster2,AliMUONConstants::ChamberThicknessInX0(),1.);
ea94c18b 401
019df241 402 // copy new track parameters for next step
96ebe67e 403 extrapTrackParam = extrapTrackParamAtCluster2;
019df241 404
405 //Extrapolate track parameters to chamber "ch1"
406 AliMUONTrackExtrap::ExtrapToZ(&extrapTrackParam, AliMUONConstants::DefaultChamberZ(ch1));
407
96ebe67e 408 // reset cluster iterator of chamber 1
409 nextInCh1.Reset();
410 iCluster1 = -1;
411
412 // look for second candidates in chamber 1
413 while ( ( clusterCh1 = static_cast<AliMUONVCluster*>(nextInCh1()) ) ) {
414 iCluster1++;
ea94c18b 415
96ebe67e 416 // try to add the current cluster fast
417 if (!TryOneClusterFast(extrapTrackParam, clusterCh1)) continue;
019df241 418
96ebe67e 419 // try to add the current cluster accuratly
420 chi2WithTwoClusters = TryTwoClusters(extrapTrackParamAtCluster2, clusterCh1, extrapTrackParamAtCluster1);
ea94c18b 421
96ebe67e 422 // if good chi2 then create a new track by adding the 2 clusters to the "trackCandidate"
423 if (chi2WithTwoClusters < maxChi2WithTwoClusters) {
424 foundSecondCluster = kTRUE;
425 foundTwoClusters = kTRUE;
ea94c18b 426
427 // Printout for debuging
428 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
96ebe67e 429 cout << "FollowTrackInStation: found second cluster in chamber(1..): " << ch1+1
430 << " (Global Chi2 = " << chi2WithTwoClusters << ")" << endl;
ea94c18b 431 }
432
3304fa09 433 if (AliMUONReconstructor::GetRecoParam()->TrackAllTracks()) {
96ebe67e 434 // copy trackCandidate into a new track put at the end of fRecTracksPtr and add the new clusters
ea94c18b 435 newTrack = new ((*fRecTracksPtr)[fRecTracksPtr->GetLast()+1]) AliMUONTrack(trackCandidate);
96ebe67e 436 UpdateTrack(*newTrack,extrapTrackParamAtCluster1,extrapTrackParamAtCluster2);
208f139e 437 fNRecTracks++;
ea94c18b 438
96ebe67e 439 // Tag clusterCh1 as used
440 clusterCh1Used[iCluster1] = kTRUE;
ea94c18b 441
208f139e 442 // Printout for debuging
443 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
96ebe67e 444 cout << "FollowTrackInStation: added two clusters in station(1..): " << nextStation+1 << endl;
208f139e 445 if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
a9e2aefa 446 }
ea94c18b 447
96ebe67e 448 } else if (chi2WithTwoClusters < bestChi2WithTwoClusters) {
449 // keep track of the best couple of clusters
450 bestChi2WithTwoClusters = chi2WithTwoClusters;
451 bestTrackParamAtCluster1 = extrapTrackParamAtCluster1;
452 bestTrackParamAtCluster2 = extrapTrackParamAtCluster2;
208f139e 453 }
ea94c18b 454
a9e2aefa 455 }
ea94c18b 456
d0bfce8d 457 }
ea94c18b 458
96ebe67e 459 // if no clusterCh1 found then consider to add clusterCh2 only
460 if (!foundSecondCluster) {
461 foundOneCluster = kTRUE;
ea94c18b 462
3304fa09 463 if (AliMUONReconstructor::GetRecoParam()->TrackAllTracks()) {
96ebe67e 464 // copy trackCandidate into a new track put at the end of fRecTracksPtr and add the new cluster
ea94c18b 465 newTrack = new ((*fRecTracksPtr)[fRecTracksPtr->GetLast()+1]) AliMUONTrack(trackCandidate);
96ebe67e 466 UpdateTrack(*newTrack,extrapTrackParamAtCluster2);
208f139e 467 fNRecTracks++;
ea94c18b 468
208f139e 469 // Printout for debuging
470 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
96ebe67e 471 cout << "FollowTrackInStation: added one cluster in chamber(1..): " << ch2+1 << endl;
208f139e 472 if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
473 }
ea94c18b 474
96ebe67e 475 } else if (!foundTwoClusters && chi2WithOneCluster < bestChi2WithOneCluster) {
476 // keep track of the best single cluster except if a couple of clusters has already been found
477 bestChi2WithOneCluster = chi2WithOneCluster;
478 bestTrackParamAtCluster1 = extrapTrackParamAtCluster2;
208f139e 479 }
ea94c18b 480
d0bfce8d 481 }
ea94c18b 482
208f139e 483 }
ea94c18b 484
208f139e 485 }
ea94c18b 486
208f139e 487 // look for candidates in chamber 1 not already attached to a track
96ebe67e 488 // if we want to keep all possible tracks or if no good couple of clusters has been found
489 if (AliMUONReconstructor::GetRecoParam()->TrackAllTracks() || !foundTwoClusters) {
ea94c18b 490
491 // Printout for debuging
208f139e 492 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
96ebe67e 493 cout << "FollowTrackInStation: look for single clusters in chamber(1..): " << ch1+1 << endl;
208f139e 494 }
ea94c18b 495
496 // add MCS effect for next step
019df241 497 AliMUONTrackExtrap::AddMCSEffect(&extrapTrackParamAtCh,AliMUONConstants::ChamberThicknessInX0(),1.);
498
499 //Extrapolate trackCandidate to chamber "ch1"
500 AliMUONTrackExtrap::ExtrapToZCov(&extrapTrackParamAtCh, AliMUONConstants::DefaultChamberZ(ch1));
501
96ebe67e 502 // reset cluster iterator of chamber 1
503 nextInCh1.Reset();
504 iCluster1 = -1;
505
506 // look for second candidates in chamber 1
507 while ( ( clusterCh1 = static_cast<AliMUONVCluster*>(nextInCh1()) ) ) {
508 iCluster1++;
ea94c18b 509
96ebe67e 510 if (clusterCh1Used[iCluster1]) continue; // Skip cluster already used
ea94c18b 511
96ebe67e 512 // try to add the current cluster fast
513 if (!TryOneClusterFast(extrapTrackParamAtCh, clusterCh1)) continue;
019df241 514
96ebe67e 515 // try to add the current cluster accuratly
516 chi2WithOneCluster = TryOneCluster(extrapTrackParamAtCh, clusterCh1, extrapTrackParamAtCluster1);
ea94c18b 517
96ebe67e 518 // if good chi2 then consider to add clusterCh1
519 // We do not try to attach a cluster in the other chamber too since it has already been done above
520 if (chi2WithOneCluster < maxChi2WithOneCluster) {
521 foundOneCluster = kTRUE;
ea94c18b 522
523 // Printout for debuging
524 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
96ebe67e 525 cout << "FollowTrackInStation: found one cluster in chamber(1..): " << ch1+1
526 << " (Chi2 = " << chi2WithOneCluster << ")" << endl;
ea94c18b 527 }
528
3304fa09 529 if (AliMUONReconstructor::GetRecoParam()->TrackAllTracks()) {
96ebe67e 530 // copy trackCandidate into a new track put at the end of fRecTracksPtr and add the new cluster
ea94c18b 531 newTrack = new ((*fRecTracksPtr)[fRecTracksPtr->GetLast()+1]) AliMUONTrack(trackCandidate);
96ebe67e 532 UpdateTrack(*newTrack,extrapTrackParamAtCluster1);
208f139e 533 fNRecTracks++;
ea94c18b 534
535 // Printout for debuging
208f139e 536 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
96ebe67e 537 cout << "FollowTrackInStation: added one cluster in chamber(1..): " << ch1+1 << endl;
208f139e 538 if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
539 }
ea94c18b 540
96ebe67e 541 } else if (chi2WithOneCluster < bestChi2WithOneCluster) {
542 // keep track of the best single cluster except if a couple of clusters has already been found
543 bestChi2WithOneCluster = chi2WithOneCluster;
544 bestTrackParamAtCluster1 = extrapTrackParamAtCluster1;
208f139e 545 }
ea94c18b 546
d0bfce8d 547 }
ea94c18b 548
208f139e 549 }
ea94c18b 550
208f139e 551 }
ea94c18b 552
208f139e 553 // fill out the best track if required else clean up the fRecTracksPtr array
3304fa09 554 if (!AliMUONReconstructor::GetRecoParam()->TrackAllTracks()) {
96ebe67e 555 if (foundTwoClusters) {
556 UpdateTrack(trackCandidate,bestTrackParamAtCluster1,bestTrackParamAtCluster2);
ea94c18b 557
208f139e 558 // Printout for debuging
559 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
96ebe67e 560 cout << "FollowTrackInStation: added the two best clusters in station(1..): " << nextStation+1 << endl;
208f139e 561 if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
a9e2aefa 562 }
ea94c18b 563
96ebe67e 564 } else if (foundOneCluster) {
565 UpdateTrack(trackCandidate,bestTrackParamAtCluster1);
ea94c18b 566
208f139e 567 // Printout for debuging
568 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
96ebe67e 569 cout << "FollowTrackInStation: added the best cluster in chamber(1..): " << bestTrackParamAtCluster1.GetClusterPtr()->GetChamberId()+1 << endl;
208f139e 570 if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
04b5ea16 571 }
ea94c18b 572
dd230855 573 } else {
96ebe67e 574 delete [] clusterCh1Used;
dd230855 575 return kFALSE;
576 }
ea94c18b 577
96ebe67e 578 } else if (foundOneCluster || foundTwoClusters) {
ea94c18b 579
580 // remove obsolete track
581 fRecTracksPtr->Remove(&trackCandidate);
582 fNRecTracks--;
583
dd230855 584 } else {
96ebe67e 585 delete [] clusterCh1Used;
dd230855 586 return kFALSE;
587 }
ea94c18b 588
96ebe67e 589 delete [] clusterCh1Used;
ea94c18b 590 return kTRUE;
591
592}
593
594 //__________________________________________________________________________
96ebe67e 595Double_t AliMUONTrackReconstructor::TryTwoClusters(const AliMUONTrackParam &trackParamAtCluster1, AliMUONVCluster* cluster2,
596 AliMUONTrackParam &trackParamAtCluster2)
ea94c18b 597{
96ebe67e 598/// Test the compatibility between the track and the 2 clusters together (using trackParam's covariance matrix):
599/// return the corresponding Chi2 accounting for covariances between the 2 clusters
600/// return trackParamAtCluster1 & 2
ea94c18b 601
96ebe67e 602 // extrapolate track parameters at the z position of the second cluster (no need to extrapolate the covariances)
603 trackParamAtCluster2.SetParameters(trackParamAtCluster1.GetParameters());
604 trackParamAtCluster2.SetZ(trackParamAtCluster1.GetZ());
605 AliMUONTrackExtrap::ExtrapToZ(&trackParamAtCluster2, cluster2->GetZ());
ea94c18b 606
96ebe67e 607 // set pointer to cluster2 into trackParamAtCluster2
608 trackParamAtCluster2.SetClusterPtr(cluster2);
ea94c18b 609
96ebe67e 610 // Set differences between track and the 2 clusters in the bending and non bending directions
611 AliMUONVCluster* cluster1 = trackParamAtCluster1.GetClusterPtr();
ea94c18b 612 TMatrixD dPos(4,1);
96ebe67e 613 dPos(0,0) = cluster1->GetX() - trackParamAtCluster1.GetNonBendingCoor();
614 dPos(1,0) = cluster1->GetY() - trackParamAtCluster1.GetBendingCoor();
615 dPos(2,0) = cluster2->GetX() - trackParamAtCluster2.GetNonBendingCoor();
616 dPos(3,0) = cluster2->GetY() - trackParamAtCluster2.GetBendingCoor();
ea94c18b 617
96ebe67e 618 // Calculate the error matrix from the track parameter covariances at first cluster
ea94c18b 619 TMatrixD error(4,4);
620 error.Zero();
96ebe67e 621 if (trackParamAtCluster1.CovariancesExist()) {
622 // Save track parameters at first cluster
623 TMatrixD paramAtCluster1Save(trackParamAtCluster1.GetParameters());
624
625 // Save track coordinates at second cluster
626 Double_t nonBendingCoor2 = trackParamAtCluster2.GetNonBendingCoor();
627 Double_t bendingCoor2 = trackParamAtCluster2.GetBendingCoor();
ea94c18b 628
96ebe67e 629 // copy track parameters at first cluster for jacobian calculation
630 AliMUONTrackParam trackParam(trackParamAtCluster1);
ea94c18b 631
96ebe67e 632 // add MCS effect to the covariance matrix at first cluster
633 AliMUONTrackExtrap::AddMCSEffect(&trackParam,AliMUONConstants::ChamberThicknessInX0(),1.);
ea94c18b 634
96ebe67e 635 // Get the pointer to the parameter covariance matrix at first cluster
636 const TMatrixD& kParamCov = trackParam.GetCovariances();
ea94c18b 637
638 // Calculate the jacobian related to the transformation between track parameters
96ebe67e 639 // at first cluster and track coordinates at the 2 cluster z-positions
ea94c18b 640 TMatrixD jacob(4,5);
641 jacob.Zero();
96ebe67e 642 // first derivative at the first cluster:
ea94c18b 643 jacob(0,0) = 1.; // dx1/dx
644 jacob(1,2) = 1.; // dy1/dy
96ebe67e 645 // first derivative at the second cluster:
ea94c18b 646 TMatrixD dParam(5,1);
647 for (Int_t i=0; i<5; i++) {
648 // Skip jacobian calculation for parameters with no associated error
649 if (kParamCov(i,i) == 0.) continue;
650 // Small variation of parameter i only
651 for (Int_t j=0; j<5; j++) {
652 if (j==i) {
653 dParam(j,0) = TMath::Sqrt(kParamCov(i,i));
96ebe67e 654 if (j == 4) dParam(j,0) *= TMath::Sign(1.,-paramAtCluster1Save(4,0)); // variation always in the same direction
ea94c18b 655 } else dParam(j,0) = 0.;
656 }
657
96ebe67e 658 // Set new track parameters at first cluster
659 trackParam.SetParameters(paramAtCluster1Save);
660 trackParam.AddParameters(dParam);
661 trackParam.SetZ(cluster1->GetZ());
ea94c18b 662
96ebe67e 663 // Extrapolate new track parameters to the z position of the second cluster
664 AliMUONTrackExtrap::ExtrapToZ(&trackParam,cluster2->GetZ());
ea94c18b 665
666 // Calculate the jacobian
96ebe67e 667 jacob(2,i) = (trackParam.GetNonBendingCoor() - nonBendingCoor2) / dParam(i,0); // dx2/dParami
668 jacob(3,i) = (trackParam.GetBendingCoor() - bendingCoor2 ) / dParam(i,0); // dy2/dParami
208f139e 669 }
ea94c18b 670
671 // Calculate the error matrix
672 TMatrixD tmp(jacob,TMatrixD::kMult,kParamCov);
673 error = TMatrixD(tmp,TMatrixD::kMultTranspose,jacob);
674 }
675
96ebe67e 676 // Add cluster resolution to the error matrix
677 error(0,0) += cluster1->GetErrX2();
678 error(1,1) += cluster1->GetErrY2();
679 error(2,2) += cluster2->GetErrX2();
680 error(3,3) += cluster2->GetErrY2();
ea94c18b 681
682 // invert the error matrix for Chi2 calculation
683 if (error.Determinant() != 0) {
684 error.Invert();
208f139e 685 } else {
ea94c18b 686 AliWarning(" Determinant error=0");
687 return 1.e10;
208f139e 688 }
689
ea94c18b 690 // Compute the Chi2 value
691 TMatrixD tmp2(dPos,TMatrixD::kTransposeMult,error);
692 TMatrixD result(tmp2,TMatrixD::kMult,dPos);
693
694 return result(0,0);
695
696}
697
698 //__________________________________________________________________________
96ebe67e 699void AliMUONTrackReconstructor::UpdateTrack(AliMUONTrack &track, AliMUONTrackParam &trackParamAtCluster)
ea94c18b 700{
96ebe67e 701 /// Add 1 cluster to the track candidate
ea94c18b 702 /// Update chi2 of the track
703
704 // Compute local chi2
96ebe67e 705 AliMUONVCluster* cluster = trackParamAtCluster.GetClusterPtr();
706 Double_t deltaX = trackParamAtCluster.GetNonBendingCoor() - cluster->GetX();
707 Double_t deltaY = trackParamAtCluster.GetBendingCoor() - cluster->GetY();
708 Double_t localChi2 = deltaX*deltaX / cluster->GetErrX2() +
709 deltaY*deltaY / cluster->GetErrY2();
ea94c18b 710
96ebe67e 711 // Flag cluster as being not removable
712 trackParamAtCluster.SetRemovable(kFALSE);
713 trackParamAtCluster.SetLocalChi2(0.); // --> Local chi2 not used
5f4ceff2 714
ea94c18b 715 // Update the chi2 of the new track
96ebe67e 716 track.SetGlobalChi2(track.GetGlobalChi2() + localChi2);
ea94c18b 717
96ebe67e 718 // Update TrackParamAtCluster
719 track.AddTrackParamAtCluster(trackParamAtCluster,*cluster);
720 track.GetTrackParamAtCluster()->Sort();
ea94c18b 721
04b5ea16 722}
723
de2cd600 724 //__________________________________________________________________________
96ebe67e 725void AliMUONTrackReconstructor::UpdateTrack(AliMUONTrack &track, AliMUONTrackParam &trackParamAtCluster1, AliMUONTrackParam &trackParamAtCluster2)
ea94c18b 726{
96ebe67e 727 /// Add 2 clusters to the track candidate
ea94c18b 728 /// Update track and local chi2
729
96ebe67e 730 // Update local chi2 at first cluster
731 AliMUONVCluster* cluster1 = trackParamAtCluster1.GetClusterPtr();
732 Double_t deltaX = trackParamAtCluster1.GetNonBendingCoor() - cluster1->GetX();
733 Double_t deltaY = trackParamAtCluster1.GetBendingCoor() - cluster1->GetY();
734 Double_t localChi2AtCluster1 = deltaX*deltaX / cluster1->GetErrX2() +
735 deltaY*deltaY / cluster1->GetErrY2();
736 trackParamAtCluster1.SetLocalChi2(localChi2AtCluster1);
ea94c18b 737
96ebe67e 738 // Flag first cluster as being removable
739 trackParamAtCluster1.SetRemovable(kTRUE);
ea94c18b 740
96ebe67e 741 // Update local chi2 at second cluster
742 AliMUONVCluster* cluster2 = trackParamAtCluster2.GetClusterPtr();
743 deltaX = trackParamAtCluster2.GetNonBendingCoor() - cluster2->GetX();
744 deltaY = trackParamAtCluster2.GetBendingCoor() - cluster2->GetY();
745 Double_t localChi2AtCluster2 = deltaX*deltaX / cluster2->GetErrX2() +
746 deltaY*deltaY / cluster2->GetErrY2();
747 trackParamAtCluster2.SetLocalChi2(localChi2AtCluster2);
ea94c18b 748
96ebe67e 749 // Flag first cluster as being removable
750 trackParamAtCluster2.SetRemovable(kTRUE);
ea94c18b 751
752 // Update the chi2 of the new track
96ebe67e 753 track.SetGlobalChi2(track.GetGlobalChi2() + localChi2AtCluster1 + localChi2AtCluster2);
ea94c18b 754
96ebe67e 755 // Update TrackParamAtCluster
756 track.AddTrackParamAtCluster(trackParamAtCluster1,*cluster1);
757 track.AddTrackParamAtCluster(trackParamAtCluster2,*cluster2);
758 track.GetTrackParamAtCluster()->Sort();
ea94c18b 759
760}
761
762 //__________________________________________________________________________
96ebe67e 763Bool_t AliMUONTrackReconstructor::RecoverTrack(AliMUONTrack &trackCandidate, const AliMUONVClusterStore& clusterStore, Int_t nextStation)
ea94c18b 764{
765 /// Try to recover the track candidate in the next station
96ebe67e 766 /// by removing the worst of the two clusters attached in the current station
ea94c18b 767 /// Return kTRUE if recovering succeeds
768 AliDebug(1,"Enter RecoverTrack");
769
96ebe67e 770 // Do not try to recover track until we have attached cluster(s) on station(1..) 3
ea94c18b 771 if (nextStation > 1) return kFALSE;
772
96ebe67e 773 Int_t worstClusterNumber = -1;
ea94c18b 774 Double_t localChi2, worstLocalChi2 = 0.;
775
96ebe67e 776 // Look for the cluster to remove
777 for (Int_t clusterNumber = 0; clusterNumber < 2; clusterNumber++) {
778 AliMUONTrackParam *trackParamAtCluster = (AliMUONTrackParam*)trackCandidate.GetTrackParamAtCluster()->UncheckedAt(clusterNumber);
ea94c18b 779
96ebe67e 780 // check if current cluster is removable
781 if (!trackParamAtCluster->IsRemovable()) return kFALSE;
ea94c18b 782
96ebe67e 783 // Pick up cluster with the worst chi2
784 localChi2 = trackParamAtCluster->GetLocalChi2();
ea94c18b 785 if (localChi2 > worstLocalChi2) {
786 worstLocalChi2 = localChi2;
96ebe67e 787 worstClusterNumber = clusterNumber;
ea94c18b 788 }
789 }
790
96ebe67e 791 // Reset best cluster as being NOT removable
792 ((AliMUONTrackParam*)trackCandidate.GetTrackParamAtCluster()->UncheckedAt((worstClusterNumber+1)%2))->SetRemovable(kFALSE);
ea94c18b 793
96ebe67e 794 // Remove the worst cluster
795 trackCandidate.RemoveTrackParamAtCluster((AliMUONTrackParam*)trackCandidate.GetTrackParamAtCluster()->UncheckedAt(worstClusterNumber));
ea94c18b 796
797 // Re-fit the track:
798 // Do not take into account the multiple scattering to speed up the fit
799 // Calculate the track parameter covariance matrix
96ebe67e 800 Fit(trackCandidate, kFALSE, kFALSE, kTRUE);
ea94c18b 801
96ebe67e 802 // Look for new cluster(s) in next station
803 return FollowTrackInStation(trackCandidate,clusterStore,nextStation);
ea94c18b 804
805}
806
807 //__________________________________________________________________________
96ebe67e 808void AliMUONTrackReconstructor::SetVertexErrXY2ForFit(AliMUONTrack &trackCandidate)
de2cd600 809{
96ebe67e 810 /// Compute the vertex resolution square from natural vertex dispersion and
208f139e 811 /// multiple scattering effets according to trackCandidate path in absorber
812 /// It is necessary to account for multiple scattering effects here instead of during the fit of
813 /// the "trackCandidate" to do not influence the result by changing track resolution at vertex
814 AliDebug(1,"Enter SetVertexForFit");
de2cd600 815
3304fa09 816 Double_t nonBendingReso2 = AliMUONReconstructor::GetRecoParam()->GetNonBendingVertexDispersion() *
817 AliMUONReconstructor::GetRecoParam()->GetNonBendingVertexDispersion();
818 Double_t bendingReso2 = AliMUONReconstructor::GetRecoParam()->GetBendingVertexDispersion() *
819 AliMUONReconstructor::GetRecoParam()->GetBendingVertexDispersion();
96ebe67e 820
208f139e 821 // add multiple scattering effets
96ebe67e 822 AliMUONTrackParam paramAtVertex(*((AliMUONTrackParam*)(trackCandidate.GetTrackParamAtCluster()->First())));
208f139e 823 paramAtVertex.DeleteCovariances(); // to be sure to account only for multiple scattering
824 AliMUONTrackExtrap::ExtrapToVertexUncorrected(&paramAtVertex,0.);
ea94c18b 825 const TMatrixD& kParamCov = paramAtVertex.GetCovariances();
826 nonBendingReso2 += kParamCov(0,0);
827 bendingReso2 += kParamCov(2,2);
96ebe67e 828
829 // Set the vertex resolution square
830 trackCandidate.SetVertexErrXY2(nonBendingReso2,bendingReso2);
208f139e 831}
832
833 //__________________________________________________________________________
96ebe67e 834void AliMUONTrackReconstructor::Fit(AliMUONTrack &track, Bool_t includeMCS, Bool_t fitWithVertex, Bool_t calcCov)
208f139e 835{
96ebe67e 836 /// Fit the track
837 /// w/wo multiple Coulomb scattering according to "includeMCS".
838 /// w/wo constraining the vertex according to "fitWithVertex".
839 /// calculating or not the covariance matrix according to "calcCov".
de2cd600 840
208f139e 841 Double_t benC, errorParam, invBenP, nonBenC, x, y;
de2cd600 842 AliMUONTrackParam *trackParam;
96ebe67e 843 Double_t arg[1], fedm, errdef, globalChi2;
208f139e 844 Int_t npari, nparx;
845 Int_t status, covStatus;
846
8cde4af5 847 // Instantiate gMinuit if not already done
848 if (!gMinuit) gMinuit = new TMinuit(6);
208f139e 849 // Clear MINUIT parameters
850 gMinuit->mncler();
851 // Give the fitted track to MINUIT
ea94c18b 852 gMinuit->SetObjectFit(&track);
208f139e 853 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 2) || (AliLog::GetGlobalDebugLevel() >= 2)) {
854 // Define print level
855 arg[0] = 1;
856 gMinuit->mnexcm("SET PRI", arg, 1, status);
857 // Print covariance matrix
858 gMinuit->mnexcm("SHO COV", arg, 0, status);
859 } else {
860 arg[0] = -1;
861 gMinuit->mnexcm("SET PRI", arg, 1, status);
862 }
de2cd600 863 // No warnings
208f139e 864 gMinuit->mnexcm("SET NOW", arg, 0, status);
865 // Define strategy
866 //arg[0] = 2;
867 //gMinuit->mnexcm("SET STR", arg, 1, status);
868
ea94c18b 869 // set flag w/wo multiple scattering according to "includeMCS"
96ebe67e 870 track.FitWithMCS(includeMCS);
ea94c18b 871 if (includeMCS) {
96ebe67e 872 // compute cluster weights only once
873 if (!track.ComputeClusterWeights()) {
ea94c18b 874 AliWarning("cannot take into account the multiple scattering effects");
96ebe67e 875 track.FitWithMCS(kFALSE);
ea94c18b 876 }
877 }
878
96ebe67e 879 track.FitWithVertex(fitWithVertex);
880 if (fitWithVertex) SetVertexErrXY2ForFit(track);
881
ea94c18b 882 // Set fitting function
883 gMinuit->SetFCN(TrackChi2);
208f139e 884
885 // Set fitted parameters (!! The order is very important for the covariance matrix !!)
96ebe67e 886 trackParam = (AliMUONTrackParam*) (track.GetTrackParamAtCluster()->First());
de2cd600 887 // could be tried with no limits for the search (min=max=0) ????
208f139e 888 // mandatory limits in non Bending to avoid NaN values of parameters
889 gMinuit->mnparm(0, "X", trackParam->GetNonBendingCoor(), 0.03, -500.0, 500.0, status);
890 gMinuit->mnparm(1, "NonBenS", trackParam->GetNonBendingSlope(), 0.001, -0.5, 0.5, status);
891 // mandatory limits in Bending to avoid NaN values of parameters
892 gMinuit->mnparm(2, "Y", trackParam->GetBendingCoor(), 0.10, -500.0, 500.0, status);
893 gMinuit->mnparm(3, "BenS", trackParam->GetBendingSlope(), 0.001, -0.5, 0.5, status);
894 gMinuit->mnparm(4, "InvBenP", trackParam->GetInverseBendingMomentum(), 0.003, -0.4, 0.4, status);
895
de2cd600 896 // minimization
208f139e 897 gMinuit->mnexcm("MIGRAD", arg, 0, status);
898
899 // Calculate the covariance matrix more accurately if required
900 if (calcCov) gMinuit->mnexcm("HESSE", arg, 0, status);
8cde4af5 901
de2cd600 902 // get results into "invBenP", "benC", "nonBenC" ("x", "y")
208f139e 903 gMinuit->GetParameter(0, x, errorParam);
904 trackParam->SetNonBendingCoor(x);
905 gMinuit->GetParameter(1, nonBenC, errorParam);
de2cd600 906 trackParam->SetNonBendingSlope(nonBenC);
208f139e 907 gMinuit->GetParameter(2, y, errorParam);
908 trackParam->SetBendingCoor(y);
909 gMinuit->GetParameter(3, benC, errorParam);
910 trackParam->SetBendingSlope(benC);
911 gMinuit->GetParameter(4, invBenP, errorParam);
912 trackParam->SetInverseBendingMomentum(invBenP);
913
de2cd600 914 // global result of the fit
96ebe67e 915 gMinuit->mnstat(globalChi2, fedm, errdef, npari, nparx, covStatus);
916 track.SetGlobalChi2(globalChi2);
208f139e 917
918 // Get the covariance matrix if required
919 if (calcCov) {
920 // Covariance matrix according to HESSE status
921 // If problem then keep only the diagonal terms (variances)
922 Double_t matrix[5][5];
923 gMinuit->mnemat(&matrix[0][0],5);
924 if (covStatus == 3) trackParam->SetCovariances(matrix);
925 else trackParam->SetVariances(matrix);
ea94c18b 926 } else trackParam->DeleteCovariances();
208f139e 927
de2cd600 928}
929
930 //__________________________________________________________________________
ea94c18b 931void TrackChi2(Int_t & /*nParam*/, Double_t * /*gradient*/, Double_t &chi2, Double_t *param, Int_t /*flag*/)
de2cd600 932{
ea94c18b 933 /// Return the "Chi2" to be minimized with Minuit for track fitting.
96ebe67e 934 /// Assumes that the trackParamAtCluster are sorted according to increasing Z.
935 /// Track parameters at each cluster are updated accordingly.
ea94c18b 936 /// Vertex is used according to the flag "trackBeingFitted->GetFitWithVertex()".
937 /// Multiple Coulomb scattering is taken into account according to the flag "trackBeingFitted->GetFitWithMCS()".
208f139e 938
939 AliMUONTrack *trackBeingFitted = (AliMUONTrack*) gMinuit->GetObjectFit();
96ebe67e 940 AliMUONTrackParam* trackParamAtCluster = (AliMUONTrackParam*) trackBeingFitted->GetTrackParamAtCluster()->First();
208f139e 941 Double_t dX, dY;
ea94c18b 942 chi2 = 0.; // initialize chi2
208f139e 943
ea94c18b 944 // update track parameters
96ebe67e 945 trackParamAtCluster->SetNonBendingCoor(param[0]);
946 trackParamAtCluster->SetNonBendingSlope(param[1]);
947 trackParamAtCluster->SetBendingCoor(param[2]);
948 trackParamAtCluster->SetBendingSlope(param[3]);
949 trackParamAtCluster->SetInverseBendingMomentum(param[4]);
950 trackBeingFitted->UpdateTrackParamAtCluster();
208f139e 951
952 // Take the vertex into account in the fit if required
96ebe67e 953 if (trackBeingFitted->FitWithVertex()) {
954 Double_t nonBendingReso2,bendingReso2;
955 trackBeingFitted->GetVertexErrXY2(nonBendingReso2,bendingReso2);
956 if (nonBendingReso2 == 0. || bendingReso2 == 0.) chi2 += 1.e10;
957 else {
958 AliMUONTrackParam paramAtVertex(*trackParamAtCluster);
959 AliMUONTrackExtrap::ExtrapToZ(&paramAtVertex, 0.); // vextex position = (0,0,0)
960 dX = paramAtVertex.GetNonBendingCoor();
961 dY = paramAtVertex.GetBendingCoor();
962 chi2 += dX * dX / nonBendingReso2 + dY * dY / bendingReso2;
208f139e 963 }
de2cd600 964 }
208f139e 965
ea94c18b 966 // compute chi2 w/wo multiple scattering
96ebe67e 967 chi2 += trackBeingFitted->ComputeGlobalChi2(trackBeingFitted->FitWithMCS());
b709ac13 968
969}
970
971 //__________________________________________________________________________
96ebe67e 972void AliMUONTrackReconstructor::ComplementTracks(const AliMUONVClusterStore& clusterStore)
b709ac13 973{
974 /// Complete tracks by adding missing clusters (if there is an overlap between
975 /// two detection elements, the track may have two clusters in the same chamber)
976 /// Re-fit track parameters and covariances
977 AliDebug(1,"Enter ComplementTracks");
978
979 Int_t chamberId, detElemId;
96ebe67e 980 Double_t chi2OfCluster, bestChi2OfCluster;
3304fa09 981 Double_t sigmaCut2 = AliMUONReconstructor::GetRecoParam()->GetSigmaCutForTracking() *
982 AliMUONReconstructor::GetRecoParam()->GetSigmaCutForTracking();
96ebe67e 983 Bool_t foundOneCluster, trackModified;
984 AliMUONVCluster* cluster;
985 AliMUONTrackParam *trackParam, *nextTrackParam, copyOfTrackParam, trackParamAtCluster, bestTrackParamAtCluster;
b709ac13 986
987 // Remove double track to complete only "good" tracks
988 RemoveDoubleTracks();
989
990 AliMUONTrack *track = (AliMUONTrack*) fRecTracksPtr->First();
991 while (track) {
992 trackModified = kFALSE;
993
96ebe67e 994 trackParam = (AliMUONTrackParam*)track->GetTrackParamAtCluster()->First();
b709ac13 995 while (trackParam) {
96ebe67e 996 foundOneCluster = kFALSE;
997 bestChi2OfCluster = 2. * sigmaCut2; // 2 because 2 quantities in chi2
998 chamberId = trackParam->GetClusterPtr()->GetChamberId();
999 detElemId = trackParam->GetClusterPtr()->GetDetElemId();
b709ac13 1000
1001 // prepare nextTrackParam before adding new cluster because of the sorting
96ebe67e 1002 nextTrackParam = (AliMUONTrackParam*)track->GetTrackParamAtCluster()->After(trackParam);
b709ac13 1003
1004 // recover track parameters from local fit and put them into a copy of trackParam
1005 copyOfTrackParam.SetZ(trackParam->GetZ());
1006 copyOfTrackParam.SetParameters(trackParam->GetSmoothParameters());
1007 copyOfTrackParam.SetCovariances(trackParam->GetSmoothCovariances());
1008
96ebe67e 1009 // Create iterators to loop over clusters in current chamber
1010 TIter nextInCh(clusterStore.CreateChamberIterator(chamberId,chamberId));
1011
b709ac13 1012 // look for one second candidate in the same chamber
96ebe67e 1013 while ( ( cluster = static_cast<AliMUONVCluster*>(nextInCh()) ) ) {
b709ac13 1014
1015 // look for a cluster in another detection element
96ebe67e 1016 if (cluster->GetDetElemId() == detElemId) continue;
b709ac13 1017
96ebe67e 1018 // try to add the current cluster fast
1019 if (!TryOneClusterFast(copyOfTrackParam, cluster)) continue;
b709ac13 1020
96ebe67e 1021 // try to add the current cluster accurately
1022 chi2OfCluster = TryOneCluster(copyOfTrackParam, cluster, trackParamAtCluster);
b709ac13 1023
1024 // if better chi2 then prepare to add this cluster to the track
96ebe67e 1025 if (chi2OfCluster < bestChi2OfCluster) {
1026 bestChi2OfCluster = chi2OfCluster;
1027 bestTrackParamAtCluster = trackParamAtCluster;
1028 foundOneCluster = kTRUE;
b709ac13 1029 }
1030
1031 }
1032
1033 // add new cluster if any
96ebe67e 1034 if (foundOneCluster) {
1035 UpdateTrack(*track,bestTrackParamAtCluster);
1036 bestTrackParamAtCluster.SetAloneInChamber(kFALSE);
b709ac13 1037 trackParam->SetAloneInChamber(kFALSE);
1038 trackModified = kTRUE;
1039 }
1040
1041 trackParam = nextTrackParam;
1042 }
1043
1044 // re-fit track parameters if needed
96ebe67e 1045 if (trackModified) Fit(*track, kTRUE, kFALSE, kTRUE);
b709ac13 1046
1047 track = (AliMUONTrack*) fRecTracksPtr->After(track);
1048 }
ea94c18b 1049
de2cd600 1050}
1051
1052 //__________________________________________________________________________
ea94c18b 1053void AliMUONTrackReconstructor::ImproveTracks()
de2cd600 1054{
ea94c18b 1055 /// Improve tracks by removing clusters with local chi2 highter than the defined cut
1056 /// Recompute track parameters and covariances at the remaining clusters
1057 AliDebug(1,"Enter ImproveTracks");
208f139e 1058
ea94c18b 1059 Double_t localChi2, worstLocalChi2;
b709ac13 1060 Int_t worstChamber, previousChamber;
1061 AliMUONTrack *track, *nextTrack;
96ebe67e 1062 AliMUONTrackParam *trackParamAtCluster, *worstTrackParamAtCluster, *previousTrackParam, *nextTrackParam;
3304fa09 1063 Double_t sigmaCut2 = AliMUONReconstructor::GetRecoParam()->GetSigmaCutForImprovement() *
1064 AliMUONReconstructor::GetRecoParam()->GetSigmaCutForImprovement();
208f139e 1065
ea94c18b 1066 // Remove double track to improve only "good" tracks
1067 RemoveDoubleTracks();
1068
b709ac13 1069 track = (AliMUONTrack*) fRecTracksPtr->First();
ea94c18b 1070 while (track) {
1071
b709ac13 1072 // prepare next track in case the actual track is suppressed
1073 nextTrack = (AliMUONTrack*) fRecTracksPtr->After(track);
1074
ea94c18b 1075 while (!track->IsImproved()) {
1076
1077 // Update track parameters and covariances
96ebe67e 1078 track->UpdateCovTrackParamAtCluster();
ea94c18b 1079
96ebe67e 1080 // Compute local chi2 of each clusters
ea94c18b 1081 track->ComputeLocalChi2(kTRUE);
1082
96ebe67e 1083 // Look for the cluster to remove
1084 worstTrackParamAtCluster = NULL;
ea94c18b 1085 worstLocalChi2 = 0.;
96ebe67e 1086 trackParamAtCluster = (AliMUONTrackParam*) track->GetTrackParamAtCluster()->First();
1087 while (trackParamAtCluster) {
ea94c18b 1088
96ebe67e 1089 // Pick up cluster with the worst chi2
1090 localChi2 = trackParamAtCluster->GetLocalChi2();
ea94c18b 1091 if (localChi2 > worstLocalChi2) {
1092 worstLocalChi2 = localChi2;
96ebe67e 1093 worstTrackParamAtCluster = trackParamAtCluster;
ea94c18b 1094 }
1095
96ebe67e 1096 trackParamAtCluster = (AliMUONTrackParam*) track->GetTrackParamAtCluster()->After(trackParamAtCluster);
de2cd600 1097 }
ea94c18b 1098
96ebe67e 1099 // Check if bad cluster found
1100 if (!worstTrackParamAtCluster) {
ea94c18b 1101 track->SetImproved(kTRUE);
1102 break;
de2cd600 1103 }
ea94c18b 1104
b709ac13 1105 // Check whether the worst chi2 is under requirement or not
3304fa09 1106 if (worstLocalChi2 < 2. * sigmaCut2) { // 2 because 2 quantities in chi2
ea94c18b 1107 track->SetImproved(kTRUE);
1108 break;
de2cd600 1109 }
ea94c18b 1110
96ebe67e 1111 // if the worst cluster is not removable then remove the entire track
1112 if (!worstTrackParamAtCluster->IsRemovable() && worstTrackParamAtCluster->IsAloneInChamber()) {
b709ac13 1113 fRecTracksPtr->Remove(track);
1114 fNRecTracks--;
ea94c18b 1115 break;
1116 }
1117
96ebe67e 1118 // Reset the second cluster in the same station as being not removable
1119 // or reset the second cluster in the same chamber as being alone
1120 worstChamber = worstTrackParamAtCluster->GetClusterPtr()->GetChamberId();
1121 previousTrackParam = (AliMUONTrackParam*) track->GetTrackParamAtCluster()->Before(worstTrackParamAtCluster);
1122 nextTrackParam = (AliMUONTrackParam*) track->GetTrackParamAtCluster()->After(worstTrackParamAtCluster);
1123 if (worstTrackParamAtCluster->IsAloneInChamber()) { // Worst cluster removable and alone in chamber
b709ac13 1124
1125 if (worstChamber%2 == 0) { // Modify flags in next chamber
1126
1127 nextTrackParam->SetRemovable(kFALSE);
96ebe67e 1128 if (!nextTrackParam->IsAloneInChamber()) // Make sure both clusters in second chamber are not removable anymore
1129 ((AliMUONTrackParam*) track->GetTrackParamAtCluster()->After(nextTrackParam))->SetRemovable(kFALSE);
b709ac13 1130
1131 } else { // Modify flags in previous chamber
1132
1133 previousTrackParam->SetRemovable(kFALSE);
96ebe67e 1134 if (!previousTrackParam->IsAloneInChamber()) // Make sure both clusters in second chamber are not removable anymore
1135 ((AliMUONTrackParam*) track->GetTrackParamAtCluster()->Before(previousTrackParam))->SetRemovable(kFALSE);
b709ac13 1136
1137 }
1138
96ebe67e 1139 } else { // Worst cluster not alone in its chamber
b709ac13 1140
96ebe67e 1141 if (previousTrackParam) previousChamber = previousTrackParam->GetClusterPtr()->GetChamberId();
b709ac13 1142 else previousChamber = -1;
1143
96ebe67e 1144 if (previousChamber == worstChamber) { // the second cluster on the same chamber is the previous one
b709ac13 1145
1146 previousTrackParam->SetAloneInChamber(kTRUE);
96ebe67e 1147 // transfert the removability to the second cluster
1148 if (worstTrackParamAtCluster->IsRemovable()) previousTrackParam->SetRemovable(kTRUE);
b709ac13 1149
96ebe67e 1150 } else { // the second cluster on the same chamber is the next one
b709ac13 1151
1152 nextTrackParam->SetAloneInChamber(kTRUE);
96ebe67e 1153 // transfert the removability to the second cluster
1154 if (worstTrackParamAtCluster->IsRemovable()) nextTrackParam->SetRemovable(kTRUE);
b709ac13 1155
1156 }
1157
1158 }
ea94c18b 1159
96ebe67e 1160 // Remove the worst cluster
1161 track->RemoveTrackParamAtCluster(worstTrackParamAtCluster);
ea94c18b 1162
1163 // Re-fit the track:
1164 // Take into account the multiple scattering
1165 // Calculate the track parameter covariance matrix
96ebe67e 1166 Fit(*track, kTRUE, kFALSE, kTRUE);
ea94c18b 1167
1168 // Printout for debuging
1169 if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
1170 cout << "ImproveTracks: track " << fRecTracksPtr->IndexOf(track)+1 << " improved " << endl;
1171 }
1172
de2cd600 1173 }
ea94c18b 1174
b709ac13 1175 track = nextTrack;
de2cd600 1176 }
1177
b709ac13 1178 // compress the array in case of some tracks have been removed
1179 fRecTracksPtr->Compress();
1180
de2cd600 1181}
1182
208f139e 1183 //__________________________________________________________________________
ea94c18b 1184void AliMUONTrackReconstructor::Finalize()
b8dc484b 1185{
b58638a9 1186 /// Recompute track parameters and covariances at each attached cluster from those at the first one
b709ac13 1187
b8dc484b 1188 AliMUONTrack *track;
b58638a9 1189
b8dc484b 1190 track = (AliMUONTrack*) fRecTracksPtr->First();
1191 while (track) {
ea94c18b 1192
1193 // update track parameters if not already done
96ebe67e 1194 if (!track->IsImproved()) track->UpdateCovTrackParamAtCluster();
ea94c18b 1195
b8dc484b 1196 track = (AliMUONTrack*) fRecTracksPtr->After(track);
ea94c18b 1197
de2cd600 1198 }
96ebe67e 1199
a9e2aefa 1200}
1201