]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONTrack.cxx
New "EventReconstructor..." structure,
[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 /*
17 $Log$
18 Revision 1.5  2000/07/18 16:04:06  gosset
19 AliMUONEventReconstructor package:
20 * a few minor modifications and more comments
21 * a few corrections
22   * right sign for Z of raw clusters
23   * right loop over chambers inside station
24   * symmetrized covariance matrix for measurements (TrackChi2MCS)
25   * right sign of charge in extrapolation (ExtrapToZ)
26   * right zEndAbsorber for Branson correction below 3 degrees
27 * use of TVirtualFitter instead of TMinuit for AliMUONTrack::Fit
28 * no parameter for AliMUONTrack::Fit() but more fit parameters in Track object
29
30 Revision 1.4  2000/06/30 10:15:48  gosset
31 Changes to EventReconstructor...:
32 precision fit with multiple Coulomb scattering;
33 extrapolation to vertex with Branson correction in absorber (JPC)
34
35 Revision 1.3  2000/06/25 13:23:28  hristov
36 stdlib.h needed for non-Linux compilation
37
38 Revision 1.2  2000/06/15 07:58:48  morsch
39 Code from MUON-dev joined
40
41 Revision 1.1.2.3  2000/06/12 10:11:34  morsch
42 Dummy copy constructor and assignment operator added
43
44 Revision 1.1.2.2  2000/06/09 12:58:05  gosset
45 Removed comment beginnings in Log sections of .cxx files
46 Suppressed most violations of coding rules
47
48 Revision 1.1.2.1  2000/06/07 14:44:53  gosset
49 Addition of files for track reconstruction in C++
50 */
51
52 //__________________________________________________________________________
53 //
54 // Reconstructed track in ALICE dimuon spectrometer
55 //__________________________________________________________________________
56
57 #include "AliMUONTrack.h"
58
59 #include <iostream.h>
60
61 #include <TClonesArray.h>
62 #include <TMath.h>
63 #include <TMatrix.h>
64 #include <TObjArray.h>
65 #include <TVirtualFitter.h>
66
67 #include "AliMUONEventReconstructor.h" 
68 #include "AliMUONHitForRec.h" 
69 #include "AliMUONSegment.h" 
70 #include "AliMUONTrackHit.h"
71
72 #include <stdlib.h>
73
74 // Functions to be minimized with Minuit
75 void TrackChi2(Int_t &NParam, Double_t *Gradient, Double_t &Chi2, Double_t *Param, Int_t Flag);
76 void TrackChi2MCS(Int_t &NParam, Double_t *Gradient, Double_t &Chi2, Double_t *Param, Int_t Flag);
77
78 Double_t MultipleScatteringAngle2(AliMUONTrackHit *TrackHit);
79
80 ClassImp(AliMUONTrack) // Class implementation in ROOT context
81
82 TVirtualFitter* AliMUONTrack::fgFitter = NULL; 
83
84   //__________________________________________________________________________
85 AliMUONTrack::AliMUONTrack(AliMUONSegment* BegSegment, AliMUONSegment* EndSegment, AliMUONEventReconstructor* EventReconstructor)
86 {
87   // Constructor from two Segment's
88   fEventReconstructor = EventReconstructor; // link back to EventReconstructor
89   // memory allocation for the TObjArray of pointers to reconstructed TrackHit's
90   fTrackHitsPtr = new TObjArray(10);
91   fNTrackHits = 0;
92   AddSegment(BegSegment); // add hits from BegSegment
93   AddSegment(EndSegment); // add hits from EndSegment
94   fTrackHitsPtr->Sort(); // sort TrackHits according to increasing Z
95   SetTrackParamAtVertex(); // set track parameters at vertex
96   // set fit conditions...
97   fFitMCS = 0;
98   fFitNParam = 3;
99   fFitStart = 1;
100   fFitFMin = -1.0;
101   return;
102 }
103
104   //__________________________________________________________________________
105 AliMUONTrack::AliMUONTrack(AliMUONSegment* Segment, AliMUONHitForRec* HitForRec, AliMUONEventReconstructor* EventReconstructor)
106 {
107   // Constructor from one Segment and one HitForRec
108   fEventReconstructor = EventReconstructor; // link back to EventReconstructor
109   // memory allocation for the TObjArray of pointers to reconstructed TrackHit's
110   fTrackHitsPtr = new TObjArray(10);
111   fNTrackHits = 0;
112   AddSegment(Segment); // add hits from Segment
113   AddHitForRec(HitForRec); // add HitForRec
114   fTrackHitsPtr->Sort(); // sort TrackHits according to increasing Z
115   SetTrackParamAtVertex(); // set track parameters at vertex
116   // set fit conditions...
117   fFitMCS = 0;
118   fFitNParam = 3;
119   fFitStart = 1;
120   fFitFMin = -1.0;
121   return;
122 }
123
124   //__________________________________________________________________________
125 AliMUONTrack::~AliMUONTrack()
126 {
127   // Destructor
128   if (fTrackHitsPtr) {
129     delete fTrackHitsPtr; // delete the TObjArray of pointers to TrackHit's
130     fTrackHitsPtr = NULL;
131   }
132 }
133
134   //__________________________________________________________________________
135 AliMUONTrack::AliMUONTrack (const AliMUONTrack& MUONTrack)
136 {
137 // Dummy copy constructor
138 }
139
140   //__________________________________________________________________________
141 AliMUONTrack & AliMUONTrack::operator=(const AliMUONTrack& MUONTrack)
142 {
143 // Dummy assignment operator
144     return *this;
145 }
146
147   //__________________________________________________________________________
148 void AliMUONTrack::Remove()
149 {
150   // Remove current track from array of tracks,
151   // and corresponding track hits from array of track hits.
152   // Compress the TClonesArray it belongs to.
153   AliMUONTrackHit *nextTrackHit;
154   AliMUONEventReconstructor *eventRec = this->fEventReconstructor;
155   TClonesArray *trackHitsPtr = eventRec->GetRecTrackHitsPtr();
156   // Loop over all track hits of track
157   AliMUONTrackHit *trackHit = (AliMUONTrackHit*) fTrackHitsPtr->First();
158   while (trackHit) {
159     nextTrackHit = (AliMUONTrackHit*) fTrackHitsPtr->After(trackHit);
160     // Remove TrackHit from event TClonesArray.
161     // Destructor is called,
162     // hence links between HitForRec's and TrackHit's are updated
163     trackHitsPtr->Remove(trackHit);
164     trackHit = nextTrackHit;
165   }
166   // Remove the track from event TClonesArray
167   // Destructor is called,
168   // hence space for TObjArray of pointers to TrackHit's is freed
169   eventRec->GetRecTracksPtr()->Remove(this);
170   // Number of tracks decreased by 1
171   eventRec->SetNRecTracks(eventRec->GetNRecTracks() - 1);
172   // Compress event TClonesArray of Track's:
173   // this is essential to retrieve the TClonesArray afterwards
174   eventRec->GetRecTracksPtr()->Compress();
175   // Compress event TClonesArray of TrackHit's:
176   // this is probably also essential to retrieve the TClonesArray afterwards
177   trackHitsPtr->Compress();
178 }
179
180   //__________________________________________________________________________
181 void AliMUONTrack::SetFitMCS(Int_t FitMCS)
182 {
183   // Set multiple Coulomb scattering option for track fit "fFitMCS"
184   // from "FitMCS" argument: 0 without, 1 with
185   if ((FitMCS == 0) || (FitMCS == 1)) fFitMCS = FitMCS;
186   // better implementation with enum(with, without) ????
187   else {
188     cout << "ERROR in AliMUONTrack::SetFitMCS(FitMCS)" << endl;
189     cout << "FitMCS = " << FitMCS << " is neither 0 nor 1" << endl;
190     exit(0);
191   }
192   return;
193 }
194
195   //__________________________________________________________________________
196 void AliMUONTrack::SetFitNParam(Int_t FitNParam)
197 {
198   // Set number of parameters for track fit "fFitNParam" from "FitNParam":
199   // 3 for momentum, 5 for momentum and position
200   if ((FitNParam == 3) || (FitNParam == 5)) fFitNParam = FitNParam;
201   else {
202     cout << "ERROR in AliMUONTrack::SetFitNParam(FitNParam)" << endl;
203     cout << "FitNParam = " << FitNParam << " is neither 3 nor 5" << endl;
204     exit(0);
205   }
206   return;
207 }
208
209   //__________________________________________________________________________
210 void AliMUONTrack::SetFitStart(Int_t FitStart)
211 {
212   // Set multiple Coulomb scattering option for track fit "fFitStart"
213   // from "FitStart" argument: 0 without, 1 with
214   if ((FitStart == 0) || (FitStart == 1)) fFitStart = FitStart;
215   // better implementation with enum(vertex, firstHit) ????
216   else {
217     cout << "ERROR in AliMUONTrack::SetFitStart(FitStart)" << endl;
218     cout << "FitStart = " << FitStart << " is neither 0 nor 1" << endl;
219     exit(0);
220   }
221   return;
222 }
223
224   //__________________________________________________________________________
225 AliMUONTrackParam* AliMUONTrack::GetTrackParamAtFirstHit(void) {
226   // Get pointer to TrackParamAtFirstHit
227   return ((AliMUONTrackHit*) (fTrackHitsPtr->First()))->GetTrackParam();}
228
229   //__________________________________________________________________________
230 void AliMUONTrack::RecursiveDump(void)
231 {
232   // Recursive dump of AliMUONTrack, i.e. with dump of TrackHit's and HitForRec's
233   AliMUONTrackHit *trackHit;
234   AliMUONHitForRec *hitForRec;
235   cout << "Recursive dump of Track: " << this << endl;
236   // Track
237   this->Dump();
238   for (Int_t trackHitIndex = 0; trackHitIndex < fNTrackHits; trackHitIndex++) {
239     trackHit = (AliMUONTrackHit*) ((*fTrackHitsPtr)[trackHitIndex]);
240     // TrackHit
241     cout << "TrackHit: " << trackHit << " (index: " << trackHitIndex << ")" << endl;
242     trackHit->Dump();
243     hitForRec = trackHit->GetHitForRecPtr();
244     // HitForRec
245     cout << "HitForRec: " << hitForRec << endl;
246     hitForRec->Dump();
247   }
248   return;
249 }
250
251   //__________________________________________________________________________
252 Int_t AliMUONTrack::HitsInCommon(AliMUONTrack* Track)
253 {
254   // Returns the number of hits in common
255   // between the current track ("this")
256   // and the track pointed to by "Track".
257   Int_t hitsInCommon = 0;
258   AliMUONTrackHit *trackHit1, *trackHit2;
259   // Loop over hits of first track
260   trackHit1 = (AliMUONTrackHit*) this->GetTrackHitsPtr()->First();
261   while (trackHit1) {
262     // Loop over hits of second track
263     trackHit2 = (AliMUONTrackHit*) Track->GetTrackHitsPtr()->First();
264     while (trackHit2) {
265       // Increment "hitsInCommon" if both TrackHits point to the same HitForRec
266       if ( (trackHit1->GetHitForRecPtr()) ==
267            (trackHit2->GetHitForRecPtr())    ) hitsInCommon++;
268       trackHit2 = (AliMUONTrackHit*) Track->GetTrackHitsPtr()->After(trackHit2);
269     } // trackHit2
270     trackHit1 = (AliMUONTrackHit*) this->GetTrackHitsPtr()->After(trackHit1);
271   } // trackHit1
272   return hitsInCommon;
273 }
274
275   //__________________________________________________________________________
276 void AliMUONTrack::Fit()
277 {
278   // Fit the current track ("this"),
279   // with or without multiple Coulomb scattering according to "fFitMCS",
280   // with the number of parameters given by "fFitNParam"
281   // (3 if one keeps X and Y fixed in "TrackParam", 5 if one lets them vary),
282   // starting, according to "fFitStart",
283   // with track parameters at vertex or at the first TrackHit.
284   // "fFitMCS", "fFitNParam" and "fFitStart" have to be set before
285   // by calling the corresponding Set methods.
286   Double_t arg[1], benC, errorParam, invBenP, lower, nonBenC, upper, x, y;
287   char parName[50];
288   AliMUONTrackParam *trackParam;
289   // Check if Minuit is initialized...
290   fgFitter = TVirtualFitter::Fitter(this); // add 3 or 5 for the maximum number of parameters ???
291   fgFitter->Clear(); // necessary ???? probably yes
292   // how to reset the printout number at every fit ????
293   // is there any risk to leave it like that ????
294   // how to go faster ???? choice of Minuit parameters like EDM ????
295   // choice of function to be minimized according to fFitMCS
296   if (fFitMCS == 0) fgFitter->SetFCN(TrackChi2);
297   else fgFitter->SetFCN(TrackChi2MCS);
298   arg[0] = 1;
299   fgFitter->ExecuteCommand("SET PRINT", arg, 1); // More printing !!!!
300   // Parameters according to "fFitStart"
301   // (should be a function to be used at every place where needed ????)
302   if (fFitStart == 0) trackParam = &fTrackParamAtVertex;
303   else trackParam = this->GetTrackParamAtFirstHit();
304   // set first 3 Minuit parameters
305   // could be tried with no limits for the search (min=max=0) ????
306   fgFitter->SetParameter(0, "InvBenP",
307                          trackParam->GetInverseBendingMomentum(),
308                          0.003, -0.4, 0.4);
309   fgFitter->SetParameter(1, "BenS",
310                          trackParam->GetBendingSlope(),
311                          0.001, -0.5, 0.5);
312   fgFitter->SetParameter(2, "NonBenS",
313                          trackParam->GetNonBendingSlope(),
314                          0.001, -0.5, 0.5);
315   if (fFitNParam == 5) {
316     // set last 2 Minuit parameters (no limits for the search: min=max=0)
317     fgFitter->SetParameter(3, "X",
318                            trackParam->GetNonBendingCoor(),
319                            0.03, 0.0, 0.0);
320     fgFitter->SetParameter(4, "Y",
321                            trackParam->GetBendingCoor(),
322                            0.10, 0.0, 0.0);
323   }
324   // search without gradient calculation in the function
325   fgFitter->ExecuteCommand("SET NOGRADIENT", arg, 0);
326   // minimization
327   fgFitter->ExecuteCommand("MINIMIZE", arg, 0);
328   // exit from Minuit
329   fgFitter->ExecuteCommand("EXIT", arg, 0); // necessary ????
330   // get results into "invBenP", "benC", "nonBenC" ("x", "y")
331   fgFitter->GetParameter(0, parName, invBenP, errorParam, lower, upper);
332   fgFitter->GetParameter(1, parName, benC, errorParam, lower, upper);
333   fgFitter->GetParameter(2, parName, nonBenC, errorParam, lower, upper);
334   if (fFitNParam == 5) {
335     fgFitter->GetParameter(3, parName, x, errorParam, lower, upper);
336     fgFitter->GetParameter(4, parName, y, errorParam, lower, upper);
337   }
338   // result of the fit into track parameters
339   trackParam->SetInverseBendingMomentum(invBenP);
340   trackParam->SetBendingSlope(benC);
341   trackParam->SetNonBendingSlope(nonBenC);
342   if (fFitNParam == 5) {
343     trackParam->SetNonBendingCoor(x);
344     trackParam->SetBendingCoor(y);
345   }
346   // global result of the fit
347   Double_t fedm, errdef;
348   Int_t npari, nparx;
349   fgFitter->GetStats(fFitFMin, fedm, errdef, npari, nparx);
350 }
351
352   //__________________________________________________________________________
353 void AliMUONTrack::AddSegment(AliMUONSegment* Segment)
354 {
355   // Add Segment to the track
356   AddHitForRec(Segment->GetHitForRec1()); // 1st hit
357   AddHitForRec(Segment->GetHitForRec2()); // 2nd hit
358 }
359
360   //__________________________________________________________________________
361 void AliMUONTrack::AddHitForRec(AliMUONHitForRec* HitForRec)
362 {
363   // Add HitForRec to the track:
364   // actual TrackHit into TClonesArray of TrackHit's for the event;
365   // pointer to actual TrackHit in TObjArray of pointers to TrackHit's for the track
366   TClonesArray *recTrackHitsPtr = this->fEventReconstructor->GetRecTrackHitsPtr();
367   Int_t eventTrackHits = this->fEventReconstructor->GetNRecTrackHits();
368   // event
369   AliMUONTrackHit* trackHit =
370     new ((*recTrackHitsPtr)[eventTrackHits]) AliMUONTrackHit(HitForRec);
371   this->fEventReconstructor->SetNRecTrackHits(eventTrackHits + 1);
372   // track
373   fTrackHitsPtr->Add(trackHit);
374   fNTrackHits++;
375 }
376
377   //__________________________________________________________________________
378 void AliMUONTrack::SetTrackParamAtHit(Int_t indexHit, AliMUONTrackParam *TrackParam)
379 {
380   // Set track parameters at TrackHit with index "indexHit"
381   // from the track parameters pointed to by "TrackParam".
382   AliMUONTrackHit* trackHit = (AliMUONTrackHit*) ((*fTrackHitsPtr)[indexHit]);
383   trackHit->SetTrackParam(TrackParam);
384 }
385
386   //__________________________________________________________________________
387 void AliMUONTrack::SetTrackParamAtVertex()
388 {
389   // Set track parameters at vertex.
390   // TrackHit's are assumed to be only in stations(1..) 4 and 5,
391   // and sorted according to increasing Z..
392   // Parameters are calculated from information in HitForRec's
393   // of first and last TrackHit's.
394   AliMUONTrackParam *trackParam =
395     &fTrackParamAtVertex; // pointer to track parameters
396   // Pointer to HitForRec of first TrackHit
397   AliMUONHitForRec *firstHit =
398     ((AliMUONTrackHit*) (fTrackHitsPtr->First()))->GetHitForRecPtr();
399   // Pointer to HitForRec of last TrackHit
400   AliMUONHitForRec *lastHit =
401     ((AliMUONTrackHit*) (fTrackHitsPtr->Last()))->GetHitForRecPtr();
402   // Z difference between first and last hits
403   Double_t deltaZ = firstHit->GetZ() - lastHit->GetZ();
404   // bending slope in stations(1..) 4 and 5
405   Double_t bendingSlope =
406     (firstHit->GetBendingCoor() - lastHit->GetBendingCoor()) / deltaZ;
407   trackParam->SetBendingSlope(bendingSlope);
408   // impact parameter
409   Double_t impactParam =
410     firstHit->GetBendingCoor() - bendingSlope * firstHit->GetZ(); // same if from firstHit and  lastHit ????
411   // signed bending momentum
412   Double_t signedBendingMomentum =
413     fEventReconstructor->GetBendingMomentumFromImpactParam(impactParam);
414   trackParam->SetInverseBendingMomentum(1.0 / signedBendingMomentum);
415   // bending slope at vertex
416   trackParam->
417     SetBendingSlope(bendingSlope +
418                     impactParam / fEventReconstructor->GetSimpleBPosition());
419   // non bending slope
420   Double_t nonBendingSlope =
421     (firstHit->GetNonBendingCoor() - lastHit->GetNonBendingCoor()) / deltaZ;
422   trackParam->SetNonBendingSlope(nonBendingSlope);
423   // vertex coordinates at (0,0,0)
424   trackParam->SetZ(0.0);
425   trackParam->SetBendingCoor(0.0);
426   trackParam->SetNonBendingCoor(0.0);
427 }
428
429   //__________________________________________________________________________
430 void TrackChi2(Int_t &NParam, Double_t *Gradient, Double_t &Chi2, Double_t *Param, Int_t Flag)
431 {
432   // Return the "Chi2" to be minimized with Minuit for track fitting,
433   // with "NParam" parameters
434   // and their current values in array pointed to by "Param".
435   // Assumes that the track hits are sorted according to increasing Z.
436   // Track parameters at each TrackHit are updated accordingly.
437   // Multiple Coulomb scattering is not taken into account
438   AliMUONTrack *trackBeingFitted;
439   AliMUONTrackHit* hit;
440   AliMUONTrackParam param1;
441   Int_t hitNumber;
442   Double_t zHit;
443   Chi2 = 0.0; // initialize Chi2
444   // copy of track parameters to be fitted
445   trackBeingFitted = (AliMUONTrack*) AliMUONTrack::Fitter()->GetObjectFit();
446   if (trackBeingFitted->GetFitStart() == 0)
447     param1 = *(trackBeingFitted->GetTrackParamAtVertex());
448   else param1 = *(trackBeingFitted->GetTrackParamAtFirstHit());
449   // Minuit parameters to be fitted into this copy
450   param1.SetInverseBendingMomentum(Param[0]);
451   param1.SetBendingSlope(Param[1]);
452   param1.SetNonBendingSlope(Param[2]);
453   if (NParam == 5) {
454     param1.SetNonBendingCoor(Param[3]);
455     param1.SetBendingCoor(Param[4]);
456   }
457   // Follow track through all planes of track hits
458   for (hitNumber = 0; hitNumber < trackBeingFitted->GetNTrackHits(); hitNumber++) {
459     hit = (AliMUONTrackHit*) (*(trackBeingFitted->GetTrackHitsPtr()))[hitNumber];
460     zHit = hit->GetHitForRecPtr()->GetZ();
461     // do something special if 2 hits with same Z ????
462     // security against infinite loop ????
463     (&param1)->ExtrapToZ(zHit); // extrapolation
464     hit->SetTrackParam(&param1);
465     // Increment Chi2
466     // done hit per hit, with hit resolution,
467     // and not with point and angle like in "reco_muon.F" !!!!
468     // Needs to add multiple scattering contribution ????
469     Double_t dX =
470       hit->GetHitForRecPtr()->GetNonBendingCoor() - (&param1)->GetNonBendingCoor();
471     Double_t dY =
472       hit->GetHitForRecPtr()->GetBendingCoor() - (&param1)->GetBendingCoor();
473     Chi2 =
474       Chi2 +
475       dX * dX / hit->GetHitForRecPtr()->GetNonBendingReso2() +
476       dY * dY / hit->GetHitForRecPtr()->GetBendingReso2();
477   }
478 }
479
480   //__________________________________________________________________________
481 void TrackChi2MCS(Int_t &NParam, Double_t *Gradient, Double_t &Chi2, Double_t *Param, Int_t Flag)
482 {
483   // Return the "Chi2" to be minimized with Minuit for track fitting,
484   // with "NParam" parameters
485   // and their current values in array pointed to by "Param".
486   // Assumes that the track hits are sorted according to increasing Z.
487   // Track parameters at each TrackHit are updated accordingly.
488   // Multiple Coulomb scattering is taken into account with covariance matrix.
489   AliMUONTrack *trackBeingFitted;
490   AliMUONTrackParam param1;
491   Chi2 = 0.0; // initialize Chi2
492   // copy of track parameters to be fitted
493   trackBeingFitted = (AliMUONTrack*) AliMUONTrack::Fitter()->GetObjectFit();
494   if (trackBeingFitted->GetFitStart() == 0)
495     param1 = *(trackBeingFitted->GetTrackParamAtVertex());
496   else param1 = *(trackBeingFitted->GetTrackParamAtFirstHit());
497   // Minuit parameters to be fitted into this copy
498   param1.SetInverseBendingMomentum(Param[0]);
499   param1.SetBendingSlope(Param[1]);
500   param1.SetNonBendingSlope(Param[2]);
501   if (NParam == 5) {
502     param1.SetNonBendingCoor(Param[3]);
503     param1.SetBendingCoor(Param[4]);
504   }
505
506   AliMUONTrackHit *hit;
507   Bool_t goodDeterminant;
508   Int_t hitNumber, hitNumber1, hitNumber2, hitNumber3;
509   Double_t zHit[10], paramBendingCoor[10], paramNonBendingCoor[10], ap[10];
510   Double_t hitBendingCoor[10], hitNonBendingCoor[10];
511   Double_t hitBendingReso2[10], hitNonBendingReso2[10];
512   // dimension 10 in parameter ??? related to AliMUONConstants::NTrackingCh() !!!!
513   Int_t numberOfHit = TMath::Min(trackBeingFitted->GetNTrackHits(), 10);
514   TMatrix *covBending = new TMatrix(numberOfHit, numberOfHit);
515   TMatrix *covNonBending = new TMatrix(numberOfHit, numberOfHit);
516
517   // Predicted coordinates and  multiple scattering angles are first calculated
518   for (hitNumber = 0; hitNumber < numberOfHit; hitNumber++) {
519     hit = (AliMUONTrackHit*) (*(trackBeingFitted->GetTrackHitsPtr()))[hitNumber];
520     zHit[hitNumber] = hit->GetHitForRecPtr()->GetZ();
521     // do something special if 2 hits with same Z ????
522     // security against infinite loop ????
523     (&param1)->ExtrapToZ(zHit[hitNumber]); // extrapolation
524     hit->SetTrackParam(&param1);
525     paramBendingCoor[hitNumber] = (&param1)->GetBendingCoor();
526     paramNonBendingCoor[hitNumber] = (&param1)->GetNonBendingCoor();
527     hitBendingCoor[hitNumber] = hit->GetHitForRecPtr()->GetBendingCoor();
528     hitNonBendingCoor[hitNumber] = hit->GetHitForRecPtr()->GetNonBendingCoor();
529     hitBendingReso2[hitNumber] = hit->GetHitForRecPtr()->GetBendingReso2();
530     hitNonBendingReso2[hitNumber] = hit->GetHitForRecPtr()->GetNonBendingReso2();
531     ap[hitNumber] = MultipleScatteringAngle2(hit); // multiple scatt. angle ^2  
532   }
533
534   // Calculates the covariance matrix
535   // One chamber is taken into account between successive hits.
536   // "ap" should be changed for taking into account the eventual missing hits
537   // by defining an "equivalent" chamber thickness !!!!
538   for (hitNumber1 = 0; hitNumber1 < numberOfHit; hitNumber1++) {    
539     for (hitNumber2 = hitNumber1; hitNumber2 < numberOfHit; hitNumber2++) {
540       // initialization to 0 (diagonal plus upper triangular part)
541       (*covBending)(hitNumber2, hitNumber1) = 0.0;
542       // contribution from multiple scattering in bending plane:
543       // loop over upstream hits
544       for (hitNumber3 = 0; hitNumber3 < hitNumber1; hitNumber3++) {     
545         (*covBending)(hitNumber2, hitNumber1) =
546           (*covBending)(hitNumber2, hitNumber1) +
547           ((zHit[hitNumber1] - zHit[hitNumber3]) *
548            (zHit[hitNumber2] - zHit[hitNumber3]) * ap[hitNumber3]); 
549       }
550       // equal contribution from multiple scattering in non bending plane
551       (*covNonBending)(hitNumber2, hitNumber1) =
552         (*covBending)(hitNumber2, hitNumber1);
553       if (hitNumber1 == hitNumber2) {
554         // Diagonal elements: add contribution from position measurements
555         // in bending plane
556         (*covBending)(hitNumber2, hitNumber1) =
557           (*covBending)(hitNumber2, hitNumber1) + hitBendingReso2[hitNumber1];
558         // and in non bending plane
559         (*covNonBending)(hitNumber2, hitNumber1) =
560           (*covNonBending)(hitNumber2, hitNumber1) + hitNonBendingReso2[hitNumber1];
561       }
562       else {
563         // Non diagonal elements: symmetrization
564         // for bending plane
565         (*covBending)(hitNumber1, hitNumber2) =
566           (*covBending)(hitNumber2, hitNumber1);
567         // and non bending plane
568         (*covNonBending)(hitNumber1, hitNumber2) =
569           (*covNonBending)(hitNumber2, hitNumber1);
570       }
571     } // for (hitNumber2 = hitNumber1;...
572   } // for (hitNumber1 = 0;...
573
574   // Inverts covariance matrix 
575   goodDeterminant = kTRUE;
576   // check whether the Invert method returns flag if matrix cannot be inverted,
577   // and do not calculate the Determinant in that case !!!!
578   if (covBending->Determinant() != 0) {
579     covBending->Invert();
580   } else {
581     goodDeterminant = kFALSE;
582     cout << "Warning in ChiMCS  Determinant Bending=0: " << endl;  
583   }
584   if (covNonBending->Determinant() != 0) {
585     covNonBending->Invert();
586   } else {
587     goodDeterminant = kFALSE;
588     cout << "Warning in ChiMCS  Determinant non Bending=0: " << endl;  
589   }
590
591   // It would be worth trying to calculate the inverse of the covariance matrix
592   // only once per fit, since it cannot change much in principle,
593   // and it would save a lot of computing time !!!!
594   
595   // Calculates Chi2
596   if (goodDeterminant) { // with Multiple Scattering if inversion correct
597     for (hitNumber1=0; hitNumber1 < numberOfHit ; hitNumber1++){ 
598       for (hitNumber2=0; hitNumber2 < numberOfHit; hitNumber2++){
599         Chi2 = Chi2 +
600           ((*covBending)(hitNumber2, hitNumber1) * 
601            (hitBendingCoor[hitNumber1] - paramBendingCoor[hitNumber1]) *
602            (hitBendingCoor[hitNumber2] - paramBendingCoor[hitNumber2]));
603         Chi2 = Chi2 +
604           ((*covNonBending)(hitNumber2, hitNumber1) *
605            (hitNonBendingCoor[hitNumber1] - paramNonBendingCoor[hitNumber1]) *
606            (hitNonBendingCoor[hitNumber2] - paramNonBendingCoor[hitNumber2]));
607       }
608     }
609   } else {  // without Multiple Scattering if inversion impossible
610     for (hitNumber1=0; hitNumber1 < numberOfHit ; hitNumber1++) { 
611       Chi2 = Chi2 +
612         ((hitBendingCoor[hitNumber1] - paramBendingCoor[hitNumber1]) *
613          (hitBendingCoor[hitNumber1] - paramBendingCoor[hitNumber1]) /
614          hitBendingReso2[hitNumber1]);
615       Chi2 = Chi2 +
616         ((hitNonBendingCoor[hitNumber1] - paramNonBendingCoor[hitNumber1]) *
617          (hitNonBendingCoor[hitNumber1] - paramNonBendingCoor[hitNumber1]) /
618          hitNonBendingReso2[hitNumber1]);      
619     }
620   }
621   
622   delete covBending;
623   delete covNonBending;
624 }
625
626 Double_t MultipleScatteringAngle2(AliMUONTrackHit *TrackHit)
627 {
628   // Returns square of multiple Coulomb scattering angle
629   // at TrackHit pointed to by "TrackHit"
630   Double_t slopeBending, slopeNonBending, radiationLength, inverseBendingMomentum2, inverseTotalMomentum2;
631   Double_t varMultipleScatteringAngle;
632   AliMUONTrack *trackBeingFitted = (AliMUONTrack*) AliMUONTrack::Fitter()->GetObjectFit();
633   AliMUONTrackParam *param = TrackHit->GetTrackParam();
634   // Better implementation in AliMUONTrack class ????
635   slopeBending = param->GetBendingSlope();
636   slopeNonBending = param->GetNonBendingSlope();
637   // thickness in radiation length for the current track,
638   // taking local angle into account
639   radiationLength =
640     trackBeingFitted->GetEventReconstructor()->GetChamberThicknessInX0() *
641     TMath::Sqrt(1.0 +
642                 slopeBending * slopeBending + slopeNonBending * slopeNonBending);
643   inverseBendingMomentum2 = 
644     param->GetInverseBendingMomentum() * param->GetInverseBendingMomentum();
645   inverseTotalMomentum2 =
646     inverseBendingMomentum2 * (1.0 + slopeBending * slopeBending) /
647     (1.0 + slopeBending *slopeBending + slopeNonBending * slopeNonBending); 
648   varMultipleScatteringAngle = 0.0136 * (1.0 + 0.038 * TMath::Log(radiationLength));
649   // The velocity is assumed to be 1 !!!!
650   varMultipleScatteringAngle = inverseTotalMomentum2 * radiationLength *
651     varMultipleScatteringAngle * varMultipleScatteringAngle;
652   return varMultipleScatteringAngle;
653 }