LCIO  02.17
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
test_trackerhit.cc
Go to the documentation of this file.
1 // test lcio::TrackerHit
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/TrackerHitImpl.h"
14 #include "IMPL/LCFlagImpl.h"
15 //#include "UTIL/Operators.h"
16 
17 #include "UTIL/CellIDEncoder.h"
18 #include "UTIL/CellIDDecoder.h"
19 #include "UTIL/ILDConf.h"
20 #include "UTIL/LCTrackerConf.h"
21 
22 //#include <iostream>
23 
24 using namespace std ;
25 using namespace lcio ;
26 
27 //static const int NRUN = 10 ;
28 static const int NEVENT = 10 ; // events
29 static const int NHITS = 1000 ; // tracker hits per event
30 
31 static string FILEN = "trackerhits.slcio" ;
32 
33 // replace mytest with the name of your test
34 const static string testname="test_trackerhit";
35 
36 //=============================================================================
37 
38 int main(int /*argc*/, char** /*argv*/ ){
39 
40  // this should be the first line in your test
41  TEST MYTEST=TEST( testname, std::cout );
42 
43  try{
44 
45 
46  MYTEST.LOG( " writing TrackerHits " );
47 
48  // create sio writer
49  LCWriter* lcWrt = LCFactory::getInstance()->createLCWriter() ;
50 
51  lcWrt->open( FILEN , LCIO::WRITE_NEW ) ;
52 
53  // EventLoop - create some events and write them to the file
54  for(int i=0;i<NEVENT;i++){
55 
56  // we need to use the implementation classes here
57  LCEventImpl* evt = new LCEventImpl() ;
58 
59 
60  evt->setRunNumber( 4711 ) ;
61  evt->setEventNumber( i ) ;
62 
63  LCCollectionVec* trkHits = new LCCollectionVec( LCIO::TRACKERHIT ) ;
64 
65  ILDCellIDEncoder<TrackerHitImpl> idEnc( "readoutUnit:8,daqChannel:16," , trkHits ) ;
66  // this is effectively the same as:
67  // CellIDEncoder<TrackerHitImpl> idEnc( LCTrackerCellID::encoding_string() + ",readoutUnit:8,daqChannel:16" , trkHits ) ;
68 
69 
70 
71  for(int j=0;j<NHITS;j++){
72 
73  TrackerHitImpl* trkHit = new TrackerHitImpl ;
74 
75  idEnc.reset() ;
76 
77  idEnc[ LCTrackerCellID::subdet() ] = ILDDetID::FTD ;
78  idEnc[ LCTrackerCellID::layer() ] = j % 100 ;
79  idEnc[ LCTrackerCellID::side() ] = ILDDetID::bwd ;
80  idEnc[ LCTrackerCellID::module() ] = j / 100 + 1 ;
81  idEnc[ LCTrackerCellID::sensor() ] = j % 4 ;
82  idEnc["daqChannel"] = j*8 ;
83 
84  idEnc.setCellID( trkHit ) ;
85 
86  trkHit->setEDep( i*j*117. ) ;
87  // trkHit->setdEdx( i*j*117. ) ;
88  trkHit->setEDepError( (i+j)*.3 ) ;
89  double pos[3] = { 1.*i, 1.*j, 1.*i*j } ;
90  trkHit->setPosition( pos ) ;
91 
92  float cov[TRKHITNCOVMATRIX] = { float(i), float(j), float(i+j) , float(2*i), float(2.*j), float(2.*(i+j) ) } ;
93  trkHit->setCovMatrix( cov );
94 
95  trkHits->addElement( trkHit ) ;
96  }
97  evt->addCollection( trkHits , "TrackerHits") ;
98 
99  lcWrt->writeEvent(evt) ;
100 
101  delete evt ;
102  }
103 
104 
105  lcWrt->close() ;
106 
107  MYTEST.LOG(" reading back TrackerHits from file " ) ;
108 
109  // create sio reader
110  LCReader* lcRdr = LCFactory::getInstance()->createLCReader() ;
111 
112  lcRdr->open( FILEN ) ;
113 
114  for(int i=0;i<NEVENT;i++){
115 
116  //std::cout << " testing event " << i << std::endl ;
117 
118  LCEvent* evt = lcRdr->readNextEvent() ;
119 
120  MYTEST( evt->getRunNumber() , 4711 , " run number " ) ;
121 
122  MYTEST( evt->getEventNumber() , i , " event number " ) ;
123 
124  LCCollection* trkHits = evt->getCollection( "TrackerHits") ;
125 
126  CellIDDecoder<TrackerHit> idDec( trkHits ) ;
127 
128  for(int j=0;j<NHITS;j++) {
129 
130  //std::cout << " testing hit " << j << std::endl ;
131 
132  TrackerHit* trkHit = dynamic_cast<TrackerHit*>(trkHits->getElementAt(j)) ;
133 
134 
135  MYTEST( idDec(trkHit)[ LCTrackerCellID::subdet() ] , ILDDetID::FTD , " cellID(trkHit) == ( ILDDetID::FTD ) " ) ;
136  MYTEST( idDec(trkHit)[ LCTrackerCellID::layer() ] , j % 100 , " cellID(trkHit) == ( j % 100 ) " ) ;
137  MYTEST( idDec(trkHit)[ LCTrackerCellID::side() ] , ILDDetID::bwd , " cellID(trkHit) == ( ILDDetID::bwd ) " ) ;
138  MYTEST( idDec(trkHit)[ LCTrackerCellID::module() ] , j / 100 + 1 , " cellID(trkHit) == ( j / 100 + 1 ) " ) ;
139  MYTEST( idDec(trkHit)[ LCTrackerCellID::sensor() ] , j % 4 , " cellID(trkHit) == ( j % 4 ) " ) ;
140  MYTEST( idDec(trkHit)["daqChannel"] , j*8 , " cellID(trkHit) == ( j*8 ) " ) ;
141 
142 
143  //std::cout << *trkHit << std::endl ;
144 
145  MYTEST( trkHit->getEDep() , i*j*117. , "EDep" ) ;
146 
147  // MYTEST( trkHit->getdEdx() , i*j*117. , "dEdx" ) ;
148  // remove float converstion and check what happens ;)
149  MYTEST( trkHit->getEDepError() , float((i+j)*.3) , "EDepError" ) ;
150  //MYTEST( trkHit->getEDepError() , (i+j)*.3 , "EDepError" ) ;
151 
152  const double* pos = trkHit->getPosition() ;
153 
154  MYTEST( pos[0] , i , " pos[0] " ) ;
155  MYTEST( pos[1] , j , " pos[1] " ) ;
156  MYTEST( pos[2] , i*j , " pos[2] " ) ;
157 
158 
159  const FloatVec& cov = trkHit->getCovMatrix() ;
160 
161  MYTEST( cov[0] , i , " cov[0] " ) ;
162  MYTEST( cov[1] , j , " cov[1] " ) ;
163  MYTEST( cov[2] , i+j , " cov[2] " ) ;
164 
165  }
166  }
167  lcRdr->close() ;
168 
169 
170  } catch( Exception &e ){
171  MYTEST.FAILED( e.what() );
172  }
173 
174  return 0;
175 }
176 
177 //=============================================================================
178 
static const string testname
#define TRKHITNCOVMATRIX
std::vector< float > FloatVec
Vector of floats.
Definition: LCIOSTLTypes.h:18
static const int NHITS
Definition: tutil.h:7
static const int NEVENT
void FAILED(const std::string &msg)
Definition: tutil.h:42
static string FILEN
int main(int argc, char **argv)
Simple program that opens existing LCIO files and appends the records needed for direct access - if t...
void LOG(const std::string &msg)
Definition: tutil.h:21