00001 // 00002 // $Id: GenericParticle.cc,v 1.4 2009/03/25 22:33:29 hegner Exp $ 00003 // 00004 00005 #include "DataFormats/PatCandidates/interface/GenericParticle.h" 00006 00007 00008 using pat::GenericParticle; 00009 00010 00012 GenericParticle::GenericParticle() : 00013 PATObject<reco::RecoCandidate>() 00014 { 00015 } 00016 00017 00019 GenericParticle::GenericParticle(const Candidate & cand) : 00020 PATObject<reco::RecoCandidate>() 00021 { 00022 fillInFrom(cand); 00023 } 00024 00025 00027 GenericParticle::GenericParticle(const edm::RefToBase<Candidate> & cand) : 00028 PATObject<reco::RecoCandidate>() 00029 { 00030 fillInFrom(*cand); 00031 refToOrig_ = edm::Ptr<reco::Candidate>(cand.id(), cand.get(), cand.key()); // correct RefToBase=>Ptr conversion 00032 } 00033 00035 GenericParticle::GenericParticle(const edm::Ptr<Candidate> & cand) : 00036 PATObject<reco::RecoCandidate>() 00037 { 00038 fillInFrom(*cand); 00039 refToOrig_ = cand; 00040 } 00041 00042 00043 00045 GenericParticle::~GenericParticle() { 00046 } 00047 00048 // ====== SETTERS ===== 00050 void GenericParticle::setTrack(const reco::TrackRef &ref, bool embed) { 00051 trackRef_ = ref; 00052 if (embed) embedTrack(); else track_.clear(); 00053 } 00054 00056 void GenericParticle::setTracks(const reco::TrackRefVector &refs, bool embed) { 00057 trackRefs_ = refs; 00058 if (embed) embedTracks(); else tracks_.clear(); 00059 } 00060 00062 void GenericParticle::setStandAloneMuon(const reco::TrackRef &ref, bool embed) { 00063 standaloneTrackRef_ = ref; 00064 if (embed) embedStandalone(); else standaloneTrack_.clear(); 00065 } 00066 00068 void GenericParticle::setCombinedMuon(const reco::TrackRef &ref, bool embed) { 00069 combinedTrackRef_ = ref; 00070 if (embed) embedCombined(); else combinedTrack_.clear(); 00071 } 00072 00074 void GenericParticle::setGsfTrack(const reco::GsfTrackRef &ref, bool embed) { 00075 gsfTrackRef_ = ref; 00076 if (embed) embedGsfTrack(); else gsfTrack_.clear(); 00077 } 00078 00080 void GenericParticle::setSuperCluster(const reco::SuperClusterRef &ref, bool embed) { 00081 superClusterRef_ = ref; 00082 if (embed) embedSuperCluster(); else superCluster_.clear(); 00083 } 00084 00086 void GenericParticle::setCaloTower(const CaloTowerRef &ref, bool embed) { 00087 caloTowerRef_ = ref; 00088 if (embed) { 00089 embedCaloTower(); 00090 } else if (!caloTower_.empty()) { 00091 CaloTowerCollection().swap(caloTower_); 00092 } 00093 } 00094 00095 00096 // ========== EMBEDDER METHODS 00098 void GenericParticle::embedTrack() { 00099 track_.clear(); 00100 if (trackRef_.isNonnull()) track_.push_back(*trackRef_); // import 00101 trackRef_ = reco::TrackRef(); // clear, to save space (zeroes compress better) 00102 } 00104 void GenericParticle::embedTracks() { 00105 tracks_.clear(); 00106 tracks_.reserve(trackRefs_.size()); 00107 for (reco::TrackRefVector::const_iterator it = trackRefs_.begin(); it != trackRefs_.end(); ++it) { 00108 if (it->isNonnull()) tracks_.push_back(**it); // embed track 00109 } 00110 trackRefs_ = reco::TrackRefVector(); // clear, to save space 00111 } 00113 void GenericParticle::embedStandalone() { 00114 standaloneTrack_.clear(); 00115 if (standaloneTrackRef_.isNonnull()) standaloneTrack_.push_back(*standaloneTrackRef_); // import 00116 standaloneTrackRef_ = reco::TrackRef(); // clear, to save space (zeroes compress better) 00117 } 00119 void GenericParticle::embedCombined() { 00120 combinedTrack_.clear(); 00121 if (combinedTrackRef_.isNonnull()) combinedTrack_.push_back(*combinedTrackRef_); // import 00122 combinedTrackRef_ = reco::TrackRef(); // clear, to save space (zeroes compress better) 00123 } 00125 void GenericParticle::embedGsfTrack() { 00126 gsfTrack_.clear(); 00127 if (gsfTrackRef_.isNonnull()) gsfTrack_.push_back(*gsfTrackRef_); // import 00128 gsfTrackRef_ = reco::GsfTrackRef(); // clear, to save space (zeroes compress better) 00129 } 00130 00132 void GenericParticle::embedSuperCluster() { 00133 superCluster_.clear(); 00134 if (superClusterRef_.isNonnull()) superCluster_.push_back(*superClusterRef_); // import 00135 superClusterRef_ = reco::SuperClusterRef(); // clear, to save space (zeroes compress better) 00136 } 00138 void GenericParticle::embedCaloTower() { 00139 if (!caloTower_.empty()) CaloTowerCollection().swap(caloTower_); 00140 if (caloTowerRef_.isNonnull()) caloTower_.push_back(*caloTowerRef_); // import 00141 caloTowerRef_ = CaloTowerRef(); // clear, to save space (zeroes compress better) 00142 } 00143 00144 00145 void GenericParticle::fillInFrom(const reco::Candidate &cand) { 00146 // first, kinematics & status 00147 setCharge(cand.charge()); 00148 setP4(cand.polarP4()); 00149 setVertex(cand.vertex()); 00150 setPdgId(cand.pdgId()); 00151 setStatus(cand.status()); 00152 // then RECO part, if available 00153 const reco::RecoCandidate *rc = dynamic_cast<const reco::RecoCandidate *>(&cand); 00154 if (rc != 0) { 00155 setTrack(rc->track()); 00156 setGsfTrack(rc->gsfTrack()); 00157 setStandAloneMuon(rc->standAloneMuon()); 00158 setCombinedMuon(rc->combinedMuon()); 00159 setSuperCluster(rc->superCluster()); 00160 setCaloTower(rc->caloTower()); 00161 size_t ntracks = rc->numberOfTracks(); 00162 if (ntracks > 0) { 00163 reco::TrackRefVector tracks; 00164 for (size_t i = 0; i < ntracks; ++i) { 00165 tracks.push_back(rc->track(i)); 00166 } 00167 setTracks(tracks); 00168 } 00169 } 00170 } 00171 00172 bool GenericParticle::overlap( const reco::Candidate &cand ) const { 00173 const reco::RecoCandidate *rc = dynamic_cast<const reco::RecoCandidate *>(&cand); 00174 if (rc != 0) { 00175 if (rc->track().isNonnull() && (track() == rc->track())) return true; 00176 if (rc->gsfTrack().isNonnull() && (gsfTrack() == rc->gsfTrack())) return true; 00177 if (rc->standAloneMuon().isNonnull() && (standAloneMuon() == rc->standAloneMuon())) return true; 00178 if (rc->combinedMuon().isNonnull() && (combinedMuon() == rc->combinedMuon())) return true; 00179 if (rc->superCluster().isNonnull() && (superCluster() == rc->superCluster())) return true; 00180 if (rc->caloTower().isNonnull() && (caloTower() == rc->caloTower())) return true; 00181 } 00182 const GenericParticle *rc2 = dynamic_cast<const GenericParticle *>(&cand); 00183 if (rc2 != 0) { 00184 if (rc2->track().isNonnull() && (track() == rc2->track())) return true; 00185 if (rc2->gsfTrack().isNonnull() && (gsfTrack() == rc2->gsfTrack())) return true; 00186 if (rc2->standAloneMuon().isNonnull() && (standAloneMuon() == rc2->standAloneMuon())) return true; 00187 if (rc2->combinedMuon().isNonnull() && (combinedMuon() == rc2->combinedMuon())) return true; 00188 if (rc2->superCluster().isNonnull() && (superCluster() == rc2->superCluster())) return true; 00189 if (rc2->caloTower().isNonnull() && (caloTower() == rc2->caloTower())) return true; 00190 } 00191 return false; 00192 }