Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 // -*- C++ -*-
0002 //
0003 // Package:    PhysicsTools/NanoAOD
0004 // Class:      EGMEnergyVarProducer
0005 //
0006 /**\class EGMEnergyVarProducer EGMEnergyVarProducer.cc PhysicsTools/NanoAOD/plugins/EGMEnergyVarProducer.cc
0007  Description: [one line class summary]
0008  Implementation:
0009      [Notes on implementation]
0010 */
0011 //
0012 // Original Author:  Emanuele Di Marco
0013 //         Created:  Wed, 06 Sep 2017 12:34:38 GMT
0014 //
0015 //
0016 
0017 // system include files
0018 #include <memory>
0019 
0020 // user include files
0021 #include "FWCore/Framework/interface/Frameworkfwd.h"
0022 #include "FWCore/Framework/interface/global/EDProducer.h"
0023 
0024 #include "FWCore/Framework/interface/Event.h"
0025 #include "FWCore/Framework/interface/MakerMacros.h"
0026 
0027 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0028 #include "FWCore/Utilities/interface/StreamID.h"
0029 
0030 #include "DataFormats/PatCandidates/interface/Electron.h"
0031 #include "DataFormats/PatCandidates/interface/Photon.h"
0032 
0033 #include "TLorentzVector.h"
0034 #include "DataFormats/Common/interface/View.h"
0035 
0036 #include "PhysicsTools/NanoAOD/interface/MatchingUtils.h"
0037 
0038 //
0039 // class declaration
0040 //
0041 
0042 template <typename T>
0043 class EGMEnergyVarProducer : public edm::global::EDProducer<> {
0044 public:
0045   explicit EGMEnergyVarProducer(const edm::ParameterSet& iConfig)
0046       : srcRaw_(consumes<edm::View<T>>(iConfig.getParameter<edm::InputTag>("srcRaw"))),
0047         srcCorr_(consumes<edm::View<T>>(iConfig.getParameter<edm::InputTag>("srcCorr"))) {
0048     produces<edm::ValueMap<float>>("eCorr");
0049   }
0050   ~EGMEnergyVarProducer() override{};
0051 
0052   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0053 
0054 private:
0055   void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
0056 
0057   // ----------member data ---------------------------
0058 
0059   edm::EDGetTokenT<edm::View<T>> srcRaw_;
0060   edm::EDGetTokenT<edm::View<T>> srcCorr_;
0061 };
0062 
0063 //
0064 // constants, enums and typedefs
0065 //
0066 
0067 //
0068 // static data member definitions
0069 //
0070 
0071 //
0072 // member functions
0073 //
0074 
0075 // ------------ method called to produce the data  ------------
0076 template <typename T>
0077 void EGMEnergyVarProducer<T>::produce(edm::StreamID streamID, edm::Event& iEvent, const edm::EventSetup& iSetup) const {
0078   edm::Handle<edm::View<T>> srcRaw;
0079   iEvent.getByToken(srcRaw_, srcRaw);
0080   edm::Handle<edm::View<T>> srcCorr;
0081   iEvent.getByToken(srcCorr_, srcCorr);
0082 
0083   unsigned nSrcRaw = srcRaw->size();
0084   unsigned nSrcCorr = srcCorr->size();
0085 
0086   std::vector<float> eCorr(nSrcCorr, -1);
0087 
0088   for (unsigned int ir = 0; ir < nSrcRaw; ir++) {
0089     auto egm_raw = srcRaw->ptrAt(ir);
0090     for (unsigned int ic = 0; ic < nSrcCorr; ic++) {
0091       auto egm_corr = srcCorr->ptrAt(ic);
0092       if (matchByCommonParentSuperClusterRef(*egm_raw, *egm_corr)) {
0093         eCorr[ir] = egm_corr->energy() / egm_raw->energy();
0094         break;
0095       }
0096     }
0097   }
0098 
0099   std::unique_ptr<edm::ValueMap<float>> eCorrV(new edm::ValueMap<float>());
0100   edm::ValueMap<float>::Filler fillerCorr(*eCorrV);
0101   fillerCorr.insert(srcRaw, eCorr.begin(), eCorr.end());
0102   fillerCorr.fill();
0103   iEvent.put(std::move(eCorrV), "eCorr");
0104 }
0105 
0106 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0107 template <typename T>
0108 void EGMEnergyVarProducer<T>::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0109   edm::ParameterSetDescription desc;
0110   desc.add<edm::InputTag>("srcRaw")->setComment("input raw physics object collection");
0111   desc.add<edm::InputTag>("srcCorr")->setComment("input corrected physics object collection");
0112   std::string modname;
0113   if (typeid(T) == typeid(pat::Electron))
0114     modname += "Electron";
0115   else if (typeid(T) == typeid(pat::Photon))
0116     modname += "Photon";
0117   modname += "EnergyVarProducer";
0118   descriptions.add(modname, desc);
0119 }
0120 
0121 typedef EGMEnergyVarProducer<pat::Electron> ElectronEnergyVarProducer;
0122 typedef EGMEnergyVarProducer<pat::Photon> PhotonEnergyVarProducer;
0123 
0124 //define this as a plug-in
0125 DEFINE_FWK_MODULE(ElectronEnergyVarProducer);
0126 DEFINE_FWK_MODULE(PhotonEnergyVarProducer);