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