Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #ifndef RecoAlgos_SingleElementCollectionRefSelector_h
0002 #define RecoAlgos_SingleElementCollectionRefSelector_h
0003 /** \class SingleElementCollectionRefSelector
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: SingleElementCollectionRefSelector.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 #include "DataFormats/Common/interface/View.h"
0021 
0022 namespace reco {
0023   namespace modules {
0024     template <typename S>
0025     struct SingleElementCollectionRefSelectorEventSetupInit;
0026   }
0027 }  // namespace reco
0028 namespace edm {
0029   class Event;
0030   class EventSetup;
0031 }  // namespace edm
0032 
0033 template <typename InputType,
0034           typename Selector,
0035           typename OutputCollection = typename ::helper::SelectedOutputCollectionTrait<edm::View<InputType> >::type,
0036           typename StoreContainer = typename ::helper::StoreContainerTrait<OutputCollection>::type,
0037           typename RefAdder = typename ::helper::SelectionAdderTrait<edm::View<InputType>, StoreContainer>::type>
0038 struct SingleElementCollectionRefSelector {
0039   typedef edm::View<InputType> InputCollection;
0040   typedef InputCollection collection;
0041   typedef StoreContainer container;
0042   typedef Selector selector;
0043   typedef typename container::const_iterator const_iterator;
0044   SingleElementCollectionRefSelector(const edm::ParameterSet& cfg, edm::ConsumesCollector&& iC)
0045       : select_(reco::modules::make<Selector>(cfg, iC)) {}
0046   const_iterator begin() const { return selected_.begin(); }
0047   const_iterator end() const { return selected_.end(); }
0048   void select(const edm::Handle<InputCollection>& c, const edm::Event&, const edm::EventSetup&) {
0049     selected_.clear();
0050     for (size_t idx = 0; idx < c->size(); ++idx) {
0051       if (select_(c->refAt(idx)))
0052         addRef_(selected_, c, idx);
0053     }
0054   }
0055 
0056 private:
0057   container selected_;
0058   selector select_;
0059   RefAdder addRef_;
0060   friend struct reco::modules::SingleElementCollectionRefSelectorEventSetupInit<SingleElementCollectionRefSelector>;
0061 };
0062 
0063 #include "CommonTools/UtilAlgos/interface/EventSetupInitTrait.h"
0064 
0065 namespace reco {
0066   namespace modules {
0067     template <typename S>
0068     struct SingleElementCollectionRefSelectorEventSetupInit {
0069       explicit SingleElementCollectionRefSelectorEventSetupInit(edm::ConsumesCollector iC) : esi_(iC) {}
0070       void init(S& s, const edm::Event& ev, const edm::EventSetup& es) { esi_.init(s.select_, ev, es); }
0071       typedef typename EventSetupInit<typename S::selector>::type ESI;
0072       ESI esi_;
0073     };
0074 
0075     template <typename I, typename S, typename O, typename C, typename R>
0076     struct EventSetupInit<SingleElementCollectionRefSelector<I, S, O, C, R> > {
0077       typedef SingleElementCollectionRefSelectorEventSetupInit<SingleElementCollectionRefSelector<I, S, O, C, R> > type;
0078     };
0079   }  // namespace modules
0080 }  // namespace reco
0081 
0082 #endif