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