Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:22:47

0001 // system include files
0002 #include <memory>
0003 
0004 // user include files
0005 #include "FWCore/Framework/interface/Frameworkfwd.h"
0006 #include "FWCore/Framework/interface/global/EDProducer.h"
0007 
0008 #include "FWCore/Framework/interface/Event.h"
0009 #include "FWCore/Framework/interface/MakerMacros.h"
0010 
0011 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0012 #include "FWCore/Utilities/interface/StreamID.h"
0013 
0014 #include "DataFormats/Candidate/interface/Candidate.h"
0015 #include "DataFormats/PatCandidates/interface/GenericParticle.h"
0016 #include "DataFormats/Math/interface/LorentzVector.h"
0017 
0018 #include "DataFormats/PatCandidates/interface/Muon.h"
0019 #include "DataFormats/Common/interface/ValueMap.h"
0020 
0021 #include "DataFormats/Common/interface/Association.h"
0022 
0023 //
0024 // class declaration
0025 //
0026 
0027 class MuonFSRAssociator : public edm::global::EDProducer<> {
0028 public:
0029   explicit MuonFSRAssociator(const edm::ParameterSet& iConfig)
0030       :
0031 
0032         photons_{consumes<pat::GenericParticleCollection>(iConfig.getParameter<edm::InputTag>("photons"))},
0033         muons_{consumes<edm::View<reco::Muon>>(iConfig.getParameter<edm::InputTag>("muons"))}
0034 
0035   {
0036     produces<edm::Association<std::vector<pat::GenericParticle>>>();
0037     produces<edm::ValueMap<int>>("fsrIndex");
0038   }
0039 
0040   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0041     edm::ParameterSetDescription desc;
0042     desc.add<edm::InputTag>("photons")->setComment("FSR photon collection to associate with muons");
0043     desc.add<edm::InputTag>("muons")->setComment("collection of muons to associate with FSR photons");
0044 
0045     descriptions.addWithDefaultLabel(desc);
0046   }
0047   ~MuonFSRAssociator() override {}
0048 
0049 private:
0050   void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
0051 
0052   // ----------member data ---------------------------
0053   const edm::EDGetTokenT<pat::GenericParticleCollection> photons_;
0054   const edm::EDGetTokenT<edm::View<reco::Muon>> muons_;
0055 };
0056 
0057 void MuonFSRAssociator::produce(edm::StreamID streamID, edm::Event& iEvent, const edm::EventSetup& iSetup) const {
0058   using namespace std;
0059 
0060   edm::Handle<pat::GenericParticleCollection> photons;
0061   iEvent.getByToken(photons_, photons);
0062   edm::Handle<edm::View<reco::Muon>> muons;
0063   iEvent.getByToken(muons_, muons);
0064 
0065   std::vector<int> muonMapping(muons->size(), -1);
0066   // loop over all muons
0067   for (auto muon = muons->begin(); muon != muons->end(); ++muon) {
0068     for (auto iter_pho = photons->begin(); iter_pho != photons->end(); iter_pho++) {
0069       if (iter_pho->hasUserCand("associatedMuon") and
0070           iter_pho->userCand("associatedMuon") == reco::CandidatePtr(muons, muon - muons->begin()))
0071         muonMapping[muon - muons->begin()] = (iter_pho - photons->begin());
0072     }
0073   }
0074 
0075   auto muon2photon = std::make_unique<edm::Association<std::vector<pat::GenericParticle>>>(photons);
0076   edm::Association<std::vector<pat::GenericParticle>>::Filler muon2photonFiller(*muon2photon);
0077   muon2photonFiller.insert(muons, muonMapping.begin(), muonMapping.end());
0078   muon2photonFiller.fill();
0079   iEvent.put(std::move(muon2photon));
0080 
0081   std::unique_ptr<edm::ValueMap<int>> bareIdx(new edm::ValueMap<int>());
0082   edm::ValueMap<int>::Filler fillerBareIdx(*bareIdx);
0083   fillerBareIdx.insert(muons, muonMapping.begin(), muonMapping.end());
0084   fillerBareIdx.fill();
0085   iEvent.put(std::move(bareIdx), "fsrIndex");
0086 }
0087 
0088 //define this as a plug-in
0089 DEFINE_FWK_MODULE(MuonFSRAssociator);