CMS 3D CMS Logo

StoreManagerTrait.h

Go to the documentation of this file.
00001 #ifndef UtilAlgos_StoreManagerTrait_h
00002 #define UtilAlgos_StoreManagerTrait_h
00003 /* \class helper::CollectionStoreManager
00004  *
00005  * \author: Luca Lista, INFN
00006  *
00007  */
00008 #include "DataFormats/Common/interface/OwnVector.h"
00009 #include "DataFormats/Common/interface/RefVector.h"
00010 #include "DataFormats/Common/interface/RefToBase.h"
00011 #include "DataFormats/Common/interface/RefToBaseVector.h"
00012 #include "DataFormats/Common/interface/Ptr.h"
00013 #include "DataFormats/Common/interface/PtrVector.h"
00014 #include <memory>
00015 #include "boost/static_assert.hpp"
00016 #include "boost/type_traits.hpp"
00017 
00018 namespace helper {
00019 
00020   template<typename Collection>
00021   struct IteratorToObjectConverter {
00022     typedef typename Collection::value_type value_type;
00023     template<typename I>
00024     static value_type convert( const I & i ) {
00025       return value_type( * * i );
00026     }
00027   };
00028   
00029   template<typename T>
00030   struct IteratorToObjectConverter<edm::OwnVector<T> > {
00031     typedef std::auto_ptr<T> value_type;
00032     template<typename I>
00033     static value_type convert( const I & i ) {
00034       return value_type( (*i)->clone() );
00035     }
00036   };
00037 
00038   template<typename C>
00039   struct IteratorToObjectConverter<edm::RefVector<C> > {
00040     typedef edm::Ref<C> value_type;
00041     template<typename I>
00042     static value_type convert( const I & i ) {
00043       return value_type( * i );
00044     }
00045   };
00046 
00047   template<typename T>
00048   struct IteratorToObjectConverter<edm::RefToBaseVector<T> > {
00049     typedef edm::RefToBase<T> value_type;
00050     template<typename I>
00051     static value_type convert( const I & i ) {
00052       return value_type( * i );
00053     }
00054   };
00055 
00056 
00057   template<typename T>
00058   struct IteratorToObjectConverter<edm::PtrVector<T> > {
00059     typedef edm::Ptr<T> value_type;
00060     template<typename I>
00061     static value_type convert( const I & i ) {
00062       return value_type( * i );
00063     }
00064   };
00065 
00066   /*
00067   template<typename OutputCollection, typename InputCollection>
00068   struct OutputCollectionCreator {
00069     static std::auto_ptr<OutputCollection> createNewCollection( const edm::Handle<InputCollection> & ) {
00070       return std::auto_ptr<OutputCollection>( new OutputCollection );
00071     }
00072   };
00073 
00074   template<typename T, typename InputCollection>
00075   struct OutputCollectionCreator<edm::RefToBaseVector<T>, InputCollection> {
00076     static std::auto_ptr<edm::RefToBaseVector<T> > createNewCollection( const edm::Handle<InputCollection> & h ) {
00077       return std::auto_ptr<edm::RefToBaseVector<T> >( new edm::RefToBaseVector<T>(h) );
00078     }
00079   };
00080   */
00081 
00082   /*
00083   template<typename T1, typename T2>
00084   struct OutputCollectionCreator<RefToBaseVector<T1>, RefToBaseVector<T2> > {
00085     static RefToBaseVector<T1> * createNewCollection( const edm::Handle<RefToBaseVector<T2> > & h ) {
00086       return new RefToBaseVector<T1>(h);
00087     }
00088   };
00089   */
00090 
00091   template<typename OutputCollection, 
00092            typename ClonePolicy = IteratorToObjectConverter<OutputCollection> >
00093   struct CollectionStoreManager {
00094     typedef OutputCollection collection;
00095     template<typename C>
00096     CollectionStoreManager( const edm::Handle<C> & h ) :
00097     selected_( new OutputCollection ) { 
00098       //      selected_ = OutputCollectionCreator<OutputCollection, C>::createNewCollection(h);
00099     }
00100     template<typename I>
00101     void cloneAndStore( const I & begin, const I & end, edm::Event & ) {
00102       using namespace std;
00103       for( I i = begin; i != end; ++ i ) {
00104         typename ClonePolicy::value_type v = ClonePolicy::convert( i );
00105         selected_->push_back( v );
00106       }
00107     }
00108     edm::OrphanHandle<collection> put( edm::Event & evt ) {
00109       return evt.put( selected_ );
00110     }
00111     size_t size() const { return selected_->size(); }
00112   private:
00113     std::auto_ptr<collection> selected_;
00114   };
00115 
00116   template<typename OutputCollection>
00117   struct ObjectSelectorBase : public edm::EDFilter {
00118     ObjectSelectorBase( const edm::ParameterSet & cfg ) {
00119       produces<OutputCollection>();
00120     }    
00121   };
00122 
00123   template<typename OutputCollection>
00124   struct StoreManagerTrait {
00125     typedef CollectionStoreManager<OutputCollection> type;
00126     typedef ObjectSelectorBase<OutputCollection> base;
00127   };
00128 
00129 }
00130 
00131 #endif

Generated on Tue Jun 9 17:42:41 2009 for CMSSW by  doxygen 1.5.4