Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-05-02 05:10:04

0001 // -*- C++ -*-
0002 //
0003 // Package:    TauAnalysis/EmbeddingProducer
0004 // Class:      MuMuForEmbeddingSelector
0005 //
0006 /**\class MuMuForEmbeddingSelector MuMuForEmbeddingSelector.cc TauAnalysis/EmbeddingProducer/plugins/MuMuForEmbeddingSelector.cc
0007 
0008  Description: [one line class summary]
0009 
0010  Implementation:
0011      [Notes on implementation]
0012 */
0013 //
0014 // Original Author:  Artur Akhmetshin
0015 //         Created:  Mon, 13 Jun 2016 11:05:32 GMT
0016 //
0017 //
0018 
0019 // system include files
0020 #include <memory>
0021 
0022 // user include files
0023 #include "FWCore/Framework/interface/Frameworkfwd.h"
0024 #include "FWCore/Framework/interface/stream/EDProducer.h"
0025 
0026 #include "FWCore/Framework/interface/Event.h"
0027 #include "FWCore/Framework/interface/MakerMacros.h"
0028 
0029 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0030 #include "FWCore/Utilities/interface/StreamID.h"
0031 
0032 #include "DataFormats/Candidate/interface/CompositeCandidate.h"
0033 #include "DataFormats/PatCandidates/interface/Muon.h"
0034 
0035 #include "DataFormats/BeamSpot/interface/BeamSpot.h"
0036 #include "DataFormats/VertexReco/interface/Vertex.h"
0037 #include "DataFormats/VertexReco/interface/VertexFwd.h"
0038 
0039 #include "DataFormats/METReco/interface/MET.h"
0040 #include "DataFormats/PatCandidates/interface/MET.h"
0041 
0042 //
0043 // class declaration
0044 //
0045 
0046 class MuMuForEmbeddingSelector : public edm::stream::EDProducer<> {
0047 public:
0048   explicit MuMuForEmbeddingSelector(const edm::ParameterSet &);
0049 
0050   static void fillDescriptions(edm::ConfigurationDescriptions &descriptions);
0051 
0052 private:
0053   void produce(edm::Event &, const edm::EventSetup &) override;
0054 
0055   // ----------member data ---------------------------
0056   edm::EDGetTokenT<edm::View<reco::CompositeCandidate>> ZmumuCandidates_;
0057   edm::EDGetTokenT<reco::VertexCollection> theVertexLabel_;
0058   edm::EDGetTokenT<reco::BeamSpot> theBeamSpotLabel_;
0059   edm::EDGetTokenT<edm::View<pat::MET>> theMETLabel_;
0060   edm::EDGetTokenT<edm::View<pat::MET>> thePuppiMETLabel_;
0061   bool use_zmass = false;
0062   static constexpr double zmass = 91.1876;
0063 };
0064 
0065 //
0066 // constants, enums and typedefs
0067 //
0068 
0069 //
0070 // static data member definitions
0071 //
0072 
0073 //
0074 // constructors and destructor
0075 //
0076 MuMuForEmbeddingSelector::MuMuForEmbeddingSelector(const edm::ParameterSet &iConfig)
0077     : ZmumuCandidates_(consumes<edm::View<reco::CompositeCandidate>>(
0078           iConfig.getParameter<edm::InputTag>("ZmumuCandidatesCollection"))) {
0079   use_zmass = iConfig.getParameter<bool>("use_zmass");
0080   produces<edm::RefVector<pat::MuonCollection>>();
0081   produces<float>("oldMass");
0082   produces<float>("newMass");
0083   produces<float>("nPairCandidates");
0084   produces<bool>("isMediumLeadingMuon");
0085   produces<bool>("isTightLeadingMuon");
0086   produces<bool>("isMediumTrailingMuon");
0087   produces<bool>("isTightTrailingMuon");
0088   produces<float>("initialMETEt");
0089   produces<float>("initialMETphi");
0090   produces<float>("initialPuppiMETEt");
0091   produces<float>("initialPuppiMETphi");
0092   theVertexLabel_ = consumes<reco::VertexCollection>(iConfig.getParameter<edm::InputTag>("inputTagVertex"));
0093   theBeamSpotLabel_ = consumes<reco::BeamSpot>(iConfig.getParameter<edm::InputTag>("inputTagBeamSpot"));
0094   theMETLabel_ = consumes<edm::View<pat::MET>>(iConfig.getParameter<edm::InputTag>("Met"));
0095   thePuppiMETLabel_ = consumes<edm::View<pat::MET>>(iConfig.getParameter<edm::InputTag>("PuppiMet"));
0096   // now do what ever other initialization is needed
0097 }
0098 
0099 //
0100 // member functions
0101 //
0102 
0103 // ------------ method called to produce the data  ------------
0104 void MuMuForEmbeddingSelector::produce(edm::Event &iEvent, const edm::EventSetup &iSetup) {
0105   using namespace edm;
0106   edm::Handle<edm::View<reco::CompositeCandidate>> ZmumuCandidatesHandle;
0107   iEvent.getByToken(ZmumuCandidates_, ZmumuCandidatesHandle);
0108   edm::View<reco::CompositeCandidate> ZmumuCandidates = *ZmumuCandidatesHandle;
0109   const reco::CompositeCandidate *chosenZCand = nullptr;
0110   const reco::CompositeCandidate *chosenZCand_zmass = nullptr;
0111   const reco::CompositeCandidate *chosenZCand_largest = nullptr;
0112   double massDifference = 9999;
0113   edm::Handle<reco::BeamSpot> beamSpot;
0114   iEvent.getByToken(theBeamSpotLabel_, beamSpot);
0115   edm::Handle<reco::VertexCollection> vertex;
0116   iEvent.getByToken(theVertexLabel_, vertex);
0117   edm::Handle<edm::View<pat::MET>> met;
0118   iEvent.getByToken(theMETLabel_, met);
0119   edm::Handle<edm::View<pat::MET>> puppimet;
0120   iEvent.getByToken(thePuppiMETLabel_, puppimet);
0121   // get primary vertex
0122   reco::Vertex::Point posVtx;
0123   reco::Vertex::Error errVtx;
0124   for (const auto &vtx : *vertex) {
0125     if (vtx.isValid() && !vtx.isFake()) {
0126       posVtx = vtx.position();
0127       errVtx = vtx.error();
0128       break;
0129     }
0130   }
0131   reco::Vertex primaryVertex(posVtx, errVtx);
0132 
0133   for (edm::View<reco::CompositeCandidate>::const_iterator iZCand = ZmumuCandidates.begin();
0134        iZCand != ZmumuCandidates.end();
0135        ++iZCand) {
0136     if (std::abs(zmass - iZCand->mass()) < massDifference) {
0137       massDifference = std::abs(zmass - iZCand->mass());
0138       chosenZCand_zmass = &(*iZCand);
0139     }
0140   }
0141   for (edm::View<reco::CompositeCandidate>::const_iterator iZCand = ZmumuCandidates.begin();
0142        iZCand != ZmumuCandidates.end();
0143        ++iZCand) {
0144     if (chosenZCand_largest == nullptr) {
0145       chosenZCand_largest = &(*iZCand);
0146     } else {
0147       if (iZCand->mass() > chosenZCand_largest->mass()) {
0148         chosenZCand_largest = &(*iZCand);
0149       }
0150     }
0151   }
0152   if (use_zmass) {
0153     chosenZCand = chosenZCand_zmass;
0154   } else {
0155     chosenZCand = chosenZCand_largest;
0156   }
0157 
0158   std::unique_ptr<edm::RefVector<pat::MuonCollection>> prod(new edm::RefVector<pat::MuonCollection>());
0159   prod->reserve(2);
0160   prod->push_back(chosenZCand->daughter(0)->masterClone().castTo<pat::MuonRef>());
0161   prod->push_back(chosenZCand->daughter(1)->masterClone().castTo<pat::MuonRef>());
0162   iEvent.put(std::move(prod));
0163   iEvent.put(std::make_unique<float>(chosenZCand_zmass->mass()), "oldMass");
0164   iEvent.put(std::make_unique<float>(chosenZCand_largest->mass()), "newMass");
0165   iEvent.put(std::make_unique<float>(ZmumuCandidates.size()), "nPairCandidates");
0166   iEvent.put(std::make_unique<bool>(chosenZCand->daughter(0)->masterClone().castTo<pat::MuonRef>()->isMediumMuon()),
0167              "isMediumLeadingMuon");
0168   iEvent.put(std::make_unique<bool>(
0169                  chosenZCand->daughter(0)->masterClone().castTo<pat::MuonRef>()->isTightMuon(primaryVertex)),
0170              "isTightLeadingMuon");
0171   iEvent.put(std::make_unique<bool>(chosenZCand->daughter(1)->masterClone().castTo<pat::MuonRef>()->isMediumMuon()),
0172              "isMediumTrailingMuon");
0173   iEvent.put(std::make_unique<bool>(
0174                  chosenZCand->daughter(1)->masterClone().castTo<pat::MuonRef>()->isTightMuon(primaryVertex)),
0175              "isTightTrailingMuon");
0176   iEvent.put(std::make_unique<float>(met->at(0).et()), "initialMETEt");
0177   iEvent.put(std::make_unique<float>(met->at(0).phi()), "initialMETphi");
0178   iEvent.put(std::make_unique<float>(puppimet->at(0).et()), "initialPuppiMETEt");
0179   iEvent.put(std::make_unique<float>(puppimet->at(0).phi()), "initialPuppiMETphi");
0180 }
0181 
0182 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0183 void MuMuForEmbeddingSelector::fillDescriptions(edm::ConfigurationDescriptions &descriptions) {
0184   // The following says we do not know what parameters are allowed so do no validation
0185   //  Please change this to state exactly what you do use, even if it is no parameters
0186   edm::ParameterSetDescription desc;
0187   desc.setUnknown();
0188   descriptions.addDefault(desc);
0189 }
0190 
0191 // define this as a plug-in
0192 DEFINE_FWK_MODULE(MuMuForEmbeddingSelector);