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/AssociationVector.h"
0015 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0016 #include "FWCore/Framework/interface/Event.h"
0017 #include "DataFormats/Common/interface/Handle.h"
0018 #include <vector>
0019 
0020 namespace helper {
0021 
0022   template <typename Alg>
0023   struct NullIsolationAlgorithmSetup {
0024     using ESConsumesToken = int;
0025     static ESConsumesToken esConsumes(edm::ConsumesCollector) { return {}; }
0026     static void init(Alg&, const edm::EventSetup&, ESConsumesToken) {}
0027   };
0028 
0029   template <typename Alg>
0030   struct IsolationAlgorithmSetup {
0031     typedef NullIsolationAlgorithmSetup<Alg> type;
0032   };
0033 }  // namespace helper
0034 
0035 template <typename C1,
0036           typename C2,
0037           typename Alg,
0038           typename OutputCollection = edm::AssociationVector<edm::RefProd<C1>, std::vector<typename Alg::value_type> >,
0039           typename Setup = typename helper::IsolationAlgorithmSetup<Alg>::type>
0040 class IsolationProducer : public edm::stream::EDProducer<> {
0041 public:
0042   IsolationProducer(const edm::ParameterSet&);
0043   ~IsolationProducer() override;
0044 
0045 private:
0046   void produce(edm::Event&, const edm::EventSetup&) override;
0047   edm::EDGetTokenT<C1> srcToken_;
0048   edm::EDGetTokenT<C2> elementsToken_;
0049   Alg alg_;
0050   typename Setup::ESConsumesToken esToken_;
0051 };
0052 
0053 template <typename C1, typename C2, typename Alg, typename OutputCollection, typename Setup>
0054 IsolationProducer<C1, C2, Alg, OutputCollection, Setup>::IsolationProducer(const edm::ParameterSet& cfg)
0055     : srcToken_(consumes<C1>(cfg.template getParameter<edm::InputTag>("src"))),
0056       elementsToken_(consumes<C2>(cfg.template getParameter<edm::InputTag>("elements"))),
0057       alg_(reco::modules::make<Alg>(cfg)),
0058       esToken_(Setup::esConsumes(consumesCollector())) {
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, esToken_);
0075 
0076   typename OutputCollection::refprod_type ref(src);
0077   auto isolations = std::make_unique<OutputCollection>(ref);
0078 
0079   size_t i = 0;
0080   for (typename C1::const_iterator lep = src->begin(); lep != src->end(); ++lep) {
0081     typename Alg::value_type iso = alg_(*lep, *elements);
0082     isolations->setValue(i++, iso);
0083   }
0084   evt.put(std::move(isolations));
0085 }
0086 
0087 #endif