Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:04:54

0001 //
0002 //
0003 
0004 #ifndef DataFormats_PatCandidates_Lepton_h
0005 #define DataFormats_PatCandidates_Lepton_h
0006 
0007 /**
0008   \class    pat::Lepton Lepton.h "DataFormats/PatCandidates/interface/Lepton.h"
0009   \brief    Analysis-level lepton class
0010 
0011    Lepton implements the analysis-level charged lepton class within the 'pat'
0012    namespace. It currently provides the link to the generated lepton and
0013    the isolation information.
0014 
0015    Please post comments and questions to the Physics Tools hypernews:
0016    https://hypernews.cern.ch/HyperNews/CMS/get/physTools.html
0017 
0018   \author   Steven Lowette, Giovanni Petrucciani, Frederic Ronga
0019 */
0020 
0021 #include "DataFormats/Candidate/interface/Particle.h"
0022 #include "DataFormats/PatCandidates/interface/PATObject.h"
0023 #include "DataFormats/PatCandidates/interface/Isolation.h"
0024 #include "DataFormats/PatCandidates/interface/PFIsolation.h"
0025 
0026 namespace pat {
0027 
0028   template <class LeptonType>
0029   class Lepton : public PATObject<LeptonType> {
0030   public:
0031     Lepton();
0032     Lepton(const LeptonType &aLepton);
0033     Lepton(const edm::RefToBase<LeptonType> &aLeptonRef);
0034     Lepton(const edm::Ptr<LeptonType> &aLeptonRef);
0035     ~Lepton() override;
0036 
0037     Lepton<LeptonType> *clone() const override { return new Lepton<LeptonType>(*this); }
0038 
0039     const reco::GenParticle *genLepton() const { return PATObject<LeptonType>::genParticle(); }
0040 
0041     void setGenLepton(const reco::GenParticleRef &gl, bool embed = false) {
0042       PATObject<LeptonType>::setGenParticleRef(gl, embed);
0043     }
0044 
0045     //============ BEGIN ISOLATION BLOCK =====
0046     /// Returns the isolation variable for a specific key (or
0047     /// pseudo-key like CaloIso), or -1.0 if not available
0048     float userIsolation(IsolationKeys key) const {
0049       if (key >= 0) {
0050         //if (key >= isolations_.size()) throw cms::Excepton("Missing Data")
0051         //<< "Isolation corresponding to key "
0052         //<< key << " was not stored for this particle.";
0053         if (size_t(key) >= isolations_.size())
0054           return -1.0;
0055         return isolations_[key];
0056       } else
0057         switch (key) {
0058           case pat::CaloIso:
0059             //if (isolations_.size() <= pat::HcalIso) throw cms::Excepton("Missing Data")
0060             //<< "CaloIso Isolation was not stored for this particle.";
0061             if (isolations_.size() <= pat::HcalIso)
0062               return -1.0;
0063             return isolations_[pat::EcalIso] + isolations_[pat::HcalIso];
0064           default:
0065             return -1.0;
0066             //throw cms::Excepton("Missing Data") << "Isolation corresponding to key "
0067             //<< key << " was not stored for this particle.";
0068         }
0069     }
0070     /// Returns the isolation variable for string type function arguments
0071     /// (to be used with the cut-string parser);
0072     /// the possible values of the strings are the enums defined in
0073     /// DataFormats/PatCandidates/interface/Isolation.h
0074     float userIsolation(const std::string &key) const {
0075       // remove leading namespace specifier
0076       std::string prunedKey = (key.find("pat::") == 0) ? std::string(key, 5) : key;
0077       if (prunedKey == "TrackIso")
0078         return userIsolation(pat::TrackIso);
0079       if (prunedKey == "EcalIso")
0080         return userIsolation(pat::EcalIso);
0081       if (prunedKey == "HcalIso")
0082         return userIsolation(pat::HcalIso);
0083       if (prunedKey == "PfAllParticleIso")
0084         return userIsolation(pat::PfAllParticleIso);
0085       if (prunedKey == "PfChargedHadronIso")
0086         return userIsolation(pat::PfChargedHadronIso);
0087       if (prunedKey == "PfNeutralHadronIso")
0088         return userIsolation(pat::PfNeutralHadronIso);
0089       if (prunedKey == "PfGammaIso")
0090         return userIsolation(pat::PfGammaIso);
0091       if (prunedKey == "User1Iso")
0092         return userIsolation(pat::User1Iso);
0093       if (prunedKey == "User2Iso")
0094         return userIsolation(pat::User2Iso);
0095       if (prunedKey == "User3Iso")
0096         return userIsolation(pat::User3Iso);
0097       if (prunedKey == "User4Iso")
0098         return userIsolation(pat::User4Iso);
0099       if (prunedKey == "User5Iso")
0100         return userIsolation(pat::User5Iso);
0101       if (prunedKey == "UserBaseIso")
0102         return userIsolation(pat::UserBaseIso);
0103       if (prunedKey == "CaloIso")
0104         return userIsolation(pat::CaloIso);
0105       if (prunedKey == "PfPUChargedHadronIso")
0106         return userIsolation(pat::PfPUChargedHadronIso);
0107       //throw cms::Excepton("Missing Data")
0108       //<< "Isolation corresponding to key "
0109       //<< key << " was not stored for this particle.";
0110       return -1.0;
0111     }
0112     /// Sets the userIsolation variable for a specific key.
0113     /// Note that you can't set isolation for a pseudo-key
0114     /// like CaloIso
0115     void setIsolation(IsolationKeys key, float value) {
0116       if (key >= 0) {
0117         if (size_t(key) >= isolations_.size())
0118           isolations_.resize(key + 1, -1.0);
0119         isolations_[key] = value;
0120       } else {
0121         throw cms::Exception("Illegal Argument")
0122             << "The key for which you're setting isolation does not correspond "
0123             << "to an individual isolation but to the sum of more independent isolations "
0124             << "(e.g. Calo = Ecal + Hcal), so you can't SET the value, just GET it.\n"
0125             << "Please set up each component independly.\n";
0126       }
0127     }
0128 
0129     // ---- specific getters ----
0130     /// Returns the tracker isolation variable that was stored in this
0131     /// object when produced, or -1.0 if there is none (overloaded if
0132     /// specific isolation functions are available from the derived
0133     /// objects)
0134     float trackIso() const { return userIsolation(pat::TrackIso); }
0135     /// Returns the sum of ecal and hcal isolation variable that were
0136     /// stored in this object when produced, or -1.0 if at least one
0137     /// is missing (overloaded if specific isolation functions are
0138     /// available from the derived objects)
0139     float caloIso() const { return userIsolation(pat::CaloIso); }
0140     /// Returns the ecal isolation variable that was stored in this
0141     /// object when produced, or -1.0 if there is none (overloaded
0142     /// if specific isolation functions are available from the
0143     /// derived objects)
0144     float ecalIso() const { return userIsolation(pat::EcalIso); }
0145     /// Returns the hcal isolation variable that was stored in this
0146     /// object when produced, or -1.0 if there is none (overloaded
0147     /// if specific isolation functions are available from the
0148     /// derived objects)
0149     float hcalIso() const { return userIsolation(pat::HcalIso); }
0150 
0151     /// PARTICLE FLOW ISOLATION
0152     /// Returns the isolation calculated with all the PFCandidates
0153     float particleIso() const { return userIsolation(pat::PfAllParticleIso); }
0154     /// Returns the isolation calculated with only the charged hadron
0155     /// PFCandidates
0156     float chargedHadronIso() const { return userIsolation(pat::PfChargedHadronIso); }
0157     /// Returns the isolation calculated with only the neutral hadron
0158     /// PFCandidates
0159     float neutralHadronIso() const { return userIsolation(pat::PfNeutralHadronIso); }
0160     /// Returns the isolation calculated with only the gamma
0161     /// PFCandidates
0162     float photonIso() const { return userIsolation(pat::PfGammaIso); }
0163     /// Returns the isolation calculated with only the pile-up charged hadron
0164     /// PFCandidates
0165     float puChargedHadronIso() const { return userIsolation(pat::PfPUChargedHadronIso); }
0166     /// Returns the user defined isolation variable #index that was
0167     /// stored in this object when produced, or -1.0 if there is none
0168     float userIso(uint8_t index = 0) const { return userIsolation(IsolationKeys(UserBaseIso + index)); }
0169 
0170     // ---- specific setters ----
0171     /// Sets tracker isolation variable
0172     void setTrackIso(float trackIso) { setIsolation(pat::TrackIso, trackIso); }
0173     /// Sets ecal isolation variable
0174     void setEcalIso(float caloIso) { setIsolation(pat::EcalIso, caloIso); }
0175     /// Sets hcal isolation variable
0176     void setHcalIso(float caloIso) { setIsolation(pat::HcalIso, caloIso); }
0177     /// Sets user isolation variable #index
0178     void setUserIso(float value, uint8_t index = 0) { setIsolation(IsolationKeys(UserBaseIso + index), value); }
0179 
0180     //============ BEGIN ISODEPOSIT BLOCK =====
0181     /// Returns the IsoDeposit associated with some key, or a null pointer if it is not available
0182     const IsoDeposit *isoDeposit(IsolationKeys key) const {
0183       for (IsoDepositPairs::const_iterator it = isoDeposits_.begin(), ed = isoDeposits_.end(); it != ed; ++it) {
0184         if (it->first == key)
0185           return &it->second;
0186       }
0187       return nullptr;
0188     }
0189 
0190     /// Sets the IsoDeposit associated with some key; if it is already existent, it is overwritten.
0191     void setIsoDeposit(IsolationKeys key, const IsoDeposit &dep) {
0192       IsoDepositPairs::iterator it = isoDeposits_.begin(), ed = isoDeposits_.end();
0193       for (; it != ed; ++it) {
0194         if (it->first == key) {
0195           it->second = dep;
0196           return;
0197         }
0198       }
0199       isoDeposits_.push_back(std::make_pair(key, dep));
0200     }
0201 
0202     // ---- specific getters ----
0203     const IsoDeposit *trackIsoDeposit() const { return isoDeposit(pat::TrackIso); }
0204     const IsoDeposit *ecalIsoDeposit() const { return isoDeposit(pat::EcalIso); }
0205     const IsoDeposit *hcalIsoDeposit() const { return isoDeposit(pat::HcalIso); }
0206     const IsoDeposit *userIsoDeposit(uint8_t index = 0) const { return isoDeposit(IsolationKeys(UserBaseIso + index)); }
0207 
0208     // ---- specific setters ----
0209     void trackIsoDeposit(const IsoDeposit &dep) { setIsoDeposit(pat::TrackIso, dep); }
0210     void ecalIsoDeposit(const IsoDeposit &dep) { setIsoDeposit(pat::EcalIso, dep); }
0211     void hcalIsoDeposit(const IsoDeposit &dep) { setIsoDeposit(pat::HcalIso, dep); }
0212     void userIsoDeposit(const IsoDeposit &dep, uint8_t index = 0) {
0213       setIsoDeposit(IsolationKeys(UserBaseIso + index), dep);
0214     }
0215 
0216     const PFIsolation &miniPFIsolation() const { return miniPFIsolation_; }
0217     void setMiniPFIsolation(PFIsolation const &iso) { miniPFIsolation_ = iso; }
0218 
0219   protected:
0220     // --- Isolation and IsoDeposit related datamebers ---
0221     typedef std::vector<std::pair<IsolationKeys, pat::IsoDeposit> > IsoDepositPairs;
0222     IsoDepositPairs isoDeposits_;
0223     std::vector<float> isolations_;
0224 
0225     PFIsolation miniPFIsolation_;
0226   };
0227 
0228   /// default constructor
0229   template <class LeptonType>
0230   Lepton<LeptonType>::Lepton() : PATObject<LeptonType>(LeptonType()) {
0231     // no common constructor, so initialize the candidate manually
0232     this->setCharge(0);
0233     this->setP4(reco::Particle::LorentzVector(0, 0, 0, 0));
0234     this->setVertex(reco::Particle::Point(0, 0, 0));
0235     this->setMiniPFIsolation(pat::PFIsolation());
0236   }
0237 
0238   /// constructor from LeptonType
0239   template <class LeptonType>
0240   Lepton<LeptonType>::Lepton(const LeptonType &aLepton) : PATObject<LeptonType>(aLepton) {
0241     this->setMiniPFIsolation(pat::PFIsolation());
0242   }
0243 
0244   /// constructor from ref to LeptonType
0245   template <class LeptonType>
0246   Lepton<LeptonType>::Lepton(const edm::RefToBase<LeptonType> &aLeptonRef) : PATObject<LeptonType>(aLeptonRef) {
0247     this->setMiniPFIsolation(pat::PFIsolation());
0248   }
0249 
0250   /// constructor from ref to LeptonType
0251   template <class LeptonType>
0252   Lepton<LeptonType>::Lepton(const edm::Ptr<LeptonType> &aLeptonRef) : PATObject<LeptonType>(aLeptonRef) {
0253     this->setMiniPFIsolation(pat::PFIsolation());
0254   }
0255 
0256   /// destructor
0257   template <class LeptonType>
0258   Lepton<LeptonType>::~Lepton() {}
0259 }  // namespace pat
0260 
0261 #endif