Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:01:16

0001 #ifndef RecoAlgos_SortCollectionSelector_h
0002 #define RecoAlgos_SortCollectionSelector_h
0003 /** \class SortCollectionSelector
0004  *
0005  * selects the first N elements based on a sorting algorithm
0006  *
0007  * \author Luca Lista, INFN
0008  *
0009  * \version $Revision: 1.1 $
0010  *
0011  * $Id: SortCollectionSelector.h,v 1.1 2009/03/03 13:07:28 llista Exp $
0012  *
0013  */
0014 
0015 #include "FWCore/Framework/interface/ConsumesCollector.h"
0016 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0017 #include "CommonTools/UtilAlgos/interface/SelectionAdderTrait.h"
0018 #include "CommonTools/UtilAlgos/interface/StoreContainerTrait.h"
0019 #include "CommonTools/UtilAlgos/interface/ParameterAdapter.h"
0020 #include "CommonTools/UtilAlgos/interface/SelectedOutputCollectionTrait.h"
0021 #include <algorithm>
0022 #include <utility>
0023 
0024 namespace edm {
0025   class Event;
0026   class EventSetup;
0027 }  // namespace edm
0028 
0029 template <typename InputCollection,
0030           typename Comparator,
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 SortCollectionSelector {
0035 public:
0036   typedef InputCollection collection;
0037 
0038 private:
0039   typedef const typename InputCollection::value_type *reference;
0040   typedef std::pair<reference, size_t> pair;
0041   typedef StoreContainer container;
0042   typedef typename container::const_iterator const_iterator;
0043 
0044 public:
0045   SortCollectionSelector(const edm::ParameterSet &cfg, edm::ConsumesCollector &&iC)
0046       : compare_(Comparator()), maxNumber_(cfg.template getParameter<unsigned int>("maxNumber")) {}
0047   const_iterator begin() const { return selected_.begin(); }
0048   const_iterator end() const { return selected_.end(); }
0049   void select(const edm::Handle<InputCollection> &c, const edm::Event &, const edm::EventSetup &) {
0050     std::vector<pair> v;
0051     for (size_t idx = 0; idx < c->size(); ++idx)
0052       v.push_back(std::make_pair(&(*c)[idx], idx));
0053     std::sort(v.begin(), v.end(), compare_);
0054     selected_.clear();
0055     for (size_t i = 0; i < maxNumber_ && i < v.size(); ++i)
0056       addRef_(selected_, c, v[i].second);
0057   }
0058 
0059 private:
0060   struct PairComparator {
0061     PairComparator(const Comparator &cmp) : cmp_(cmp) {}
0062     bool operator()(const pair &t1, const pair &t2) const { return cmp_(*t1.first, *t2.first); }
0063     Comparator cmp_;
0064   };
0065   PairComparator compare_;
0066   unsigned int maxNumber_;
0067   StoreContainer selected_;
0068   RefAdder addRef_;
0069 };
0070 
0071 #endif