Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-05-10 05:44:09

0001 #ifndef PhysicsTools_TagAndProbe_interface_AnythingToValueMap_h
0002 #define PhysicsTools_TagAndProbe_interface_AnythingToValueMap_h
0003 
0004 #include "FWCore/Framework/interface/stream/EDProducer.h"
0005 #include "FWCore/Framework/interface/Event.h"
0006 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0007 #include "FWCore/Utilities/interface/InputTag.h"
0008 
0009 #include "DataFormats/Common/interface/View.h"
0010 #include "DataFormats/Common/interface/RefToBase.h"
0011 #include "DataFormats/Common/interface/ValueMap.h"
0012 
0013 namespace pat {
0014   namespace helper {
0015 
0016     template <class Adaptor,
0017               class Collection = typename Adaptor::Collection,
0018               typename value_type = typename Adaptor::value_type>
0019     class AnythingToValueMap : public edm::stream::EDProducer<> {
0020     public:
0021       typedef typename edm::ValueMap<value_type> Map;
0022       typedef typename Map::Filler MapFiller;
0023       explicit AnythingToValueMap(const edm::ParameterSet& iConfig)
0024           : failSilently_(iConfig.getUntrackedParameter<bool>("failSilently", false)),
0025             src_(consumes<Collection>(iConfig.getParameter<edm::InputTag>("src"))),
0026             adaptor_(iConfig, consumesCollector()) {
0027         produces<Map>(adaptor_.label());
0028       }
0029       ~AnythingToValueMap() override {}
0030 
0031       void produce(edm::Event& iEvent, const edm::EventSetup& iSetup) override;
0032 
0033     private:
0034       bool failSilently_;
0035       edm::EDGetTokenT<Collection> src_;
0036       Adaptor adaptor_;
0037     };
0038 
0039     template <class Adaptor, class Collection, typename value_type>
0040     void AnythingToValueMap<Adaptor, Collection, value_type>::produce(edm::Event& iEvent,
0041                                                                       const edm::EventSetup& iSetup) {
0042       edm::Handle<Collection> handle;
0043       iEvent.getByToken(src_, handle);
0044       if (handle.failedToGet() && failSilently_)
0045         return;
0046 
0047       bool adaptorOk = adaptor_.init(iEvent);
0048       if ((!adaptorOk) && failSilently_)
0049         return;
0050 
0051       std::vector<value_type> ret;
0052       ret.reserve(handle->size());
0053 
0054       adaptor_.run(*handle, ret);
0055 
0056       auto map = std::make_unique<Map>();
0057       MapFiller filler(*map);
0058       filler.insert(handle, ret.begin(), ret.end());
0059       filler.fill();
0060       iEvent.put(std::move(map), adaptor_.label());
0061     }
0062 
0063     template <class Adaptor,
0064               class Collection = typename Adaptor::Collection,
0065               typename value_type = typename Adaptor::value_type>
0066     class ManyThingsToValueMaps : public edm::stream::EDProducer<> {
0067     public:
0068       typedef typename edm::ValueMap<value_type> Map;
0069       typedef typename Map::Filler MapFiller;
0070       explicit ManyThingsToValueMaps(const edm::ParameterSet& iConfig)
0071           : failSilently_(iConfig.getUntrackedParameter<bool>("failSilently", false)),
0072             src_(consumes<Collection>(iConfig.getParameter<edm::InputTag>("collection"))),
0073             inputs_(iConfig.getParameter<std::vector<edm::InputTag> >("associations")) {
0074         for (std::vector<edm::InputTag>::const_iterator it = inputs_.begin(), ed = inputs_.end(); it != ed; ++it) {
0075           adaptors_.push_back(Adaptor(*it, iConfig, consumesCollector()));
0076           produces<Map>(adaptors_.back().label());
0077         }
0078       }
0079       ~ManyThingsToValueMaps() override {}
0080 
0081       void produce(edm::Event& iEvent, const edm::EventSetup& iSetup) override;
0082 
0083     private:
0084       bool failSilently_;
0085       edm::EDGetTokenT<Collection> src_;
0086       std::vector<edm::InputTag> inputs_;
0087       std::vector<Adaptor> adaptors_;
0088     };
0089 
0090     template <class Adaptor, class Collection, typename value_type>
0091     void ManyThingsToValueMaps<Adaptor, Collection, value_type>::produce(edm::Event& iEvent,
0092                                                                          const edm::EventSetup& iSetup) {
0093       edm::Handle<Collection> handle;
0094       iEvent.getByToken(src_, handle);
0095       if (handle.failedToGet() && failSilently_)
0096         return;
0097 
0098       std::vector<value_type> ret;
0099       ret.reserve(handle->size());
0100 
0101       for (typename std::vector<Adaptor>::iterator it = adaptors_.begin(), ed = adaptors_.end(); it != ed; ++it) {
0102         ret.clear();
0103         if (it->run(iEvent, *handle, ret)) {
0104           auto map = std::make_unique<Map>();
0105           MapFiller filler(*map);
0106           filler.insert(handle, ret.begin(), ret.end());
0107           filler.fill();
0108           iEvent.put(std::move(map), it->label());
0109         } else {
0110           if (!failSilently_)
0111             throw cms::Exception("ManyThingsToValueMaps") << "Error in adapter " << it->label() << "\n";
0112         }
0113       }
0114     }
0115 
0116   }  // namespace helper
0117 }  // namespace pat
0118 
0119 #endif