Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 11:05:33

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