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
#include "FWCore/Utilities/interface/EDMException.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"

#include "CommonTools/CandUtils/interface/pdgIdUtils.h"
#include "DataFormats/HepMCCandidate/interface/GenParticle.h"
#include "AnalysisDataFormats/TopObjects/interface/TopGenEvent.h"

/// default contructor
TopGenEvent::TopGenEvent(reco::GenParticleRefProd& decaySubset, reco::GenParticleRefProd& initSubset) {
  parts_ = decaySubset;
  initPartons_ = initSubset;
}

const reco::GenParticle* TopGenEvent::candidate(int id, unsigned int parentId) const {
  const reco::GenParticle* cand = nullptr;
  const reco::GenParticleCollection& partsColl = *parts_;
  for (unsigned int i = 0; i < partsColl.size(); ++i) {
    if (partsColl[i].pdgId() == id) {
      if (parentId == 0 ? true : partsColl[i].mother() && std::abs(partsColl[i].mother()->pdgId()) == (int)parentId) {
        cand = &partsColl[i];
      }
    }
  }
  return cand;
}

void TopGenEvent::print() const {
  edm::LogVerbatim log("TopGenEvent");
  log << "\n"
      << "--------------------------------------\n"
      << "- Dump TopGenEvent Content           -\n"
      << "--------------------------------------\n";
  for (reco::GenParticleCollection::const_iterator part = parts_->begin(); part < parts_->end(); ++part) {
    log << "pdgId:" << std::setw(5) << part->pdgId() << ", "
        << "mass:" << std::setw(11) << part->p4().mass() << ", "
        << "energy:" << std::setw(11) << part->energy() << ", "
        << "pt:" << std::setw(11) << part->pt() << "\n";
  }
}

int TopGenEvent::numberOfLeptons(bool fromWBoson) const {
  int lep = 0;
  const reco::GenParticleCollection& partsColl = *parts_;
  for (unsigned int i = 0; i < partsColl.size(); ++i) {
    if (reco::isLepton(partsColl[i])) {
      if (fromWBoson) {
        if (partsColl[i].mother() && std::abs(partsColl[i].mother()->pdgId()) == TopDecayID::WID) {
          ++lep;
        }
      } else {
        ++lep;
      }
    }
  }
  return lep;
}

int TopGenEvent::numberOfLeptons(WDecay::LeptonType typeRestriction, bool fromWBoson) const {
  int leptonType = -1;
  switch (typeRestriction) {
      // resolve whether or not there is
      // any restriction in lepton types
    case WDecay::kElec:
      leptonType = TopDecayID::elecID;
      break;
    case WDecay::kMuon:
      leptonType = TopDecayID::muonID;
      break;
    case WDecay::kTau:
      leptonType = TopDecayID::tauID;
      break;
    case WDecay::kNone:
      break;
  }
  int lep = 0;
  const reco::GenParticleCollection& partsColl = *parts_;
  for (unsigned int i = 0; i < partsColl.size(); ++i) {
    if (fromWBoson) {
      // restrict to particles originating from the W boson
      if (!(partsColl[i].mother() && std::abs(partsColl[i].mother()->pdgId()) == TopDecayID::WID)) {
        continue;
      }
    }
    if (leptonType > 0) {
      // in case of lepton type restriction
      if (std::abs(partsColl[i].pdgId()) == leptonType) {
        ++lep;
      }
    } else {
      // take any lepton type into account else
      if (reco::isLepton(partsColl[i])) {
        ++lep;
      }
    }
  }
  return lep;
}

int TopGenEvent::numberOfBQuarks(bool fromTopQuark) const {
  int bq = 0;
  const reco::GenParticleCollection& partsColl = *parts_;
  for (unsigned int i = 0; i < partsColl.size(); ++i) {
    //depend if radiation qqbar are included or not
    if (std::abs(partsColl[i].pdgId()) == TopDecayID::bID) {
      if (fromTopQuark) {
        if (partsColl[i].mother() && std::abs(partsColl[i].mother()->pdgId()) == TopDecayID::tID) {
          ++bq;
        }
      } else {
        ++bq;
      }
    }
  }
  return bq;
}

