Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-07-10 02:34:23

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         throwOnMissing_(cfg.template getUntrackedParameter<bool>("throwOnMissing", true)),
0042         selectorInit_(this->consumesCollector()),
0043         selector_(cfg, this->consumesCollector()),
0044         sizeSelector_(reco::modules::make<SizeSelector>(cfg)),
0045         postProcessor_(cfg, this->consumesCollector()) {
0046     const std::string filter("filter");
0047     std::vector<std::string> bools = cfg.template getParameterNamesForType<bool>();
0048     bool found = std::find(bools.begin(), bools.end(), filter) != bools.end();
0049     if (found)
0050       filter_ = cfg.template getParameter<bool>(filter);
0051     postProcessor_.init(*this);
0052   }
0053   /// destructor
0054   ~ObjectSelectorBase() override {}
0055 
0056 private:
0057   /// process one event
0058   bool filter(edm::Event& evt, const edm::EventSetup& es) override {
0059     selectorInit_.init(selector_, evt, es);
0060     edm::Handle<typename Selector::collection> source;
0061     if (!throwOnMissing_ && !source.isValid()) {
0062       return !filter_;
0063     }
0064     evt.getByToken(srcToken_, source);
0065     StoreManager manager(source);
0066     selector_.select(source, evt, es);
0067     manager.cloneAndStore(selector_.begin(), selector_.end(), evt);
0068     bool result = (!filter_ || sizeSelector_(manager.size()));
0069     edm::OrphanHandle<OutputCollection> filtered = manager.put(evt);
0070     postProcessor_.process(filtered, evt);
0071     return result;
0072   }
0073   /// source collection label
0074   edm::EDGetTokenT<typename Selector::collection> srcToken_;
0075   /// filter event
0076   bool filter_;
0077   /// trhow on missing
0078   bool throwOnMissing_;
0079   /// Object collection selector
0080   Init selectorInit_;
0081   Selector selector_;
0082   /// selected object collection size selector
0083   SizeSelector sizeSelector_;
0084   /// post processor
0085   PostProcessor postProcessor_;
0086 };
0087 
0088 #endif