Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:32:44

0001 #ifndef PhysicsTools_UtilAlgos_interface_EDFilterValueMapWrapper_h
0002 #define PhysicsTools_UtilAlgos_interface_EDFilterValueMapWrapper_h
0003 
0004 /**
0005  This is derived from EDFilterValueMapWrapper but rather than filtering it just stores a valuemap with the result
0006 */
0007 
0008 #include "DataFormats/Common/interface/ValueMap.h"
0009 #include "FWCore/Framework/interface/stream/EDProducer.h"
0010 #include "FWCore/Common/interface/EventBase.h"
0011 #include "FWCore/Framework/interface/Event.h"
0012 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0013 
0014 namespace edm {
0015 
0016   template <class T, class C>
0017   class FilterValueMapWrapper : public edm::stream::EDProducer<> {
0018   public:
0019     /// some convenient typedefs. Recall that C is a container class.
0020     typename C::iterator iterator;
0021     typename C::const_iterator const_iterator;
0022 
0023     /// default contructor. Declares the output (type "C") and the filter (of type T, operates on C::value_type)
0024     FilterValueMapWrapper(const edm::ParameterSet& cfg) : src_(consumes<C>(cfg.getParameter<edm::InputTag>("src"))) {
0025       filter_ = std::shared_ptr<T>(new T(cfg.getParameter<edm::ParameterSet>("filterParams")));
0026       produces<edm::ValueMap<int>>();
0027     }
0028     /// default destructor
0029     ~FilterValueMapWrapper() override {}
0030     /// everything which has to be done during the event loop. NOTE: We can't use the eventSetup in FWLite so ignore it
0031     void produce(edm::Event& event, const edm::EventSetup& eventSetup) override {
0032       // create a collection of the objects to put into the event
0033       auto objsToPut = std::make_unique<C>();
0034       // get the handle to the objects in the event.
0035       edm::Handle<C> h_c;
0036       event.getByToken(src_, h_c);
0037       std::vector<int> bitOut;
0038       // loop through and add passing value_types to the output vector
0039       for (typename C::const_iterator ibegin = h_c->begin(), iend = h_c->end(), i = ibegin; i != iend; ++i) {
0040         bitOut.push_back((*filter_)(*i));
0041       }
0042       std::unique_ptr<edm::ValueMap<int>> o(new edm::ValueMap<int>());
0043       edm::ValueMap<int>::Filler filler(*o);
0044       filler.insert(h_c, bitOut.begin(), bitOut.end());
0045       filler.fill();
0046       event.put(std::move(o));
0047     }
0048 
0049   protected:
0050     /// InputTag of the input source
0051     edm::EDGetTokenT<C> src_;
0052     /// shared pointer to analysis class of type BasicAnalyzer
0053     std::shared_ptr<T> filter_;
0054   };
0055 
0056 }  // namespace edm
0057 
0058 #endif