File indexing completed on 2024-04-06 12:01:15
0001 #ifndef CommonTools_UtilAlgos_AssociatedVariableCollectionSelector_h
0002 #define CommonTools_UtilAlgos_AssociatedVariableCollectionSelector_h
0003
0004
0005
0006
0007
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/Framework/interface/Event.h"
0014 #include "DataFormats/Common/interface/Handle.h"
0015 #include "CommonTools/UtilAlgos/interface/SelectionAdderTrait.h"
0016 #include "CommonTools/UtilAlgos/interface/SelectedOutputCollectionTrait.h"
0017 #include "CommonTools/UtilAlgos/interface/StoreContainerTrait.h"
0018 #include "CommonTools/UtilAlgos/interface/ParameterAdapter.h"
0019 #include "DataFormats/Common/interface/getRef.h"
0020
0021 namespace reco {
0022 namespace modules {
0023 template <typename S>
0024 struct AssociatedVariableCollectionSelectorEventSetupInit;
0025 }
0026 }
0027
0028 template <typename InputCollection,
0029 typename VarCollection,
0030 typename Selector,
0031 typename OutputCollection = typename ::helper::SelectedOutputCollectionTrait<InputCollection>::type,
0032 typename StoreContainer = typename ::helper::StoreContainerTrait<OutputCollection>::type,
0033 typename RefAdder = typename ::helper::SelectionAdderTrait<InputCollection, StoreContainer>::type>
0034 class AssociatedVariableCollectionSelector {
0035 public:
0036 typedef InputCollection collection;
0037 typedef StoreContainer container;
0038 typedef Selector selector;
0039 typedef typename container::const_iterator const_iterator;
0040 AssociatedVariableCollectionSelector(const edm::ParameterSet& cfg, edm::ConsumesCollector&& iC)
0041 : varToken_(iC.consumes<VarCollection>(cfg.template getParameter<edm::InputTag>("var"))),
0042 select_(reco::modules::make<Selector>(cfg, iC)) {}
0043 const_iterator begin() const { return selected_.begin(); }
0044 const_iterator end() const { return selected_.end(); }
0045 void select(const edm::Handle<InputCollection>& c, const edm::Event& evt, const edm::EventSetup&) {
0046 selected_.clear();
0047 edm::Handle<VarCollection> var;
0048 evt.getByToken(varToken_, var);
0049 for (size_t idx = 0; idx < c->size(); ++idx) {
0050 if (select_((*c)[idx], (*var)[edm::getRef(c, idx)]))
0051 addRef_(selected_, c, idx);
0052 }
0053 }
0054
0055 private:
0056 edm::EDGetTokenT<VarCollection> varToken_;
0057 container selected_;
0058 selector select_;
0059 RefAdder addRef_;
0060 friend struct reco::modules::AssociatedVariableCollectionSelectorEventSetupInit<AssociatedVariableCollectionSelector>;
0061 };
0062
0063 #include "CommonTools/UtilAlgos/interface/EventSetupInitTrait.h"
0064
0065 namespace reco {
0066 namespace modules {
0067 template <typename S>
0068 struct AssociatedVariableCollectionSelectorEventSetupInit {
0069 explicit AssociatedVariableCollectionSelectorEventSetupInit(edm::ConsumesCollector iC) : esi_(iC) {}
0070
0071 void init(S& s, const edm::Event& evt, const edm::EventSetup& es) { esi_.init(s.select_, evt, es); }
0072 typedef typename EventSetupInit<typename S::selector>::type ESI;
0073 ESI esi_;
0074 };
0075
0076 template <typename I, typename V, typename S, typename O, typename C, typename R>
0077 struct EventSetupInit<AssociatedVariableCollectionSelector<I, V, S, O, C, R> > {
0078 typedef AssociatedVariableCollectionSelectorEventSetupInit<AssociatedVariableCollectionSelector<I, V, S, O, C, R> >
0079 type;
0080 };
0081
0082 }
0083 }
0084
0085 #endif