]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONTrack.cxx
New class added to check the reconstruction of muon tracks using reference tracks
[u/mrichter/AliRoot.git] / MUON / AliMUONTrack.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id$ */
17
18 ///////////////////////////////////////////////////
19 //
20 // Reconstructed track
21 // in
22 // ALICE
23 // dimuon
24 // spectrometer
25 //
26 ///////////////////////////////////////////////////
27
28 #include <stdlib.h> // for exit()
29
30 #include <Riostream.h> // for cout
31 #include <TMath.h>
32 #include <TMatrixD.h>
33 #include <TObjArray.h>
34 #include <TVirtualFitter.h>
35
36 #include "AliMUONEventReconstructor.h" 
37 #include "AliMUONHitForRec.h" 
38 #include "AliMUONSegment.h" 
39 #include "AliMUONTrack.h"
40 #include "AliMUONTrackHit.h"
41 #include "AliMUONTriggerTrack.h"
42 #include "AliMUONConstants.h"
43
44 // Functions to be minimized with Minuit
45 void TrackChi2(Int_t &NParam, Double_t *Gradient, Double_t &Chi2, Double_t *Param, Int_t Flag);
46 void TrackChi2MCS(Int_t &NParam, Double_t *Gradient, Double_t &Chi2, Double_t *Param, Int_t Flag);
47
48 void mnvertLocal(Double_t* a, Int_t l, Int_t m, Int_t n, Int_t& ifail);
49
50 Double_t MultipleScatteringAngle2(AliMUONTrackHit *TrackHit);
51
52 ClassImp(AliMUONTrack) // Class implementation in ROOT context
53
54 TVirtualFitter* AliMUONTrack::fgFitter = NULL; 
55
56   //__________________________________________________________________________
57 AliMUONTrack::AliMUONTrack()
58   : TObject() 
59 {
60   // Default constructor
61   fgFitter = 0;
62   fEventReconstructor = 0;
63   fTrackHitsPtr = new TObjArray(10);
64   fTrackParamAtHit = new TClonesArray("AliMUONTrackParam",10);  
65   fHitForRecAtHit = new TClonesArray("AliMUONHitForRec",10); 
66   fTrackID = 0;
67 }
68
69   //__________________________________________________________________________
70 AliMUONTrack::AliMUONTrack(AliMUONSegment* BegSegment, AliMUONSegment* EndSegment, AliMUONEventReconstructor* EventReconstructor)
71   : TObject()
72 {
73   // Constructor from two Segment's
74   fEventReconstructor = EventReconstructor; // link back to EventReconstructor
75   // memory allocation for the TObjArray of pointers to reconstructed TrackHit's
76   fTrackHitsPtr = new TObjArray(10);
77   fNTrackHits = 0;
78   AddSegment(BegSegment); // add hits from BegSegment
79   AddSegment(EndSegment); // add hits from EndSegment
80   fTrackHitsPtr->Sort(); // sort TrackHits according to increasing Z
81   SetTrackParamAtVertex(); // set track parameters at vertex
82   fTrackParamAtHit = new TClonesArray("AliMUONTrackParam",10);
83   fHitForRecAtHit = new TClonesArray("AliMUONHitForRec",10);
84   // set fit conditions...
85   fFitMCS = 0;
86   fFitNParam = 3;
87   fFitStart = 1;
88   fFitFMin = -1.0;
89   fMatchTrigger = kFALSE;
90   fChi2MatchTrigger = 0;
91   fTrackID = 0;
92   return;
93 }
94
95   //__________________________________________________________________________
96 AliMUONTrack::AliMUONTrack(AliMUONSegment* Segment, AliMUONHitForRec* HitForRec, AliMUONEventReconstructor* EventReconstructor)
97   : TObject()
98 {
99   // Constructor from one Segment and one HitForRec
100   fEventReconstructor = EventReconstructor; // link back to EventReconstructor
101   // memory allocation for the TObjArray of pointers to reconstructed TrackHit's
102   fTrackHitsPtr = new TObjArray(10);
103   fNTrackHits = 0;
104   AddSegment(Segment); // add hits from Segment
105   AddHitForRec(HitForRec); // add HitForRec
106   fTrackHitsPtr->Sort(); // sort TrackHits according to increasing Z
107   SetTrackParamAtVertex(); // set track parameters at vertex
108   fTrackParamAtHit = new TClonesArray("AliMUONTrackParam",10);
109   fHitForRecAtHit = new TClonesArray("AliMUONHitForRec",10);
110   // set fit conditions...
111   fFitMCS = 0;
112   fFitNParam = 3;
113   fFitStart = 1;
114   fFitFMin = -1.0;
115   fMatchTrigger = kFALSE;
116   fChi2MatchTrigger = 0;
117   fTrackID = 0;
118   return;
119 }
120
121   //__________________________________________________________________________
122 AliMUONTrack::~AliMUONTrack()
123 {
124   // Destructor
125   if (fTrackHitsPtr) {
126     delete fTrackHitsPtr; // delete the TObjArray of pointers to TrackHit's
127     fTrackHitsPtr = NULL;
128   }
129   
130   if (fTrackParamAtHit) {
131     // delete the TClonesArray of pointers to TrackParam
132     delete fTrackParamAtHit;
133     fTrackParamAtHit = NULL;
134   }
135
136   if (fHitForRecAtHit) {
137     // delete the TClonesArray of pointers to HitForRec
138     delete fHitForRecAtHit;
139     fHitForRecAtHit = NULL;
140   }
141 }
142
143   //__________________________________________________________________________
144 AliMUONTrack::AliMUONTrack (const AliMUONTrack& theMUONTrack)
145   :  TObject(theMUONTrack)
146 {
147   //fEventReconstructor = new AliMUONEventReconstructor(*MUONTrack.fEventReconstructor);
148                                // is it right ?
149                                // NO, because it would use dummy copy constructor
150                                // and AliMUONTrack is not the owner of its EventReconstructor 
151   fEventReconstructor = theMUONTrack.fEventReconstructor;
152   fTrackParamAtVertex = theMUONTrack.fTrackParamAtVertex;
153
154  // necessary to make a copy of the objects and not only the pointers in TObjArray.
155   fTrackHitsPtr  =  new TObjArray(10);
156   for (Int_t index = 0; index < (theMUONTrack.fTrackHitsPtr)->GetEntriesFast(); index++) {
157     AliMUONTrackHit *trackHit = new AliMUONTrackHit(*(AliMUONTrackHit*)(theMUONTrack.fTrackHitsPtr)->At(index));
158     fTrackHitsPtr->Add(trackHit);
159   }
160
161   // necessary to make a copy of the objects and not only the pointers in TClonesArray.
162   fTrackParamAtHit  =  new TClonesArray("AliMUONTrackParam",10);
163   for (Int_t index = 0; index < (theMUONTrack.fTrackParamAtHit)->GetEntriesFast(); index++) {
164     {new ((*fTrackParamAtHit)[fTrackParamAtHit->GetEntriesFast()]) 
165         AliMUONTrackParam(*(AliMUONTrackParam*)(theMUONTrack.fTrackParamAtHit)->At(index));}
166   }
167
168   // necessary to make a copy of the objects and not only the pointers in TClonesArray.
169   fHitForRecAtHit  =  new TClonesArray("AliMUONHitForRec",10);
170   for (Int_t index = 0; index < (theMUONTrack.fHitForRecAtHit)->GetEntriesFast(); index++) {
171     {new ((*fHitForRecAtHit)[fHitForRecAtHit->GetEntriesFast()]) 
172         AliMUONHitForRec(*(AliMUONHitForRec*)(theMUONTrack.fHitForRecAtHit)->At(index));}
173   }
174
175   fNTrackHits       =  theMUONTrack.fNTrackHits;
176   fFitMCS           =  theMUONTrack.fFitMCS;
177   fFitNParam        =  theMUONTrack.fFitNParam;
178   fFitFMin          =  theMUONTrack.fFitFMin;
179   fFitStart         =  theMUONTrack.fFitStart;
180   fMatchTrigger     =  theMUONTrack.fMatchTrigger;
181   fChi2MatchTrigger =  theMUONTrack.fChi2MatchTrigger;
182   fTrackID          =  theMUONTrack.fTrackID;
183 }
184
185   //__________________________________________________________________________
186 AliMUONTrack & AliMUONTrack::operator=(const AliMUONTrack& theMUONTrack)
187 {
188
189   // check assignement to self
190   if (this == &theMUONTrack)
191     return *this;
192
193   // base class assignement
194   TObject::operator=(theMUONTrack);
195
196   // fEventReconstructor =  new AliMUONEventReconstructor(*MUONTrack.fEventReconstructor); // is it right ?
197                                // is it right ? NO because it would use dummy copy constructor
198   fEventReconstructor =  theMUONTrack.fEventReconstructor;
199   fTrackParamAtVertex =  theMUONTrack.fTrackParamAtVertex;
200
201  // necessary to make a copy of the objects and not only the pointers in TObjArray.
202   fTrackHitsPtr  =  new TObjArray(10);
203   for (Int_t index = 0; index < (theMUONTrack.fTrackHitsPtr)->GetEntriesFast(); index++) {
204     AliMUONTrackHit *trackHit = new AliMUONTrackHit(*(AliMUONTrackHit*)(theMUONTrack.fTrackHitsPtr)->At(index));
205     fTrackHitsPtr->Add(trackHit);
206   }
207
208   // necessary to make a copy of the objects and not only the pointers in TClonesArray.
209   fTrackParamAtHit  =  new TClonesArray("AliMUONTrackParam",10);
210   for (Int_t index = 0; index < (theMUONTrack.fTrackParamAtHit)->GetEntriesFast(); index++) {
211     {new ((*fTrackParamAtHit)[fTrackParamAtHit->GetEntriesFast()]) 
212         AliMUONTrackParam(*(AliMUONTrackParam*)(theMUONTrack.fTrackParamAtHit)->At(index));}
213   }
214
215   // necessary to make a copy of the objects and not only the pointers in TClonesArray.
216   fHitForRecAtHit  =  new TClonesArray("AliMUONHitForRec",10);
217   for (Int_t index = 0; index < (theMUONTrack.fHitForRecAtHit)->GetEntriesFast(); index++) {
218     {new ((*fHitForRecAtHit)[fHitForRecAtHit->GetEntriesFast()]) 
219         AliMUONHitForRec(*(AliMUONHitForRec*)(theMUONTrack.fHitForRecAtHit)->At(index));}
220   }
221
222   fNTrackHits         =  theMUONTrack.fNTrackHits;
223   fFitMCS             =  theMUONTrack.fFitMCS;
224   fFitNParam          =  theMUONTrack.fFitNParam;
225   fFitFMin            =  theMUONTrack.fFitFMin;
226   fFitStart           =  theMUONTrack.fFitStart;
227   fMatchTrigger       =  theMUONTrack.fMatchTrigger;
228   fChi2MatchTrigger   =  theMUONTrack.fChi2MatchTrigger;
229   fTrackID            =  theMUONTrack.fTrackID;
230
231   return *this;
232 }
233
234   //__________________________________________________________________________
235 void AliMUONTrack::Remove()
236 {
237   // Remove current track from array of tracks,
238   // and corresponding track hits from array of track hits.
239   // Compress the TClonesArray it belongs to.
240   AliMUONTrackHit *nextTrackHit;
241   AliMUONEventReconstructor *eventRec = this->fEventReconstructor;
242   TClonesArray *trackHitsPtr = eventRec->GetRecTrackHitsPtr();
243   // Loop over all track hits of track
244   AliMUONTrackHit *trackHit = (AliMUONTrackHit*) fTrackHitsPtr->First();
245   while (trackHit) {
246     nextTrackHit = (AliMUONTrackHit*) fTrackHitsPtr->After(trackHit);
247     // Remove TrackHit from event TClonesArray.
248     // Destructor is called,
249     // hence links between HitForRec's and TrackHit's are updated
250     trackHitsPtr->Remove(trackHit);
251     trackHit = nextTrackHit;
252   }
253   // Remove the track from event TClonesArray
254   // Destructor is called,
255   // hence space for TObjArray of pointers to TrackHit's is freed
256   eventRec->GetRecTracksPtr()->Remove(this);
257   // Number of tracks decreased by 1
258   eventRec->SetNRecTracks(eventRec->GetNRecTracks() - 1);
259   // Compress event TClonesArray of Track's:
260   // this is essential to retrieve the TClonesArray afterwards
261   eventRec->GetRecTracksPtr()->Compress();
262   // Compress event TClonesArray of TrackHit's:
263   // this is probably also essential to retrieve the TClonesArray afterwards
264   trackHitsPtr->Compress();
265 }
266
267   //__________________________________________________________________________
268 void AliMUONTrack::SetFitMCS(Int_t FitMCS)
269 {
270   // Set multiple Coulomb scattering option for track fit "fFitMCS"
271   // from "FitMCS" argument: 0 without, 1 with
272   if ((FitMCS == 0) || (FitMCS == 1)) fFitMCS = FitMCS;
273   // better implementation with enum(with, without) ????
274   else {
275     cout << "ERROR in AliMUONTrack::SetFitMCS(FitMCS)" << endl;
276     cout << "FitMCS = " << FitMCS << " is neither 0 nor 1" << endl;
277     exit(0);
278   }
279   return;
280 }
281
282   //__________________________________________________________________________
283 void AliMUONTrack::SetFitNParam(Int_t FitNParam)
284 {
285   // Set number of parameters for track fit "fFitNParam" from "FitNParam":
286   // 3 for momentum, 5 for momentum and position
287   if ((FitNParam == 3) || (FitNParam == 5)) fFitNParam = FitNParam;
288   else {
289     cout << "ERROR in AliMUONTrack::SetFitNParam(FitNParam)" << endl;
290     cout << "FitNParam = " << FitNParam << " is neither 3 nor 5" << endl;
291     exit(0);
292   }
293   return;
294 }
295
296   //__________________________________________________________________________
297 void AliMUONTrack::SetFitStart(Int_t FitStart)
298 {
299   // Set multiple Coulomb scattering option for track fit "fFitStart"
300   // from "FitStart" argument: 0 without, 1 with
301   if ((FitStart == 0) || (FitStart == 1)) fFitStart = FitStart;
302   // better implementation with enum(vertex, firstHit) ????
303   else {
304     cout << "ERROR in AliMUONTrack::SetFitStart(FitStart)" << endl;
305     cout << "FitStart = " << FitStart << " is neither 0 nor 1" << endl;
306     exit(0);
307   }
308   return;
309 }
310
311   //__________________________________________________________________________
312 AliMUONTrackParam* AliMUONTrack::GetTrackParamAtFirstHit(void) const {
313   // Get pointer to TrackParamAtFirstHit
314   return ((AliMUONTrackHit*) (fTrackHitsPtr->First()))->GetTrackParam();}
315
316   //__________________________________________________________________________
317 void AliMUONTrack::RecursiveDump(void) const
318 {
319   // Recursive dump of AliMUONTrack, i.e. with dump of TrackHit's and HitForRec's
320   AliMUONTrackHit *trackHit;
321   AliMUONHitForRec *hitForRec;
322   cout << "Recursive dump of Track: " << this << endl;
323   // Track
324   this->Dump();
325   for (Int_t trackHitIndex = 0; trackHitIndex < fNTrackHits; trackHitIndex++) {
326     trackHit = (AliMUONTrackHit*) ((*fTrackHitsPtr)[trackHitIndex]);
327     // TrackHit
328     cout << "TrackHit: " << trackHit << " (index: " << trackHitIndex << ")" << endl;
329     trackHit->Dump();
330     hitForRec = trackHit->GetHitForRecPtr();
331     // HitForRec
332     cout << "HitForRec: " << hitForRec << endl;
333     hitForRec->Dump();
334   }
335   return;
336 }
337   
338   //__________________________________________________________________________
339 Bool_t* AliMUONTrack::CompatibleTrack(AliMUONTrack * Track, Double_t Sigma2Cut) const
340 {
341   // Return kTRUE/kFALSE for each chamber if hit is compatible or not 
342   TClonesArray *hitArray, *thisHitArray;
343   AliMUONHitForRec *hit, *thisHit;
344   Int_t chamberNumber;
345   Float_t deltaZ;
346   Float_t deltaZMax = 1.; // 1 cm
347   Float_t chi2 = 0;
348   Bool_t *nCompHit = new Bool_t[AliMUONConstants::NTrackingCh()]; 
349
350   for ( Int_t ch = 0; ch < AliMUONConstants::NTrackingCh(); ch++) {
351     nCompHit[ch] = kFALSE;
352   }
353
354   thisHitArray = this->GetHitForRecAtHit();
355
356   hitArray =  Track->GetHitForRecAtHit();
357
358   for (Int_t iHthis = 0; iHthis < thisHitArray->GetEntriesFast(); iHthis++) {
359     thisHit = (AliMUONHitForRec*) thisHitArray->At(iHthis);
360     chamberNumber = thisHit->GetChamberNumber();
361     if (chamberNumber < 0 || chamberNumber > AliMUONConstants::NTrackingCh()) continue; 
362     nCompHit[chamberNumber] = kFALSE;
363     for (Int_t iH = 0; iH < hitArray->GetEntriesFast(); iH++) {
364       hit = (AliMUONHitForRec*) hitArray->At(iH);
365       deltaZ = TMath::Abs(thisHit->GetZ() - hit->GetZ());
366       chi2 = thisHit->NormalizedChi2WithHitForRec(hit,Sigma2Cut); // set cut to 4 sigmas
367       if (chi2 < 3. && deltaZ < deltaZMax) {
368         nCompHit[chamberNumber] = kTRUE;
369         break;
370       }
371     }  
372   }
373   
374   return nCompHit;
375 }
376
377   //__________________________________________________________________________
378 Int_t AliMUONTrack::HitsInCommon(AliMUONTrack* Track) const
379 {
380   // Returns the number of hits in common
381   // between the current track ("this")
382   // and the track pointed to by "Track".
383   Int_t hitsInCommon = 0;
384   AliMUONTrackHit *trackHit1, *trackHit2;
385   // Loop over hits of first track
386   trackHit1 = (AliMUONTrackHit*) this->GetTrackHitsPtr()->First();
387   while (trackHit1) {
388     // Loop over hits of second track
389     trackHit2 = (AliMUONTrackHit*) Track->GetTrackHitsPtr()->First();
390     while (trackHit2) {
391       // Increment "hitsInCommon" if both TrackHits point to the same HitForRec
392       if ( (trackHit1->GetHitForRecPtr()) ==
393            (trackHit2->GetHitForRecPtr())    ) hitsInCommon++;
394       trackHit2 = (AliMUONTrackHit*) Track->GetTrackHitsPtr()->After(trackHit2);
395     } // trackHit2
396     trackHit1 = (AliMUONTrackHit*) this->GetTrackHitsPtr()->After(trackHit1);
397   } // trackHit1
398   return hitsInCommon;
399 }
400
401   //__________________________________________________________________________
402 void AliMUONTrack::MatchTriggerTrack(TClonesArray *triggerTrackArray)
403 {
404   // Match this track with one trigger track if possible
405   AliMUONTrackParam trackParam; 
406   AliMUONTriggerTrack *triggerTrack;
407   Double_t xTrack, yTrack, ySlopeTrack, dTrigTrackMin2, dTrigTrack2;
408   Double_t nSigmaCut2;
409
410   Double_t distSigma[3]={1,1,0.02}; // sigma of distributions (trigger-track) X,Y,slopeY
411   Double_t distTriggerTrack[3] = {0,0,0};
412
413   fMatchTrigger = kFALSE;
414   fChi2MatchTrigger = 0;
415
416   trackParam = *((AliMUONTrackParam*) fTrackParamAtHit->Last()); 
417   trackParam.ExtrapToZ(AliMUONConstants::DefaultChamberZ(10)); // extrap to 1st trigger chamber
418
419   nSigmaCut2 =  fEventReconstructor->GetMaxSigma2Distance(); // nb of sigma**2 for cut
420   xTrack = trackParam.GetNonBendingCoor();
421   yTrack = trackParam.GetBendingCoor();
422   ySlopeTrack = trackParam.GetBendingSlope();
423   dTrigTrackMin2 = 999;
424   
425   triggerTrack = (AliMUONTriggerTrack*) triggerTrackArray->First();
426   while(triggerTrack){
427     distTriggerTrack[0] = (triggerTrack->GetX11()-xTrack)/distSigma[0];
428     distTriggerTrack[1] = (triggerTrack->GetY11()-yTrack)/distSigma[1];
429     distTriggerTrack[2] = (TMath::Tan(triggerTrack->GetThetay())-ySlopeTrack)/distSigma[2];
430     dTrigTrack2 = 0;
431     for (Int_t iVar = 0; iVar < 3; iVar++)
432       dTrigTrack2 += distTriggerTrack[iVar]*distTriggerTrack[iVar];
433     if (dTrigTrack2 < dTrigTrackMin2 && dTrigTrack2 < nSigmaCut2) {
434       dTrigTrackMin2 = dTrigTrack2;
435       fMatchTrigger = kTRUE;
436       fChi2MatchTrigger =  dTrigTrack2/3.; // Normalized Chi2, 3 variables (X,Y,slopeY)
437     }
438     triggerTrack = (AliMUONTriggerTrack*) triggerTrackArray->After(triggerTrack);
439   }
440
441 }
442   //__________________________________________________________________________
443 void AliMUONTrack::Fit()
444 {
445   // Fit the current track ("this"),
446   // with or without multiple Coulomb scattering according to "fFitMCS",
447   // with the number of parameters given by "fFitNParam"
448   // (3 if one keeps X and Y fixed in "TrackParam", 5 if one lets them vary),
449   // starting, according to "fFitStart",
450   // with track parameters at vertex or at the first TrackHit.
451   // "fFitMCS", "fFitNParam" and "fFitStart" have to be set before
452   // by calling the corresponding Set methods.
453   Double_t arg[1], benC, errorParam, invBenP, lower, nonBenC, upper, x, y;
454   char parName[50];
455   AliMUONTrackParam *trackParam;
456   // Check if Minuit is initialized...
457   fgFitter = TVirtualFitter::Fitter(this); // add 3 or 5 for the maximum number of parameters ???
458   fgFitter->Clear(); // necessary ???? probably yes
459   // how to reset the printout number at every fit ????
460   // is there any risk to leave it like that ????
461   // how to go faster ???? choice of Minuit parameters like EDM ????
462   // choice of function to be minimized according to fFitMCS
463   if (fFitMCS == 0) fgFitter->SetFCN(TrackChi2);
464   else fgFitter->SetFCN(TrackChi2MCS);
465   // Switch off printout
466   arg[0] = -1;
467   fgFitter->ExecuteCommand("SET PRINT", arg, 1); // More printing !!!!
468   // No warnings
469   fgFitter->ExecuteCommand("SET NOW", arg, 0);
470   // Parameters according to "fFitStart"
471   // (should be a function to be used at every place where needed ????)
472   if (fFitStart == 0) trackParam = &fTrackParamAtVertex;
473   else trackParam = this->GetTrackParamAtFirstHit();
474   // set first 3 Minuit parameters
475   // could be tried with no limits for the search (min=max=0) ????
476   fgFitter->SetParameter(0, "InvBenP",
477                          trackParam->GetInverseBendingMomentum(),
478                          0.003, -0.4, 0.4);
479   fgFitter->SetParameter(1, "BenS",
480                          trackParam->GetBendingSlope(),
481                          0.001, -0.5, 0.5);
482   fgFitter->SetParameter(2, "NonBenS",
483                          trackParam->GetNonBendingSlope(),
484                          0.001, -0.5, 0.5);
485   if (fFitNParam == 5) {
486     // set last 2 Minuit parameters
487     // mandatory limits in Bending to avoid NaN values of parameters
488     fgFitter->SetParameter(3, "X",
489                            trackParam->GetNonBendingCoor(),
490                            0.03, -500.0, 500.0);
491     // mandatory limits in non Bending to avoid NaN values of parameters
492     fgFitter->SetParameter(4, "Y",
493                            trackParam->GetBendingCoor(),
494                            0.10, -500.0, 500.0);
495   }
496   // search without gradient calculation in the function
497   fgFitter->ExecuteCommand("SET NOGRADIENT", arg, 0);
498   // minimization
499   fgFitter->ExecuteCommand("MINIMIZE", arg, 0);
500   // exit from Minuit
501   //  fgFitter->ExecuteCommand("EXIT", arg, 0); // necessary ????
502   // get results into "invBenP", "benC", "nonBenC" ("x", "y")
503   fgFitter->GetParameter(0, parName, invBenP, errorParam, lower, upper);
504   fgFitter->GetParameter(1, parName, benC, errorParam, lower, upper);
505   fgFitter->GetParameter(2, parName, nonBenC, errorParam, lower, upper);
506   if (fFitNParam == 5) {
507     fgFitter->GetParameter(3, parName, x, errorParam, lower, upper);
508     fgFitter->GetParameter(4, parName, y, errorParam, lower, upper);
509   }
510   // result of the fit into track parameters
511   trackParam->SetInverseBendingMomentum(invBenP);
512   trackParam->SetBendingSlope(benC);
513   trackParam->SetNonBendingSlope(nonBenC);
514   if (fFitNParam == 5) {
515     trackParam->SetNonBendingCoor(x);
516     trackParam->SetBendingCoor(y);
517   }
518   // global result of the fit
519   Double_t fedm, errdef;
520   Int_t npari, nparx;
521   fgFitter->GetStats(fFitFMin, fedm, errdef, npari, nparx);
522 }
523
524   //__________________________________________________________________________
525 void AliMUONTrack::AddSegment(AliMUONSegment* Segment)
526 {
527   // Add Segment to the track
528   AddHitForRec(Segment->GetHitForRec1()); // 1st hit
529   AddHitForRec(Segment->GetHitForRec2()); // 2nd hit
530 }
531
532   //__________________________________________________________________________
533 void AliMUONTrack::AddHitForRec(AliMUONHitForRec* HitForRec)
534 {
535   // Add HitForRec to the track:
536   // actual TrackHit into TClonesArray of TrackHit's for the event;
537   // pointer to actual TrackHit in TObjArray of pointers to TrackHit's for the track
538   TClonesArray *recTrackHitsPtr = this->fEventReconstructor->GetRecTrackHitsPtr();
539   Int_t eventTrackHits = this->fEventReconstructor->GetNRecTrackHits();
540   // event
541   AliMUONTrackHit* trackHit =
542     new ((*recTrackHitsPtr)[eventTrackHits]) AliMUONTrackHit(HitForRec);
543   this->fEventReconstructor->SetNRecTrackHits(eventTrackHits + 1);
544   // track
545   fTrackHitsPtr->Add(trackHit);
546   fNTrackHits++;
547 }
548
549   //__________________________________________________________________________
550 void AliMUONTrack::SetTrackParamAtHit(Int_t indexHit, AliMUONTrackParam *TrackParam) const
551 {
552   // Set track parameters at TrackHit with index "indexHit"
553   // from the track parameters pointed to by "TrackParam".
554   //PH  AliMUONTrackHit* trackHit = (AliMUONTrackHit*) ((*fTrackHitsPtr)[indexHit]);
555   AliMUONTrackHit* trackHit = (AliMUONTrackHit*) (fTrackHitsPtr->At(indexHit));
556   trackHit->SetTrackParam(TrackParam);
557 }
558
559   //__________________________________________________________________________
560 void AliMUONTrack::SetTrackParamAtVertex()
561 {
562   // Set track parameters at vertex.
563   // TrackHit's are assumed to be only in stations(1..) 4 and 5,
564   // and sorted according to increasing Z..
565   // Parameters are calculated from information in HitForRec's
566   // of first and last TrackHit's.
567   AliMUONTrackParam *trackParam =
568     &fTrackParamAtVertex; // pointer to track parameters
569   // Pointer to HitForRec of first TrackHit
570   AliMUONHitForRec *firstHit =
571     ((AliMUONTrackHit*) (fTrackHitsPtr->First()))->GetHitForRecPtr();
572   // Pointer to HitForRec of last TrackHit
573   AliMUONHitForRec *lastHit =
574     ((AliMUONTrackHit*) (fTrackHitsPtr->Last()))->GetHitForRecPtr();
575   // Z difference between first and last hits
576   Double_t deltaZ = firstHit->GetZ() - lastHit->GetZ();
577   // bending slope in stations(1..) 4 and 5
578   Double_t bendingSlope =
579     (firstHit->GetBendingCoor() - lastHit->GetBendingCoor()) / deltaZ;
580   trackParam->SetBendingSlope(bendingSlope);
581   // impact parameter
582   Double_t impactParam =
583     firstHit->GetBendingCoor() - bendingSlope * firstHit->GetZ(); // same if from firstHit and  lastHit ????
584   // signed bending momentum
585   Double_t signedBendingMomentum =
586     fEventReconstructor->GetBendingMomentumFromImpactParam(impactParam);
587   trackParam->SetInverseBendingMomentum(1.0 / signedBendingMomentum);
588   // bending slope at vertex
589   trackParam->
590     SetBendingSlope(bendingSlope +
591                     impactParam / fEventReconstructor->GetSimpleBPosition());
592   // non bending slope
593   Double_t nonBendingSlope =
594     (firstHit->GetNonBendingCoor() - lastHit->GetNonBendingCoor()) / deltaZ;
595   trackParam->SetNonBendingSlope(nonBendingSlope);
596   // vertex coordinates at (0,0,0)
597   trackParam->SetZ(0.0);
598   trackParam->SetBendingCoor(0.0);
599   trackParam->SetNonBendingCoor(0.0);
600 }
601
602   //__________________________________________________________________________
603 void TrackChi2(Int_t &NParam, Double_t * /*Gradient*/, Double_t &Chi2, Double_t *Param, Int_t /*Flag*/)
604 {
605   // Return the "Chi2" to be minimized with Minuit for track fitting,
606   // with "NParam" parameters
607   // and their current values in array pointed to by "Param".
608   // Assumes that the track hits are sorted according to increasing Z.
609   // Track parameters at each TrackHit are updated accordingly.
610   // Multiple Coulomb scattering is not taken into account
611   AliMUONTrack *trackBeingFitted;
612   AliMUONTrackHit* hit;
613   AliMUONTrackParam param1;
614   Int_t hitNumber;
615   Double_t zHit;
616   Chi2 = 0.0; // initialize Chi2
617   // copy of track parameters to be fitted
618   trackBeingFitted = (AliMUONTrack*) AliMUONTrack::Fitter()->GetObjectFit();
619   if (trackBeingFitted->GetFitStart() == 0)
620     param1 = *(trackBeingFitted->GetTrackParamAtVertex());
621   else param1 = *(trackBeingFitted->GetTrackParamAtFirstHit());
622   // Minuit parameters to be fitted into this copy
623   param1.SetInverseBendingMomentum(Param[0]);
624   param1.SetBendingSlope(Param[1]);
625   param1.SetNonBendingSlope(Param[2]);
626   if (NParam == 5) {
627     param1.SetNonBendingCoor(Param[3]);
628     param1.SetBendingCoor(Param[4]);
629   }
630   // Follow track through all planes of track hits
631   for (hitNumber = 0; hitNumber < trackBeingFitted->GetNTrackHits(); hitNumber++) {
632     hit = (AliMUONTrackHit*) (*(trackBeingFitted->GetTrackHitsPtr()))[hitNumber];
633     zHit = hit->GetHitForRecPtr()->GetZ();
634     // do something special if 2 hits with same Z ????
635     // security against infinite loop ????
636     (&param1)->ExtrapToZ(zHit); // extrapolation
637     hit->SetTrackParam(&param1);
638     // Increment Chi2
639     // done hit per hit, with hit resolution,
640     // and not with point and angle like in "reco_muon.F" !!!!
641     // Needs to add multiple scattering contribution ????
642     Double_t dX =
643       hit->GetHitForRecPtr()->GetNonBendingCoor() - (&param1)->GetNonBendingCoor();
644     Double_t dY =
645       hit->GetHitForRecPtr()->GetBendingCoor() - (&param1)->GetBendingCoor();
646     Chi2 =
647       Chi2 +
648       dX * dX / hit->GetHitForRecPtr()->GetNonBendingReso2() +
649       dY * dY / hit->GetHitForRecPtr()->GetBendingReso2();
650   }
651 }
652
653   //__________________________________________________________________________
654 void TrackChi2MCS(Int_t &NParam, Double_t * /*Gradient*/, Double_t &Chi2, Double_t *Param, Int_t /*Flag*/)
655 {
656   // Return the "Chi2" to be minimized with Minuit for track fitting,
657   // with "NParam" parameters
658   // and their current values in array pointed to by "Param".
659   // Assumes that the track hits are sorted according to increasing Z.
660   // Track parameters at each TrackHit are updated accordingly.
661   // Multiple Coulomb scattering is taken into account with covariance matrix.
662   AliMUONTrack *trackBeingFitted;
663   AliMUONTrackParam param1;
664   Chi2 = 0.0; // initialize Chi2
665   // copy of track parameters to be fitted
666   trackBeingFitted = (AliMUONTrack*) AliMUONTrack::Fitter()->GetObjectFit();
667   if (trackBeingFitted->GetFitStart() == 0)
668     param1 = *(trackBeingFitted->GetTrackParamAtVertex());
669   else param1 = *(trackBeingFitted->GetTrackParamAtFirstHit());
670   // Minuit parameters to be fitted into this copy
671   param1.SetInverseBendingMomentum(Param[0]);
672   param1.SetBendingSlope(Param[1]);
673   param1.SetNonBendingSlope(Param[2]);
674   if (NParam == 5) {
675     param1.SetNonBendingCoor(Param[3]);
676     param1.SetBendingCoor(Param[4]);
677   }
678
679   AliMUONTrackHit *hit;
680   Int_t chCurrent, chPrev = 0, hitNumber, hitNumber1, hitNumber2, hitNumber3;
681   Double_t z, z1, z2, z3;
682   AliMUONTrackHit *hit1, *hit2, *hit3;
683   Double_t hbc1, hbc2, pbc1, pbc2;
684   Double_t hnbc1, hnbc2, pnbc1, pnbc2;
685   Int_t numberOfHit = trackBeingFitted->GetNTrackHits();
686   TMatrixD *covBending = new TMatrixD(numberOfHit, numberOfHit);
687   TMatrixD *covNonBending = new TMatrixD(numberOfHit, numberOfHit);
688   Double_t *msa2 = new Double_t[numberOfHit];
689
690   // Predicted coordinates and  multiple scattering angles are first calculated
691   for (hitNumber = 0; hitNumber < numberOfHit; hitNumber++) {
692     hit = (AliMUONTrackHit*) (*(trackBeingFitted->GetTrackHitsPtr()))[hitNumber];
693     z = hit->GetHitForRecPtr()->GetZ();
694     // do something special if 2 hits with same Z ????
695     // security against infinite loop ????
696     (&param1)->ExtrapToZ(z); // extrapolation
697     hit->SetTrackParam(&param1);
698     // square of multiple scattering angle at current hit, with one chamber
699     msa2[hitNumber] = MultipleScatteringAngle2(hit);
700     // correction for eventual missing hits or multiple hits in a chamber,
701     // according to the number of chambers
702     // between the current hit and the previous one
703     chCurrent = hit->GetHitForRecPtr()->GetChamberNumber();
704     if (hitNumber > 0) msa2[hitNumber] = msa2[hitNumber] * (chCurrent - chPrev);
705     chPrev = chCurrent;
706   }
707
708   // Calculates the covariance matrix
709   for (hitNumber1 = 0; hitNumber1 < numberOfHit; hitNumber1++) { 
710     hit1 = (AliMUONTrackHit*) (*(trackBeingFitted->GetTrackHitsPtr()))[hitNumber1];
711     z1 = hit1->GetHitForRecPtr()->GetZ();
712     for (hitNumber2 = hitNumber1; hitNumber2 < numberOfHit; hitNumber2++) {
713       hit2 = (AliMUONTrackHit*) (*(trackBeingFitted->GetTrackHitsPtr()))[hitNumber2];
714       z2 = hit2->GetHitForRecPtr()->GetZ();
715       // initialization to 0 (diagonal plus upper triangular part)
716       (*covBending)(hitNumber2, hitNumber1) = 0.0;
717       // contribution from multiple scattering in bending plane:
718       // loop over upstream hits
719       for (hitNumber3 = 0; hitNumber3 < hitNumber1; hitNumber3++) {     
720         hit3 = (AliMUONTrackHit*)
721           (*(trackBeingFitted->GetTrackHitsPtr()))[hitNumber3];
722         z3 = hit3->GetHitForRecPtr()->GetZ();
723         (*covBending)(hitNumber2, hitNumber1) =
724           (*covBending)(hitNumber2, hitNumber1) +
725           ((z1 - z3) * (z2 - z3) * msa2[hitNumber3]); 
726       }
727       // equal contribution from multiple scattering in non bending plane
728       (*covNonBending)(hitNumber2, hitNumber1) =
729         (*covBending)(hitNumber2, hitNumber1);
730       if (hitNumber1 == hitNumber2) {
731         // Diagonal elements: add contribution from position measurements
732         // in bending plane
733         (*covBending)(hitNumber2, hitNumber1) =
734           (*covBending)(hitNumber2, hitNumber1) +
735           hit1->GetHitForRecPtr()->GetBendingReso2();
736         // and in non bending plane
737         (*covNonBending)(hitNumber2, hitNumber1) =
738           (*covNonBending)(hitNumber2, hitNumber1) +
739           hit1->GetHitForRecPtr()->GetNonBendingReso2();
740       }
741       else {
742         // Non diagonal elements: symmetrization
743         // for bending plane
744         (*covBending)(hitNumber1, hitNumber2) =
745           (*covBending)(hitNumber2, hitNumber1);
746         // and non bending plane
747         (*covNonBending)(hitNumber1, hitNumber2) =
748           (*covNonBending)(hitNumber2, hitNumber1);
749       }
750     } // for (hitNumber2 = hitNumber1;...
751   } // for (hitNumber1 = 0;...
752     
753   // Inversion of covariance matrices
754   // with "mnvertLocal", local "mnvert" function of Minuit.
755   // One cannot use directly "mnvert" since "TVirtualFitter" does not know it.
756   // One will have to replace this local function by the right inversion function
757   // from a specialized Root package for symmetric positive definite matrices,
758   // when available!!!!
759   Int_t ifailBending;
760   mnvertLocal(&((*covBending)(0,0)), numberOfHit, numberOfHit, numberOfHit,
761               ifailBending);
762   Int_t ifailNonBending;
763   mnvertLocal(&((*covNonBending)(0,0)), numberOfHit, numberOfHit, numberOfHit,
764               ifailNonBending);
765
766   // It would be worth trying to calculate the inverse of the covariance matrix
767   // only once per fit, since it cannot change much in principle,
768   // and it would save a lot of computing time !!!!
769   
770   // Calculates Chi2
771   if ((ifailBending == 0) && (ifailNonBending == 0)) {
772     // with Multiple Scattering if inversion correct
773     for (hitNumber1 = 0; hitNumber1 < numberOfHit ; hitNumber1++) { 
774       hit1 = (AliMUONTrackHit*) (*(trackBeingFitted->GetTrackHitsPtr()))[hitNumber1];
775       hbc1 = hit1->GetHitForRecPtr()->GetBendingCoor();
776       pbc1 = hit1->GetTrackParam()->GetBendingCoor();
777       hnbc1 = hit1->GetHitForRecPtr()->GetNonBendingCoor();
778       pnbc1 = hit1->GetTrackParam()->GetNonBendingCoor();
779       for (hitNumber2 = 0; hitNumber2 < numberOfHit; hitNumber2++) {
780         hit2 = (AliMUONTrackHit*)
781           (*(trackBeingFitted->GetTrackHitsPtr()))[hitNumber2];
782         hbc2 = hit2->GetHitForRecPtr()->GetBendingCoor();
783         pbc2 = hit2->GetTrackParam()->GetBendingCoor();
784         hnbc2 = hit2->GetHitForRecPtr()->GetNonBendingCoor();
785         pnbc2 = hit2->GetTrackParam()->GetNonBendingCoor();
786         Chi2 = Chi2 +
787           ((*covBending)(hitNumber2, hitNumber1) *
788            (hbc1 - pbc1) * (hbc2 - pbc2)) +
789           ((*covNonBending)(hitNumber2, hitNumber1) *
790            (hnbc1 - pnbc1) * (hnbc2 - pnbc2));
791       }
792     }
793   } else {
794     // without Multiple Scattering if inversion impossible
795     for (hitNumber1 = 0; hitNumber1 < numberOfHit ; hitNumber1++) { 
796       hit1 = (AliMUONTrackHit*) (*(trackBeingFitted->GetTrackHitsPtr()))[hitNumber1];
797       hbc1 = hit1->GetHitForRecPtr()->GetBendingCoor();
798       pbc1 = hit1->GetTrackParam()->GetBendingCoor();
799       hnbc1 = hit1->GetHitForRecPtr()->GetNonBendingCoor();
800       pnbc1 = hit1->GetTrackParam()->GetNonBendingCoor();
801       Chi2 = Chi2 + 
802         ((hbc1 - pbc1) * (hbc1 - pbc1) /
803          hit1->GetHitForRecPtr()->GetBendingReso2()) +
804         ((hnbc1 - pnbc1) * (hnbc1 - pnbc1) /
805          hit1->GetHitForRecPtr()->GetNonBendingReso2());
806     }
807   }
808   
809   delete covBending;
810   delete covNonBending;
811   delete [] msa2;
812 }
813
814 Double_t MultipleScatteringAngle2(AliMUONTrackHit *TrackHit)
815 {
816   // Returns square of multiple Coulomb scattering angle
817   // at TrackHit pointed to by "TrackHit"
818   Double_t slopeBending, slopeNonBending, radiationLength, inverseBendingMomentum2, inverseTotalMomentum2;
819   Double_t varMultipleScatteringAngle;
820   AliMUONTrack *trackBeingFitted = (AliMUONTrack*) AliMUONTrack::Fitter()->GetObjectFit();
821   AliMUONTrackParam *param = TrackHit->GetTrackParam();
822   // Better implementation in AliMUONTrack class ????
823   slopeBending = param->GetBendingSlope();
824   slopeNonBending = param->GetNonBendingSlope();
825   // thickness in radiation length for the current track,
826   // taking local angle into account
827   radiationLength =
828     trackBeingFitted->GetEventReconstructor()->GetChamberThicknessInX0() *
829     TMath::Sqrt(1.0 +
830                 slopeBending * slopeBending + slopeNonBending * slopeNonBending);
831   inverseBendingMomentum2 = 
832     param->GetInverseBendingMomentum() * param->GetInverseBendingMomentum();
833   inverseTotalMomentum2 =
834     inverseBendingMomentum2 * (1.0 + slopeBending * slopeBending) /
835     (1.0 + slopeBending *slopeBending + slopeNonBending * slopeNonBending); 
836   varMultipleScatteringAngle = 0.0136 * (1.0 + 0.038 * TMath::Log(radiationLength));
837   // The velocity is assumed to be 1 !!!!
838   varMultipleScatteringAngle = inverseTotalMomentum2 * radiationLength *
839     varMultipleScatteringAngle * varMultipleScatteringAngle;
840   return varMultipleScatteringAngle;
841 }
842
843 //______________________________________________________________________________
844  void mnvertLocal(Double_t *a, Int_t l, Int_t, Int_t n, Int_t &ifail)
845 {
846 //*-*-*-*-*-*-*-*-*-*-*-*Inverts a symmetric matrix*-*-*-*-*-*-*-*-*-*-*-*-*
847 //*-*                    ==========================
848 //*-*        inverts a symmetric matrix.   matrix is first scaled to
849 //*-*        have all ones on the diagonal (equivalent to change of units)
850 //*-*        but no pivoting is done since matrix is positive-definite.
851 //*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
852
853   // taken from TMinuit package of Root (l>=n)
854   // fVERTs, fVERTq and fVERTpp changed to localVERTs, localVERTq and localVERTpp
855   //  Double_t localVERTs[n], localVERTq[n], localVERTpp[n];
856   Double_t * localVERTs = new Double_t[n];
857   Double_t * localVERTq = new Double_t[n];
858   Double_t * localVERTpp = new Double_t[n];
859   // fMaxint changed to localMaxint
860   Int_t localMaxint = n;
861
862     /* System generated locals */
863     Int_t aOffset;
864
865     /* Local variables */
866     Double_t si;
867     Int_t i, j, k, kp1, km1;
868
869     /* Parameter adjustments */
870     aOffset = l + 1;
871     a -= aOffset;
872
873     /* Function Body */
874     ifail = 0;
875     if (n < 1) goto L100;
876     if (n > localMaxint) goto L100;
877 //*-*-                  scale matrix by sqrt of diag elements
878     for (i = 1; i <= n; ++i) {
879         si = a[i + i*l];
880         if (si <= 0) goto L100;
881         localVERTs[i-1] = 1 / TMath::Sqrt(si);
882     }
883     for (i = 1; i <= n; ++i) {
884         for (j = 1; j <= n; ++j) {
885             a[i + j*l] = a[i + j*l]*localVERTs[i-1]*localVERTs[j-1];
886         }
887     }
888 //*-*-                                       . . . start main loop . . . .
889     for (i = 1; i <= n; ++i) {
890         k = i;
891 //*-*-                  preparation for elimination step1
892         if (a[k + k*l] != 0) localVERTq[k-1] = 1 / a[k + k*l];
893         else goto L100;
894         localVERTpp[k-1] = 1;
895         a[k + k*l] = 0;
896         kp1 = k + 1;
897         km1 = k - 1;
898         if (km1 < 0) goto L100;
899         else if (km1 == 0) goto L50;
900         else               goto L40;
901 L40:
902         for (j = 1; j <= km1; ++j) {
903             localVERTpp[j-1] = a[j + k*l];
904             localVERTq[j-1]  = a[j + k*l]*localVERTq[k-1];
905             a[j + k*l]   = 0;
906         }
907 L50:
908         if (k - n < 0) goto L51;
909         else if (k - n == 0) goto L60;
910         else                goto L100;
911 L51:
912         for (j = kp1; j <= n; ++j) {
913             localVERTpp[j-1] = a[k + j*l];
914             localVERTq[j-1]  = -a[k + j*l]*localVERTq[k-1];
915             a[k + j*l]   = 0;
916         }
917 //*-*-                  elimination proper
918 L60:
919         for (j = 1; j <= n; ++j) {
920             for (k = j; k <= n; ++k) { a[j + k*l] += localVERTpp[j-1]*localVERTq[k-1]; }
921         }
922     }
923 //*-*-                  elements of left diagonal and unscaling
924     for (j = 1; j <= n; ++j) {
925         for (k = 1; k <= j; ++k) {
926             a[k + j*l] = a[k + j*l]*localVERTs[k-1]*localVERTs[j-1];
927             a[j + k*l] = a[k + j*l];
928         }
929     }
930     delete localVERTs;
931     delete localVERTq;
932     delete localVERTpp;
933     return;
934 //*-*-                  failure return
935 L100:
936     delete localVERTs;
937     delete localVERTq;
938     delete localVERTpp;
939     ifail = 1;
940 } /* mnvertLocal */
941