View Javadoc

1   package org.lcsim.recon.tracking.gtrbase;
2   // McTrack
3   
4   import java.util.Set;
5   import java.util.SortedSet;
6   import java.util.TreeSet;
7   import java.util.Iterator;
8   import org.lcsim.recon.tracking.trfbase.Surface;
9   
10  /**
11   * This class describes a propagated Monte Carlo track.
12   * It is made up of a set of McTrackStates along the trajectory
13   * of the track.
14   *<p>
15   * We use a SortedSet to guarantee ordering in s.
16   *<p>
17   * The first state is the start of the trajectory (production
18   * or entering detector volume) and the last is the end of the
19   * trajectory (decay or volume exit).
20   *<p>
21   * The VTrack's represent the changing state of the track.
22   *<p>
23   * Users may fetch the entire set of states or may request the
24   * state at a specified surface.
25   *
26   *@author Norman A. Graf
27   *@version 1.0
28   *
29   */
30  public class McTrack
31  {   
32      // attributes
33      
34      private static McTrackState BadState = new McTrackState();
35      
36      // Version number
37      private int _version;
38      
39      // List of states.
40      private SortedSet _states;
41      
42      // MC Particle Id
43      // Id of the MC particle which created the states of this track
44      int _mctrackid;
45      
46      // PDG particle type
47      int _pdgid;
48      
49      // Particle Parentage
50      // Packed word containing information about particles heritage
51      int _parentword;
52      
53      // methods
54      
55      // Return the index for the first matching state.
56      // Return -1 if there is no match.
57      private int find( Surface srf)
58      {
59          int count = 0;
60          for ( Iterator ista=_states.iterator(); ista.hasNext(); )
61              if ( ((McTrackState)ista.next()).track().surface().pureEqual(srf) ) return count++;
62          return -1;
63          
64      }
65      // methods
66      
67      /**
68       *Construct a default instance.
69       * Leaves object in invalid state.
70       *
71       */
72      public McTrack()
73      {
74      }
75      
76      /**
77       *Construct a replica of the McTrack (copy constructor).
78       *
79       * @param   mct The McTrack to replicate.
80       */
81      public McTrack(McTrack mct)
82      {
83          _states = new TreeSet();
84          for(Iterator it = mct._states.iterator(); it.hasNext(); )
85          {
86              _states.add(it.next());
87          }
88          _mctrackid = mct._mctrackid;
89          _parentword = mct._parentword;
90          _pdgid = mct._pdgid;
91      }
92      
93      /**
94       * Construct n instance from a list of states, the MC Track ID, and a
95       * packed parent word (see gtrbase/McParent).
96       * Size must be nonzero and all states must be valid or
97       * assertion is thrown and all states are discarded.
98       *
99       * @param   states A list of track states.
100      * @param   mcid   The MC track ID.
101      * @param   parentword A packed parent word.
102      * @param   pdgid   The pdgid associated with this track.
103      */
104     public McTrack( Set states, int mcid, int parentword, int pdgid)
105     {
106         _states = new TreeSet();
107         for(Iterator it = states.iterator(); it.hasNext(); )
108         {
109             _states.add(it.next());
110         }
111         _mctrackid = mcid;
112         _parentword = parentword;
113         _pdgid = pdgid;
114         // Check that all states are valid.
115         int ok_count = 0;
116         for ( Iterator ista=_states.iterator(); ista.hasNext(); )
117         {
118             if ( ! ((McTrackState)ista.next()).isValid() ) break;
119             ++ok_count;
120         }
121         if( ok_count != _states.size() ) throw new IllegalArgumentException("McTrackStates not valid!");
122         if ( ok_count != _states.size() )
123             _states.clear();
124         
125     }
126     
127     /**
128      *Test track validity.
129      *
130      * @return true if this track has one or more states.
131      */
132     public boolean isValid()
133     {
134         if(_states == null) return false;
135         return _states.size()!=0;
136     }
137     
138     /**
139      *Return the list of states.
140      *
141      * @return The list of states comprising this track.
142      */
143     public Set states()
144     {
145         TreeSet states = new TreeSet();
146         for(Iterator it = _states.iterator(); it.hasNext(); )
147         {
148             states.add(it.next());
149         }
150         return states;
151     }
152     
153     /**
154      *Return whether a surface can be matched.
155      *
156      * @param   srf The surface to check.
157      * @return true whether this track contains a state at the surface srf.
158      */
159     public boolean hasSurface( Surface srf)
160     {
161         return find(srf) != -1;
162     }
163     
164     /**
165      *Return the state for a particular surface.
166      *The first match is returned.
167      * Surface bounds are not required to match.
168      * A reference to an invalid state is returned if the surface
169      * cannot be matched.
170      *
171      * @param   srf The surface for which the state is requested.
172      * @return The McTrackState at the surface srf.
173      */
174     public McTrackState state( Surface srf)
175     {
176         for ( Iterator ista=_states.iterator(); ista.hasNext(); )
177         {
178             McTrackState tmp = (McTrackState)ista.next();
179             if ( tmp.track().surface().pureEqual(srf) ) return new McTrackState(tmp);
180         }
181         return BadState;
182     }
183     
184     /**
185      *Add an McTrackState to this track.
186      *
187      * @param   state The McTrackState to add.
188      */
189     public void addState(McTrackState state)
190     {
191         if(_states == null)  _states = new TreeSet();
192         if(_states.contains(state)) throw new IllegalArgumentException("McTrack already contains this state!");
193         _states.add(new McTrackState(state));
194     }
195     
196     /**
197      *Return the MC Id for the Track creating this track's states.
198      *
199      * @return The MC track ID.
200      */
201     public int mcTrackId()
202     {
203         return _mctrackid;
204     }
205     
206     /**
207      *Set the MC Id for the Track creating this track's states.
208      *
209      * @param   mctrackid The MC track ID.
210      */
211     public void setMcTrackId( int mctrackid)
212     {
213         _mctrackid = mctrackid;
214     }
215     
216     
217     /**
218      *Return the PDG particle Id for the Track creating this track's states.
219      *
220      * @return The MC track pdg ID.
221      */
222     public int pdgId()
223     {
224         return _pdgid;
225     }
226     
227     /**
228      *Set the PDG particle Id for the Track creating this track's states
229      *
230      * @param   pdgid The MC track pdg ID.
231      */
232     public void setPdgId( int pdgid)
233     {
234         _pdgid = pdgid;
235     }
236     
237     
238     /**
239      *Return the parentage of this particle.
240      *
241      * @return The MCParent for this track's MC track.
242      */
243     public McParent parent()
244     {
245         return new McParent(_parentword);
246     }
247     
248     /**
249      *Set the MC parentage of this particle.
250      *
251      * @param   parent The MCParent for this track's MC track.
252      */
253     public void setParent(McParent parent)
254     {
255         _parentword = parent.parentWord();
256     }
257     
258     /**
259      *Set the MC parentage of this particle
260      *
261      * @param   parentword The packed MC parentage.
262      */
263     public void setParent(int parentword)
264     {
265         _parentword = parentword;
266     }
267     
268     /**
269      *output stream
270      *
271      * @return A String representation of this instance.
272      */
273     public String toString()
274     {
275         StringBuffer sb;
276         if(isValid())
277         {
278             sb = new StringBuffer("McTrack of type ");
279             sb.append(pdgId() + " and track ID " + mcTrackId()+"\n");
280             sb.append( "with " +_states.size() + " MC track states\n\n");
281             for(Iterator it = _states.iterator(); it.hasNext(); )
282             {
283                 sb.append(it.next());
284             }
285         }
286         else
287         {
288             sb = new StringBuffer("Invalid McTrack");
289         }
290         return sb.toString();
291     }
292  
293     /**
294      *Equality
295      *
296      * @param   mct The McTrack to test.
297      * @return true if equal.
298      */
299     public boolean equals(McTrack mct)
300     {
301         return _states.equals(mct._states);
302     }
303     
304     
305     /**
306      *Test inequality.
307      *
308      * @param   mct The McTrack to test.
309      * @return true if not equal.
310      */
311     public boolean notEquals(McTrack mct)
312     {
313         return !equals(mct);
314     }
315     
316 }