Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 //
0002 //
0003 
0004 #include "DataFormats/PatCandidates/interface/GenericParticle.h"
0005 
0006 using pat::GenericParticle;
0007 
0008 /// default constructor
0009 GenericParticle::GenericParticle() : PATObject<reco::RecoCandidate>() {}
0010 
0011 /// constructor from Candidate
0012 GenericParticle::GenericParticle(const Candidate &cand) : PATObject<reco::RecoCandidate>() { fillInFrom(cand); }
0013 
0014 /// constructor from ref to RecoCandidate
0015 GenericParticle::GenericParticle(const edm::RefToBase<Candidate> &cand) : PATObject<reco::RecoCandidate>() {
0016   fillInFrom(*cand);
0017   refToOrig_ = edm::Ptr<reco::Candidate>(cand.id(), cand.get(), cand.key());  // correct RefToBase=>Ptr conversion
0018 }
0019 
0020 /// constructor from ref to RecoCandidate
0021 GenericParticle::GenericParticle(const edm::Ptr<Candidate> &cand) : PATObject<reco::RecoCandidate>() {
0022   fillInFrom(*cand);
0023   refToOrig_ = cand;
0024 }
0025 
0026 /// destructor
0027 GenericParticle::~GenericParticle() {}
0028 
0029 // ====== SETTERS =====
0030 /// sets master track reference (or even embed it into the object)
0031 void GenericParticle::setTrack(const reco::TrackRef &ref, bool embed) {
0032   trackRef_ = ref;
0033   if (embed)
0034     embedTrack();
0035   else
0036     track_.clear();
0037 }
0038 
0039 /// sets multiple track references (or even embed the tracks into the object - whatch out for disk size issues!)
0040 void GenericParticle::setTracks(const reco::TrackRefVector &refs, bool embed) {
0041   trackRefs_ = refs;
0042   if (embed)
0043     embedTracks();
0044   else
0045     tracks_.clear();
0046 }
0047 
0048 /// sets stand-alone muon track reference (or even embed it into the object)
0049 void GenericParticle::setStandAloneMuon(const reco::TrackRef &ref, bool embed) {
0050   standaloneTrackRef_ = ref;
0051   if (embed)
0052     embedStandalone();
0053   else
0054     standaloneTrack_.clear();
0055 }
0056 
0057 /// sets combined muon track reference (or even embed it into the object)
0058 void GenericParticle::setCombinedMuon(const reco::TrackRef &ref, bool embed) {
0059   combinedTrackRef_ = ref;
0060   if (embed)
0061     embedCombined();
0062   else
0063     combinedTrack_.clear();
0064 }
0065 
0066 /// sets gsf track reference (or even embed it into the object)
0067 void GenericParticle::setGsfTrack(const reco::GsfTrackRef &ref, bool embed) {
0068   gsfTrackRef_ = ref;
0069   if (embed)
0070     embedGsfTrack();
0071   else
0072     gsfTrack_.clear();
0073 }
0074 
0075 /// sets supercluster reference (or even embed it into the object)
0076 void GenericParticle::setSuperCluster(const reco::SuperClusterRef &ref, bool embed) {
0077   superClusterRef_ = ref;
0078   if (embed)
0079     embedSuperCluster();
0080   else
0081     superCluster_.clear();
0082 }
0083 
0084 /// sets calotower reference (or even embed it into the object)
0085 void GenericParticle::setCaloTower(const CaloTowerRef &ref, bool embed) {
0086   caloTowerRef_ = ref;
0087   if (embed) {
0088     embedCaloTower();
0089   } else if (!caloTower_.empty()) {
0090     CaloTowerCollection().swap(caloTower_);
0091   }
0092 }
0093 
0094 // ========== EMBEDDER METHODS
0095 /// embeds the master track instead of keeping a reference to it
0096 void GenericParticle::embedTrack() {
0097   track_.clear();
0098   if (trackRef_.isNonnull())
0099     track_.push_back(*trackRef_);  // import
0100   trackRef_ = reco::TrackRef();    // clear, to save space (zeroes compress better)
0101 }
0102 /// embeds the other tracks instead of keeping references
0103 void GenericParticle::embedTracks() {
0104   tracks_.clear();
0105   tracks_.reserve(trackRefs_.size());
0106   for (reco::TrackRefVector::const_iterator it = trackRefs_.begin(); it != trackRefs_.end(); ++it) {
0107     if (it->isNonnull())
0108       tracks_.push_back(**it);  // embed track
0109   }
0110   trackRefs_ = reco::TrackRefVector();  // clear, to save space
0111 }
0112 /// embeds the stand-alone track instead of keeping a reference to it
0113 void GenericParticle::embedStandalone() {
0114   standaloneTrack_.clear();
0115   if (standaloneTrackRef_.isNonnull())
0116     standaloneTrack_.push_back(*standaloneTrackRef_);  // import
0117   standaloneTrackRef_ = reco::TrackRef();              // clear, to save space (zeroes compress better)
0118 }
0119 /// embeds the combined track instead of keeping a reference to it
0120 void GenericParticle::embedCombined() {
0121   combinedTrack_.clear();
0122   if (combinedTrackRef_.isNonnull())
0123     combinedTrack_.push_back(*combinedTrackRef_);  // import
0124   combinedTrackRef_ = reco::TrackRef();            // clear, to save space (zeroes compress better)
0125 }
0126 /// embeds the gsf track instead of keeping a reference to it
0127 void GenericParticle::embedGsfTrack() {
0128   gsfTrack_.clear();
0129   if (gsfTrackRef_.isNonnull())
0130     gsfTrack_.push_back(*gsfTrackRef_);  // import
0131   gsfTrackRef_ = reco::GsfTrackRef();    // clear, to save space (zeroes compress better)
0132 }
0133 
0134 /// embeds the supercluster instead of keeping a reference to it
0135 void GenericParticle::embedSuperCluster() {
0136   superCluster_.clear();
0137   if (superClusterRef_.isNonnull())
0138     superCluster_.push_back(*superClusterRef_);  // import
0139   superClusterRef_ = reco::SuperClusterRef();    // clear, to save space (zeroes compress better)
0140 }
0141 /// embeds the calotower instead of keeping a reference to it
0142 void GenericParticle::embedCaloTower() {
0143   if (!caloTower_.empty())
0144     CaloTowerCollection().swap(caloTower_);
0145   if (caloTowerRef_.isNonnull())
0146     caloTower_.push_back(*caloTowerRef_);  // import
0147   caloTowerRef_ = CaloTowerRef();          // clear, to save space (zeroes compress better)
0148 }
0149 
0150 void GenericParticle::fillInFrom(const reco::Candidate &cand) {
0151   // first, kinematics & status
0152   setCharge(cand.charge());
0153   setP4(cand.polarP4());
0154   setVertex(cand.vertex());
0155   setPdgId(cand.pdgId());
0156   setStatus(cand.status());
0157   // then RECO part, if available
0158   const reco::RecoCandidate *rc = dynamic_cast<const reco::RecoCandidate *>(&cand);
0159   if (rc != nullptr) {
0160     setTrack(rc->track());
0161     setGsfTrack(rc->gsfTrack());
0162     setStandAloneMuon(rc->standAloneMuon());
0163     setCombinedMuon(rc->combinedMuon());
0164     setSuperCluster(rc->superCluster());
0165     setCaloTower(rc->caloTower());
0166     size_t ntracks = rc->numberOfTracks();
0167     if (ntracks > 0) {
0168       reco::TrackRefVector tracks;
0169       for (size_t i = 0; i < ntracks; ++i) {
0170         tracks.push_back(rc->track(i));
0171       }
0172       setTracks(tracks);
0173     }
0174   }
0175 }
0176 
0177 bool GenericParticle::overlap(const reco::Candidate &cand) const {
0178   const reco::RecoCandidate *rc = dynamic_cast<const reco::RecoCandidate *>(&cand);
0179   if (rc != nullptr) {
0180     if (rc->track().isNonnull() && (track() == rc->track()))
0181       return true;
0182     if (rc->gsfTrack().isNonnull() && (gsfTrack() == rc->gsfTrack()))
0183       return true;
0184     if (rc->standAloneMuon().isNonnull() && (standAloneMuon() == rc->standAloneMuon()))
0185       return true;
0186     if (rc->combinedMuon().isNonnull() && (combinedMuon() == rc->combinedMuon()))
0187       return true;
0188     if (rc->superCluster().isNonnull() && (superCluster() == rc->superCluster()))
0189       return true;
0190     if (rc->caloTower().isNonnull() && (caloTower() == rc->caloTower()))
0191       return true;
0192   }
0193   const GenericParticle *rc2 = dynamic_cast<const GenericParticle *>(&cand);
0194   if (rc2 != nullptr) {
0195     if (rc2->track().isNonnull() && (track() == rc2->track()))
0196       return true;
0197     if (rc2->gsfTrack().isNonnull() && (gsfTrack() == rc2->gsfTrack()))
0198       return true;
0199     if (rc2->standAloneMuon().isNonnull() && (standAloneMuon() == rc2->standAloneMuon()))
0200       return true;
0201     if (rc2->combinedMuon().isNonnull() && (combinedMuon() == rc2->combinedMuon()))
0202       return true;
0203     if (rc2->superCluster().isNonnull() && (superCluster() == rc2->superCluster()))
0204       return true;
0205     if (rc2->caloTower().isNonnull() && (caloTower() == rc2->caloTower()))
0206       return true;
0207   }
0208   return false;
0209 }