]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONTrackExtrap.cxx
c0aae90b7862e9ba38bb26cd264616b8aa8fed51
[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 //
20 // Tools
21 // for
22 // track
23 // extrapolation
24 // in
25 // ALICE
26 // dimuon
27 // spectrometer
28 //
29 ///////////////////////////////////////////////////
30
31 #include "AliMUONTrackExtrap.h" 
32 #include "AliMUONTrackParam.h"
33 #include "AliMUONConstants.h"
34
35 #include "AliMagF.h" 
36
37 #include <TMath.h>
38 #include <TMatrixD.h>
39 #include <TGeoManager.h>
40
41 #include <Riostream.h>
42
43 /// \cond CLASSIMP
44 ClassImp(AliMUONTrackExtrap) // Class implementation in ROOT context
45 /// \endcond
46
47 const AliMagF* AliMUONTrackExtrap::fgkField = 0x0;
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 Double_t AliMUONTrackExtrap::GetImpactParamFromBendingMomentum(Double_t bendingMomentum)
55 {
56   /// Returns impact parameter at vertex in bending plane (cm),
57   /// from the signed bending momentum "BendingMomentum" in bending plane (GeV/c),
58   /// using simple values for dipole magnetic field.
59   /// The sign of "BendingMomentum" is the sign of the charge.
60   
61   if (bendingMomentum == 0.) return 1.e10;
62   
63   Double_t simpleBPosition = 0.5 * (AliMUONConstants::CoilZ() + AliMUONConstants::YokeZ());
64   Double_t simpleBLength = 0.5 * (AliMUONConstants::CoilL() + AliMUONConstants::YokeL());
65   Float_t b[3], x[3] = {0.,0.,(Float_t) simpleBPosition};
66   if (fgkField) fgkField->Field(x,b);
67   else {
68     cout<<"F-AliMUONTrackExtrap::GetField: fgkField = 0x0"<<endl;
69     exit(-1);
70   }
71   Double_t simpleBValue = (Double_t) b[0];
72   
73   return (-0.0003 * simpleBValue * simpleBLength * simpleBPosition / bendingMomentum);
74 }
75
76   //__________________________________________________________________________
77 Double_t AliMUONTrackExtrap::GetBendingMomentumFromImpactParam(Double_t impactParam)
78 {
79   /// Returns signed bending momentum in bending plane (GeV/c),
80   /// the sign being the sign of the charge for particles moving forward in Z,
81   /// from the impact parameter "ImpactParam" at vertex in bending plane (cm),
82   /// using simple values for dipole magnetic field.
83   
84   if (impactParam == 0.) return 1.e10;
85   
86   Double_t simpleBPosition = 0.5 * (AliMUONConstants::CoilZ() + AliMUONConstants::YokeZ());
87   Double_t simpleBLength = 0.5 * (AliMUONConstants::CoilL() + AliMUONConstants::YokeL());
88   Float_t b[3], x[3] = {0.,0.,(Float_t) simpleBPosition};
89   if (fgkField) fgkField->Field(x,b);
90   else {
91     cout<<"F-AliMUONTrackExtrap::GetField: fgkField = 0x0"<<endl;
92     exit(-1);
93   }
94   Double_t simpleBValue = (Double_t) b[0];
95   
96   return (-0.0003 * simpleBValue * simpleBLength * simpleBPosition / impactParam);
97 }
98
99   //__________________________________________________________________________
100 void AliMUONTrackExtrap::ExtrapToZ(AliMUONTrackParam* trackParam, Double_t zEnd)
101 {
102   /// Interface to track parameter extrapolation to the plane at "Z" using Helix or Rungekutta algorithm.
103   /// On return, the track parameters resulting from the extrapolation are updated in trackParam.
104   if (fgkUseHelix) AliMUONTrackExtrap::ExtrapToZHelix(trackParam,zEnd);
105   else AliMUONTrackExtrap::ExtrapToZRungekutta(trackParam,zEnd);
106 }
107
108   //__________________________________________________________________________
109 void AliMUONTrackExtrap::ExtrapToZHelix(AliMUONTrackParam* trackParam, Double_t zEnd)
110 {
111   /// Track parameter extrapolation to the plane at "Z" using Helix algorithm.
112   /// On return, the track parameters resulting from the extrapolation are updated in trackParam.
113   if (trackParam->GetZ() == zEnd) return; // nothing to be done if same Z
114   Double_t forwardBackward; // +1 if forward, -1 if backward
115   if (zEnd < trackParam->GetZ()) forwardBackward = 1.0; // spectro. z<0 
116   else forwardBackward = -1.0;
117   Double_t v3[7], v3New[7]; // 7 in parameter ????
118   Int_t i3, stepNumber;
119   // For safety: return kTRUE or kFALSE ????
120   // Parameter vector for calling EXTRAP_ONESTEP
121   ConvertTrackParamForExtrap(trackParam, forwardBackward, v3);
122   // sign of charge (sign of fInverseBendingMomentum if forward motion)
123   // must be changed if backward extrapolation
124   Double_t chargeExtrap = forwardBackward * TMath::Sign(Double_t(1.0), trackParam->GetInverseBendingMomentum());
125   // Extrapolation loop
126   stepNumber = 0;
127   while (((-forwardBackward * (v3[2] - zEnd)) <= 0.0) && (stepNumber < fgkMaxStepNumber)) { // spectro. z<0
128     stepNumber++;
129     ExtrapOneStepHelix(chargeExtrap, fgkHelixStepLength, v3, v3New);
130     if ((-forwardBackward * (v3New[2] - zEnd)) > 0.0) break; // one is beyond Z spectro. z<0
131     // better use TArray ????
132     for (i3 = 0; i3 < 7; i3++) {v3[i3] = v3New[i3];}
133   }
134   // check fgkMaxStepNumber ????
135   // Interpolation back to exact Z (2nd order)
136   // should be in function ???? using TArray ????
137   Double_t dZ12 = v3New[2] - v3[2]; // 1->2
138   if (TMath::Abs(dZ12) > 0) {
139     Double_t dZ1i = zEnd - v3[2]; // 1-i
140     Double_t dZi2 = v3New[2] - zEnd; // i->2
141     Double_t xPrime = (v3New[0] - v3[0]) / dZ12;
142     Double_t xSecond = ((v3New[3] / v3New[5]) - (v3[3] / v3[5])) / dZ12;
143     Double_t yPrime = (v3New[1] - v3[1]) / dZ12;
144     Double_t ySecond = ((v3New[4] / v3New[5]) - (v3[4] / v3[5])) / dZ12;
145     v3[0] = v3[0] + xPrime * dZ1i - 0.5 * xSecond * dZ1i * dZi2; // X
146     v3[1] = v3[1] + yPrime * dZ1i - 0.5 * ySecond * dZ1i * dZi2; // Y
147     v3[2] = zEnd; // Z
148     Double_t xPrimeI = xPrime - 0.5 * xSecond * (dZi2 - dZ1i);
149     Double_t yPrimeI = yPrime - 0.5 * ySecond * (dZi2 - dZ1i);
150     // (PX, PY, PZ)/PTOT assuming forward motion
151     v3[5] = 1.0 / TMath::Sqrt(1.0 + xPrimeI * xPrimeI + yPrimeI * yPrimeI); // PZ/PTOT
152     v3[3] = xPrimeI * v3[5]; // PX/PTOT
153     v3[4] = yPrimeI * v3[5]; // PY/PTOT
154   } else {
155     cout<<"W-AliMUONTrackExtrap::ExtrapToZHelix: Extrap. to Z not reached, Z = "<<zEnd<<endl;
156   }
157   // Recover track parameters (charge back for forward motion)
158   RecoverTrackParam(v3, chargeExtrap * forwardBackward, trackParam);
159 }
160
161   //__________________________________________________________________________
162 void AliMUONTrackExtrap::ExtrapToZRungekutta(AliMUONTrackParam* trackParam, Double_t zEnd)
163 {
164   /// Track parameter extrapolation to the plane at "Z" using Rungekutta algorithm.
165   /// On return, the track parameters resulting from the extrapolation are updated in trackParam.
166   if (trackParam->GetZ() == zEnd) return; // nothing to be done if same Z
167   Double_t forwardBackward; // +1 if forward, -1 if backward
168   if (zEnd < trackParam->GetZ()) forwardBackward = 1.0; // spectro. z<0 
169   else forwardBackward = -1.0;
170   // sign of charge (sign of fInverseBendingMomentum if forward motion)
171   // must be changed if backward extrapolation
172   Double_t chargeExtrap = forwardBackward * TMath::Sign(Double_t(1.0), trackParam->GetInverseBendingMomentum());
173   Double_t v3[7], v3New[7];
174   Double_t dZ, step;
175   Int_t stepNumber = 0;
176   
177   // Extrapolation loop (until within tolerance)
178   Double_t residue = zEnd - trackParam->GetZ();
179   while (TMath::Abs(residue) > fgkRungeKuttaMaxResidue && stepNumber <= fgkMaxStepNumber) {
180     dZ = zEnd - trackParam->GetZ();
181     // step lenght assuming linear trajectory
182     step = dZ * TMath::Sqrt(1.0 + trackParam->GetBendingSlope()*trackParam->GetBendingSlope() +
183                             trackParam->GetNonBendingSlope()*trackParam->GetNonBendingSlope());
184     ConvertTrackParamForExtrap(trackParam, forwardBackward, v3);
185     do { // reduce step lenght while zEnd oversteped
186       if (stepNumber > fgkMaxStepNumber) {
187         cout<<"W-AliMUONTrackExtrap::ExtrapToZRungekutta: Too many trials: "<<stepNumber<<endl;
188         break;
189       }
190       stepNumber ++;
191       step = TMath::Abs(step);
192       AliMUONTrackExtrap::ExtrapOneStepRungekutta(chargeExtrap,step,v3,v3New);
193       residue = zEnd - v3New[2];
194       step *= dZ/(v3New[2]-trackParam->GetZ());
195     } while (residue*dZ < 0 && TMath::Abs(residue) > fgkRungeKuttaMaxResidue);
196     RecoverTrackParam(v3New, chargeExtrap * forwardBackward, trackParam);
197   }
198   
199   // terminate the extropolation with a straight line up to the exact "zEnd" value
200   trackParam->SetNonBendingCoor(trackParam->GetNonBendingCoor() + residue * trackParam->GetNonBendingSlope());
201   trackParam->SetBendingCoor(trackParam->GetBendingCoor() + residue * trackParam->GetBendingSlope());
202   trackParam->SetZ(zEnd);
203 }
204
205   //__________________________________________________________________________
206 void AliMUONTrackExtrap::ConvertTrackParamForExtrap(AliMUONTrackParam* trackParam, Double_t forwardBackward, Double_t *v3)
207 {
208   /// Set vector of Geant3 parameters pointed to by "v3" from track parameters in trackParam.
209   /// Since AliMUONTrackParam is only geometry, one uses "forwardBackward"
210   /// to know whether the particle is going forward (+1) or backward (-1).
211   v3[0] = trackParam->GetNonBendingCoor(); // X
212   v3[1] = trackParam->GetBendingCoor(); // Y
213   v3[2] = trackParam->GetZ(); // Z
214   Double_t pYZ = TMath::Abs(1.0 / trackParam->GetInverseBendingMomentum());
215   Double_t pZ = pYZ / TMath::Sqrt(1.0 + trackParam->GetBendingSlope() * trackParam->GetBendingSlope());
216   v3[6] = TMath::Sqrt(pYZ * pYZ + pZ * pZ * trackParam->GetNonBendingSlope() * trackParam->GetNonBendingSlope()); // PTOT
217   v3[5] = -forwardBackward * pZ / v3[6]; // PZ/PTOT spectro. z<0
218   v3[3] = trackParam->GetNonBendingSlope() * v3[5]; // PX/PTOT
219   v3[4] = trackParam->GetBendingSlope() * v3[5]; // PY/PTOT
220 }
221
222   //__________________________________________________________________________
223 void AliMUONTrackExtrap::RecoverTrackParam(Double_t *v3, Double_t charge, AliMUONTrackParam* trackParam)
224 {
225   /// Set track parameters in trackParam from Geant3 parameters pointed to by "v3",
226   /// assumed to be calculated for forward motion in Z.
227   /// "InverseBendingMomentum" is signed with "charge".
228   trackParam->SetNonBendingCoor(v3[0]); // X
229   trackParam->SetBendingCoor(v3[1]); // Y
230   trackParam->SetZ(v3[2]); // Z
231   Double_t pYZ = v3[6] * TMath::Sqrt(1.0 - v3[3] * v3[3]);
232   trackParam->SetInverseBendingMomentum(charge/pYZ);
233   trackParam->SetBendingSlope(v3[4]/v3[5]);
234   trackParam->SetNonBendingSlope(v3[3]/v3[5]);
235 }
236
237   //__________________________________________________________________________
238 void AliMUONTrackExtrap::ExtrapToZCov(AliMUONTrackParam* trackParam, Double_t zEnd, Bool_t updatePropagator)
239 {
240   /// Track parameters and their covariances extrapolated to the plane at "zEnd".
241   /// On return, results from the extrapolation are updated in trackParam.
242   
243   if (trackParam->GetZ() == zEnd) return; // nothing to be done if same z
244   
245   // No need to propagate the covariance matrix if it does not exist
246   if (!trackParam->CovariancesExist()) {
247     cout<<"W-AliMUONTrackExtrap::ExtrapToZCov: Covariance matrix does not exist"<<endl;
248     // Extrapolate track parameters to "zEnd"
249     ExtrapToZ(trackParam,zEnd);
250     return;
251   }
252   
253   // Save the actual track parameters
254   AliMUONTrackParam trackParamSave(*trackParam);
255   TMatrixD paramSave(trackParamSave.GetParameters());
256   Double_t zBegin = trackParamSave.GetZ();
257   
258   // Get reference to the parameter covariance matrix
259   const TMatrixD& kParamCov = trackParam->GetCovariances();
260   
261   // Extrapolate track parameters to "zEnd"
262   ExtrapToZ(trackParam,zEnd);
263   
264   // Get reference to the extrapolated parameters
265   const TMatrixD& extrapParam = trackParam->GetParameters();
266   
267   // Calculate the jacobian related to the track parameters extrapolation to "zEnd"
268   TMatrixD jacob(5,5);
269   jacob.Zero();
270   TMatrixD dParam(5,1);
271   for (Int_t i=0; i<5; i++) {
272     // Skip jacobian calculation for parameters with no associated error
273     if (kParamCov(i,i) == 0.) continue;
274     
275     // Small variation of parameter i only
276     for (Int_t j=0; j<5; j++) {
277       if (j==i) {
278         dParam(j,0) = TMath::Sqrt(kParamCov(i,i));
279         if (j == 4) dParam(j,0) *= TMath::Sign(1.,-paramSave(4,0)); // variation always in the same direction
280       } else dParam(j,0) = 0.;
281     }
282     
283     // Set new parameters
284     trackParamSave.SetParameters(paramSave);
285     trackParamSave.AddParameters(dParam);
286     trackParamSave.SetZ(zBegin);
287     
288     // Extrapolate new track parameters to "zEnd"
289     ExtrapToZ(&trackParamSave,zEnd);
290     
291     // Calculate the jacobian
292     TMatrixD jacobji(trackParamSave.GetParameters(),TMatrixD::kMinus,extrapParam);
293     jacobji *= 1. / dParam(i,0);
294     jacob.SetSub(0,i,jacobji);
295   }
296   
297   // Extrapolate track parameter covariances to "zEnd"
298   TMatrixD tmp(kParamCov,TMatrixD::kMultTranspose,jacob);
299   TMatrixD tmp2(jacob,TMatrixD::kMult,tmp);
300   trackParam->SetCovariances(tmp2);
301   
302   // Update the propagator if required
303   if (updatePropagator) trackParam->UpdatePropagator(jacob);
304   
305 }
306
307   //__________________________________________________________________________
308 void AliMUONTrackExtrap::ExtrapToStation(AliMUONTrackParam* trackParamIn, Int_t station, AliMUONTrackParam *trackParamOut)
309 {
310   /// Track parameters extrapolated from "trackParamIn" to both chambers of the station(0..) "station"
311   /// are returned in the array (dimension 2) of track parameters pointed to by "TrackParamOut"
312   /// (index 0 and 1 for first and second chambers).
313   Double_t extZ[2], z1, z2;
314   Int_t i1 = -1, i2 = -1; // = -1 to avoid compilation warnings
315   // range of station to be checked ????
316   z1 = AliMUONConstants::DefaultChamberZ(2 * station);
317   z2 = AliMUONConstants::DefaultChamberZ(2 * station + 1);
318   // First and second Z to extrapolate at
319   if ((z1 > trackParamIn->GetZ()) && (z2 > trackParamIn->GetZ())) {i1 = 0; i2 = 1;}
320   else if ((z1 < trackParamIn->GetZ()) && (z2 < trackParamIn->GetZ())) {i1 = 1; i2 = 0;}
321   else {
322     cout<<"E-AliMUONTrackExtrap::ExtrapToStation: Starting Z ("<<trackParamIn->GetZ()
323         <<") in between z1 ("<<z1<<") and z2 ("<<z2<<") of station(0..)"<<station<<endl;
324     exit(-1);
325   }
326   extZ[i1] = z1;
327   extZ[i2] = z2;
328   // copy of track parameters
329   trackParamOut[i1] = *trackParamIn;
330   // first extrapolation
331   ExtrapToZ(&(trackParamOut[i1]),extZ[0]);
332   trackParamOut[i2] = trackParamOut[i1];
333   // second extrapolation
334   ExtrapToZ(&(trackParamOut[i2]),extZ[1]);
335   return;
336 }
337
338   //__________________________________________________________________________
339 void AliMUONTrackExtrap::ExtrapToVertexUncorrected(AliMUONTrackParam* trackParam, Double_t zVtx)
340 {
341   /// Extrapolation to the vertex (at the z position "zVtx") without Branson and energy loss corrections.
342   /// Returns the track parameters resulting from the extrapolation in the current TrackParam.
343   /// Include multiple Coulomb scattering effects in trackParam covariances.
344   
345   if (trackParam->GetZ() == zVtx) return; // nothing to be done if already at vertex
346   
347   if (trackParam->GetZ() > zVtx) { // spectro. (z<0)
348     cout<<"W-AliMUONTrackExtrap::ExtrapToVertexUncorrected: Starting Z ("<<trackParam->GetZ()
349         <<") upstream the vertex (zVtx = "<<zVtx<<")"<<endl;
350     exit(-1);
351   }
352   
353   // Check the vertex position relatively to the absorber
354   if (zVtx < AliMUONConstants::AbsZBeg() && zVtx > AliMUONConstants::AbsZEnd()) { // spectro. (z<0)
355     cout<<"W-AliMUONTrackExtrap::ExtrapToVertex: Ending Z ("<<zVtx
356         <<") inside the front absorber ("<<AliMUONConstants::AbsZBeg()<<","<<AliMUONConstants::AbsZEnd()<<")"<<endl;
357   } else if (zVtx < AliMUONConstants::AbsZEnd() ) { // spectro. (z<0)
358     cout<<"W-AliMUONTrackExtrap::ExtrapToVertex: Ending Z ("<<zVtx
359         <<") downstream the front absorber (zAbsorberEnd = "<<AliMUONConstants::AbsZEnd()<<")"<<endl;
360     if (trackParam->CovariancesExist()) ExtrapToZCov(trackParam,zVtx);
361     else ExtrapToZ(trackParam,zVtx);
362     return;
363   }
364   
365   // Check the track position relatively to the absorber and extrapolate track parameters to the end of the absorber if needed
366   if (trackParam->GetZ() > AliMUONConstants::AbsZBeg()) { // spectro. (z<0)
367     cout<<"W-AliMUONTrackExtrap::ExtrapToVertex: Starting Z ("<<trackParam->GetZ()
368         <<") upstream the front absorber (zAbsorberBegin = "<<AliMUONConstants::AbsZBeg()<<")"<<endl;
369     if (trackParam->CovariancesExist()) ExtrapToZCov(trackParam,zVtx);
370     else ExtrapToZ(trackParam,zVtx);
371     return;
372   } else if (trackParam->GetZ() > AliMUONConstants::AbsZEnd()) { // spectro. (z<0)
373     cout<<"W-AliMUONTrackExtrap::ExtrapToVertex: Starting Z ("<<trackParam->GetZ()
374         <<") inside the front absorber ("<<AliMUONConstants::AbsZBeg()<<","<<AliMUONConstants::AbsZEnd()<<")"<<endl;
375   } else {
376     if (trackParam->CovariancesExist()) ExtrapToZCov(trackParam,AliMUONConstants::AbsZEnd());
377     else ExtrapToZ(trackParam,AliMUONConstants::AbsZEnd());
378   }
379   
380   // Then add MCS effect in absorber to the parameters covariances
381   AliMUONTrackParam trackParamIn(*trackParam);
382   ExtrapToZ(&trackParamIn, TMath::Min(zVtx, AliMUONConstants::AbsZBeg()));
383   Double_t trackXYZIn[3];
384   trackXYZIn[0] = trackParamIn.GetNonBendingCoor();
385   trackXYZIn[1] = trackParamIn.GetBendingCoor();
386   trackXYZIn[2] = trackParamIn.GetZ();
387   Double_t trackXYZOut[3];
388   trackXYZOut[0] = trackParam->GetNonBendingCoor();
389   trackXYZOut[1] = trackParam->GetBendingCoor();
390   trackXYZOut[2] = trackParam->GetZ();
391   Double_t pathLength = 0.;
392   Double_t f0 = 0.;
393   Double_t f1 = 0.;
394   Double_t f2 = 0.;
395   Double_t meanRho = 0.;
396   GetAbsorberCorrectionParam(trackXYZIn,trackXYZOut,pathLength,f0,f1,f2,meanRho);
397   AddMCSEffectInAbsorber(trackParam,pathLength,f0,f1,f2);
398   
399   // finally go to the vertex
400   ExtrapToZCov(trackParam,zVtx);
401   
402 }
403
404   //__________________________________________________________________________
405 void AliMUONTrackExtrap::AddMCSEffectInAbsorber(AliMUONTrackParam* param, Double_t pathLength, Double_t f0, Double_t f1, Double_t f2)
406 {
407   /// Add to the track parameter covariances the effects of multiple Coulomb scattering
408   /// at the end of the front absorber using the absorber correction parameters
409   
410   // absorber related covariance parameters
411   Double_t bendingSlope = param->GetBendingSlope();
412   Double_t nonBendingSlope = param->GetNonBendingSlope();
413   Double_t inverseBendingMomentum = param->GetInverseBendingMomentum();
414   Double_t alpha2 = 0.0136 * 0.0136 * inverseBendingMomentum * inverseBendingMomentum * (1.0 + bendingSlope * bendingSlope) /
415                                         (1.0 + bendingSlope *bendingSlope + nonBendingSlope * nonBendingSlope); // velocity = 1
416   Double_t varCoor = alpha2 * (pathLength * pathLength * f0 - 2. * pathLength * f1 + f2);
417   Double_t covCorrSlope = alpha2 * (pathLength * f0 - f1);
418   Double_t varSlop = alpha2 * f0;
419   
420   TMatrixD newParamCov(param->GetCovariances());
421   // Non bending plane
422   newParamCov(0,0) += varCoor;       newParamCov(0,1) += covCorrSlope;
423   newParamCov(1,0) += covCorrSlope;  newParamCov(1,1) += varSlop;
424   // Bending plane
425   newParamCov(2,2) += varCoor;       newParamCov(2,3) += covCorrSlope;
426   newParamCov(3,2) += covCorrSlope;  newParamCov(3,3) += varSlop;
427   
428   // Set new covariances
429   param->SetCovariances(newParamCov);
430   
431 }
432
433   //__________________________________________________________________________
434 void AliMUONTrackExtrap::GetAbsorberCorrectionParam(Double_t trackXYZIn[3], Double_t trackXYZOut[3], Double_t &pathLength,
435                                                     Double_t &f0, Double_t &f1, Double_t &f2, Double_t &meanRho)
436 {
437   /// Parameters used to correct for Multiple Coulomb Scattering and energy loss in absorber
438   /// Calculated assuming a linear propagation between track positions trackXYZIn and trackXYZOut
439   // pathLength: path length between trackXYZIn and trackXYZOut (cm)
440   // f0:         0th moment of z calculated with the inverse radiation-length distribution
441   // f1:         1st moment of z calculated with the inverse radiation-length distribution
442   // f2:         2nd moment of z calculated with the inverse radiation-length distribution
443   // meanRho:    average density of crossed material (g/cm3)
444   
445   // Reset absorber's parameters
446   pathLength = 0.;
447   f0 = 0.;
448   f1 = 0.;
449   f2 = 0.;
450   meanRho = 0.;
451   
452   // Check whether the geometry is available
453   if (!gGeoManager) {
454     cout<<"E-AliMUONTrackExtrap::GetAbsorberCorrectionParam: no TGeo"<<endl;
455     return;
456   }
457   
458   // Initialize starting point and direction
459   pathLength = TMath::Sqrt((trackXYZOut[0] - trackXYZIn[0])*(trackXYZOut[0] - trackXYZIn[0])+
460                            (trackXYZOut[1] - trackXYZIn[1])*(trackXYZOut[1] - trackXYZIn[1])+
461                            (trackXYZOut[2] - trackXYZIn[2])*(trackXYZOut[2] - trackXYZIn[2]));
462   if (pathLength < TGeoShape::Tolerance()) return;
463   Double_t b[3];
464   b[0] = (trackXYZOut[0] - trackXYZIn[0]) / pathLength;
465   b[1] = (trackXYZOut[1] - trackXYZIn[1]) / pathLength;
466   b[2] = (trackXYZOut[2] - trackXYZIn[2]) / pathLength;
467   TGeoNode *currentnode = gGeoManager->InitTrack(trackXYZIn, b);
468   if (!currentnode) {
469     cout<<"E-AliMUONTrackExtrap::GetAbsorberCorrectionParam: start point out of geometry"<<endl;
470     return;
471   }
472   
473   // loop over absorber slices and calculate absorber's parameters
474   Double_t rho = 0.; // material density (g/cm3)
475   Double_t x0 = 0.;  // radiation-length (cm-1)
476   Double_t localPathLength = 0;
477   Double_t remainingPathLength = pathLength;
478   Double_t zB = trackXYZIn[2];
479   Double_t zE, dzB, dzE;
480   do {
481     // Get material properties
482     TGeoMaterial *material = currentnode->GetVolume()->GetMedium()->GetMaterial();
483     rho = material->GetDensity();
484     x0 = material->GetRadLen();
485     if (!material->IsMixture()) x0 /= rho; // different normalization in the modeler for mixture
486     
487     // Get path length within this material
488     gGeoManager->FindNextBoundary(remainingPathLength);
489     localPathLength = gGeoManager->GetStep() + 1.e-6;
490     // Check if boundary within remaining path length. If so, make sure to cross the boundary to prepare the next step
491     if (localPathLength >= remainingPathLength) localPathLength = remainingPathLength;
492     else {
493       currentnode = gGeoManager->Step();
494       if (!currentnode) {
495         cout<<"E-AliMUONTrackExtrap::GetAbsorberCorrectionParam: navigation failed"<<endl;
496         f0 = f1 = f2 = meanRho = 0.;
497         return;
498       }
499       if (!gGeoManager->IsEntering()) {
500         // make another small step to try to enter in new absorber slice
501         gGeoManager->SetStep(0.001);
502         currentnode = gGeoManager->Step();
503         if (!gGeoManager->IsEntering() || !currentnode) {
504           cout<<"E-AliMUONTrackExtrap::GetAbsorberCorrectionParam: navigation failed"<<endl;
505           f0 = f1 = f2 = meanRho = 0.;
506           return;
507         }
508         localPathLength += 0.001;
509       }
510     }
511     
512     // calculate absorber's parameters
513     zE = b[2] * localPathLength + zB;
514     dzB = zB - trackXYZIn[2];
515     dzE = zE - trackXYZIn[2];
516     f0 += localPathLength / x0;
517     f1 += (dzE*dzE - dzB*dzB) / b[2] / b[2] / x0 / 2.;
518     f2 += (dzE*dzE*dzE - dzB*dzB*dzB) / b[2] / b[2] / b[2] / x0 / 3.;
519     meanRho += localPathLength * rho;
520     
521     // prepare next step
522     zB = zE;
523     remainingPathLength -= localPathLength;
524   } while (remainingPathLength > TGeoShape::Tolerance());
525   
526   meanRho /= pathLength;
527 }
528
529   //__________________________________________________________________________
530 Double_t AliMUONTrackExtrap::GetMCSAngle2(const AliMUONTrackParam& param, Double_t dZ, Double_t x0)
531 {
532   /// Return the angular dispersion square due to multiple Coulomb scattering
533   /// through a material of thickness "dZ" and of radiation length "x0"
534   /// assuming linear propagation and using the small angle approximation.
535   
536   Double_t bendingSlope = param.GetBendingSlope();
537   Double_t nonBendingSlope = param.GetNonBendingSlope();
538   Double_t inverseTotalMomentum2 = param.GetInverseBendingMomentum() * param.GetInverseBendingMomentum() *
539                                    (1.0 + bendingSlope * bendingSlope) /
540                                    (1.0 + bendingSlope *bendingSlope + nonBendingSlope * nonBendingSlope); 
541   // Path length in the material
542   Double_t pathLength = TMath::Abs(dZ) * TMath::Sqrt(1.0 + bendingSlope*bendingSlope + nonBendingSlope*nonBendingSlope);
543   // relativistic velocity
544   Double_t velo = 1.;
545   // Angular dispersion square of the track (variance) in a plane perpendicular to the trajectory
546   Double_t theta02 = 0.0136 / velo * (1 + 0.038 * TMath::Log(pathLength/x0));
547   
548   return theta02 * theta02 * inverseTotalMomentum2 * pathLength / x0;
549 }
550
551   //__________________________________________________________________________
552 void AliMUONTrackExtrap::AddMCSEffect(AliMUONTrackParam *param, Double_t dZ, Double_t x0)
553 {
554   /// Add to the track parameter covariances the effects of multiple Coulomb scattering
555   /// through a material of thickness "dZ" and of radiation length "x0"
556   /// assuming linear propagation and using the small angle approximation.
557   
558   Double_t bendingSlope = param->GetBendingSlope();
559   Double_t nonBendingSlope = param->GetNonBendingSlope();
560   Double_t inverseTotalMomentum2 = param->GetInverseBendingMomentum() * param->GetInverseBendingMomentum() *
561                                    (1.0 + bendingSlope * bendingSlope) /
562                                    (1.0 + bendingSlope *bendingSlope + nonBendingSlope * nonBendingSlope); 
563   // Path length in the material
564   Double_t pathLength = TMath::Abs(dZ) * TMath::Sqrt(1.0 + bendingSlope*bendingSlope + nonBendingSlope*nonBendingSlope);
565   Double_t pathLength2 = pathLength * pathLength;
566   // relativistic velocity
567   Double_t velo = 1.;
568   // Angular dispersion square of the track (variance) in a plane perpendicular to the trajectory
569   Double_t theta02 = 0.0136 / velo * (1 + 0.038 * TMath::Log(pathLength/x0));
570   theta02 *= theta02 * inverseTotalMomentum2 * pathLength / x0;
571   
572   Double_t varCoor      = pathLength2 * theta02 / 3.;
573   Double_t varSlop      = theta02;
574   Double_t covCorrSlope = pathLength * theta02 / 2.;
575   
576   // Add effects of multiple Coulomb scattering in track parameter covariances
577   TMatrixD newParamCov(param->GetCovariances());
578   // Non bending plane
579   newParamCov(0,0) += varCoor;       newParamCov(0,1) += covCorrSlope;
580   newParamCov(1,0) += covCorrSlope;  newParamCov(1,1) += varSlop;
581   // Bending plane
582   newParamCov(2,2) += varCoor;       newParamCov(2,3) += covCorrSlope;
583   newParamCov(3,2) += covCorrSlope;  newParamCov(3,3) += varSlop;
584   
585   // Set new covariances
586   param->SetCovariances(newParamCov);
587 }
588
589   //__________________________________________________________________________
590 void AliMUONTrackExtrap::ExtrapToVertex(AliMUONTrackParam* trackParam, Double_t xVtx, Double_t yVtx, Double_t zVtx,
591                                         Bool_t CorrectForMCS, Bool_t CorrectForEnergyLoss)
592 {
593   /// Extrapolation to the vertex.
594   /// Returns the track parameters resulting from the extrapolation of the current TrackParam.
595   /// Changes parameters according to Branson correction through the absorber and energy loss
596   
597   if (trackParam->GetZ() == zVtx) return; // nothing to be done if already at vertex
598   
599   if (trackParam->GetZ() > zVtx) { // spectro. (z<0)
600     cout<<"F-AliMUONTrackExtrap::ExtrapToVertex: Starting Z ("<<trackParam->GetZ()
601         <<") upstream the vertex (zVtx = "<<zVtx<<")"<<endl;
602     exit(-1);
603   }
604   
605   // Check if correction required
606   if (!CorrectForMCS && !CorrectForEnergyLoss) {
607     ExtrapToZ(trackParam,zVtx);
608     return;
609   }
610   
611   // Check the vertex position relatively to the absorber
612   if (zVtx < AliMUONConstants::AbsZBeg() && zVtx > AliMUONConstants::AbsZEnd()) { // spectro. (z<0)
613     cout<<"W-AliMUONTrackExtrap::ExtrapToVertex: Ending Z ("<<zVtx
614         <<") inside the front absorber ("<<AliMUONConstants::AbsZBeg()<<","<<AliMUONConstants::AbsZEnd()<<")"<<endl;
615   } else if (zVtx < AliMUONConstants::AbsZEnd() ) { // spectro. (z<0)
616     cout<<"W-AliMUONTrackExtrap::ExtrapToVertex: Ending Z ("<<zVtx
617         <<") downstream the front absorber (zAbsorberEnd = "<<AliMUONConstants::AbsZEnd()<<")"<<endl;
618     ExtrapToZ(trackParam,zVtx);
619     return;
620   }
621   
622   // Check the track position relatively to the absorber and extrapolate track parameters to the end of the absorber if needed
623   if (trackParam->GetZ() > AliMUONConstants::AbsZBeg()) { // spectro. (z<0)
624     cout<<"W-AliMUONTrackExtrap::ExtrapToVertex: Starting Z ("<<trackParam->GetZ()
625         <<") upstream the front absorber (zAbsorberBegin = "<<AliMUONConstants::AbsZBeg()<<")"<<endl;
626     ExtrapToZ(trackParam,zVtx);
627     return;
628   } else if (trackParam->GetZ() > AliMUONConstants::AbsZEnd()) { // spectro. (z<0)
629     cout<<"W-AliMUONTrackExtrap::ExtrapToVertex: Starting Z ("<<trackParam->GetZ()
630         <<") inside the front absorber ("<<AliMUONConstants::AbsZBeg()<<","<<AliMUONConstants::AbsZEnd()<<")"<<endl;
631   } else {
632     ExtrapToZ(trackParam,AliMUONConstants::AbsZEnd());
633   }
634   
635   // Get absorber correction parameters assuming linear propagation from vertex to the track position
636   Double_t trackXYZOut[3];
637   trackXYZOut[0] = trackParam->GetNonBendingCoor();
638   trackXYZOut[1] = trackParam->GetBendingCoor();
639   trackXYZOut[2] = trackParam->GetZ();
640   Double_t trackXYZIn[3];
641   trackXYZIn[2] = TMath::Min(zVtx, AliMUONConstants::AbsZBeg()); // spectro. (z<0)
642   trackXYZIn[0] = trackXYZOut[0] + (xVtx - trackXYZOut[0]) / (zVtx - trackXYZOut[2]) * (trackXYZIn[2] - trackXYZOut[2]);
643   trackXYZIn[1] = trackXYZOut[1] + (yVtx - trackXYZOut[1]) / (zVtx - trackXYZOut[2]) * (trackXYZIn[2] - trackXYZOut[2]);
644   Double_t pathLength = 0.;
645   Double_t f0 = 0.;
646   Double_t f1 = 0.;
647   Double_t f2 = 0.;
648   Double_t meanRho = 0.;
649   GetAbsorberCorrectionParam(trackXYZIn,trackXYZOut,pathLength,f0,f1,f2,meanRho);
650   
651   // Calculate energy loss
652   Double_t pTot = trackParam->P();
653   Double_t charge = TMath::Sign(Double_t(1.0), trackParam->GetInverseBendingMomentum());      
654   Double_t deltaP = TotalMomentumEnergyLoss(pTot,pathLength,meanRho);
655   
656   // Correct for half of energy loss
657   Double_t nonBendingSlope, bendingSlope;
658   if (CorrectForEnergyLoss) {
659     pTot += 0.5 * deltaP;
660     nonBendingSlope = trackParam->GetNonBendingSlope();
661     bendingSlope = trackParam->GetBendingSlope();
662     trackParam->SetInverseBendingMomentum(charge / pTot *
663           TMath::Sqrt(1.0 + nonBendingSlope*nonBendingSlope + bendingSlope*bendingSlope) /
664           TMath::Sqrt(1.0 + bendingSlope*bendingSlope));
665   }
666   
667   if (CorrectForMCS) {
668     // Position of the Branson plane (spectro. (z<0))
669     Double_t zB = (f1>0.) ? trackXYZIn[2] - f2/f1 : 0.;
670     
671     // Get track position in the Branson plane corrected for magnetic field effect
672     ExtrapToZ(trackParam,zVtx);
673     Double_t xB = trackParam->GetNonBendingCoor() + (zB - zVtx) * trackParam->GetNonBendingSlope();
674     Double_t yB = trackParam->GetBendingCoor()    + (zB - zVtx) * trackParam->GetBendingSlope();
675     
676     // Get track slopes corrected for multiple scattering (spectro. (z<0))
677     nonBendingSlope = (zB<0.) ? (xB - xVtx) / (zB - zVtx) : trackParam->GetNonBendingSlope();
678     bendingSlope    = (zB<0.) ? (yB - yVtx) / (zB - zVtx) : trackParam->GetBendingSlope();
679     
680     // Set track parameters at vertex
681     trackParam->SetNonBendingCoor(xVtx);
682     trackParam->SetBendingCoor(yVtx);
683     trackParam->SetZ(zVtx);
684     trackParam->SetNonBendingSlope(nonBendingSlope);
685     trackParam->SetBendingSlope(bendingSlope);
686   } else {
687     ExtrapToZ(trackParam,zVtx);
688     nonBendingSlope = trackParam->GetNonBendingSlope();
689     bendingSlope = trackParam->GetBendingSlope();
690   }
691   
692   // Correct for second half of energy loss
693   if (CorrectForEnergyLoss) pTot += 0.5 * deltaP;
694   
695   // Set track parameters at vertex
696   trackParam->SetInverseBendingMomentum(charge / pTot *
697         TMath::Sqrt(1.0 + nonBendingSlope*nonBendingSlope + bendingSlope*bendingSlope) /
698         TMath::Sqrt(1.0 + bendingSlope*bendingSlope));
699   
700 }
701
702   //__________________________________________________________________________
703 Double_t AliMUONTrackExtrap::TotalMomentumEnergyLoss(AliMUONTrackParam* trackParam, Double_t xVtx, Double_t yVtx, Double_t zVtx)
704 {
705   /// Calculate the total momentum energy loss in-between the track position and the vertex assuming a linear propagation
706   
707   if (trackParam->GetZ() == zVtx) return 0.; // nothing to be done if already at vertex
708   
709   // Check whether the geometry is available
710   if (!gGeoManager) {
711     cout<<"E-AliMUONTrackExtrap::TotalMomentumEnergyLoss: no TGeo"<<endl;
712     return 0.;
713   }
714   
715   // Get encountered material correction parameters assuming linear propagation from vertex to the track position
716   Double_t trackXYZOut[3];
717   trackXYZOut[0] = trackParam->GetNonBendingCoor();
718   trackXYZOut[1] = trackParam->GetBendingCoor();
719   trackXYZOut[2] = trackParam->GetZ();
720   Double_t trackXYZIn[3];
721   trackXYZIn[0] = xVtx;
722   trackXYZIn[1] = yVtx;
723   trackXYZIn[2] = zVtx;
724   Double_t pathLength = 0.;
725   Double_t f0 = 0.;
726   Double_t f1 = 0.;
727   Double_t f2 = 0.;
728   Double_t meanRho = 0.;
729   GetAbsorberCorrectionParam(trackXYZIn,trackXYZOut,pathLength,f0,f1,f2,meanRho);
730   
731   // Calculate energy loss
732   Double_t pTot = trackParam->P();
733   return TotalMomentumEnergyLoss(pTot,pathLength,meanRho);
734 }
735
736   //__________________________________________________________________________
737 Double_t AliMUONTrackExtrap::TotalMomentumEnergyLoss(Double_t pTotal, Double_t pathLength, Double_t rho)
738 {
739   /// Returns the total momentum energy loss in the front absorber
740   Double_t muMass = 0.105658369;
741   Double_t p2=pTotal*pTotal;
742   Double_t beta2=p2/(p2 + muMass*muMass);
743   Double_t dE=ApproximateBetheBloch(beta2)*pathLength*rho;
744   
745   return dE;
746 }
747
748   //__________________________________________________________________________
749 Double_t AliMUONTrackExtrap::ApproximateBetheBloch(Double_t beta2) 
750 {
751 /// This is an approximation of the Bethe-Bloch formula with 
752 /// the density effect taken into account at beta*gamma > 3.5
753 /// (the approximation is reasonable only for solid materials) 
754
755   if (beta2/(1-beta2)>3.5*3.5)
756      return 0.153e-3/beta2*(log(3.5*5940)+0.5*log(beta2/(1-beta2)) - beta2);
757
758   return 0.153e-3/beta2*(log(5940*beta2/(1-beta2)) - beta2);
759 }
760
761  //__________________________________________________________________________
762 void AliMUONTrackExtrap::ExtrapOneStepHelix(Double_t charge, Double_t step, Double_t *vect, Double_t *vout)
763 {
764 /// <pre>
765 ///    ******************************************************************
766 ///    *                                                                *
767 ///    *  Performs the tracking of one step in a magnetic field         *
768 ///    *  The trajectory is assumed to be a helix in a constant field   *
769 ///    *  taken at the mid point of the step.                           *
770 ///    *  Parameters:                                                   *
771 ///    *   input                                                        *
772 ///    *     STEP =arc length of the step asked                         *
773 ///    *     VECT =input vector (position,direction cos and momentum)   *
774 ///    *     CHARGE=  electric charge of the particle                   *
775 ///    *   output                                                       *
776 ///    *     VOUT = same as VECT after completion of the step           *
777 ///    *                                                                *
778 ///    *    ==>Called by : <USER>, GUSWIM                               *
779 ///    *       Author    m.hansroul  *********                          *
780 ///    *       modified  s.egli, s.v.levonian                           *
781 ///    *       modified  v.perevoztchikov
782 ///    *                                                                *
783 ///    ******************************************************************
784 /// </pre>
785
786 // modif: everything in double precision
787
788     Double_t xyz[3], h[4], hxp[3];
789     Double_t h2xy, hp, rho, tet;
790     Double_t sint, sintt, tsint, cos1t;
791     Double_t f1, f2, f3, f4, f5, f6;
792
793     const Int_t kix  = 0;
794     const Int_t kiy  = 1;
795     const Int_t kiz  = 2;
796     const Int_t kipx = 3;
797     const Int_t kipy = 4;
798     const Int_t kipz = 5;
799     const Int_t kipp = 6;
800
801     const Double_t kec = 2.9979251e-4;
802     //
803     //    ------------------------------------------------------------------
804     //
805     //       units are kgauss,centimeters,gev/c
806     //
807     vout[kipp] = vect[kipp];
808     if (TMath::Abs(charge) < 0.00001) {
809       for (Int_t i = 0; i < 3; i++) {
810         vout[i] = vect[i] + step * vect[i+3];
811         vout[i+3] = vect[i+3];
812       }
813       return;
814     }
815     xyz[0]    = vect[kix] + 0.5 * step * vect[kipx];
816     xyz[1]    = vect[kiy] + 0.5 * step * vect[kipy];
817     xyz[2]    = vect[kiz] + 0.5 * step * vect[kipz];
818
819     //cmodif: call gufld (xyz, h) changed into:
820     GetField (xyz, h);
821  
822     h2xy = h[0]*h[0] + h[1]*h[1];
823     h[3] = h[2]*h[2]+ h2xy;
824     if (h[3] < 1.e-12) {
825       for (Int_t i = 0; i < 3; i++) {
826         vout[i] = vect[i] + step * vect[i+3];
827         vout[i+3] = vect[i+3];
828       }
829       return;
830     }
831     if (h2xy < 1.e-12*h[3]) {
832       ExtrapOneStepHelix3(charge*h[2], step, vect, vout);
833       return;
834     }
835     h[3] = TMath::Sqrt(h[3]);
836     h[0] /= h[3];
837     h[1] /= h[3];
838     h[2] /= h[3];
839     h[3] *= kec;
840
841     hxp[0] = h[1]*vect[kipz] - h[2]*vect[kipy];
842     hxp[1] = h[2]*vect[kipx] - h[0]*vect[kipz];
843     hxp[2] = h[0]*vect[kipy] - h[1]*vect[kipx];
844  
845     hp = h[0]*vect[kipx] + h[1]*vect[kipy] + h[2]*vect[kipz];
846
847     rho = -charge*h[3]/vect[kipp];
848     tet = rho * step;
849
850     if (TMath::Abs(tet) > 0.15) {
851       sint = TMath::Sin(tet);
852       sintt = (sint/tet);
853       tsint = (tet-sint)/tet;
854       cos1t = 2.*(TMath::Sin(0.5*tet))*(TMath::Sin(0.5*tet))/tet;
855     } else {
856       tsint = tet*tet/36.;
857       sintt = (1. - tsint);
858       sint = tet*sintt;
859       cos1t = 0.5*tet;
860     }
861
862     f1 = step * sintt;
863     f2 = step * cos1t;
864     f3 = step * tsint * hp;
865     f4 = -tet*cos1t;
866     f5 = sint;
867     f6 = tet * cos1t * hp;
868  
869     vout[kix] = vect[kix] + f1*vect[kipx] + f2*hxp[0] + f3*h[0];
870     vout[kiy] = vect[kiy] + f1*vect[kipy] + f2*hxp[1] + f3*h[1];
871     vout[kiz] = vect[kiz] + f1*vect[kipz] + f2*hxp[2] + f3*h[2];
872  
873     vout[kipx] = vect[kipx] + f4*vect[kipx] + f5*hxp[0] + f6*h[0];
874     vout[kipy] = vect[kipy] + f4*vect[kipy] + f5*hxp[1] + f6*h[1];
875     vout[kipz] = vect[kipz] + f4*vect[kipz] + f5*hxp[2] + f6*h[2];
876  
877     return;
878 }
879
880  //__________________________________________________________________________
881 void AliMUONTrackExtrap::ExtrapOneStepHelix3(Double_t field, Double_t step, Double_t *vect, Double_t *vout)
882 {
883 /// <pre>
884 ///     ******************************************************************
885 ///     *                                                                *
886 ///     *       Tracking routine in a constant field oriented            *
887 ///     *       along axis 3                                             *
888 ///     *       Tracking is performed with a conventional                *
889 ///     *       helix step method                                        *
890 ///     *                                                                *
891 ///     *    ==>Called by : <USER>, GUSWIM                               *
892 ///     *       Authors    R.Brun, M.Hansroul  *********                 *
893 ///     *       Rewritten  V.Perevoztchikov
894 ///     *                                                                *
895 ///     ******************************************************************
896 /// </pre>
897
898     Double_t hxp[3];
899     Double_t h4, hp, rho, tet;
900     Double_t sint, sintt, tsint, cos1t;
901     Double_t f1, f2, f3, f4, f5, f6;
902
903     const Int_t kix  = 0;
904     const Int_t kiy  = 1;
905     const Int_t kiz  = 2;
906     const Int_t kipx = 3;
907     const Int_t kipy = 4;
908     const Int_t kipz = 5;
909     const Int_t kipp = 6;
910
911     const Double_t kec = 2.9979251e-4;
912
913 // 
914 //     ------------------------------------------------------------------
915 // 
916 //       units are kgauss,centimeters,gev/c
917 // 
918     vout[kipp] = vect[kipp];
919     h4 = field * kec;
920
921     hxp[0] = - vect[kipy];
922     hxp[1] = + vect[kipx];
923  
924     hp = vect[kipz];
925
926     rho = -h4/vect[kipp];
927     tet = rho * step;
928     if (TMath::Abs(tet) > 0.15) {
929       sint = TMath::Sin(tet);
930       sintt = (sint/tet);
931       tsint = (tet-sint)/tet;
932       cos1t = 2.* TMath::Sin(0.5*tet) * TMath::Sin(0.5*tet)/tet;
933     } else {
934       tsint = tet*tet/36.;
935       sintt = (1. - tsint);
936       sint = tet*sintt;
937       cos1t = 0.5*tet;
938     }
939
940     f1 = step * sintt;
941     f2 = step * cos1t;
942     f3 = step * tsint * hp;
943     f4 = -tet*cos1t;
944     f5 = sint;
945     f6 = tet * cos1t * hp;
946  
947     vout[kix] = vect[kix] + f1*vect[kipx] + f2*hxp[0];
948     vout[kiy] = vect[kiy] + f1*vect[kipy] + f2*hxp[1];
949     vout[kiz] = vect[kiz] + f1*vect[kipz] + f3;
950  
951     vout[kipx] = vect[kipx] + f4*vect[kipx] + f5*hxp[0];
952     vout[kipy] = vect[kipy] + f4*vect[kipy] + f5*hxp[1];
953     vout[kipz] = vect[kipz] + f4*vect[kipz] + f6;
954
955     return;
956 }
957
958  //__________________________________________________________________________
959 void AliMUONTrackExtrap::ExtrapOneStepRungekutta(Double_t charge, Double_t step, Double_t* vect, Double_t* vout)
960 {
961 /// <pre>
962 ///     ******************************************************************
963 ///     *                                                                *
964 ///     *  Runge-Kutta method for tracking a particle through a magnetic *
965 ///     *  field. Uses Nystroem algorithm (See Handbook Nat. Bur. of     *
966 ///     *  Standards, procedure 25.5.20)                                 *
967 ///     *                                                                *
968 ///     *  Input parameters                                              *
969 ///     *       CHARGE    Particle charge                                *
970 ///     *       STEP      Step size                                      *
971 ///     *       VECT      Initial co-ords,direction cosines,momentum     *
972 ///     *  Output parameters                                             *
973 ///     *       VOUT      Output co-ords,direction cosines,momentum      *
974 ///     *  User routine called                                           *
975 ///     *       CALL GUFLD(X,F)                                          *
976 ///     *                                                                *
977 ///     *    ==>Called by : <USER>, GUSWIM                               *
978 ///     *       Authors    R.Brun, M.Hansroul  *********                 *
979 ///     *                  V.Perevoztchikov (CUT STEP implementation)    *
980 ///     *                                                                *
981 ///     *                                                                *
982 ///     ******************************************************************
983 /// </pre>
984
985     Double_t h2, h4, f[4];
986     Double_t xyzt[3], a, b, c, ph,ph2;
987     Double_t secxs[4],secys[4],seczs[4],hxp[3];
988     Double_t g1, g2, g3, g4, g5, g6, ang2, dxt, dyt, dzt;
989     Double_t est, at, bt, ct, cba;
990     Double_t f1, f2, f3, f4, rho, tet, hnorm, hp, rho1, sint, cost;
991     
992     Double_t x;
993     Double_t y;
994     Double_t z;
995     
996     Double_t xt;
997     Double_t yt;
998     Double_t zt;
999
1000     Double_t maxit = 1992;
1001     Double_t maxcut = 11;
1002
1003     const Double_t kdlt   = 1e-4;
1004     const Double_t kdlt32 = kdlt/32.;
1005     const Double_t kthird = 1./3.;
1006     const Double_t khalf  = 0.5;
1007     const Double_t kec = 2.9979251e-4;
1008
1009     const Double_t kpisqua = 9.86960440109;
1010     const Int_t kix  = 0;
1011     const Int_t kiy  = 1;
1012     const Int_t kiz  = 2;
1013     const Int_t kipx = 3;
1014     const Int_t kipy = 4;
1015     const Int_t kipz = 5;
1016   
1017     // *.
1018     // *.    ------------------------------------------------------------------
1019     // *.
1020     // *             this constant is for units cm,gev/c and kgauss
1021     // *
1022     Int_t iter = 0;
1023     Int_t ncut = 0;
1024     for(Int_t j = 0; j < 7; j++)
1025       vout[j] = vect[j];
1026
1027     Double_t  pinv   = kec * charge / vect[6];
1028     Double_t tl = 0.;
1029     Double_t h = step;
1030     Double_t rest;
1031
1032  
1033     do {
1034       rest  = step - tl;
1035       if (TMath::Abs(h) > TMath::Abs(rest)) h = rest;
1036       //cmodif: call gufld(vout,f) changed into:
1037
1038       GetField(vout,f);
1039
1040       // *
1041       // *             start of integration
1042       // *
1043       x      = vout[0];
1044       y      = vout[1];
1045       z      = vout[2];
1046       a      = vout[3];
1047       b      = vout[4];
1048       c      = vout[5];
1049
1050       h2     = khalf * h;
1051       h4     = khalf * h2;
1052       ph     = pinv * h;
1053       ph2    = khalf * ph;
1054       secxs[0] = (b * f[2] - c * f[1]) * ph2;
1055       secys[0] = (c * f[0] - a * f[2]) * ph2;
1056       seczs[0] = (a * f[1] - b * f[0]) * ph2;
1057       ang2 = (secxs[0]*secxs[0] + secys[0]*secys[0] + seczs[0]*seczs[0]);
1058       if (ang2 > kpisqua) break;
1059
1060       dxt    = h2 * a + h4 * secxs[0];
1061       dyt    = h2 * b + h4 * secys[0];
1062       dzt    = h2 * c + h4 * seczs[0];
1063       xt     = x + dxt;
1064       yt     = y + dyt;
1065       zt     = z + dzt;
1066       // *
1067       // *              second intermediate point
1068       // *
1069
1070       est = TMath::Abs(dxt) + TMath::Abs(dyt) + TMath::Abs(dzt);
1071       if (est > h) {
1072         if (ncut++ > maxcut) break;
1073         h *= khalf;
1074         continue;
1075       }
1076  
1077       xyzt[0] = xt;
1078       xyzt[1] = yt;
1079       xyzt[2] = zt;
1080
1081       //cmodif: call gufld(xyzt,f) changed into:
1082       GetField(xyzt,f);
1083
1084       at     = a + secxs[0];
1085       bt     = b + secys[0];
1086       ct     = c + seczs[0];
1087
1088       secxs[1] = (bt * f[2] - ct * f[1]) * ph2;
1089       secys[1] = (ct * f[0] - at * f[2]) * ph2;
1090       seczs[1] = (at * f[1] - bt * f[0]) * ph2;
1091       at     = a + secxs[1];
1092       bt     = b + secys[1];
1093       ct     = c + seczs[1];
1094       secxs[2] = (bt * f[2] - ct * f[1]) * ph2;
1095       secys[2] = (ct * f[0] - at * f[2]) * ph2;
1096       seczs[2] = (at * f[1] - bt * f[0]) * ph2;
1097       dxt    = h * (a + secxs[2]);
1098       dyt    = h * (b + secys[2]);
1099       dzt    = h * (c + seczs[2]);
1100       xt     = x + dxt;
1101       yt     = y + dyt;
1102       zt     = z + dzt;
1103       at     = a + 2.*secxs[2];
1104       bt     = b + 2.*secys[2];
1105       ct     = c + 2.*seczs[2];
1106
1107       est = TMath::Abs(dxt)+TMath::Abs(dyt)+TMath::Abs(dzt);
1108       if (est > 2.*TMath::Abs(h)) {
1109         if (ncut++ > maxcut) break;
1110         h *= khalf;
1111         continue;
1112       }
1113  
1114       xyzt[0] = xt;
1115       xyzt[1] = yt;
1116       xyzt[2] = zt;
1117
1118       //cmodif: call gufld(xyzt,f) changed into:
1119       GetField(xyzt,f);
1120
1121       z      = z + (c + (seczs[0] + seczs[1] + seczs[2]) * kthird) * h;
1122       y      = y + (b + (secys[0] + secys[1] + secys[2]) * kthird) * h;
1123       x      = x + (a + (secxs[0] + secxs[1] + secxs[2]) * kthird) * h;
1124
1125       secxs[3] = (bt*f[2] - ct*f[1])* ph2;
1126       secys[3] = (ct*f[0] - at*f[2])* ph2;
1127       seczs[3] = (at*f[1] - bt*f[0])* ph2;
1128       a      = a+(secxs[0]+secxs[3]+2. * (secxs[1]+secxs[2])) * kthird;
1129       b      = b+(secys[0]+secys[3]+2. * (secys[1]+secys[2])) * kthird;
1130       c      = c+(seczs[0]+seczs[3]+2. * (seczs[1]+seczs[2])) * kthird;
1131
1132       est    = TMath::Abs(secxs[0]+secxs[3] - (secxs[1]+secxs[2]))
1133         + TMath::Abs(secys[0]+secys[3] - (secys[1]+secys[2]))
1134         + TMath::Abs(seczs[0]+seczs[3] - (seczs[1]+seczs[2]));
1135
1136       if (est > kdlt && TMath::Abs(h) > 1.e-4) {
1137         if (ncut++ > maxcut) break;
1138         h *= khalf;
1139         continue;
1140       }
1141
1142       ncut = 0;
1143       // *               if too many iterations, go to helix
1144       if (iter++ > maxit) break;
1145
1146       tl += h;
1147       if (est < kdlt32) 
1148         h *= 2.;
1149       cba    = 1./ TMath::Sqrt(a*a + b*b + c*c);
1150       vout[0] = x;
1151       vout[1] = y;
1152       vout[2] = z;
1153       vout[3] = cba*a;
1154       vout[4] = cba*b;
1155       vout[5] = cba*c;
1156       rest = step - tl;
1157       if (step < 0.) rest = -rest;
1158       if (rest < 1.e-5*TMath::Abs(step)) return;
1159
1160     } while(1);
1161
1162     // angle too big, use helix
1163
1164     f1  = f[0];
1165     f2  = f[1];
1166     f3  = f[2];
1167     f4  = TMath::Sqrt(f1*f1+f2*f2+f3*f3);
1168     rho = -f4*pinv;
1169     tet = rho * step;
1170  
1171     hnorm = 1./f4;
1172     f1 = f1*hnorm;
1173     f2 = f2*hnorm;
1174     f3 = f3*hnorm;
1175
1176     hxp[0] = f2*vect[kipz] - f3*vect[kipy];
1177     hxp[1] = f3*vect[kipx] - f1*vect[kipz];
1178     hxp[2] = f1*vect[kipy] - f2*vect[kipx];
1179  
1180     hp = f1*vect[kipx] + f2*vect[kipy] + f3*vect[kipz];
1181
1182     rho1 = 1./rho;
1183     sint = TMath::Sin(tet);
1184     cost = 2.*TMath::Sin(khalf*tet)*TMath::Sin(khalf*tet);
1185
1186     g1 = sint*rho1;
1187     g2 = cost*rho1;
1188     g3 = (tet-sint) * hp*rho1;
1189     g4 = -cost;
1190     g5 = sint;
1191     g6 = cost * hp;
1192  
1193     vout[kix] = vect[kix] + g1*vect[kipx] + g2*hxp[0] + g3*f1;
1194     vout[kiy] = vect[kiy] + g1*vect[kipy] + g2*hxp[1] + g3*f2;
1195     vout[kiz] = vect[kiz] + g1*vect[kipz] + g2*hxp[2] + g3*f3;
1196  
1197     vout[kipx] = vect[kipx] + g4*vect[kipx] + g5*hxp[0] + g6*f1;
1198     vout[kipy] = vect[kipy] + g4*vect[kipy] + g5*hxp[1] + g6*f2;
1199     vout[kipz] = vect[kipz] + g4*vect[kipz] + g5*hxp[2] + g6*f3;
1200
1201     return;
1202 }
1203
1204 //___________________________________________________________
1205  void  AliMUONTrackExtrap::GetField(Double_t *Position, Double_t *Field)
1206 {
1207   /// interface for arguments in double precision (Why ? ChF)
1208   Float_t x[3], b[3];
1209
1210   x[0] = Position[0]; x[1] = Position[1]; x[2] = Position[2];
1211
1212   if (fgkField) fgkField->Field(x,b);
1213   else {
1214     cout<<"F-AliMUONTrackExtrap::GetField: fgkField = 0x0"<<endl;
1215     exit(-1);
1216   }
1217   
1218   Field[0] = b[0]; Field[1] = b[1]; Field[2] = b[2];
1219
1220   return;
1221 }