LCIO  02.17
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
test_trackerpulse.cc
Go to the documentation of this file.
1 // test lcio::TrackerPulse
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/TrackerPulseImpl.h"
14 #include "IMPL/LCFlagImpl.h"
15 
16 //#include <iostream>
17 
18 using namespace std ;
19 using namespace lcio ;
20 
21 //static const int NRUN = 10 ;
22 static const int NEVENT = 10 ; // events
23 static const int NPULSES = 1000 ; // tracker pulses per event
24 
25 static string FILEN = "trackerpulses.slcio" ;
26 
27 // replace mytest with the name of your test
28 const static string testname="test_trackerpulse";
29 
30 //=============================================================================
31 
32 int main(int /*argc*/, char** /*argv*/ ){
33 
34  // this should be the first line in your test
35  TEST MYTEST=TEST( testname, std::cout );
36 
37  try{
38 
39 
40  MYTEST.LOG( " writing TrackerPulses " );
41 
42  // create sio writer
43  LCWriter* lcWrt = LCFactory::getInstance()->createLCWriter() ;
44 
45  lcWrt->open( FILEN , LCIO::WRITE_NEW ) ;
46 
47  // EventLoop - create some events and write them to the file
48  for(int i=0;i<NEVENT;i++){
49 
50  // we need to use the implementation classes here
51  LCEventImpl* evt = new LCEventImpl() ;
52 
53 
54  evt->setRunNumber( 4711 ) ;
55  evt->setEventNumber( i ) ;
56 
57  LCCollectionVec* trkPulses = new LCCollectionVec( LCIO::TRACKERPULSE ) ;
58  LCCollectionVec* trkPulsesCov = new LCCollectionVec( LCIO::TRACKERPULSE ) ;
59 
60  LCFlagImpl flag( trkPulsesCov->getFlag() ) ;
61  flag.setBit( LCIO::TRAWBIT_CM ) ;
62  trkPulsesCov->setFlag( flag.getFlag() ) ;
63 
64 
65  for(int j=0;j<NPULSES;j++){
66  TrackerPulseImpl* trkPulse = new TrackerPulseImpl ;
67  trkPulse->setTime( 3.1415 + 0.1 * i ) ;
68  //trkPulse->setTimeError( (i+j) * .003 ) ;
69  trkPulse->setCharge( 3.1415 + 0.1 * j ) ;
70  //trkPulse->setChargeError( (i+j)*.005 ) ;
71 
72  trkPulses->addElement( trkPulse ) ;
73  }
74  for(int j=0;j<NPULSES;j++){
75  TrackerPulseImpl* trkPulse = new TrackerPulseImpl ;
76  trkPulse->setTime( 3.1415 + 0.1 * i ) ;
77  //trkPulse->setTimeError( (i+j) * .003 ) ;
78  trkPulse->setCharge( 3.1415 + 0.1 * j ) ;
79  //trkPulse->setChargeError( (i+j)*.005 ) ;
80 
81  float cov[3] = { float(i) , float(j) ,float(i*j) } ;
82  trkPulse->setCovMatrix( cov );
83 
84  trkPulsesCov->addElement( trkPulse ) ;
85  }
86 
87  evt->addCollection( trkPulses , "TrackerPulses") ;
88  evt->addCollection( trkPulsesCov , "TrackerPulsesWithCovMatrix") ;
89 
90  lcWrt->writeEvent(evt) ;
91 
92  delete evt ;
93  }
94 
95 
96  lcWrt->close() ;
97 
98  MYTEST.LOG(" reading back TrackerPulses from file " ) ;
99 
100  // create sio reader
101  LCReader* lcRdr = LCFactory::getInstance()->createLCReader() ;
102 
103  lcRdr->open( FILEN ) ;
104 
105  for(int i=0;i<NEVENT;i++){
106 
107  //std::cout << " testing event " << i << std::endl ;
108 
109  LCEvent* evt = lcRdr->readNextEvent() ;
110 
111  MYTEST( evt->getRunNumber() , 4711 , " run number " ) ;
112 
113  MYTEST( evt->getEventNumber() , i , " event number " ) ;
114 
115  LCCollection* trkPulses = evt->getCollection( "TrackerPulses") ;
116 
117  //MYTEST.LOG(" reading back TrackerPulses from file " ) ;
118 
119  for(int j=0;j<NPULSES;j++) {
120 
121  //std::cout << " testing pulse " << j << std::endl ;
122 
123  TrackerPulse* trkPulse = dynamic_cast<TrackerPulse*>(trkPulses->getElementAt(j)) ;
124 
125  MYTEST( trkPulse->getTime(), float( 3.1415 + 0.1 * i ), "time" ) ;
126  //MYTEST( trkPulse->getTimeError(), float( (i + j) * .003 ), "time error" ) ;
127  MYTEST( trkPulse->getCharge(), float( 3.1415 + 0.1 * j ), "charge" ) ;
128  //MYTEST( trkPulse->getChargeError(), float( (i + j) * .005 ), "charge error" ) ;
129 
130 
131  const FloatVec& cov = trkPulse->getCovMatrix() ;
132 
133  // should be initialized to 0
134  MYTEST( cov[0] , 0 , " cov[0] " ) ;
135  MYTEST( cov[1] , 0 , " cov[1] " ) ;
136  MYTEST( cov[2] , 0 , " cov[2] " ) ;
137 
138  }
139 
140  LCCollection* trkPulsesCov = evt->getCollection( "TrackerPulsesWithCovMatrix") ;
141 
142  //MYTEST.LOG(" reading back TrackerPulsesWithCovMatrix from file " ) ;
143 
144  for(int j=0;j<NPULSES;j++) {
145 
146  //std::cout << " testing pulse " << j << std::endl ;
147 
148  TrackerPulse* trkPulse = dynamic_cast<TrackerPulse*>(trkPulsesCov->getElementAt(j)) ;
149 
150  MYTEST( trkPulse->getTime(), float( 3.1415 + 0.1 * i ), "time" ) ;
151  //MYTEST( trkPulse->getTimeError(), float( (i + j) * .003 ), "time error" ) ;
152  MYTEST( trkPulse->getCharge(), float( 3.1415 + 0.1 * j ), "charge" ) ;
153  //MYTEST( trkPulse->getChargeError(), float( (i + j) * .005 ), "charge error" ) ;
154  const FloatVec& cov = trkPulse->getCovMatrix() ;
155 
156  MYTEST( cov[0] , i , " cov[0] " ) ;
157  MYTEST( cov[1] , j , " cov[1] " ) ;
158  MYTEST( cov[2] , i*j , " cov[2] " ) ;
159  }
160 
161  }
162  lcRdr->close() ;
163 
164 
165  } catch( Exception &e ){
166  MYTEST.FAILED( e.what() );
167  }
168 
169  return 0;
170 }
171 
172 //=============================================================================
173 
std::vector< float > FloatVec
Vector of floats.
Definition: LCIOSTLTypes.h:18
static const int NPULSES
Definition: tutil.h:7
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...
static const int NEVENT
static const string testname
void LOG(const std::string &msg)
Definition: tutil.h:21
static string FILEN