Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
/*
 *  See header file for a description of this class.
 *
 *  \author Paolo Ronchese INFN Padova
 *
 */

//-----------------------
// This Class' Header --
//-----------------------
#include "HeavyFlavorAnalysis/RecoDecay/interface/BPHDecayMomentum.h"

//-------------------------------
// Collaborating Class Headers --
//-------------------------------
#include "HeavyFlavorAnalysis/RecoDecay/interface/BPHRecoCandidate.h"
#include "HeavyFlavorAnalysis/RecoDecay/interface/BPHAddFourMomenta.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"

//---------------
// C++ Headers --
//---------------
using namespace std;

//-------------------
// Initializations --
//-------------------

//----------------
// Constructors --
//----------------
BPHDecayMomentum::BPHDecayMomentum(int daugNum, int compNum) : oldMom(true) {
  nList.reserve(daugNum);
  dList.reserve(daugNum);
  nComp.reserve(compNum);
  cList.reserve(compNum);
}

BPHDecayMomentum::BPHDecayMomentum(const map<string, BPHDecayMomentum::Component>& daugMap, int compNum)
    : oldMom(true) {
  // clone and store simple particles
  clonesList(daugMap);
  nComp.reserve(compNum);
  cList.reserve(compNum);
}

BPHDecayMomentum::BPHDecayMomentum(const map<string, BPHDecayMomentum::Component>& daugMap,
                                   const map<string, BPHRecoConstCandPtr> compMap)
    :  // store the map of names to previously reconstructed particles
      cMap(compMap),
      oldMom(true) {
  // clone and store simple particles
  clonesList(daugMap);
  // store previously reconstructed particles using information in cMap
  dCompList();
}

//--------------
// Destructor --
//--------------
BPHDecayMomentum::~BPHDecayMomentum() {
  // delete all clones
  int n = dList.size();
  while (n--)
    delete dList[n];
}

//--------------
// Operations --
//--------------
const pat::CompositeCandidate& BPHDecayMomentum::composite() const {
  if (oldMom)
    computeMomentum();
  return compCand;
}

const vector<string>& BPHDecayMomentum::daugNames() const { return nList; }

const vector<string>& BPHDecayMomentum::compNames() const { return nComp; }

const vector<const reco::Candidate*>& BPHDecayMomentum::daughters() const { return dList; }

const vector<const reco::Candidate*>& BPHDecayMomentum::daughFull() const {
  // compute total momentum to update the full list of decay products
  if (oldMom)
    computeMomentum();
  return dFull;
}

const reco::Candidate* BPHDecayMomentum::originalReco(const reco::Candidate* daug) const {
  // return the original particle for a given clone
  // return null pointer if not found
  map<const reco::Candidate*, const reco::Candidate*>::const_iterator iter = clonesMap.find(daug);
  return (iter != clonesMap.end() ? iter->second : nullptr);
}

const vector<BPHRecoConstCandPtr>& BPHDecayMomentum::daughComp() const {
  // return the list of previously reconstructed particles
  return cList;
}

const reco::Candidate* BPHDecayMomentum::getDaug(const string& name) const {
  // return a simple particle from the name
  // return null pointer if not found
  string::size_type pos = name.find('/');
  if (pos != string::npos) {
    const BPHRecoCandidate* comp = getComp(name.substr(0, pos)).get();
    return (comp == nullptr ? nullptr : comp->getDaug(name.substr(pos + 1)));
  }
  map<const string, const reco::Candidate*>::const_iterator iter = dMap.find(name);
  return (iter != dMap.end() ? iter->second : nullptr);
}

BPHRecoConstCandPtr BPHDecayMomentum::getComp(const string& name) const {
  // return a previously reconstructed particle from the name
  // return null pointer if not found
  string::size_type pos = name.find('/');
  if (pos != string::npos) {
    const BPHRecoCandidate* comp = getComp(name.substr(0, pos)).get();
    return (comp == nullptr ? nullptr : comp->getComp(name.substr(pos + 1)));
  }
  map<const string, BPHRecoConstCandPtr>::const_iterator iter = cMap.find(name);
  return (iter != cMap.end() ? iter->second : nullptr);
}

const vector<BPHDecayMomentum::Component>& BPHDecayMomentum::componentList() const {
  // return an object filled in the constructor
  // to be used in the creation of other bases of BPHRecoCandidate
  return compList;
}

