View Javadoc

1   package org.lcsim.lcio;
2   
3   import hep.io.sio.SIOInputStream;
4   import hep.io.sio.SIOOutputStream;
5   import hep.io.sio.SIORef;
6   
7   import java.io.IOException;
8   import java.util.ArrayList;
9   import java.util.Collections;
10  import java.util.Iterator;
11  import java.util.List;
12  
13  import org.lcsim.event.Track;
14  import org.lcsim.event.TrackState;
15  import org.lcsim.event.TrackerHit;
16  import org.lcsim.event.base.BaseTrack;
17  import org.lcsim.event.base.BaseTrackState;
18  
19  /**
20   * 
21   * @author Tony Johnson
22   * @author Jeremy McCormick
23   * @version $Id: SIOTrack.java,v 1.6 2012/06/18 23:02:14 jeremy Exp $
24   */
25  class SIOTrack extends BaseTrack
26  {
27      private List<SIORef> tempHits;
28      private List<SIORef> tempTracks;
29  
30      SIOTrack(SIOInputStream in, int flag, int version, double bField) throws IOException
31      {
32          _type = in.readInt();
33  
34          // read TrackStates
35          int nTrackStates = 1; // set to 1 per default for backwards compatibility
36  
37          if (version>=2000)
38          {
39              nTrackStates = in.readInt();
40          }
41  
42          for (int i = 0; i<nTrackStates; i++)
43          {
44              // TODO put this code into SIOTrackState.java ?
45              BaseTrackState ts = new BaseTrackState();
46  
47              if (version>=2000)
48              {
49                  ts.setLocation(in.readInt());
50              }
51  
52              ts.setD0(in.readFloat());
53              ts.setPhi(in.readFloat());
54              ts.setOmega(in.readFloat());
55              ts.setZ0(in.readFloat());
56              ts.setTanLambda(in.readFloat());
57              
58              // Compute the momentum while we have access to the B-field.
59              ts.computeMomentum(bField);
60              
61              double[] covMatrix = new double[15]; // FIXME hardcoded 15
62              for (int j = 0; j<covMatrix.length; j++)
63                  covMatrix[j] = in.readFloat();
64              ts.setCovMatrix(covMatrix);
65              double[] referencePoint = new double[3]; // FIXME hardcoded 3
66              for (int j = 0; j<referencePoint.length; j++)
67                  referencePoint[j] = in.readFloat();
68              ts.setReferencePoint(referencePoint);
69  
70              getTrackStates().add(ts);
71          }
72  
73          _chi2 = in.readFloat();
74          _ndf = in.readInt();
75          _dEdx = in.readFloat();
76          _dEdxErr = in.readFloat();
77          _innermostHitRadius = in.readFloat();
78          
79          int nHitNumbers = in.readInt();
80          _subdetId = new int[nHitNumbers];
81          for (int i = 0; i<nHitNumbers; i++)
82          {
83              _subdetId[i] = in.readInt();
84          }
85          
86          int nTracks = in.readInt();
87          tempTracks = new ArrayList(nTracks);
88          _tracks = null;
89          for (int i = 0; i<nTracks; i++)
90          {
91              tempTracks.add(in.readPntr());
92          }
93          
94          if (LCIOUtil.bitTest(flag, LCIOConstants.TRBIT_HITS))
95          {
96              int nHits = in.readInt();
97              tempHits = new ArrayList(nHits);
98              _hits = null;
99              for (int i = 0; i<nHits; i++)
100             {
101                 tempHits.add(in.readPntr());
102             }
103         }
104 
105         in.readPTag(this);
106     }
107 
108     static void write(Track track, SIOOutputStream out, int flag) throws IOException
109     {
110         out.writeInt(track.getType());
111 
112         // write out TrackStates
113         List<TrackState> trackstates = track.getTrackStates();
114         out.writeInt(trackstates.size());
115 
116         for (Iterator it = trackstates.iterator(); it.hasNext();)
117         {
118             TrackState trackstate = (TrackState) it.next();
119 
120             // TODO put this code into SIOTrackState.java ?
121             out.writeInt(trackstate.getLocation());
122             out.writeFloat((float)trackstate.getD0());
123             out.writeFloat((float)trackstate.getPhi());
124             out.writeFloat((float)trackstate.getOmega());
125             out.writeFloat((float)trackstate.getZ0());
126             out.writeFloat((float)trackstate.getTanLambda());
127             double[] covMatrix = trackstate.getCovMatrix();
128             for (int i = 0; i<covMatrix.length; i++)
129                 out.writeFloat((float)covMatrix[i]);
130             double[] referencePoint = trackstate.getReferencePoint();
131             for (int i = 0; i<referencePoint.length; i++)
132                 out.writeFloat((float)referencePoint[i]);
133         }
134 
135         out.writeFloat((float) track.getChi2());
136         out.writeInt(track.getNDF());
137         out.writeFloat((float) track.getdEdx());
138         out.writeFloat((float) track.getdEdxError());
139         out.writeFloat((float) track.getRadiusOfInnermostHit());
140         
141         int[] hitNumbers = track.getSubdetectorHitNumbers();
142         out.writeInt(hitNumbers.length);
143         for (int i = 0; i<hitNumbers.length; i++)
144         {
145             out.writeInt(hitNumbers[i]);
146         }
147         
148         List<Track> tracks = track.getTracks();
149         out.writeInt(tracks.size());
150         for (Track t: tracks)
151         {
152             out.writePntr(t);
153         }
154         
155         if (LCIOUtil.bitTest(flag, LCIOConstants.TRBIT_HITS))
156         {
157             List<TrackerHit> hits = track.getTrackerHits();
158             out.writeInt(hits.size());
159             for (TrackerHit hit: hits)
160             {
161                 out.writePntr(hit);
162             }
163         }
164         out.writePTag(track);
165     }
166 
167     public List<TrackerHit> getTrackerHits()
168     {
169         if (_hits==null && tempHits!=null)
170         {
171             _hits = new ArrayList<TrackerHit>(tempHits.size());
172             for (SIORef ref: tempHits)
173             {
174                 _hits.add((TrackerHit) ref.getObject());
175             }
176             tempHits = null;
177         }
178         return _hits==null ? Collections.<TrackerHit> emptyList() : _hits;
179     }
180 
181     public List<Track> getTracks()
182     {
183         if (_tracks==null && tempTracks!=null)
184         {
185             _tracks = new ArrayList<Track>(tempTracks.size());
186             for (SIORef ref: tempTracks)
187             {
188                 _tracks.add((Track) ref.getObject());
189             }
190             tempTracks = null;
191         }
192         return _tracks==null ? Collections.<Track> emptyList() : _tracks;
193     }
194 }