16 unsigned int &numdof){
25 double Vc=0.0507*1.15;
34 for (
unsigned int i=0;i<num_cdc;i++)
cdc_updates[i].used_in_fit=
false;
35 for (
unsigned int i=0;i<num_fdc;i++)
fdc_updates[i].used_in_fit=
false;
51 double my_cdc_anneal=cdc_anneal_factor*cdc_anneal_factor;
52 double my_fdc_anneal=fdc_anneal_factor*fdc_anneal_factor;
55 double fdc_chi2cut=my_fdc_anneal*var_fdc_cut;
58 double cdc_chi2cut=my_cdc_anneal*var_cdc_cut;
64 unsigned int max_num_fdc_used_in_fit=num_fdc_hits;
66 unsigned int cdc_index=0;
67 if (num_cdc_hits>0) cdc_index=num_cdc_hits-1;
68 bool more_cdc_measurements=(num_cdc_hits>0);
76 if (more_cdc_measurements){
86 jout <<
"Entering DTrackFitterKalmanSIMD_ALT1::KalmanForward ================================" << endl;
87 jout <<
" We have the following starting parameters for our fit. S = State vector, C = Covariance matrix" << endl;
90 jout << setprecision(6);
91 jout <<
" There are " << num_cdc <<
" CDC Updates and " << num_fdc <<
" FDC Updates on this track" << endl;
92 jout <<
" There are " << num_cdc_hits <<
" CDC Hits and " << num_fdc_hits <<
" FDC Hits on this track" << endl;
93 jout <<
" With NUM_FDC_SIGMA_CUT = " << NUM_FDC_SIGMA_CUT <<
" and NUM_CDC_SIGMA_CUT = " << NUM_CDC_SIGMA_CUT << endl;
94 jout <<
" fdc_anneal_factor = " << fdc_anneal_factor <<
" cdc_anneal_factor = " << cdc_anneal_factor << endl;
95 jout <<
" yields fdc_chi2cut = " << fdc_chi2cut <<
" cdc_chi2cut = " << cdc_chi2cut << endl;
97 jout <<
" S0_ which is the state vector of the reference trajectory at the break point step:" << endl;
99 jout <<
" ===== Beginning pass over reference trajectory ======== " << endl;
102 unsigned int k_minus_1=k-1;
105 if (C(0,0)<0 || C(1,1)<0 || C(2,2)<0 || C(3,3)<0 || C(4,4)<0){
146 jout <<
" At reference trajectory index " << k <<
" at z=" << z << endl;
147 jout <<
" The State vector from the reference trajectory" << endl;
149 jout <<
" The Jacobian matrix " << endl;
151 jout <<
" The Q matrix "<< endl;
153 jout <<
" The updated State vector S=S0+J*(S-S0_)" << endl;
155 jout <<
" The updated Covariance matrix C=J*(C*J_T)+Q;" << endl;
176 double vpred_uncorrected=x*sina+y*cosa;
179 double upred=x*cosa-y*sina;
182 double tu=tx*cosa-ty*sina;
183 double alpha=atan(tu);
184 double cosalpha=cos(alpha);
185 double cosalpha2=cosalpha*cosalpha;
186 double sinalpha=
sin(alpha);
189 double doca=(upred-
u)*cosalpha;
194 double nz_sinalpha_plus_nr_cosalpha=nz*sinalpha+nr*cosalpha;
200 double tv=tx*sina+ty*cosa;
201 double Mdiff=v-(vpred_uncorrected+doca*(nz_sinalpha_plus_nr_cosalpha
207 double temp2=nz_sinalpha_plus_nr_cosalpha
210 H_T(
state_x)=sina+cosa*cosalpha*temp2;
211 H_T(
state_y)=cosa-sina*cosalpha*temp2;
213 double cos2_minus_sin2=cosalpha2-sinalpha*sinalpha;
214 double fac=nz*cos2_minus_sin2-2.*nr*cosalpha*sinalpha;
215 double doca_cosalpha=doca*cosalpha;
216 double temp=doca_cosalpha*fac;
218 -doca_cosalpha*(tu*sina+tv*cosa*cos2_minus_sin2)
221 -doca_cosalpha*(tu*cosa-tv*sina*cos2_minus_sin2)
236 vector<DMatrix5x1> Klist;
237 vector<double> Mlist;
238 vector<DMatrix1x5> Hlist;
239 vector<double> Vlist;
241 vector<unsigned int>used_ids;
246 double InvV=1./Vtemp;;
249 double chi2_hit=Mdiff*Mdiff*InvV;
250 double prob_hit=exp(-0.5*chi2_hit)/
sqrt(
M_TWO_PI*Vtemp);
256 probs.push_back(prob_hit);
259 Mlist.push_back(Mdiff);
260 Klist.push_back(InvV*(C*H_T));
262 used_ids.push_back(
id);
268 unsigned int my_id=
id-m;
274 doca=(upred-
u)*cosalpha;
280 Mdiff=v-(vpred_uncorrected+doca*(nz_sinalpha_plus_nr_cosalpha
285 doca_cosalpha=doca*cosalpha;
286 temp=doca_cosalpha*fac;
288 -doca_cosalpha*(tu*sina+tv*cosa*cos2_minus_sin2)
291 -doca_cosalpha*(tu*cosa-tv*sina*cos2_minus_sin2)
304 chi2_hit=Mdiff*Mdiff*InvV;
308 if(chi2_hit<fdc_chi2cut){
309 probs.push_back(prob_hit);
310 Mlist.push_back(Mdiff);
313 Klist.push_back(InvV*(C*H_T));
315 used_ids.push_back(my_id);
321 double prob_tot=1
e-100;
322 for (
unsigned int m=0;m<probs.size();m++){
329 if (skip_plane==
false){
332 for (
unsigned int m=0;m<Klist.size();m++){
333 double my_prob=probs[m]/prob_tot;
334 S+=my_prob*(Mlist[m]*Klist[m]);
335 sum-=my_prob*(Klist[m]*Hlist[m]);
338 jout <<
" Adjusting state vector for FDC hit " << m <<
" with prob " << my_prob <<
" S:" << endl;
346 for (
unsigned int m=0;m<Hlist.size();m++){
347 unsigned int my_id=used_ids[m];
348 double scale=(skip_plane)?1.:(1.-Hlist[m]*Klist[m]);
358 if (skip_plane==
false){
359 chisq+=(probs[m]/prob_tot)*(1.-Hlist[m]*Klist[m])*Mlist[m]*Mlist[m]/Vlist[m];
364 if (skip_plane==
false){
370 if (
DEBUG_LEVEL > 25) jout <<
" == There is a single FDC hit on this plane" << endl;
374 double Vtemp=V+Vproj;
375 double InvV=1./Vtemp;
378 double chi2_hit=Mdiff*Mdiff*InvV;
379 if (chi2_hit<fdc_chi2cut){
384 if (skip_plane==
false){
392 jout <<
"S Update: " << endl; S.
Print();
393 jout <<
"C Uodate: " << endl; C.Print();
398 double scale=(skip_plane)?1.:(1.-H*K);
408 if (skip_plane==
false){
410 chisq+=scale*Mdiff*Mdiff/V;
416 printf(
"hit %d p %5.2f t %f dm %5.2f sig %f chi2 %5.2f z %5.2f\n",
432 else if (more_cdc_measurements ){
435 wirepos+=(z-z0w)*dir;
440 double doca2=dx*dx+dy*dy;
443 if (doca2>old_doca2 ){
459 double tanl=1./
sqrt(tx*tx+ty*ty);
460 double sinl=
sin(atan(tanl));
468 double denom=my_ux*my_ux+my_uy*my_uy;
481 double two_step=step1+step2;
488 dz=-((
S(
state_x)-origin.X()-ux*dzw)*my_ux
489 +(
S(
state_y)-origin.Y()-uy*dzw)*my_uy)/denom;
491 if (fabs(dz)>two_step || dz<0){
506 for (
int m=0;m<my_steps;m++){
520 Step(my_z,newz,dedx,S);
535 Step(my_z,newz,dedx,S);
568 wirepos+=(ztemp-z0w)*dir;
577 while(doca2<old_doca2){
582 Step(ztemp,newz,dedx,S);
586 wirepos+=(newz-z0w)*dir;
614 num_steps=int(fabs(dz/my_dz));
615 dz3=dz-num_steps*my_dz;
618 for (
int m=0;m<num_steps;m++){
630 Step(z,newz,dedx,S0);
646 Step(my_z,newz,dedx,S0);
658 Step(z,newz,dedx,S0);
663 wirepos+=(newz-z0w)*dir;
665 double xw=wirepos.X();
666 double yw=wirepos.Y();
671 double cosstereo=
my_cdchits[cdc_index]->cosstereo;
672 double d=
sqrt(dx*dx+dy*dy)*cosstereo+
EPS;
675 double cosstereo2_over_d=cosstereo*cosstereo/d;
676 H_T(
state_x)=dx*cosstereo2_over_d;
678 H_T(
state_y)=dy*cosstereo2_over_d;
684 double dm=0.39,tdrift=0.,tcorr=0.;
689 int ring_index=mywire->
ring-1;
690 int straw_index=mywire->
straw-1;
692 double phi_d=atan2(dy,dx);
694 =
max_sag[ring_index][straw_index]*(1.-dz*dz/5625.)
696 double dphi=phi_d-mywire->
origin.Phi();
697 while (dphi>M_PI) dphi-=2*M_PI;
698 while (dphi<-M_PI) dphi+=2*M_PI;
699 if (mywire->
origin.Y()<0) dphi*=-1.;
718 double InvV1=1./(Vc+Vproj);
721 _DBG_ <<
"Negative variance???" << endl;
726 double chi2_hit=res*res*InvV1;
727 if (chi2_hit<cdc_chi2cut){
737 if (Ctest(0,0)>0 && Ctest(1,1)>0 && Ctest(2,2)>0 && Ctest(3,3)>0
746 jout <<
" == Adding CDC Hit in Ring " <<
my_cdchits[cdc_index]->hit->wire->ring << endl;
747 jout <<
" New S: " << endl; S.
Print();
748 jout <<
" New C: " << endl; C.Print();
756 double scale=(skip_ring)?1.:(1.-H*K);
765 if (skip_ring==
false && tdrift >= 0.){
766 chisq+=scale*res*res/Vc;
771 jout <<
"Ring " <<
my_cdchits[cdc_index]->hit->wire->ring
772 <<
" Straw " <<
my_cdchits[cdc_index]->hit->wire->straw
773 <<
" Pred " << d <<
" Meas " << dm
774 <<
" Sigma meas " <<
sqrt(Vc)
776 <<
" Chi2 " << (1.-H*K)*res*res/Vc << endl;
794 for (
int m=0;m<num_steps;m++){
806 Step(my_z,z,dedx,S0);
832 else more_cdc_measurements=
false;
835 wirepos=origin+(z-z0w)*dir;
853 double my_dz=
mStepSizeZ*(dz_to_target>0?1.:-1.);
854 int num_steps=int(fabs(dz_to_target/my_dz));
856 for (
int k=0;k<num_steps;k++){
857 double newz=z_+my_dz;
884 double d=
sqrt(dx*dx+dy*dy);
887 double one_over_d=1./d;
904 _DBG_ <<
"Negative variance???" << endl;
912 double res=0.1466666667-d;
919 chisq+=(1.-H*K)*res*res/Vc;
929 unsigned int new_index=(3*num_fdc)/4;
933 unsigned int new_index=num_fdc/2;
952 cout <<
"Position after forward filter: " <<
x_ <<
", " <<
y_ <<
", " <<
z_ <<endl;
960 unsigned int new_index=num_fdc/2;
982 unsigned int new_index=num_fdc/2;
991 unsigned int num_good=0;
992 unsigned int num_hits=num_cdc+max_num_fdc_used_in_fit;
993 for (
unsigned int j=0;j<num_cdc;j++){
996 for (
unsigned int j=0;j<num_fdc;j++){
1002 unsigned int new_index=(3*num_fdc)/4;
1006 unsigned int new_index=num_fdc/2;
1025 if (
forward_traj.size()<2)
return RESOURCE_UNAVAILABLE;
1037 jout <<
"---- Smoothed residuals ----" <<endl;
1038 jout << setprecision(4);
1041 for (
unsigned int m=max-1;m>0;m--){
1049 if (
DEBUG_LEVEL>5)
_DBG_ <<
"Invalid values for smoothed parameters..." << endl;
1050 return VALUE_OUT_OF_RANGE;
1067 double vpred_uncorrected=x*sina+y*cosa;
1070 double upred=x*cosa-y*sina;
1073 double tu=tx*cosa-ty*sina;
1074 double alpha=atan(tu);
1075 double cosalpha=cos(alpha);
1077 double sinalpha=
sin(alpha);
1080 double doca=(upred-
u)*cosalpha;
1085 double nz_sinalpha_plus_nr_cosalpha=nz*sinalpha+nr*cosalpha;
1088 double tv=tx*sina+ty*cosa;
1089 double resi=v-(vpred_uncorrected+doca*(nz_sinalpha_plus_nr_cosalpha
1093 jout <<
"Layer " <<
my_fdchits[id]->hit->wire->layer
1094 <<
": x "<< x <<
" " << u*cosa+v*sina <<
" y " << y
1095 <<
" " << v*cosa-u*sina
1096 <<
" coordinate along wire " << v <<
" resi_c " <<resi
1097 <<
" wire position " << u
1106 double temp2=nz_sinalpha_plus_nr_cosalpha-tv*sinalpha;
1107 H_T(
state_x)=sina+cosa*cosalpha*temp2;
1108 H_T(
state_y)=cosa-sina*cosalpha*temp2;
1110 double cos2_minus_sin2=cosalpha*cosalpha-sinalpha*sinalpha;
1111 double fac=nz*cos2_minus_sin2-2.*nr*cosalpha*sinalpha;
1112 double doca_cosalpha=doca*cosalpha;
1113 double temp=doca_cosalpha*fac;
1115 -doca_cosalpha*(tu*sina+tv*cosa*cos2_minus_sin2)
1118 -doca_cosalpha*(tu*cosa-tv*sina*cos2_minus_sin2)
1142 if (
DEBUG_LEVEL>5)
_DBG_ <<
"Invalid values for smoothed parameters..." << endl;
1143 return VALUE_OUT_OF_RANGE;
unsigned int break_point_fdc_index
jerror_t BrentsAlgorithm(double ds1, double ds2, double dedx, DVector2 &pos, const double z0wire, const DVector2 &origin, const DVector2 &dir, DMatrix5x1 &Sc, double &ds_out)
double MINIMUM_HIT_FRACTION
vector< vector< double > > max_sag
deque< DKalmanForwardTrajectory_t > forward_traj
unsigned int break_point_step_index
vector< DKalmanUpdate_t > fdc_updates
vector< DKalmanSIMDFDCHit_t * > my_fdchits
vector< vector< double > > sag_phi_offset
unsigned int break_point_cdc_index
const DMagneticFieldMap * bfield
unsigned int MIN_HITS_FOR_REFIT
DMatrix5x5 SubSym(const DMatrix5x5 &m2) const
DMatrix5x5 SandwichMultiply(const DMatrix5x5 &A)
#define TIME_UNIT_CONVERSION
vector< DKalmanSIMDCDCHit_t * > my_cdchits
virtual double GetBz(double x, double y, double z) const =0
jerror_t StepJacobian(double oldz, double newz, const DMatrix5x1 &S, double dEdx, DMatrix5x5 &J)
void ComputeCDCDrift(double dphi, double delta, double t, double B, double &d, double &V, double &tcorr)
double Step(double oldz, double newz, double dEdx, DMatrix5x1 &S)
DMatrix5x5 MultiplyTranspose(const DMatrix5x1 &m1)
vector< bool > cdc_used_in_fit
jerror_t SmoothForward(void)
jerror_t FillPullsVectorEntry(const DMatrix5x1 &Ss, const DMatrix5x5 &Cs, const DKalmanForwardTrajectory_t &traj, const DKalmanSIMDCDCHit_t *hit, const DKalmanUpdate_t &update, vector< pull_t > &mypulls)
printf("string=%s", string)
vector< DKalmanUpdate_t > cdc_updates
DMatrix5x5 AddSym(const DMatrix5x5 &m2) const
kalman_error_t KalmanForward(double fdc_anneal, double cdc_anneal, DMatrix5x1 &S, DMatrix5x5 &C, double &chisq, unsigned int &numdof)
double GetdEdx(double q_over_p, double K_rho_Z_over_A, double rho_Z_over_A, double rho_Z_over_A_LnI, double Z)
vector< bool > fdc_used_in_fit