Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-02-05 23:51:10

0001 #ifndef CommonTools_UtilAlgos_AssociatedVariableCollectionSelector_h
0002 #define CommonTools_UtilAlgos_AssociatedVariableCollectionSelector_h
0003 /* \class AssociatedVariableCollectionSelector
0004  *
0005  * \author Luca Lista, INFN
0006  *
0007  * \version $Id: AssociatedVariableCollectionSelector.h,v 1.2 2010/02/20 20:55:13 wmtan Exp $
0008  *
0009  */
0010 #include "FWCore/Framework/interface/ConsumesCollector.h"
0011 #include "FWCore/Utilities/interface/InputTag.h"
0012 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0013 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0014 #include "FWCore/Framework/interface/Event.h"
0015 #include "DataFormats/Common/interface/Handle.h"
0016 #include "CommonTools/UtilAlgos/interface/SelectionAdderTrait.h"
0017 #include "CommonTools/UtilAlgos/interface/SelectedOutputCollectionTrait.h"
0018 #include "CommonTools/UtilAlgos/interface/StoreContainerTrait.h"
0019 #include "CommonTools/UtilAlgos/interface/ParameterAdapter.h"
0020 #include "DataFormats/Common/interface/getRef.h"
0021 
0022 namespace reco {
0023   namespace modules {
0024     template <typename S>
0025     struct AssociatedVariableCollectionSelectorEventSetupInit;
0026   }
0027 }  // namespace reco
0028 
0029 template <typename InputCollection,
0030           typename VarCollection,
0031           typename Selector,
0032           typename OutputCollection = typename ::helper::SelectedOutputCollectionTrait<InputCollection>::type,
0033           typename StoreContainer = typename ::helper::StoreContainerTrait<OutputCollection>::type,
0034           typename RefAdder = typename ::helper::SelectionAdderTrait<InputCollection, StoreContainer>::type>
0035 class AssociatedVariableCollectionSelector {
0036 public:
0037   typedef InputCollection collection;
0038   typedef StoreContainer container;
0039   typedef Selector selector;
0040   typedef typename container::const_iterator const_iterator;
0041   AssociatedVariableCollectionSelector(const edm::ParameterSet& cfg, edm::ConsumesCollector&& iC)
0042       : varToken_(iC.consumes<VarCollection>(cfg.template getParameter<edm::InputTag>("var"))),
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& evt, const edm::EventSetup&) {
0047     selected_.clear();
0048     edm::Handle<VarCollection> var;
0049     evt.getByToken(varToken_, var);
0050     for (size_t idx = 0; idx < c->size(); ++idx) {
0051       if (select_((*c)[idx], (*var)[edm::getRef(c, idx)]))
0052         addRef_(selected_, c, idx);
0053     }
0054   }
0055 
0056   static void fillPSetDescription(edm::ParameterSetDescription& desc) {
0057     desc.add<edm::InputTag>("var", edm::InputTag(""));
0058     Selector::fillPSetDescription(desc);
0059   }
0060 
0061 private:
0062   edm::EDGetTokenT<VarCollection> varToken_;
0063   container selected_;
0064   selector select_;
0065   RefAdder addRef_;
0066   friend struct reco::modules::AssociatedVariableCollectionSelectorEventSetupInit<AssociatedVariableCollectionSelector>;
0067 };
0068 
0069 #include "CommonTools/UtilAlgos/interface/EventSetupInitTrait.h"
0070 
0071 namespace reco {
0072   namespace modules {
0073     template <typename S>
0074     struct AssociatedVariableCollectionSelectorEventSetupInit {
0075       explicit AssociatedVariableCollectionSelectorEventSetupInit(edm::ConsumesCollector iC) : esi_(iC) {}
0076 
0077       void init(S& s, const edm::Event& evt, const edm::EventSetup& es) { esi_.init(s.select_, evt, es); }
0078       typedef typename EventSetupInit<typename S::selector>::type ESI;
0079       ESI esi_;
0080     };
0081 
0082     template <typename I, typename V, typename S, typename O, typename C, typename R>
0083     struct EventSetupInit<AssociatedVariableCollectionSelector<I, V, S, O, C, R> > {
0084       typedef AssociatedVariableCollectionSelectorEventSetupInit<AssociatedVariableCollectionSelector<I, V, S, O, C, R> >
0085           type;
0086     };
0087 
0088   }  // namespace modules
0089 }  // namespace reco
0090 
0091 #endif