Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-05-10 22:24:50

0001 #ifndef IsolationAlgos_IsolationProducer_h
0002 #define IsolationAlgos_IsolationProducer_h
0003 /* \class IsolationProducer<C1, C2, Algo>
0004  *
0005  * \author Francesco Fabozzi, INFN
0006  *
0007  * template class to store isolation
0008  *
0009  */
0010 #include "CommonTools/UtilAlgos/interface/ParameterAdapter.h"
0011 #include "FWCore/Framework/interface/stream/EDProducer.h"
0012 #include "FWCore/ParameterSet/interface/ParameterSetfwd.h"
0013 #include "FWCore/Utilities/interface/InputTag.h"
0014 #include "DataFormats/Common/interface/ValueMap.h"
0015 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0016 #include "FWCore/Framework/interface/Event.h"
0017 #include "DataFormats/Common/interface/Handle.h"
0018 #include "CommonTools/UtilAlgos/interface/MasterCollectionHelper.h"
0019 #include <vector>
0020 
0021 namespace helper {
0022 
0023   template <typename Alg>
0024   struct NullIsolationAlgorithmSetup {
0025     static void init(Alg&, const edm::EventSetup&) {}
0026   };
0027 
0028   template <typename Alg>
0029   struct IsolationAlgorithmSetup {
0030     typedef NullIsolationAlgorithmSetup<Alg> type;
0031   };
0032 }  // namespace helper
0033 
0034 namespace reco {
0035   namespace modulesNew {
0036 
0037     template <typename C1,
0038               typename C2,
0039               typename Alg,
0040               typename OutputCollection = edm::ValueMap<float>,
0041               typename Setup = typename helper::IsolationAlgorithmSetup<Alg>::type>
0042     class IsolationProducer : public edm::stream::EDProducer<> {
0043     public:
0044       IsolationProducer(const edm::ParameterSet&);
0045       ~IsolationProducer() override;
0046 
0047     private:
0048       void produce(edm::Event&, const edm::EventSetup&) override;
0049       edm::EDGetTokenT<C1> srcToken_;
0050       edm::EDGetTokenT<C2> elementsToken_;
0051       Alg alg_;
0052     };
0053 
0054     template <typename C1, typename C2, typename Alg, typename OutputCollection, typename Setup>
0055     IsolationProducer<C1, C2, Alg, OutputCollection, Setup>::IsolationProducer(const edm::ParameterSet& cfg)
0056         : srcToken_(consumes<C1>(cfg.template getParameter<edm::InputTag>("src"))),
0057           elementsToken_(consumes<C2>(cfg.template getParameter<edm::InputTag>("elements"))),
0058           alg_(reco::modules::make<Alg>(cfg)) {
0059       produces<OutputCollection>();
0060     }
0061 
0062     template <typename C1, typename C2, typename Alg, typename OutputCollection, typename Setup>
0063     IsolationProducer<C1, C2, Alg, OutputCollection, Setup>::~IsolationProducer() {}
0064 
0065     template <typename C1, typename C2, typename Alg, typename OutputCollection, typename Setup>
0066     void IsolationProducer<C1, C2, Alg, OutputCollection, Setup>::produce(edm::Event& evt, const edm::EventSetup& es) {
0067       using namespace edm;
0068       using namespace std;
0069       Handle<C1> src;
0070       Handle<C2> elements;
0071       evt.getByToken(srcToken_, src);
0072       evt.getByToken(elementsToken_, elements);
0073 
0074       Setup::init(alg_, es);
0075 
0076       ::helper::MasterCollection<C1> master(src, evt);
0077       auto isolations = std::make_unique<OutputCollection>();
0078       if (!src->empty()) {
0079         typename OutputCollection::Filler filler(*isolations);
0080         vector<double> iso(master.size(), -1);
0081         size_t i = 0;
0082         for (typename C1::const_iterator lep = src->begin(); lep != src->end(); ++lep)
0083           iso[master.index(i++)] = alg_(*lep, *elements);
0084         filler.insert(master.get(), iso.begin(), iso.end());
0085         filler.fill();
0086       }
0087       evt.put(std::move(isolations));
0088     }
0089 
0090   }  // namespace modulesNew
0091 }  // namespace reco
0092 
0093 #endif