File indexing completed on 2025-02-05 23:51:10
0001 #ifndef RecoAlgos_SortCollectionSelector_h
0002 #define RecoAlgos_SortCollectionSelector_h
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #include "FWCore/Framework/interface/ConsumesCollector.h"
0016 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0017 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0018 #include "CommonTools/UtilAlgos/interface/SelectionAdderTrait.h"
0019 #include "CommonTools/UtilAlgos/interface/StoreContainerTrait.h"
0020 #include "CommonTools/UtilAlgos/interface/ParameterAdapter.h"
0021 #include "CommonTools/UtilAlgos/interface/SelectedOutputCollectionTrait.h"
0022 #include <algorithm>
0023 #include <utility>
0024
0025 namespace edm {
0026 class Event;
0027 class EventSetup;
0028 }
0029
0030 template <typename InputCollection,
0031 typename Comparator,
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 SortCollectionSelector {
0036 public:
0037 typedef InputCollection collection;
0038
0039 private:
0040 typedef const typename InputCollection::value_type *reference;
0041 typedef std::pair<reference, size_t> pair;
0042 typedef StoreContainer container;
0043 typedef typename container::const_iterator const_iterator;
0044
0045 public:
0046 SortCollectionSelector(const edm::ParameterSet &cfg, edm::ConsumesCollector &&iC)
0047 : compare_(Comparator()), maxNumber_(cfg.template getParameter<unsigned int>("maxNumber")) {}
0048 const_iterator begin() const { return selected_.begin(); }
0049 const_iterator end() const { return selected_.end(); }
0050 void select(const edm::Handle<InputCollection> &c, const edm::Event &, const edm::EventSetup &) {
0051 std::vector<pair> v;
0052 for (size_t idx = 0; idx < c->size(); ++idx)
0053 v.push_back(std::make_pair(&(*c)[idx], idx));
0054 std::sort(v.begin(), v.end(), compare_);
0055 selected_.clear();
0056 for (size_t i = 0; i < maxNumber_ && i < v.size(); ++i)
0057 addRef_(selected_, c, v[i].second);
0058 }
0059
0060 static void fillPSetDescription(edm::ParameterSetDescription &desc) { desc.add<unsigned int>("maxNumber", 1); }
0061
0062 private:
0063 struct PairComparator {
0064 PairComparator(const Comparator &cmp) : cmp_(cmp) {}
0065 bool operator()(const pair &t1, const pair &t2) const { return cmp_(*t1.first, *t2.first); }
0066 Comparator cmp_;
0067 };
0068 PairComparator compare_;
0069 unsigned int maxNumber_;
0070 StoreContainer selected_;
0071 RefAdder addRef_;
0072 };
0073
0074 #endif