Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:49:20

0001 /*
0002  *  See header file for a description of this class.
0003  *
0004  *  \author Paolo Ronchese INFN Padova
0005  *
0006  */
0007 
0008 //-----------------------
0009 // This Class' Header --
0010 //-----------------------
0011 #include "HeavyFlavorAnalysis/RecoDecay/interface/BPHDecayMomentum.h"
0012 
0013 //-------------------------------
0014 // Collaborating Class Headers --
0015 //-------------------------------
0016 #include "HeavyFlavorAnalysis/RecoDecay/interface/BPHRecoCandidate.h"
0017 #include "HeavyFlavorAnalysis/RecoDecay/interface/BPHAddFourMomenta.h"
0018 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0019 
0020 //---------------
0021 // C++ Headers --
0022 //---------------
0023 using namespace std;
0024 
0025 //-------------------
0026 // Initializations --
0027 //-------------------
0028 
0029 //----------------
0030 // Constructors --
0031 //----------------
0032 BPHDecayMomentum::BPHDecayMomentum() : oldMom(true) { dList.reserve(2); }
0033 
0034 BPHDecayMomentum::BPHDecayMomentum(const map<string, BPHDecayMomentum::Component>& daugMap) : oldMom(true) {
0035   // clone and store simple particles
0036   clonesList(daugMap);
0037 }
0038 
0039 BPHDecayMomentum::BPHDecayMomentum(const map<string, BPHDecayMomentum::Component>& daugMap,
0040                                    const map<string, BPHRecoConstCandPtr> compMap)
0041     :  // store the map of names to previously reconstructed particles
0042       cMap(compMap),
0043       oldMom(true) {
0044   // clone and store simple particles
0045   clonesList(daugMap);
0046   // store previously reconstructed particles using information in cMap
0047   dCompList();
0048 }
0049 
0050 //--------------
0051 // Destructor --
0052 //--------------
0053 BPHDecayMomentum::~BPHDecayMomentum() {
0054   // delete all clones
0055   int n = dList.size();
0056   while (n--)
0057     delete dList[n];
0058 }
0059 
0060 //--------------
0061 // Operations --
0062 //--------------
0063 const pat::CompositeCandidate& BPHDecayMomentum::composite() const {
0064   if (oldMom)
0065     computeMomentum();
0066   return compCand;
0067 }
0068 
0069 const vector<string>& BPHDecayMomentum::daugNames() const { return nList; }
0070 
0071 const vector<string>& BPHDecayMomentum::compNames() const { return nComp; }
0072 
0073 const vector<const reco::Candidate*>& BPHDecayMomentum::daughters() const { return dList; }
0074 
0075 const vector<const reco::Candidate*>& BPHDecayMomentum::daughFull() const {
0076   // compute total momentum to update the full list of decay products
0077   if (oldMom)
0078     computeMomentum();
0079   return dFull;
0080 }
0081 
0082 const reco::Candidate* BPHDecayMomentum::originalReco(const reco::Candidate* daug) const {
0083   // return the original particle for a given clone
0084   // return null pointer if not found
0085   map<const reco::Candidate*, const reco::Candidate*>::const_iterator iter = clonesMap.find(daug);
0086   return (iter != clonesMap.end() ? iter->second : nullptr);
0087 }
0088 
0089 const vector<BPHRecoConstCandPtr>& BPHDecayMomentum::daughComp() const {
0090   // return the list of previously reconstructed particles
0091   return cList;
0092 }
0093 
0094 const reco::Candidate* BPHDecayMomentum::getDaug(const string& name) const {
0095   // return a simple particle from the name
0096   // return null pointer if not found
0097   string::size_type pos = name.find('/');
0098   if (pos != string::npos) {
0099     const BPHRecoCandidate* comp = getComp(name.substr(0, pos)).get();
0100     return (comp == nullptr ? nullptr : comp->getDaug(name.substr(pos + 1)));
0101   }
0102   map<const string, const reco::Candidate*>::const_iterator iter = dMap.find(name);
0103   return (iter != dMap.end() ? iter->second : nullptr);
0104 }
0105 
0106 BPHRecoConstCandPtr BPHDecayMomentum::getComp(const string& name) const {
0107   // return a previously reconstructed particle from the name
0108   // return null pointer if not found
0109   string::size_type pos = name.find('/');
0110   if (pos != string::npos) {
0111     const BPHRecoCandidate* comp = getComp(name.substr(0, pos)).get();
0112     return (comp == nullptr ? nullptr : comp->getComp(name.substr(pos + 1)));
0113   }
0114   map<const string, BPHRecoConstCandPtr>::const_iterator iter = cMap.find(name);
0115   return (iter != cMap.end() ? iter->second : nullptr);
0116 }
0117 
0118 const vector<BPHDecayMomentum::Component>& BPHDecayMomentum::componentList() const {
0119   // return an object filled in the constructor
0120   // to be used in the creation of other bases of BPHRecoCandidate
0121   return compList;
0122 }
0123 
0124 void BPHDecayMomentum::addP(const string& name, const reco::Candidate* daug, double mass) {
0125   if (dMap.find(name) != dMap.end()) {
0126     edm::LogPrint("TooManyParticles") << "BPHDecayMomentum::add: "
0127                                       << "Decay product already inserted with name " << name << " , skipped";
0128   }
0129   setNotUpdated();
0130   reco::Candidate* dnew = daug->clone();
0131   if (mass > 0.0)
0132     dnew->setMass(mass);
0133   nList.push_back(name);
0134   dList.push_back(dnew);
0135   dMap[name] = dnew;
0136   clonesMap[dnew] = daug;
0137   return;
0138 }
0139 
0140 void BPHDecayMomentum::addP(const string& name, const BPHRecoConstCandPtr& comp) {
0141   setNotUpdated();
0142   nComp.push_back(name);
0143   cList.push_back(comp);
0144   cMap[name] = comp;
0145   clonesMap.insert(comp->clonesMap.begin(), comp->clonesMap.end());
0146   return;
0147 }
0148 
0149 void BPHDecayMomentum::setNotUpdated() const {
0150   oldMom = true;
0151   return;
0152 }
0153 
0154 void BPHDecayMomentum::clonesList(const map<string, Component>& daugMap) {
0155   int n = daugMap.size();
0156   dList.resize(n);
0157   nList.resize(n);
0158   // reset and fill a list
0159   // to be used in the creation of other bases of BPHRecoCandidate
0160   compList.clear();
0161   compList.reserve(n);
0162   // loop over daughters
0163   int i = 0;
0164   double mass;
0165   reco::Candidate* dnew;
0166   map<string, Component>::const_iterator iter = daugMap.begin();
0167   map<string, Component>::const_iterator iend = daugMap.end();
0168   while (iter != iend) {
0169     const pair<string, Component>& entry = *iter++;
0170     const Component& comp = entry.second;
0171     const reco::Candidate* cand = comp.cand;
0172     // store component for usage
0173     // in the creation of other bases of BPHRecoCandidate
0174     compList.push_back(comp);
0175     // clone particle and store it with its name
0176     dList[i] = dnew = cand->clone();
0177     const string& name = nList[i++] = entry.first;
0178     dMap[name] = dnew;
0179     clonesMap[dnew] = cand;
0180     // set daughter mass if requested
0181     mass = comp.mass;
0182     if (mass > 0)
0183       dnew->setMass(mass);
0184   }
0185   return;
0186 }
0187 
0188 void BPHDecayMomentum::dCompList() {
0189   // fill lists of previously reconstructed particles and their names
0190   // and retrieve cascade decay products
0191   int n = cMap.size();
0192   cList.resize(n);
0193   nComp.resize(n);
0194   int i = 0;
0195   map<string, BPHRecoConstCandPtr>::const_iterator iter = cMap.begin();
0196   map<string, BPHRecoConstCandPtr>::const_iterator iend = cMap.end();
0197   while (iter != iend) {
0198     const pair<string, BPHRecoConstCandPtr>& entry = *iter++;
0199     nComp[i] = entry.first;
0200     BPHRecoConstCandPtr comp = entry.second;
0201     cList[i++] = comp;
0202     clonesMap.insert(comp->clonesMap.begin(), comp->clonesMap.end());
0203   }
0204   return;
0205 }
0206 
0207 void BPHDecayMomentum::sumMomentum(const vector<const reco::Candidate*> dl) const {
0208   // add the particles to pat::CompositeCandidate
0209   int n = dl.size();
0210   while (n--)
0211     compCand.addDaughter(*dl[n]);
0212   return;
0213 }
0214 
0215 void BPHDecayMomentum::fillDaug(vector<const reco::Candidate*>& ad) const {
0216   // recursively fill the list of simple particles, produced
0217   // directly or in cascade decays
0218   ad.insert(ad.end(), dList.begin(), dList.end());
0219   int n = cList.size();
0220   while (n--)
0221     cList[n]->fillDaug(ad);
0222   return;
0223 }
0224 
0225 void BPHDecayMomentum::computeMomentum() const {
0226   // reset full list of daughters
0227   dFull.clear();
0228   fillDaug(dFull);
0229   // reset and fill pat::CompositeCandidate
0230   compCand.clearDaughters();
0231   sumMomentum(dFull);
0232   // compute the total momentum
0233   AddFourMomenta addP4;
0234   addP4.set(compCand);
0235   oldMom = false;
0236   return;
0237 }