00001 #ifndef RecoAlgos_SingleElementCollectionSelector_h 00002 #define RecoAlgos_SingleElementCollectionSelector_h 00003 00015 #include "PhysicsTools/UtilAlgos/interface/SelectionAdderTrait.h" 00016 #include "PhysicsTools/UtilAlgos/interface/StoreContainerTrait.h" 00017 #include "PhysicsTools/UtilAlgos/interface/ParameterAdapter.h" 00018 namespace reco { 00019 namespace modules { 00020 template<typename S> struct SingleElementCollectionSelectorEventSetupInit; 00021 } 00022 } 00023 template<typename InputCollection, typename Selector, 00024 typename OutputCollection = typename ::helper::SelectedOutputCollectionTrait<InputCollection>::type, 00025 typename StoreContainer = typename ::helper::StoreContainerTrait<OutputCollection>::type, 00026 typename RefAdder = typename ::helper::SelectionAdderTrait<InputCollection, StoreContainer>::type> 00027 struct SingleElementCollectionSelector { 00028 typedef InputCollection collection; 00029 typedef StoreContainer container; 00030 typedef Selector selector; 00031 typedef typename container::const_iterator const_iterator; 00032 SingleElementCollectionSelector(const edm::ParameterSet & cfg) : 00033 select_(reco::modules::make<Selector>(cfg)) { } 00034 const_iterator begin() const { return selected_.begin(); } 00035 const_iterator end() const { return selected_.end(); } 00036 void select(const edm::Handle<InputCollection> & c, const edm::Event &, const edm::EventSetup&) { 00037 selected_.clear(); 00038 for(size_t idx = 0; idx < c->size(); ++ idx) { 00039 if(select_((*c)[idx])) 00040 addRef_(selected_, c, idx); 00041 } 00042 } 00043 private: 00044 container selected_; 00045 selector select_; 00046 RefAdder addRef_; 00047 friend class reco::modules::SingleElementCollectionSelectorEventSetupInit<SingleElementCollectionSelector>; 00048 }; 00049 00050 #include "PhysicsTools/UtilAlgos/interface/EventSetupInitTrait.h" 00051 00052 namespace reco { 00053 namespace modules { 00054 template<typename S> 00055 struct SingleElementCollectionSelectorEventSetupInit { 00056 static void init(S & s, const edm::Event & ev, const edm::EventSetup& es) { 00057 typedef typename EventSetupInit<typename S::selector>::type ESI; 00058 ESI::init(s.select_, ev, es); 00059 } 00060 }; 00061 00062 template<typename I, typename S, typename O, typename C, typename R> 00063 struct EventSetupInit<SingleElementCollectionSelector<I, S, O, C, R> > { 00064 typedef SingleElementCollectionSelectorEventSetupInit<SingleElementCollectionSelector<I, S, O, C, R> > type; 00065 }; 00066 } 00067 } 00068 00069 #endif