Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:04: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 // Rescale three-momentum, preserving mass
0294 void PFCandidate::rescaleMomentum(double rescaleFactor) {
0295   if (rescaleFactor < 0)
0296     throw cms::Exception(
0297         "NegativeScaling",
0298         "Scale factor " + std::to_string(rescaleFactor) + " is < 0. Cannot rescale momentum by this value");
0299 
0300   float e = std::sqrt(p() * p() * rescaleFactor * rescaleFactor + mass() * mass());
0301 
0302   LorentzVector rescaledp4(rescaleFactor * px(), rescaleFactor * py(), rescaleFactor * pz(), e);
0303   setP4(rescaledp4);
0304 }
0305 
0306 void PFCandidate::setFlag(Flags theFlag, bool value) {
0307   if (value)
0308     flags_ = flags_ | (1 << theFlag);
0309   else
0310     flags_ = flags_ ^ (1 << theFlag);
0311 }
0312 
0313 bool PFCandidate::flag(Flags theFlag) const { return (flags_ >> theFlag) & 1; }
0314 
0315 ostream& reco::operator<<(ostream& out, const PFCandidate& c) {
0316   if (!out)
0317     return out;
0318 
0319   out << "\tPFCandidate type: " << c.particleId();
0320   out << setiosflags(ios::right);
0321   out << setiosflags(ios::fixed);
0322   out << setprecision(3);
0323   out << " E/pT/eta/phi " << c.energy() << "/" << c.pt() << "/" << c.eta() << "/" << c.phi();
0324   if (c.flag(PFCandidate::T_FROM_DISP))
0325     out << ", T_FROM_DISP" << endl;
0326   else if (c.flag(PFCandidate::T_TO_DISP))
0327     out << ", T_TO_DISP" << endl;
0328   else if (c.flag(PFCandidate::T_FROM_GAMMACONV))
0329     out << ", T_FROM_GAMMACONV" << endl;
0330   else if (c.flag(PFCandidate::GAMMA_TO_GAMMACONV))
0331     out << ", GAMMA_TO_GAMMACONV" << endl;
0332 
0333   out << ", blocks/iele: ";
0334 
0335   PFCandidate::ElementsInBlocks eleInBlocks = c.elementsInBlocks();
0336   for (unsigned i = 0; i < eleInBlocks.size(); i++) {
0337     PFBlockRef blockRef = eleInBlocks[i].first;
0338     unsigned indexInBlock = eleInBlocks[i].second;
0339 
0340     out << "(" << blockRef.key() << "|" << indexInBlock << "), ";
0341   }
0342 
0343   out << " source:" << c.sourcePtr_.id() << "/" << c.sourcePtr_.key();
0344 
0345   //   PFBlockRef blockRef = c.block();
0346   //   int blockid = blockRef.key();
0347   //   const edm::OwnVector< reco::PFBlockElement >& elements = c.elements();
0348   //   out<< "\t# of elements " << elements.size()
0349   //      <<" from block " << blockid << endl;
0350 
0351   //   // print each element in turn
0352 
0353   //   for(unsigned ie=0; ie<elements.size(); ie++) {
0354   //     out<<"\t"<< elements[ie] <<endl;
0355   //   }
0356 
0357   // Improved printout for electrons if PFCandidateElectronExtra is available
0358   if (c.particleId() == PFCandidate::e && c.electronExtraRef().isNonnull() && c.electronExtraRef().isAvailable()) {
0359     out << std::endl << *(c.electronExtraRef());
0360   }
0361   out << resetiosflags(ios::right | ios::fixed);
0362   return out;
0363 }
0364 
0365 static unsigned long long bitPackRefInfo(const edm::RefCore& iCore, size_t iIndex) {
0366   unsigned long long bitPack = iIndex;
0367   bitPack |= static_cast<unsigned long long>(iCore.id().productIndex()) << 32;
0368   bitPack |= static_cast<unsigned long long>(iCore.id().processIndex()) << 48;
0369   return bitPack;
0370 }
0371 
0372 void PFCandidate::storeRefInfo(unsigned int iMask,
0373                                unsigned int iBit,
0374                                bool iIsValid,
0375                                const edm::RefCore& iCore,
0376                                size_t iKey,
0377                                const edm::EDProductGetter* iGetter) {
0378   size_t index = s_refsBefore[storedRefsBitPattern_ & iMask];
0379   if (nullptr == getter_) {
0380     getter_ = iGetter;
0381   }
0382 
0383   if (iIsValid) {
0384     if (0 == (storedRefsBitPattern_ & iBit)) {
0385       refsInfo_.insert(refsInfo_.begin() + index, bitPackRefInfo(iCore, iKey));
0386       if (iGetter == nullptr)
0387         refsCollectionCache_.insert(refsCollectionCache_.begin() + index, static_cast<void const*>(iCore.productPtr()));
0388       else
0389         refsCollectionCache_.insert(refsCollectionCache_.begin() + index, nullptr);
0390     } else {
0391       assert(refsInfo_.size() > index);
0392       *(refsInfo_.begin() + index) = bitPackRefInfo(iCore, iKey);
0393       if (iGetter == nullptr)
0394         *(refsCollectionCache_.begin() + index) = static_cast<void const*>(iCore.productPtr());
0395       else
0396         *(refsCollectionCache_.begin() + index) = nullptr;
0397     }
0398     storedRefsBitPattern_ |= iBit;
0399   } else {
0400     if (storedRefsBitPattern_ & iBit) {
0401       refsInfo_.erase(refsInfo_.begin() + index);
0402       refsCollectionCache_.erase(refsCollectionCache_.begin() + index);
0403       storedRefsBitPattern_ ^= iBit;
0404     }
0405   }
0406 }
0407 
0408 bool PFCandidate::getRefInfo(
0409     unsigned int iMask, unsigned int iBit, edm::ProductID& oProductID, size_t& oIndex, size_t& aIndex) const {
0410   if (0 == (iBit & storedRefsBitPattern_)) {
0411     return false;
0412   }
0413   aIndex = s_refsBefore[storedRefsBitPattern_ & iMask];
0414   unsigned long long bitPacked = refsInfo_[aIndex];
0415   oIndex = bitPacked & 0xFFFFFFFFULL;  //low 32 bits are the index
0416   unsigned short productIndex = (bitPacked & 0x0000FFFF00000000ULL) >> 32;
0417   unsigned short processIndex = (bitPacked & 0xFFFF000000000000ULL) >> 48;
0418   oProductID = edm::ProductID(processIndex, productIndex);
0419   return true;
0420 }
0421 
0422 void PFCandidate::setTrackRef(const reco::TrackRef& iRef) {
0423   if (!charge()) {
0424     string err;
0425     err += "PFCandidate::setTrackRef: this is a neutral candidate! ";
0426     err += "particleId_=";
0427     char num[4];
0428     sprintf(num, "%d", particleId());
0429     err += num;
0430 
0431     throw cms::Exception("InconsistentReference", err.c_str());
0432   }
0433 
0434   storeRefInfo(kRefTrackMask, kRefTrackBit, iRef.isNonnull(), iRef.refCore(), iRef.key(), iRef.productGetter());
0435 }
0436 
0437 reco::TrackRef PFCandidate::trackRef() const { GETREF(reco::Track, kRefTrackMask, kRefTrackBit); }
0438 
0439 void PFCandidate::setMuonRef(reco::MuonRef const& iRef) {
0440   if (trackRef() != iRef->track()) {
0441     string err;
0442     err += "PFCandidate::setMuonRef: inconsistent track references!";
0443 
0444     throw cms::Exception("InconsistentReference", err.c_str());
0445   }
0446 
0447   storeRefInfo(kRefMuonMask, kRefMuonBit, iRef.isNonnull(), iRef.refCore(), iRef.key(), iRef.productGetter());
0448 }
0449 
0450 reco::MuonRef PFCandidate::muonRef() const { GETREF(reco::Muon, kRefMuonMask, kRefMuonBit); }
0451 
0452 //////////////
0453 void PFCandidate::setGsfTrackRef(reco::GsfTrackRef const& iRef) {
0454   //  Removed by F. Beaudette. Would like to be able to save the GsfTrackRef even for charged pions
0455   //  if( particleId() != e ) {
0456   //    string err;
0457   //    err += "PFCandidate::setGsfTrackRef: this is not an electron ! particleId_=";
0458   //    char num[4];
0459   //    sprintf( num, "%d", particleId());
0460   //    err += num;
0461   //
0462   //    throw cms::Exception("InconsistentReference",
0463   //                         err.c_str() );
0464   //  }
0465 
0466   storeRefInfo(kRefGsfTrackMask, kRefGsfTrackBit, iRef.isNonnull(), iRef.refCore(), iRef.key(), iRef.productGetter());
0467 }
0468 
0469 reco::GsfTrackRef PFCandidate::gsfTrackRef() const { GETREF(reco::GsfTrack, kRefGsfTrackMask, kRefGsfTrackBit); }
0470 
0471 //////////////
0472 void PFCandidate::setDisplacedVertexRef(const reco::PFDisplacedVertexRef& iRef, Flags type) {
0473   if (particleId() != h) {
0474     string err;
0475     err += "PFCandidate::setDisplacedVertexRef: this is not a hadron! particleId_=";
0476     char num[4];
0477     sprintf(num, "%d", particleId());
0478     err += num;
0479 
0480     throw cms::Exception("InconsistentReference", err.c_str());
0481   } else if (!flag(T_FROM_DISP) && !flag(T_TO_DISP)) {
0482     string err;
0483     err += "PFCandidate::setDisplacedVertexRef: particule flag is neither T_FROM_DISP nor T_TO_DISP";
0484 
0485     throw cms::Exception("InconsistentReference", err.c_str());
0486   }
0487 
0488   if (type == T_TO_DISP && flag(T_TO_DISP))
0489     storeRefInfo(kRefDisplacedVertexDauMask,
0490                  kRefDisplacedVertexDauBit,
0491                  iRef.isNonnull(),
0492                  iRef.refCore(),
0493                  iRef.key(),
0494                  iRef.productGetter());
0495   else if (type == T_FROM_DISP && flag(T_FROM_DISP))
0496     storeRefInfo(kRefDisplacedVertexMotMask,
0497                  kRefDisplacedVertexMotBit,
0498                  iRef.isNonnull(),
0499                  iRef.refCore(),
0500                  iRef.key(),
0501                  iRef.productGetter());
0502   else if ((type == T_FROM_DISP && !flag(T_FROM_DISP)) || (type == T_TO_DISP && !flag(T_TO_DISP))) {
0503     string err;
0504     err += "PFCandidate::setDisplacedVertexRef: particule flag is not switched on";
0505 
0506     throw cms::Exception("InconsistentReference", err.c_str());
0507   }
0508 }
0509 
0510 reco::PFDisplacedVertexRef PFCandidate::displacedVertexRef(Flags type) const {
0511   if (type == T_TO_DISP) {
0512     GETREF(reco::PFDisplacedVertex, kRefDisplacedVertexDauMask, kRefDisplacedVertexDauBit);
0513   } else if (type == T_FROM_DISP) {
0514     GETREF(reco::PFDisplacedVertex, kRefDisplacedVertexMotMask, kRefDisplacedVertexMotBit);
0515   }
0516   return reco::PFDisplacedVertexRef();
0517 }
0518 
0519 //////////////
0520 void PFCandidate::setConversionRef(reco::ConversionRef const& iRef) {
0521   if (particleId() != gamma) {
0522     string err;
0523     err += "PFCandidate::setConversionRef: this is not a (converted) photon ! particleId_=";
0524     char num[4];
0525     sprintf(num, "%d", particleId());
0526     err += num;
0527 
0528     throw cms::Exception("InconsistentReference", err.c_str());
0529   } else if (!flag(GAMMA_TO_GAMMACONV)) {
0530     string err;
0531     err += "PFCandidate::setConversionRef: particule flag is not GAMMA_TO_GAMMACONV";
0532 
0533     throw cms::Exception("InconsistentReference", err.c_str());
0534   }
0535 
0536   storeRefInfo(
0537       kRefConversionMask, kRefConversionBit, iRef.isNonnull(), iRef.refCore(), iRef.key(), iRef.productGetter());
0538 }
0539 
0540 reco::ConversionRef PFCandidate::conversionRef() const {
0541   GETREF(reco::Conversion, kRefConversionMask, kRefConversionBit);
0542 }
0543 
0544 //////////////
0545 void PFCandidate::setV0Ref(reco::VertexCompositeCandidateRef const& iRef) {
0546   storeRefInfo(kRefV0Mask, kRefV0Bit, iRef.isNonnull(), iRef.refCore(), iRef.key(), iRef.productGetter());
0547 }
0548 
0549 reco::VertexCompositeCandidateRef PFCandidate::v0Ref() const {
0550   GETREF(reco::VertexCompositeCandidate, kRefV0Mask, kRefV0Bit);
0551 }
0552 
0553 //////////////
0554 void PFCandidate::setGsfElectronRef(reco::GsfElectronRef const& iRef) {
0555   storeRefInfo(
0556       kRefGsfElectronMask, kRefGsfElectronBit, iRef.isNonnull(), iRef.refCore(), iRef.key(), iRef.productGetter());
0557 }
0558 
0559 reco::GsfElectronRef PFCandidate::gsfElectronRef() const {
0560   GETREF(reco::GsfElectron, kRefGsfElectronMask, kRefGsfElectronBit);
0561 }
0562 
0563 //////////////
0564 void PFCandidate::setPFElectronExtraRef(reco::PFCandidateElectronExtraRef const& iRef) {
0565   storeRefInfo(kRefPFElectronExtraMask,
0566                kRefPFElectronExtraBit,
0567                iRef.isNonnull(),
0568                iRef.refCore(),
0569                iRef.key(),
0570                iRef.productGetter());
0571 }
0572 
0573 reco::PFCandidateElectronExtraRef PFCandidate::electronExtraRef() const {
0574   GETREF(reco::PFCandidateElectronExtra, kRefPFElectronExtraMask, kRefPFElectronExtraBit);
0575 }
0576 
0577 reco::PhotonRef PFCandidate::photonRef() const { GETREF(reco::Photon, kRefPhotonMask, kRefPhotonBit); }
0578 
0579 reco::PFCandidatePhotonExtraRef PFCandidate::photonExtraRef() const {
0580   GETREF(reco::PFCandidatePhotonExtra, kRefPFPhotonExtraMask, kRefPFPhotonExtraBit);
0581 }
0582 
0583 reco::PFCandidateEGammaExtraRef PFCandidate::egammaExtraRef() const {
0584   GETREF(reco::PFCandidateEGammaExtra, kRefPFEGammaExtraMask, kRefPFEGammaExtraBit);
0585 }
0586 
0587 reco::SuperClusterRef PFCandidate::superClusterRef() const {
0588   GETREF(reco::SuperCluster, kRefSuperClusterMask, kRefSuperClusterBit);
0589 }
0590 
0591 void PFCandidate::setPhotonRef(const reco::PhotonRef& iRef) {
0592   if (particleId() != gamma && particleId() != e) {
0593     string err;
0594     err += "PFCandidate::setSuperClusterRef: this is not an electron neither a photon ! particleId_=";
0595     char num[4];
0596     sprintf(num, "%d", particleId());
0597     err += num;
0598 
0599     throw cms::Exception("InconsistentReference", err.c_str());
0600   }
0601 
0602   storeRefInfo(kRefPhotonMask, kRefPhotonBit, iRef.isNonnull(), iRef.refCore(), iRef.key(), iRef.productGetter());
0603 }
0604 
0605 void PFCandidate::setSuperClusterRef(const reco::SuperClusterRef& iRef) {
0606   if (particleId() != gamma && particleId() != e) {
0607     string err;
0608     err += "PFCandidate::setSuperClusterRef: this is not an electron neither a photon ! particleId_=";
0609     char num[4];
0610     sprintf(num, "%d", particleId());
0611     err += num;
0612 
0613     throw cms::Exception("InconsistentReference", err.c_str());
0614   }
0615 
0616   storeRefInfo(
0617       kRefSuperClusterMask, kRefSuperClusterBit, iRef.isNonnull(), iRef.refCore(), iRef.key(), iRef.productGetter());
0618 }
0619 
0620 void PFCandidate::setPFPhotonExtraRef(const reco::PFCandidatePhotonExtraRef& iRef) {
0621   storeRefInfo(
0622       kRefPFPhotonExtraMask, kRefPFPhotonExtraBit, iRef.isNonnull(), iRef.refCore(), iRef.key(), iRef.productGetter());
0623 }
0624 
0625 void PFCandidate::setPFEGammaExtraRef(const reco::PFCandidateEGammaExtraRef& iRef) {
0626   storeRefInfo(
0627       kRefPFEGammaExtraMask, kRefPFEGammaExtraBit, iRef.isNonnull(), iRef.refCore(), iRef.key(), iRef.productGetter());
0628 }
0629 
0630 const math::XYZPoint& PFCandidate::vertexLegacy(PFCandidate::PFVertexType vertexType) const {
0631   switch (vertexType) {
0632     case kCandVertex:
0633       return LeafCandidate::vertex();
0634       break;
0635     //the following cases will only be called for legacy AOD which does not have an embedded vertex
0636     case kTrkVertex:
0637       return trackRef()->vertex();
0638       break;
0639     case kComMuonVertex:
0640       return muonRef()->combinedMuon()->vertex();
0641       break;
0642     case kSAMuonVertex:
0643       return muonRef()->standAloneMuon()->vertex();
0644       break;
0645     case kTrkMuonVertex:
0646       return muonRef()->track()->vertex();
0647       break;
0648     case kTPFMSMuonVertex:
0649       return muonRef()->tpfmsTrack()->vertex();
0650       break;
0651     case kPickyMuonVertex:
0652       return muonRef()->pickyTrack()->vertex();
0653       break;
0654     case kDYTMuonVertex:
0655       return muonRef()->dytTrack()->vertex();
0656       break;
0657 
0658     case kGSFVertex:
0659       return gsfTrackRef()->vertex();
0660       break;
0661   }
0662   return LeafCandidate::vertex();
0663 }
0664 
0665 const PFCandidate::ElementsInBlocks& PFCandidate::elementsInBlocks() const {
0666   if (nullptr == elementsInBlocks_.load(std::memory_order_acquire)) {
0667     std::unique_ptr<ElementsInBlocks> temp(new ElementsInBlocks(blocksStorage_.size()));
0668     for (unsigned int icopy = 0; icopy != blocksStorage_.size(); ++icopy)
0669       (*temp)[icopy] = std::make_pair(blocksStorage_[icopy], elementsStorage_[icopy]);
0670     ElementsInBlocks* expected = nullptr;
0671     if (elementsInBlocks_.compare_exchange_strong(expected, temp.get(), std::memory_order_acq_rel)) {
0672       temp.release();
0673     }
0674   }
0675   return *(elementsInBlocks_.load(std::memory_order_acquire));
0676 }