Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:54:18

0001 //
0002 //
0003 
0004 #ifndef DataFormats_PatCandidates_GenericParticle_h
0005 #define DataFormats_PatCandidates_GenericParticle_h
0006 
0007 /**
0008   \class    pat::GenericParticle GenericParticle.h "DataFormats/PatCandidates/interface/GenericParticle.h"
0009   \brief    Analysis-level Generic Particle class (e.g. for hadron or muon not fully reconstructed)
0010 
0011    GenericParticle implements the analysis-level generic particle class within the 'pat'
0012    namespace.
0013 
0014   \author   Giovanni Petrucciani
0015 */
0016 
0017 #include "DataFormats/PatCandidates/interface/PATObject.h"
0018 #include "DataFormats/RecoCandidate/interface/RecoCandidate.h"
0019 #include "DataFormats/EgammaReco/interface/SuperClusterFwd.h"
0020 #include "DataFormats/EgammaReco/interface/SuperCluster.h"
0021 #include "DataFormats/GsfTrackReco/interface/GsfTrackFwd.h"
0022 #include "DataFormats/GsfTrackReco/interface/GsfTrack.h"
0023 #include "DataFormats/PatCandidates/interface/Isolation.h"
0024 #include "DataFormats/PatCandidates/interface/Vertexing.h"
0025 #include "DataFormats/HepMCCandidate/interface/GenParticle.h"
0026 
0027 // Define typedefs for convenience
0028 namespace pat {
0029   class GenericParticle;
0030   typedef std::vector<GenericParticle> GenericParticleCollection;
0031   typedef edm::Ref<GenericParticleCollection> GenericParticleRef;
0032   typedef edm::RefVector<GenericParticleCollection> GenericParticleRefVector;
0033 }  // namespace pat
0034 
0035 // Class definition
0036 namespace pat {
0037 
0038   class GenericParticle : public PATObject<reco::RecoCandidate> {
0039   public:
0040     /// default constructor
0041     GenericParticle();
0042     /// constructor from Candidate
0043     GenericParticle(const reco::Candidate &aGenericParticle);
0044     /// constructor from ref to Candidate
0045     GenericParticle(const edm::RefToBase<reco::Candidate> &aGenericParticleRef);
0046     /// constructor from ref to Candidate
0047     GenericParticle(const edm::Ptr<reco::Candidate> &aGenericParticleRef);
0048     /// destructor
0049     ~GenericParticle() override;
0050 
0051     /// required reimplementation of the Candidate's clone method
0052     GenericParticle *clone() const override { return new GenericParticle(*this); }
0053 
0054     /// Checks for overlap with another candidate.
0055     /// It will return 'true' if the other candidate is a RecoCandidate,
0056     /// and if they reference to at least one same non null track, supercluster or calotower (except for the multiple tracks)
0057     /// NOTE: It won't work with embedded references
0058     bool overlap(const Candidate &) const override;
0059 
0060     /// reference to a master track (might be transient refs if Tracks are embedded)
0061     /// returns null ref if there is no master track
0062     reco::TrackRef track() const override { return track_.empty() ? trackRef_ : reco::TrackRef(&track_, 0); }
0063     /// reference to one of a set of multiple tracks (might be transient refs if Tracks are embedded)
0064     /// throws exception if idx >= numberOfTracks()
0065     reco::TrackRef track(size_t idx) const override {
0066       if (idx >= numberOfTracks())
0067         throw cms::Exception("Index out of bounds")
0068             << "Requested track " << idx << " out of " << numberOfTracks() << ".\n";
0069       return (tracks_.empty() ? trackRefs_[idx] : reco::TrackRef(&tracks_, idx));
0070     }
0071     /// number of multiple tracks (not including the master one)
0072     size_t numberOfTracks() const override { return tracks_.empty() ? trackRefs_.size() : tracks_.size(); }
0073     /// reference to a GsfTrack (might be transient ref if SuperCluster is embedded)
0074     /// returns null ref if there is no gsf track
0075     reco::GsfTrackRef gsfTrack() const override {
0076       return (gsfTrack_.empty() ? gsfTrackRef_ : reco::GsfTrackRef(&gsfTrack_, 0));
0077     }
0078     /// reference to a stand-alone muon Track (might be transient ref if SuperCluster is embedded)
0079     /// returns null ref if there is no stand-alone muon track
0080     reco::TrackRef standAloneMuon() const override {
0081       return (standaloneTrack_.empty() ? standaloneTrackRef_ : reco::TrackRef(&standaloneTrack_, 0));
0082     }
0083     /// reference to a combined muon Track (might be transient ref if SuperCluster is embedded)
0084     /// returns null ref if there is no combined muon track
0085     reco::TrackRef combinedMuon() const override {
0086       return (combinedTrack_.empty() ? combinedTrackRef_ : reco::TrackRef(&combinedTrack_, 0));
0087     }
0088     /// reference to a SuperCluster (might be transient ref if SuperCluster is embedded)
0089     /// returns null ref if there is no supercluster
0090     reco::SuperClusterRef superCluster() const override {
0091       return superCluster_.empty() ? superClusterRef_ : reco::SuperClusterRef(&superCluster_, 0);
0092     }
0093     /// reference to a CaloTower  (might be transient ref if CaloTower is embedded)
0094     /// returns null ref if there is no calotower
0095     CaloTowerRef caloTower() const override {
0096       return caloTower_.empty() ? caloTowerRef_ : CaloTowerRef(&caloTower_, 0);
0097     }
0098 
0099     /// sets master track reference (or even embed it into the object)
0100     virtual void setTrack(const reco::TrackRef &ref, bool embed = false);
0101     /// sets multiple track references (or even embed the tracks into the object - whatch out for disk size issues!)
0102     virtual void setTracks(const reco::TrackRefVector &refs, bool embed = false);
0103     /// sets stand-alone muon track reference (or even embed it into the object)
0104     virtual void setStandAloneMuon(const reco::TrackRef &ref, bool embed = false);
0105     /// sets combined muon track reference (or even embed it into the object)
0106     virtual void setCombinedMuon(const reco::TrackRef &ref, bool embed = false);
0107     /// sets gsf track reference (or even embed it into the object)
0108     virtual void setGsfTrack(const reco::GsfTrackRef &ref, bool embed = false);
0109     /// sets supercluster reference (or even embed it into the object)
0110     virtual void setSuperCluster(const reco::SuperClusterRef &ref, bool embed = false);
0111     /// sets calotower reference (or even embed it into the object)
0112     virtual void setCaloTower(const CaloTowerRef &ref, bool embed = false);
0113 
0114     /// embeds the master track instead of keeping a reference to it
0115     void embedTrack();
0116     /// embeds the other tracks instead of keeping references
0117     void embedTracks();
0118     /// embeds the stand-alone track instead of keeping a reference to it
0119     void embedStandalone();
0120     /// embeds the combined track instead of keeping a reference to it
0121     void embedCombined();
0122     /// embeds the gsf track instead of keeping a reference to it
0123     void embedGsfTrack();
0124     /// embeds the supercluster instead of keeping a reference to it
0125     void embedSuperCluster();
0126     /// embeds the calotower instead of keeping a reference to it
0127     void embedCaloTower();
0128 
0129     /// returns a user defined quality value, if set by the user to some meaningful value
0130     float quality() { return quality_; }
0131     /// sets a user defined quality value
0132     void setQuality(float quality) { quality_ = quality; }
0133 
0134     //============ BEGIN ISOLATION BLOCK =====
0135     /// Returns the isolation variable for a specifc key (or
0136     /// pseudo-key like CaloIso), or -1.0 if not available
0137     float userIsolation(IsolationKeys key) const {
0138       if (key >= 0) {
0139         //if (key >= isolations_.size()) throw cms::Excepton("Missing Data")
0140         //<< "Isolation corresponding to key "
0141         //<< key << " was not stored for this particle.";
0142         if (size_t(key) >= isolations_.size())
0143           return -1.0;
0144         return isolations_[key];
0145       } else
0146         switch (key) {
0147           case pat::CaloIso:
0148             //if (isolations_.size() <= pat::HcalIso) throw cms::Excepton("Missing Data")
0149             //<< "CalIsoo Isolation was not stored for this particle.";
0150             if (isolations_.size() <= pat::HcalIso)
0151               return -1.0;
0152             return isolations_[pat::EcalIso] + isolations_[pat::HcalIso];
0153           default:
0154             return -1.0;
0155             //throw cms::Excepton("Missing Data") << "Isolation corresponding to key "
0156             //<< key << " was not stored for this particle.";
0157         }
0158     }
0159     /// Returns the isolation variable for string type function arguments
0160     /// (to be used with the cut-string parser);
0161     /// the possible values of the strings are the enums defined in
0162     /// DataFormats/PatCandidates/interface/Isolation.h
0163     float userIsolation(const std::string &key) const {
0164       // remove leading namespace specifier
0165       std::string prunedKey = (key.find("pat::") == 0) ? std::string(key, 5) : key;
0166       if (prunedKey == "TrackIso")
0167         return userIsolation(pat::TrackIso);
0168       if (prunedKey == "EcalIso")
0169         return userIsolation(pat::EcalIso);
0170       if (prunedKey == "HcalIso")
0171         return userIsolation(pat::HcalIso);
0172       if (prunedKey == "PfAllParticleIso")
0173         return userIsolation(pat::PfAllParticleIso);
0174       if (prunedKey == "PfChargedHadronIso")
0175         return userIsolation(pat::PfChargedHadronIso);
0176       if (prunedKey == "PfNeutralHadronIso")
0177         return userIsolation(pat::PfNeutralHadronIso);
0178       if (prunedKey == "PfGammaIso")
0179         return userIsolation(pat::PfGammaIso);
0180       if (prunedKey == "User1Iso")
0181         return userIsolation(pat::User1Iso);
0182       if (prunedKey == "User2Iso")
0183         return userIsolation(pat::User2Iso);
0184       if (prunedKey == "User3Iso")
0185         return userIsolation(pat::User3Iso);
0186       if (prunedKey == "User4Iso")
0187         return userIsolation(pat::User4Iso);
0188       if (prunedKey == "User5Iso")
0189         return userIsolation(pat::User5Iso);
0190       if (prunedKey == "UserBaseIso")
0191         return userIsolation(pat::UserBaseIso);
0192       if (prunedKey == "CaloIso")
0193         return userIsolation(pat::CaloIso);
0194       //throw cms::Excepton("Missing Data")
0195       //<< "Isolation corresponding to key "
0196       //<< key << " was not stored for this particle.";
0197       return -1.0;
0198     }
0199     /// Sets the isolation variable for a specifc key.
0200     /// Note that you can't set isolation for a pseudo-key like CaloIso
0201     void setIsolation(IsolationKeys key, float value) {
0202       if (key >= 0) {
0203         if (size_t(key) >= isolations_.size())
0204           isolations_.resize(key + 1, -1.0);
0205         isolations_[key] = value;
0206       } else {
0207         throw cms::Exception("Illegal Argument")
0208             << "The key for which you're setting isolation does not correspond "
0209             << "to an individual isolation but to the sum of more independent isolations "
0210             << "(e.g. Calo = Ecal + Hcal), so you can't SET the value, just GET it.\n"
0211             << "Please set up each component independly.\n";
0212       }
0213     }
0214 
0215     // ---- specific getters ----
0216     /// Return the tracker isolation variable that was stored in this
0217     /// object when produced, or -1.0 if there is none
0218     float trackIso() const { return userIsolation(pat::TrackIso); }
0219     /// Return the sum of ecal and hcal isolation variable that were
0220     /// stored in this object when produced, or -1.0 if at least one
0221     /// is missing
0222     float caloIso() const { return userIsolation(pat::CaloIso); }
0223     /// Return the ecal isolation variable that was stored in this
0224     /// object when produced, or -1.0 if there is none
0225     float ecalIso() const { return userIsolation(pat::EcalIso); }
0226     /// Return the hcal isolation variable that was stored in this
0227     /// object when produced, or -1.0 if there is none
0228     float hcalIso() const { return userIsolation(pat::HcalIso); }
0229 
0230     // ---- specific setters ----
0231     /// Sets tracker isolation variable
0232     void setTrackIso(float trackIso) { setIsolation(pat::TrackIso, trackIso); }
0233     /// Sets ecal isolation variable
0234     void setEcalIso(float caloIso) { setIsolation(pat::EcalIso, caloIso); }
0235     /// Sets hcal isolation variable
0236     void setHcalIso(float caloIso) { setIsolation(pat::HcalIso, caloIso); }
0237     /// Sets user isolation variable #index
0238     void setUserIso(float value, uint8_t index = 0) { setIsolation(IsolationKeys(UserBaseIso + index), value); }
0239 
0240     //============ BEGIN ISODEPOSIT BLOCK =====
0241     /// Returns the IsoDeposit associated with some key, or a null pointer if it is not available
0242     const IsoDeposit *isoDeposit(IsolationKeys key) const {
0243       for (IsoDepositPairs::const_iterator it = isoDeposits_.begin(), ed = isoDeposits_.end(); it != ed; ++it) {
0244         if (it->first == key)
0245           return &it->second;
0246       }
0247       return nullptr;
0248     }
0249 
0250     /// Sets the IsoDeposit associated with some key; if it is already existent, it is overwritten.
0251     void setIsoDeposit(IsolationKeys key, const IsoDeposit &dep) {
0252       IsoDepositPairs::iterator it = isoDeposits_.begin(), ed = isoDeposits_.end();
0253       for (; it != ed; ++it) {
0254         if (it->first == key) {
0255           it->second = dep;
0256           return;
0257         }
0258       }
0259       isoDeposits_.push_back(std::make_pair(key, dep));
0260     }
0261 
0262     // ---- specific getters ----
0263     const IsoDeposit *trackIsoDeposit() const { return isoDeposit(pat::TrackIso); }
0264     const IsoDeposit *ecalIsoDeposit() const { return isoDeposit(pat::EcalIso); }
0265     const IsoDeposit *hcalIsoDeposit() const { return isoDeposit(pat::HcalIso); }
0266     const IsoDeposit *userIsoDeposit(uint8_t index = 0) const { return isoDeposit(IsolationKeys(UserBaseIso + index)); }
0267 
0268     // ---- specific setters ----
0269     void trackIsoDeposit(const IsoDeposit &dep) { setIsoDeposit(pat::TrackIso, dep); }
0270     void ecalIsoDeposit(const IsoDeposit &dep) { setIsoDeposit(pat::EcalIso, dep); }
0271     void hcalIsoDeposit(const IsoDeposit &dep) { setIsoDeposit(pat::HcalIso, dep); }
0272     void userIsoDeposit(const IsoDeposit &dep, uint8_t index = 0) {
0273       setIsoDeposit(IsolationKeys(UserBaseIso + index), dep);
0274     }
0275 
0276     /// Vertex association (or associations, if any). Return null pointer if none has been set
0277     const pat::VertexAssociation *vertexAssociation(size_t index = 0) const {
0278       return vtxAss_.size() > index ? &vtxAss_[index] : nullptr;
0279     }
0280     /// Vertex associations. Can be empty if it was not enabled in the config file
0281     const std::vector<pat::VertexAssociation> &vertexAssociations() const { return vtxAss_; }
0282     /// Set a single vertex association
0283     void setVertexAssociation(const pat::VertexAssociation &assoc) {
0284       vtxAss_ = std::vector<pat::VertexAssociation>(1, assoc);
0285     }
0286     /// Set multiple vertex associations
0287     void setVertexAssociations(const std::vector<pat::VertexAssociation> &assocs) { vtxAss_ = assocs; }
0288 
0289   protected:
0290     // Any sort of single tracks
0291     reco::TrackRef trackRef_, standaloneTrackRef_, combinedTrackRef_;  // ref
0292     reco::TrackCollection track_, standaloneTrack_, combinedTrack_;    // embedded
0293 
0294     // GsfTrack
0295     reco::GsfTrackRef gsfTrackRef_;      // normal
0296     reco::GsfTrackCollection gsfTrack_;  // embedded
0297 
0298     // CaloTower
0299     CaloTowerRef caloTowerRef_;      // ref
0300     CaloTowerCollection caloTower_;  // embedded
0301 
0302     // SuperCluster
0303     reco::SuperClusterRef superClusterRef_;      // ref
0304     reco::SuperClusterCollection superCluster_;  // embedded
0305 
0306     // Multiple tracks
0307     reco::TrackRefVector trackRefs_;  // by ref
0308     reco::TrackCollection tracks_;    // embedded
0309 
0310     // information originally in external branches
0311     // some quality variable
0312     float quality_;
0313 
0314     // --- Isolation and IsoDeposit related datamebers ---
0315     typedef std::vector<std::pair<IsolationKeys, pat::IsoDeposit> > IsoDepositPairs;
0316     IsoDepositPairs isoDeposits_;
0317     std::vector<float> isolations_;
0318 
0319     // --- Vertexing information
0320     std::vector<pat::VertexAssociation> vtxAss_;
0321 
0322     void fillInFrom(const reco::Candidate &cand);
0323   };
0324 
0325 }  // namespace pat
0326 
0327 #endif