LCIO  02.17
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
test_calohit.cc
Go to the documentation of this file.
1 // test lcio::CalorimeterHit
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"
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 NHITS = 1000 ; // calorimeter hits per event
24 
25 static string FILEN = "calohit.slcio" ;
26 
27 // replace mytest with the name of your test
28 const static string testname="test_calohit";
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 CalorimeterHits " );
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* calHits = new LCCollectionVec( LCIO::CALORIMETERHIT ) ;
58  LCCollectionVec* calHitsErr = new LCCollectionVec( LCIO::CALORIMETERHIT ) ;
59  LCFlagImpl calFlag( calHits->getFlag() ) ;
60  calFlag.setBit( LCIO::RCHBIT_LONG ) ;
61  calHits->setFlag( calFlag.getFlag() ) ;
62 
63  calFlag.setBit( LCIO::RCHBIT_ENERGY_ERROR ) ;
64  calHitsErr->setFlag( calFlag.getFlag() ) ;
65 
66  for(int j=0;j<NHITS;j++){
67  CalorimeterHitImpl* calHit = new CalorimeterHitImpl ;
68  calHit->setEnergy( i*j*117. ) ;
69  calHit->setCellID0( i+100000 + j ) ;
70  float pos[3] = { float(i) , float(j) ,float(i*j) } ;
71  calHit->setPosition( pos ) ;
72  calHits->addElement( calHit ) ;
73  }
74  for(int j=0;j<NHITS;j++){
75  CalorimeterHitImpl* calHit = new CalorimeterHitImpl ;
76  calHit->setEnergy( i*j*117. ) ;
77  calHit->setEnergyError( i*j*0.117 ) ;
78  calHit->setCellID0( i+100000 + j ) ;
79  float pos[3] = { float(i) , float(j) ,float(i*j) } ;
80  calHit->setPosition( pos ) ;
81  calHitsErr->addElement( calHit ) ;
82  }
83  evt->addCollection( calHits , "CalorimeterHits") ;
84  evt->addCollection( calHitsErr , "CalorimeterHitsWithEnergyError") ;
85 
86  lcWrt->writeEvent(evt) ;
87 
88  delete evt ;
89  }
90 
91 
92  lcWrt->close() ;
93 
94  MYTEST.LOG(" reading back CalorimeterHits from file " ) ;
95 
96  // create sio reader
97  LCReader* lcRdr = LCFactory::getInstance()->createLCReader() ;
98 
99  lcRdr->open( FILEN ) ;
100 
101  for(int i=0;i<NEVENT;i++){
102 
103  //std::cout << " testing event " << i << std::endl ;
104 
105  LCEvent* evt = lcRdr->readNextEvent() ;
106 
107  MYTEST( evt->getRunNumber() , 4711 , " run number " ) ;
108 
109  MYTEST( evt->getEventNumber() , i , " event number " ) ;
110 
111  LCCollection* calHits = evt->getCollection( "CalorimeterHits") ;
112  LCCollection* calHitsErr = evt->getCollection( "CalorimeterHitsWithEnergyError") ;
113 
114  for(int j=0;j<NHITS;j++) {
115 
116  //std::cout << " testing hit " << j << std::endl ;
117 
118  CalorimeterHit* calHit = dynamic_cast<CalorimeterHit*>(calHits->getElementAt(j)) ;
119 
120  MYTEST( calHit->getEnergy() , i*j*117. , "energy" ) ;
121  MYTEST( calHit->getCellID0() , i+100000 + j , " cellid0 " ) ;
122 
123  const float* pos = calHit->getPosition() ;
124 
125  MYTEST( pos[0] , i , " pos[0] " ) ;
126  MYTEST( pos[1] , j , " pos[1] " ) ;
127  MYTEST( pos[2] , i*j , " pos[2] " ) ;
128 
129  }
130  for(int j=0;j<NHITS;j++) {
131 
132  //std::cout << " testing hit " << j << std::endl ;
133 
134  CalorimeterHit* calHit = dynamic_cast<CalorimeterHit*>(calHitsErr->getElementAt(j)) ;
135 
136  MYTEST( calHit->getEnergy() , i*j*117. , "energy" ) ;
137  MYTEST( calHit->getEnergyError() , float(i*j*0.117) , "energy error" ) ;
138  MYTEST( calHit->getCellID0() , i+100000 + j , " cellid0 " ) ;
139 
140  const float* pos = calHit->getPosition() ;
141 
142  MYTEST( pos[0] , i , " pos[0] " ) ;
143  MYTEST( pos[1] , j , " pos[1] " ) ;
144  MYTEST( pos[2] , i*j , " pos[2] " ) ;
145 
146  }
147  }
148  lcRdr->close() ;
149 
150 
151  } catch( Exception &e ){
152  MYTEST.FAILED( e.what() );
153  }
154 
155  return 0;
156 }
157 
158 //=============================================================================
159 
static string FILEN
Definition: test_calohit.cc:25
static const int NHITS
Definition: test_calohit.cc:23
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...
void LOG(const std::string &msg)
Definition: tutil.h:21
static const int NEVENT
Definition: test_calohit.cc:22
static const string testname
Definition: test_calohit.cc:28