Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-11-15 04:14:51

0001 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidate.h"
0002 //#include "DataFormats/ParticleFlowReco/interface/PFBlock.h"
0003 
0004 #include "DataFormats/MuonReco/interface/Muon.h"
0005 #include "DataFormats/GsfTrackReco/interface/GsfTrack.h"
0006 #include "DataFormats/ParticleFlowReco/interface/PFDisplacedVertex.h"
0007 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidateElectronExtra.h"
0008 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidatePhotonExtra.h"
0009 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidateEGammaExtra.h"
0010 #include "DataFormats/EgammaCandidates/interface/Conversion.h"
0011 #include "DataFormats/EgammaCandidates/interface/Photon.h"
0012 
0013 #include "FWCore/Utilities/interface/Exception.h"
0014 
0015 #include <ostream>
0016 #include <iomanip>
0017 
0018 using namespace reco;
0019 using namespace std;
0020 
0021 #include "DataFormats/ParticleFlowCandidate/src/CountBits.h"
0022 
0023 PFCandidate::PFCandidate()
0024     : elementsInBlocks_(nullptr),
0025       ecalERatio_(1.),
0026       hcalERatio_(1.),
0027       hoERatio_(1.),
0028       rawEcalEnergy_(0.),
0029       rawHcalEnergy_(0.),
0030       rawHoEnergy_(0.),
0031       ps1Energy_(0.),
0032       ps2Energy_(0.),
0033       flags_(0),
0034       deltaP_(0.),
0035       vertexType_(kCandVertex),
0036       mva_Isolated_(PFCandidate::bigMva_),
0037       mva_e_pi_(PFCandidate::bigMva_),
0038       mva_e_mu_(PFCandidate::bigMva_),
0039       mva_pi_mu_(PFCandidate::bigMva_),
0040       mva_nothing_gamma_(PFCandidate::bigMva_),
0041       mva_nothing_nh_(PFCandidate::bigMva_),
0042       mva_gamma_nh_(PFCandidate::bigMva_),
0043       dnn_e_sigIsolated_(PFCandidate::bigMva_),
0044       dnn_e_sigNonIsolated_(PFCandidate::bigMva_),
0045       dnn_e_bkgNonIsolated_(PFCandidate::bigMva_),
0046       dnn_e_bkgTau_(PFCandidate::bigMva_),
0047       dnn_e_bkgPhoton_(PFCandidate::bigMva_),
0048       dnn_gamma_(PFCandidate::bigMva_),
0049       getter_(nullptr),
0050       storedRefsBitPattern_(0),
0051       time_(0.f),
0052       timeError_(-1.f) {
0053   muonTrackType_ = reco::Muon::None;
0054 
0055   setPdgId(translateTypeToPdgId(X));
0056   refsInfo_.reserve(3);
0057   std::fill(hcalDepthEnergyFractions_.begin(), hcalDepthEnergyFractions_.end(), 0.f);
0058 }
0059 
0060 const math::XYZPoint& PFCandidate::vertex() const { return vertexLegacy(vertexType_); }
0061 
0062 PFCandidate::PFCandidate(const PFCandidatePtr& sourcePtr) : PFCandidate(*sourcePtr) {
0063   sourcePtr_ = sourcePtr;
0064   hcalDepthEnergyFractions_ = sourcePtr->hcalDepthEnergyFractions_;  // GP not sure it's needed
0065 }
0066 
0067 PFCandidate::PFCandidate(Charge charge, const LorentzVector& p4, ParticleType partId)
0068     : CompositeCandidate(charge, p4),
0069       elementsInBlocks_(nullptr),
0070       ecalERatio_(1.),
0071       hcalERatio_(1.),
0072       hoERatio_(1.),
0073       rawEcalEnergy_(0.),
0074       rawHcalEnergy_(0.),
0075       rawHoEnergy_(0.),
0076       ps1Energy_(0.),
0077       ps2Energy_(0.),
0078       flags_(0),
0079       deltaP_(0.),
0080       vertexType_(kCandVertex),
0081       mva_Isolated_(PFCandidate::bigMva_),
0082       mva_e_pi_(PFCandidate::bigMva_),
0083       mva_e_mu_(PFCandidate::bigMva_),
0084       mva_pi_mu_(PFCandidate::bigMva_),
0085       mva_nothing_gamma_(PFCandidate::bigMva_),
0086       mva_nothing_nh_(PFCandidate::bigMva_),
0087       mva_gamma_nh_(PFCandidate::bigMva_),
0088       dnn_e_sigIsolated_(PFCandidate::bigMva_),
0089       dnn_e_sigNonIsolated_(PFCandidate::bigMva_),
0090       dnn_e_bkgNonIsolated_(PFCandidate::bigMva_),
0091       dnn_e_bkgTau_(PFCandidate::bigMva_),
0092       dnn_e_bkgPhoton_(PFCandidate::bigMva_),
0093       dnn_gamma_(PFCandidate::bigMva_),
0094       getter_(nullptr),
0095       storedRefsBitPattern_(0),
0096       time_(0.f),
0097       timeError_(-1.f) {
0098   refsInfo_.reserve(3);
0099   blocksStorage_.reserve(10);
0100   elementsStorage_.reserve(10);
0101   std::fill(hcalDepthEnergyFractions_.begin(), hcalDepthEnergyFractions_.end(), 0.f);
0102 
0103   muonTrackType_ = reco::Muon::None;
0104 
0105   // proceed with various consistency checks
0106 
0107   // charged candidate: track ref and charge must be non null
0108   if (partId == h || partId == e || partId == mu) {
0109     if (charge == 0) {
0110       string err;
0111       err += "Attempt to construct a charged PFCandidate with a zero charge";
0112       throw cms::Exception("InconsistentValue", err.c_str());
0113     }
0114   } else {
0115     if (charge) {
0116       string err;
0117       err += "Attempt to construct a neutral PFCandidate ";
0118       err += "with a non-zero charge";
0119       throw cms::Exception("InconsistentValue", err.c_str());
0120     }
0121   }
0122   setPdgId(translateTypeToPdgId(partId));
0123 }
0124 
0125 PFCandidate::PFCandidate(PFCandidate const& iOther)
0126     : CompositeCandidate(iOther),
0127       elementsInBlocks_(nullptr),
0128       blocksStorage_(iOther.blocksStorage_),
0129       elementsStorage_(iOther.elementsStorage_),
0130       sourcePtr_(iOther.sourcePtr_),
0131       muonTrackType_(iOther.muonTrackType_),
0132       ecalERatio_(iOther.ecalERatio_),
0133       hcalERatio_(iOther.hcalERatio_),
0134       hoERatio_(iOther.hoERatio_),
0135       rawEcalEnergy_(iOther.rawEcalEnergy_),
0136       rawHcalEnergy_(iOther.rawHcalEnergy_),
0137       rawHoEnergy_(iOther.rawHoEnergy_),
0138       ps1Energy_(iOther.ps1Energy_),
0139       ps2Energy_(iOther.ps2Energy_),
0140       flags_(iOther.flags_),
0141       deltaP_(iOther.deltaP_),
0142       vertexType_(iOther.vertexType_),
0143       mva_Isolated_(iOther.mva_Isolated_),
0144       mva_e_pi_(iOther.mva_e_pi_),
0145       mva_e_mu_(iOther.mva_e_mu_),
0146       mva_pi_mu_(iOther.mva_pi_mu_),
0147       mva_nothing_gamma_(iOther.mva_nothing_gamma_),
0148       mva_nothing_nh_(iOther.mva_nothing_nh_),
0149       mva_gamma_nh_(iOther.mva_gamma_nh_),
0150       dnn_e_sigIsolated_(iOther.dnn_e_sigIsolated_),
0151       dnn_e_sigNonIsolated_(iOther.dnn_e_sigNonIsolated_),
0152       dnn_e_bkgNonIsolated_(iOther.dnn_e_bkgNonIsolated_),
0153       dnn_e_bkgTau_(iOther.dnn_e_bkgTau_),
0154       dnn_e_bkgPhoton_(iOther.dnn_e_bkgPhoton_),
0155       dnn_gamma_(iOther.dnn_gamma_),
0156       positionAtECALEntrance_(iOther.positionAtECALEntrance_),
0157       getter_(iOther.getter_),
0158       storedRefsBitPattern_(iOther.storedRefsBitPattern_),
0159       refsInfo_(iOther.refsInfo_),
0160       refsCollectionCache_(iOther.refsCollectionCache_),
0161       time_(iOther.time_),
0162       timeError_(iOther.timeError_),
0163       hcalDepthEnergyFractions_(iOther.hcalDepthEnergyFractions_) {
0164   auto tmp = iOther.elementsInBlocks_.load(std::memory_order_acquire);
0165   if (nullptr != tmp) {
0166     elementsInBlocks_.store(new ElementsInBlocks{*tmp}, std::memory_order_release);
0167   }
0168 }
0169 
0170 PFCandidate& PFCandidate::operator=(PFCandidate const& iOther) {
0171   CompositeCandidate::operator=(iOther);
0172   auto tmp = iOther.elementsInBlocks_.load(std::memory_order_acquire);
0173   if (nullptr != tmp) {
0174     delete elementsInBlocks_.exchange(new ElementsInBlocks{*tmp}, std::memory_order_acq_rel);
0175   } else {
0176     delete elementsInBlocks_.exchange(nullptr, std::memory_order_acq_rel);
0177   }
0178   blocksStorage_ = iOther.blocksStorage_;
0179   elementsStorage_ = iOther.elementsStorage_;
0180   sourcePtr_ = iOther.sourcePtr_;
0181   muonTrackType_ = iOther.muonTrackType_;
0182   ecalERatio_ = iOther.ecalERatio_;
0183   hcalERatio_ = iOther.hcalERatio_;
0184   hoERatio_ = iOther.hoERatio_;
0185   rawEcalEnergy_ = iOther.rawEcalEnergy_;
0186   rawHcalEnergy_ = iOther.rawHcalEnergy_;
0187   rawHoEnergy_ = iOther.rawHoEnergy_;
0188   ps1Energy_ = iOther.ps1Energy_;
0189   ps2Energy_ = iOther.ps2Energy_;
0190   flags_ = iOther.flags_;
0191   deltaP_ = iOther.deltaP_;
0192   vertexType_ = iOther.vertexType_;
0193   mva_Isolated_ = iOther.mva_Isolated_;
0194   mva_e_pi_ = iOther.mva_e_pi_;
0195   mva_e_mu_ = iOther.mva_e_mu_;
0196   mva_pi_mu_ = iOther.mva_pi_mu_;
0197   mva_nothing_gamma_ = iOther.mva_nothing_gamma_;
0198   mva_nothing_nh_ = iOther.mva_nothing_nh_;
0199   mva_gamma_nh_ = iOther.mva_gamma_nh_;
0200   dnn_e_sigIsolated_ = iOther.dnn_e_sigIsolated_;
0201   dnn_e_sigNonIsolated_ = iOther.dnn_e_sigNonIsolated_;
0202   dnn_e_bkgNonIsolated_ = iOther.dnn_e_bkgNonIsolated_;
0203   dnn_e_bkgTau_ = iOther.dnn_e_bkgTau_;
0204   dnn_e_bkgPhoton_ = iOther.dnn_e_bkgPhoton_;
0205   dnn_gamma_ = iOther.dnn_gamma_;
0206   positionAtECALEntrance_ = iOther.positionAtECALEntrance_;
0207   getter_ = iOther.getter_;
0208   storedRefsBitPattern_ = iOther.storedRefsBitPattern_;
0209   refsInfo_ = iOther.refsInfo_;
0210   refsCollectionCache_ = iOther.refsCollectionCache_;
0211   time_ = iOther.time_;
0212   timeError_ = iOther.timeError_;
0213   hcalDepthEnergyFractions_ = iOther.hcalDepthEnergyFractions_;
0214   return *this;
0215 }
0216 
0217 PFCandidate::~PFCandidate() { delete elementsInBlocks_.load(std::memory_order_acquire); }
0218 
0219 PFCandidate* PFCandidate::clone() const { return new PFCandidate(*this); }
0220 
0221 void PFCandidate::addElementInBlock(const reco::PFBlockRef& blockref, unsigned elementIndex) {
0222   //elementsInBlocks_.push_back( make_pair(blockref.key(), elementIndex) );
0223   if (blocksStorage_.empty())
0224     blocksStorage_ = Blocks(blockref.id());
0225   blocksStorage_.push_back(blockref);
0226   elementsStorage_.push_back(elementIndex);
0227   auto ptr = elementsInBlocks_.exchange(nullptr);
0228   delete ptr;
0229 }
0230 
0231 PFCandidate::ParticleType PFCandidate::translatePdgIdToType(int pdgid) const {
0232   switch (std::abs(pdgid)) {
0233     case 211:
0234       return h;
0235     case 11:
0236       return e;
0237     case 13:
0238       return mu;
0239     case 22:
0240       return gamma;
0241     case 130:
0242       return h0;
0243     case 1:
0244       return h_HF;
0245     case 2:
0246       return egamma_HF;
0247     case 0:
0248       return X;
0249     default:
0250       return X;
0251   }
0252 }
0253 
0254 int PFCandidate::translateTypeToPdgId(ParticleType type) const {
0255   int thecharge = charge();
0256 
0257   switch (type) {
0258     case h:
0259       return thecharge * 211;  // pi+
0260     case e:
0261       return thecharge * (-11);
0262     case mu:
0263       return thecharge * (-13);
0264     case gamma:
0265       return 22;
0266     case h0:
0267       return 130;  // K_L0
0268     case h_HF:
0269       return 1;  // dummy pdg code
0270     case egamma_HF:
0271       return 2;  // dummy pdg code
0272     case X:
0273     default:
0274       return 0;
0275   }
0276 }
0277 
0278 void PFCandidate::setParticleType(ParticleType type) { setPdgId(translateTypeToPdgId(type)); }
0279 
0280 bool PFCandidate::overlap(const reco::Candidate& other) const {
0281   CandidatePtr myPtr = sourceCandidatePtr(0);
0282   if (myPtr.isNull())
0283     return false;
0284   for (size_t i = 0, n = other.numberOfSourceCandidatePtrs(); i < n; ++i) {
0285     CandidatePtr otherPtr = other.sourceCandidatePtr(i);
0286     if ((otherPtr == myPtr) || (sourcePtr_.isNonnull() && otherPtr.isNonnull() && sourcePtr_->overlap(*otherPtr))) {
0287       return true;
0288     }
0289   }
0290   return false;
0291 }
0292 
0293 void PFCandidate::rescaleMomentum(double rescaleFactor) {
0294   LorentzVector rescaledp4 = p4();
0295   rescaledp4 *= rescaleFactor;
0296   setP4(rescaledp4);
0297 }
0298 
0299 void PFCandidate::setFlag(Flags theFlag, bool value) {
0300   if (value)
0301     flags_ = flags_ | (1 << theFlag);
0302   else
0303     flags_ = flags_ ^ (1 << theFlag);
0304 }
0305 
0306 bool PFCandidate::flag(Flags theFlag) const { return (flags_ >> theFlag) & 1; }
0307 
0308 ostream& reco::operator<<(ostream& out, const PFCandidate& c) {
0309   if (!out)
0310     return out;
0311 
0312   out << "\tPFCandidate type: " << c.particleId();
0313   out << setiosflags(ios::right);
0314   out << setiosflags(ios::fixed);
0315   out << setprecision(3);
0316   out << " E/pT/eta/phi " << c.energy() << "/" << c.pt() << "/" << c.eta() << "/" << c.phi();
0317   if (c.flag(PFCandidate::T_FROM_DISP))
0318     out << ", T_FROM_DISP" << endl;
0319   else if (c.flag(PFCandidate::T_TO_DISP))
0320     out << ", T_TO_DISP" << endl;
0321   else if (c.flag(PFCandidate::T_FROM_GAMMACONV))
0322     out << ", T_FROM_GAMMACONV" << endl;
0323   else if (c.flag(PFCandidate::GAMMA_TO_GAMMACONV))
0324     out << ", GAMMA_TO_GAMMACONV" << endl;
0325 
0326   out << ", blocks/iele: ";
0327 
0328   PFCandidate::ElementsInBlocks eleInBlocks = c.elementsInBlocks();
0329   for (unsigned i = 0; i < eleInBlocks.size(); i++) {
0330     PFBlockRef blockRef = eleInBlocks[i].first;
0331     unsigned indexInBlock = eleInBlocks[i].second;
0332 
0333     out << "(" << blockRef.key() << "|" << indexInBlock << "), ";
0334   }
0335 
0336   out << " source:" << c.sourcePtr_.id() << "/" << c.sourcePtr_.key();
0337 
0338   //   PFBlockRef blockRef = c.block();
0339   //   int blockid = blockRef.key();
0340   //   const edm::OwnVector< reco::PFBlockElement >& elements = c.elements();
0341   //   out<< "\t# of elements " << elements.size()
0342   //      <<" from block " << blockid << endl;
0343 
0344   //   // print each element in turn
0345 
0346   //   for(unsigned ie=0; ie<elements.size(); ie++) {
0347   //     out<<"\t"<< elements[ie] <<endl;
0348   //   }
0349 
0350   // Improved printout for electrons if PFCandidateElectronExtra is available
0351   if (c.particleId() == PFCandidate::e && c.electronExtraRef().isNonnull() && c.electronExtraRef().isAvailable()) {
0352     out << std::endl << *(c.electronExtraRef());
0353   }
0354   out << resetiosflags(ios::right | ios::fixed);
0355   return out;
0356 }
0357 
0358 static unsigned long long bitPackRefInfo(const edm::RefCore& iCore, size_t iIndex) {
0359   unsigned long long bitPack = iIndex;
0360   bitPack |= static_cast<unsigned long long>(iCore.id().productIndex()) << 32;
0361   bitPack |= static_cast<unsigned long long>(iCore.id().processIndex()) << 48;
0362   return bitPack;
0363 }
0364 
0365 void PFCandidate::storeRefInfo(unsigned int iMask,
0366                                unsigned int iBit,
0367                                bool iIsValid,
0368                                const edm::RefCore& iCore,
0369                                size_t iKey,
0370                                const edm::EDProductGetter* iGetter) {
0371   size_t index = s_refsBefore[storedRefsBitPattern_ & iMask];
0372   if (nullptr == getter_) {
0373     getter_ = iGetter;
0374   }
0375 
0376   if (iIsValid) {
0377     if (0 == (storedRefsBitPattern_ & iBit)) {
0378       refsInfo_.insert(refsInfo_.begin() + index, bitPackRefInfo(iCore, iKey));
0379       if (iGetter == nullptr)
0380         refsCollectionCache_.insert(refsCollectionCache_.begin() + index, static_cast<void const*>(iCore.productPtr()));
0381       else
0382         refsCollectionCache_.insert(refsCollectionCache_.begin() + index, nullptr);
0383     } else {
0384       assert(refsInfo_.size() > index);
0385       *(refsInfo_.begin() + index) = bitPackRefInfo(iCore, iKey);
0386       if (iGetter == nullptr)
0387         *(refsCollectionCache_.begin() + index) = static_cast<void const*>(iCore.productPtr());
0388       else
0389         *(refsCollectionCache_.begin() + index) = nullptr;
0390     }
0391     storedRefsBitPattern_ |= iBit;
0392   } else {
0393     if (storedRefsBitPattern_ & iBit) {
0394       refsInfo_.erase(refsInfo_.begin() + index);
0395       refsCollectionCache_.erase(refsCollectionCache_.begin() + index);
0396       storedRefsBitPattern_ ^= iBit;
0397     }
0398   }
0399 }
0400 
0401 bool PFCandidate::getRefInfo(
0402     unsigned int iMask, unsigned int iBit, edm::ProductID& oProductID, size_t& oIndex, size_t& aIndex) const {
0403   if (0 == (iBit & storedRefsBitPattern_)) {
0404     return false;
0405   }
0406   aIndex = s_refsBefore[storedRefsBitPattern_ & iMask];
0407   unsigned long long bitPacked = refsInfo_[aIndex];
0408   oIndex = bitPacked & 0xFFFFFFFFULL;  //low 32 bits are the index
0409   unsigned short productIndex = (bitPacked & 0x0000FFFF00000000ULL) >> 32;
0410   unsigned short processIndex = (bitPacked & 0xFFFF000000000000ULL) >> 48;
0411   oProductID = edm::ProductID(processIndex, productIndex);
0412   return true;
0413 }
0414 
0415 void PFCandidate::setTrackRef(const reco::TrackRef& iRef) {
0416   if (!charge()) {
0417     string err;
0418     err += "PFCandidate::setTrackRef: this is a neutral candidate! ";
0419     err += "particleId_=";
0420     char num[4];
0421     sprintf(num, "%d", particleId());
0422     err += num;
0423 
0424     throw cms::Exception("InconsistentReference", err.c_str());
0425   }
0426 
0427   storeRefInfo(kRefTrackMask, kRefTrackBit, iRef.isNonnull(), iRef.refCore(), iRef.key(), iRef.productGetter());
0428 }
0429 
0430 reco::TrackRef PFCandidate::trackRef() const { GETREF(reco::Track, kRefTrackMask, kRefTrackBit); }
0431 
0432 void PFCandidate::setMuonRef(reco::MuonRef const& iRef) {
0433   if (trackRef() != iRef->track()) {
0434     string err;
0435     err += "PFCandidate::setMuonRef: inconsistent track references!";
0436 
0437     throw cms::Exception("InconsistentReference", err.c_str());
0438   }
0439 
0440   storeRefInfo(kRefMuonMask, kRefMuonBit, iRef.isNonnull(), iRef.refCore(), iRef.key(), iRef.productGetter());
0441 }
0442 
0443 reco::MuonRef PFCandidate::muonRef() const { GETREF(reco::Muon, kRefMuonMask, kRefMuonBit); }
0444 
0445 //////////////
0446 void PFCandidate::setGsfTrackRef(reco::GsfTrackRef const& iRef) {
0447   //  Removed by F. Beaudette. Would like to be able to save the GsfTrackRef even for charged pions
0448   //  if( particleId() != e ) {
0449   //    string err;
0450   //    err += "PFCandidate::setGsfTrackRef: this is not an electron ! particleId_=";
0451   //    char num[4];
0452   //    sprintf( num, "%d", particleId());
0453   //    err += num;
0454   //
0455   //    throw cms::Exception("InconsistentReference",
0456   //                         err.c_str() );
0457   //  }
0458 
0459   storeRefInfo(kRefGsfTrackMask, kRefGsfTrackBit, iRef.isNonnull(), iRef.refCore(), iRef.key(), iRef.productGetter());
0460 }
0461 
0462 reco::GsfTrackRef PFCandidate::gsfTrackRef() const { GETREF(reco::GsfTrack, kRefGsfTrackMask, kRefGsfTrackBit); }
0463 
0464 //////////////
0465 void PFCandidate::setDisplacedVertexRef(const reco::PFDisplacedVertexRef& iRef, Flags type) {
0466   if (particleId() != h) {
0467     string err;
0468     err += "PFCandidate::setDisplacedVertexRef: this is not a hadron! particleId_=";
0469     char num[4];
0470     sprintf(num, "%d", particleId());
0471     err += num;
0472 
0473     throw cms::Exception("InconsistentReference", err.c_str());
0474   } else if (!flag(T_FROM_DISP) && !flag(T_TO_DISP)) {
0475     string err;
0476     err += "PFCandidate::setDisplacedVertexRef: particule flag is neither T_FROM_DISP nor T_TO_DISP";
0477 
0478     throw cms::Exception("InconsistentReference", err.c_str());
0479   }
0480 
0481   if (type == T_TO_DISP && flag(T_TO_DISP))
0482     storeRefInfo(kRefDisplacedVertexDauMask,
0483                  kRefDisplacedVertexDauBit,
0484                  iRef.isNonnull(),
0485                  iRef.refCore(),
0486                  iRef.key(),
0487                  iRef.productGetter());
0488   else if (type == T_FROM_DISP && flag(T_FROM_DISP))
0489     storeRefInfo(kRefDisplacedVertexMotMask,
0490                  kRefDisplacedVertexMotBit,
0491                  iRef.isNonnull(),
0492                  iRef.refCore(),
0493                  iRef.key(),
0494                  iRef.productGetter());
0495   else if ((type == T_FROM_DISP && !flag(T_FROM_DISP)) || (type == T_TO_DISP && !flag(T_TO_DISP))) {
0496     string err;
0497     err += "PFCandidate::setDisplacedVertexRef: particule flag is not switched on";
0498 
0499     throw cms::Exception("InconsistentReference", err.c_str());
0500   }
0501 }
0502 
0503 reco::PFDisplacedVertexRef PFCandidate::displacedVertexRef(Flags type) const {
0504   if (type == T_TO_DISP) {
0505     GETREF(reco::PFDisplacedVertex, kRefDisplacedVertexDauMask, kRefDisplacedVertexDauBit);
0506   } else if (type == T_FROM_DISP) {
0507     GETREF(reco::PFDisplacedVertex, kRefDisplacedVertexMotMask, kRefDisplacedVertexMotBit);
0508   }
0509   return reco::PFDisplacedVertexRef();
0510 }
0511 
0512 //////////////
0513 void PFCandidate::setConversionRef(reco::ConversionRef const& iRef) {
0514   if (particleId() != gamma) {
0515     string err;
0516     err += "PFCandidate::setConversionRef: this is not a (converted) photon ! particleId_=";
0517     char num[4];
0518     sprintf(num, "%d", particleId());
0519     err += num;
0520 
0521     throw cms::Exception("InconsistentReference", err.c_str());
0522   } else if (!flag(GAMMA_TO_GAMMACONV)) {
0523     string err;
0524     err += "PFCandidate::setConversionRef: particule flag is not GAMMA_TO_GAMMACONV";
0525 
0526     throw cms::Exception("InconsistentReference", err.c_str());
0527   }
0528 
0529   storeRefInfo(
0530       kRefConversionMask, kRefConversionBit, iRef.isNonnull(), iRef.refCore(), iRef.key(), iRef.productGetter());
0531 }
0532 
0533 reco::ConversionRef PFCandidate::conversionRef() const {
0534   GETREF(reco::Conversion, kRefConversionMask, kRefConversionBit);
0535 }
0536 
0537 //////////////
0538 void PFCandidate::setV0Ref(reco::VertexCompositeCandidateRef const& iRef) {
0539   storeRefInfo(kRefV0Mask, kRefV0Bit, iRef.isNonnull(), iRef.refCore(), iRef.key(), iRef.productGetter());
0540 }
0541 
0542 reco::VertexCompositeCandidateRef PFCandidate::v0Ref() const {
0543   GETREF(reco::VertexCompositeCandidate, kRefV0Mask, kRefV0Bit);
0544 }
0545 
0546 //////////////
0547 void PFCandidate::setGsfElectronRef(reco::GsfElectronRef const& iRef) {
0548   storeRefInfo(
0549       kRefGsfElectronMask, kRefGsfElectronBit, iRef.isNonnull(), iRef.refCore(), iRef.key(), iRef.productGetter());
0550 }
0551 
0552 reco::GsfElectronRef PFCandidate::gsfElectronRef() const {
0553   GETREF(reco::GsfElectron, kRefGsfElectronMask, kRefGsfElectronBit);
0554 }
0555 
0556 //////////////
0557 void PFCandidate::setPFElectronExtraRef(reco::PFCandidateElectronExtraRef const& iRef) {
0558   storeRefInfo(kRefPFElectronExtraMask,
0559                kRefPFElectronExtraBit,
0560                iRef.isNonnull(),
0561                iRef.refCore(),
0562                iRef.key(),
0563                iRef.productGetter());
0564 }
0565 
0566 reco::PFCandidateElectronExtraRef PFCandidate::electronExtraRef() const {
0567   GETREF(reco::PFCandidateElectronExtra, kRefPFElectronExtraMask, kRefPFElectronExtraBit);
0568 }
0569 
0570 reco::PhotonRef PFCandidate::photonRef() const { GETREF(reco::Photon, kRefPhotonMask, kRefPhotonBit); }
0571 
0572 reco::PFCandidatePhotonExtraRef PFCandidate::photonExtraRef() const {
0573   GETREF(reco::PFCandidatePhotonExtra, kRefPFPhotonExtraMask, kRefPFPhotonExtraBit);
0574 }
0575 
0576 reco::PFCandidateEGammaExtraRef PFCandidate::egammaExtraRef() const {
0577   GETREF(reco::PFCandidateEGammaExtra, kRefPFEGammaExtraMask, kRefPFEGammaExtraBit);
0578 }
0579 
0580 reco::SuperClusterRef PFCandidate::superClusterRef() const {
0581   GETREF(reco::SuperCluster, kRefSuperClusterMask, kRefSuperClusterBit);
0582 }
0583 
0584 void PFCandidate::setPhotonRef(const reco::PhotonRef& iRef) {
0585   if (particleId() != gamma && particleId() != e) {
0586     string err;
0587     err += "PFCandidate::setSuperClusterRef: this is not an electron neither a photon ! particleId_=";
0588     char num[4];
0589     sprintf(num, "%d", particleId());
0590     err += num;
0591 
0592     throw cms::Exception("InconsistentReference", err.c_str());
0593   }
0594 
0595   storeRefInfo(kRefPhotonMask, kRefPhotonBit, iRef.isNonnull(), iRef.refCore(), iRef.key(), iRef.productGetter());
0596 }
0597 
0598 void PFCandidate::setSuperClusterRef(const reco::SuperClusterRef& iRef) {
0599   if (particleId() != gamma && particleId() != e) {
0600     string err;
0601     err += "PFCandidate::setSuperClusterRef: this is not an electron neither a photon ! particleId_=";
0602     char num[4];
0603     sprintf(num, "%d", particleId());
0604     err += num;
0605 
0606     throw cms::Exception("InconsistentReference", err.c_str());
0607   }
0608 
0609   storeRefInfo(
0610       kRefSuperClusterMask, kRefSuperClusterBit, iRef.isNonnull(), iRef.refCore(), iRef.key(), iRef.productGetter());
0611 }
0612 
0613 void PFCandidate::setPFPhotonExtraRef(const reco::PFCandidatePhotonExtraRef& iRef) {
0614   storeRefInfo(
0615       kRefPFPhotonExtraMask, kRefPFPhotonExtraBit, iRef.isNonnull(), iRef.refCore(), iRef.key(), iRef.productGetter());
0616 }
0617 
0618 void PFCandidate::setPFEGammaExtraRef(const reco::PFCandidateEGammaExtraRef& iRef) {
0619   storeRefInfo(
0620       kRefPFEGammaExtraMask, kRefPFEGammaExtraBit, iRef.isNonnull(), iRef.refCore(), iRef.key(), iRef.productGetter());
0621 }
0622 
0623 const math::XYZPoint& PFCandidate::vertexLegacy(PFCandidate::PFVertexType vertexType) const {
0624   switch (vertexType) {
0625     case kCandVertex:
0626       return LeafCandidate::vertex();
0627       break;
0628     //the following cases will only be called for legacy AOD which does not have an embedded vertex
0629     case kTrkVertex:
0630       return trackRef()->vertex();
0631       break;
0632     case kComMuonVertex:
0633       return muonRef()->combinedMuon()->vertex();
0634       break;
0635     case kSAMuonVertex:
0636       return muonRef()->standAloneMuon()->vertex();
0637       break;
0638     case kTrkMuonVertex:
0639       return muonRef()->track()->vertex();
0640       break;
0641     case kTPFMSMuonVertex:
0642       return muonRef()->tpfmsTrack()->vertex();
0643       break;
0644     case kPickyMuonVertex:
0645       return muonRef()->pickyTrack()->vertex();
0646       break;
0647     case kDYTMuonVertex:
0648       return muonRef()->dytTrack()->vertex();
0649       break;
0650 
0651     case kGSFVertex:
0652       return gsfTrackRef()->vertex();
0653       break;
0654   }
0655   return LeafCandidate::vertex();
0656 }
0657 
0658 const PFCandidate::ElementsInBlocks& PFCandidate::elementsInBlocks() const {
0659   if (nullptr == elementsInBlocks_.load(std::memory_order_acquire)) {
0660     std::unique_ptr<ElementsInBlocks> temp(new ElementsInBlocks(blocksStorage_.size()));
0661     for (unsigned int icopy = 0; icopy != blocksStorage_.size(); ++icopy)
0662       (*temp)[icopy] = std::make_pair(blocksStorage_[icopy], elementsStorage_[icopy]);
0663     ElementsInBlocks* expected = nullptr;
0664     if (elementsInBlocks_.compare_exchange_strong(expected, temp.get(), std::memory_order_acq_rel)) {
0665       temp.release();
0666     }
0667   }
0668   return *(elementsInBlocks_.load(std::memory_order_acquire));
0669 }