]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONTrackExtrap.cxx
Do not update AMORE pool with an empty object
[u/mrichter/AliRoot.git] / MUON / AliMUONTrackExtrap.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id$ */
17
18 //-----------------------------------------------------------------------------
19 // Class AliMUONTrackExtrap
20 // ------------------------
21 // Tools for track extrapolation in ALICE dimuon spectrometer
22 // Author: Philippe Pillot
23 //-----------------------------------------------------------------------------
24
25 #include "AliMUONTrackExtrap.h" 
26 #include "AliMUONTrackParam.h"
27 #include "AliMUONConstants.h"
28 #include "AliMUONReconstructor.h"
29
30 #include "AliMagF.h"
31 #include "AliExternalTrackParam.h"
32
33 #include <TGeoGlobalMagField.h>
34 #include <TGeoManager.h>
35 #include <TMath.h>
36 #include <TDatabasePDG.h>
37
38 #include <Riostream.h>
39
40 /// \cond CLASSIMP
41 ClassImp(AliMUONTrackExtrap) // Class implementation in ROOT context
42 /// \endcond
43
44 const Double_t AliMUONTrackExtrap::fgkSimpleBPosition = 0.5 * (AliMUONConstants::CoilZ() + AliMUONConstants::YokeZ());
45 const Double_t AliMUONTrackExtrap::fgkSimpleBLength = 0.5 * (AliMUONConstants::CoilL() + AliMUONConstants::YokeL());
46       Double_t AliMUONTrackExtrap::fgSimpleBValue = 0.;
47       Bool_t   AliMUONTrackExtrap::fgFieldON = kFALSE;
48 const Bool_t   AliMUONTrackExtrap::fgkUseHelix = kFALSE;
49 const Int_t    AliMUONTrackExtrap::fgkMaxStepNumber = 5000;
50 const Double_t AliMUONTrackExtrap::fgkHelixStepLength = 6.;
51 const Double_t AliMUONTrackExtrap::fgkRungeKuttaMaxResidue = 0.002;
52
53 //__________________________________________________________________________
54 void AliMUONTrackExtrap::SetField()
55 {
56   /// set field on/off flag;  
57   /// set field at the centre of the dipole
58   const Double_t x[3] = {50.,50.,fgkSimpleBPosition};
59   Double_t b[3] = {0.,0.,0.};
60   TGeoGlobalMagField::Instance()->Field(x,b);
61   fgSimpleBValue = b[0];
62   fgFieldON = fgSimpleBValue ? kTRUE : kFALSE;
63   
64 }
65
66 //__________________________________________________________________________
67 Double_t AliMUONTrackExtrap::GetImpactParamFromBendingMomentum(Double_t bendingMomentum)
68 {
69   /// Returns impact parameter at vertex in bending plane (cm),
70   /// from the signed bending momentum "BendingMomentum" in bending plane (GeV/c),
71   /// using simple values for dipole magnetic field.
72   /// The sign of "BendingMomentum" is the sign of the charge.
73   
74   if (bendingMomentum == 0.) return 1.e10;
75   
76   const Double_t kCorrectionFactor = 1.1; // impact parameter is 10% underestimated
77   
78   return kCorrectionFactor * (-0.0003 * fgSimpleBValue * fgkSimpleBLength * fgkSimpleBPosition / bendingMomentum);
79 }
80
81 //__________________________________________________________________________
82 Double_t 
83 AliMUONTrackExtrap::GetBendingMomentumFromImpactParam(Double_t impactParam)
84 {
85   /// Returns signed bending momentum in bending plane (GeV/c),
86   /// the sign being the sign of the charge for particles moving forward in Z,
87   /// from the impact parameter "ImpactParam" at vertex in bending plane (cm),
88   /// using simple values for dipole magnetic field.
89   
90   if (impactParam == 0.) return 1.e10;
91   
92   const Double_t kCorrectionFactor = 1.1; // bending momentum is 10% underestimated
93   
94   if (fgFieldON) 
95   {
96     return kCorrectionFactor * (-0.0003 * fgSimpleBValue * fgkSimpleBLength * fgkSimpleBPosition / impactParam);
97   }
98   else 
99   {
100     return AliMUONConstants::GetMostProbBendingMomentum();
101   }
102 }
103
104 //__________________________________________________________________________
105 void AliMUONTrackExtrap::LinearExtrapToZ(AliMUONTrackParam* trackParam, Double_t zEnd)
106 {
107   /// Track parameters linearly extrapolated to the plane at "zEnd".
108   /// On return, results from the extrapolation are updated in trackParam.
109   
110   if (trackParam->GetZ() == zEnd) return; // nothing to be done if same z
111   
112   // Compute track parameters
113   Double_t dZ = zEnd - trackParam->GetZ();
114   trackParam->SetNonBendingCoor(trackParam->GetNonBendingCoor() + trackParam->GetNonBendingSlope() * dZ);
115   trackParam->SetBendingCoor(trackParam->GetBendingCoor() + trackParam->GetBendingSlope() * dZ);
116   trackParam->SetZ(zEnd);
117 }
118
119 //__________________________________________________________________________
120 void AliMUONTrackExtrap::LinearExtrapToZCov(AliMUONTrackParam* trackParam, Double_t zEnd, Bool_t updatePropagator)
121 {
122   /// Track parameters and their covariances linearly extrapolated to the plane at "zEnd".
123   /// On return, results from the extrapolation are updated in trackParam.
124   
125   if (trackParam->GetZ() == zEnd) return; // nothing to be done if same z
126   
127   // No need to propagate the covariance matrix if it does not exist
128   if (!trackParam->CovariancesExist()) {
129     cout<<"W-AliMUONTrackExtrap::LinearExtrapToZCov: Covariance matrix does not exist"<<endl;
130     // Extrapolate linearly track parameters to "zEnd"
131     LinearExtrapToZ(trackParam,zEnd);
132     return;
133   }
134   
135   // Compute track parameters
136   Double_t dZ = zEnd - trackParam->GetZ();
137   trackParam->SetNonBendingCoor(trackParam->GetNonBendingCoor() + trackParam->GetNonBendingSlope() * dZ);
138   trackParam->SetBendingCoor(trackParam->GetBendingCoor() + trackParam->GetBendingSlope() * dZ);
139   trackParam->SetZ(zEnd);
140   
141   // Calculate the jacobian related to the track parameters linear extrapolation to "zEnd"
142   TMatrixD jacob(5,5);
143   jacob.UnitMatrix();
144   jacob(0,1) = dZ;
145   jacob(2,3) = dZ;
146   
147   // Extrapolate track parameter covariances to "zEnd"
148   TMatrixD tmp(trackParam->GetCovariances(),TMatrixD::kMultTranspose,jacob);
149   TMatrixD tmp2(jacob,TMatrixD::kMult,tmp);
150   trackParam->SetCovariances(tmp2);
151   
152   // Update the propagator if required
153   if (updatePropagator) trackParam->UpdatePropagator(jacob);
154 }
155
156 //__________________________________________________________________________
157 Bool_t AliMUONTrackExtrap::ExtrapToZ(AliMUONTrackParam* trackParam, Double_t zEnd)
158 {
159   /// Interface to track parameter extrapolation to the plane at "Z" using Helix or Rungekutta algorithm.
160   /// On return, the track parameters resulting from the extrapolation are updated in trackParam.
161   if (!fgFieldON) {
162     AliMUONTrackExtrap::LinearExtrapToZ(trackParam,zEnd);
163     return kTRUE;
164   }
165   else if (fgkUseHelix) return AliMUONTrackExtrap::ExtrapToZHelix(trackParam,zEnd);
166   else return AliMUONTrackExtrap::ExtrapToZRungekutta(trackParam,zEnd);
167 }
168
169 //__________________________________________________________________________
170 Bool_t AliMUONTrackExtrap::ExtrapToZHelix(AliMUONTrackParam* trackParam, Double_t zEnd)
171 {
172   /// Track parameter extrapolation to the plane at "Z" using Helix algorithm.
173   /// On return, the track parameters resulting from the extrapolation are updated in trackParam.
174   if (trackParam->GetZ() == zEnd) return kTRUE; // nothing to be done if same Z
175   Double_t forwardBackward; // +1 if forward, -1 if backward
176   if (zEnd < trackParam->GetZ()) forwardBackward = 1.0; // spectro. z<0 
177   else forwardBackward = -1.0;
178   Double_t v3[7], v3New[7]; // 7 in parameter ????
179   Int_t i3, stepNumber;
180   // For safety: return kTRUE or kFALSE ????
181   // Parameter vector for calling EXTRAP_ONESTEP
182   ConvertTrackParamForExtrap(trackParam, forwardBackward, v3);
183   // sign of charge (sign of fInverseBendingMomentum if forward motion)
184   // must be changed if backward extrapolation
185   Double_t chargeExtrap = forwardBackward * TMath::Sign(Double_t(1.0), trackParam->GetInverseBendingMomentum());
186   // Extrapolation loop
187   stepNumber = 0;
188   while (((-forwardBackward * (v3[2] - zEnd)) <= 0.0) && (stepNumber < fgkMaxStepNumber)) { // spectro. z<0
189     stepNumber++;
190     ExtrapOneStepHelix(chargeExtrap, fgkHelixStepLength, v3, v3New);
191     if ((-forwardBackward * (v3New[2] - zEnd)) > 0.0) break; // one is beyond Z spectro. z<0
192                                                              // better use TArray ????
193     for (i3 = 0; i3 < 7; i3++) {v3[i3] = v3New[i3];}
194   }
195   // check fgkMaxStepNumber ????
196   // Interpolation back to exact Z (2nd order)
197   // should be in function ???? using TArray ????
198   Double_t dZ12 = v3New[2] - v3[2]; // 1->2
199   if (TMath::Abs(dZ12) > 0) {
200     Double_t dZ1i = zEnd - v3[2]; // 1-i
201     Double_t dZi2 = v3New[2] - zEnd; // i->2
202     Double_t xPrime = (v3New[0] - v3[0]) / dZ12;
203     Double_t xSecond = ((v3New[3] / v3New[5]) - (v3[3] / v3[5])) / dZ12;
204     Double_t yPrime = (v3New[1] - v3[1]) / dZ12;
205     Double_t ySecond = ((v3New[4] / v3New[5]) - (v3[4] / v3[5])) / dZ12;
206     v3[0] = v3[0] + xPrime * dZ1i - 0.5 * xSecond * dZ1i * dZi2; // X
207     v3[1] = v3[1] + yPrime * dZ1i - 0.5 * ySecond * dZ1i * dZi2; // Y
208     v3[2] = zEnd; // Z
209     Double_t xPrimeI = xPrime - 0.5 * xSecond * (dZi2 - dZ1i);
210     Double_t yPrimeI = yPrime - 0.5 * ySecond * (dZi2 - dZ1i);
211     // (PX, PY, PZ)/PTOT assuming forward motion
212     v3[5] = 1.0 / TMath::Sqrt(1.0 + xPrimeI * xPrimeI + yPrimeI * yPrimeI); // PZ/PTOT
213     v3[3] = xPrimeI * v3[5]; // PX/PTOT
214     v3[4] = yPrimeI * v3[5]; // PY/PTOT
215   } else {
216     cout<<"W-AliMUONTrackExtrap::ExtrapToZHelix: Extrap. to Z not reached, Z = "<<zEnd<<endl;
217   }
218   // Recover track parameters (charge back for forward motion)
219   RecoverTrackParam(v3, chargeExtrap * forwardBackward, trackParam);
220   return kTRUE;
221 }
222
223 //__________________________________________________________________________
224 Bool_t AliMUONTrackExtrap::ExtrapToZRungekutta(AliMUONTrackParam* trackParam, Double_t zEnd)
225 {
226   /// Track parameter extrapolation to the plane at "Z" using Rungekutta algorithm.
227   /// On return, the track parameters resulting from the extrapolation are updated in trackParam.
228   if (trackParam->GetZ() == zEnd) return kTRUE; // nothing to be done if same Z
229   Double_t forwardBackward; // +1 if forward, -1 if backward
230   if (zEnd < trackParam->GetZ()) forwardBackward = 1.0; // spectro. z<0 
231   else forwardBackward = -1.0;
232   // sign of charge (sign of fInverseBendingMomentum if forward motion)
233   // must be changed if backward extrapolation
234   Double_t chargeExtrap = forwardBackward * TMath::Sign(Double_t(1.0), trackParam->GetInverseBendingMomentum());
235   Double_t v3[7], v3New[7];
236   Double_t dZ, step;
237   Int_t stepNumber = 0;
238   
239   // Extrapolation loop (until within tolerance or the track turn around)
240   Double_t residue = zEnd - trackParam->GetZ();
241   Bool_t uturn = kFALSE;
242   Bool_t tooManyStep = kFALSE;
243   while (TMath::Abs(residue) > fgkRungeKuttaMaxResidue && stepNumber <= fgkMaxStepNumber) {
244     
245     dZ = zEnd - trackParam->GetZ();
246     // step lenght assuming linear trajectory
247     step = dZ * TMath::Sqrt(1.0 + trackParam->GetBendingSlope()*trackParam->GetBendingSlope() +
248                             trackParam->GetNonBendingSlope()*trackParam->GetNonBendingSlope());
249     ConvertTrackParamForExtrap(trackParam, forwardBackward, v3);
250     
251     do { // reduce step lenght while zEnd oversteped
252       if (stepNumber > fgkMaxStepNumber) {
253         cout<<"W-AliMUONTrackExtrap::ExtrapToZRungekutta: Too many trials: "<<stepNumber<<endl;
254         tooManyStep = kTRUE;
255         break;
256       }
257       stepNumber ++;
258       step = TMath::Abs(step);
259       AliMUONTrackExtrap::ExtrapOneStepRungekutta(chargeExtrap,step,v3,v3New);
260       residue = zEnd - v3New[2];
261       step *= dZ/(v3New[2]-trackParam->GetZ());
262     } while (residue*dZ < 0 && TMath::Abs(residue) > fgkRungeKuttaMaxResidue);
263     
264     if (v3New[5]*v3[5] < 0) { // the track turned around
265       cout<<"W-AliMUONTrackExtrap::ExtrapToZRungekutta: The track turned around"<<endl;
266       uturn = kTRUE;
267       break;
268     } else RecoverTrackParam(v3New, chargeExtrap * forwardBackward, trackParam);
269     
270   }
271   
272   // terminate the extropolation with a straight line up to the exact "zEnd" value
273   if (uturn) {
274     
275     // track ends +-100 meters away in the bending direction
276     dZ = zEnd - v3[2];
277     Double_t bendingSlope = TMath::Sign(1.e4,-fgSimpleBValue*trackParam->GetInverseBendingMomentum()) / dZ;
278     Double_t pZ = TMath::Abs(1. / trackParam->GetInverseBendingMomentum()) / TMath::Sqrt(1.0 + bendingSlope * bendingSlope);
279     Double_t nonBendingSlope = TMath::Sign(TMath::Abs(v3[3]) * v3[6] / pZ, trackParam->GetNonBendingSlope());
280     trackParam->SetNonBendingCoor(trackParam->GetNonBendingCoor() + dZ * nonBendingSlope);
281     trackParam->SetNonBendingSlope(nonBendingSlope);
282     trackParam->SetBendingCoor(trackParam->GetBendingCoor() + dZ * bendingSlope);
283     trackParam->SetBendingSlope(bendingSlope);
284     trackParam->SetZ(zEnd);
285     
286     return kFALSE;
287     
288   } else {
289     
290     // track extrapolated normally
291     trackParam->SetNonBendingCoor(trackParam->GetNonBendingCoor() + residue * trackParam->GetNonBendingSlope());
292     trackParam->SetBendingCoor(trackParam->GetBendingCoor() + residue * trackParam->GetBendingSlope());
293     trackParam->SetZ(zEnd);
294     
295     return !tooManyStep;
296     
297   }
298   
299 }
300
301 //__________________________________________________________________________
302 void AliMUONTrackExtrap::ConvertTrackParamForExtrap(AliMUONTrackParam* trackParam, Double_t forwardBackward, Double_t *v3)
303 {
304   /// Set vector of Geant3 parameters pointed to by "v3" from track parameters in trackParam.
305   /// Since AliMUONTrackParam is only geometry, one uses "forwardBackward"
306   /// to know whether the particle is going forward (+1) or backward (-1).
307   v3[0] = trackParam->GetNonBendingCoor(); // X
308   v3[1] = trackParam->GetBendingCoor(); // Y
309   v3[2] = trackParam->GetZ(); // Z
310   Double_t pYZ = TMath::Abs(1.0 / trackParam->GetInverseBendingMomentum());
311   Double_t pZ = pYZ / TMath::Sqrt(1.0 + trackParam->GetBendingSlope() * trackParam->GetBendingSlope());
312   v3[6] = TMath::Sqrt(pYZ * pYZ + pZ * pZ * trackParam->GetNonBendingSlope() * trackParam->GetNonBendingSlope()); // PTOT
313   v3[5] = -forwardBackward * pZ / v3[6]; // PZ/PTOT spectro. z<0
314   v3[3] = trackParam->GetNonBendingSlope() * v3[5]; // PX/PTOT
315   v3[4] = trackParam->GetBendingSlope() * v3[5]; // PY/PTOT
316 }
317
318 //__________________________________________________________________________
319 void AliMUONTrackExtrap::RecoverTrackParam(Double_t *v3, Double_t charge, AliMUONTrackParam* trackParam)
320 {
321   /// Set track parameters in trackParam from Geant3 parameters pointed to by "v3",
322   /// assumed to be calculated for forward motion in Z.
323   /// "InverseBendingMomentum" is signed with "charge".
324   trackParam->SetNonBendingCoor(v3[0]); // X
325   trackParam->SetBendingCoor(v3[1]); // Y
326   trackParam->SetZ(v3[2]); // Z
327   Double_t pYZ = v3[6] * TMath::Sqrt((1.-v3[3])*(1.+v3[3]));
328   trackParam->SetInverseBendingMomentum(charge/pYZ);
329   trackParam->SetBendingSlope(v3[4]/v3[5]);
330   trackParam->SetNonBendingSlope(v3[3]/v3[5]);
331 }
332
333 //__________________________________________________________________________
334 Bool_t AliMUONTrackExtrap::ExtrapToZCov(AliMUONTrackParam* trackParam, Double_t zEnd, Bool_t updatePropagator)
335 {
336   /// Track parameters and their covariances extrapolated to the plane at "zEnd".
337   /// On return, results from the extrapolation are updated in trackParam.
338   
339   if (trackParam->GetZ() == zEnd) return kTRUE; // nothing to be done if same z
340   
341   if (!fgFieldON) { // linear extrapolation if no magnetic field
342     AliMUONTrackExtrap::LinearExtrapToZCov(trackParam,zEnd,updatePropagator);
343     return kTRUE;
344   }
345   
346   // No need to propagate the covariance matrix if it does not exist
347   if (!trackParam->CovariancesExist()) {
348     cout<<"W-AliMUONTrackExtrap::ExtrapToZCov: Covariance matrix does not exist"<<endl;
349     // Extrapolate track parameters to "zEnd"
350     return ExtrapToZ(trackParam,zEnd);
351   }
352   
353   // Save the actual track parameters
354   AliMUONTrackParam trackParamSave(*trackParam);
355   TMatrixD paramSave(trackParamSave.GetParameters());
356   Double_t zBegin = trackParamSave.GetZ();
357   
358   // Get reference to the parameter covariance matrix
359   const TMatrixD& kParamCov = trackParam->GetCovariances();
360         
361   // Extrapolate track parameters to "zEnd"
362   // Do not update the covariance matrix if the extrapolation failed
363   if (!ExtrapToZ(trackParam,zEnd)) return kFALSE;
364   
365   // Get reference to the extrapolated parameters
366   const TMatrixD& extrapParam = trackParam->GetParameters();
367   
368   // Calculate the jacobian related to the track parameters extrapolation to "zEnd"
369   Bool_t extrapStatus = kTRUE;
370   TMatrixD jacob(5,5);
371   jacob.Zero();
372   TMatrixD dParam(5,1);
373   Double_t direction[5] = {-1.,-1.,1.,1.,-1.};
374   for (Int_t i=0; i<5; i++) {
375     // Skip jacobian calculation for parameters with no associated error
376     if (kParamCov(i,i) <= 0.) continue;
377     
378     // Small variation of parameter i only
379     for (Int_t j=0; j<5; j++) {
380       if (j==i) {
381         dParam(j,0) = TMath::Sqrt(kParamCov(i,i));
382         dParam(j,0) *= TMath::Sign(1.,direction[j]*paramSave(j,0)); // variation always in the same direction
383       } else dParam(j,0) = 0.;
384     }
385     
386     // Set new parameters
387     trackParamSave.SetParameters(paramSave);
388     trackParamSave.AddParameters(dParam);
389     trackParamSave.SetZ(zBegin);
390     
391     // Extrapolate new track parameters to "zEnd"
392     if (!ExtrapToZ(&trackParamSave,zEnd)) {
393       cout<<"W-AliMUONTrackExtrap::ExtrapToZCov: Bad covariance matrix"<<endl;
394       extrapStatus = kFALSE;
395     }
396     
397     // Calculate the jacobian
398     TMatrixD jacobji(trackParamSave.GetParameters(),TMatrixD::kMinus,extrapParam);
399     jacobji *= 1. / dParam(i,0);
400     jacob.SetSub(0,i,jacobji);
401   }
402   
403   // Extrapolate track parameter covariances to "zEnd"
404   TMatrixD tmp(kParamCov,TMatrixD::kMultTranspose,jacob);
405   TMatrixD tmp2(jacob,TMatrixD::kMult,tmp);
406   trackParam->SetCovariances(tmp2);
407   
408   // Update the propagator if required
409   if (updatePropagator) trackParam->UpdatePropagator(jacob);
410   
411   return extrapStatus;
412 }
413
414 //__________________________________________________________________________
415 void AliMUONTrackExtrap::AddMCSEffectInAbsorber(AliMUONTrackParam* param, Double_t signedPathLength, Double_t f0, Double_t f1, Double_t f2)
416 {
417   /// Add to the track parameter covariances the effects of multiple Coulomb scattering
418   /// signedPathLength must have the sign of (zOut - zIn) where all other parameters are assumed to be given at zOut.
419   
420   // absorber related covariance parameters
421   Double_t bendingSlope = param->GetBendingSlope();
422   Double_t nonBendingSlope = param->GetNonBendingSlope();
423   Double_t inverseBendingMomentum = param->GetInverseBendingMomentum();
424   Double_t alpha2 = 0.0136 * 0.0136 * inverseBendingMomentum * inverseBendingMomentum * (1.0 + bendingSlope * bendingSlope) /
425                     (1.0 + bendingSlope *bendingSlope + nonBendingSlope * nonBendingSlope); // velocity = 1
426   Double_t pathLength = TMath::Abs(signedPathLength);
427   Double_t varCoor = alpha2 * (pathLength * pathLength * f0 - 2. * pathLength * f1 + f2);
428   Double_t covCorrSlope = TMath::Sign(1.,signedPathLength) * alpha2 * (pathLength * f0 - f1);
429   Double_t varSlop = alpha2 * f0;
430   
431   // Set MCS covariance matrix
432   TMatrixD newParamCov(param->GetCovariances());
433   // Non bending plane
434   newParamCov(0,0) += varCoor;       newParamCov(0,1) += covCorrSlope;
435   newParamCov(1,0) += covCorrSlope;  newParamCov(1,1) += varSlop;
436   // Bending plane
437   newParamCov(2,2) += varCoor;       newParamCov(2,3) += covCorrSlope;
438   newParamCov(3,2) += covCorrSlope;  newParamCov(3,3) += varSlop;
439   
440   // Set momentum related covariances if B!=0
441   if (fgFieldON) {
442     // compute derivative d(q/Pxy) / dSlopeX and d(q/Pxy) / dSlopeY
443     Double_t dqPxydSlopeX = inverseBendingMomentum * nonBendingSlope / (1. + nonBendingSlope*nonBendingSlope + bendingSlope*bendingSlope);
444     Double_t dqPxydSlopeY = - inverseBendingMomentum * nonBendingSlope*nonBendingSlope * bendingSlope /
445                               (1. + bendingSlope*bendingSlope) / (1. + nonBendingSlope*nonBendingSlope + bendingSlope*bendingSlope);
446     // Inverse bending momentum (due to dependences with bending and non bending slopes)
447     newParamCov(4,0) += dqPxydSlopeX * covCorrSlope; newParamCov(0,4) += dqPxydSlopeX * covCorrSlope;
448     newParamCov(4,1) += dqPxydSlopeX * varSlop;      newParamCov(1,4) += dqPxydSlopeX * varSlop;
449     newParamCov(4,2) += dqPxydSlopeY * covCorrSlope; newParamCov(2,4) += dqPxydSlopeY * covCorrSlope;
450     newParamCov(4,3) += dqPxydSlopeY * varSlop;      newParamCov(3,4) += dqPxydSlopeY * varSlop;
451     newParamCov(4,4) += (dqPxydSlopeX*dqPxydSlopeX + dqPxydSlopeY*dqPxydSlopeY) * varSlop;
452   }
453   
454   // Set new covariances
455   param->SetCovariances(newParamCov);
456 }
457
458 //__________________________________________________________________________
459 void AliMUONTrackExtrap::CorrectMCSEffectInAbsorber(AliMUONTrackParam* param,
460                                                     Double_t xVtx, Double_t yVtx, Double_t zVtx,
461                                                     Double_t errXVtx, Double_t errYVtx,
462                                                     Double_t absZBeg, Double_t pathLength, Double_t f0, Double_t f1, Double_t f2)
463 {
464   /// Correct parameters and corresponding covariances using Branson correction
465   /// - input param are parameters and covariances at the end of absorber
466   /// - output param are parameters and covariances at vertex
467   /// Absorber correction parameters are supposed to be calculated at the current track z-position
468   
469   // Position of the Branson plane (spectro. (z<0))
470   Double_t zB = (f1>0.) ? absZBeg - f2/f1 : 0.;
471   
472   // Add MCS effects to current parameter covariances (spectro. (z<0))
473   AddMCSEffectInAbsorber(param, -pathLength, f0, f1, f2);
474   
475   // Get track parameters and covariances in the Branson plane corrected for magnetic field effect
476   ExtrapToZCov(param,zVtx);
477   LinearExtrapToZCov(param,zB);
478   
479   // compute track parameters at vertex
480   TMatrixD newParam(5,1);
481   newParam(0,0) = xVtx;
482   newParam(1,0) = (param->GetNonBendingCoor() - xVtx) / (zB - zVtx);
483   newParam(2,0) = yVtx;
484   newParam(3,0) = (param->GetBendingCoor() - yVtx) / (zB - zVtx);
485   newParam(4,0) = param->GetCharge() / param->P() *
486                   TMath::Sqrt(1.0 + newParam(1,0)*newParam(1,0) + newParam(3,0)*newParam(3,0)) /
487                   TMath::Sqrt(1.0 + newParam(3,0)*newParam(3,0));
488   
489   // Get covariances in (X, SlopeX, Y, SlopeY, q*PTot) coordinate system
490   TMatrixD paramCovP(param->GetCovariances());
491   Cov2CovP(param->GetParameters(),paramCovP);
492   
493   // Get the covariance matrix in the (XVtx, X, YVtx, Y, q*PTot) coordinate system
494   TMatrixD paramCovVtx(5,5);
495   paramCovVtx.Zero();
496   paramCovVtx(0,0) = errXVtx * errXVtx;
497   paramCovVtx(1,1) = paramCovP(0,0);
498   paramCovVtx(2,2) = errYVtx * errYVtx;
499   paramCovVtx(3,3) = paramCovP(2,2);
500   paramCovVtx(4,4) = paramCovP(4,4);
501   paramCovVtx(1,3) = paramCovP(0,2);
502   paramCovVtx(3,1) = paramCovP(2,0);
503   paramCovVtx(1,4) = paramCovP(0,4);
504   paramCovVtx(4,1) = paramCovP(4,0);
505   paramCovVtx(3,4) = paramCovP(2,4);
506   paramCovVtx(4,3) = paramCovP(4,2);
507   
508   // Jacobian of the transformation (XVtx, X, YVtx, Y, q*PTot) -> (XVtx, SlopeXVtx, YVtx, SlopeYVtx, q*PTotVtx)
509   TMatrixD jacob(5,5);
510   jacob.UnitMatrix();
511   jacob(1,0) = - 1. / (zB - zVtx);
512   jacob(1,1) = 1. / (zB - zVtx);
513   jacob(3,2) = - 1. / (zB - zVtx);
514   jacob(3,3) = 1. / (zB - zVtx);
515   
516   // Compute covariances at vertex in the (XVtx, SlopeXVtx, YVtx, SlopeYVtx, q*PTotVtx) coordinate system
517   TMatrixD tmp(paramCovVtx,TMatrixD::kMultTranspose,jacob);
518   TMatrixD newParamCov(jacob,TMatrixD::kMult,tmp);
519   
520   // Compute covariances at vertex in the (XVtx, SlopeXVtx, YVtx, SlopeYVtx, q/PyzVtx) coordinate system
521   CovP2Cov(newParam,newParamCov);
522   
523   // Set parameters and covariances at vertex
524   param->SetParameters(newParam);
525   param->SetZ(zVtx);
526   param->SetCovariances(newParamCov);
527 }
528
529 //__________________________________________________________________________
530 void AliMUONTrackExtrap::CorrectELossEffectInAbsorber(AliMUONTrackParam* param, Double_t eLoss, Double_t sigmaELoss2)
531 {
532   /// Correct parameters for energy loss and add energy loss fluctuation effect to covariances
533   
534   // Get parameter covariances in (X, SlopeX, Y, SlopeY, q*PTot) coordinate system
535   TMatrixD newParamCov(param->GetCovariances());
536   Cov2CovP(param->GetParameters(),newParamCov);
537   
538   // Compute new parameters corrected for energy loss
539   Double_t muMass = TDatabasePDG::Instance()->GetParticle("mu-")->Mass(); // GeV
540   Double_t p = param->P();
541   Double_t e = TMath::Sqrt(p*p + muMass*muMass);
542   Double_t eCorr = e + eLoss;
543   Double_t pCorr = TMath::Sqrt(eCorr*eCorr - muMass*muMass);
544   Double_t nonBendingSlope = param->GetNonBendingSlope();
545   Double_t bendingSlope = param->GetBendingSlope();
546   param->SetInverseBendingMomentum(param->GetCharge() / pCorr *
547                                    TMath::Sqrt(1.0 + nonBendingSlope*nonBendingSlope + bendingSlope*bendingSlope) /
548                                    TMath::Sqrt(1.0 + bendingSlope*bendingSlope));
549   
550   // Add effects of energy loss fluctuation to covariances
551   newParamCov(4,4) += eCorr * eCorr / pCorr / pCorr * sigmaELoss2;
552   
553   // Get new parameter covariances in (X, SlopeX, Y, SlopeY, q/Pyz) coordinate system
554   CovP2Cov(param->GetParameters(),newParamCov);
555   
556   // Set new parameter covariances
557   param->SetCovariances(newParamCov);
558 }
559
560 //__________________________________________________________________________
561 Bool_t AliMUONTrackExtrap::GetAbsorberCorrectionParam(Double_t trackXYZIn[3], Double_t trackXYZOut[3], Double_t pTotal,
562                                                       Double_t &pathLength, Double_t &f0, Double_t &f1, Double_t &f2,
563                                                       Double_t &meanRho, Double_t &totalELoss, Double_t &sigmaELoss2)
564 {
565   /// Parameters used to correct for Multiple Coulomb Scattering and energy loss in absorber
566   /// Calculated assuming a linear propagation from trackXYZIn to trackXYZOut (order is important)
567   // pathLength: path length between trackXYZIn and trackXYZOut (cm)
568   // f0:         0th moment of z calculated with the inverse radiation-length distribution
569   // f1:         1st moment of z calculated with the inverse radiation-length distribution
570   // f2:         2nd moment of z calculated with the inverse radiation-length distribution
571   // meanRho:    average density of crossed material (g/cm3)
572   // totalELoss: total energy loss in absorber
573   
574   // Reset absorber's parameters
575   pathLength = 0.;
576   f0 = 0.;
577   f1 = 0.;
578   f2 = 0.;
579   meanRho = 0.;
580   totalELoss = 0.;
581   sigmaELoss2 = 0.;
582   
583   // Check whether the geometry is available
584   if (!gGeoManager) {
585     cout<<"E-AliMUONTrackExtrap::GetAbsorberCorrectionParam: no TGeo"<<endl;
586     return kFALSE;
587   }
588   
589   // Initialize starting point and direction
590   pathLength = TMath::Sqrt((trackXYZOut[0] - trackXYZIn[0])*(trackXYZOut[0] - trackXYZIn[0])+
591                            (trackXYZOut[1] - trackXYZIn[1])*(trackXYZOut[1] - trackXYZIn[1])+
592                            (trackXYZOut[2] - trackXYZIn[2])*(trackXYZOut[2] - trackXYZIn[2]));
593   if (pathLength < TGeoShape::Tolerance()) return kFALSE;
594   Double_t b[3];
595   b[0] = (trackXYZOut[0] - trackXYZIn[0]) / pathLength;
596   b[1] = (trackXYZOut[1] - trackXYZIn[1]) / pathLength;
597   b[2] = (trackXYZOut[2] - trackXYZIn[2]) / pathLength;
598   TGeoNode *currentnode = gGeoManager->InitTrack(trackXYZIn, b);
599   if (!currentnode) {
600     cout<<"E-AliMUONTrackExtrap::GetAbsorberCorrectionParam: start point out of geometry"<<endl;
601     return kFALSE;
602   }
603   
604   // loop over absorber slices and calculate absorber's parameters
605   Double_t rho = 0.; // material density (g/cm3)
606   Double_t x0 = 0.;  // radiation-length (cm-1)
607   Double_t atomicA = 0.; // A of material
608   Double_t atomicZ = 0.; // Z of material
609   Double_t atomicZoverA = 0.; // Z/A of material
610   Double_t localPathLength = 0;
611   Double_t remainingPathLength = pathLength;
612   Double_t zB = trackXYZIn[2];
613   Double_t zE, dzB, dzE;
614   do {
615     // Get material properties
616     TGeoMaterial *material = currentnode->GetVolume()->GetMedium()->GetMaterial();
617     rho = material->GetDensity();
618     x0 = material->GetRadLen();
619     atomicA = material->GetA();
620     atomicZ = material->GetZ();
621     if(material->IsMixture()){
622       TGeoMixture * mixture = (TGeoMixture*)material;
623       atomicZoverA = 0.;
624       Double_t sum = 0.;
625       for (Int_t iel=0;iel<mixture->GetNelements();iel++){
626         sum  += mixture->GetWmixt()[iel];
627         atomicZoverA += mixture->GetZmixt()[iel]*mixture->GetWmixt()[iel]/mixture->GetAmixt()[iel];
628       }
629       atomicZoverA/=sum;
630     }
631     else atomicZoverA = atomicZ/atomicA;
632     
633     // Get path length within this material
634     gGeoManager->FindNextBoundary(remainingPathLength);
635     localPathLength = gGeoManager->GetStep() + 1.e-6;
636     // Check if boundary within remaining path length. If so, make sure to cross the boundary to prepare the next step
637     if (localPathLength >= remainingPathLength) localPathLength = remainingPathLength;
638     else {
639       currentnode = gGeoManager->Step();
640       if (!currentnode) {
641         cout<<"E-AliMUONTrackExtrap::GetAbsorberCorrectionParam: navigation failed"<<endl;
642         f0 = f1 = f2 = meanRho = totalELoss = sigmaELoss2 = 0.;
643         return kFALSE;
644       }
645       if (!gGeoManager->IsEntering()) {
646         // make another small step to try to enter in new absorber slice
647         gGeoManager->SetStep(0.001);
648         currentnode = gGeoManager->Step();
649         if (!gGeoManager->IsEntering() || !currentnode) {
650           cout<<"E-AliMUONTrackExtrap::GetAbsorberCorrectionParam: navigation failed"<<endl;
651           f0 = f1 = f2 = meanRho = totalELoss = sigmaELoss2 = 0.;
652           return kFALSE;
653         }
654         localPathLength += 0.001;
655       }
656     }
657     
658     // calculate absorber's parameters
659     zE = b[2] * localPathLength + zB;
660     dzB = zB - trackXYZIn[2];
661     dzE = zE - trackXYZIn[2];
662     f0 += localPathLength / x0;
663     f1 += (dzE*dzE - dzB*dzB) / b[2] / b[2] / x0 / 2.;
664     f2 += (dzE*dzE*dzE - dzB*dzB*dzB) / b[2] / b[2] / b[2] / x0 / 3.;
665     meanRho += localPathLength * rho;
666     totalELoss += BetheBloch(pTotal, localPathLength, rho, atomicZ, atomicZoverA);
667     sigmaELoss2 += EnergyLossFluctuation2(pTotal, localPathLength, rho, atomicZoverA);
668     
669     // prepare next step
670     zB = zE;
671     remainingPathLength -= localPathLength;
672   } while (remainingPathLength > TGeoShape::Tolerance());
673   
674   meanRho /= pathLength;
675   
676   return kTRUE;
677 }
678
679 //__________________________________________________________________________
680 Double_t AliMUONTrackExtrap::GetMCSAngle2(const AliMUONTrackParam& param, Double_t dZ, Double_t x0)
681 {
682   /// Return the angular dispersion square due to multiple Coulomb scattering
683   /// through a material of thickness "dZ" and of radiation length "x0"
684   /// assuming linear propagation and using the small angle approximation.
685   
686   Double_t bendingSlope = param.GetBendingSlope();
687   Double_t nonBendingSlope = param.GetNonBendingSlope();
688   Double_t inverseTotalMomentum2 = param.GetInverseBendingMomentum() * param.GetInverseBendingMomentum() *
689                                    (1.0 + bendingSlope * bendingSlope) /
690                                    (1.0 + bendingSlope *bendingSlope + nonBendingSlope * nonBendingSlope); 
691   // Path length in the material
692   Double_t pathLength = TMath::Abs(dZ) * TMath::Sqrt(1.0 + bendingSlope*bendingSlope + nonBendingSlope*nonBendingSlope);
693   // relativistic velocity
694   Double_t velo = 1.;
695   // Angular dispersion square of the track (variance) in a plane perpendicular to the trajectory
696   Double_t theta02 = 0.0136 / velo * (1 + 0.038 * TMath::Log(pathLength/x0));
697   
698   return theta02 * theta02 * inverseTotalMomentum2 * pathLength / x0;
699 }
700
701 //__________________________________________________________________________
702 void AliMUONTrackExtrap::AddMCSEffect(AliMUONTrackParam *param, Double_t dZ, Double_t x0)
703 {
704   /// Add to the track parameter covariances the effects of multiple Coulomb scattering
705   /// through a material of thickness "Abs(dZ)" and of radiation length "x0"
706   /// assuming linear propagation and using the small angle approximation.
707   /// dZ = zOut - zIn (sign is important) and "param" is assumed to be given zOut.
708   /// If x0 <= 0., assume dZ = pathLength/x0 and consider the material thickness as negligible.
709   
710   Double_t bendingSlope = param->GetBendingSlope();
711   Double_t nonBendingSlope = param->GetNonBendingSlope();
712   Double_t inverseBendingMomentum = param->GetInverseBendingMomentum();
713   Double_t inverseTotalMomentum2 = inverseBendingMomentum * inverseBendingMomentum *
714                                    (1.0 + bendingSlope * bendingSlope) /
715                                    (1.0 + bendingSlope *bendingSlope + nonBendingSlope * nonBendingSlope); 
716   // Path length in the material
717   Double_t signedPathLength = dZ * TMath::Sqrt(1.0 + bendingSlope*bendingSlope + nonBendingSlope*nonBendingSlope);
718   Double_t pathLengthOverX0 = (x0 > 0.) ? TMath::Abs(signedPathLength) / x0 : TMath::Abs(signedPathLength);
719   // relativistic velocity
720   Double_t velo = 1.;
721   // Angular dispersion square of the track (variance) in a plane perpendicular to the trajectory
722   Double_t theta02 = 0.0136 / velo * (1 + 0.038 * TMath::Log(pathLengthOverX0));
723   theta02 *= theta02 * inverseTotalMomentum2 * pathLengthOverX0;
724   
725   Double_t varCoor      = (x0 > 0.) ? signedPathLength * signedPathLength * theta02 / 3. : 0.;
726   Double_t varSlop      = theta02;
727   Double_t covCorrSlope = (x0 > 0.) ? signedPathLength * theta02 / 2. : 0.;
728   
729   // Set MCS covariance matrix
730   TMatrixD newParamCov(param->GetCovariances());
731   // Non bending plane
732   newParamCov(0,0) += varCoor;       newParamCov(0,1) += covCorrSlope;
733   newParamCov(1,0) += covCorrSlope;  newParamCov(1,1) += varSlop;
734   // Bending plane
735   newParamCov(2,2) += varCoor;       newParamCov(2,3) += covCorrSlope;
736   newParamCov(3,2) += covCorrSlope;  newParamCov(3,3) += varSlop;
737   
738   // Set momentum related covariances if B!=0
739   if (fgFieldON) {
740     // compute derivative d(q/Pxy) / dSlopeX and d(q/Pxy) / dSlopeY
741     Double_t dqPxydSlopeX = inverseBendingMomentum * nonBendingSlope / (1. + nonBendingSlope*nonBendingSlope + bendingSlope*bendingSlope);
742     Double_t dqPxydSlopeY = - inverseBendingMomentum * nonBendingSlope*nonBendingSlope * bendingSlope /
743                               (1. + bendingSlope*bendingSlope) / (1. + nonBendingSlope*nonBendingSlope + bendingSlope*bendingSlope);
744     // Inverse bending momentum (due to dependences with bending and non bending slopes)
745     newParamCov(4,0) += dqPxydSlopeX * covCorrSlope; newParamCov(0,4) += dqPxydSlopeX * covCorrSlope;
746     newParamCov(4,1) += dqPxydSlopeX * varSlop;      newParamCov(1,4) += dqPxydSlopeX * varSlop;
747     newParamCov(4,2) += dqPxydSlopeY * covCorrSlope; newParamCov(2,4) += dqPxydSlopeY * covCorrSlope;
748     newParamCov(4,3) += dqPxydSlopeY * varSlop;      newParamCov(3,4) += dqPxydSlopeY * varSlop;
749     newParamCov(4,4) += (dqPxydSlopeX*dqPxydSlopeX + dqPxydSlopeY*dqPxydSlopeY) * varSlop;
750   }
751   
752   // Set new covariances
753   param->SetCovariances(newParamCov);
754 }
755
756 //__________________________________________________________________________
757 void AliMUONTrackExtrap::ExtrapToVertex(AliMUONTrackParam* trackParam,
758                                         Double_t xVtx, Double_t yVtx, Double_t zVtx,
759                                         Double_t errXVtx, Double_t errYVtx,
760                                         Bool_t correctForMCS, Bool_t correctForEnergyLoss)
761 {
762   /// Main method for extrapolation to the vertex:
763   /// Returns the track parameters and covariances resulting from the extrapolation of the current trackParam
764   /// Changes parameters and covariances according to multiple scattering and energy loss corrections:
765   /// if correctForMCS=kTRUE:  compute parameters using Branson correction and add correction resolution to covariances
766   /// if correctForMCS=kFALSE: add parameter dispersion due to MCS in parameter covariances
767   /// if correctForEnergyLoss=kTRUE:  correct parameters for energy loss and add energy loss fluctuation to covariances
768   /// if correctForEnergyLoss=kFALSE: do nothing about energy loss
769   
770   if (trackParam->GetZ() == zVtx) return; // nothing to be done if already at vertex
771   
772   if (trackParam->GetZ() > zVtx) { // spectro. (z<0)
773     cout<<"E-AliMUONTrackExtrap::ExtrapToVertex: Starting Z ("<<trackParam->GetZ()
774         <<") upstream the vertex (zVtx = "<<zVtx<<")"<<endl;
775     return;
776   }
777   
778   // Check the vertex position relatively to the absorber
779   if (zVtx < AliMUONConstants::AbsZBeg() && zVtx > AliMUONConstants::AbsZEnd()) { // spectro. (z<0)
780     cout<<"W-AliMUONTrackExtrap::ExtrapToVertex: Ending Z ("<<zVtx
781         <<") inside the front absorber ("<<AliMUONConstants::AbsZBeg()<<","<<AliMUONConstants::AbsZEnd()<<")"<<endl;
782   } else if (zVtx < AliMUONConstants::AbsZEnd() ) { // spectro. (z<0)
783     cout<<"W-AliMUONTrackExtrap::ExtrapToVertex: Ending Z ("<<zVtx
784         <<") downstream the front absorber (zAbsorberEnd = "<<AliMUONConstants::AbsZEnd()<<")"<<endl;
785     if (trackParam->CovariancesExist()) ExtrapToZCov(trackParam,zVtx);
786     else ExtrapToZ(trackParam,zVtx);
787     return;
788   }
789   
790   // Check the track position relatively to the absorber and extrapolate track parameters to the end of the absorber if needed
791   if (trackParam->GetZ() > AliMUONConstants::AbsZBeg()) { // spectro. (z<0)
792     cout<<"W-AliMUONTrackExtrap::ExtrapToVertex: Starting Z ("<<trackParam->GetZ()
793         <<") upstream the front absorber (zAbsorberBegin = "<<AliMUONConstants::AbsZBeg()<<")"<<endl;
794     if (trackParam->CovariancesExist()) ExtrapToZCov(trackParam,zVtx);
795     else ExtrapToZ(trackParam,zVtx);
796     return;
797   } else if (trackParam->GetZ() > AliMUONConstants::AbsZEnd()) { // spectro. (z<0)
798     cout<<"W-AliMUONTrackExtrap::ExtrapToVertex: Starting Z ("<<trackParam->GetZ()
799         <<") inside the front absorber ("<<AliMUONConstants::AbsZBeg()<<","<<AliMUONConstants::AbsZEnd()<<")"<<endl;
800   } else {
801     if (trackParam->CovariancesExist()) ExtrapToZCov(trackParam,AliMUONConstants::AbsZEnd());
802     else ExtrapToZ(trackParam,AliMUONConstants::AbsZEnd());
803   }
804   
805   // Get absorber correction parameters assuming linear propagation in absorber
806   Double_t trackXYZOut[3];
807   trackXYZOut[0] = trackParam->GetNonBendingCoor();
808   trackXYZOut[1] = trackParam->GetBendingCoor();
809   trackXYZOut[2] = trackParam->GetZ();
810   Double_t trackXYZIn[3];
811   if (correctForMCS) { // assume linear propagation until the vertex
812     trackXYZIn[2] = TMath::Min(zVtx, AliMUONConstants::AbsZBeg()); // spectro. (z<0)
813     trackXYZIn[0] = trackXYZOut[0] + (xVtx - trackXYZOut[0]) / (zVtx - trackXYZOut[2]) * (trackXYZIn[2] - trackXYZOut[2]);
814     trackXYZIn[1] = trackXYZOut[1] + (yVtx - trackXYZOut[1]) / (zVtx - trackXYZOut[2]) * (trackXYZIn[2] - trackXYZOut[2]);
815   } else {
816     AliMUONTrackParam trackParamIn(*trackParam);
817     ExtrapToZ(&trackParamIn, TMath::Min(zVtx, AliMUONConstants::AbsZBeg()));
818     trackXYZIn[0] = trackParamIn.GetNonBendingCoor();
819     trackXYZIn[1] = trackParamIn.GetBendingCoor();
820     trackXYZIn[2] = trackParamIn.GetZ();
821   }
822   Double_t pTot = trackParam->P();
823   Double_t pathLength, f0, f1, f2, meanRho, totalELoss, sigmaELoss2;
824   if (!GetAbsorberCorrectionParam(trackXYZIn,trackXYZOut,pTot,pathLength,f0,f1,f2,meanRho,totalELoss,sigmaELoss2)) {
825     cout<<"E-AliMUONTrackExtrap::ExtrapToVertex: Unable to take into account the absorber effects"<<endl;
826     if (trackParam->CovariancesExist()) ExtrapToZCov(trackParam,zVtx);
827     else ExtrapToZ(trackParam,zVtx);
828     return;
829   }
830   
831   // Compute track parameters and covariances at vertex according to correctForMCS and correctForEnergyLoss flags
832   if (correctForMCS) {
833     
834     if (correctForEnergyLoss) {
835       
836       // Correct for multiple scattering and energy loss
837       CorrectELossEffectInAbsorber(trackParam, 0.5*totalELoss, 0.5*sigmaELoss2);
838       CorrectMCSEffectInAbsorber(trackParam, xVtx, yVtx, zVtx, errXVtx, errYVtx,
839                                  trackXYZIn[2], pathLength, f0, f1, f2);
840       CorrectELossEffectInAbsorber(trackParam, 0.5*totalELoss, 0.5*sigmaELoss2);
841       
842     } else {
843       
844       // Correct for multiple scattering
845       CorrectMCSEffectInAbsorber(trackParam, xVtx, yVtx, zVtx, errXVtx, errYVtx,
846                                  trackXYZIn[2], pathLength, f0, f1, f2);
847     }
848     
849   } else {
850     
851     if (correctForEnergyLoss) {
852       
853       // Correct for energy loss add multiple scattering dispersion in covariance matrix
854       CorrectELossEffectInAbsorber(trackParam, 0.5*totalELoss, 0.5*sigmaELoss2);
855       AddMCSEffectInAbsorber(trackParam, -pathLength, f0, f1, f2); // (spectro. (z<0))
856       ExtrapToZCov(trackParam, trackXYZIn[2]);
857       CorrectELossEffectInAbsorber(trackParam, 0.5*totalELoss, 0.5*sigmaELoss2);
858       ExtrapToZCov(trackParam, zVtx);
859       
860     } else {
861       
862       // add multiple scattering dispersion in covariance matrix
863       AddMCSEffectInAbsorber(trackParam, -pathLength, f0, f1, f2); // (spectro. (z<0))
864       ExtrapToZCov(trackParam, zVtx);
865       
866     }
867     
868   }
869   
870 }
871
872 //__________________________________________________________________________
873 void AliMUONTrackExtrap::ExtrapToVertex(AliMUONTrackParam* trackParam,
874                                         Double_t xVtx, Double_t yVtx, Double_t zVtx,
875                                         Double_t errXVtx, Double_t errYVtx)
876 {
877   /// Extrapolate track parameters to vertex, corrected for multiple scattering and energy loss effects
878   /// Add branson correction resolution and energy loss fluctuation to parameter covariances
879   ExtrapToVertex(trackParam, xVtx, yVtx, zVtx, errXVtx, errYVtx, kTRUE, kTRUE);
880 }
881
882 //__________________________________________________________________________
883 void AliMUONTrackExtrap::ExtrapToVertexWithoutELoss(AliMUONTrackParam* trackParam,
884                                                     Double_t xVtx, Double_t yVtx, Double_t zVtx,
885                                                     Double_t errXVtx, Double_t errYVtx)
886 {
887   /// Extrapolate track parameters to vertex, corrected for multiple scattering effects only
888   /// Add branson correction resolution to parameter covariances
889   ExtrapToVertex(trackParam, xVtx, yVtx, zVtx, errXVtx, errYVtx, kTRUE, kFALSE);
890 }
891
892 //__________________________________________________________________________
893 void AliMUONTrackExtrap::ExtrapToVertexWithoutBranson(AliMUONTrackParam* trackParam, Double_t zVtx)
894 {
895   /// Extrapolate track parameters to vertex, corrected for energy loss effects only
896   /// Add dispersion due to multiple scattering and energy loss fluctuation to parameter covariances
897   ExtrapToVertex(trackParam, 0., 0., zVtx, 0., 0., kFALSE, kTRUE);
898 }
899
900 //__________________________________________________________________________
901 void AliMUONTrackExtrap::ExtrapToVertexUncorrected(AliMUONTrackParam* trackParam, Double_t zVtx)
902 {
903   /// Extrapolate track parameters to vertex without multiple scattering and energy loss corrections
904   /// Add dispersion due to multiple scattering to parameter covariances
905   ExtrapToVertex(trackParam, 0., 0., zVtx, 0., 0., kFALSE, kFALSE);
906 }
907
908 //__________________________________________________________________________
909 Double_t AliMUONTrackExtrap::TotalMomentumEnergyLoss(AliMUONTrackParam* trackParam, Double_t xVtx, Double_t yVtx, Double_t zVtx)
910 {
911   /// Calculate the total momentum energy loss in-between the track position and the vertex assuming a linear propagation
912   
913   if (trackParam->GetZ() == zVtx) return 0.; // nothing to be done if already at vertex
914   
915   // Check whether the geometry is available
916   if (!gGeoManager) {
917     cout<<"E-AliMUONTrackExtrap::TotalMomentumEnergyLoss: no TGeo"<<endl;
918     return 0.;
919   }
920   
921   // Get encountered material correction parameters assuming linear propagation from vertex to the track position
922   Double_t trackXYZOut[3];
923   trackXYZOut[0] = trackParam->GetNonBendingCoor();
924   trackXYZOut[1] = trackParam->GetBendingCoor();
925   trackXYZOut[2] = trackParam->GetZ();
926   Double_t trackXYZIn[3];
927   trackXYZIn[0] = xVtx;
928   trackXYZIn[1] = yVtx;
929   trackXYZIn[2] = zVtx;
930   Double_t pTot = trackParam->P();
931   Double_t pathLength, f0, f1, f2, meanRho, totalELoss, sigmaELoss2;
932   GetAbsorberCorrectionParam(trackXYZIn,trackXYZOut,pTot,pathLength,f0,f1,f2,meanRho,totalELoss,sigmaELoss2);
933   
934   // total momentum corrected for energy loss
935   Double_t muMass = TDatabasePDG::Instance()->GetParticle("mu-")->Mass(); // GeV
936   Double_t e = TMath::Sqrt(pTot*pTot + muMass*muMass);
937   Double_t eCorr = e + totalELoss;
938   Double_t pTotCorr = TMath::Sqrt(eCorr*eCorr - muMass*muMass);
939   
940   return pTotCorr - pTot;
941 }
942
943 //__________________________________________________________________________
944 Double_t AliMUONTrackExtrap::BetheBloch(Double_t pTotal, Double_t pathLength, Double_t rho, Double_t atomicZ, Double_t atomicZoverA)
945 {
946   /// Returns the mean total momentum energy loss of muon with total momentum='pTotal'
947   /// in the absorber layer of lenght='pathLength', density='rho', A='atomicA' and Z='atomicZ'
948   Double_t muMass = TDatabasePDG::Instance()->GetParticle("mu-")->Mass(); // GeV
949   
950   // mean exitation energy (GeV)
951   Double_t i;
952   if (atomicZ < 13) i = (12. * atomicZ + 7.) * 1.e-9;
953   else i = (9.76 * atomicZ + 58.8 * TMath::Power(atomicZ,-0.19)) * 1.e-9;
954   
955   return pathLength * rho * AliExternalTrackParam::BetheBlochGeant(pTotal/muMass, rho, 0.20, 3.00, i, atomicZoverA);
956 }
957
958 //__________________________________________________________________________
959 Double_t AliMUONTrackExtrap::EnergyLossFluctuation2(Double_t pTotal, Double_t pathLength, Double_t rho, Double_t atomicZoverA)
960 {
961   /// Returns the total momentum energy loss fluctuation of muon with total momentum='pTotal'
962   /// in the absorber layer of lenght='pathLength', density='rho', A='atomicA' and Z='atomicZ'
963   Double_t muMass = TDatabasePDG::Instance()->GetParticle("mu-")->Mass(); // GeV
964   //Double_t eMass = 0.510998918e-3; // GeV
965   Double_t k = 0.307075e-3; // GeV.g^-1.cm^2
966   Double_t p2=pTotal*pTotal;
967   Double_t beta2=p2/(p2 + muMass*muMass);
968   
969   Double_t fwhm = 2. * k * rho * pathLength * atomicZoverA / beta2; // FWHM of the energy loss Landau distribution
970   Double_t sigma2 = fwhm * fwhm / (8.*log(2.)); // gaussian: fwmh = 2 * srqt(2*ln(2)) * sigma (i.e. fwmh = 2.35 * sigma)
971   
972   //sigma2 = k * rho * pathLength * atomicZ / atomicA * eMass; // sigma2 of the energy loss gaussian distribution
973   
974   return sigma2;
975 }
976
977 //__________________________________________________________________________
978 void AliMUONTrackExtrap::Cov2CovP(const TMatrixD &param, TMatrixD &cov)
979 {
980   /// change coordinate system: (X, SlopeX, Y, SlopeY, q/Pyz) -> (X, SlopeX, Y, SlopeY, q*PTot)
981   /// parameters (param) are given in the (X, SlopeX, Y, SlopeY, q/Pyz) coordinate system
982   
983   // charge * total momentum
984   Double_t qPTot = TMath::Sqrt(1. + param(1,0)*param(1,0) + param(3,0)*param(3,0)) /
985                    TMath::Sqrt(1. + param(3,0)*param(3,0)) / param(4,0);
986   
987   // Jacobian of the opposite transformation
988   TMatrixD jacob(5,5);
989   jacob.UnitMatrix();
990   jacob(4,1) = qPTot * param(1,0) / (1. + param(1,0)*param(1,0) + param(3,0)*param(3,0));
991   jacob(4,3) = - qPTot * param(1,0) * param(1,0) * param(3,0) /
992                  (1. + param(3,0)*param(3,0)) / (1. + param(1,0)*param(1,0) + param(3,0)*param(3,0));
993   jacob(4,4) = - qPTot / param(4,0);
994   
995   // compute covariances in new coordinate system
996   TMatrixD tmp(cov,TMatrixD::kMultTranspose,jacob);
997   cov.Mult(jacob,tmp);
998 }
999
1000 //__________________________________________________________________________
1001 void AliMUONTrackExtrap::CovP2Cov(const TMatrixD &param, TMatrixD &covP)
1002 {
1003   /// change coordinate system: (X, SlopeX, Y, SlopeY, q*PTot) -> (X, SlopeX, Y, SlopeY, q/Pyz)
1004   /// parameters (param) are given in the (X, SlopeX, Y, SlopeY, q/Pyz) coordinate system
1005   
1006   // charge * total momentum
1007   Double_t qPTot = TMath::Sqrt(1. + param(1,0)*param(1,0) + param(3,0)*param(3,0)) /
1008                    TMath::Sqrt(1. + param(3,0)*param(3,0)) / param(4,0);
1009   
1010   // Jacobian of the transformation
1011   TMatrixD jacob(5,5);
1012   jacob.UnitMatrix();
1013   jacob(4,1) = param(4,0) * param(1,0) / (1. + param(1,0)*param(1,0) + param(3,0)*param(3,0));
1014   jacob(4,3) = - param(4,0) * param(1,0) * param(1,0) * param(3,0) /
1015                  (1. + param(3,0)*param(3,0)) / (1. + param(1,0)*param(1,0) + param(3,0)*param(3,0));
1016   jacob(4,4) = - param(4,0) / qPTot;
1017   
1018   // compute covariances in new coordinate system
1019   TMatrixD tmp(covP,TMatrixD::kMultTranspose,jacob);
1020   covP.Mult(jacob,tmp);
1021 }
1022
1023  //__________________________________________________________________________
1024 void AliMUONTrackExtrap::ExtrapOneStepHelix(Double_t charge, Double_t step, Double_t *vect, Double_t *vout)
1025 {
1026 /// <pre>
1027 ///    ******************************************************************
1028 ///    *                                                                *
1029 ///    *  Performs the tracking of one step in a magnetic field         *
1030 ///    *  The trajectory is assumed to be a helix in a constant field   *
1031 ///    *  taken at the mid point of the step.                           *
1032 ///    *  Parameters:                                                   *
1033 ///    *   input                                                        *
1034 ///    *     STEP =arc length of the step asked                         *
1035 ///    *     VECT =input vector (position,direction cos and momentum)   *
1036 ///    *     CHARGE=  electric charge of the particle                   *
1037 ///    *   output                                                       *
1038 ///    *     VOUT = same as VECT after completion of the step           *
1039 ///    *                                                                *
1040 ///    *    ==>Called by : USER, GUSWIM                               *
1041 ///    *       Author    m.hansroul  *********                          *
1042 ///    *       modified  s.egli, s.v.levonian                           *
1043 ///    *       modified  v.perevoztchikov
1044 ///    *                                                                *
1045 ///    ******************************************************************
1046 /// </pre>
1047
1048 // modif: everything in double precision
1049
1050     Double_t xyz[3], h[4], hxp[3];
1051     Double_t h2xy, hp, rho, tet;
1052     Double_t sint, sintt, tsint, cos1t;
1053     Double_t f1, f2, f3, f4, f5, f6;
1054
1055     const Int_t kix  = 0;
1056     const Int_t kiy  = 1;
1057     const Int_t kiz  = 2;
1058     const Int_t kipx = 3;
1059     const Int_t kipy = 4;
1060     const Int_t kipz = 5;
1061     const Int_t kipp = 6;
1062
1063     const Double_t kec = 2.9979251e-4;
1064     //
1065     //    ------------------------------------------------------------------
1066     //
1067     //       units are kgauss,centimeters,gev/c
1068     //
1069     vout[kipp] = vect[kipp];
1070     if (TMath::Abs(charge) < 0.00001) {
1071       for (Int_t i = 0; i < 3; i++) {
1072         vout[i] = vect[i] + step * vect[i+3];
1073         vout[i+3] = vect[i+3];
1074       }
1075       return;
1076     }
1077     xyz[0]    = vect[kix] + 0.5 * step * vect[kipx];
1078     xyz[1]    = vect[kiy] + 0.5 * step * vect[kipy];
1079     xyz[2]    = vect[kiz] + 0.5 * step * vect[kipz];
1080
1081     //cmodif: call gufld (xyz, h) changed into:
1082     TGeoGlobalMagField::Instance()->Field(xyz,h);
1083  
1084     h2xy = h[0]*h[0] + h[1]*h[1];
1085     h[3] = h[2]*h[2]+ h2xy;
1086     if (h[3] < 1.e-12) {
1087       for (Int_t i = 0; i < 3; i++) {
1088         vout[i] = vect[i] + step * vect[i+3];
1089         vout[i+3] = vect[i+3];
1090       }
1091       return;
1092     }
1093     if (h2xy < 1.e-12*h[3]) {
1094       ExtrapOneStepHelix3(charge*h[2], step, vect, vout);
1095       return;
1096     }
1097     h[3] = TMath::Sqrt(h[3]);
1098     h[0] /= h[3];
1099     h[1] /= h[3];
1100     h[2] /= h[3];
1101     h[3] *= kec;
1102
1103     hxp[0] = h[1]*vect[kipz] - h[2]*vect[kipy];
1104     hxp[1] = h[2]*vect[kipx] - h[0]*vect[kipz];
1105     hxp[2] = h[0]*vect[kipy] - h[1]*vect[kipx];
1106  
1107     hp = h[0]*vect[kipx] + h[1]*vect[kipy] + h[2]*vect[kipz];
1108
1109     rho = -charge*h[3]/vect[kipp];
1110     tet = rho * step;
1111
1112     if (TMath::Abs(tet) > 0.15) {
1113       sint = TMath::Sin(tet);
1114       sintt = (sint/tet);
1115       tsint = (tet-sint)/tet;
1116       cos1t = 2.*(TMath::Sin(0.5*tet))*(TMath::Sin(0.5*tet))/tet;
1117     } else {
1118       tsint = tet*tet/36.;
1119       sintt = (1. - tsint);
1120       sint = tet*sintt;
1121       cos1t = 0.5*tet;
1122     }
1123
1124     f1 = step * sintt;
1125     f2 = step * cos1t;
1126     f3 = step * tsint * hp;
1127     f4 = -tet*cos1t;
1128     f5 = sint;
1129     f6 = tet * cos1t * hp;
1130  
1131     vout[kix] = vect[kix] + f1*vect[kipx] + f2*hxp[0] + f3*h[0];
1132     vout[kiy] = vect[kiy] + f1*vect[kipy] + f2*hxp[1] + f3*h[1];
1133     vout[kiz] = vect[kiz] + f1*vect[kipz] + f2*hxp[2] + f3*h[2];
1134  
1135     vout[kipx] = vect[kipx] + f4*vect[kipx] + f5*hxp[0] + f6*h[0];
1136     vout[kipy] = vect[kipy] + f4*vect[kipy] + f5*hxp[1] + f6*h[1];
1137     vout[kipz] = vect[kipz] + f4*vect[kipz] + f5*hxp[2] + f6*h[2];
1138  
1139     return;
1140 }
1141
1142  //__________________________________________________________________________
1143 void AliMUONTrackExtrap::ExtrapOneStepHelix3(Double_t field, Double_t step, Double_t *vect, Double_t *vout)
1144 {
1145 /// <pre>
1146 ///     ******************************************************************
1147 ///     *                                                                *
1148 ///     *       Tracking routine in a constant field oriented            *
1149 ///     *       along axis 3                                             *
1150 ///     *       Tracking is performed with a conventional                *
1151 ///     *       helix step method                                        *
1152 ///     *                                                                *
1153 ///     *    ==>Called by : USER, GUSWIM                                 *
1154 ///     *       Authors    R.Brun, M.Hansroul  *********                 *
1155 ///     *       Rewritten  V.Perevoztchikov
1156 ///     *                                                                *
1157 ///     ******************************************************************
1158 /// </pre>
1159
1160     Double_t hxp[3];
1161     Double_t h4, hp, rho, tet;
1162     Double_t sint, sintt, tsint, cos1t;
1163     Double_t f1, f2, f3, f4, f5, f6;
1164
1165     const Int_t kix  = 0;
1166     const Int_t kiy  = 1;
1167     const Int_t kiz  = 2;
1168     const Int_t kipx = 3;
1169     const Int_t kipy = 4;
1170     const Int_t kipz = 5;
1171     const Int_t kipp = 6;
1172
1173     const Double_t kec = 2.9979251e-4;
1174
1175 // 
1176 //     ------------------------------------------------------------------
1177 // 
1178 //       units are kgauss,centimeters,gev/c
1179 // 
1180     vout[kipp] = vect[kipp];
1181     h4 = field * kec;
1182
1183     hxp[0] = - vect[kipy];
1184     hxp[1] = + vect[kipx];
1185  
1186     hp = vect[kipz];
1187
1188     rho = -h4/vect[kipp];
1189     tet = rho * step;
1190     if (TMath::Abs(tet) > 0.15) {
1191       sint = TMath::Sin(tet);
1192       sintt = (sint/tet);
1193       tsint = (tet-sint)/tet;
1194       cos1t = 2.* TMath::Sin(0.5*tet) * TMath::Sin(0.5*tet)/tet;
1195     } else {
1196       tsint = tet*tet/36.;
1197       sintt = (1. - tsint);
1198       sint = tet*sintt;
1199       cos1t = 0.5*tet;
1200     }
1201
1202     f1 = step * sintt;
1203     f2 = step * cos1t;
1204     f3 = step * tsint * hp;
1205     f4 = -tet*cos1t;
1206     f5 = sint;
1207     f6 = tet * cos1t * hp;
1208  
1209     vout[kix] = vect[kix] + f1*vect[kipx] + f2*hxp[0];
1210     vout[kiy] = vect[kiy] + f1*vect[kipy] + f2*hxp[1];
1211     vout[kiz] = vect[kiz] + f1*vect[kipz] + f3;
1212  
1213     vout[kipx] = vect[kipx] + f4*vect[kipx] + f5*hxp[0];
1214     vout[kipy] = vect[kipy] + f4*vect[kipy] + f5*hxp[1];
1215     vout[kipz] = vect[kipz] + f4*vect[kipz] + f6;
1216
1217     return;
1218 }
1219
1220  //__________________________________________________________________________
1221 void AliMUONTrackExtrap::ExtrapOneStepRungekutta(Double_t charge, Double_t step, Double_t* vect, Double_t* vout)
1222 {
1223 /// <pre>
1224 ///     ******************************************************************
1225 ///     *                                                                *
1226 ///     *  Runge-Kutta method for tracking a particle through a magnetic *
1227 ///     *  field. Uses Nystroem algorithm (See Handbook Nat. Bur. of     *
1228 ///     *  Standards, procedure 25.5.20)                                 *
1229 ///     *                                                                *
1230 ///     *  Input parameters                                              *
1231 ///     *       CHARGE    Particle charge                                *
1232 ///     *       STEP      Step size                                      *
1233 ///     *       VECT      Initial co-ords,direction cosines,momentum     *
1234 ///     *  Output parameters                                             *
1235 ///     *       VOUT      Output co-ords,direction cosines,momentum      *
1236 ///     *  User routine called                                           *
1237 ///     *       CALL GUFLD(X,F)                                          *
1238 ///     *                                                                *
1239 ///     *    ==>Called by : USER, GUSWIM                                 *
1240 ///     *       Authors    R.Brun, M.Hansroul  *********                 *
1241 ///     *                  V.Perevoztchikov (CUT STEP implementation)    *
1242 ///     *                                                                *
1243 ///     *                                                                *
1244 ///     ******************************************************************
1245 /// </pre>
1246
1247     Double_t h2, h4, f[4];
1248     Double_t xyzt[3], a, b, c, ph,ph2;
1249     Double_t secxs[4],secys[4],seczs[4],hxp[3];
1250     Double_t g1, g2, g3, g4, g5, g6, ang2, dxt, dyt, dzt;
1251     Double_t est, at, bt, ct, cba;
1252     Double_t f1, f2, f3, f4, rho, tet, hnorm, hp, rho1, sint, cost;
1253     
1254     Double_t x;
1255     Double_t y;
1256     Double_t z;
1257     
1258     Double_t xt;
1259     Double_t yt;
1260     Double_t zt;
1261
1262     Double_t maxit = 1992;
1263     Double_t maxcut = 11;
1264
1265     const Double_t kdlt   = 1e-4;
1266     const Double_t kdlt32 = kdlt/32.;
1267     const Double_t kthird = 1./3.;
1268     const Double_t khalf  = 0.5;
1269     const Double_t kec = 2.9979251e-4;
1270
1271     const Double_t kpisqua = 9.86960440109;
1272     const Int_t kix  = 0;
1273     const Int_t kiy  = 1;
1274     const Int_t kiz  = 2;
1275     const Int_t kipx = 3;
1276     const Int_t kipy = 4;
1277     const Int_t kipz = 5;
1278   
1279     // *.
1280     // *.    ------------------------------------------------------------------
1281     // *.
1282     // *             this constant is for units cm,gev/c and kgauss
1283     // *
1284     Int_t iter = 0;
1285     Int_t ncut = 0;
1286     for(Int_t j = 0; j < 7; j++)
1287       vout[j] = vect[j];
1288
1289     Double_t  pinv   = kec * charge / vect[6];
1290     Double_t tl = 0.;
1291     Double_t h = step;
1292     Double_t rest;
1293
1294  
1295     do {
1296       rest  = step - tl;
1297       if (TMath::Abs(h) > TMath::Abs(rest)) h = rest;
1298       //cmodif: call gufld(vout,f) changed into:
1299       TGeoGlobalMagField::Instance()->Field(vout,f);
1300
1301       // *
1302       // *             start of integration
1303       // *
1304       x      = vout[0];
1305       y      = vout[1];
1306       z      = vout[2];
1307       a      = vout[3];
1308       b      = vout[4];
1309       c      = vout[5];
1310
1311       h2     = khalf * h;
1312       h4     = khalf * h2;
1313       ph     = pinv * h;
1314       ph2    = khalf * ph;
1315       secxs[0] = (b * f[2] - c * f[1]) * ph2;
1316       secys[0] = (c * f[0] - a * f[2]) * ph2;
1317       seczs[0] = (a * f[1] - b * f[0]) * ph2;
1318       ang2 = (secxs[0]*secxs[0] + secys[0]*secys[0] + seczs[0]*seczs[0]);
1319       if (ang2 > kpisqua) break;
1320
1321       dxt    = h2 * a + h4 * secxs[0];
1322       dyt    = h2 * b + h4 * secys[0];
1323       dzt    = h2 * c + h4 * seczs[0];
1324       xt     = x + dxt;
1325       yt     = y + dyt;
1326       zt     = z + dzt;
1327       // *
1328       // *              second intermediate point
1329       // *
1330
1331       est = TMath::Abs(dxt) + TMath::Abs(dyt) + TMath::Abs(dzt);
1332       if (est > h) {
1333         if (ncut++ > maxcut) break;
1334         h *= khalf;
1335         continue;
1336       }
1337  
1338       xyzt[0] = xt;
1339       xyzt[1] = yt;
1340       xyzt[2] = zt;
1341
1342       //cmodif: call gufld(xyzt,f) changed into:
1343       TGeoGlobalMagField::Instance()->Field(xyzt,f);
1344
1345       at     = a + secxs[0];
1346       bt     = b + secys[0];
1347       ct     = c + seczs[0];
1348
1349       secxs[1] = (bt * f[2] - ct * f[1]) * ph2;
1350       secys[1] = (ct * f[0] - at * f[2]) * ph2;
1351       seczs[1] = (at * f[1] - bt * f[0]) * ph2;
1352       at     = a + secxs[1];
1353       bt     = b + secys[1];
1354       ct     = c + seczs[1];
1355       secxs[2] = (bt * f[2] - ct * f[1]) * ph2;
1356       secys[2] = (ct * f[0] - at * f[2]) * ph2;
1357       seczs[2] = (at * f[1] - bt * f[0]) * ph2;
1358       dxt    = h * (a + secxs[2]);
1359       dyt    = h * (b + secys[2]);
1360       dzt    = h * (c + seczs[2]);
1361       xt     = x + dxt;
1362       yt     = y + dyt;
1363       zt     = z + dzt;
1364       at     = a + 2.*secxs[2];
1365       bt     = b + 2.*secys[2];
1366       ct     = c + 2.*seczs[2];
1367
1368       est = TMath::Abs(dxt)+TMath::Abs(dyt)+TMath::Abs(dzt);
1369       if (est > 2.*TMath::Abs(h)) {
1370         if (ncut++ > maxcut) break;
1371         h *= khalf;
1372         continue;
1373       }
1374  
1375       xyzt[0] = xt;
1376       xyzt[1] = yt;
1377       xyzt[2] = zt;
1378
1379       //cmodif: call gufld(xyzt,f) changed into:
1380       TGeoGlobalMagField::Instance()->Field(xyzt,f);
1381
1382       z      = z + (c + (seczs[0] + seczs[1] + seczs[2]) * kthird) * h;
1383       y      = y + (b + (secys[0] + secys[1] + secys[2]) * kthird) * h;
1384       x      = x + (a + (secxs[0] + secxs[1] + secxs[2]) * kthird) * h;
1385
1386       secxs[3] = (bt*f[2] - ct*f[1])* ph2;
1387       secys[3] = (ct*f[0] - at*f[2])* ph2;
1388       seczs[3] = (at*f[1] - bt*f[0])* ph2;
1389       a      = a+(secxs[0]+secxs[3]+2. * (secxs[1]+secxs[2])) * kthird;
1390       b      = b+(secys[0]+secys[3]+2. * (secys[1]+secys[2])) * kthird;
1391       c      = c+(seczs[0]+seczs[3]+2. * (seczs[1]+seczs[2])) * kthird;
1392
1393       est    = TMath::Abs(secxs[0]+secxs[3] - (secxs[1]+secxs[2]))
1394         + TMath::Abs(secys[0]+secys[3] - (secys[1]+secys[2]))
1395         + TMath::Abs(seczs[0]+seczs[3] - (seczs[1]+seczs[2]));
1396
1397       if (est > kdlt && TMath::Abs(h) > 1.e-4) {
1398         if (ncut++ > maxcut) break;
1399         h *= khalf;
1400         continue;
1401       }
1402
1403       ncut = 0;
1404       // *               if too many iterations, go to helix
1405       if (iter++ > maxit) break;
1406
1407       tl += h;
1408       if (est < kdlt32) 
1409         h *= 2.;
1410       cba    = 1./ TMath::Sqrt(a*a + b*b + c*c);
1411       vout[0] = x;
1412       vout[1] = y;
1413       vout[2] = z;
1414       vout[3] = cba*a;
1415       vout[4] = cba*b;
1416       vout[5] = cba*c;
1417       rest = step - tl;
1418       if (step < 0.) rest = -rest;
1419       if (rest < 1.e-5*TMath::Abs(step)) return;
1420
1421     } while(1);
1422
1423     // angle too big, use helix
1424
1425     f1  = f[0];
1426     f2  = f[1];
1427     f3  = f[2];
1428     f4  = TMath::Sqrt(f1*f1+f2*f2+f3*f3);
1429     rho = -f4*pinv;
1430     tet = rho * step;
1431  
1432     hnorm = 1./f4;
1433     f1 = f1*hnorm;
1434     f2 = f2*hnorm;
1435     f3 = f3*hnorm;
1436
1437     hxp[0] = f2*vect[kipz] - f3*vect[kipy];
1438     hxp[1] = f3*vect[kipx] - f1*vect[kipz];
1439     hxp[2] = f1*vect[kipy] - f2*vect[kipx];
1440  
1441     hp = f1*vect[kipx] + f2*vect[kipy] + f3*vect[kipz];
1442
1443     rho1 = 1./rho;
1444     sint = TMath::Sin(tet);
1445     cost = 2.*TMath::Sin(khalf*tet)*TMath::Sin(khalf*tet);
1446
1447     g1 = sint*rho1;
1448     g2 = cost*rho1;
1449     g3 = (tet-sint) * hp*rho1;
1450     g4 = -cost;
1451     g5 = sint;
1452     g6 = cost * hp;
1453  
1454     vout[kix] = vect[kix] + g1*vect[kipx] + g2*hxp[0] + g3*f1;
1455     vout[kiy] = vect[kiy] + g1*vect[kipy] + g2*hxp[1] + g3*f2;
1456     vout[kiz] = vect[kiz] + g1*vect[kipz] + g2*hxp[2] + g3*f3;
1457  
1458     vout[kipx] = vect[kipx] + g4*vect[kipx] + g5*hxp[0] + g6*f1;
1459     vout[kipy] = vect[kipy] + g4*vect[kipy] + g5*hxp[1] + g6*f2;
1460     vout[kipz] = vect[kipz] + g4*vect[kipz] + g5*hxp[2] + g6*f3;
1461
1462     return;
1463 }
1464