LCIO  02.17
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
lcrtrelation.cc
Go to the documentation of this file.
1 #include "lcio.h"
2 
3 #include "IO/LCReader.h"
4 #include "IMPL/LCTOOLS.h"
5 #include "EVENT/LCCollection.h"
6 #include "EVENT/Track.h"
7 #include "EVENT/Cluster.h"
8 
9 using namespace std ;
10 using namespace lcio ;
11 
12 
13 
14 // ------ define some structs for extensions and relations:-----------
15 
16 // NB: the first template argument of the parent class has to be the class itself !
17 
18 // a simple int extension
19 struct Index : LCIntExtension<Index> {} ;
20 
21 // a simple float extension
22 struct Mass : LCFloatExtension<Mass> {} ;
23 
24 // a vector of strings (pointers) that are owned by the object that is extended
25 struct ParticleIDs : LCOwnedExtensionVector<ParticleIDs,std::string> {};
26 
27 
28 // note extensions can be attched to any LCObject and be of any type, i.e. in particular
29 // of user defined types:
30 struct UserClass{
31  int someInt ;
32  float aFloat ;
33 };
34 struct MyUserExtension : LCOwnedExtension<MyUserExtension,UserClass> {} ;
35 
36 
37 // relations have to specify the type of the objects from and to which they relate:
38 
39 // example: a one to many relationship between tracks and clusters:
40 struct TrkCluLink : LC1ToNRelation<TrkCluLink,Track,Cluster> {} ;
41 
42 
43 // a many to many relationship between MCParticles
44 struct ParentDaughter : LCNToNRelation<ParentDaughter,MCParticle,MCParticle> {} ;
45 
46 
47 //-----------------------------------------------------------------------------------------
48 
53 int main(int /*argc*/, char** /*argv*/ ){
54 
55 
56  LCReader* lcReader = LCFactory::getInstance()->createLCReader() ;
57 
58  lcReader->open( "recjob.slcio" ) ;
59 
60  LCEvent* evt ;
61  int nEvents = 0 ;
62 
63 
64  //----------- the event loop -----------
65  while( (evt = lcReader->readNextEvent()) != 0 && nEvents < 1 ) {
66 
67 
68  LCCollection* mcpcol = evt->getCollection("MCParticle" ) ;
69 
70  int nmcp = mcpcol->getNumberOfElements() ;
71 
72  for(int i=0 ; i< nmcp ; i++ ){
73 
74  MCParticle* mcp = dynamic_cast<MCParticle*>( mcpcol->getElementAt(i) ) ;
75 
76 
77  // ints can be assigned directly - w/o pointer !
78  // make every particle know it's index in the collection
79  mcp->ext<Index>() = i ;
80 
81  mcp->ext<Mass>() = mcp->getMass() ;
82 
83  // assign a user object to each particle:
84 
85  mcp->ext<MyUserExtension>() = new UserClass ;
86 
87  mcp->ext<MyUserExtension>()->someInt = i*42 ;
88  mcp->ext<MyUserExtension>()->aFloat = i*3.1415 ;
89 
90  // assign some pid strings - note ownership is taken by particle
91 
92  if( ! (i % 2) )
93 
94  mcp->ext<ParticleIDs>()->push_back( new std::string("charged") ) ;
95 
96  else
97 
98  mcp->ext<ParticleIDs>()->push_back( new std::string("neutral") ) ;
99 
100  if( ! (i % 3) )
101 
102  mcp->ext<ParticleIDs>()->push_back( new std::string("hadron") ) ;
103 
104  else if( ! ((i+1) % 3) )
105 
106  mcp->ext<ParticleIDs>()->push_back( new std::string("photon") ) ;
107 
108  else if( ! ((i+2) % 3) )
109 
110  mcp->ext<ParticleIDs>()->push_back( new std::string("electron") ) ;
111 
112 
113  // copy the parent - daughter relationship:
114 
115  const MCParticleVec& daughters = mcp->getDaughters() ;
116 
117  for(unsigned j=0 ; j< daughters.size() ; j++ ){
118 
119  add_relation<ParentDaughter>( mcp, daughters[j] ) ;
120  }
121 
122 
123  }
124 
125 
126  LCCollection* trkcol = evt->getCollection("SomeTracks" ) ;
127  LCCollection* clucol = evt->getCollection("SomeClusters" ) ;
128 
129  if( trkcol && clucol ) {
130 
131  int nclu = clucol->getNumberOfElements() ;
132  int ntrk = trkcol->getNumberOfElements() ;
133 
134 
135  for(int j=0 ; j< ntrk ; j++ ){
136 
137  Track* trk = dynamic_cast<Track*> ( trkcol->getElementAt(j) ) ;
138 
139  // ints can be assigned directly - w/o pointer !
140  // make every track know it's index in the collection
141  trk->ext<Index>() = j ;
142 
143  for(int k=0 ; k< nclu ; k++ ){
144 
145  Cluster* clu = dynamic_cast<Cluster*> ( clucol->getElementAt(k) ) ;
146 
147  // make every cluster know it's index in the collection
148  if( j == 0 )
149  clu->ext<Index>() = k ;
150 
151  if( j % 2 && ( k == j || k == j-1 ) )
152 
153  add_relation<TrkCluLink>( trk ,clu );
154  }
155  }
156 
157 
158  // --- now print the relations:
159 
160  std::cout << " ---- tracks assigned to clusters: " << std::endl ;
161  for(int j=0 ; j< ntrk ; j++ ){
162 
163 
164  Track* trk = dynamic_cast<Track*> ( trkcol->getElementAt(j) ) ;
165 
166  std::cout << " track " << trk->ext<Index>() << " assigned to clusters : " ;
167 
168  TrkCluLink::to::rel_type clulist = trk->rel<TrkCluLink::to>() ;
169 
170  for( TrkCluLink::to::const_iterator iclu = clulist->begin() ;
171  iclu != clulist->end() ; ++iclu ){
172 
173  Cluster* clu = *iclu ; // iterator is of type pointer to container element
174 
175  std::cout << clu->ext<Index>() << ", " ;
176  }
177  std::cout << std::endl ;
178  }
179 
180  std::cout << " ----- now the inverse relation : " << std::endl ;
181 
182  for(int k=0 ; k< nclu ; k++ ){
183 
184  Cluster* clu = dynamic_cast<Cluster*> ( clucol->getElementAt(k) ) ;
185 
186 
187  Track* trk = clu->rel<TrkCluLink::from>() ;
188 
189  std::cout << " cluster "
190  << clu->ext<Index>() << " assigned from track: " ;
191  if( trk != 0 )
192  std::cout << trk->ext<Index>() << std::endl ;
193  else
194  std::cout << " none " << std::endl ;
195  }
196 
197  std::cout << std::endl ;
198 
199 
200  // print MCParticle extensions and relations:
201 
202  std::cout << " ----- MCParticles in event : : " << std::endl ;
203 
204  nmcp = ( nmcp < 10 ? nmcp : 10 ) ;
205 
206  MCParticle* mcp0 = 0 ; // pointer for first particle
207 
208  for(int i=0 ; i<nmcp ; i++ ){
209 
210  MCParticle* mcp = dynamic_cast<MCParticle*>( mcpcol->getElementAt(i) ) ;
211 
212  if( i == 0 ) mcp0 = mcp ;
213 
214  ParticleIDs::ext_type pidv = mcp->ext<ParticleIDs>() ;
215 
216  std::cout << " --- particle " << mcp->ext<Index>() << " found to be : " ;
217 
218  for( ParticleIDs::const_iterator ipid = pidv->begin() ; ipid != pidv->end(); ++ipid){
219 
220  std::cout << **ipid << ", " ;
221  }
222 
223  if( ( *(*pidv)[0] == "charged" && *(*pidv)[1] == "photon" ) ||
224  ( *(*pidv)[0] == "neutral" && *(*pidv)[1] == "electron") )
225 
226  std::cout << " --- ooops ! " ;
227 
228  std::cout << std::endl ;
229 
230 
231 
232  std::cout << " --- particle " << mcp->ext<Index>() << " user extension : "
233  << " someInt: " << mcp->ext<MyUserExtension>()->someInt
234  << " aFloat: " << mcp->ext<MyUserExtension>()->aFloat
235  << std::endl ;
236 
237 
238  // now check that the runtime relation for daughters:
239 
240  const MCParticleVec& daughters = mcp->getDaughters() ;
241 
242  std::cout << " --- particle " << mcp->ext<Index>() << " daughters: "
243  << std::endl ;
244 
245  std::cout << " --- from MCParticle: " ;
246 
247  for(MCParticleVec::const_iterator idau = daughters.begin() ;
248  idau != daughters.end() ; ++idau){
249 
250  std::cout << (*idau)->ext<Index>() << ", " ;
251  }
252  std::cout << std::endl ;
253 
254 
255  std::cout << " --- from runtime rel: " ;
256 
257 
258  ParentDaughter::to::rel_type daulist = mcp->rel<ParentDaughter::to>() ;
259 
260  for( ParentDaughter::to::const_iterator idau = daulist->begin();
261  idau != daulist->end(); ++idau){
262 
263  std::cout << (*idau)->ext<Index>() << ", " ;
264  }
265  std::cout << std::endl ;
266 
267 
268  std::cout << " --- particle " << mcp->ext<Index>() << " parents: "
269  << std::endl ;
270 
271  std::cout << " --- from MCParticle: " ;
272 
273 
274  const MCParticleVec& parents = mcp->getParents() ;
275 
276  for(MCParticleVec::const_iterator ipar = parents.begin() ;
277  ipar != parents.end() ; ++ipar){
278 
279  std::cout << (*ipar)->ext<Index>() << ", " ;
280  }
281  std::cout << std::endl ;
282 
283 
284  std::cout << " --- from runtime rel: " ;
285 
286 
287  ParentDaughter::from::rel_type parlist = mcp->rel<ParentDaughter::from>() ;
288 
289  for( ParentDaughter::from::const_iterator ipar = parlist->begin();
290  ipar != parlist->end(); ++ipar){
291 
292  std::cout << (*ipar)->ext<Index>() << ", " ;
293  }
294  std::cout << std::endl ;
295 
296 
297  // for demonstration we can rearange the daughter relationships:
298  if(i>0)
299 
300  merge_relations<ParentDaughter>( mcp0 , mcp ) ;
301  }
302 
303 
304  for(int i=0 ; i<nmcp ; i++ ){
305 
306  MCParticle* mcp = dynamic_cast<MCParticle*>( mcpcol->getElementAt(i) ) ;
307 
308  std::cout << " --- particle " << mcp->ext<Index>() << " ( mass: " << mcp->ext<Mass>() << ") "
309  << " daughters after merging : " ;
310 
311 
312  ParentDaughter::to::rel_type daulist = mcp->rel<ParentDaughter::to>() ;
313 
314  for( ParentDaughter::to::const_iterator idau = daulist->begin();
315  idau != daulist->end(); ++idau){
316 
317  std::cout << (*idau)->ext<Index>() << ", " ;
318  }
319  std::cout << std::endl ;
320 
321  }
322 
323 
324  } else {
325  std::cout << " couldn't find Track and Cluster collection in event !" << std::endl ;
326  }
327 
328  nEvents ++ ;
329  }
330  // -------- end of event loop -----------
331 
332 
333  lcReader->close() ;
334  delete lcReader ;
335  return 0 ;
336 }
337 
338 
std::vector< MCParticle * > MCParticleVec
Vector of (pointers to) MCParticles.
Definition: MCParticle.h:19
T endl(T...args)
float aFloat
Definition: lcrtrelation.cc:32
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...
LCReader * lcReader
Definition: lsh.cc:78