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