Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-08-12 22:40:29

0001 #ifndef MUSCLEFITMUONSELECTOR
0002 #define MUSCLEFITMUONSELECTOR
0003 
0004 #include "FWCore/Framework/interface/Frameworkfwd.h"
0005 #include "FWCore/Framework/interface/Event.h"
0006 #include "FWCore/Framework/interface/ConsumesCollector.h"
0007 #include "DataFormats/Candidate/interface/LeafCandidate.h"
0008 #include "DataFormats/PatCandidates/interface/CompositeCandidate.h"
0009 #include "DataFormats/PatCandidates/interface/CompositeCandidate.h"
0010 #include "DataFormats/PatCandidates/interface/Muon.h"
0011 #include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
0012 #include "SimDataFormats/Track/interface/SimTrackContainer.h"
0013 #include "FWCore/Utilities/interface/InputTag.h"
0014 #include "MuonAnalysis/MomentumScaleCalibration/interface/GenMuonPair.h"
0015 #include "MuonAnalysis/MomentumScaleCalibration/interface/Muon.h"
0016 
0017 #include "HepMC/GenParticle.h"
0018 #include "HepMC/GenEvent.h"
0019 
0020 #include "MuScleFitPlotter.h"
0021 
0022 #include <vector>
0023 
0024 /**
0025 * This class is used to perform the selection of muon pairs in MuScleFit. <br>
0026 * It receives the event and returns a muon collection containing all the selected muons.
0027 */
0028 typedef reco::Particle::LorentzVector lorentzVector;
0029 
0030 class MuScleFitMuonSelector {
0031 public:
0032   MuScleFitMuonSelector(edm::ConsumesCollector& iC,
0033                         const edm::InputTag& muonLabel,
0034                         const int muonType,
0035                         const bool PATmuons,
0036                         const std::vector<int>& resfind,
0037                         const bool speedup,
0038                         const std::string& genParticlesName,
0039                         const bool compareToSimTracks,
0040                         const edm::InputTag& simTracksCollectionName,
0041                         const bool sherpa,
0042                         const bool debug)
0043       : muonLabel_(muonLabel),
0044         muonType_(muonType),
0045         PATmuons_(PATmuons),
0046         resfind_(resfind),
0047         speedup_(speedup),
0048         genParticlesName_(genParticlesName),
0049         compareToSimTracks_(compareToSimTracks),
0050         simTracksCollectionName_(simTracksCollectionName),
0051         sherpa_(sherpa),
0052         debug_(debug),
0053         onia2MuMuToken_(iC.consumes<pat::CompositeCandidateCollection>(edm::InputTag("onia2MuMuPatTrkTrk"))),
0054         trackCollectionToken_(iC.consumes<reco::TrackCollection>(muonLabel_)),
0055         patMuonToken_(iC.consumes<pat::MuonCollection>(muonLabel_)),
0056         recoMuonToken_(iC.consumes<reco::MuonCollection>(muonLabel_)),
0057         caloMuonToken_(iC.consumes<reco::CaloMuonCollection>(muonLabel_)),
0058         evtMCToken_(iC.consumes<edm::HepMCProduct>(genParticlesName_)),
0059         genParticleToken_(iC.consumes<reco::GenParticleCollection>(genParticlesName_)),
0060         simTrackToken_(iC.consumes<edm::SimTrackContainer>(simTracksCollectionName_)) {}
0061   ~MuScleFitMuonSelector() = default;
0062 
0063   //Method to get the muon after FSR (status 1 muon in PYTHIA6) starting from status 3 muon which is daughter of the Z
0064   const reco::Candidate* getStatus1Muon(const reco::Candidate* status3Muon);
0065 
0066   //Method to get the muon before FSR (status 3 muon in PYTHIA6) starting from status 3 muon which is daughter of the Z
0067   const reco::Candidate* getStatus3Muon(const reco::Candidate* status3Muon);
0068 
0069   /// Main method used to select muons of type specified by muonType_ from the collection specified by muonLabel_ and PATmuons_
0070   void selectMuons(const edm::Event& event,
0071                    std::vector<MuScleFitMuon>& muons,
0072                    std::vector<GenMuonPair>& genPair,
0073                    std::vector<std::pair<lorentzVector, lorentzVector> >& simPair,
0074                    MuScleFitPlotter* plotter);
0075 
0076 protected:
0077   /// Apply the Onia cuts to select globalMuons
0078   bool selGlobalMuon(const pat::Muon* aMuon);
0079   /// Apply the Onia cuts to select trackerMuons
0080   bool selTrackerMuon(const pat::Muon* aMuon);
0081 
0082   // Generator and simulation level information
0083   GenMuonPair findGenMuFromRes(const reco::GenParticleCollection* genParticles);
0084   GenMuonPair findGenMuFromRes(const edm::HepMCProduct* evtMC);
0085   std::pair<lorentzVector, lorentzVector> findSimMuFromRes(const edm::Handle<edm::HepMCProduct>& evtMC,
0086                                                            const edm::Handle<edm::SimTrackContainer>& simTracks);
0087   void selectGeneratedMuons(const edm::Handle<pat::CompositeCandidateCollection>& collAll,
0088                             const std::vector<const pat::Muon*>& collMuSel,
0089                             std::vector<GenMuonPair>& genPair,
0090                             MuScleFitPlotter* plotter);
0091   void selectGenSimMuons(const edm::Event& event,
0092                          std::vector<GenMuonPair>& genPair,
0093                          std::vector<std::pair<lorentzVector, lorentzVector> >& simPair,
0094                          MuScleFitPlotter* plotter);
0095   // void selectGeneratedMuons(const edm::Event & event, std::vector<std::pair<lorentzVector,lorentzVector> > & genPair);
0096   void selectSimulatedMuons(const edm::Event& event,
0097                             const bool ifHepMC,
0098                             edm::Handle<edm::HepMCProduct> evtMC,
0099                             std::vector<std::pair<lorentzVector, lorentzVector> >& simPair,
0100                             MuScleFitPlotter* plotter);
0101 
0102   /// Template function used to convert the muon collection to a vector of reco::LeafCandidate
0103   template <typename T>
0104   std::vector<MuScleFitMuon> fillMuonCollection(const std::vector<T>& tracks) {
0105     std::vector<MuScleFitMuon> muons;
0106     typename std::vector<T>::const_iterator track;
0107     for (track = tracks.begin(); track != tracks.end(); ++track) {
0108       reco::Particle::LorentzVector mu;
0109       mu = reco::Particle::LorentzVector(track->px(), track->py(), track->pz(), sqrt(track->p() * track->p() + mMu2));
0110 
0111       Double_t hitsTk(0), hitsMuon(0), ptError(0);
0112       if (const reco::Muon* myMu = dynamic_cast<const reco::Muon*>(&(*track))) {
0113         hitsTk = myMu->innerTrack()->hitPattern().numberOfValidTrackerHits();
0114         hitsMuon = myMu->innerTrack()->hitPattern().numberOfValidMuonHits();
0115         ptError = myMu->innerTrack()->ptError();
0116       } else if (const pat::Muon* myMu = dynamic_cast<const pat::Muon*>(&(*track))) {
0117         hitsTk = myMu->innerTrack()->hitPattern().numberOfValidTrackerHits();
0118         hitsMuon = myMu->innerTrack()->hitPattern().numberOfValidMuonHits();
0119         ptError = myMu->innerTrack()->ptError();
0120       } else if (const reco::Track* myMu = dynamic_cast<const reco::Track*>(&(*track))) {
0121         hitsTk = myMu->hitPattern().numberOfValidTrackerHits();
0122         hitsMuon = myMu->hitPattern().numberOfValidMuonHits();
0123         ptError = myMu->ptError();
0124       }
0125 
0126       MuScleFitMuon muon(mu, track->charge(), ptError, hitsTk, hitsMuon);
0127 
0128       if (debug_ > 0) {
0129         std::cout << "[MuScleFitMuonSelector::fillMuonCollection] after MuScleFitMuon initialization" << std::endl;
0130         std::cout << "  muon = " << muon << std::endl;
0131       }
0132 
0133       muons.push_back(muon);
0134     }
0135     return muons;
0136   }
0137 
0138   /// Template function used to extract the selected muon type from the muon collection
0139   template <typename T>
0140   void takeSelectedMuonType(const T& muon, std::vector<reco::Track>& tracks) {
0141     // std::cout<<"muon "<<muon->isGlobalMuon()<<muon->isStandAloneMuon()<<muon->isTrackerMuon()<<std::endl;
0142     //NNBB: one muon can be of many kinds at once but with the muonType_ we are sure
0143     // to avoid double counting of the same muon
0144     if (muon->isGlobalMuon() && muonType_ == 1)
0145       tracks.push_back(*(muon->globalTrack()));
0146     else if (muon->isStandAloneMuon() && muonType_ == 2)
0147       tracks.push_back(*(muon->outerTrack()));
0148     else if (muon->isTrackerMuon() && muonType_ == 3)
0149       tracks.push_back(*(muon->innerTrack()));
0150 
0151     else if (muonType_ == 10 && !(muon->isStandAloneMuon()))  //particular case!!
0152       tracks.push_back(*(muon->innerTrack()));
0153     else if (muonType_ == 11 && muon->isGlobalMuon())
0154       tracks.push_back(*(muon->innerTrack()));
0155     else if (muonType_ == 13 && muon->isTrackerMuon())
0156       tracks.push_back(*(muon->innerTrack()));
0157   }
0158 
0159   const edm::InputTag muonLabel_;
0160   const int muonType_;
0161   const bool PATmuons_;
0162   const std::vector<int> resfind_;
0163   const bool speedup_;
0164   const std::string genParticlesName_;
0165   const bool compareToSimTracks_;
0166   const edm::InputTag simTracksCollectionName_;
0167   const bool sherpa_;
0168   const bool debug_;
0169 
0170   const edm::EDGetTokenT<pat::CompositeCandidateCollection> onia2MuMuToken_;
0171   const edm::EDGetTokenT<reco::TrackCollection> trackCollectionToken_;
0172   const edm::EDGetTokenT<pat::MuonCollection> patMuonToken_;
0173   const edm::EDGetTokenT<reco::MuonCollection> recoMuonToken_;
0174   const edm::EDGetTokenT<reco::CaloMuonCollection> caloMuonToken_;
0175   const edm::EDGetTokenT<edm::HepMCProduct> evtMCToken_;
0176   const edm::EDGetTokenT<reco::GenParticleCollection> genParticleToken_;
0177   const edm::EDGetTokenT<edm::SimTrackContainer> simTrackToken_;
0178 
0179   static const double mMu2;
0180   static const unsigned int motherPdgIdArray[6];
0181 };
0182 
0183 #endif