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