Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DTrackFitterKalmanSIMD.h
Go to the documentation of this file.
1 #ifndef _DTrackFitterKalmanSIMD_H_
2 #define _DTrackFitterKalmanSIMD_H_
3 
4 #include <vector>
5 #include <deque>
6 
7 #include <DMatrixSIMD.h>
8 #include <DVector3.h>
11 #include "HDGEOMETRY/DGeometry.h"
14 #include "CDC/DCDCTrackHit.h"
15 #include "FDC/DFDCPseudo.h"
16 #include <TH3.h>
17 #include <TH2.h>
18 #include <TH1I.h>
19 #include <TMatrixFSym.h>
20 #include "DResourcePool.h"
21 
22 #ifndef M_TWO_PI
23 #define M_TWO_PI 6.28318530717958647692
24 #endif
25 
26 #define qBr2p 0.003 // conversion for converting q*B*r to GeV/c
27 #define EPS 3.0e-8
28 #define BIG 1.0e8
29 #define EPS2 1.e-4
30 #define EPS3 1.e-2
31 #define Z_MIN -100.
32 #define Z_MAX 370.0
33 #define R_MAX 65.0
34 #define R2_MAX 4225.0
35 #define R_MAX_FORWARD 65.0
36 #ifndef SPEED_OF_LIGHT
37 #define SPEED_OF_LIGHT 29.9792458
38 #endif
39 // The next constant is 1/c and is intended to avoid too many unnecessary
40 // divisions by the speed of light
41 #define TIME_UNIT_CONVERSION 3.33564095198152014e-02
42 #define ONE_OVER_C TIME_UNIT_CONVERSION
43 #define Q_OVER_PT_MAX 100. // 10 MeV/c
44 #define TAN_MAX 10.
45 
46 #define MAX_CHI2 1e6
47 //#define MINIMUM_HIT_FRACTION 0.5
48 
49 #define DELTA_R 1.0 // distance in r to extend the trajectory beyond the last point
50 
51 #define FDC_CATHODE_VARIANCE 0.000225
52 #define FDC_ANODE_VARIANCE 0.000225
53 
54 #define ONE_THIRD 0.33333333333333333
55 #define ONE_SIXTH 0.16666666666666667
56 #define TWO_THIRDS 0.66666666666666667
57 
58 //#define DE_PER_STEP_WIRE_BASED 0.0005 // in GeV
59 //#define DE_PER_STEP_TIME_BASED 0.0005 // in GeV
60 #define DE_PER_STEP 0.001 // in GeV
61 #define BFIELD_FRAC 0.0001
62 #define MIN_STEP_SIZE 0.1 // in cm
63 
64 #define ELECTRON_MASS 0.000511 // GeV
65 
66 using namespace std;
67 
81 };
82 
83 
84 typedef struct{
86  double z0wire;
87  double tdrift,cosstereo;
88  const DCDCTrackHit *hit;
89  int status;
91 
92 typedef struct{
93  double t,cosa,sina;
94  double phiX,phiY,phiZ; // Alignment constants
95  double uwire,vstrip,vvar,z,dE;
96  double nr,nz;
97  int status;
98  const DFDCPseudo *hit;
100 
101 typedef struct{
102  DMatrix5x5 J,Q,Ckk;
105  double s,t,B;
106  double rho_Z_over_A,K_rho_Z_over_A,LnI,Z;
107  double chi2c_factor,chi2a_factor,chi2a_corr;
108  unsigned int h_id;
110 
111 typedef struct{
112  DMatrix5x5 J,Q,Ckk;
114  double z,s,t,B;
115  double rho_Z_over_A,K_rho_Z_over_A,LnI,Z;
116  double chi2c_factor,chi2a_factor,chi2a_corr;
117  unsigned int h_id;
118  unsigned int num_hits;
120 
121 typedef struct{
124  double doca;
125  double tcorr,tdrift;
126  double dDdt0;
127  double variance;
131 
132 
134  public:
135 // enum tracking_level{
136 // kWireBased,
137 // kTimeBased,
138 // };
139  DTrackFitterKalmanSIMD(JEventLoop *loop);
141  if (WRITE_ML_TRAINING_OUTPUT){
142  mlfile.close();
143  }
144  for (unsigned int i=0;i<my_cdchits.size();i++){
145  delete my_cdchits[i];
146  }
147  for (unsigned int i=0;i<my_fdchits.size();i++){
148  delete my_fdchits[i];
149  }
150  my_fdchits.clear();
151  my_cdchits.clear();
152  central_traj.clear();
153  forward_traj.clear();
154  used_cdc_indices.clear();
155  used_fdc_indices.clear();
156  cov.clear();
157  fcov.clear();
158 
159  len=0.,ftime=0.0,var_ftime=0.0;
160  x_=0.,y_=0.,tx_=0.,ty_=0.,q_over_p_ = 0.0;
161  z_=0.,phi_=0.,tanl_=0.,q_over_pt_ =0, D_= 0.0;
162  chisq_ = 0.0;
163  ndf_ = 0;
164 
165  for (unsigned int i=0;i<cdcwires.size();i++){
166  for (unsigned int j=0;j<cdcwires[i].size();j++){
167  delete cdcwires[i][j];
168  }
169  }
170  cdcwires.clear();
171 
172  }
173 
174  // Virtual methods from TrackFitter base class
175  string Name(void) const {return string("KalmanSIMD");}
176  fit_status_t FitTrack(void);
177  double ChiSq(fit_type_t fit_type, DReferenceTrajectory *rt, double *chisq_ptr=NULL, int *dof_ptr=NULL, vector<pull_t> *pulls_ptr=NULL);
178 
179  unsigned int GetRatioMeasuredPotentialFDCHits(void) const {return my_fdchits.size()/potential_fdc_hits_on_track;}
180  unsigned int GetRatioMeasuredPotentialCDCHits(void) const {return my_cdchits.size()/potential_cdc_hits_on_track;}
181 
182  jerror_t AddCDCHit(const DCDCTrackHit *cdchit);
183  jerror_t AddFDCHit(const DFDCPseudo *fdchit);
184 
185  jerror_t KalmanLoop(void);
186  virtual kalman_error_t KalmanForward(double fdc_anneal,double cdc_anneal,
187  DMatrix5x1 &S,DMatrix5x5 &C,
188  double &chisq,unsigned int &numdof);
189  virtual jerror_t SmoothForward(vector<pull_t>&mypulls);
190  virtual jerror_t ExtrapolateForwardToOtherDetectors(void);
191  jerror_t ExtrapolateCentralToOtherDetectors(void);
192 
193  kalman_error_t KalmanForwardCDC(double anneal,DMatrix5x1 &S,DMatrix5x5 &C,
194  double &chisq,unsigned int &numdof);
195  kalman_error_t KalmanCentral(double anneal_factor,DMatrix5x1 &S,
196  DMatrix5x5 &C,DVector2 &xy,double &chisq,
197  unsigned int &myndf);
198  jerror_t ExtrapolateToVertex(DVector2 &xy,DMatrix5x1 &Sc,DMatrix5x5 &Cc);
199  jerror_t ExtrapolateToVertex(DVector2 &xy,DMatrix5x1 &Sc);
200  jerror_t ExtrapolateToVertex(DMatrix5x1 &S, DMatrix5x5 &C);
201  jerror_t ExtrapolateToVertex(DMatrix5x1 &S);
202  jerror_t SetReferenceTrajectory(DMatrix5x1 &S);
203  jerror_t SetCDCForwardReferenceTrajectory(DMatrix5x1 &S);
204  jerror_t SetCDCReferenceTrajectory(const DVector2 &xy,DMatrix5x1 &Sc);
205  void GetMomentum(DVector3 &mom);
206  void GetPosition(DVector3 &pos);
207  void GetCovarianceMatrix(vector< vector<double> >&mycov){
208  mycov.assign(cov.begin(),cov.end());
209  };
210  void GetForwardCovarianceMatrix(vector< vector<double> >&mycov){
211  mycov.assign(fcov.begin(),fcov.end());
212  };
213 
214 
215  double GetCharge(void){return q_over_pt_>0?1.:-1.;};
216  double GetChiSq(void){return chisq_;}
217  unsigned int GetNDF(void){return ndf_;};
218  double GetdEdx(double q_over_p,double K_rho_Z_over_A,double rho_Z_over_A,
219  double rho_Z_over_A_LnI,double Z);
220  double GetEnergyVariance(double ds,double beta2,double K_rho_Z_over_A);
221 
222  double GetFDCDriftDistance(double time, double Bz) const {
223  return fdc_drift_distance(time, Bz);
224  }
225 
226  protected:
231  };
236  };
237 
244  };
251  };
259  state_T
260  };
261 
262  void locate(const double *xx,int n,double x,int *j);
263  unsigned int locate(vector<double>&xx,double x);
264 
265  double fdc_drift_variance(double t) const;
266  double fdc_drift_distance(double t,double Bz) const;
267 
268  void ResetKalmanSIMD(void);
269  jerror_t GetProcessNoise(double z,double ds,double chi2c_factor,
270  double chi2a_factor,
271  double chi2a_corr,
272  const DMatrix5x1 &S,DMatrix5x5 &Q);
273 
274  double Step(double oldz,double newz, double dEdx,DMatrix5x1 &S);
275  double FasterStep(double oldz,double newz, double dEdx,DMatrix5x1 &S);
276  void FastStep(double &z,double ds, double dEdx,DMatrix5x1 &S);
277  void FastStep(DVector2 &xy,double ds, double dEdx,DMatrix5x1 &S);
278  jerror_t StepJacobian(double oldz,double newz,const DMatrix5x1 &S,
279  double dEdx,DMatrix5x5 &J);
280  jerror_t CalcDerivAndJacobian(double z,double dz,const DMatrix5x1 &S,
281  double dEdx,
282  DMatrix5x5 &J,DMatrix5x1 &D);
283  jerror_t CalcJacobian(double z,double dz,const DMatrix5x1 &S,
284  double dEdx,DMatrix5x5 &J);
285  jerror_t CalcDeriv(double z,const DMatrix5x1 &S, double dEdx,
286  DMatrix5x1 &D);
287  jerror_t CalcDeriv(DVector2 &dxy,
288  const DMatrix5x1 &S,double dEdx,DMatrix5x1 &D1);
289 
290  jerror_t StepJacobian(const DVector2 &xy,double ds,const DMatrix5x1 &S,
291  double dEdx,DMatrix5x5 &J);
292  jerror_t StepJacobian(const DVector2 &xy,const DVector2 &dxy,double ds,
293  const DMatrix5x1 &S,double dEdx,DMatrix5x5 &J);
294 
295  jerror_t StepStateAndCovariance(DVector2 &xy,double ds,
296  double dEdx,DMatrix5x1 &S,
297  DMatrix5x5 &J,DMatrix5x5 &C);
298 
299  jerror_t Step(DVector2 &xy,double ds,DMatrix5x1 &S, double dEdx);
300  jerror_t FasterStep(DVector2 &xy,double ds,DMatrix5x1 &S, double dEdx);
301 
302  jerror_t CalcDerivAndJacobian(const DVector2 &xy,DVector2 &dxy,
303  const DMatrix5x1 &S,double dEdx,
304  DMatrix5x5 &J1,DMatrix5x1 &D1);
305 
306  jerror_t GetProcessNoiseCentral(double ds,double chi2c_factor,
307  double chi2a_factor,double chi2a_corr,
308  const DMatrix5x1 &S,DMatrix5x5 &Q);
309  jerror_t SmoothForwardCDC(vector<pull_t>&mypulls);
310  jerror_t SmoothCentral(vector<pull_t>&cdc_pulls);
311  jerror_t FillPullsVectorEntry(const DMatrix5x1 &Ss,const DMatrix5x5 &Cs,
312  const DKalmanForwardTrajectory_t &traj,
313  const DKalmanSIMDCDCHit_t *hit,
314  const DKalmanUpdate_t &update,
315  vector<pull_t>&mypulls);
316  jerror_t SwimToPlane(DMatrix5x1 &S);
317  jerror_t FindCentralResiduals(vector<DKalmanUpdate_t>updates);
318  jerror_t SwimCentral(DVector3 &pos,DMatrix5x1 &Sc);
319  jerror_t BrentsAlgorithm(double ds1,double ds2,
320  double dedx,DVector2 &pos,
321  const double z0wire,
322  const DVector2 &origin,
323  const DVector2 &dir,
324  DMatrix5x1 &Sc, double &ds_out);
325  jerror_t BrentsAlgorithm(double z,double dz,
326  double dedx,const double z0wire,
327  const DVector2 &origin,
328  const DVector2 &dir,DMatrix5x1 &S,
329  double &dz_out);
330  jerror_t BrentForward(double z, double dedx,
331  const double z0w,
332  const DVector2 &origin,
333  const DVector2 &dir, DMatrix5x1 &S,
334  double &dz);
335  jerror_t BrentCentral(double dedx,
336  DVector2 &xy,
337  const double z0w,
338  const DVector2 &origin,
339  const DVector2 &dir,
340  DMatrix5x1 &Sc, double &ds);
341 
342  jerror_t PropagateForwardCDC(int length,int &index,double &z,double &r2,
343  DMatrix5x1 &S, bool &stepped_to_boundary);
344  jerror_t PropagateForward(int length,int &index,double &z,double zhit,
345  DMatrix5x1 &S,bool &done,
346  bool &stepped_to_boundary,
347  bool &stepped_to_endplate);
348  jerror_t PropagateCentral(int length, int &index,DVector2 &my_xy,
349  double &var_t_factor,
350  DMatrix5x1 &Sc,bool &stepped_to_boundary);
351 
352  shared_ptr<TMatrixFSym> Get7x7ErrorMatrix(DMatrixDSym C);
353  shared_ptr<TMatrixFSym> Get7x7ErrorMatrixForward(DMatrixDSym C);
354 
355  kalman_error_t ForwardFit(const DMatrix5x1 &S,const DMatrix5x5 &C0);
356  kalman_error_t ForwardCDCFit(const DMatrix5x1 &S,const DMatrix5x5 &C0);
357  kalman_error_t CentralFit(const DVector2 &startpos,
358  const DMatrix5x1 &Sc,const DMatrix5x5 &C0);
359  kalman_error_t RecoverBrokenTracks(double anneal_factor,
360  DMatrix5x1 &S,
361  DMatrix5x5 &C,
362  const DMatrix5x5 &C0,
363  double &chisq,
364  unsigned int &numdof);
365  kalman_error_t RecoverBrokenTracks(double anneal_factor,
366  DMatrix5x1 &S,
367  DMatrix5x5 &C,
368  const DMatrix5x5 &C0,
369  DVector2 &pos,
370  double &chisq,
371  unsigned int &numdof);
372  kalman_error_t RecoverBrokenForwardTracks(double fdc_anneal_factor,
373  double cdc_anneal_factor,
374  DMatrix5x1 &S,
375  DMatrix5x5 &C,
376  const DMatrix5x5 &C0,
377  double &chisq,
378  unsigned int &numdof);
379 
380  void ComputeCDCDrift(double dphi,double delta,double t,double B,double &d,
381  double &V, double &tcorr);
382  void TransformCovariance(DMatrix5x5 &C);
383 
384  //const DMagneticFieldMap *bfield; ///< pointer to magnetic field map
385  //const DGeometry *geom;
386  //const DLorentzDeflections *lorentz_def;// pointer to lorentz correction map
387  //const DMaterialMap *material; // pointer to material map
388  //const DRootGeom *RootGeom;
389 
390  // list of hits on track
391  vector<DKalmanSIMDCDCHit_t *>my_cdchits;
392  vector<DKalmanSIMDFDCHit_t *>my_fdchits;
393 
394  // list of indices of hits used in the fit
395  vector<unsigned int>used_fdc_indices;
396  vector<unsigned int>used_cdc_indices;
397 
398 
399  // Step sizes
400  double mStepSizeZ,mStepSizeS,mCentralStepSize;
402 
403  // Track parameters for forward region
404  double x_,y_,tx_,ty_,q_over_p_;
405  // Alternate track parameters for central region
406  double z_,phi_,tanl_,q_over_pt_,D_;
407  // chi2 of fit
408  double chisq_;
409  // number of degrees of freedom
410  unsigned int ndf_;
411  // Covariance matrix
412  vector< vector <double> > cov;
413  vector< vector <double> > fcov;
414 
415  // Lists containing state, covariance, and jacobian at each step
416  deque<DKalmanCentralTrajectory_t>central_traj;
417  deque<DKalmanForwardTrajectory_t>forward_traj;
418 
419  // lists containing updated state vector and covariance at measurement point
420  vector<DKalmanUpdate_t>fdc_updates;
421  vector<DKalmanUpdate_t>cdc_updates;
422 
423  // Keep track of which hits were used in the fit
424  vector<bool>cdc_used_in_fit;
425  vector<bool>fdc_used_in_fit;
426 
427  // flight time and path length
428  double ftime, len, var_ftime;
429 
430  // B-field and gradient
431  double Bx,By,Bz;
432  double dBxdx,dBxdy,dBxdz,dBydx,dBydy,dBydz,dBzdx,dBzdy,dBzdz;
433  bool get_field;
435 
436  // endplate dimensions and location
437  double endplate_z, endplate_dz, endplate_r2min, endplate_r2max;
439  // upstream cdc start position
440  vector<double>cdc_origin;
441  // outer detectors
442  double dTOFz,dFCALz,dDIRCz;
443 
444  // Mass hypothesis
445  double MASS,mass2;
446  double m_ratio; // electron mass/MASS
447  double m_ratio_sq; // .. and its square
448  double two_m_e; // twice the electron mass
449  double m_e_sq; // square of electron mass
450 
451  // Lorentz deflection parameters
452  double LORENTZ_NR_PAR1,LORENTZ_NR_PAR2,LORENTZ_NZ_PAR1,LORENTZ_NZ_PAR2;
453 
454  // Moliere fraction F and functions that depend on it
455  double MOLIERE_FRACTION,MOLIERE_RATIO1,MOLIERE_RATIO2;
457 
458  // tables of time-to-drift values
459  vector<double>cdc_drift_table;
460  vector<double>fdc_drift_table;
461  // parameters for functional form for cdc time-to-distance
462  double long_drift_func[3][3];
463  double short_drift_func[3][3];
464  double long_drift_Bscale_par1,long_drift_Bscale_par2;
465  double short_drift_Bscale_par1,short_drift_Bscale_par2;
466 
467  // Vertex time
469  // Variance in vertex time
470  double mVarT0;
471  // Detector giving t0
473 
474  // indexes for kink/break-point analysis
475  unsigned int break_point_cdc_index,break_point_step_index;
476  unsigned int break_point_fdc_index;
477 
487  double TARGET_Z;
489  unsigned int MIN_HITS_FOR_REFIT;
490  double THETA_CUT,MINIMUM_HIT_FRACTION;
492  int RING_TO_SKIP,PLANE_TO_SKIP;
495  bool ALIGNMENT,ALIGNMENT_CENTRAL,ALIGNMENT_FORWARD;
496  double COVARIANCE_SCALE_FACTOR_FORWARD, COVARIANCE_SCALE_FACTOR_CENTRAL;
497 
498  bool USE_CDC_HITS,USE_FDC_HITS;
499 
500  // Maximum number of sigma's away from the predicted position to include hit
501  double NUM_CDC_SIGMA_CUT,NUM_FDC_SIGMA_CUT;
502 
503  // Parameters for annealing scheme
504  double ANNEAL_POW_CONST,ANNEAL_SCALE;
505  double FORWARD_ANNEAL_POW_CONST,FORWARD_ANNEAL_SCALE;
506 
507  // minimum pt or p
508  double PT_MIN;
509  double Q_OVER_P_MAX;
510 
511  // parameters for scaling drift table for CDC
512  double CDC_DRIFT_BSCALE_PAR1,CDC_DRIFT_BSCALE_PAR2;
513  // parameters for CDC resolution function
514  double CDC_RES_PAR1,CDC_RES_PAR2,CDC_RES_PAR3;
515  // parameter for scaling CDC hit variance for fits involving FDC hits.
517  // minimum drift time to use CDC hit (can be negative)
519 
520  vector<vector<double> >max_sag;
521  vector<vector<double> >sag_phi_offset;
522 
523  vector<vector<DCDCWire *> > cdcwires;
524  vector<double> cdc_rmid;
525 
526  // Parameters for dealing with FDC drift B dependence
527  double FDC_DRIFT_BSCALE_PAR1,FDC_DRIFT_BSCALE_PAR2;
528 
529  // Parameters for drift resolution
530  double DRIFT_RES_PARMS[3];
531  // parameters for time-to-distance function for FDC
532  double DRIFT_FUNC_PARMS[6];
533 
534  // Identity matrix
536  // Matrices with zeroes in them
539 
540  // FDC wire info
541  vector<double>fdc_z_wires;
543  double fdc_rmax;
544  vector<double> fdc_rmin_packages;
545 
546  // start counter geom info
547  vector<vector<DVector3> >sc_dir; // direction vector in plane of plastic
548  vector<vector<DVector3> >sc_pos;
549  vector<vector<DVector3> >sc_norm;
550  double SC_BARREL_R2,SC_END_NOSE_Z,SC_PHI_SECTOR1;
551 
552  // Beam position and direction
553  DVector2 beam_center, beam_dir;
554  double beam_z0;
555 
556  bool IsHadron,IsElectron,IsPositron;
557  TH1I *alignDerivHists[46];
558  TH2I *brentCheckHists[2];
559 
561  ofstream mlfile;
562 
563  private:
564  unsigned int last_material_map;
565  shared_ptr<DResourcePool<TMatrixFSym>> dResourcePool_TMatrixFSym;
566 
567 };
568 
569 #endif // _DTrackFitterKalmanSIMD_H_
const DFDCPseudo * hit
vector< vector< double > > fcov
unsigned int GetRatioMeasuredPotentialCDCHits(void) const
#define MOLIERE_FRACTION
The DTrackFitter class is a base class for different charged track fitting algorithms. It does not actually fit the track itself, but provides the interface and some common support features most algorthims will need to implement.
Definition: DTrackFitter.h:61
vector< vector< double > > max_sag
vector< unsigned int > used_cdc_indices
TVector2 DVector2
Definition: DVector2.h:9
double short_drift_func[3][3]
TVector3 DVector3
Definition: DVector3.h:14
Double_t x[NCHANNELS]
Definition: st_tw_resols.C:39
char string[256]
#define C0
Definition: src/md5.cpp:24
deque< DKalmanForwardTrajectory_t > forward_traj
double GetFDCDriftDistance(double time, double Bz) const
vector< vector< DVector3 > > sc_norm
shared_ptr< DResourcePool< TMatrixFSym > > dResourcePool_TMatrixFSym
DetectorSystem_t
Definition: GlueX.h:15
vector< DKalmanUpdate_t > fdc_updates
class DFDCPseudo: definition for a reconstructed point in the FDC
Definition: DFDCPseudo.h:74
vector< DKalmanSIMDFDCHit_t * > my_fdchits
TMatrixDSym DMatrixDSym
Definition: DMatrixDSym.h:13
static char index(char c)
Definition: base64.cpp:115
vector< vector< DCDCWire * > > cdcwires
vector< vector< double > > sag_phi_offset
unsigned int GetRatioMeasuredPotentialFDCHits(void) const
static void locate(const double *xx, int n, double x, int *j)
void GetForwardCovarianceMatrix(vector< vector< double > > &mycov)
double long_drift_func[3][3]
vector< double > fdc_rmin_packages
vector< vector< double > > cov
void GetCovarianceMatrix(vector< vector< double > > &mycov)
vector< DKalmanSIMDCDCHit_t * > my_cdchits
vector< unsigned int > used_fdc_indices
#define S(str)
Definition: hddm-c.cpp:84
vector< vector< DVector3 > > sc_pos
const DCDCTrackHit * hit
TDirectory * dir
Definition: bcal_hist_eff.C:31
vector< DKalmanUpdate_t > cdc_updates
vector< vector< DVector3 > > sc_dir
deque< DKalmanCentralTrajectory_t > central_traj