File indexing completed on 2024-04-06 12:01:17
0001 #ifndef UtilAlgos_StoreManagerTrait_h
0002 #define UtilAlgos_StoreManagerTrait_h
0003
0004
0005
0006
0007
0008 #include "DataFormats/Common/interface/OwnVector.h"
0009 #include "DataFormats/Common/interface/RefVector.h"
0010 #include "DataFormats/Common/interface/RefToBase.h"
0011 #include "DataFormats/Common/interface/RefToBaseVector.h"
0012 #include "DataFormats/Common/interface/Ptr.h"
0013 #include "DataFormats/Common/interface/PtrVector.h"
0014 #include "FWCore/Framework/interface/Event.h"
0015 #include <memory>
0016 #include "boost/type_traits.hpp"
0017
0018 namespace helper {
0019
0020 template <typename Collection>
0021 struct IteratorToObjectConverter {
0022 typedef typename Collection::value_type value_type;
0023 template <typename I>
0024 static value_type convert(const I &i) {
0025 return value_type(**i);
0026 }
0027 };
0028
0029 template <typename T>
0030 struct IteratorToObjectConverter<edm::OwnVector<T> > {
0031 typedef std::unique_ptr<T> value_type;
0032 template <typename I>
0033 static value_type convert(const I &i) {
0034 return value_type((*i)->clone());
0035 }
0036 };
0037
0038 template <typename C>
0039 struct IteratorToObjectConverter<edm::RefVector<C> > {
0040 typedef edm::Ref<C> value_type;
0041 template <typename I>
0042 static value_type convert(const I &i) {
0043 return value_type(*i);
0044 }
0045 };
0046
0047 template <typename T>
0048 struct IteratorToObjectConverter<edm::RefToBaseVector<T> > {
0049 typedef edm::RefToBase<T> value_type;
0050 template <typename I>
0051 static value_type convert(const I &i) {
0052 return value_type(*i);
0053 }
0054 };
0055
0056 template <typename T>
0057 struct IteratorToObjectConverter<edm::PtrVector<T> > {
0058 typedef edm::Ptr<T> value_type;
0059 template <typename I>
0060 static value_type convert(const I &i) {
0061 return value_type(*i);
0062 }
0063 };
0064
0065 template <typename OutputCollection, typename ClonePolicy = IteratorToObjectConverter<OutputCollection> >
0066 struct CollectionStoreManager {
0067 typedef OutputCollection collection;
0068 template <typename C>
0069 CollectionStoreManager(const edm::Handle<C> &h) : selected_(new OutputCollection) {}
0070 template <typename I>
0071 void cloneAndStore(const I &begin, const I &end, edm::Event &) {
0072 using namespace std;
0073 for (I i = begin; i != end; ++i) {
0074 typename ClonePolicy::value_type v = ClonePolicy::convert(i);
0075 selected_->push_back(std::move(v));
0076 }
0077 }
0078 edm::OrphanHandle<collection> put(edm::Event &evt) { return evt.put(std::move(selected_)); }
0079 size_t size() const { return selected_->size(); }
0080
0081 private:
0082 std::unique_ptr<collection> selected_;
0083 };
0084
0085 template <typename OutputCollection, typename EdmFilter>
0086 struct ObjectSelectorBase : public EdmFilter {
0087 ObjectSelectorBase(const edm::ParameterSet &) { this->template produces<OutputCollection>(); }
0088 };
0089
0090 template <typename OutputCollection, typename EdmFilter>
0091 struct StoreManagerTrait {
0092 using type = CollectionStoreManager<OutputCollection>;
0093 using base = ObjectSelectorBase<OutputCollection, EdmFilter>;
0094 };
0095
0096 }
0097
0098 #endif