Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-01-13 01:43:19

0001 #ifndef RecoAlgos_SingleElementCollectionSelector_h
0002 #define RecoAlgos_SingleElementCollectionSelector_h
0003 /** \class SingleElementCollectionSelector
0004  *
0005  * selects a subset of a collection based
0006  * on single element selection done via functor
0007  *
0008  * \author Luca Lista, INFN
0009  *
0010  * \version $Revision: 1.1 $
0011  *
0012  * $Id: SingleElementCollectionSelector.h,v 1.1 2009/03/03 13:07:28 llista Exp $
0013  *
0014  */
0015 #include "FWCore/Framework/interface/ConsumesCollector.h"
0016 #include "CommonTools/UtilAlgos/interface/SelectionAdderTrait.h"
0017 #include "CommonTools/UtilAlgos/interface/StoreContainerTrait.h"
0018 #include "CommonTools/UtilAlgos/interface/ParameterAdapter.h"
0019 #include "CommonTools/UtilAlgos/interface/SelectedOutputCollectionTrait.h"
0020 
0021 namespace reco {
0022   namespace modules {
0023     template <typename S>
0024     struct SingleElementCollectionSelectorEventSetupInit;
0025   }
0026 }  // namespace reco
0027 namespace edm {
0028   class Event;
0029   class EventSetup;
0030 }  // namespace edm
0031 
0032 template <typename InputCollection,
0033           typename Selector,
0034           typename OutputCollection = typename ::helper::SelectedOutputCollectionTrait<InputCollection>::type,
0035           typename StoreContainer = typename ::helper::StoreContainerTrait<OutputCollection>::type,
0036           typename RefAdder = typename ::helper::SelectionAdderTrait<InputCollection, StoreContainer>::type>
0037 struct SingleElementCollectionSelector {
0038   typedef InputCollection collection;
0039   typedef StoreContainer container;
0040   typedef Selector selector;
0041   typedef typename container::const_iterator const_iterator;
0042   SingleElementCollectionSelector(const edm::ParameterSet& cfg, edm::ConsumesCollector&& iC)
0043       : select_(reco::modules::make<Selector>(cfg, iC)) {}
0044   const_iterator begin() const { return selected_.begin(); }
0045   const_iterator end() const { return selected_.end(); }
0046   void select(const edm::Handle<InputCollection>& c, const edm::Event&, const edm::EventSetup&) {
0047     selected_.clear();
0048     for (size_t idx = 0; idx < c->size(); ++idx) {
0049       if (select_((*c)[idx]))
0050         addRef_(selected_, c, idx);
0051     }
0052   }
0053 
0054 private:
0055   container selected_;
0056   selector select_;
0057   RefAdder addRef_;
0058   friend struct reco::modules::SingleElementCollectionSelectorEventSetupInit<SingleElementCollectionSelector>;
0059 };
0060 
0061 #include "CommonTools/UtilAlgos/interface/EventSetupInitTrait.h"
0062 
0063 namespace reco {
0064   namespace modules {
0065     template <typename S>
0066     struct SingleElementCollectionSelectorEventSetupInit {
0067       explicit SingleElementCollectionSelectorEventSetupInit(edm::ConsumesCollector iC) : esi_(iC) {}
0068       typedef typename EventSetupInit<typename S::selector>::type ESI;
0069       void init(S& s, const edm::Event& ev, const edm::EventSetup& es) { esi_.init(s.select_, ev, es); }
0070       ESI esi_;
0071     };
0072 
0073     template <typename I, typename S, typename O, typename C, typename R>
0074     struct EventSetupInit<SingleElementCollectionSelector<I, S, O, C, R> > {
0075       typedef SingleElementCollectionSelectorEventSetupInit<SingleElementCollectionSelector<I, S, O, C, R> > type;
0076     };
0077   }  // namespace modules
0078 }  // namespace reco
0079 
0080 #endif