Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

LCTypedVector.h

Go to the documentation of this file.
00001 #ifndef LCTypedVector_h
00002 #define LCTypedVector_h 1
00003 
00004 #include <vector>
00005 
00006 #include "EVENT/LCObject.h"
00007 #include "EVENT/LCCollection.h"
00008 #include "Exceptions.h"
00009 
00010 #include <sstream>
00011 #include <typeinfo>
00012 
00013 
00014 namespace UTIL{
00015 
00016 
00017   /** Templated helper class that is an std::vector<T*> of the elements in the LCCollection.
00018    *  This can be used, e.g. if the objects are to be passed to an algorithm in terms of
00019    *  iterators. For example:<p>
00020    *   &nbsp; LCTypedVector<SimTrackerHits> sim( evt->getCollection("tpc") ) ;<br>
00021    *   &nbsp; LCCollectionVec* trackerHits = new LCCollectionVec( LCIO::TRACKERHIT )  ;<br>
00022    *   &nbsp; std::transform( sim.begin(), sim.end(), std::back_inserter( *trackerHits ) , digitizer ) ;
00023    *  <p>Note that instantiating an LCTypedVector involves copying and casting of the pointers in 
00024    *  the collection. In a future release of LCIO this might not be neccessary any more.
00025    *  @author gaede
00026    *  @version $Id: LCTypedVector.h,v 1.2 2006/03/28 17:18:49 gaede Exp $
00027    */
00028   template <class T> 
00029   class LCTypedVector : public  std::vector<T*> {
00030     
00031   public:  
00032     
00033     LCTypedVector( EVENT::LCCollection* col ) : _col( col) {
00034       
00035       resize( _col->getNumberOfElements() ) ;
00036       
00037       for(int i=0;i<_col->getNumberOfElements();i++ ) {
00038         
00039         (*this)[i] = dynamic_cast<T*>( col->getElementAt(i) ) ;
00040         
00041         // check the first element for the proper type
00042         if( i == 0 && (*this)[i] == 0  ){
00043 
00044           std::stringstream str ;
00045           str << "LCTypedVector: cannot convert " << col->getTypeName() << " to " 
00046               << typeid(T).name()  ;
00047 
00048           throw EVENT::Exception(  str.str().c_str() ) ; 
00049         }
00050       }
00051     }
00052     
00053     EVENT::LCCollection* col() { return _col ; }
00054     
00055   protected:
00056     EVENT::LCCollection* _col ;
00057   } ; 
00058   
00059 } // namespace
00060 #endif
00061 
00062 

Generated on Sun Jun 3 06:32:01 2007 for A TPC Tracking Environment by  doxygen 1.3.9.1