Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-10-25 09:36:14

0001 #ifndef RecoAlgos_ObjectSelectorBase_h
0002 #define RecoAlgos_ObjectSelectorBase_h
0003 /** \class ObjectSelectorBase
0004  *
0005  * selects a subset of a collection.
0006  *
0007  * \author Luca Lista, INFN
0008  *
0009  * \version $Revision: 1.3 $
0010  *
0011  * $Id: ObjectSelectorBase.h,v 1.3 2010/02/20 20:55:27 wmtan Exp $
0012  *
0013  */
0014 
0015 #include "FWCore/Framework/interface/Event.h"
0016 #include "FWCore/Framework/interface/ConsumesCollector.h"
0017 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0018 #include "FWCore/Utilities/interface/InputTag.h"
0019 #include "CommonTools/UtilAlgos/interface/ParameterAdapter.h"
0020 #include <utility>
0021 #include <vector>
0022 #include <memory>
0023 #include <algorithm>
0024 
0025 template <typename Selector,
0026           typename OutputCollection,
0027           typename SizeSelector,
0028           typename PostProcessor,
0029           typename StoreManager,
0030           typename Base,
0031           typename Init>
0032 class ObjectSelectorBase : public Base {
0033 public:
0034   /// constructor
0035   // ObjectSelectorBase()=default;
0036   explicit ObjectSelectorBase(const edm::ParameterSet& cfg)
0037       : Base(cfg),
0038         srcToken_(
0039             this->template consumes<typename Selector::collection>(cfg.template getParameter<edm::InputTag>("src"))),
0040         filter_(false),
0041         selectorInit_(this->consumesCollector()),
0042         selector_(cfg, this->consumesCollector()),
0043         sizeSelector_(reco::modules::make<SizeSelector>(cfg)),
0044         postProcessor_(cfg, this->consumesCollector()) {
0045     const std::string filter("filter");
0046     std::vector<std::string> bools = cfg.template getParameterNamesForType<bool>();
0047     bool found = std::find(bools.begin(), bools.end(), filter) != bools.end();
0048     if (found)
0049       filter_ = cfg.template getParameter<bool>(filter);
0050     postProcessor_.init(*this);
0051   }
0052   /// destructor
0053   ~ObjectSelectorBase() override {}
0054 
0055 private:
0056   /// process one event
0057   bool filter(edm::Event& evt, const edm::EventSetup& es) override {
0058     selectorInit_.init(selector_, evt, es);
0059     edm::Handle<typename Selector::collection> source;
0060     evt.getByToken(srcToken_, source);
0061     StoreManager manager(source);
0062     selector_.select(source, evt, es);
0063     manager.cloneAndStore(selector_.begin(), selector_.end(), evt);
0064     bool result = (!filter_ || sizeSelector_(manager.size()));
0065     edm::OrphanHandle<OutputCollection> filtered = manager.put(evt);
0066     postProcessor_.process(filtered, evt);
0067     return result;
0068   }
0069   /// source collection label
0070   edm::EDGetTokenT<typename Selector::collection> srcToken_;
0071   /// filter event
0072   bool filter_;
0073   /// Object collection selector
0074   Init selectorInit_;
0075   Selector selector_;
0076   /// selected object collection size selector
0077   SizeSelector sizeSelector_;
0078   /// post processor
0079   PostProcessor postProcessor_;
0080 };
0081 
0082 #endif