Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DLorentzDeflections.cc
Go to the documentation of this file.
1 #include "DLorentzDeflections.h"
2 #include <math.h>
3 #include <stdlib.h>
4 
5 // Locate a position in array xx given x
6 static void locate(const double *xx,int n,double x,int *j){
7  int ju,jm,jl;
8  int ascnd;
9 
10  jl=-1;
11  ju=n;
12  ascnd=(xx[n-1]>=xx[0]);
13  while(ju-jl>1){
14  jm=(ju+jl)>>1;
15  if ( (x>=xx[jm])==ascnd)
16  jl=jm;
17  else
18  ju=jm;
19  }
20  if (x==xx[0]) *j=0;
21  else if (x==xx[n-1]) *j=n-2;
22  else *j=jl;
23 }
24 
25 // Polynomial interpolation on a grid.
26 // Adapted from Numerical Recipes in C (2nd Edition), pp. 121-122.
27 static void polint(const double *xa, const double *ya,int n,double x, double *y,
28  double *dy){
29  int i,m,ns=0;
30  double den,dif,dift,ho,hp,w;
31 
32  double *c=(double *)calloc(n,sizeof(double));
33  double *d=(double *)calloc(n,sizeof(double));
34 
35  dif=fabs(x-xa[0]);
36  for (i=0;i<n;i++){
37  if ((dift=fabs(x-xa[i]))<dif){
38  ns=i;
39  dif=dift;
40  }
41  c[i]=ya[i];
42  d[i]=ya[i];
43  }
44  *y=ya[ns--];
45 
46  for (m=1;m<n;m++){
47  for (i=1;i<=n-m;i++){
48  ho=xa[i-1]-x;
49  hp=xa[i+m-1]-x;
50  w=c[i+1-1]-d[i-1];
51  if ((den=ho-hp)==0.0){
52  free(c);
53  free(d);
54  return;
55  }
56  den=w/den;
57  d[i-1]=hp*den;
58  c[i-1]=ho*den;
59 
60  }
61 
62  *y+=(*dy=(2*ns<(n-m) ?c[ns+1]:d[ns--]));
63  }
64  free(c);
65  free(d);
66 }
67 
68 // Obtain slope parameters describing Lorentz deflection by interpolating
69 // on the Lorentz deflection table
71  double y,double z,
72  double &tanz,
73  double &tanr) const{
74  int imin,ind,ind2;
75  double r=sqrt(x*x+y*y);
76 
77  // Locate positions in x and z arrays given r and z
80 
81  // First do interpolation in z direction
82  imin=PACKAGE_Z_POINTS*(ind2/PACKAGE_Z_POINTS); // Integer division...
83  double ytemp[LORENTZ_X_POINTS],ytemp2[LORENTZ_X_POINTS],dy;
84  for (int j=0;j<LORENTZ_X_POINTS;j++){
85  polint(&lorentz_z[imin],&lorentz_nx[j][imin],PACKAGE_Z_POINTS,z,
86  &ytemp[j],&dy);
87  polint(&lorentz_z[imin],&lorentz_nz[j][imin],PACKAGE_Z_POINTS,z,
88  &ytemp2[j],&dy);
89  }
90  // Then do final interpolation in x direction
91  if (ind>0){
92  imin=((ind+3)>LORENTZ_X_POINTS)?(LORENTZ_X_POINTS-4):(ind-1);
93  }
94  else imin=0;
95  polint(&lorentz_x[imin],&ytemp[imin],4,r,&tanr,&dy);
96  polint(&lorentz_x[imin],&ytemp2[imin],4,r,&tanz,&dy);
97 
98  return NOERROR;
99 }
100 
101 // Obtain deflection of avalanche along wire due to Lorentz force
102 double DLorentzDeflections::GetLorentzCorrection(double x,double y,double z,
103  double alpha,double dx) const
104 {
105  double tanz=0.,tanr=0.;
106  GetLorentzCorrectionParameters(x,y,z,tanz,tanr);
107 
108  // Deflection along wire
109  double phi=atan2(y,x);
110  return (-tanz*dx*sin(alpha)*cos(phi)+tanr*dx*cos(alpha));
111 }
static void polint(const double *xa, const double *ya, int n, double x, double *y, double *dy)
#define PACKAGE_Z_POINTS
Double_t x[NCHANNELS]
Definition: st_tw_resols.C:39
#define c
#define y
#define LORENTZ_X_POINTS
#define LORENTZ_Z_POINTS
double lorentz_x[LORENTZ_X_POINTS]
double lorentz_nx[LORENTZ_X_POINTS][LORENTZ_Z_POINTS]
static void locate(const double *xx, int n, double x, int *j)
const double alpha
jerror_t GetLorentzCorrectionParameters(double x, double y, double z, double &tanz, double &tanr) const
double lorentz_z[LORENTZ_Z_POINTS]
double lorentz_nz[LORENTZ_X_POINTS][LORENTZ_Z_POINTS]
double sqrt(double)
double GetLorentzCorrection(double x, double y, double z, double alpha, double dx) const
double sin(double)