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