View Javadoc

1   package org.lcsim.event.base;
2   
3   import static java.lang.Math.abs;
4   
5   import java.io.PrintStream;
6   
7   import org.lcsim.constants.Constants;
8   import org.lcsim.event.TrackState;
9   
10  /**
11   * Implementation of the org.lcsim.event.TrackState interface.
12   * @author Jeremy McCormick
13   * @version $Id: BaseTrackState.java,v 1.2 2012/06/18 23:02:14 jeremy Exp $
14   */
15  public class BaseTrackState implements TrackState
16  {
17      public static final int PARAMETERS_SIZE  = 5; // Number of LCIO track parameters.
18      public static final int REF_POINT_SIZE   = 3; // Size of reference point (x, y, z).
19      public static final int MOMENTUM_SIZE    = 3; // Size of momentum array (px, py, pz).
20      public static final int COV_MATRIX_SIZE = 15; // Size of covariance matrix array.
21      
22      // Initialization here is wasteful, but it protects against users leaving these null.
23      private double[] _parameters = new double[PARAMETERS_SIZE];     // Parameters array.
24      private double[] _referencePoint = new double[REF_POINT_SIZE];  // Reference point.
25      private double[] _covMatrix = new double[COV_MATRIX_SIZE];      // Covariance matrix.
26      //TODO what is momentum doing here?
27      private double[] momentum; 
28  
29      // Location encoding.
30      private int _location = TrackState.AtIP; // default location
31         
32      public BaseTrackState()
33      {}
34      
35      //fully qualified constructor
36      public BaseTrackState(double[] trackParameters, double[] covarianceMatrix, double[] position, int location)
37      {
38          _location = location;
39          System.arraycopy(trackParameters, 0, _parameters, 0, PARAMETERS_SIZE);
40          System.arraycopy(covarianceMatrix, 0, _covMatrix, 0, COV_MATRIX_SIZE);
41          System.arraycopy(position,0, _referencePoint, 0, REF_POINT_SIZE);
42      }
43      
44      // Ctor with parameters and B-field.  
45      // The reference point, covariance matrix, and location can be set later.
46      public BaseTrackState(double[] parameters, double bfield)
47      {
48          setParameters(parameters, bfield);
49      }
50      
51      // Fully qualified constructor.
52      public BaseTrackState(double[] parameters, double[] referencePoint, double[] covMatrix, int location, double bfield)
53      {
54          setParameters(parameters, bfield);
55          setReferencePoint(referencePoint);
56          setCovMatrix(covMatrix);
57          setLocation(location);
58      }
59       
60      public int getLocation()
61      {
62          return _location;
63      }
64      
65      public double[] getReferencePoint()
66      {
67          return _referencePoint;
68      }
69  
70      public double[] getCovMatrix()
71      {
72          return _covMatrix;
73      }
74      
75      public double getD0()
76      {
77          return _parameters[BaseTrack.D0];
78      }
79  
80      public double getPhi()
81      {
82          return _parameters[BaseTrack.PHI];
83      }
84  
85      public double getZ0()
86      {
87          return _parameters[BaseTrack.Z0];
88      }
89  
90      public double getOmega()
91      {
92          return _parameters[BaseTrack.OMEGA];
93      }
94  
95      public double getTanLambda()
96      {
97          return _parameters[BaseTrack.TANLAMBDA];
98      }
99      
100     public void setD0(double d0)
101     {
102         _parameters[BaseTrack.D0] = d0;
103     }
104 
105     public void setPhi(double phi)
106     {
107         _parameters[BaseTrack.PHI] = phi;
108     }
109 
110     public void setZ0(double z0)
111     {
112         _parameters[BaseTrack.Z0] = z0;
113     }
114 
115     public void setOmega(double d)
116     {
117         _parameters[BaseTrack.OMEGA] = d;
118     }
119 
120     public void setTanLambda(double d)
121     { 
122         _parameters[BaseTrack.TANLAMBDA] = d;
123     }
124     
125     public void setLocation(int location)
126     {
127         if (location < 0 || location > TrackState.LastLocation)
128             throw new IllegalArgumentException("The location must be between 0 and " + TrackState.LastLocation);
129         this._location = location;
130     }
131 
132     // FIXME Should be array copy?
133     public void setReferencePoint(double[] referencePoint)
134     {
135         if (referencePoint.length != REF_POINT_SIZE) throw new IllegalArgumentException("referencePoint.length != " + REF_POINT_SIZE);
136         this._referencePoint = referencePoint;
137     }
138     
139     // FIXME Should be array copy?
140     public void setCovMatrix(double[] covMatrix)
141     {
142         if (covMatrix.length != COV_MATRIX_SIZE) throw new IllegalArgumentException("covMatrix.length != " + COV_MATRIX_SIZE);
143         this._covMatrix = covMatrix;
144     }
145     
146     // If setParameters or a qualified constructor was not called, this could be null.
147     public double[] getMomentum()
148     {
149         return momentum;
150     }
151     
152     // Get a track parameter by ordinal.
153     public double getParameter(int param)
154     {   
155         if (param < 0 || param > (PARAMETERS_SIZE - 1))
156             throw new IllegalArgumentException("Parameter ordinal " + param + " is invalid.");
157         return _parameters[param];
158     }
159     
160     // Get the parameters as a double array.  
161     // Use ordinals in BaseTrack for the index into the array.
162     public double[] getParameters()
163     {
164         return _parameters;
165     }
166     
167     /**
168      * Set the track parameters.  Computes momentum and charge, also.
169      * @param p
170      * @param bfield
171      */
172     public void setParameters(double[] p, double bfield)
173     {
174         copyParameters(p, _parameters);
175         computeMomentum(bfield);
176     }
177     
178     static final void copyParameters(double[] p1, double[] p2)
179     {
180         if (p1.length != 5)
181             throw new IllegalArgumentException("First array is not size " + PARAMETERS_SIZE);
182         if (p2.length != 5)
183             throw new IllegalArgumentException("Second aray is not size" + PARAMETERS_SIZE);
184         System.arraycopy(p1, 0, p2, 0, PARAMETERS_SIZE);
185     }
186     
187     /**
188      * Compute the momentum of this TrackState, setting the internal array containing (px,py,pz),
189      * and return the result.
190      * @param bz The B-field in Z.
191      * @return The computed momentum.
192      */
193     public double[] computeMomentum(double bz)
194     {
195         momentum = computeMomentum(this, bz);
196         return momentum;
197     }
198     
199     /**
200      * Compute the momentum of a TrackState, given a Bz field component.
201      * @param ts The TrackState.
202      * @param Bz The magnetic field component Bz.
203      * @return The momentum computed from the TrackState's parameters.
204      */
205     public static final double[] computeMomentum(TrackState ts, double magneticField)
206     {
207         double omega = ts.getOmega();
208         if(abs(omega) < 0.0000001) omega = 0.0000001;
209         double Pt = abs((1./omega) * magneticField * Constants.fieldConversion);  
210         double[] momentum = new double[3];
211         momentum[0] = Pt * Math.cos(ts.getPhi());
212         momentum[1] = Pt * Math.sin(ts.getPhi());
213         momentum[2] = Pt * ts.getTanLambda();
214         return momentum;
215     }
216     
217     /**
218      * Convert object to a String.
219      */
220     public String toString()
221     {
222         StringBuffer buff = new StringBuffer();
223         buff.append("location = " + getLocation() + "\n");
224         buff.append("D0 = " + getD0() + "\n");
225         buff.append("phi = " + getPhi() + "\n");
226         buff.append("Z0 = " + getZ0() + "\n");
227         buff.append("tanLambda = " + getTanLambda() + "\n");
228         buff.append("omega = " + getOmega() + "\n");
229         buff.append("referencePoint = " + _referencePoint[0] + " " + _referencePoint[1] + " " + _referencePoint[2] + "\n");
230         buff.append("covarianceMatrix = ");
231         for (int i=0; i<_covMatrix.length; i++) 
232         {
233             buff.append(_covMatrix[i] + " ");
234         }
235         buff.append("\n");
236         buff.append("momentum = ");
237         for (int i=0; i<this.MOMENTUM_SIZE; i++)
238         {
239             buff.append(momentum[i] + " ");
240         }
241         buff.append("\n");
242         return buff.toString();
243     }
244     
245     public void printOut(PrintStream ps)
246     {
247         ps.println(toString());
248     }
249 }