LCIO  02.17
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
test_RTextension.cc
Go to the documentation of this file.
1 // RTextension for LCIO tests
4 
5 #include "tutil.h"
6 #include "lcio.h"
7 
8 #include "EVENT/LCIO.h"
9 #include "IMPL/MCParticleImpl.h"
11 
12 
13 #include <iostream>
14 
15 using namespace std ;
16 using namespace lcio ;
17 
18 // replace mytest with the name of your test
19 const static string testname="testRTextension";
20 
21 //=============================================================================
22 
23 struct SomeClass{
24  // define a type for a simple int extension
25  // ( inside a class for testing ... )
26  struct Index : LCIntExtension<Index> {} ;
27 
28  double someDouble ;
29  int someInt ;
30  bool someBool ;
31 };
32 
33 
34 struct UserStruct : LCOwnedExtension<UserStruct,SomeClass> {} ;
35 
36 //a simple float extension
37 struct Mass : LCFloatExtension<Mass> {} ;
38 
39 
40 // a vector of strings (pointers) that are owned by the object that is extended
41 struct ParticleIDs : LCOwnedExtensionVector< ParticleIDs,std::string >{} ;
42 
43 
44 // a many to many relationship between MCParticles and Reconstructed Particles
45 struct MCTruth2Rec : LCNToNRelation< MCTruth2Rec, MCParticle, ReconstructedParticle> {} ;
46 
47 
48 //=============================================================================
49 
50 int main(int /*argc*/, char** /*argv*/ ){
51 
52  // this should be the first line in your test
53  TEST MYTEST=TEST( testname, std::cout );
54 
55 
56  MCParticle* mcp = new MCParticleImpl ;
57  ReconstructedParticleImpl* rcp = new ReconstructedParticleImpl ;
58 
59  MYTEST.LOG( " test to add int, float and stringVec extension to MCParticle ..." );
60 
61  mcp->ext<SomeClass::Index>() = 42 ;
62 
63  MYTEST( mcp->ext<SomeClass::Index>() , 42, " ext<SomeClass::Index> == 42 " ) ;
64 
65  rcp->ext<SomeClass::Index>() = 42 ;
66 
67  MYTEST( rcp->ext<SomeClass::Index>() , 42, " ext<SomeClass::Index> == 42 " ) ;
68 
69  mcp->ext<Mass>() = 42.12345f ;
70 
71  MYTEST( mcp->ext<Mass>() , 42.12345f, " ext<Mass> == 42.12345f " ) ;
72 
73 
74  mcp->ext<ParticleIDs>()->push_back( new std::string("Pion") ) ;
75  mcp->ext<ParticleIDs>()->push_back( new std::string("Muon") ) ;
76  mcp->ext<ParticleIDs>()->push_back( new std::string("Tau") ) ;
77 
78  std::string pid1 = *(*mcp->ext<ParticleIDs>())[1] ;
79 
80  MYTEST( pid1 , "Muon" , " *(*mcp->ext<ParticleIDs>())[1] == \"Muon\" " ) ;
81 
82  mcp->ext<SomeClass::Index>() = 0 ;
83  MYTEST( mcp->ext<SomeClass::Index>() , 0, " ext<SomeClass::Index> == 0 " ) ;
84 
85 
86  MYTEST.LOG( " test owned extension with user class ..." ) ;
87 
88  MYTEST( mcp->ext<UserStruct>() , (void*) 0 , " ext<SomeClass> == 0 " ) ; // pointer is not yet initiallized
89 
90  mcp->ext<UserStruct>() = new SomeClass ; // add a new user object
91 
92  mcp->ext<UserStruct>()->someDouble = 3.141592 ;
93 
94  MYTEST( mcp->ext<UserStruct>()->someDouble, 3.141592 , " mcp->ext<UserStruct>()->someDouble == 3.141592 " );
95 
96 
97  MYTEST.LOG( " test relations ... " ) ;
98 
99  add_relation<MCTruth2Rec>( mcp , rcp ) ;
100  add_relation<MCTruth2Rec>( mcp , rcp ) ;
101  add_relation<MCTruth2Rec>( mcp , rcp ) ;
102 
103  MYTEST( mcp->rel<MCTruth2Rec::to>()->size() , unsigned( 3 ) , " mcp->rel<MCTruth2Rec::to>()->size() == 3 " ) ;
104 
105  MYTEST( rcp->rel<MCTruth2Rec::from>()->size() , unsigned( 3 ) , " rcp->rel<MCTruth2Rec::from>()->size() == 3 " ) ;
106 
107 
108  for( MCTruth2Rec::to::const_iterator it = mcp->rel<MCTruth2Rec::to>()->begin() ;
109  it != mcp->rel<MCTruth2Rec::to>()->end() ; ++it ) {
110 
111  MYTEST( *it , rcp , " mcp->rel<MCTruth2Rec::to>() == rcp " ) ;
112 
113  }
114 
115  for( MCTruth2Rec::from::const_iterator it = rcp->rel<MCTruth2Rec::from>()->begin() ;
116  it != rcp->rel<MCTruth2Rec::from>()->end() ; ++it ) {
117 
118  MYTEST( *it , mcp , " rcp->rel<MCTruth2Rec::from>() == mcp " ) ;
119 
120  }
121 
122 
123  //----------------------------------------------------------
124  //if( true ){
125  if( false ){
126  // force test program to fail in this way:
127  MYTEST.FAILED( "oops, something went wrong..." );
128  }
129 
130 
131  return 0;
132 }
133 
134 //=============================================================================
135 
double someDouble
T end(T...args)
Definition: tutil.h:7
static const string testname
STL class.
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