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