]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONTrackK.cxx
Logging of Debug, Info and Error Messages follwing AliRoot Standard http://aliweb...
[u/mrichter/AliRoot.git] / MUON / AliMUONTrackK.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 #include <stdlib.h> // for exit()
17
18 #include <Riostream.h>
19 #include <TClonesArray.h>
20 #include <TMatrixD.h>
21
22 #include "AliMUONTrackK.h"
23 #include "AliCallf77.h"
24 #include "AliMUON.h"
25 #include "AliMUONChamber.h"
26 #include "AliMUONEventReconstructor.h"
27 #include "AliMUONSegment.h"
28 #include "AliMUONHitForRec.h"
29 #include "AliMUONRawCluster.h"
30 #include "AliMUONTrackParam.h"
31 #include "AliRun.h"
32 #include "AliLog.h"
33 //#include "AliMagF.h"
34
35 const Int_t AliMUONTrackK::fgkSize = 5;
36 const Int_t AliMUONTrackK::fgkNSigma = 4; 
37 const Int_t AliMUONTrackK::fgkTriesMax = 10000; 
38 const Double_t AliMUONTrackK::fgkEpsilon = 0.002; 
39
40 void mnvertLocalK(Double_t* a, Int_t l, Int_t m, Int_t n, Int_t& ifail);
41
42 ClassImp(AliMUONTrackK) // Class implementation in ROOT context
43
44   // A few calls in Fortran or from Fortran (extrap.F).
45 #ifndef WIN32 
46 # define extrap_onestep_helix extrap_onestep_helix_
47 # define extrap_onestep_helix3 extrap_onestep_helix3_
48 # define extrap_onestep_rungekutta extrap_onestep_rungekutta_
49 # define gufld_double gufld_double_
50 #else 
51 # define extrap_onestep_helix EXTRAP_ONESTEP_HELIX
52 # define extrap_onestep_helix3 EXTRAP_ONESTEP_HELIX3
53 # define extrap_onestep_rungekutta EXTRAP_ONESTEP_RUNGEKUTTA
54 # define gufld_double GUFLD_DOUBLE
55 #endif 
56
57 extern "C" {
58   void type_of_call extrap_onestep_helix
59   (Double_t &Charge, Double_t &StepLength, Double_t *VGeant3, Double_t *VGeant3New);
60
61   void type_of_call extrap_onestep_helix3
62   (Double_t &Field, Double_t &StepLength, Double_t *VGeant3, Double_t *VGeant3New);
63
64   void type_of_call extrap_onestep_rungekutta
65   (Double_t &Charge, Double_t &StepLength, Double_t *VGeant3, Double_t *VGeant3New);
66
67   void type_of_call gufld_double(Double_t *Position, Double_t *Field);
68     /*  void type_of_call gufld_double(Double_t *Position, Double_t *Field) {
69     // interface to "gAlice->Field()->Field" for arguments in double precision
70     Float_t x[3], b[3];
71     x[0] = Position[0]; x[1] = Position[1]; x[2] = Position[2];
72     gAlice->Field()->Field(x, b);
73     Field[0] = b[0]; Field[1] = b[1]; Field[2] = b[2];
74   }
75     */
76 }
77
78 Int_t AliMUONTrackK::fgNOfPoints = 0; 
79 AliMUON* AliMUONTrackK::fgMUON = NULL;
80 AliMUONEventReconstructor* AliMUONTrackK::fgEventReconstructor = NULL; 
81 TClonesArray* AliMUONTrackK::fgHitForRec = NULL; 
82
83   //__________________________________________________________________________
84 AliMUONTrackK::AliMUONTrackK()
85   : TObject()
86 {
87   // Default constructor
88
89   fgEventReconstructor = NULL; // pointer to event reconstructor
90   fgMUON = NULL; // pointer to Muon module
91   fgHitForRec = NULL; // pointer to points
92   fgNOfPoints = 0; // number of points
93
94   fStartSegment = NULL;
95   fTrackHitsPtr = NULL;
96   fNTrackHits = 0;
97   fTrackPar = NULL;
98   fTrackParNew = NULL;
99   fCovariance = NULL;
100   fWeight = NULL;
101   fSkipHit = NULL;
102
103   return;
104 }
105
106   //__________________________________________________________________________
107 AliMUONTrackK::AliMUONTrackK(AliMUONEventReconstructor *EventReconstructor, TClonesArray *hitForRec)
108   : TObject()
109 {
110   // Constructor
111
112   fgEventReconstructor = EventReconstructor; // pointer to event reconstructor
113   fgMUON = (AliMUON*) gAlice->GetModule("MUON"); // pointer to Muon module
114   fgHitForRec = hitForRec; // pointer to points
115   fgNOfPoints = fgHitForRec->GetEntriesFast(); // number of points
116
117   fStartSegment = NULL;
118   fTrackHitsPtr = NULL;
119   fNTrackHits = 0;
120   fChi2 = 0;
121   fTrackPar = NULL;
122   fTrackParNew = NULL;
123   fCovariance = NULL;
124   fWeight = NULL;
125   fSkipHit = NULL;
126
127   return;
128 }
129
130   //__________________________________________________________________________
131 AliMUONTrackK::AliMUONTrackK(AliMUONSegment *segment)
132   : TObject()
133 {
134   // Constructor from a segment
135   Double_t dX, dY, dZ;
136   AliMUONHitForRec *hit1, *hit2;
137   AliMUONRawCluster *clus;
138   TClonesArray *rawclusters;
139
140   fStartSegment = segment;
141   fRecover = 0;
142   // Pointers to hits from the segment
143   hit1 = segment->GetHitForRec1();
144   hit2 = segment->GetHitForRec2();
145   hit1->SetNTrackHits(hit1->GetNTrackHits()+1); // mark hit as being on track
146   hit2->SetNTrackHits(hit2->GetNTrackHits()+1); // mark hit as being on track
147   // check sorting in Z
148   if (hit1->GetZ() > hit2->GetZ()) {
149     hit1 = hit2;
150     hit2 = segment->GetHitForRec1();
151   }
152   // memory allocation for the TObjArray of pointers to reconstructed TrackHit's
153   fTrackHitsPtr = new TObjArray(10);
154   fNTrackHits = 2;
155   fChi2 = 0;
156   fBPFlag = kFALSE;
157   fTrackPar = new TMatrixD(fgkSize,1); // track parameters
158   fTrackParNew = new TMatrixD(fgkSize,1); // track parameters
159   fCovariance = new TMatrixD(fgkSize,fgkSize); // covariance matrix
160   fWeight = new TMatrixD(fgkSize,fgkSize); // weight matrix (inverse of covariance)
161
162   // Fill array of track parameters
163   if (hit1->GetChamberNumber() > 7) {
164     // last tracking station
165     (*fTrackPar)(0,0) = hit1->GetBendingCoor(); // y
166     (*fTrackPar)(1,0) = hit1->GetNonBendingCoor(); // x
167     fPosition = hit1->GetZ(); // z
168     fTrackHitsPtr->Add((TObjArray*)hit2); // add hit 2
169     fTrackHitsPtr->Add((TObjArray*)hit1); // add hit 1
170     fTrackDir = -1;
171   } else {
172     // last but one tracking station
173     (*fTrackPar)(0,0) = hit2->GetBendingCoor(); // y
174     (*fTrackPar)(1,0) = hit2->GetNonBendingCoor(); // x
175     fPosition = hit2->GetZ(); // z
176     fTrackHitsPtr->Add((TObjArray*)hit1); // add hit 1
177     fTrackHitsPtr->Add((TObjArray*)hit2); // add hit 2
178     fTrackDir = 1;
179   }
180   dZ = hit2->GetZ() - hit1->GetZ();
181   dY = hit2->GetBendingCoor() - hit1->GetBendingCoor();
182   dX = hit2->GetNonBendingCoor() - hit1->GetNonBendingCoor();
183   (*fTrackPar)(2,0) = TMath::ATan2(dY,dZ); // alpha
184   (*fTrackPar)(3,0) = TMath::ATan2(dX,dZ/TMath::Cos((*fTrackPar)(2,0))); // beta
185   (*fTrackPar)(4,0) = 1/fgEventReconstructor->GetBendingMomentumFromImpactParam(segment->GetBendingImpact()); // 1/Pt
186   (*fTrackPar)(4,0) *= TMath::Cos((*fTrackPar)(3,0)); // 1/p
187   cout << fgEventReconstructor->GetBendingMomentumFromImpactParam(segment->GetBendingImpact()) << " " << 1/(*fTrackPar)(4,0) << " ";
188   if (fgEventReconstructor->GetRecGeantHits()) { 
189     // from GEANT hits
190     cout << ((AliMUONHitForRec*)((*fTrackHitsPtr)[0]))->GetTHTrack() << "<-->" << ((AliMUONHitForRec*)((*fTrackHitsPtr)[1]))->GetTHTrack() << endl;
191   } else {
192     // from raw clusters
193     for (Int_t i=0; i<2; i++) {
194       hit1 = (AliMUONHitForRec*) ((*fTrackHitsPtr)[i]);
195       rawclusters = fgMUON->GetMUONData()->RawClusters(hit1->GetChamberNumber());
196       clus = (AliMUONRawCluster*) rawclusters->UncheckedAt(hit1->GetHitNumber());
197       cout << clus->GetTrack(1)-1;
198       if (clus->GetTrack(2) != 0) cout << " " << clus->GetTrack(2)-1;
199       if (i == 0) cout << " <--> ";
200     }
201     cout << endl;
202   }
203   // Evaluate covariance (and weight) matrix
204   EvalCovariance(dZ);
205
206   return;
207 }
208
209   //__________________________________________________________________________
210 AliMUONTrackK::~AliMUONTrackK()
211 {
212   // Destructor
213
214   if (fTrackHitsPtr) {
215     delete fTrackHitsPtr; // delete the TObjArray of pointers to TrackHit's
216     fTrackHitsPtr = NULL;
217   }
218   delete fTrackPar; delete fTrackParNew; delete fCovariance;
219   delete fWeight; 
220 }
221
222   //__________________________________________________________________________
223 AliMUONTrackK::AliMUONTrackK (const AliMUONTrackK& source)
224   : TObject(source)
225 {
226 // Protected copy constructor
227
228   AliFatal("Not implemented.");
229 }
230
231   //__________________________________________________________________________
232 AliMUONTrackK & AliMUONTrackK::operator=(const AliMUONTrackK& source)
233 {
234   // Assignment operator
235   // Members
236   if(&source == this) return *this;
237
238   // base class assignement
239   TObject::operator=(source);
240
241   fStartSegment = source.fStartSegment;
242   fNTrackHits = source.fNTrackHits;
243   fChi2 = source.fChi2;
244   fPosition = source.fPosition;
245   fPositionNew = source.fPositionNew;
246   fTrackDir = source.fTrackDir;
247   fBPFlag = source.fBPFlag;
248   fRecover = source.fRecover;
249   fSkipHit = source.fSkipHit;
250
251   // Pointers
252   fTrackHitsPtr = new TObjArray(*source.fTrackHitsPtr);
253   //source.fTrackHitsPtr->Dump();
254   //fTrackHitsPtr->Dump();
255   
256   fTrackPar = new TMatrixD(*source.fTrackPar); // track parameters
257   fTrackParNew = new TMatrixD(*source.fTrackParNew); // track parameters
258   fCovariance = new TMatrixD(*source.fCovariance); // covariance matrix
259   fWeight = new TMatrixD(*source.fWeight); // weight matrix (inverse of covariance)
260
261   return *this;
262 }
263
264   //__________________________________________________________________________
265 void AliMUONTrackK::EvalCovariance(Double_t dZ)
266 {
267   // Evaluate covariance (and weight) matrix for track candidate
268   Double_t sigmaB, sigmaNonB, tanA, tanB, dAdY, rad, dBdX, dBdY;
269
270   sigmaB = fgEventReconstructor->GetBendingResolution(); // bending resolution
271   sigmaNonB = fgEventReconstructor->GetNonBendingResolution(); // non-bending resolution
272
273   (*fWeight)(0,0) = sigmaB*sigmaB; // <yy>
274
275   (*fWeight)(1,1) = sigmaNonB*sigmaNonB; // <xx>
276
277   tanA = TMath::Tan((*fTrackPar)(2,0));
278   dAdY = 1/(1+tanA*tanA)/dZ;
279   (*fWeight)(2,2) = dAdY*dAdY*(*fWeight)(0,0)*2; // <aa>
280   (*fWeight)(0,2) = dAdY*(*fWeight)(0,0); // <ya>
281   (*fWeight)(2,0) = (*fWeight)(0,2);
282
283   rad = dZ/TMath::Cos((*fTrackPar)(2,0));
284   tanB = TMath::Tan((*fTrackPar)(3,0));
285   dBdX = 1/(1+tanB*tanB)/rad;
286   dBdY = 0; // neglect
287   (*fWeight)(3,3) = dBdX*dBdX*(*fWeight)(1,1)*2; // <bb>
288   (*fWeight)(1,3) = dBdX*(*fWeight)(1,1); // <xb>
289   (*fWeight)(3,1) = (*fWeight)(1,3);
290
291   //(*fWeight)(4,4) = ((*fTrackPar)(4,0)*0.2)*((*fTrackPar)(4,0)*0.2); // error 20%
292   (*fWeight)(4,4) = ((*fTrackPar)(4,0)*0.5)*((*fTrackPar)(4,0)*0.5); // error 50%
293
294   // check whether the Invert method returns flag if matrix cannot be inverted,
295   // and do not calculate the Determinant in that case !!!!
296   if (fWeight->Determinant() != 0) {
297
298     // fWeight->Invert();
299
300     Int_t ifailWeight;
301     mnvertLocalK(&((*fWeight)(0,0)), fgkSize,fgkSize,fgkSize,ifailWeight);
302   } else {
303     AliWarning(" Determinant fWeight=0:");
304   }
305   return;
306 }
307
308   //__________________________________________________________________________
309 Bool_t AliMUONTrackK::KalmanFilter(Int_t ichamBeg, Int_t ichamEnd, Bool_t Back, Double_t zDipole1, Double_t zDipole2)
310 {
311   // Follows track through detector stations 
312   Bool_t miss, success;
313   Int_t ichamb, iFB, iMin, iMax, dChamb, ichambOK, i;
314   Int_t ihit, firstIndx, lastIndx, currIndx, dChambMiss, iDindx=0;
315   Double_t zEnd, dChi2;
316   AliMUONHitForRec *hitAdd, *firstHit, *lastHit, *hit;
317   AliMUONRawCluster *clus;
318   TClonesArray *rawclusters;
319   hit = 0; clus = 0; rawclusters = 0;
320
321   miss = kTRUE;
322   success = kTRUE;
323   Int_t endOfProp = 0;
324   iFB = TMath::Sign(1,ichamEnd-ichamBeg);
325   iMin = TMath::Min(ichamEnd,ichamBeg);
326   iMax = TMath::Max(ichamEnd,ichamBeg);
327   ichamb = ichamBeg;
328   ichambOK = ichamb;
329
330   // Get indices of the 1'st and last hits on the track candidate
331   firstHit = (AliMUONHitForRec*) fTrackHitsPtr->First();
332   lastHit = (AliMUONHitForRec*) fTrackHitsPtr->Last();
333   firstIndx = fgHitForRec->IndexOf(firstHit);
334   lastIndx = fgHitForRec->IndexOf(lastHit);
335   currIndx = TMath::Abs (TMath::Max(firstIndx*iFB,lastIndx*iFB));
336   if (Back) {
337     // backpropagation
338     currIndx = 2; 
339     iDindx = 1;
340     if (fRecover != 0) {
341       // find hit with the highest Z
342       Double_t zbeg = 0;
343       for (i=0; i<fNTrackHits; i++) {
344         hitAdd = (AliMUONHitForRec*) ((*fTrackHitsPtr)[i]);
345         zEnd = hitAdd->GetZ();
346         if (zEnd > zbeg) zbeg = zEnd;
347         else {
348           currIndx = fNTrackHits - i + 2; //???
349           break;
350         }
351       } //for (Int_t i=0;
352     }
353   } else if (fRecover != 0) {
354     Back = kTRUE; // dirty trick
355     iDindx = -1;
356     if (ichamBeg == 7 || ichamBeg == 8) currIndx = fNTrackHits - 2;
357     else {
358       Double_t zbeg = ((AliMUONHitForRec*)((*fTrackHitsPtr)[0]))->GetZ();
359       for (i=1; i<fNTrackHits; i++) {
360         hitAdd = (AliMUONHitForRec*) ((*fTrackHitsPtr)[i]);
361         zEnd = hitAdd->GetZ();
362         if (zEnd < zbeg) break;
363       } //for (Int_t i=1;
364       currIndx = fNTrackHits - i; //???
365     }
366   }
367
368   while (ichamb>=iMin && ichamb<=iMax) {
369   // Find the closest hit in Z, not belonging to the current plane
370     if (Back) {
371       // backpropagation
372       hitAdd = (AliMUONHitForRec*) ((*fTrackHitsPtr)[fNTrackHits-currIndx]);
373       zEnd = hitAdd->GetZ();
374     } else {
375       zEnd = -9999;
376       for (ihit=currIndx+iFB; ihit>=0 && ihit<fgNOfPoints; ihit+=iFB) {
377         hitAdd = (AliMUONHitForRec*) ((*fgHitForRec)[ihit]);
378         //if (TMath::Abs(hitAdd->GetZ()-fPosition) > 0.1) {
379         if (TMath::Abs(hitAdd->GetZ()-fPosition) > 0.5) {
380           zEnd = hitAdd->GetZ();
381           currIndx = ihit;
382           break;
383         }
384       }
385     }
386     if (zEnd<-999 && ichamb==ichamEnd) endOfProp = 1; // end-of-propagation
387     else {
388       // Check if there is a missing chamber
389       if (zEnd<-999 || TMath::Abs(hitAdd->GetChamberNumber()-ichamb) > 1) {
390         if (!Back && zEnd>-999) currIndx -= iFB;
391         ichamb += iFB;
392         zEnd = (&(fgMUON->Chamber(ichamb)))->Z();
393         miss = kTRUE;
394       } else {
395         ichamb = hitAdd->GetChamberNumber();
396         miss = kFALSE;
397       }
398     }
399     if (ichamb<iMin || ichamb>iMax) break;
400     // Check for missing station 
401     if (!Back) {
402       dChamb = TMath::Abs(ichamb-ichambOK); 
403       if (dChamb > 1) {
404         dChambMiss = endOfProp;
405         //Check if (iFB > 0) dChambMiss++;
406         if (iFB > 0) {
407           if (TMath::Odd(ichambOK)) dChambMiss++;
408           else dChambMiss--;
409         }
410         //cout << dChamb << " " << ichambOK << " " << fgNOfPoints << endl;
411         if (TMath::Odd(ichambOK) && dChamb > 3-dChambMiss) {
412           // missing station - abandon track
413           //cout << dChamb << " " << ichambOK << " " << fgNOfPoints << " " << 1/(*fTrackPar)(4,0) << endl;
414           /*
415           for (Int_t i1=0; i1<fgNOfPoints; i1++) {
416             cout << " Hit #" << ((AliMUONHitForRec*)((*fgHitForRec)[i1]))->GetChamberNumber() << " ";
417             cout << ((AliMUONHitForRec*)((*fgHitForRec)[i1]))->GetBendingCoor() << " ";
418             cout << ((AliMUONHitForRec*)((*fgHitForRec)[i1]))->GetNonBendingCoor() << " ";
419             cout << ((AliMUONHitForRec*)((*fgHitForRec)[i1]))->GetZ() << " " << " ";
420             cout << ((AliMUONHitForRec*)((*fgHitForRec)[i1]))->GetTHTrack() << endl;
421           }
422           //cout << endl;
423           */
424           /*
425           cout << fNTrackHits << endl;
426           for (Int_t i1=0; i1<fNTrackHits; i1++) {
427             hit = (AliMUONHitForRec*) ((*fTrackHitsPtr)[i1]);
428             printf(" * %d %10.4f %10.4f %10.4f", 
429                    hit->GetChamberNumber(), hit->GetBendingCoor(), 
430                    hit->GetNonBendingCoor(), hit->GetZ());
431             if (fgEventReconstructor->GetRecGeantHits()) { 
432               // from GEANT hits
433               printf(" %3d %3d \n", hit->GetGeantSignal(), hit->GetTHTrack());
434             } else {
435               // from raw clusters
436               rawclusters = fgMUON->RawClustAddress(hit->GetChamberNumber());
437               clus = (AliMUONRawCluster*) rawclusters->UncheckedAt(hit->GetHitNumber());
438               printf("%3d", clus->fTracks[1]-1); 
439               if (clus->fTracks[2] != 0) printf("%3d \n", clus->fTracks[2]-1);
440               else printf("\n");
441             }
442           }
443           */
444           if (fNTrackHits>2 && fRecover==0 && !(ichambOK==((AliMUONHitForRec*)((*fTrackHitsPtr)[0]))->GetChamberNumber())) {
445             // try to recover track later
446             Recover();
447           } 
448           return kFALSE;
449         }
450         //Check else if (TMath::Even(ichambOK) && dChamb > 2-endOfProp) {
451         else if (TMath::Even(ichambOK) && dChamb > 2-dChambMiss) {
452           // missing station - abandon track
453           //cout << dChamb << " " << ichambOK << " " << fgNOfPoints << " " << 1/(*fTrackPar)(4,0) << endl;
454           /*
455           for (Int_t i1=0; i1<fgNOfPoints; i1++) {
456             cout << " Hit #" << ((AliMUONHitForRec*)((*fgHitForRec)[i1]))->GetChamberNumber() << " ";
457             cout << ((AliMUONHitForRec*)((*fgHitForRec)[i1]))->GetBendingCoor() << " ";
458             cout << ((AliMUONHitForRec*)((*fgHitForRec)[i1]))->GetNonBendingCoor() << " ";
459             cout << ((AliMUONHitForRec*)((*fgHitForRec)[i1]))->GetZ() << " " << " ";
460             cout << ((AliMUONHitForRec*)((*fgHitForRec)[i1]))->GetTHTrack() << endl;
461           }
462           //cout << endl;
463           */
464           /*
465           cout << fNTrackHits << endl;
466           for (Int_t i1=0; i1<fNTrackHits; i1++) {
467             hit = (AliMUONHitForRec*) ((*fTrackHitsPtr)[i1]);
468             printf(" * %d %10.4f %10.4f %10.4f", 
469                    hit->GetChamberNumber(), hit->GetBendingCoor(), 
470                    hit->GetNonBendingCoor(), hit->GetZ());
471             if (fgEventReconstructor->GetRecGeantHits()) { 
472               // from GEANT hits
473               printf(" %3d %3d \n", hit->GetGeantSignal(), hit->GetTHTrack());
474             } else {
475               // from raw clusters
476               rawclusters = fgMUON->RawClustAddress(hit->GetChamberNumber());
477               clus = (AliMUONRawCluster*) rawclusters->UncheckedAt(hit->GetHitNumber());
478               printf("%3d", clus->fTracks[1]-1); 
479               if (clus->fTracks[2] != 0) printf("%3d \n", clus->fTracks[2]-1);
480               else printf("\n");
481             }
482           }
483           */
484           if (fNTrackHits>2 && fRecover==0 && !(ichambOK==((AliMUONHitForRec*)((*fTrackHitsPtr)[0]))->GetChamberNumber())) {
485             // try to recover track later
486             Recover();
487           } 
488           return kFALSE;
489         }
490       }
491     }
492     if (endOfProp != 0) break;
493
494     // propagate to the found Z
495
496     // Check if track steps into dipole
497     if (fPosition>zDipole2 && zEnd<zDipole2) {
498       //LinearPropagation(zDipole2-zBeg); 
499       ParPropagation(zDipole2); 
500       MSThin(1); // multiple scattering in the chamber
501       WeightPropagation(zDipole2); // propagate weight matrix
502       fPosition = fPositionNew;
503       *fTrackPar = *fTrackParNew; 
504       //MagnetPropagation(zEnd); 
505       ParPropagation(zEnd); 
506       WeightPropagation(zEnd);
507       fPosition = fPositionNew;
508     } 
509     // Check if track steps out of dipole
510     else if (fPosition>zDipole1 && zEnd<zDipole1) {
511       //MagnetPropagation(zDipole1-zBeg); 
512       ParPropagation(zDipole1); 
513       MSThin(1); // multiple scattering in the chamber
514       WeightPropagation(zDipole1);
515       fPosition = fPositionNew;
516       *fTrackPar = *fTrackParNew; 
517       //LinearPropagation(zEnd-zDipole1); 
518       ParPropagation(zEnd); 
519       WeightPropagation(zEnd);
520       fPosition = fPositionNew;
521     } else {
522       ParPropagation(zEnd);
523       //MSThin(1); // multiple scattering in the chamber
524       if (TMath::Abs(zEnd-fPosition) > 5) MSThin(1); // multiple scattering in the chamber
525       WeightPropagation(zEnd);
526       fPosition = fPositionNew;
527     }
528
529     // Add measurement
530     if (fRecover != 0 && hitAdd == fSkipHit && !miss) {
531       // recovered track - remove the hit
532       miss = kTRUE;
533       ichamb = hitAdd->GetChamberNumber();
534       if (fRecover == 1) {
535         // remove the last hit
536         fTrackHitsPtr->Remove((TObjArray*)hitAdd); // remove hit
537         fNTrackHits --;
538         hitAdd->SetNTrackHits(hitAdd->GetNTrackHits()-1); // unmark hit 
539       } else {
540         // remove the hits
541         for (i=fNTrackHits-1; i>1; i--) {
542           hitAdd = (AliMUONHitForRec*)((*fTrackHitsPtr)[i]);
543           fTrackHitsPtr->Remove((TObjArray*)hitAdd); // remove hit
544           hitAdd->SetNTrackHits(hitAdd->GetNTrackHits()-1); // unmark hit 
545           fNTrackHits --;
546           if (hitAdd == fSkipHit) break;
547         } // for (i=fNTrackHits-1;
548       }
549       Back = kFALSE;
550       fRecover =0; // ????????? Dec-17-2001
551       ichambOK = ((AliMUONHitForRec*)((*fTrackHitsPtr)[fNTrackHits-1]))->GetChamberNumber();
552       currIndx = fgHitForRec->IndexOf(fSkipHit);
553     }
554
555     if (Back && !miss) {
556       // backward propagator
557       TMatrixD pointWeight(fgkSize,fgkSize);
558       TMatrixD point(fgkSize,1);
559       TMatrixD trackParTmp = point;
560       point(0,0) = hitAdd->GetBendingCoor();
561       point(1,0) = hitAdd->GetNonBendingCoor();
562       pointWeight(0,0) = 1/hitAdd->GetBendingReso2();
563       pointWeight(1,1) = 1/hitAdd->GetNonBendingReso2();
564       TryPoint(point,pointWeight,trackParTmp,dChi2);
565       *fTrackPar = trackParTmp;
566       *fWeight += pointWeight; 
567       fChi2 += dChi2; // Chi2
568       if (ichamb==ichamEnd) break; 
569       currIndx += iDindx;
570     } else {
571       // forward propagator
572       if (miss || !FindPoint(ichamb,zEnd,currIndx,iFB,hitAdd)) {
573         // missing point
574         *fTrackPar = *fTrackParNew; 
575       } else {
576         //add point
577         fTrackHitsPtr->Add((TObjArray*)hitAdd); // add hit
578         fNTrackHits ++;
579         hitAdd->SetNTrackHits(hitAdd->GetNTrackHits()+1); // mark hit as being on track
580         ichambOK = ichamb;
581         currIndx = fgHitForRec->IndexOf(hitAdd); // Check
582       }
583     }
584   } // while
585   cout << fNTrackHits << " " << fChi2 << " " << 1/(*fTrackPar)(4,0) << " " << fPosition << endl;
586   return success;
587 }
588
589   //__________________________________________________________________________
590 void AliMUONTrackK::ParPropagation(Double_t zEnd)
591 {
592   // Propagation of track parameters to zEnd
593   Int_t iFB, nTries;
594   Double_t dZ, step, distance, charge;
595   Double_t vGeant3[7], vGeant3New[7];
596
597   nTries = 0;
598   // First step using linear extrapolation
599   dZ = zEnd - fPosition;
600   iFB = (Int_t)TMath::Sign(Double_t(1.0),dZ);
601   step = dZ/TMath::Cos((*fTrackPar)(2,0))/TMath::Cos((*fTrackPar)(3,0)); // linear estimate
602   charge = iFB*TMath::Sign(Double_t(1.0),(*fTrackPar)(4,0));
603   fPositionNew = fPosition;
604   *fTrackParNew = *fTrackPar;
605   SetGeantParam(vGeant3,iFB);
606
607   // Check if overstep
608   do {
609     step = TMath::Abs(step);
610     // Propagate parameters
611     extrap_onestep_rungekutta(charge,step,vGeant3,vGeant3New);
612     distance = zEnd - vGeant3New[2];
613     step *= dZ/(vGeant3New[2]-fPositionNew);
614     nTries ++;
615   } while (distance*iFB < 0 && TMath::Abs(distance) > fgkEpsilon);
616
617   GetFromGeantParam(vGeant3New,iFB);
618
619   // Position ajustment (until within tolerance)
620   while (TMath::Abs(distance) > fgkEpsilon) {
621     dZ = zEnd - fPositionNew;
622     iFB = (Int_t)TMath::Sign(Double_t(1.0),dZ);
623     step = dZ/TMath::Cos((*fTrackParNew)(2,0))/TMath::Cos((*fTrackParNew)(3,0));
624     step = TMath::Abs(step);
625     SetGeantParam(vGeant3,iFB);
626     do {
627       // binary search
628       // Propagate parameters
629       extrap_onestep_rungekutta(charge,step,vGeant3,vGeant3New);
630       distance = zEnd - vGeant3New[2];
631       step /= 2;
632       nTries ++;
633       if (nTries > fgkTriesMax) {
634         cout << " ***** ParPropagation: too many tries " << nTries << endl;
635         exit(0);
636       }
637     } while (distance*iFB < 0);
638
639     GetFromGeantParam(vGeant3New,iFB);
640   }
641   //cout << nTries << endl;
642   return;
643 }
644 /*
645   //__________________________________________________________________________
646 void AliMUONTrackK::WeightPropagation(void)
647 {
648   // Propagation of the weight matrix
649   // W = DtWD, where D is Jacobian 
650
651   // !!! not implemented TMatrixD weight1(*fJacob,TMatrixD::kAtBA,*fWeight); // DtWD
652   TMatrixD weight1(*fWeight,TMatrixD::kMult,*fJacob); // WD
653   *fWeight = TMatrixD(*fJacob,TMatrixD::kTransposeMult,weight1); // DtWD
654   return;
655 }
656 */
657   //__________________________________________________________________________
658 void AliMUONTrackK::WeightPropagation(Double_t zEnd)
659 {
660   // Propagation of the weight matrix
661   // W = DtWD, where D is Jacobian 
662   Int_t i, j;
663   Double_t dPar;
664
665   TMatrixD jacob(fgkSize,fgkSize);
666   jacob = 0;
667
668   // Save initial and propagated parameters
669   TMatrixD trackPar0 = *fTrackPar;
670   TMatrixD trackParNew0 = *fTrackParNew;
671   Double_t savePosition = fPositionNew;
672
673   // Get covariance matrix
674   *fCovariance = *fWeight;
675   // check whether the Invert method returns flag if matrix cannot be inverted,
676   // and do not calculate the Determinant in that case !!!!
677   if (fCovariance->Determinant() != 0) {
678     //   fCovariance->Invert();
679     Int_t ifailCov;
680     mnvertLocalK(&((*fCovariance)(0,0)), fgkSize,fgkSize,fgkSize,ifailCov);
681   } else {
682     AliWarning(" Determinant fCovariance=0:");
683   }
684
685   // Loop over parameters to find change of the initial vs propagated ones
686   zEnd = fPosition;
687   fPosition = fPositionNew;
688   for (i=0; i<fgkSize; i++) {
689     dPar = TMath::Sqrt((*fCovariance)(i,i));
690     *fTrackPar = trackParNew0;
691     (*fTrackPar)(i,0) += dPar;
692     ParPropagation(zEnd);
693     for (j=0; j<fgkSize; j++) {
694       jacob(j,i) = ((*fTrackParNew)(j,0)-trackPar0(j,0))/dPar;
695     }
696   }
697
698   //jacob->Print();
699   //trackParNew0.Print();
700   //TMatrixD par1(jacob,TMatrixD::kMult,trackPar0); //
701   //par1.Print();
702   /*
703   if (jacob.Determinant() != 0) {
704     //  jacob.Invert();
705   } else {
706     cout << " ***** Warning in WeightPropagation: Determinant jacob=0:" << endl;
707   }
708   */
709   TMatrixD weight1(*fWeight,TMatrixD::kMult,jacob); // WD
710   *fWeight = TMatrixD(jacob,TMatrixD::kTransposeMult,weight1); // DtWD
711   //fWeight->Print();
712
713   // Restore initial and propagated parameters
714   *fTrackPar = trackPar0;
715   *fTrackParNew = trackParNew0;
716   fPosition = zEnd;
717   fPositionNew = savePosition;
718   return;
719 }
720
721   //__________________________________________________________________________
722 Bool_t AliMUONTrackK::FindPoint(Int_t ichamb, Double_t zEnd, Int_t currIndx, Int_t iFB, AliMUONHitForRec *&hitAdd)
723 {
724   // Picks up point within a window for the chamber No ichamb 
725   // Split the track if there are more than 1 hit
726   Int_t ihit, nRecTracks;
727   Double_t windowB, windowNonB, dChi2Tmp=0, dChi2, y, x, savePosition=0;
728   TClonesArray *trackPtr;
729   AliMUONHitForRec *hit, *hitLoop;
730   AliMUONTrackK *trackK;
731
732   Bool_t ok = kFALSE;
733   //sigmaB = fgEventReconstructor->GetBendingResolution(); // bending resolution
734   //sigmaNonB = fgEventReconstructor->GetNonBendingResolution(); // non-bending resolution
735   *fCovariance = *fWeight;
736   // check whether the Invert method returns flag if matrix cannot be inverted,
737   // and do not calculate the Determinant in that case !!!!
738   if (fCovariance->Determinant() != 0) {
739     //  fCovariance->Invert();
740
741       Int_t ifailCov;
742       mnvertLocalK(&((*fCovariance)(0,0)), fgkSize,fgkSize,fgkSize,ifailCov);
743   } else {
744     AliWarning("Determinant fCovariance=0:");
745   }
746   //windowB = fgkNSigma*TMath::Sqrt((*fCovariance)(0,0)+sigmaB*sigmaB);
747   //windowNonB = fgkNSigma*TMath::Sqrt((*fCovariance)(1,1)+sigmaNonB*sigmaNonB);
748   // Loop over all hits and take hits from the chamber
749   TMatrixD pointWeight(fgkSize,fgkSize);
750   TMatrixD saveWeight = pointWeight;
751   TMatrixD pointWeightTmp = pointWeight;
752   TMatrixD point(fgkSize,1);
753   TMatrixD trackPar = point;
754   TMatrixD trackParTmp = point;
755   Int_t nHitsOK = 0;
756
757   for (ihit=currIndx; ihit>=0 && ihit<fgNOfPoints; ihit+=iFB) {
758     hit = (AliMUONHitForRec*) ((*fgHitForRec)[ihit]);
759     if (hit->GetChamberNumber() == ichamb) {
760       //if (TMath::Abs(hit->GetZ()-zEnd) < 0.1) {
761       if (TMath::Abs(hit->GetZ()-zEnd) < 0.5) {
762         if (TMath::Abs(hit->GetZ()-zEnd) > 0.1) {
763           // adjust position: for multiple hits in the chamber
764           // (mostly (only?) for GEANT hits)
765           zEnd = hit->GetZ();
766           *fTrackPar = *fTrackParNew;
767           ParPropagation(zEnd);
768           WeightPropagation(zEnd);
769           fPosition = fPositionNew;
770           *fTrackPar = *fTrackParNew;
771           // Get covariance
772           *fCovariance = *fWeight;
773           if (fCovariance->Determinant() != 0) {
774             //fCovariance->Invert();
775             Int_t ifailCov;
776             mnvertLocalK(&((*fCovariance)(0,0)), fgkSize,fgkSize,fgkSize,ifailCov);
777           } else {
778             AliWarning("Determinant fCovariance=0:");
779           }
780         }
781         y = hit->GetBendingCoor();
782         x = hit->GetNonBendingCoor();
783         windowB = fgkNSigma*TMath::Sqrt((*fCovariance)(0,0)+hit->GetBendingReso2());
784         windowNonB = fgkNSigma*TMath::Sqrt((*fCovariance)(1,1)+hit->GetNonBendingReso2());
785         if (TMath::Abs((*fTrackParNew)(0,0)-y) <= windowB &&
786             TMath::Abs((*fTrackParNew)(1,0)-x) <= windowNonB) {
787           // Vector of measurements and covariance matrix
788           point.Zero();
789           point(0,0) = y;
790           point(1,0) = x;
791           pointWeight(0,0) = 1/hit->GetBendingReso2();
792           pointWeight(1,1) = 1/hit->GetNonBendingReso2();
793           TryPoint(point,pointWeight,trackPar,dChi2);
794           if (TMath::Abs(1./(trackPar)(4,0)) < fgEventReconstructor->GetMinBendingMomentum()) continue; // p < p_min - next hit
795           ok = kTRUE;
796           nHitsOK++;
797           //if (nHitsOK > -1) {
798           if (nHitsOK == 1) {
799             // Save current members
800             saveWeight = *fWeight;
801             savePosition = fPosition;
802             // temporary storage for the current track
803             dChi2Tmp = dChi2;
804             trackParTmp = trackPar;
805             pointWeightTmp = pointWeight;
806             hitAdd = hit;
807           } else {
808             // branching: create a new track
809             trackPtr = fgEventReconstructor->GetRecTracksPtr();
810             nRecTracks = fgEventReconstructor->GetNRecTracks();
811             trackK = new ((*trackPtr)[nRecTracks])
812                      AliMUONTrackK(*this); // dummy copy constructor
813             *trackK = *this;
814             fgEventReconstructor->SetNRecTracks(nRecTracks+1);
815             //cout << " ******** New track: " << ichamb << " " << hit->GetTHTrack() << " " << 1/(trackPar)(4,0) << " " << hit->GetBendingCoor() << " " << fNTrackHits << " " << nRecTracks << endl;
816             trackK->fRecover = 0;
817             *(trackK->fTrackPar) = trackPar;
818             *(trackK->fWeight) += pointWeight; 
819             trackK->fChi2 += dChi2;
820             // Mark hits as being on 2 tracks
821             for (Int_t i=0; i<fNTrackHits; i++) {
822               hitLoop = (AliMUONHitForRec*) ((*fTrackHitsPtr)[i]);
823               hitLoop->SetNTrackHits(hitLoop->GetNTrackHits()+1); 
824               /*
825               cout << " ** ";
826               cout << hitLoop->GetChamberNumber() << " ";
827               cout << hitLoop->GetBendingCoor() << " ";
828               cout << hitLoop->GetNonBendingCoor() << " ";
829               cout << hitLoop->GetZ() << " " << " ";
830               cout << hitLoop->GetGeantSignal() << " " << " ";
831               cout << hitLoop->GetTHTrack() << endl;
832               printf(" ** %d %10.4f %10.4f %10.4f %d %d \n", 
833                      hitLoop->GetChamberNumber(), hitLoop->GetBendingCoor(), 
834                      hitLoop->GetNonBendingCoor(), hitLoop->GetZ(), 
835                      hitLoop->GetGeantSignal(), hitLoop->GetTHTrack());
836               */
837             }
838             //add point
839             trackK->fTrackHitsPtr->Add((TObjArray*)hit); // add hit
840             trackK->fNTrackHits ++;
841             hit->SetNTrackHits(hit->GetNTrackHits()+1); // mark hit as being on track
842             if (ichamb == 9) {
843               // the last chamber
844               trackK->fTrackDir = -1;
845               trackK->fBPFlag = kTRUE; 
846             }
847           }
848         }
849       }
850     } else break; // different chamber
851   } // for (ihit=currIndx;
852   if (ok) {
853     *fTrackPar = trackParTmp;
854     *fWeight = saveWeight;
855     *fWeight += pointWeightTmp; 
856     fChi2 += dChi2Tmp; // Chi2
857     // Restore members
858     fPosition = savePosition;
859   }
860   return ok;
861 }
862
863   //__________________________________________________________________________
864 void AliMUONTrackK::TryPoint(TMatrixD &point, const TMatrixD &pointWeight, TMatrixD &trackParTmp, Double_t &dChi2)
865 {
866   // Adds a measurement point (modifies track parameters and computes
867   // change of Chi2)
868
869   // Solving linear system (W+U)p' = U(m-p) + (W+U)p
870   TMatrixD wu = *fWeight;
871   wu += pointWeight; // W+U
872   trackParTmp = point;
873   trackParTmp -= *fTrackParNew; // m-p
874   TMatrixD right(pointWeight,TMatrixD::kMult,trackParTmp); // U(m-p)
875   TMatrixD right1(wu,TMatrixD::kMult,*fTrackParNew); // (W+U)p
876   right += right1; // U(m-p) + (W+U)p
877
878   // check whether the Invert method returns flag if matrix cannot be inverted,
879   // and do not calculate the Determinant in that case !!!!
880   if (wu.Determinant() != 0) {
881
882     //  wu.Invert();
883      Int_t ifailWU;
884       mnvertLocalK(&((wu)(0,0)), fgkSize,fgkSize,fgkSize,ifailWU);
885   } else {
886     AliWarning("Determinant wu=0:");
887   }
888   trackParTmp = TMatrixD(wu,TMatrixD::kMult,right); 
889
890   right1 = trackParTmp;
891   right1 -= point; // p'-m
892   point = trackParTmp;
893   point -= *fTrackParNew; // p'-p
894   right = TMatrixD(*fWeight,TMatrixD::kMult,point); // W(p'-p)
895   TMatrixD value(point,TMatrixD::kTransposeMult,right); // (p'-p)'W(p'-p)
896   dChi2 = value(0,0);
897   right = TMatrixD(pointWeight,TMatrixD::kMult,right1); // U(p'-m)
898   value = TMatrixD(right1,TMatrixD::kTransposeMult,right); // (p'-m)'U(p'-m)
899   dChi2 += value(0,0);
900   return;
901 }
902
903   //__________________________________________________________________________
904 void AliMUONTrackK::MSThin(Int_t sign)
905 {
906   // Adds multiple scattering in a thin layer (only angles are affected)
907   Double_t cosAlph, cosBeta, momentum, velo, path, theta0;
908
909   // check whether the Invert method returns flag if matrix cannot be inverted,
910   // and do not calculate the Determinant in that case !!!!
911   if (fWeight->Determinant() != 0) {
912     //fWeight->Invert(); // covariance
913
914     Int_t ifailWeight;
915     mnvertLocalK(&((*fWeight)(0,0)), fgkSize,fgkSize,fgkSize,ifailWeight);
916   } else {
917     AliWarning("Determinant fWeight=0:");
918   }
919
920   cosAlph = TMath::Cos((*fTrackParNew)(2,0));
921   cosBeta = TMath::Cos((*fTrackParNew)(3,0));
922   momentum = 1/(*fTrackParNew)(4,0); // particle momentum
923   //velo = momentum/TMath::Sqrt(momentum*momentum+muonMass*muonMass); // velocity/c for muon hypothesis
924   velo = 1; // relativistic
925   path = fgEventReconstructor->GetChamberThicknessInX0()/cosAlph/cosBeta; // path length
926   theta0 = 0.0136/velo/momentum*TMath::Sqrt(path)*(1+0.038*TMath::Log(path)); // projected scattering angle
927
928   (*fWeight)(2,2) += sign*theta0/cosBeta*theta0/cosBeta; // alpha
929   (*fWeight)(3,3) += sign*theta0*theta0; // beta
930   //fWeight->Invert(); // weight
931
932   Int_t ifailWeight;
933   mnvertLocalK(&((*fWeight)(0,0)), fgkSize,fgkSize,fgkSize,ifailWeight);
934   return;
935 }
936   //__________________________________________________________________________
937 void AliMUONTrackK::StartBack(void)
938 {
939   // Starts backpropagator
940   
941   fBPFlag = kTRUE;
942   fChi2 = 0;
943   for (Int_t i=0; i<fgkSize; i++) {
944     for (Int_t j=0; j<fgkSize; j++) {
945       if (j==i) (*fWeight)(i,i) /= 100;
946       //if (j==i) (*fWeight)(i,i) /= fNTrackHits*fNTrackHits;
947       else (*fWeight)(j,i) = 0;
948     }
949   }
950 }
951
952   //__________________________________________________________________________
953 void AliMUONTrackK::SetGeantParam(Double_t *VGeant3, Int_t iFB)
954 {
955   // Set vector of Geant3 parameters pointed to by "VGeant3"
956   // from track parameters 
957
958   VGeant3[0] = (*fTrackParNew)(1,0); // X
959   VGeant3[1] = (*fTrackParNew)(0,0); // Y
960   VGeant3[2] = fPositionNew; // Z
961   VGeant3[3] = iFB*TMath::Sin((*fTrackParNew)(3,0)); // Px/Ptot
962   VGeant3[4] = iFB*TMath::Cos((*fTrackParNew)(3,0))*TMath::Sin((*fTrackParNew)(2,0)); // Py/Ptot
963   VGeant3[5] = iFB*TMath::Sqrt(1.0-VGeant3[3]*VGeant3[3]-VGeant3[4]*VGeant3[4]); // Pz/Ptot
964   VGeant3[6] = 1/TMath::Abs((*fTrackParNew)(4,0)); // Ptot
965 }
966
967   //__________________________________________________________________________
968 void AliMUONTrackK::GetFromGeantParam(Double_t *VGeant3, Int_t iFB)
969 {
970   // Get track parameters from vector of Geant3 parameters pointed 
971   // to by "VGeant3"
972
973   fPositionNew = VGeant3[2]; // Z
974   (*fTrackParNew)(0,0) = VGeant3[1]; // Y 
975   (*fTrackParNew)(1,0) = VGeant3[0]; // X
976   (*fTrackParNew)(3,0) = TMath::ASin(iFB*VGeant3[3]); // beta
977   (*fTrackParNew)(2,0) = TMath::ASin(iFB*VGeant3[4]/TMath::Cos((*fTrackParNew)(3,0))); // alpha
978   (*fTrackParNew)(4,0) = 1/VGeant3[6]*TMath::Sign(Double_t(1.0),(*fTrackPar)(4,0)); // 1/Ptot
979 }
980
981   //__________________________________________________________________________
982 void AliMUONTrackK::SetTrackQuality(Int_t iChi2)
983 {
984   // Computes "track quality" from Chi2 (if iChi2==0) or vice versa
985
986   if (fChi2 > 250) {
987     cout << " ***** Too high Chi2: " << fChi2 << endl;
988     fChi2 = 250;
989     //   exit(0);
990   }
991   if (iChi2 == 0) fChi2 = fNTrackHits + (250.-fChi2)/251;
992   else fChi2 = 250 - (fChi2-fNTrackHits)*251;
993 }
994
995   //__________________________________________________________________________
996 Int_t AliMUONTrackK::Compare(const TObject* trackK) const
997 {
998   // "Compare" function to sort with decreasing "track quality".
999   // Returns +1 (0, -1) if quality of current track
1000   // is smaller than (equal to, larger than) quality of trackK
1001
1002   if (fChi2 < ((AliMUONTrackK*)trackK)->fChi2) return(+1);
1003   else if (fChi2 == ((AliMUONTrackK*)trackK)->fChi2) return(0);
1004   else return(-1);
1005 }
1006
1007   //__________________________________________________________________________
1008 Bool_t AliMUONTrackK::KeepTrack(AliMUONTrackK* track0) const
1009 {
1010   // Check whether or not to keep current track 
1011   // (keep, if it has less than half of common hits with track0)
1012   Int_t hitsInCommon, nHits0, i, j, nTrackHits2;
1013   AliMUONHitForRec *hit0, *hit1;
1014
1015   hitsInCommon = 0;
1016   nHits0 = track0->fNTrackHits;
1017   nTrackHits2 = fNTrackHits/2;
1018
1019   for (i=0; i<nHits0; i++) {
1020     // Check if hit belongs to several tracks
1021     hit0 = (AliMUONHitForRec*) (*track0->fTrackHitsPtr)[i]; 
1022     if (hit0->GetNTrackHits() == 1) continue; 
1023     for (j=0; j<fNTrackHits; j++) {
1024       hit1 = (AliMUONHitForRec*) (*fTrackHitsPtr)[j]; 
1025       if (hit1->GetNTrackHits() == 1) continue; 
1026       if (hit0 == hit1) {
1027         hitsInCommon++;
1028         if (hitsInCommon >= nTrackHits2) return kFALSE;
1029         break;
1030       }
1031     } // for (j=0; 
1032   } // for (i=0; 
1033   return kTRUE;
1034 }
1035
1036   //__________________________________________________________________________
1037 void AliMUONTrackK::Kill(void)
1038 {
1039   // Kill track candidate
1040   Int_t i;
1041   AliMUONHitForRec *hit;
1042
1043   if (fTrackHitsPtr) {
1044     // Remove track mark from hits
1045     for (i=0; i<fNTrackHits; i++) {
1046       hit = (AliMUONHitForRec*) (*fTrackHitsPtr)[i]; 
1047       hit->SetNTrackHits(hit->GetNTrackHits()-1); 
1048     }
1049   }
1050   fgEventReconstructor->GetRecTracksPtr()->Remove(this);
1051 }
1052
1053   //__________________________________________________________________________
1054 void AliMUONTrackK::Branson(void)
1055 {
1056   // Propagates track to the vertex thru absorber using Branson correction
1057   // (makes use of the AliMUONTrackParam class)
1058  
1059   AliMUONTrackParam *trackParam = new AliMUONTrackParam();
1060   trackParam->SetBendingCoor((*fTrackPar)(0,0));
1061   trackParam->SetNonBendingCoor((*fTrackPar)(1,0));
1062   trackParam->SetBendingSlope(TMath::Tan((*fTrackPar)(2,0)));
1063   trackParam->SetNonBendingSlope(TMath::Tan((*fTrackPar)(3,0))/TMath::Cos((*fTrackPar)(2,0)));
1064   trackParam->SetInverseBendingMomentum((*fTrackPar)(4,0)/TMath::Cos((*fTrackPar)(3,0)));
1065   trackParam->SetZ(fPosition);
1066
1067   trackParam->ExtrapToVertex();
1068
1069   (*fTrackPar)(0,0) = trackParam->GetBendingCoor();
1070   (*fTrackPar)(1,0) = trackParam->GetNonBendingCoor();
1071   (*fTrackPar)(2,0) = TMath::ATan(trackParam->GetBendingSlope());
1072   (*fTrackPar)(3,0) = TMath::ATan(TMath::Cos((*fTrackPar)(2,0))*trackParam->GetNonBendingSlope());
1073   (*fTrackPar)(4,0) = TMath::Cos((*fTrackPar)(3,0))*trackParam->GetInverseBendingMomentum();
1074   fPosition = trackParam->GetZ();
1075   delete trackParam;
1076   cout << 1/(*fTrackPar)(4,0) << " " << fPosition << " " << (*fTrackPar)(0,0) << endl;
1077
1078   // Get covariance matrix
1079   *fCovariance = *fWeight;
1080   if (fCovariance->Determinant() != 0) {
1081     //    fCovariance->Invert();
1082
1083       Int_t ifailCov;
1084       mnvertLocalK(&((*fCovariance)(0,0)), fgkSize,fgkSize,fgkSize,ifailCov);
1085   } else {
1086     AliWarning("Determinant fCovariance=0:");
1087   }
1088 }
1089
1090   //__________________________________________________________________________
1091 void AliMUONTrackK::GoToZ(Double_t zEnd)
1092 {
1093   // Propagates track to given Z
1094
1095   ParPropagation(zEnd);
1096   MSThin(1); // multiple scattering in the chamber
1097   WeightPropagation(zEnd);
1098   fPosition = fPositionNew;
1099   *fTrackPar = *fTrackParNew; 
1100 }
1101
1102   //__________________________________________________________________________
1103 void AliMUONTrackK::GoToVertex(void)
1104 {
1105   // Version 3.08
1106   // Propagates track to the vertex
1107   // All material constants are taken from AliRoot
1108
1109     static Double_t x01[5] = { 24.282,  // C
1110                                24.282,  // C
1111                                11.274,  // Concrete
1112                                 1.758,  // Fe 
1113                                 1.758}; // Fe (cm)
1114   // inner part theta < 3 degrees
1115     static Double_t x02[5] = { 30413,  // Air
1116                                24.282, // C
1117                                11.274, // Concrete
1118                                1.758,  // Fe
1119                                0.369}; // W (cm)
1120   // z positions of the materials inside the absober outer part theta > 3 degres
1121   static Double_t zPos[10] = {90, 105, 315, 443, 468};
1122   // R > 1
1123   // R < 1
1124
1125   Double_t dZ, r0Norm, x0, deltaP, dChi2, pTotal, pOld;
1126   AliMUONHitForRec *hit;
1127   AliMUONRawCluster *clus;
1128   TClonesArray *rawclusters;
1129
1130   // First step to the rear end of the absorber
1131   Double_t zRear = 503;
1132   GoToZ(zRear);
1133   Double_t tan3 = TMath::Tan(3./180*TMath::Pi());
1134
1135   // Go through absorber
1136   pOld = 1/(*fTrackPar)(4,0);
1137   Double_t r0Rear = (*fTrackPar)(0,0)*(*fTrackPar)(0,0) + 
1138                     (*fTrackPar)(1,0)*(*fTrackPar)(1,0);
1139   r0Rear = TMath::Sqrt(r0Rear)/fPosition/tan3;
1140   r0Norm = r0Rear;
1141   for (Int_t i=4; i>=0; i--) {
1142     ParPropagation(zPos[i]);
1143     WeightPropagation(zPos[i]);
1144     dZ = TMath::Abs (fPositionNew-fPosition);
1145     if (r0Norm > 1) x0 = x01[i];
1146     else x0 = x02[i];
1147     MSLine(dZ,x0); // multiple scattering in the medium (linear approximation)
1148     fPosition = fPositionNew;
1149     *fTrackPar = *fTrackParNew; 
1150     r0Norm = (*fTrackPar)(0,0)*(*fTrackPar)(0,0) + 
1151              (*fTrackPar)(1,0)*(*fTrackPar)(1,0);
1152     r0Norm = TMath::Sqrt(r0Norm)/fPosition/tan3;
1153   }
1154   // Correct momentum for energy losses
1155   pTotal = 1/TMath::Abs((*fTrackPar)(4,0));
1156   Double_t p0 = pTotal;
1157   for (Int_t j=0; j<2; j++) {
1158     /*
1159     if (r0Rear > 1) {
1160       if (p0 < 20) {
1161         deltaP = 2.164 + 0.145e-1*p0 - 0.417e-3*p0*p0;
1162       } else {
1163         deltaP = 2.275 + 0.102e-2*p0 - 0.674e-6*p0*p0;
1164       }
1165     } else {
1166       if (p0 < 20) {
1167         deltaP = 2.581 + 0.188e-1*p0 - 0.398e-3*p0*p0;
1168       } else {
1169         deltaP = 2.727 + 0.356e-2*p0 + 0.242e-5*p0*p0;
1170       }
1171     }
1172     */
1173     if (r0Rear < 1) {
1174       //W
1175       if (p0<15) {
1176         deltaP = 2.737 + 0.0494*p0 - 0.001123*p0*p0;
1177       } else {
1178         deltaP = 3.0643 + 0.01346*p0;
1179       }
1180     } else {
1181       //Pb
1182       if (p0<15) {
1183         deltaP  = 2.1380 + 0.0351*p0 - 0.000853*p0*p0;
1184       } else {
1185         deltaP = 2.407 + 0.00702*p0;
1186       }
1187     }
1188
1189     p0 = pTotal + deltaP/TMath::Cos((*fTrackPar)(2,0))/TMath::Cos((*fTrackPar)(3,0));
1190   }
1191   (*fTrackPar)(4,0) = 1/p0*TMath::Sign((Double_t)1.,(*fTrackPar)(4,0));
1192
1193   // Go to the vertex
1194   ParPropagation((Double_t)0.);
1195   WeightPropagation((Double_t)0.);
1196   fPosition = fPositionNew;
1197   //*fTrackPar = *fTrackParNew; 
1198   // Add vertex as a hit
1199   TMatrixD pointWeight(fgkSize,fgkSize);
1200   TMatrixD point(fgkSize,1);
1201   TMatrixD trackParTmp = point;
1202   point(0,0) = 0; // vertex coordinate - should be taken somewhere
1203   point(1,0) = 0; // vertex coordinate - should be taken somewhere
1204   pointWeight(0,0) = 1/1.e-3/1.e-3; // 10 um error
1205   pointWeight(1,1) = 1/1.e-3/1.e-3; // 10 um error
1206   TryPoint(point,pointWeight,trackParTmp,dChi2);
1207   *fTrackPar = trackParTmp;
1208   *fWeight += pointWeight; 
1209   fChi2 += dChi2; // Chi2
1210   cout << pOld << " " << 1/(*fTrackPar)(4,0) << " " << dChi2 << " " << fChi2 << " " << fNTrackHits << endl;
1211   for (Int_t i1=0; i1<fNTrackHits; i1++) {
1212     hit =  (AliMUONHitForRec*) ((*fTrackHitsPtr)[i1]);
1213     printf ("%4d", hit->GetChamberNumber()); 
1214     //cout << ((AliMUONHitForRec*)((*fTrackHitsPtr)[i1]))->GetChamberNumber() << " ";
1215   }
1216   cout << endl;
1217   for (Int_t i1=0; i1<fNTrackHits; i1++) {
1218     hit =  (AliMUONHitForRec*) ((*fTrackHitsPtr)[i1]);
1219     //cout << ((AliMUONHitForRec*)((*fTrackHitsPtr)[i1]))->GetHitNumber() << " ";
1220     //cout << ((AliMUONHitForRec*)((*fTrackHitsPtr)[i1]))->GetZ() << " ";
1221     printf ("%4d", fgHitForRec->IndexOf(hit)); 
1222     //cout << fgHitForRec->IndexOf(((AliMUONHitForRec*)((*fTrackHitsPtr)[i1]))) << " ";
1223   }
1224   cout << endl;
1225   if (fgEventReconstructor->GetRecGeantHits()) { 
1226       // from GEANT hits
1227     for (Int_t i1=0; i1<fNTrackHits; i1++) {
1228       hit =  (AliMUONHitForRec*) ((*fTrackHitsPtr)[i1]);
1229       cout << hit->GetTHTrack() + hit->GetGeantSignal()*10000 << " ";
1230     }
1231   } else {
1232     // from raw clusters
1233     for (Int_t i1=0; i1<fNTrackHits; i1++) {
1234       hit =  (AliMUONHitForRec*) ((*fTrackHitsPtr)[i1]);
1235       rawclusters = fgMUON->GetMUONData()->RawClusters(hit->GetChamberNumber());
1236       clus = (AliMUONRawCluster*) rawclusters->UncheckedAt(hit->GetHitNumber());
1237       printf ("%4d", clus->GetTrack(1) - 1); 
1238       //cout << clus->fTracks[1] - 1 << " ";
1239     }
1240     cout << endl;
1241     for (Int_t i1=0; i1<fNTrackHits; i1++) {
1242       hit =  (AliMUONHitForRec*) ((*fTrackHitsPtr)[i1]);
1243       rawclusters = fgMUON->GetMUONData()->RawClusters(hit->GetChamberNumber());
1244       clus = (AliMUONRawCluster*) rawclusters->UncheckedAt(hit->GetHitNumber());
1245       if (clus->GetTrack(2) != 0) printf ("%4d", clus->GetTrack(2) - 1);
1246       else printf ("%4s", "   ");
1247       //if (clus->fTracks[2] != 0) cout << clus->fTracks[2] - 1 << " ";
1248     }
1249   }
1250   cout << endl;
1251   for (Int_t i1=0; i1<fNTrackHits; i1++) {
1252     //cout << ((AliMUONHitForRec*)((*fTrackHitsPtr)[i1]))->GetHitNumber() << " ";
1253     cout << ((AliMUONHitForRec*)((*fTrackHitsPtr)[i1]))->GetZ() << " ";
1254     //cout << fgHitForRec->IndexOf(((AliMUONHitForRec*)((*fTrackHitsPtr)[i1]))) << " ";
1255   }
1256   cout << endl;
1257   cout << "---------------------------------------------------" << endl;
1258
1259   // Get covariance matrix
1260   *fCovariance = *fWeight;
1261   if (fCovariance->Determinant() != 0) {
1262     //   fCovariance->Invert();
1263
1264       Int_t ifailCov;
1265       mnvertLocalK(&((*fCovariance)(0,0)), fgkSize,fgkSize,fgkSize,ifailCov);
1266   } else {
1267    AliWarning("Determinant fCovariance=0:" );
1268   }
1269 }
1270
1271   //__________________________________________________________________________
1272 void AliMUONTrackK::MSLine(Double_t dZ, Double_t x0)
1273 {
1274   // Adds multiple scattering in a thick layer for linear propagation
1275
1276   Double_t cosAlph = TMath::Cos((*fTrackPar)(2,0));
1277   Double_t tanAlph = TMath::Tan((*fTrackPar)(2,0));
1278   Double_t cosBeta = TMath::Cos((*fTrackPar)(3,0));
1279   Double_t sinBeta;
1280   sinBeta = TMath::Sin((*fTrackPar)(3,0));
1281   Double_t tanBeta = TMath::Tan((*fTrackPar)(3,0));
1282   Double_t momentum = 1/(*fTrackPar)(4,0);
1283   Double_t velo = 1; // relativistic velocity
1284   Double_t step = TMath::Abs(dZ)/cosAlph/cosBeta; // step length
1285
1286   // Projected scattering angle
1287   Double_t theta0 = 0.0136/velo/momentum/TMath::Sqrt(x0)*(1+0.038*TMath::Log(step/x0)); 
1288   Double_t theta02 = theta0*theta0;
1289   Double_t dl2 = step*step/2*theta02;
1290   Double_t dl3 = dl2*step*2/3;
1291
1292   //Derivatives
1293   Double_t dYdT = 1/cosAlph;
1294   Double_t dYdB = 0; //(*fTrackPar)(2,0)*sinBeta/cosAlph;
1295   Double_t dXdT = tanAlph*tanBeta;
1296   //Double_t dXdB = (1+(*fTrackPar)(2,0)*tanAlph*sinBeta*sinBeta)/cosBeta;
1297   Double_t dXdB = 1/cosBeta;
1298   Double_t dAdT = 1/cosBeta;
1299   Double_t dAdB = 0; //(*fTrackPar)(2,0)*tanBeta;
1300
1301   // Get covariance matrix
1302   *fCovariance = *fWeight;
1303   if (fCovariance->Determinant() != 0) {
1304     //   fCovariance->Invert();
1305
1306        Int_t ifailCov;
1307        mnvertLocalK(&((*fCovariance)(0,0)), fgkSize,fgkSize,fgkSize,ifailCov);
1308   } else {
1309     AliWarning("Determinant fCovariance=0:" );
1310   }
1311
1312   (*fCovariance)(0,0) += dl3*(dYdT*dYdT+dYdB*dYdB); // <yy>
1313   (*fCovariance)(1,1) += dl3*(dXdT*dXdT+dXdB*dXdB); // <xx>
1314   (*fCovariance)(2,2) += theta02*step*(dAdT*dAdT+dAdB*dAdB); // <aa>
1315   (*fCovariance)(3,3) += theta02*step; // <bb>
1316
1317   (*fCovariance)(0,1) += dl3*(dYdT*dXdT+dYdB*dXdB); // <yx>
1318   (*fCovariance)(1,0) = (*fCovariance)(0,1);
1319
1320   (*fCovariance)(0,2) += dl2*(dYdT*dAdT+dYdB*dAdB); // <ya>
1321   (*fCovariance)(2,0) = (*fCovariance)(0,2);
1322
1323   (*fCovariance)(0,3) += dl2*dYdB; // <yb>
1324   (*fCovariance)(3,0) = (*fCovariance)(0,3);
1325
1326   (*fCovariance)(1,2) += dl2*(dXdT*dAdT+dXdB*dAdB); // <xa>
1327   (*fCovariance)(2,1) = (*fCovariance)(1,2);
1328
1329   (*fCovariance)(1,3) += dl2*dXdB; // <xb>
1330   (*fCovariance)(3,1) = (*fCovariance)(1,3);
1331
1332   (*fCovariance)(2,3) += theta02*step*dAdB; // <ab>
1333   (*fCovariance)(3,2) = (*fCovariance)(2,3);
1334
1335   // Get weight matrix
1336   *fWeight = *fCovariance;
1337   if (fWeight->Determinant() != 0) {
1338     //  fWeight->Invert();
1339
1340        Int_t ifailWeight;
1341        mnvertLocalK(&((*fWeight)(0,0)), fgkSize,fgkSize,fgkSize,ifailWeight);
1342   } else {
1343     AliWarning("Determinant fWeight=0:");
1344   }
1345 }
1346  
1347   //__________________________________________________________________________
1348 void AliMUONTrackK::Recover(void)
1349 {
1350   // Adds new failed track(s) which can be tried to be recovered
1351   Int_t nRecTracks, ichamb;
1352   TClonesArray *trackPtr;
1353   AliMUONTrackK *trackK;
1354
1355   //cout << " ******** Enter Recover " << endl;
1356   //return;
1357   trackPtr = fgEventReconstructor->GetRecTracksPtr();
1358
1359   // The last hit will be removed
1360   nRecTracks = fgEventReconstructor->GetNRecTracks();
1361
1362   // Check if the track candidate doesn't exist yet
1363   for (Int_t i=0; i<nRecTracks; i++) {
1364     trackK = (AliMUONTrackK*) ((*trackPtr)[i]);
1365     if (trackK->fNTrackHits == 2 && trackK->GetRecover() == 0) continue;
1366     if (trackK == this) continue;
1367     //if (trackK->GetRecover() != 1) continue;
1368     if (trackK->fNTrackHits >= fNTrackHits-1) {
1369       /*
1370       for (Int_t j=0; j<fNTrackHits-1; j++) {
1371         if ((*trackK->fTrackHitsPtr)[j] != ((*fTrackHitsPtr)[j])) break;
1372         return;
1373       } // for (Int_t j=0;
1374       */
1375       if ((*trackK->fTrackHitsPtr)[0] == ((*fTrackHitsPtr)[0])) return;
1376     }
1377   } // for (Int_t i=0;
1378
1379   cout << " ******** Enter Recover " << endl;
1380   trackK = new ((*trackPtr)[nRecTracks]) AliMUONTrackK(fStartSegment); 
1381   fgEventReconstructor->SetNRecTracks(nRecTracks+1);
1382   trackK->fRecover = 1;
1383   trackK->fSkipHit = (AliMUONHitForRec*) ((*fTrackHitsPtr)[fNTrackHits-1]);
1384   trackK->fNTrackHits = fNTrackHits;
1385   delete trackK->fTrackHitsPtr; // not efficient ?
1386   trackK->fTrackHitsPtr = new TObjArray(*fTrackHitsPtr);
1387   cout << nRecTracks << " " << trackK->fRecover << endl;
1388
1389   // The hit before missing chamber will be removed
1390   Int_t ichamBeg = ((AliMUONHitForRec*)((*fTrackHitsPtr)[0]))->GetChamberNumber();
1391   Int_t indxSkip = -1;
1392   if (ichamBeg == 9) {
1393     // segment in the last station
1394     // look for the missing chamber
1395     for (Int_t i=1; i<fNTrackHits; i++) {
1396       ichamb = ((AliMUONHitForRec*)((*fTrackHitsPtr)[i]))->GetChamberNumber();
1397       if (TMath::Abs(ichamBeg-ichamb)>1 && i>2) {
1398         indxSkip = i;
1399         break;
1400       }
1401       ichamBeg = ichamb;
1402     } // for (Int_t i=1;
1403   } else {
1404     // in the last but one station
1405     for (Int_t i=1; i<fNTrackHits; i++) {
1406       ichamb = ((AliMUONHitForRec*)((*fTrackHitsPtr)[i]))->GetChamberNumber();
1407       if (TMath::Abs(ichamBeg-ichamb)>1 && ichamb<4) {
1408         indxSkip = i;
1409         break;
1410       }
1411       ichamBeg = ichamb;
1412     } // for (Int_t i=1;
1413   }
1414   if (indxSkip < 0) return;
1415   
1416   // Check if the track candidate doesn't exist yet
1417   for (Int_t i=0; i<nRecTracks; i++) {
1418     trackK = (AliMUONTrackK*) ((*trackPtr)[i]);
1419     if (trackK->fNTrackHits == 2 && trackK->GetRecover() == 0) continue;
1420     if (trackK == this) continue;
1421     //if (trackK->GetRecover() != 1) continue;
1422     if (trackK->fNTrackHits >= indxSkip-1) {
1423       /*
1424       for (Int_t j=0; j<indxSkip-1; j++) {
1425         if ((*trackK->fTrackHitsPtr)[j] != ((*fTrackHitsPtr)[j])) break;
1426         return;
1427       } // for (Int_t j=0;
1428       */
1429       if ((*trackK->fTrackHitsPtr)[0] == ((*fTrackHitsPtr)[0])) return;
1430     }
1431   } // for (Int_t i=0;
1432
1433   nRecTracks = fgEventReconstructor->GetNRecTracks();
1434   trackK = new ((*trackPtr)[nRecTracks]) AliMUONTrackK(fStartSegment); 
1435   fgEventReconstructor->SetNRecTracks(nRecTracks+1);
1436   trackK->fRecover = 2;
1437   trackK->fSkipHit = (AliMUONHitForRec*) ((*fTrackHitsPtr)[indxSkip-1]);
1438   trackK->fNTrackHits = fNTrackHits;
1439   delete trackK->fTrackHitsPtr; // not efficient ?
1440   trackK->fTrackHitsPtr = new TObjArray(*fTrackHitsPtr);
1441   cout << nRecTracks << " " << trackK->fRecover << endl;
1442 }
1443
1444 //______________________________________________________________________________
1445  void mnvertLocalK(Double_t *a, Int_t l, Int_t, Int_t n, Int_t &ifail)
1446 {
1447 //*-*-*-*-*-*-*-*-*-*-*-*Inverts a symmetric matrix*-*-*-*-*-*-*-*-*-*-*-*-*
1448 //*-*                    ==========================
1449 //*-*        inverts a symmetric matrix.   matrix is first scaled to
1450 //*-*        have all ones on the diagonal (equivalent to change of units)
1451 //*-*        but no pivoting is done since matrix is positive-definite.
1452 //*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
1453
1454   // taken from TMinuit package of Root (l>=n)
1455   // fVERTs, fVERTq and fVERTpp changed to localVERTs, localVERTq and localVERTpp
1456   //  Double_t localVERTs[n], localVERTq[n], localVERTpp[n];
1457   Double_t * localVERTs = new Double_t[n];
1458   Double_t * localVERTq = new Double_t[n];
1459   Double_t * localVERTpp = new Double_t[n];
1460   // fMaxint changed to localMaxint
1461   Int_t localMaxint = n;
1462
1463     /* System generated locals */
1464     Int_t aOffset;
1465
1466     /* Local variables */
1467     Double_t si;
1468     Int_t i, j, k, kp1, km1;
1469
1470     /* Parameter adjustments */
1471     aOffset = l + 1;
1472     a -= aOffset;
1473
1474     /* Function Body */
1475     ifail = 0;
1476     if (n < 1) goto L100;
1477     if (n > localMaxint) goto L100;
1478 //*-*-                  scale matrix by sqrt of diag elements
1479     for (i = 1; i <= n; ++i) {
1480         si = a[i + i*l];
1481         if (si <= 0) goto L100;
1482         localVERTs[i-1] = 1 / TMath::Sqrt(si);
1483     }
1484     for (i = 1; i <= n; ++i) {
1485         for (j = 1; j <= n; ++j) {
1486             a[i + j*l] = a[i + j*l]*localVERTs[i-1]*localVERTs[j-1];
1487         }
1488     }
1489 //*-*-                                       . . . start main loop . . . .
1490     for (i = 1; i <= n; ++i) {
1491         k = i;
1492 //*-*-                  preparation for elimination step1
1493         if (a[k + k*l] != 0) localVERTq[k-1] = 1 / a[k + k*l];
1494         else goto L100;
1495         localVERTpp[k-1] = 1;
1496         a[k + k*l] = 0;
1497         kp1 = k + 1;
1498         km1 = k - 1;
1499         if (km1 < 0) goto L100;
1500         else if (km1 == 0) goto L50;
1501         else               goto L40;
1502 L40:
1503         for (j = 1; j <= km1; ++j) {
1504             localVERTpp[j-1] = a[j + k*l];
1505             localVERTq[j-1]  = a[j + k*l]*localVERTq[k-1];
1506             a[j + k*l]   = 0;
1507         }
1508 L50:
1509         if (k - n < 0) goto L51;
1510         else if (k - n == 0) goto L60;
1511         else                goto L100;
1512 L51:
1513         for (j = kp1; j <= n; ++j) {
1514             localVERTpp[j-1] = a[k + j*l];
1515             localVERTq[j-1]  = -a[k + j*l]*localVERTq[k-1];
1516             a[k + j*l]   = 0;
1517         }
1518 //*-*-                  elimination proper
1519 L60:
1520         for (j = 1; j <= n; ++j) {
1521             for (k = j; k <= n; ++k) { a[j + k*l] += localVERTpp[j-1]*localVERTq[k-1]; }
1522         }
1523     }
1524 //*-*-                  elements of left diagonal and unscaling
1525     for (j = 1; j <= n; ++j) {
1526         for (k = 1; k <= j; ++k) {
1527             a[k + j*l] = a[k + j*l]*localVERTs[k-1]*localVERTs[j-1];
1528             a[j + k*l] = a[k + j*l];
1529         }
1530     }
1531     delete localVERTs;
1532     delete localVERTq;
1533     delete localVERTpp;
1534     return;
1535 //*-*-                  failure return
1536 L100:
1537     delete localVERTs;
1538     delete localVERTq;
1539     delete localVERTpp;
1540     ifail = 1;
1541 } /* mnvertLocal */