LCIO  02.17
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
test_tracks.cc
Go to the documentation of this file.
1 // test lcio::Track
4 
5 #include "tutil.h"
6 #include "lcio.h"
7 
8 #include "EVENT/LCIO.h"
9 #include "IO/LCReader.h"
10 #include "IO/LCWriter.h"
11 #include "IMPL/LCEventImpl.h"
12 #include "IMPL/LCCollectionVec.h"
13 #include "IMPL/TrackImpl.h"
14 #include "IMPL/TrackStateImpl.h"
15 
16 #include "UTIL/Operators.h"
17 #include "UTIL/LCIterator.h"
18 
19 #include <sstream>
20 #include <assert.h>
21 
22 using namespace std ;
23 using namespace lcio ;
24 
25 //static const int NRUN = 10 ;
26 static const int NEVENT = 3 ; // events
27 static const int NTRACKS = 10 ; // tracks per event
28 
29 static string FILEN = "tracks.slcio" ;
30 
31 // replace mytest with the name of your test
32 const static string testname="test_tracks";
33 
34 //=============================================================================
35 
36 int main(int /*argc*/, char** /*argv*/ ){
37 
38  // this should be the first line in your test
39  TEST MYTEST=TEST( testname, std::cout );
40 
41  try{
42 
43  MYTEST.LOG( " writing Tracks " );
44 
45  // create sio writer
46  LCWriter* lcWrt = LCFactory::getInstance()->createLCWriter() ;
47 
48  lcWrt->open( FILEN , LCIO::WRITE_NEW ) ;
49 
50 
51  // EventLoop - create some events and write them to the file
52  for( int i=0 ; i<NEVENT ; i++ ){
53 
54  // we need to use the implementation classes here
55  LCEventImpl* evt = new LCEventImpl() ;
56 
57 
58  evt->setRunNumber( 4711 ) ;
59  evt->setEventNumber( i ) ;
60 
61  LCCollectionVec* tracks = new LCCollectionVec( LCIO::TRACK ) ;
62  LCCollectionVec* tracksCopies = new LCCollectionVec( LCIO::TRACK ) ;
63  LCCollectionVec* tracksWithMultipleStates = new LCCollectionVec( LCIO::TRACK ) ;
64  LCCollectionVec* tracksWithMultipleStatesCopies = new LCCollectionVec( LCIO::TRACK ) ;
65 
66  for( int j=0 ; j<NTRACKS ; j++ ){
67 
68  TrackImpl* trk = new TrackImpl ;
69 
70  trk->setTypeBit( 7 ) ;
71  trk->setOmega( (i+j) * .1 ) ;
72  trk->setTanLambda( (i+j) * .2 ) ;
73  trk->setPhi( (i+j) * .3 ) ;
74  trk->setD0( i * .1 ) ;
75  trk->setZ0( j * .1 ) ;
76  trk->setChi2( 1.01 ) ;
77  trk->setNdf( 42 ) ;
78  trk->setRadiusOfInnermostHit( 3.14159265 ) ;
79  trk->setdEdx( 3.14159265 ) ;
80  trk->setdEdxError( 42. ) ;
81 
82  float cov[15] = { 1.,2.,3.,4.,5.,6.,7.,8.,9.,10.,11.,12.,13.,14.,15. } ;
83  trk->setCovMatrix( cov ) ;
84 
85  float ref[3] = { 1. , 2. , 3. } ;
86  trk->setReferencePoint( ref ) ;
87 
88  // add tracks that where used to create this track
89  if( tracks->size() > 1 ){
90  trk->addTrack( dynamic_cast<TrackImpl*> ( (*tracks)[ tracks->size() - 1 ] ) ) ;
91  trk->addTrack( dynamic_cast<TrackImpl*> ( (*tracks)[ tracks->size() - 2 ] ) ) ;
92  }
93 
94  TrackImpl* trkc = new TrackImpl(*trk) ;
95 
96  tracks->addElement( trk ) ;
97  tracksCopies->addElement( trkc ) ;
98  }
99 
100  for( int j=0 ; j<NTRACKS ; j++ ){
101  TrackImpl* trk = new TrackImpl ;
102 
103  trk->setTypeBit( 7 ) ;
104 
105  trk->setOmega( (i+j) * .1 ) ;
106  trk->setTanLambda( (i+j) * .2 ) ;
107  trk->setPhi( (i+j) * .3 ) ;
108  trk->setD0( i * .1 ) ;
109  trk->setZ0( j * .1 ) ;
110  trk->setChi2( 1.01 ) ;
111  trk->setNdf( 42 ) ;
112  trk->setRadiusOfInnermostHit( 3.14159265 ) ;
113  trk->setdEdx( 3.14159265 ) ;
114  trk->setdEdxError( 42. ) ;
115 
116  float cov[15] = { 1.,2.,3.,4.,5.,6.,7.,8.,9.,10.,11.,12.,13.,14.,15. } ;
117  trk->setCovMatrix( cov ) ;
118 
119  float ref[3] = { 1. , 2. , 3. } ;
120  trk->setReferencePoint( ref ) ;
121 
122  // add some additional TrackStates
123  for( int ki=1 ; ki < 4 ; ki++ ){
124 
125  float k( ki );
126 
127  float covL[15] = { k+1,k+2,k+3,k+4,k+5,k+6,k+7,k+8,k+9,k+10,k+11,k+12,k+13,k+14,k+15 } ;
128  float refL[3] = { k*1 , k*2 , k*3 } ;
129 
130  TrackStateImpl* trkstate = new TrackStateImpl(
131  k, // location
132  ( i*j*k * .1 ), // d0
133  ( (i+j+k) * .3 ), // phi
134  ( (i+j+k) * .1 ), // omega
135  ( i*j*k * .1 ), // z0
136  ( (i+j+k) * .2 ), // tanlambda
137  covL,
138  refL
139  );
140  trk->addTrackState( trkstate ) ;
141  }
142 
143  TrackImpl* trkc = new TrackImpl(*trk) ;
144 
145  tracksWithMultipleStates->addElement( trk ) ;
146  tracksWithMultipleStatesCopies->addElement( trkc ) ;
147  }
148 
149  evt->addCollection( tracks , "Tracks") ;
150  evt->addCollection( tracksCopies , "TracksCopies") ;
151  evt->addCollection( tracksWithMultipleStates , "TracksWithMultipleStates") ;
152  evt->addCollection( tracksWithMultipleStatesCopies , "TracksWithMultipleStatesCopies") ;
153 
154  lcWrt->writeEvent(evt) ;
155 
156  delete evt ;
157  }
158 
159 
160  lcWrt->close() ;
161  delete lcWrt;
162 
163  MYTEST.LOG(" reading back Tracks from file " ) ;
164 
165  // create sio reader
166  LCReader* lcRdr = LCFactory::getInstance()->createLCReader() ;
167 
168  lcRdr->open( FILEN ) ;
169 
170  for( int i=0 ; i<NEVENT ; i++ ){
171 
172  //std::cout << " testing event " << i << std::endl ;
173 
174  LCEvent* evt = lcRdr->readNextEvent() ;
175 
176  MYTEST( evt->getRunNumber() , 4711 , " run number " ) ;
177 
178  MYTEST( evt->getEventNumber() , i , " event number " ) ;
179 
180  // LCCollection* tracks = evt->getCollection( "Tracks") ;
181 
182  // MYTEST.LOG(" reading back Tracks (one TrackState only) " ) ;
183 
184  // for( int j=0 ; j<NTRACKS ; j++ ){
185 
186  // //std::cout << " testing track " << j << std::endl ;
187 
188  // Track* trk = dynamic_cast<Track*>(tracks->getElementAt(j)) ;
189 
190  int j=0 ;
191 
192  LCIterator<Track> it( evt, "Tracks" ) ;
193  while( Track* trk = it.next() ) {
194 
195  MYTEST( trk->getOmega(), float( (i+j) * .1 ), "getOmega" ) ;
196  MYTEST( trk->getTanLambda(), float( (i+j) * .2 ), "getTanLambda" ) ;
197  MYTEST( trk->getPhi(), float( (i+j) * .3 ), "getPhi" ) ;
198  MYTEST( trk->getD0(), float( i * .1 ), "getD0" ) ;
199  MYTEST( trk->getZ0(), float( j * .1 ), "getZ0" ) ;
200 
201  MYTEST( trk->getChi2(), float(1.01) , "getChi2" ) ;
202  MYTEST( trk->getNdf(), 42 , "getNdf" ) ;
203  MYTEST( trk->getdEdx(), float(3.14159265) , "getdEdx" ) ;
204  MYTEST( trk->getdEdxError(), float(42.) , "getdEdxError" ) ;
205  MYTEST( trk->getRadiusOfInnermostHit(), float(3.14159265) , "getRadiusOfInnermostHit" ) ;
206 
207  const FloatVec& cov = trk->getCovMatrix() ;
208 
209  for( unsigned int k=0 ; k<cov.size() ; k++ ){
210  stringstream ss;
211  ss << " cov[" << k << "] " ;
212  MYTEST( cov[k] , float(k+1) , ss.str() ) ;
213  }
214  const float* ref = trk->getReferencePoint() ;
215 
216  for( unsigned int k=0 ; k<3 ; k++ ){
217  stringstream ss;
218  ss << " ref[" << k << "] " ;
219  MYTEST( ref[k] , float(k+1) , ss.str() ) ;
220  }
221 
222  ++j ;
223  }
224 
225  stringstream ssG;
226  ssG << " read back " << it->getNumberOfElements() << " tracks from collection " << "Tracks using the LCIterator<Track>.\n" ;
227  MYTEST.LOG( ssG.str() ) ;
228 
229 
230  j=0 ;
231 
232  LCIterator<Track> itc( evt, "TracksCopies" ) ;
233  while( Track* trk = itc.next() ) {
234 
235  MYTEST( trk->getOmega(), float( (i+j) * .1 ), "getOmega" ) ;
236  MYTEST( trk->getTanLambda(), float( (i+j) * .2 ), "getTanLambda" ) ;
237  MYTEST( trk->getPhi(), float( (i+j) * .3 ), "getPhi" ) ;
238  MYTEST( trk->getD0(), float( i * .1 ), "getD0" ) ;
239  MYTEST( trk->getZ0(), float( j * .1 ), "getZ0" ) ;
240 
241  MYTEST( trk->getChi2(), float(1.01) , "getChi2" ) ;
242  MYTEST( trk->getNdf(), 42 , "getNdf" ) ;
243  MYTEST( trk->getdEdx(), float(3.14159265) , "getdEdx" ) ;
244  MYTEST( trk->getdEdxError(), float(42.) , "getdEdxError" ) ;
245  MYTEST( trk->getRadiusOfInnermostHit(), float(3.14159265) , "getRadiusOfInnermostHit" ) ;
246 
247  const FloatVec& cov = trk->getCovMatrix() ;
248 
249  for( unsigned int k=0 ; k<cov.size() ; k++ ){
250  stringstream ss;
251  ss << " cov[" << k << "] " ;
252  MYTEST( cov[k] , float(k+1) , ss.str() ) ;
253  }
254  const float* ref = trk->getReferencePoint() ;
255 
256  for( unsigned int k=0 ; k<3 ; k++ ){
257  stringstream ss;
258  ss << " ref[" << k << "] " ;
259  MYTEST( ref[k] , float(k+1) , ss.str() ) ;
260  }
261 
262  ++j ;
263  }
264 
265  ssG.str("");
266  ssG << " read back " << it->getNumberOfElements() << " tracks from collection " << "TracksCopies using the LCIterator<Track>.\n" ;
267  MYTEST.LOG( ssG.str() ) ;
268 
269 
270 
271 
272  LCCollection* tracksWithMultipleStates = evt->getCollection( "TracksWithMultipleStates") ;
273  LCCollection* tracksWithMultipleStatesCopies = evt->getCollection( "TracksWithMultipleStatesCopies") ;
274 
275  MYTEST.LOG(" reading back TracksWithMultipleStates " ) ;
276 
277  for( j=0 ; j<NTRACKS ; j++ ){
278 
279  //std::cout << " testing track " << j << std::endl ;
280 
281  Track* trk = dynamic_cast<Track*>(tracksWithMultipleStates->getElementAt(j)) ;
282 
283  MYTEST( trk->getOmega(), float( (i+j) * .1 ), "getOmega" ) ;
284  MYTEST( trk->getTanLambda(), float( (i+j) * .2 ), "getTanLambda" ) ;
285  MYTEST( trk->getPhi(), float( (i+j) * .3 ), "getPhi" ) ;
286  MYTEST( trk->getD0(), float( i * .1 ), "getD0" ) ;
287  MYTEST( trk->getZ0(), float( j * .1 ), "getZ0" ) ;
288 
289  MYTEST( trk->getChi2(), float(1.01) , "getChi2" ) ;
290  MYTEST( trk->getNdf(), 42 , "getNdf" ) ;
291  MYTEST( trk->getdEdx(), float(3.14159265) , "getdEdx" ) ;
292  MYTEST( trk->getdEdxError(), float(42.) , "getdEdxError" ) ;
293  MYTEST( trk->getRadiusOfInnermostHit(), float(3.14159265) , "getRadiusOfInnermostHit" ) ;
294 
295  const FloatVec& cov = trk->getCovMatrix() ;
296 
297  for( unsigned int k=0 ; k<cov.size() ; k++ ){
298  stringstream ss;
299  ss << " cov[" << k << "] " ;
300  MYTEST( cov[k] , float(k+1) , ss.str() ) ;
301  }
302 
303  const float* ref = trk->getReferencePoint() ;
304 
305  for( unsigned int k=0 ; k<3 ; k++ ){
306  stringstream ss;
307  ss << " ref[" << k << "] " ;
308  MYTEST( ref[k] , float(k+1) , ss.str() ) ;
309  }
310 
311  // more than one trackstate
312  const TrackStateVec& trackstates = trk->getTrackStates() ;
313 
314  for( unsigned int k=1 ; k<trackstates.size() ; k++ ){
315 
316  //std::cout << " testing trackstate " << k << std::endl ;
317  //std::cout << " testing trackstate " << endl << header(trackstates[k]) << lcio_short<EVENT::TrackState>(trackstates[k]) << endl ;
318 
319  MYTEST( trackstates[k]->getOmega(), float( (i+j+k) * .1 ), "getOmega" ) ;
320  MYTEST( trackstates[k]->getTanLambda(), float( (i+j+k) * .2 ), "getTanLambda" ) ;
321  MYTEST( trackstates[k]->getPhi(), float( (i+j+k) * .3 ), "getPhi" ) ;
322  MYTEST( trackstates[k]->getD0(), float( i*j*k * .1 ), "getD0" ) ;
323  MYTEST( trackstates[k]->getZ0(), float( i*j*k * .1 ), "getZ0" ) ;
324 
325  const FloatVec& covM = trackstates[k]->getCovMatrix() ;
326 
327  for( unsigned int l=0 ; l<covM.size() ; l++ ){
328  stringstream ss;
329  ss << " cov[" << l << "] " ;
330  MYTEST( covM[l] , float(k+(l+1)) , ss.str() ) ;
331  }
332 
333  const float* refL = trackstates[k]->getReferencePoint() ;
334 
335  for( unsigned int l=0 ; l<3 ; l++ ){
336  stringstream ss;
337  ss << " ref[" << l << "] " ;
338  MYTEST( refL[l] , float(k*(l+1)) , ss.str() ) ;
339  }
340  }
341 
342 
343 
344  // ---------- test copies -------------------------------
345  Track* trkc = dynamic_cast<Track*>(tracksWithMultipleStatesCopies->getElementAt(j)) ;
346  MYTEST( trkc->getOmega(), float( (i+j) * .1 ), "getOmega" ) ;
347  MYTEST( trkc->getTanLambda(), float( (i+j) * .2 ), "getTanLambda" ) ;
348  MYTEST( trkc->getPhi(), float( (i+j) * .3 ), "getPhi" ) ;
349  MYTEST( trkc->getD0(), float( i * .1 ), "getD0" ) ;
350  MYTEST( trkc->getZ0(), float( j * .1 ), "getZ0" ) ;
351 
352  MYTEST( trkc->getChi2(), float(1.01) , "getChi2" ) ;
353  MYTEST( trkc->getNdf(), 42 , "getNdf" ) ;
354  MYTEST( trkc->getdEdx(), float(3.14159265) , "getdEdx" ) ;
355  MYTEST( trkc->getdEdxError(), float(42.) , "getdEdxError" ) ;
356  MYTEST( trkc->getRadiusOfInnermostHit(), float(3.14159265) , "getRadiusOfInnermostHit" ) ;
357 
358  const FloatVec& covc = trkc->getCovMatrix() ;
359 
360  for( unsigned int k=0 ; k<covc.size() ; k++ ){
361  stringstream ss;
362  ss << " cov[" << k << "] " ;
363  MYTEST( covc[k] , float(k+1) , ss.str() ) ;
364  }
365 
366  ref = trkc->getReferencePoint() ;
367 
368  for( unsigned int k=0 ; k<3 ; k++ ){
369  stringstream ss;
370  ss << " ref[" << k << "] " ;
371  MYTEST( ref[k] , float(k+1) , ss.str() ) ;
372  }
373 
374  // more than one trackstate
375  const TrackStateVec& trackstatesc = trkc->getTrackStates() ;
376 
377  for( unsigned int k=1 ; k<trackstatesc.size() ; k++ ){
378 
379  MYTEST( trackstatesc[k]->getOmega(), float( (i+j+k) * .1 ), "getOmega" ) ;
380  MYTEST( trackstatesc[k]->getTanLambda(), float( (i+j+k) * .2 ), "getTanLambda" ) ;
381  MYTEST( trackstatesc[k]->getPhi(), float( (i+j+k) * .3 ), "getPhi" ) ;
382  MYTEST( trackstatesc[k]->getD0(), float( i*j*k * .1 ), "getD0" ) ;
383  MYTEST( trackstatesc[k]->getZ0(), float( i*j*k * .1 ), "getZ0" ) ;
384 
385  const FloatVec& covM = trackstatesc[k]->getCovMatrix() ;
386 
387  for( unsigned int l=0 ; l<covM.size() ; l++ ){
388  stringstream ss;
389  ss << " cov[" << l << "] " ;
390  MYTEST( covM[l] , float(k+(l+1)) , ss.str() ) ;
391  }
392 
393  const float* refL = trackstatesc[k]->getReferencePoint() ;
394 
395  for( unsigned int l=0 ; l<3 ; l++ ){
396  stringstream ss;
397  ss << " ref[" << l << "] " ;
398  MYTEST( refL[l] , float(k*(l+1)) , ss.str() ) ;
399  }
400  }
401 
402  // ---------- end test copies -------------------------------
403 
404 
405 
406  // Test getClosestTrackState( float x, float y, float z )
407  const TrackState* trackstate = trk->getClosestTrackState( 0, 0, 0 ) ;
408  assert( trackstate != NULL );
409  ref = trackstate->getReferencePoint() ;
410 
411  //std::cout << " closest trackstate " << endl << header(trackstate) << lcio_short<EVENT::TrackState>(trackstate) << endl ;
412 
413  for( unsigned int k=0 ; k<3 ; k++ ){
414  stringstream ss;
415  ss << " closest trackstate ref[" << k << "] " ;
416  MYTEST( ref[k] , float(k+1) , ss.str() ) ;
417  }
418 
419  // Test getTrackState( int location )
420  trackstate = trk->getTrackState( TrackState::AtIP ) ;
421  assert( trackstate != NULL );
422  ref = trackstate->getReferencePoint() ;
423 
424  //std::cout << " trackstate AtIP" << endl << header(trackstate) << lcio_short<EVENT::TrackState>(trackstate) << endl ;
425 
426  for( unsigned int k=0 ; k<3 ; k++ ){
427  stringstream ss;
428  ss << " trackstate AtIP ref[" << k << "] " ;
429  MYTEST( ref[k] , float(k+1) , ss.str() ) ;
430  }
431  }
432 
433  }
434  lcRdr->close() ;
435  delete lcRdr;
436 
437 
438  } catch( Exception &e ){
439  MYTEST.FAILED( e.what() );
440  }
441 
442  return 0;
443  }
444 
445  //=============================================================================
446 
std::vector< float > FloatVec
Vector of floats.
Definition: LCIOSTLTypes.h:18
static const string testname
Definition: test_tracks.cc:32
Definition: tutil.h:7
std::vector< TrackState * > TrackStateVec
Vector of (pointers to) TrackStates.
Definition: TrackState.h:17
static const int NEVENT
Definition: test_tracks.cc:26
void FAILED(const std::string &msg)
Definition: tutil.h:42
int main(int argc, char **argv)
Simple program that opens existing LCIO files and appends the records needed for direct access - if t...
T str(T...args)
T ref(T...args)
void LOG(const std::string &msg)
Definition: tutil.h:21
static string FILEN
Definition: test_tracks.cc:29
static const int NTRACKS
Definition: test_tracks.cc:27