/data3/calcul/jacquem/working_dir/Micromegas/micromegasFrameWork/lcio/src/cpp/src/EXAMPLE/lcrtrelation.cc

Go to the documentation of this file.
00001 #include "lcio.h"
00002 
00003 #include "IO/LCReader.h"
00004 #include "IMPL/LCTOOLS.h"
00005 #include "EVENT/LCCollection.h" 
00006 #include "EVENT/Track.h" 
00007 #include "EVENT/Cluster.h" 
00008 
00009 using namespace std ;
00010 using namespace lcio ;
00011 
00012 
00013 
00014 // ------  define some structs for extensions and relations:-----------
00015 
00016 // NB: the first template argument of the parent class has to the class itself !
00017 
00018 // a simple int extension 
00019 struct Index : LCIntExtension<Index> {} ;
00020 
00021 // a simple float extension 
00022 struct Mass : LCFloatExtension<Mass> {} ;
00023 
00024 // a vector of strings (pointers) that are owned by the object that is extended 
00025 struct ParticleIDs : LCOwnedExtensionVector<ParticleIDs,std::string> {};
00026 
00027 
00028 // note extensions can be attched to any LCObject and be of any type, i.e. in particular
00029 // of user defined types:
00030 struct UserClass{
00031   int someInt ;
00032   float aFloat ;
00033 };
00034 struct MyUserExtension : LCOwnedExtension<MyUserExtension,UserClass> {} ;
00035 
00036 
00037 // relations have to specify the type of the objects from and to which they relate: 
00038 
00039 // example:   a one to many relationship between tracks and clusters:
00040 struct TrkCluLink : LC1ToNRelation<TrkCluLink,Track,Cluster> {} ;
00041 
00042 
00043 // a many to many relationship between MCParticles
00044 struct ParentDaughter : LCNToNRelation<ParentDaughter,MCParticle,MCParticle> {} ;
00045 
00046 
00047 //-----------------------------------------------------------------------------------------
00048 
00049 /** Example/test program for new runtime extensions and relations
00050  *  requires recjob.slcio (from simjob/recjob).
00051  */
00052 
00053 int main(int argc, char** argv ){
00054 
00055 
00056   LCReader* lcReader = LCFactory::getInstance()->createLCReader() ;
00057 
00058   lcReader->open( "recjob.slcio" ) ;
00059 
00060   LCEvent* evt ;
00061   int nEvents = 0 ;
00062   
00063 
00064   //----------- the event loop -----------
00065   while( (evt = lcReader->readNextEvent()) != 0 && nEvents < 1 ) {
00066     
00067 
00068     LCCollection* mcpcol = evt->getCollection("MCParticle" ) ;
00069     
00070     int nmcp = mcpcol->getNumberOfElements() ;
00071 
00072     for(int i=0 ; i< nmcp  ; i++ ){
00073 
00074       MCParticle*  mcp = dynamic_cast<MCParticle*>( mcpcol->getElementAt(i) ) ;
00075         
00076 
00077       // ints can be assigned directly - w/o pointer !
00078       // make every particle know it's index in the collection 
00079       mcp->ext<Index>() = i ;   
00080 
00081       mcp->ext<Mass>() = mcp->getMass() ;
00082 
00083       // assign a user object to each particle:
00084 
00085       mcp->ext<MyUserExtension>() = new UserClass ;
00086 
00087       mcp->ext<MyUserExtension>()->someInt = i*42 ;
00088       mcp->ext<MyUserExtension>()->aFloat = i*3.1415 ;
00089 
00090       // assign some pid strings - note ownership is taken by particle 
00091 
00092       if( ! (i % 2) )
00093 
00094         mcp->ext<ParticleIDs>()->push_back( new std::string("charged")  )  ;
00095 
00096       else
00097 
00098         mcp->ext<ParticleIDs>()->push_back( new std::string("neutral")  )  ;
00099       
00100       if( ! (i % 3) )
00101 
00102         mcp->ext<ParticleIDs>()->push_back( new std::string("hadron")  )  ;
00103 
00104       else if( ! ((i+1) % 3) )
00105 
00106         mcp->ext<ParticleIDs>()->push_back( new std::string("photon")  )  ;
00107 
00108       else if( ! ((i+2) % 3) )
00109 
00110         mcp->ext<ParticleIDs>()->push_back( new std::string("electron")  )  ;
00111 
00112 
00113       // copy the parent - daughter relationship:
00114 
00115       const MCParticleVec& daughters = mcp->getDaughters() ;
00116 
00117       for(unsigned j=0 ; j< daughters.size()   ; j++ ){
00118 
00119         add_relation<ParentDaughter>( mcp, daughters[j] ) ;
00120       }
00121       
00122       
00123     }
00124     
00125 
00126     LCCollection* trkcol = evt->getCollection("SomeTracks" ) ;
00127     LCCollection* clucol = evt->getCollection("SomeClusters" ) ;
00128 
00129     if( trkcol && clucol ) {
00130 
00131       int nclu = clucol->getNumberOfElements() ;
00132       int ntrk = trkcol->getNumberOfElements() ;
00133 
00134 
00135       for(int j=0 ; j< ntrk  ; j++ ){
00136         
00137         Track*   trk = dynamic_cast<Track*>   ( trkcol->getElementAt(j) ) ;
00138         
00139         // ints can be assigned directly - w/o pointer !
00140         // make every track know it's index in the collection 
00141         trk->ext<Index>() = j ;   
00142         
00143         for(int k=0 ; k< nclu ; k++ ){
00144           
00145           Cluster* clu = dynamic_cast<Cluster*> ( clucol->getElementAt(k) ) ;
00146           
00147           // make every cluster know it's index in the collection 
00148           if( j == 0 )
00149             clu->ext<Index>() = k ;
00150           
00151           if( j % 2  && ( k == j || k == j-1 ) )  
00152 
00153             add_relation<TrkCluLink>( trk ,clu );
00154         }
00155       }
00156       
00157       
00158       // --- now print the relations:
00159 
00160       std::cout << "   ---- tracks assigned to clusters: " << std::endl ;
00161       for(int j=0 ; j< ntrk ; j++ ){
00162 
00163 
00164         Track*   trk = dynamic_cast<Track*>   ( trkcol->getElementAt(j) ) ;
00165 
00166         std::cout << " track " << trk->ext<Index>()  << " assigned to clusters :   " ; 
00167 
00168         TrkCluLink::to::rel_type clulist =  trk->rel<TrkCluLink::to>() ;
00169 
00170         for( TrkCluLink::to::const_iterator iclu = clulist->begin() ;
00171              iclu != clulist->end() ; ++iclu ){
00172           
00173           Cluster* clu = *iclu ; // iterator is of type pointer to container element
00174 
00175           std::cout <<  clu->ext<Index>() << ", " ; 
00176         }
00177         std::cout << std::endl ; 
00178       }
00179 
00180       std::cout << "   ----- now the inverse relation : " << std::endl ;
00181 
00182       for(int k=0 ; k< nclu ; k++ ){
00183           
00184         Cluster* clu = dynamic_cast<Cluster*> ( clucol->getElementAt(k) ) ;
00185 
00186 
00187         Track* trk =  clu->rel<TrkCluLink::from>() ;
00188         
00189         std::cout << " cluster " 
00190                   << clu->ext<Index>() << " assigned from track:   " ;
00191         if( trk != 0 )
00192           std::cout << trk->ext<Index>() << std::endl ; 
00193         else
00194           std::cout << " none "  << std::endl ; 
00195       }
00196 
00197       std::cout << std::endl ; 
00198 
00199 
00200       // print MCParticle extensions and relations:
00201 
00202       std::cout << "   ----- MCParticles in event : : " << std::endl ;
00203       
00204       nmcp = ( nmcp < 10 ? nmcp : 10 ) ;
00205 
00206       MCParticle*  mcp0 = 0 ;   // pointer for first particle
00207 
00208       for(int i=0 ; i<nmcp  ; i++ ){
00209         
00210         MCParticle*  mcp = dynamic_cast<MCParticle*>( mcpcol->getElementAt(i) ) ;
00211 
00212         if( i == 0 ) mcp0 = mcp  ;
00213         
00214         ParticleIDs::ext_type pidv =  mcp->ext<ParticleIDs>() ;
00215         
00216         std::cout <<  "   --- particle " << mcp->ext<Index>()  << " found to be : " ;  
00217         
00218         for( ParticleIDs::const_iterator ipid = pidv->begin() ; ipid != pidv->end(); ++ipid){
00219           
00220           std::cout << **ipid << ", " ;
00221         }
00222         
00223         if( ( *(*pidv)[0] == "charged" && *(*pidv)[1] == "photon"  ) ||
00224             ( *(*pidv)[0] == "neutral" && *(*pidv)[1] == "electron")   )
00225 
00226           std::cout << " --- ooops ! " ;
00227         
00228         std::cout << std::endl ; 
00229         
00230 
00231 
00232         std::cout <<  "   --- particle " << mcp->ext<Index>()   <<  " user extension : " 
00233                   << " someInt: " << mcp->ext<MyUserExtension>()->someInt
00234                   << " aFloat:  " << mcp->ext<MyUserExtension>()->aFloat
00235                   << std::endl ;
00236 
00237 
00238         // now check that the runtime relation for daughters:
00239 
00240         const MCParticleVec& daughters = mcp->getDaughters() ;
00241 
00242         std::cout <<  "   --- particle " << mcp->ext<Index>()   <<  " daughters: " 
00243                   << std::endl ;  
00244 
00245         std::cout <<  "      --- from MCParticle:  " ; 
00246 
00247         for(MCParticleVec::const_iterator idau = daughters.begin() ; 
00248             idau != daughters.end() ; ++idau){
00249           
00250           std::cout << (*idau)->ext<Index>() << ", " ; 
00251         }
00252         std::cout << std::endl ; 
00253         
00254 
00255         std::cout <<  "      --- from runtime rel: " ; 
00256 
00257 
00258         ParentDaughter::to::rel_type daulist =  mcp->rel<ParentDaughter::to>() ;
00259         
00260         for( ParentDaughter::to::const_iterator idau = daulist->begin(); 
00261              idau != daulist->end(); ++idau){
00262           
00263           std::cout << (*idau)->ext<Index>() << ", " ;
00264         }
00265         std::cout << std::endl ; 
00266         
00267 
00268         std::cout <<  "   --- particle " << mcp->ext<Index>()   <<  " parents: " 
00269                   << std::endl ;  
00270 
00271         std::cout <<  "      --- from MCParticle:  " ; 
00272 
00273 
00274         const MCParticleVec& parents = mcp->getParents() ;
00275 
00276         for(MCParticleVec::const_iterator ipar = parents.begin() ; 
00277             ipar != parents.end() ; ++ipar){
00278           
00279           std::cout << (*ipar)->ext<Index>() << ", " ; 
00280         }
00281         std::cout << std::endl ; 
00282         
00283 
00284         std::cout <<  "      --- from runtime rel: " ; 
00285 
00286 
00287         ParentDaughter::from::rel_type parlist =  mcp->rel<ParentDaughter::from>() ;
00288         
00289         for( ParentDaughter::from::const_iterator ipar = parlist->begin(); 
00290              ipar != parlist->end(); ++ipar){
00291           
00292           std::cout << (*ipar)->ext<Index>() << ", " ;
00293         }
00294         std::cout << std::endl ; 
00295 
00296 
00297         // for demonstration we can rearange the daughter relationships:
00298         if(i>0) 
00299 
00300           merge_relations<ParentDaughter>( mcp0 , mcp ) ;
00301       }
00302       
00303 
00304       for(int i=0 ; i<nmcp  ; i++ ){
00305         
00306         MCParticle*  mcp = dynamic_cast<MCParticle*>( mcpcol->getElementAt(i) ) ;
00307 
00308         std::cout <<  "   --- particle " << mcp->ext<Index>()  << " ( mass: " << mcp->ext<Mass>() << ") "
00309                   <<  " daughters after merging : " ;
00310 
00311 
00312         ParentDaughter::to::rel_type daulist =  mcp->rel<ParentDaughter::to>() ;
00313         
00314         for( ParentDaughter::to::const_iterator idau = daulist->begin(); 
00315              idau != daulist->end(); ++idau){
00316           
00317           std::cout << (*idau)->ext<Index>() << ", " ;
00318         }
00319         std::cout << std::endl ; 
00320 
00321       }
00322 
00323 
00324     } else {
00325       std::cout << " couldn't find Track and Cluster collection in event !" << std::endl ;
00326     }
00327     
00328     nEvents ++ ;
00329   } 
00330   // -------- end of event loop -----------
00331   
00332 
00333   lcReader->close() ;
00334   delete lcReader ;
00335   return 0 ;
00336 }
00337 
00338   

Generated on Mon Jan 7 13:15:21 2013 for MicromegasFramework by  doxygen 1.4.7