Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-28 01:34:05

0001 //
0002 //
0003 
0004 #ifndef DataFormats_PatCandidates_Muon_h
0005 #define DataFormats_PatCandidates_Muon_h
0006 
0007 /**
0008   \class    pat::Muon Muon.h "DataFormats/PatCandidates/interface/Muon.h"
0009   \brief    Analysis-level muon class
0010 
0011    pat::Muon implements the analysis-level muon class within the 'pat'
0012    namespace.
0013 
0014    Please post comments and questions to the Physics Tools hypernews:
0015    https://hypernews.cern.ch/HyperNews/CMS/get/physTools.html
0016 
0017   \author   Steven Lowette, Giovanni Petrucciani, Frederic Ronga, Colin Bernet
0018 
0019 */
0020 
0021 #include "DataFormats/MuonReco/interface/Muon.h"
0022 #include "DataFormats/MuonReco/interface/MuonFwd.h"
0023 #include "DataFormats/MuonReco/interface/MuonSelectors.h"
0024 #include "DataFormats/MuonReco/interface/MuonMETCorrectionData.h"
0025 #include "DataFormats/MuonReco/interface/MuonTimeExtra.h"
0026 
0027 #include "DataFormats/TrackReco/interface/Track.h"
0028 #include "DataFormats/PatCandidates/interface/Lepton.h"
0029 #include "DataFormats/ParticleFlowCandidate/interface/IsolatedPFCandidateFwd.h"
0030 #include "DataFormats/ParticleFlowCandidate/interface/IsolatedPFCandidate.h"
0031 #include "DataFormats/MuonReco/interface/MuonSimInfo.h"
0032 
0033 // Define typedefs for convenience
0034 namespace pat {
0035   class Muon;
0036   typedef std::vector<Muon> MuonCollection;
0037   typedef edm::Ref<MuonCollection> MuonRef;
0038   typedef edm::RefVector<MuonCollection> MuonRefVector;
0039 }  // namespace pat
0040 
0041 namespace reco {
0042   /// pipe operator (introduced to use pat::Muon with PFTopProjectors)
0043   std::ostream& operator<<(std::ostream& out, const pat::Muon& obj);
0044 }  // namespace reco
0045 
0046 // Class definition
0047 namespace pat {
0048 
0049   class PATMuonSlimmer;
0050 
0051   class Muon : public Lepton<reco::Muon> {
0052   public:
0053     /// default constructor
0054     Muon();
0055     /// constructor from a reco muon
0056     Muon(const reco::Muon& aMuon);
0057     /// constructor from a RefToBase to a reco muon (to be superseded by Ptr counterpart)
0058     Muon(const edm::RefToBase<reco::Muon>& aMuonRef);
0059     /// constructor from a Ptr to a reco muon
0060     Muon(const edm::Ptr<reco::Muon>& aMuonRef);
0061     /// destructor
0062     ~Muon() override;
0063 
0064     /// required reimplementation of the Candidate's clone method
0065     Muon* clone() const override { return new Muon(*this); }
0066 
0067     // ---- methods for content embedding ----
0068     /// reference to Track reconstructed in the tracker only (reimplemented from reco::Muon)
0069     reco::TrackRef track() const override;
0070     using reco::RecoCandidate::track;  // avoid hiding the base implementation
0071     /// reference to Track reconstructed in the tracker only (reimplemented from reco::Muon)
0072     reco::TrackRef innerTrack() const override { return track(); }
0073     /// reference to Track reconstructed in the muon detector only (reimplemented from reco::Muon)
0074     reco::TrackRef standAloneMuon() const override;
0075     /// reference to Track reconstructed in the muon detector only (reimplemented from reco::Muon)
0076     reco::TrackRef outerTrack() const override { return standAloneMuon(); }
0077     /// reference to Track reconstructed in both tracked and muon detector (reimplemented from reco::Muon)
0078     reco::TrackRef combinedMuon() const override;
0079     /// reference to Track reconstructed in both tracked and muon detector (reimplemented from reco::Muon)
0080     reco::TrackRef globalTrack() const override { return combinedMuon(); }
0081     /// Track selected to be the best measurement of the muon parameters (including PFlow global information)
0082     const reco::Track* bestTrack() const override { return muonBestTrack().get(); }
0083     /// Track selected to be the best measurement of the muon parameters (including PFlow global information)
0084     reco::TrackRef muonBestTrack() const override;
0085     /// Track selected to be the best measurement of the muon parameters (from muon information alone)
0086     reco::TrackRef tunePMuonBestTrack() const override;
0087 
0088     /// set reference to Track selected to be the best measurement of the muon parameters (reimplemented from reco::Muon)
0089     /// if force == false, do not embed this track if it's embedded already (e.g. ig it's a tracker track, and that's already embedded)
0090     void embedMuonBestTrack(bool force = false);
0091     /// set reference to Track selected to be the best measurement of the muon parameters (reimplemented from reco::Muon)
0092     /// if force == false, do not embed this track if it's embedded already (e.g. ig it's a tracker track, and that's already embedded)
0093     void embedTunePMuonBestTrack(bool force = false);
0094     /// set reference to Track reconstructed in the tracker only (reimplemented from reco::Muon)
0095     void embedTrack();
0096     /// set reference to Track reconstructed in the muon detector only (reimplemented from reco::Muon)
0097     void embedStandAloneMuon();
0098     /// set reference to Track reconstructed in both tracked and muon detector (reimplemented from reco::Muon)
0099     void embedCombinedMuon();
0100     // rekey TrackExtra references of embedded tracks
0101     void rekeyEmbeddedTracks(std::vector<edm::Handle<edm::Association<reco::TrackExtraCollection>>> const& assocs);
0102 
0103     // ---- methods for MuonMETCorrectionData ----
0104     /// muon MET corrections for caloMET; returns the muon correction struct if embedded during pat tuple production or an empty element
0105     reco::MuonMETCorrectionData caloMETMuonCorrs() const {
0106       return (embeddedCaloMETMuonCorrs_ ? caloMETMuonCorrs_.front() : reco::MuonMETCorrectionData());
0107     };
0108     void embedCaloMETMuonCorrs(const reco::MuonMETCorrectionData& t);
0109     /// muon MET corrections for tcMET; returns the muon correction struct if embedded during pat tuple production or an empty element
0110     reco::MuonMETCorrectionData tcMETMuonCorrs() const {
0111       return (embeddedTCMETMuonCorrs_ ? tcMETMuonCorrs_.front() : reco::MuonMETCorrectionData());
0112     };
0113     void embedTcMETMuonCorrs(const reco::MuonMETCorrectionData& t);
0114 
0115     // ---- methods for TeV refit tracks ----
0116 
0117     /// reference to Track reconstructed using hits in the tracker + "good" muon hits (reimplemented from reco::Muon)
0118     reco::TrackRef pickyTrack() const override;
0119     /// reference to Track reconstructed using hits in the tracker + info from the first muon station that has hits (reimplemented from reco::Muon)
0120     reco::TrackRef tpfmsTrack() const override;
0121     /// reference to Track reconstructed using DYT algorithm
0122     reco::TrackRef dytTrack() const override;
0123     /// Deprecated accessors to call the corresponding above two functions; no dytMuon since this naming is deprecated.
0124     reco::TrackRef pickyMuon() const { return pickyTrack(); }  // JMTBAD gcc deprecated attribute?
0125     reco::TrackRef tpfmsMuon() const { return tpfmsTrack(); }  // JMTBAD gcc deprecated attribute?
0126     /// embed reference to the above picky Track
0127     void embedPickyMuon();
0128     /// embed reference to the above tpfms Track
0129     void embedTpfmsMuon();
0130     /// embed reference to the above dyt Track
0131     void embedDytMuon();
0132 
0133     // add extra timing information
0134     void readTimeExtra(const reco::MuonTimeExtra& t);
0135 
0136     // ---- PF specific methods ----
0137     /// reference to the source IsolatedPFCandidates
0138     /// null if this has been built from a standard muon
0139     reco::PFCandidateRef pfCandidateRef() const;
0140     /// add a reference to the source IsolatedPFCandidate
0141     void setPFCandidateRef(const reco::PFCandidateRef& ref) { pfCandidateRef_ = ref; }
0142     /// embed the IsolatedPFCandidate pointed to by pfCandidateRef_
0143     void embedPFCandidate();
0144     /// get the number of non-null PF candidates
0145     size_t numberOfSourceCandidatePtrs() const override {
0146       size_t res = 0;
0147       if (pfCandidateRef_.isNonnull())
0148         res++;
0149       if (refToOrig_.isNonnull())
0150         res++;
0151       return res;
0152     }
0153     /// get the candidate pointer with index i
0154     reco::CandidatePtr sourceCandidatePtr(size_type i) const override;
0155 
0156     // ---- methods for accessing muon identification ----
0157     /// accessor for the various muon id algorithms currently defined
0158     /// in DataFormats/MuonReco/interface/MuonSelectors.h
0159     /// e.g. bool result = patmuon.muonID("TMLastStationLoose")
0160     bool muonID(const std::string& name) const;
0161     /// wrapper for the muonID method to maintain backwards compatibility
0162     /// with when the reco::Muon::isGood method existed
0163     bool isGood(const std::string& name) const { return muonID(name); }
0164     /// if muon id results are ever extracted from muon id value maps
0165     /// then the isMuonIDAvailable method will be defined
0166     //bool isMuonIDAvailable(const std::string& name) const;
0167 
0168     /// Muon Selectors as specified in
0169     /// https://twiki.cern.ch/twiki/bin/view/CMSPublic/SWGuideMuonId
0170     bool isTightMuon(const reco::Vertex&) const;
0171     bool isLooseMuon() const;
0172     bool isMediumMuon() const;
0173     bool isSoftMuon(const reco::Vertex&) const;
0174     bool isHighPtMuon(const reco::Vertex&) const;
0175 
0176     // ---- overload of isolation functions ----
0177     /// Overload of pat::Lepton::trackIso(); returns the value of
0178     /// the summed track pt in a cone of deltaR<0.3
0179     float trackIso() const { return isolationR03().sumPt; }
0180     /// Overload of pat::Lepton::trackIso(); returns the value of
0181     /// the summed Et of all recHits in the ecal in a cone of
0182     /// deltaR<0.3
0183     float ecalIso() const { return isolationR03().emEt; }
0184     /// Overload of pat::Lepton::trackIso(); returns the value of
0185     /// the summed Et of all caloTowers in the hcal in a cone of
0186     /// deltaR<0.4
0187     float hcalIso() const { return isolationR03().hadEt; }
0188     /// Overload of pat::Lepton::trackIso(); returns the sum of
0189     /// ecalIso() and hcalIso
0190     float caloIso() const { return ecalIso() + hcalIso(); }
0191 
0192     /// returns PUPPI isolations
0193     float puppiChargedHadronIso() const { return puppiChargedHadronIso_; }
0194     float puppiNeutralHadronIso() const { return puppiNeutralHadronIso_; }
0195     float puppiPhotonIso() const { return puppiPhotonIso_; }
0196     /// returns PUPPINoLeptons isolations
0197     float puppiNoLeptonsChargedHadronIso() const { return puppiNoLeptonsChargedHadronIso_; }
0198     float puppiNoLeptonsNeutralHadronIso() const { return puppiNoLeptonsNeutralHadronIso_; }
0199     float puppiNoLeptonsPhotonIso() const { return puppiNoLeptonsPhotonIso_; }
0200     /// sets PUPPI isolations
0201     void setIsolationPUPPI(float chargedhadrons, float neutralhadrons, float photons) {
0202       puppiChargedHadronIso_ = chargedhadrons;
0203       puppiNeutralHadronIso_ = neutralhadrons;
0204       puppiPhotonIso_ = photons;
0205     }
0206     /// sets PUPPINoLeptons isolations
0207     void setIsolationPUPPINoLeptons(float chargedhadrons, float neutralhadrons, float photons) {
0208       puppiNoLeptonsChargedHadronIso_ = chargedhadrons;
0209       puppiNoLeptonsNeutralHadronIso_ = neutralhadrons;
0210       puppiNoLeptonsPhotonIso_ = photons;
0211     }
0212 
0213     /// Muon High Level Selection
0214     /// The user can choose to cache this info so they can drop the
0215     /// global tracks. If the global track is present these should
0216     /// not be set, but the "getters" will return the appropriate
0217     /// value. The exception is dB which requires the beamline
0218     //  as external input.
0219 
0220     // ---- embed various impact parameters with errors ----
0221     //
0222     // example:
0223     //
0224     //    // this will return the muon inner track
0225     //    // transverse impact parameter
0226     //    // relative to the primary vertex
0227     //    muon->dB(pat::Muon::PV2D);
0228     //
0229     //    // this will return the uncertainty
0230     //    // on the muon inner track
0231     //    // transverse impact parameter
0232     //    // relative to the primary vertex
0233     //    // or -1.0 if there is no valid PV in the event
0234     //    muon->edB(pat::Muon::PV2D);
0235     //
0236     // IpType defines the type of the impact parameter
0237     typedef enum IPTYPE { PV2D = 0, PV3D = 1, BS2D = 2, BS3D = 3, PVDZ = 4, IpTypeSize = 5 } IpType;
0238     void initImpactParameters(void);  // init IP defaults in a constructor
0239     double dB(IPTYPE type) const;
0240     double edB(IPTYPE type) const;
0241 
0242     /// the version without arguments returns PD2D, but with an absolute value (for backwards compatibility)
0243     double dB() const { return std::abs(dB(PV2D)); }
0244     /// the version without arguments returns PD2D, but with an absolute value (for backwards compatibility)
0245     double edB() const { return std::abs(edB(PV2D)); }
0246 
0247     void setDB(double dB, double edB, IPTYPE type = PV2D) {
0248       ip_[type] = dB;
0249       eip_[type] = edB;
0250       cachedIP_ |= (1 << int(type));
0251     }
0252 
0253     /// numberOfValidHits returns the number of valid hits on the global track.
0254     unsigned int numberOfValidHits() const;
0255     void setNumberOfValidHits(unsigned int numberOfValidHits) {
0256       numberOfValidHits_ = numberOfValidHits;
0257       cachedNumberOfValidHits_ = true;
0258     }
0259 
0260     /// Norm chi2 gives the normalized chi2 of the global track.
0261     double normChi2() const;
0262     void setNormChi2(double normChi2) {
0263       normChi2_ = normChi2;
0264       cachedNormChi2_ = true;
0265     }
0266 
0267     /// Returns the segment compatibility, using muon::segmentCompatibility (DataFormats/MuonReco/interface/MuonSelectors.h)
0268     double segmentCompatibility(
0269         reco::Muon::ArbitrationType arbitrationType = reco::Muon::SegmentAndTrackArbitration) const;
0270 
0271     /// pipe operator (introduced to use pat::Muon with PFTopProjectors)
0272     friend std::ostream& reco::operator<<(std::ostream& out, const pat::Muon& obj);
0273 
0274     friend class PATMuonSlimmer;
0275 
0276     float pfEcalEnergy() const { return pfEcalEnergy_; }
0277     void setPfEcalEnergy(float pfEcalEnergy) { pfEcalEnergy_ = pfEcalEnergy; }
0278 
0279     /// near-by jet information
0280     float jetPtRatio() const { return jetPtRatio_; }
0281     float jetPtRel() const { return jetPtRel_; }
0282     void setJetPtRatio(float jetPtRatio) { jetPtRatio_ = jetPtRatio; }
0283     void setJetPtRel(float jetPtRel) { jetPtRel_ = jetPtRel; }
0284 
0285     /// Soft Muon MVA
0286     float softMvaValue() const { return softMvaValue_; }
0287     void setSoftMvaValue(float softmva) { softMvaValue_ = softmva; }
0288 
0289     /// Muon MVA ID
0290     float mvaIDValue() const { return mvaIDValue_; }
0291     void setMvaIDValue(float mvaID) { mvaIDValue_ = mvaID; }
0292 
0293     // 1/beta for prompt particle hypothesis
0294     /// (time is constraint to the bunch crossing time)
0295     float inverseBeta() const { return inverseBeta_; }
0296     float inverseBetaErr() const { return inverseBetaErr_; }
0297 
0298     /// MC matching information
0299     reco::MuonSimType simType() const { return simType_; }
0300     reco::ExtendedMuonSimType simExtType() const { return simExtType_; }
0301     //  FLAVOUR:
0302     //  - for non-muons: 0
0303     //  - for primary muons: 13
0304     //  - for non primary muons: flavour of the mother: std::abs(pdgId) of heaviest quark, or 15 for tau
0305     int simFlavour() const { return simFlavour_; }
0306     int simHeaviestMotherFlavour() const { return simHeaviestMotherFlavour_; }
0307     int simPdgId() const { return simPdgId_; }
0308     int simMotherPdgId() const { return simMotherPdgId_; }
0309     int simBX() const { return simBX_; }
0310     int simTpEvent() const { return simTpEvent_; }
0311     float simProdRho() const { return simProdRho_; }
0312     float simProdZ() const { return simProdZ_; }
0313     float simPt() const { return simPt_; }
0314     float simEta() const { return simEta_; }
0315     float simPhi() const { return simPhi_; }
0316     float simMatchQuality() const { return simMatchQuality_; }
0317 
0318     void initSimInfo(void);
0319     void setSimType(reco::MuonSimType type) { simType_ = type; }
0320     void setExtSimType(reco::ExtendedMuonSimType type) { simExtType_ = type; }
0321     void setSimFlavour(int f) { simFlavour_ = f; }
0322     void setSimHeaviestMotherFlavour(int id) { simHeaviestMotherFlavour_ = id; }
0323     void setSimPdgId(int id) { simPdgId_ = id; }
0324     void setSimMotherPdgId(int id) { simMotherPdgId_ = id; }
0325     void setSimBX(int bx) { simBX_ = bx; }
0326     void setSimTpEvent(int tpEvent) { simTpEvent_ = tpEvent; }
0327     void setSimProdRho(float rho) { simProdRho_ = rho; }
0328     void setSimProdZ(float z) { simProdZ_ = z; }
0329     void setSimPt(float pt) { simPt_ = pt; }
0330     void setSimEta(float eta) { simEta_ = eta; }
0331     void setSimPhi(float phi) { simPhi_ = phi; }
0332     void setSimMatchQuality(float quality) { simMatchQuality_ = quality; }
0333 
0334     /// Trigger information
0335     const pat::TriggerObjectStandAlone* l1Object(const size_t idx = 0) const {
0336       return triggerObjectMatchByType(trigger::TriggerL1Mu, idx);
0337     }
0338     const pat::TriggerObjectStandAlone* hltObject(const size_t idx = 0) const {
0339       return triggerObjectMatchByType(trigger::TriggerMuon, idx);
0340     }
0341     bool triggered(const char* pathName) const { return triggerObjectMatchByPath(pathName, true, true) != nullptr; }
0342 
0343   protected:
0344     // ---- for content embedding ----
0345 
0346     /// best muon track (global pflow)
0347     bool embeddedMuonBestTrack_;
0348     std::vector<reco::Track> muonBestTrack_;
0349     /// best muon track (muon only)
0350     bool embeddedTunePMuonBestTrack_;
0351     std::vector<reco::Track> tunePMuonBestTrack_;
0352     /// track of inner track detector
0353     bool embeddedTrack_;
0354     std::vector<reco::Track> track_;
0355     /// track of muon system
0356     bool embeddedStandAloneMuon_;
0357     std::vector<reco::Track> standAloneMuon_;
0358     /// track of combined fit
0359     bool embeddedCombinedMuon_;
0360     std::vector<reco::Track> combinedMuon_;
0361 
0362     /// muon MET corrections for tcMET
0363     bool embeddedTCMETMuonCorrs_;
0364     std::vector<reco::MuonMETCorrectionData> tcMETMuonCorrs_;
0365     /// muon MET corrections for caloMET
0366     bool embeddedCaloMETMuonCorrs_;
0367     std::vector<reco::MuonMETCorrectionData> caloMETMuonCorrs_;
0368 
0369     // Capability to embed TeV refit tracks as the inner/outer/combined ones.
0370     bool embeddedPickyMuon_;
0371     bool embeddedTpfmsMuon_;
0372     bool embeddedDytMuon_;
0373     std::vector<reco::Track> pickyMuon_;
0374     std::vector<reco::Track> tpfmsMuon_;
0375     std::vector<reco::Track> dytMuon_;
0376 
0377     // ---- PF specific members ----
0378     /// true if the IsolatedPFCandidate is embedded
0379     bool embeddedPFCandidate_;
0380     /// if embeddedPFCandidate_, a copy of the source IsolatedPFCandidate
0381     /// is stored in this vector
0382     reco::PFCandidateCollection pfCandidate_;
0383     /// reference to the IsolatedPFCandidate this has been built from
0384     /// null if this has been built from a standard muon
0385     reco::PFCandidateRef pfCandidateRef_;
0386 
0387     // V+Jets group selection variables.
0388     bool cachedNormChi2_;  /// has the normalized chi2 been cached?
0389     double normChi2_;      /// globalTrack->chi2() / globalTrack->ndof()
0390 
0391     bool cachedNumberOfValidHits_;    /// has the numberOfValidHits been cached?
0392     unsigned int numberOfValidHits_;  /// globalTrack->numberOfValidHits()
0393 
0394     // ---- cached impact parameters ----
0395     uint8_t cachedIP_;       // has the IP (former dB) been cached?
0396     float ip_[IpTypeSize];   // dB and edB are the impact parameter at the primary vertex,
0397     float eip_[IpTypeSize];  // and its uncertainty as recommended by the tracking group
0398 
0399     /// PUPPI isolations
0400     float puppiChargedHadronIso_;
0401     float puppiNeutralHadronIso_;
0402     float puppiPhotonIso_;
0403     /// PUPPINoLeptons isolations
0404     float puppiNoLeptonsChargedHadronIso_;
0405     float puppiNoLeptonsNeutralHadronIso_;
0406     float puppiNoLeptonsPhotonIso_;
0407 
0408     float pfEcalEnergy_;
0409 
0410     /// near-by jet information
0411     float jetPtRatio_;
0412     float jetPtRel_;
0413 
0414     /// Muon MVA
0415     float mvaIDValue_;
0416     float softMvaValue_;
0417 
0418     /// Inverse beta
0419     float inverseBeta_;
0420     float inverseBetaErr_;
0421 
0422     /// MC matching information
0423     reco::MuonSimType simType_;
0424     reco::ExtendedMuonSimType simExtType_;
0425     int simFlavour_;
0426     int simHeaviestMotherFlavour_;
0427     int simPdgId_;
0428     int simMotherPdgId_;
0429     int simBX_;
0430     int simTpEvent_;
0431     float simProdRho_;
0432     float simProdZ_;
0433     float simPt_;
0434     float simEta_;
0435     float simPhi_;
0436     float simMatchQuality_;
0437   };
0438 
0439 }  // namespace pat
0440 
0441 #endif