11package org .jlab .rec .ahdc .KalmanFilter ;
22
33import java .util .ArrayList ;
4- import java .util .Collections ;
54import java .util .HashMap ;
65import java .util .Random ;
76
8- import org .apache .commons .math3 .linear .Array2DRowRealMatrix ;
97import org .apache .commons .math3 .linear .ArrayRealVector ;
108import org .apache .commons .math3 .linear .RealMatrix ;
119import org .apache .commons .math3 .linear .RealVector ;
1210import org .jlab .clas .pdg .PDGParticle ;
1311import org .jlab .clas .tracking .kalmanfilter .Material ;
14- import org .jlab .io .base .DataBank ;
15- import org .jlab .io .base .DataEvent ;
1612import org .jlab .rec .ahdc .Hit .Hit ;
1713import org .jlab .rec .ahdc .Track .Track ;
1814
@@ -36,20 +32,21 @@ public class KalmanFilter {
3632
3733 public KalmanFilter (PDGParticle particle , int Niter ) {this .particle = particle ; this .Niter = Niter ;}
3834
35+ HashMap <String , Material > materialHashMap = MaterialMap .generateMaterials ();
36+
3937 private PDGParticle particle ;
4038 private int Niter = 40 ; // number of iterations for the Kalman Filter
4139 private boolean IsVtxDefined = false ; // implemented but not used yet
42- private double [] vertex_resolutions = { 0.09 , 1e10 }; // {error in r squared in mm^2, error in z squared in mm^2}
43- // mm, they the misalignement with respect to the AHDC
44- private double clas_alignement = + 54 ;
45- private double atof_alignement = - 32.7 ;
46- double vz_constraint = 0 ;
40+ private double vz_constraint = 0 ;
41+ // mm, they are the misalignement with respect to the AHDC: the are defined in ALERTEngine
42+ private double clas_alignement = 0 ;
43+ private double atof_alignement = 0 ;
44+
4745 private int counter = 0 ; // number of utilisation of the Kalman Filter
48- HashMap <Integer , RadialKFHit > ATOF_hits = null ; // trackid vs KFHit
4946 HashMap <Integer , ArrayList <int []>> ATOF_hits_predicted = new HashMap <>(); // trackid vs (sector, layer, wedge)
50- AlertTOFDetector ATOFdet = null ;
47+ AlertTOFDetector ATOFdet = null ; // a reference to the ATOF geometry
5148
52- public void propagation (ArrayList <Track > tracks , DataEvent event , final double magfield , boolean IsMC ) {
49+ public void propagation (ArrayList <Track > tracks , final double magfield , boolean IsMC ) {
5350
5451 try {
5552 counter ++;
@@ -58,44 +55,11 @@ public void propagation(ArrayList<Track> tracks, DataEvent event, final double m
5855 final int numberOfVariables = 6 ;
5956 final double tesla = 0.001 ;
6057 final double [] B = {0.0 , 0.0 , magfield / 10 * tesla };
61- HashMap <String , Material > materialHashMap = MaterialMap .generateMaterials ();
62- // Recover the vertex of the electron
63- if (event .hasBank ("REC::Particle" ) && !IsVtxDefined ) { // as we run the KF several times, !IsVtxDefined prevent to look for the vertex again
64- DataBank recBank = event .getBank ("REC::Particle" );
65- int row = 0 ;
66- while ((!IsVtxDefined ) && row < recBank .rows ()) {
67- if (recBank .getInt ("pid" , row ) == 11 ) {
68- IsVtxDefined = true ;
69- vz_constraint = 10 *recBank .getFloat ("vz" ,row ) + (IsMC ? 0 : clas_alignement ); // mm
70-
71- // TO DO: compute electron resolution as function of p and theta
72- // the fine tuning will be done later
73- //double px = recBank.getFloat("px",row);
74- //double py = recBank.getFloat("py",row);
75- //double pz = recBank.getFloat("pz",row);
76- //double p = Math.sqrt(px*px+py*py+pz*pz);
77- //double theta = Math.acos(pz/p);
78-
79- vertex_resolutions [0 ] = 0.09 ;
80- vertex_resolutions [1 ] = 64 ;
81- }
82- row ++;
83- }
84- }
85-
86- // Define the beamline as an axtra hit for the track fitting
87- RadialKFHit hit_beam = new RadialKFHit (0 , 0 , vz_constraint );
88- RealMatrix measurementNoise = new Array2DRowRealMatrix (
89- new double [][]{
90- {vertex_resolutions [0 ], 0.0000 , 0.0000 },
91- {0.00 , 1e10 , 0.0000 },
92- {0.00 , 0.0000 , vertex_resolutions [1 ]}
93- });//3x3;
94- hit_beam .setMeasurementNoise (measurementNoise );
58+
9559
9660 // Loop over tracks
9761 for (Track track : tracks ) {
98- // Initialize state vector
62+ /// Initialize state vector
9963 double x0 = 0.0 ;
10064 double y0 = 0.0 ;
10165 double z0 = (IsVtxDefined && counter < 2 ) ? vz_constraint : track .get_Z0 ();
@@ -104,71 +68,82 @@ public void propagation(ArrayList<Track> tracks, DataEvent event, final double m
10468 double pz0 = track .get_pz ();
10569
10670 double [] y = new double []{x0 , y0 , z0 , px0 , py0 , pz0 };
107- // Read list of hits
71+
72+ /// Read list of hits
73+ RadialKFHit beam_hit = track .getBeamlineHit ();
10874 ArrayList <Hit > AHDC_hits = track .getHits ();
109- Collections . sort ( AHDC_hits ); // sorted following the compareTo() method in Hit.java
75+ ArrayList < RadialKFHit > ATOF_hits = track . getATOFHits ();
11076
111- // Initialize propagator
77+ /// Initialize propagator
11278 RungeKutta4 RK4 = new RungeKutta4 (particle , numberOfVariables , B );
11379 Propagator propagator = new Propagator (RK4 );
11480
115- // Initialization of the Kalman Fitter
116- // for the error matrix: first 3 lines in mm^2; last 3 lines in MeV^2
81+ /// Initialization of the Kalman Fitter
11782 RealVector initialStateEstimate = new ArrayRealVector (y );
11883 RealMatrix initialErrorCovariance = track .getErrorCovarianceMatrix ();
11984 KFitter TrackFitter = new KFitter (initialStateEstimate , initialErrorCovariance , propagator , materialHashMap );
12085
12186 // Loop over number of iterations
12287 for (int k = 0 ; k < Niter ; k ++) {
123- // Forward propagation
88+
89+ // Forward propagation in AHDC
12490 for (Hit hit : AHDC_hits ) {
12591 TrackFitter .predict (hit , true );
12692 TrackFitter .correct (hit );
12793
12894 }
129- // Forward propagation towards the ATOF bar
130- {
131- RadialKFHit hit = ATOF_hits .get (track .get_trackId ());
132- if (hit != null ) {
95+
96+ // Take into account ATOF hits
97+ if (!AHDC_hits .isEmpty () && !ATOF_hits .isEmpty ()) {
98+ // Forward propagation in ATOF
99+ for (KFHit hit : ATOF_hits ) {
133100 TrackFitter .predict (hit , true );
134101 TrackFitter .correct (hit );
135- // Backward propagation to the last ahdc layer
136- if (AHDC_hits .size () > 0 ) {
137- Hit hhit = AHDC_hits .get (AHDC_hits .size ()-1 );
138- TrackFitter .predict (hhit , false );
139- TrackFitter .correct (hhit );
140- }
102+ }
103+ // Backward propagation in ATOF
104+ for (int i = ATOF_hits .size ()-2 ; i >= 0 ; i --){
105+ KFHit hit = ATOF_hits .get (i );
106+ TrackFitter .predict (hit , false );
107+ TrackFitter .correct (hit );
108+ }
109+ // Backward propagation to the last AHDC layer
110+ {
111+ Hit hit = AHDC_hits .getLast ();
112+ TrackFitter .predict (hit , false );
113+ TrackFitter .correct (hit );
141114 }
142115 }
143- // Backward propagation (last layer to first layer)
144- for (int i = AHDC_hits .size () - 2 ; i >= 0 ; i --) {
116+
117+ // Backward propagation (from AHDC last layer to first AHDC layer)
118+ for (int i = AHDC_hits .size ()-2 ; i >= 0 ; i --) {
145119 Hit hit = AHDC_hits .get (i );
146120 TrackFitter .predict (hit , false );
147121 TrackFitter .correct (hit );
148122 }
149- // Backward propagation (first layer to beamline)
123+
124+ // Backward propagation (from first AHDC layer to beamline)
150125 {
151- TrackFitter .predict (hit_beam , false );
152- TrackFitter .correct (hit_beam );
126+ TrackFitter .predict (beam_hit , false );
127+ TrackFitter .correct (beam_hit );
153128 }
154129
155- }
130+ } // end loop over nb. iteration
156131
132+ /// Output: position and momentum
157133 RealVector x_out = TrackFitter .getStateEstimationVector ();
158134 track .setPositionAndMomentumVec (x_out .toArray ());
159135 track .setErrorCovarianceMatrix (TrackFitter .getErrorCovarianceMatrix ());
160136
161- // Post fit propagation (no correction) to set the residuals
137+ /// Post fit propagation (no correction)
162138 KFitter PostFitPropagator = new KFitter (TrackFitter .getStateEstimationVector (), initialErrorCovariance , new Propagator (RK4 ), materialHashMap );
163- // Projection towards AHDC hits
139+
140+ // Forward propagation in AHDC
164141 for (Hit hit : AHDC_hits ) {
165142 PostFitPropagator .predict (hit , true );
166- if ( hit .getId ()>0 ){ // for the beamline the hit id is 0, so we only look at AHDC hits
167- hit .setResidual (PostFitPropagator .residual (hit ));
168- }
143+ hit .setResidual (PostFitPropagator .residual (hit )); // output: residual
169144 }
170145
171- // Fill track and hit bank
146+ // Fill values for AHDC::kftrack
172147 // TO DO : s and p_drift have to be checked to be sure they represent what we want
173148 Stepper current_stepper = PostFitPropagator .getStepper ();
174149 double s = current_stepper .sTot ;
@@ -189,6 +164,11 @@ public void propagation(ArrayList<Track> tracks, DataEvent event, final double m
189164 track .set_path (s );
190165 track .set_n_hits (AHDC_hits .size ());
191166
167+
168+ ////////
169+ /// Revision of the code: I'm here
170+ /// /////
171+
192172 // Projection towards the ATOF surfaces
193173 // R1 : lower surface of an ATOF bar
194174 // R2 : upper surface of an ATOF bar = lower surface of an ATOF wedge
@@ -199,7 +179,7 @@ public void propagation(ArrayList<Track> tracks, DataEvent event, final double m
199179 double R1 = Math .hypot (pR1 .x (), pR1 .y ());
200180 double R2 = Math .hypot (pR2 .x (), pR2 .y ());
201181 double R3 = Math .hypot (pR3 .x (), pR3 .y ());
202- {
182+ {
203183 // From last AHDC hit to surface R1
204184 RadialSurfaceKFHit hitR1 = new RadialSurfaceKFHit (R1 );
205185 PostFitPropagator .predict (hitR1 , true );
@@ -270,11 +250,9 @@ public void propagation(ArrayList<Track> tracks, DataEvent event, final double m
270250 }
271251 }
272252 public void set_Niter (int Niter ) {this .Niter = Niter ;}
273- public int get_Niter () {return this .Niter ;}
274- public void set_particle (PDGParticle particle ) {this .particle = particle ;}
253+ public int get_Niter () {return this .Niter ;}
254+ public void set_particle (PDGParticle particle ) {this .particle = particle ;}
275255 public PDGParticle get_particle () {return this .particle ;}
276- public void set_ATOF_hits (HashMap <Integer , RadialKFHit > ATOF_hits ){ this .ATOF_hits = ATOF_hits ;};
277- public HashMap <Integer , RadialKFHit > get_ATOF_hits () { return this .ATOF_hits ;}
278256 public HashMap <Integer , ArrayList <int []>> get_ATOF_hits_predicted () {return this .ATOF_hits_predicted ;}
279257
280258 // Test
@@ -441,4 +419,8 @@ else if (layer == 3) {
441419 }
442420
443421 public void set_ATOF_detector (AlertTOFDetector atof ) { this .ATOFdet = atof ;}
422+ public void set_clas_alignement (double _shift ) {this .clas_alignement = _shift ;}
423+ public void set_atof_alignement (double _shift ) {this .atof_alignement = _shift ;}
424+ public void set_vz_constraint (double _vz ) {this .vz_constraint = _vz ;}
425+ public void set_vertex_flag (boolean _flag ) {this .IsVtxDefined = _flag ;}
444426}
0 commit comments