void BPHDecayMomentum::addP(const string& name, const reco::Candidate* daug, double mass) {
  if (dMap.find(name) != dMap.end()) {
    edm::LogPrint("TooManyParticles") << "BPHDecayMomentum::add: "
                                      << "Decay product already inserted with name " << name << " , skipped";
  }
  setNotUpdated();
  reco::Candidate* dnew = daug->clone();
  if (mass > 0.0)
    dnew->setMass(mass);
  nList.push_back(name);
  dList.push_back(dnew);
  dMap[name] = dnew;
  clonesMap[dnew] = daug;
  return;
}

void BPHDecayMomentum::addP(const string& name, const BPHRecoConstCandPtr& comp) {
  setNotUpdated();
  nComp.push_back(name);
  cList.push_back(comp);
  cMap[name] = comp;
  clonesMap.insert(comp->clonesMap.begin(), comp->clonesMap.end());
  return;
}

void BPHDecayMomentum::setNotUpdated() const {
  oldMom = true;
  return;
}

void BPHDecayMomentum::clonesList(const map<string, Component>& daugMap) {
  int n = daugMap.size();
  dList.resize(n);
  nList.resize(n);
  // reset and fill a list
  // to be used in the creation of other bases of BPHRecoCandidate
  compList.clear();
  compList.reserve(n);
  // loop over daughters
  int i = 0;
  double mass;
  reco::Candidate* dnew;
  map<string, Component>::const_iterator iter = daugMap.begin();
  map<string, Component>::const_iterator iend = daugMap.end();
  while (iter != iend) {
    const map<string, Component>::value_type& entry = *iter++;
    const Component& comp = entry.second;
    const reco::Candidate* cand = comp.cand;
    // store component for usage
    // in the creation of other bases of BPHRecoCandidate
    compList.push_back(comp);
    // clone particle and store it with its name
    dList[i] = dnew = cand->clone();
    const string& name = nList[i++] = entry.first;
    dMap[name] = dnew;
    clonesMap[dnew] = cand;
    // set daughter mass if requested
    mass = comp.mass;
    if (mass > 0)
      dnew->setMass(mass);
  }
  return;
}

void BPHDecayMomentum::dCompList() {
  // fill lists of previously reconstructed particles and their names
  // and retrieve cascade decay products
  int n = cMap.size();
  cList.resize(n);
  nComp.resize(n);
  int i = 0;
  map<string, BPHRecoConstCandPtr>::const_iterator c_iter = cMap.begin();
  map<string, BPHRecoConstCandPtr>::const_iterator c_iend = cMap.end();
  while (c_iter != c_iend) {
    const map<string, BPHRecoConstCandPtr>::value_type& c_entry = *c_iter++;
    nComp[i] = c_entry.first;
    BPHRecoConstCandPtr comp = c_entry.second;
    cList[i++] = comp;
    clonesMap.insert(comp->clonesMap.begin(), comp->clonesMap.end());
  }
  return;
}

void BPHDecayMomentum::sumMomentum(const vector<const reco::Candidate*>& dl, const vector<string>& dn) const {
  // add the particles to pat::CompositeCandidate
  int n = dl.size();
  while (n--)
    compCand.addDaughter(*dl[n], dn[n]);
  return;
}

void BPHDecayMomentum::fillDaug(vector<const reco::Candidate*>& ad, const string& name, vector<string>& an) const {
  // recursively fill the list of simple particles, produced
  // directly or in cascade decays
  ad.insert(ad.end(), dList.begin(), dList.end());
  vector<string>::const_iterator iter = nList.begin();
  vector<string>::const_iterator iend = nList.end();
  while (iter != iend)
    an.push_back(name + *iter++);
  int n = cList.size();
  while (n--)
    cList[n]->fillDaug(ad, name + nComp[n] + "/", an);
  return;
}

void BPHDecayMomentum::computeMomentum() const {
  // reset full list of daughters
  dFull.clear();
  dFull.reserve(10);
  vector<string> nFull;
  nFull.reserve(10);
  fillDaug(dFull, "", nFull);
  // reset and fill pat::CompositeCandidate
  compCand.clearDaughters();
  sumMomentum(dFull, nFull);
  // compute the total momentum
  AddFourMomenta addP4;
  addP4.set(compCand);
  oldMom = false;
  return;
}