std::vector<const reco::GenParticle*> TopGenEvent::topSisters() const {
  std::vector<const reco::GenParticle*> sisters;
  for (reco::GenParticleCollection::const_iterator part = parts_->begin(); part < parts_->end(); ++part) {
    if (part->numberOfMothers() == 0 && std::abs(part->pdgId()) != TopDecayID::tID) {
      // choose top sister which do not have a
      // mother and are whether top nor anti-top
      if (dynamic_cast<const reco::GenParticle*>(&(*part)) == nullptr) {
        throw edm::Exception(edm::errors::InvalidReference, "Not a GenParticle");
      }
      sisters.push_back(part->clone());
    }
  }
  return sisters;
}

const reco::GenParticle* TopGenEvent::daughterQuarkOfTop(bool invertCharge) const {
  const reco::GenParticle* cand = nullptr;
  for (reco::GenParticleCollection::const_iterator top = parts_->begin(); top < parts_->end(); ++top) {
    if (top->pdgId() == (invertCharge ? -TopDecayID::tID : TopDecayID::tID)) {
      for (reco::GenParticle::const_iterator quark = top->begin(); quark < top->end(); ++quark) {
        if (std::abs(quark->pdgId()) <= TopDecayID::bID) {
          cand = dynamic_cast<const reco::GenParticle*>(&(*quark));
          if (cand == nullptr) {
            throw edm::Exception(edm::errors::InvalidReference, "Not a GenParticle");
          }
          break;
        }
      }
    }
  }
  return cand;
}

const reco::GenParticle* TopGenEvent::daughterQuarkOfWPlus(bool invertQuarkCharge, bool invertBosonCharge) const {
  const reco::GenParticle* cand = nullptr;
  const reco::GenParticleCollection& partsColl = *parts_;
  for (unsigned int i = 0; i < partsColl.size(); ++i) {
    if (partsColl[i].mother() &&
        partsColl[i].mother()->pdgId() == (invertBosonCharge ? -TopDecayID::WID : TopDecayID::WID) &&
        std::abs(partsColl[i].pdgId()) <= TopDecayID::bID &&
        (invertQuarkCharge ? reco::flavour(partsColl[i]) < 0 : reco::flavour(partsColl[i]) > 0)) {
      cand = &partsColl[i];
    }
  }
  return cand;
}

std::vector<const reco::GenParticle*> TopGenEvent::lightQuarks(bool includingBQuarks) const {
  std::vector<const reco::GenParticle*> lightQuarks;
  for (reco::GenParticleCollection::const_iterator part = parts_->begin(); part < parts_->end(); ++part) {
    if ((includingBQuarks && std::abs(part->pdgId()) == TopDecayID::bID) || std::abs(part->pdgId()) < TopDecayID::bID) {
      if (dynamic_cast<const reco::GenParticle*>(&(*part)) == nullptr) {
        throw edm::Exception(edm::errors::InvalidReference, "Not a GenParticle");
      }
      lightQuarks.push_back(part->clone());
    }
  }
  return lightQuarks;
}

std::vector<const reco::GenParticle*> TopGenEvent::radiatedGluons(int pdgId) const {
  std::vector<const reco::GenParticle*> rads;
  for (reco::GenParticleCollection::const_iterator part = parts_->begin(); part < parts_->end(); ++part) {
    if (part->mother() && part->mother()->pdgId() == pdgId) {
      if (part->pdgId() == TopDecayID::glueID) {
        if (dynamic_cast<const reco::GenParticle*>(&(*part)) == nullptr) {
          throw edm::Exception(edm::errors::InvalidReference, "Not a GenParticle");
        }
      }
      rads.push_back(part->clone());
    }
  }
  return rads;
}