Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:50:11

0001 #ifndef CommonTools_UtilAlgos_MasterCollectionHelper_h
0002 #define CommonTools_UtilAlgos_MasterCollectionHelper_h
0003 /* \class helper::MasterCollection<C>
0004  *
0005  * differentiate index addressing in case of edm::View
0006  * since a View retrieving a collection of reference
0007  * would have referece indices (key()) pointing to the master
0008  * collection different from reference position into the view
0009  *
0010  * \author Luca Lista, INFN
0011  *
0012  * \version $Id: MasterCollectionHelper.h,v 1.1 2009/03/03 13:07:26 llista Exp $
0013  *
0014  */
0015 #include "DataFormats/Common/interface/Handle.h"
0016 #include "DataFormats/Common/interface/View.h"
0017 #include "DataFormats/Common/interface/RefToBaseProd.h"
0018 #include "FWCore/Framework/interface/makeRefToBaseProdFrom.h"
0019 
0020 namespace helper {
0021   template <typename C1>
0022   struct MasterCollection {
0023     typedef edm::Ref<C1> ref_type;
0024     explicit MasterCollection(const edm::Handle<C1>& handle, edm::Event const& event) : handle_(handle) {}
0025     size_t size() const { return handle_->size(); }
0026     size_t index(size_t i) const { return i; }
0027     const edm::Handle<C1>& get() const { return handle_; }
0028     ref_type getRef(size_t idx) const { return ref_type(get(), idx); }
0029     template <typename R>
0030     R getConcreteRef(size_t idx) const {
0031       return getRef(idx);
0032     }
0033 
0034   private:
0035     edm::Handle<C1> handle_;
0036   };
0037 
0038   template <typename T>
0039   struct MasterCollection<edm::View<T> > {
0040     typedef edm::RefToBase<T> ref_type;
0041     explicit MasterCollection(const edm::Handle<edm::View<T> >& handle, edm::Event const& event) : handle_(handle) {
0042       if (!handle_->empty()) {
0043         ref_ = edm::makeRefToBaseProdFrom(handle_->refAt(0), event);
0044       }
0045     }
0046     size_t size() const {
0047       if (ref_.isNull())
0048         return 0;
0049       return ref_->size();
0050     }
0051     size_t index(size_t i) const { return handle_->refAt(i).key(); }
0052     const edm::RefToBaseProd<T>& get() const { return ref_; }
0053     ref_type getRef(size_t idx) const { return ref_type(get(), idx); }
0054     template <typename R>
0055     R getConcreteRef(size_t idx) const {
0056       return getRef(idx).template castTo<R>();
0057     }
0058 
0059   private:
0060     edm::Handle<edm::View<T> > handle_;
0061     edm::RefToBaseProd<T> ref_;
0062   };
0063 
0064 }  // namespace helper
0065 
0066 #endif