Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:23:40

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 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0014 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0015 
0016 namespace edm {
0017 
0018   template <class T, class C>
0019   class FilterValueMapWrapper : public edm::stream::EDProducer<> {
0020   public:
0021     /// some convenient typedefs. Recall that C is a container class.
0022     typename C::iterator iterator;
0023     typename C::const_iterator const_iterator;
0024 
0025     /// default contructor. Declares the output (type "C") and the filter (of type T, operates on C::value_type)
0026     FilterValueMapWrapper(const edm::ParameterSet& cfg) : src_(consumes<C>(cfg.getParameter<edm::InputTag>("src"))) {
0027       filter_ = std::shared_ptr<T>(new T(cfg.getParameter<edm::ParameterSet>("filterParams")));
0028       produces<edm::ValueMap<int>>();
0029     }
0030     /// default destructor
0031     ~FilterValueMapWrapper() override {}
0032     /// everything which has to be done during the event loop. NOTE: We can't use the eventSetup in FWLite so ignore it
0033     void produce(edm::Event& event, const edm::EventSetup& eventSetup) override {
0034       // create a collection of the objects to put into the event
0035       auto objsToPut = std::make_unique<C>();
0036       // get the handle to the objects in the event.
0037       edm::Handle<C> h_c;
0038       event.getByToken(src_, h_c);
0039       std::vector<int> bitOut;
0040       bitOut.reserve(h_c->size());
0041       // loop through and add passing value_types to the output vector
0042       for (typename C::const_iterator ibegin = h_c->begin(), iend = h_c->end(), i = ibegin; i != iend; ++i) {
0043         bitOut.push_back((*filter_)(*i));
0044       }
0045       std::unique_ptr<edm::ValueMap<int>> o(new edm::ValueMap<int>());
0046       edm::ValueMap<int>::Filler filler(*o);
0047       filler.insert(h_c, bitOut.begin(), bitOut.end());
0048       filler.fill();
0049       event.put(std::move(o));
0050     }
0051 
0052     static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0053       edm::ParameterSetDescription desc;
0054 
0055       desc.add<edm::InputTag>("src")->setComment("input collection");
0056       desc.add<edm::ParameterSetDescription>("filterParams", T::getDescription());
0057 
0058       descriptions.addWithDefaultLabel(desc);
0059     }
0060 
0061   protected:
0062     /// InputTag of the input source
0063     edm::EDGetTokenT<C> src_;
0064     /// shared pointer to analysis class of type BasicAnalyzer
0065     std::shared_ptr<T> filter_;
0066   };
0067 
0068 }  // namespace edm
0069 
0070 #endif