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