View Javadoc

1   /*
2    * HelicalTrackHit.java
3    *
4    * Created on November 13, 2007, 12:18 PM
5    *
6    */
7   
8   package org.lcsim.fit.helicaltrack;
9   
10  import java.util.ArrayList;
11  import java.util.List;
12  
13  import hep.physics.matrix.SymmetricMatrix;
14  import hep.physics.vec.Hep3Vector;
15  
16  import org.lcsim.event.MCParticle;
17  import org.lcsim.event.RawTrackerHit;
18  import org.lcsim.event.TrackerHit;
19  import org.lcsim.geometry.subdetector.BarrelEndcapFlag;
20  
21  /**
22   * Encapsulate the hit information needed by HelicalTrackFitter.
23   *
24   * To account for situations where the best estimate of the hit position and
25   * covariance matrix are subject to corrections, both nominal and corrected
26   * values of these quantities are stored.  The nominal and corrected values are
27   * initially identical, but the protected methods setCorrectedPosition and
28   * setCorrectedCovMatrix can be used to update the corrected values.  Currently,
29   * this feature is only used for stereo hits, where the hit position and covariance
30   * matrix depend on the track direction.
31   * 
32   * @author Richard Partridge
33   * @version $Id$
34   * 
35   */
36  public class HelicalTrackHit implements Comparable, TrackerHit {
37      
38      private double[] _pos;
39      private Hep3Vector _poscor;
40      private SymmetricMatrix _cov;
41      private SymmetricMatrix _covcor;
42      private double _dEdx;
43      private double _time;
44      private int _type;
45      private List<RawTrackerHit> _rawhits;
46      private String _detname;
47      private int _layer;
48      private BarrelEndcapFlag _beflag;
49      private List<MCParticle> _mcplist;
50      private double _r;
51      private double _phi;
52      private double _drphi;
53      private double _dr;
54      private double _chisq;
55      protected double _eps = 1e-2;
56      protected double _epsParallel;// = 1e-2;
57      protected double _epsStereoAngle = 1e-2;
58      protected long id; // FIXME: Dummy value that needs to be set from RawTrackerHit data.
59  
60      /**
61       * Create a new instance of {@ HelicalTrackHit}
62       */
63      public HelicalTrackHit() {        
64      }
65  
66      /**
67       * Create a new instance of {@ HelicalTrackHit}
68       */
69      public HelicalTrackHit(Hep3Vector pos, SymmetricMatrix cov, double dEdx, double time, int type,
70              List rawhits, String detname, int layer, BarrelEndcapFlag beflag) {
71          
72          init(pos, cov, dEdx, time, type, rawhits, detname, layer, beflag);
73          
74      }
75      
76      /**
77       * Initialize the {@link HelicalTrackHit}.
78       */
79      public void init(Hep3Vector pos, SymmetricMatrix cov, double dEdx, double time, int type,
80              List rawhits, String detname, int layer, BarrelEndcapFlag beflag) {
81          _pos = pos.v();
82          _poscor = pos;
83          _cov = cov;
84          _covcor = cov;
85          _dEdx = dEdx;
86          _time = time;
87          _type = type;
88          //  If we are passed a list of raw tracker hits use it, otherwise, create a new list
89          if (rawhits != null) {
90              _rawhits = rawhits;
91          }
92          else {
93              _rawhits = new ArrayList<RawTrackerHit>();
94          }
95          _detname = detname;
96          _layer = layer;
97          _beflag = beflag;
98          _mcplist = new ArrayList<MCParticle>();
99          _chisq = 0.;
100         setPolarVariables();
101     }
102     
103     public void setEpsParallel(double _epsParallel) {
104         this._epsParallel = _epsParallel;
105     }
106     
107     public double getEpsParallel(){
108         return _epsParallel;
109     }
110     
111     public void setEpsStereoAngle(double _epsStereoAngle) {
112         this._epsStereoAngle = _epsStereoAngle;
113     }
114     
115     /**
116      * Return the corrected x coordinate of the HelicalTrackHit
117      * @return x coordinate
118      */
119     public double x() {
120         return _poscor.x();
121     }
122     
123     /**
124      * Return the corrected y coordinate of the HelicalTrackHit
125      * @return y coordinate
126      */
127     public double y() {
128         return _poscor.y();
129     }
130     
131     /**
132      * Return the corrected z coordinate of the HelicalTrackHit
133      * @return z coordinate
134      */
135     public double z() {
136         return _poscor.z();
137     }
138     
139     /**
140      * Return the corrected radius of the hit (i.e., distance from the z axis)
141      * @return radial coordinate
142      */
143     public double r() {
144         return _r;
145     }
146     
147     /**
148      * Return the corrected azimuthal coordinate of the hit
149      * @return azimuthal coordinate
150      */
151     public double phi() {
152         return _phi;
153     }
154     
155     /**
156      * Return the corrected uncertainty in the azimuthal coordinate  r*phi
157      * @return uncertainty in r*phi
158      */
159     public double drphi() {
160         return _drphi;
161     }
162     
163     /**
164      * Return the corrected uncertainty in the radial coordinate r
165      * @return uncertainty in r
166      */
167     public double dr() {
168         return _dr;
169     }
170     
171     /**
172      * Return chi^2 penalty for the hit (used by cross hits when one or both of
173      * the unmeasured coordinates is beyond its allowed range).
174      * @return chi^2 penalty
175      */
176     public double chisq() {
177         return _chisq;
178     }
179     
180     /**
181      * Return the BarrelEndcapFlag appropriate for this hit
182      * @return BarrelEndcapFlag for this hit
183      */
184     public BarrelEndcapFlag BarrelEndcapFlag() {
185         return _beflag;
186     }
187     
188     /**
189      * Implement comparable interface to allow hits to be sorted  by their corrected
190      * z coordinate
191      * @param hit2 HelicalTrackHit to be compared against this instance
192      * @return 1 if the z for this hit is greater than for hit2
193      */
194     public int compareTo(Object hit2) {
195         double zhit = ((HelicalTrackHit) hit2).z();
196         if (_poscor.z() < zhit) return -1;
197         if (_poscor.z() == zhit) return 0;
198         return 1;
199     }
200     
201     /**
202      * Associate an MCParticle with this hit.
203      * @param mcp MCParticle that is associated with this hit
204      */
205     public void addMCParticle(MCParticle mcp) {
206         if (!_mcplist.contains(mcp)) _mcplist.add(mcp);
207         return;
208     }
209     
210     /**
211      * Returns a list of MCParticles belonging to this hit.
212      * null is returned if no list can be found.
213      * @return A list of MCParticles, or null if none can be retrieved.
214      */
215     public List<MCParticle> getMCParticles(){
216         return _mcplist;
217     }
218     
219     /**
220      * Return the nominal (uncorrected) hit position.
221      * @return nominal hit position
222      */
223     public double[] getPosition() {
224         return _pos;
225     }
226     
227     /**
228      * Return the corrected hit position.
229      * @return Corrected hit position
230      */
231     public Hep3Vector getCorrectedPosition() {
232         return _poscor;
233     }
234     
235     /**
236      * Return the nominal (uncorrected) covariance matrix.
237      * @return nominal covariance matrix
238      */
239     public double[] getCovMatrix() {
240         return _cov.asPackedArray(true);
241     }
242     
243     /**
244      * Return the corrected covariance matrix.
245      * @return corrected covariance matrix
246      */
247     public SymmetricMatrix getCorrectedCovMatrix() {
248         return _covcor;
249     }
250     
251     /**
252      * Return the energy deposit for this hit.
253      * @return energy deposit
254      */
255     public double getdEdx() {
256         return _dEdx;
257     }
258     
259     /**
260      * Return the time for this hit.
261      * @return hit time
262      */
263     public double getTime() {
264         return _time;
265     }
266     
267     /**
268      * Return the hit type.
269      * @return hit type
270      */
271     public int getType() {
272         return _type;
273     }
274     
275     public int getQuality() 
276     {
277         return 0;
278     }
279     
280     public double getEdepError()
281     {
282         return 0.;
283     }
284     
285     /**
286      * Return a list of raw hits for this hit.
287      * @return raw hit list
288      */
289     public List getRawHits() {
290         return _rawhits;
291     }
292     
293     public String Detector() {
294         return _detname;
295     }
296     
297     public int Layer() {
298         return _layer;
299     }
300     
301     /**
302      * Return the layer identifier string.
303      * @return layer identifier
304      */
305     public String getLayerIdentifier() {
306         return _detname+_layer+_beflag;
307     }
308     
309     /**
310      * Return a string describing the hit.
311      * @return hit description
312      */
313     public String toString() {
314         StringBuffer sb = new StringBuffer("HelicalTrackHit: \n");
315         sb.append("Layer Identifier= "+getLayerIdentifier()+"\n");
316         sb.append("Position= "+_poscor.toString()+"\n");
317         sb.append("Covariane= "+_covcor.toString()+"\n");
318         sb.append("dE/dx= "+this.getdEdx()+"\n");
319         sb.append("Time= "+this.getTime()+"\n");
320         return sb.toString();
321     }
322     
323     /**
324      * Set the hit position.
325      * @param position : hit position
326      *  
327      */
328     public void setPosition(double[] position){
329     	_pos = position;
330     }
331 
332     /**
333      * Set the corrected hit position.
334      * @param poscor corrected hit position
335      */
336     protected void setCorrectedPosition(Hep3Vector poscor) {
337         _poscor = poscor;
338         setPolarVariables();
339         return;
340     }
341     
342     /**
343      * Set the corrected covariance matrix.
344      * @param covcor corrected covariance matrix
345      */
346     protected void setCorrectedCovMatrix(SymmetricMatrix covcor) {
347         _covcor = covcor;
348         setPolarVariables();
349         return;
350     }
351     
352     protected void addRawHit(RawTrackerHit rawhit) {
353         if (!_rawhits.contains(rawhit)) _rawhits.add(rawhit);
354         return;
355     }
356     
357     /**
358      * Set the chi^2 penalty for the hit (used by cross hits when one or both of
359      * the unmeasured coordinates is beyond its allowed range).
360      * @param chisq chi^2 penalty
361      */
362     protected void setChisq(double chisq) {
363         _chisq = chisq;
364         return;
365     }
366     
367     protected double drphicalc(Hep3Vector pos, SymmetricMatrix cov) {
368         double x = pos.x();
369         double y = pos.y();
370         double r2 = x*x + y*y;
371         return Math.sqrt((y*y * cov.e(0,0) + x*x * cov.e(1,1) - 2. * x * y * cov.e(0,1)) / r2);
372     }
373     
374     protected double drcalc(Hep3Vector pos, SymmetricMatrix cov) {
375         double x = pos.x();
376         double y = pos.y();
377         double r2 = x*x + y*y;
378         return Math.sqrt((x*x * cov.e(0,0) + y*y * cov.e(1,1) + 2. * x * y * cov.e(0,1)) / r2);
379     }
380     
381     /**
382      * Calculate the polar coordinates _r, _phi from the cartesian
383      * coordinates _x, _y and cache them in this object since we
384      * expect them to be used repeatedly by the track finding code.
385      */
386     private void setPolarVariables() {
387         double x = _poscor.x();
388         double y = _poscor.y();
389         _r = Math.sqrt(x*x + y*y);
390         _phi = Math.atan2(y, x);
391         if (_phi < 0.) _phi += 2. * Math.PI;
392         _drphi = drphicalc(_poscor, _covcor);
393         _dr = drcalc(_poscor, _covcor);
394         
395         //do ! > because that'll handle the NaN case
396         if (! (_dr>_eps))
397             _dr = _eps;
398         
399         if (! (_drphi>_eps))
400             _drphi = _eps;
401         
402         return;
403     }
404     
405     public long getCellID()
406     {
407         return id;
408     }
409 }