CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/CommonTools/UtilAlgos/interface/MasterCollectionHelper.h

Go to the documentation of this file.
00001 #ifndef CommonTools_UtilAlgos_MasterCollectionHelper_h
00002 #define CommonTools_UtilAlgos_MasterCollectionHelper_h
00003 /* \class helper::MasterCollection<C>
00004  *
00005  * differentiate index addressing in case of edm::View
00006  * since a View retrieving a collection of reference
00007  * would have referece indices (key()) pointing to the master
00008  * collection different from reference position into the view
00009  *
00010  * \author Luca Lista, INFN
00011  *
00012  * \version $Id: MasterCollectionHelper.h,v 1.2 2013/02/28 00:34:26 wmtan Exp $
00013  *
00014  */
00015 #include "DataFormats/Common/interface/Handle.h"
00016 #include "DataFormats/Common/interface/View.h"
00017 #include "DataFormats/Common/interface/RefToBaseProd.h"
00018 
00019 namespace helper {
00020   template<typename C1>
00021   struct MasterCollection {
00022     typedef edm::Ref<C1> ref_type;
00023     explicit MasterCollection(const edm::Handle<C1> & handle) : 
00024       handle_(handle) { }
00025     size_t size() const { return handle_->size(); }
00026     size_t index(size_t i) const { return i; }
00027     const edm::Handle<C1> & get() const { return handle_; }
00028     ref_type getRef(size_t idx) const { return ref_type(get(), idx); }
00029     template<typename R>
00030     R getConcreteRef(size_t idx) const { return getRef(idx); }
00031   private:
00032     edm::Handle<C1> handle_;
00033   };
00034   
00035   template<typename T>
00036   struct MasterCollection<edm::View<T> > {
00037     typedef edm::RefToBase<T> ref_type;
00038     explicit MasterCollection(const edm::Handle<edm::View<T> > & handle) :
00039       handle_(handle) {
00040       if(handle_->size() != 0) 
00041         ref_ = edm::RefToBaseProd<T>(handle_->refAt(0));
00042     }
00043     size_t size() const { 
00044       if (ref_.isNull()) return 0;
00045       return ref_->size(); 
00046     }
00047     size_t index(size_t i) const { 
00048       return handle_->refAt(i).key(); 
00049     }
00050     const edm::RefToBaseProd<T> & get() const { return ref_; }
00051     ref_type getRef(size_t idx) const { return ref_type(get(), idx); }
00052     template<typename R>
00053     R getConcreteRef(size_t idx) const { return getRef(idx).template castTo<R>(); }
00054   private:
00055     edm::Handle<edm::View<T> > handle_;
00056     edm::RefToBaseProd<T> ref_;
00057   };
00058 
00059 }
00060 
00061 #endif
00062