File indexing completed on 2023-03-17 10:49:18
0001 #include "DataFormats/Candidate/interface/CompositeCandidate.h"
0002 #include "FWCore/Utilities/interface/Exception.h"
0003
0004 using namespace reco;
0005
0006 CompositeCandidate::CompositeCandidate(const Candidate& c, const std::string& name) : LeafCandidate(c), name_(name) {
0007 size_t n = c.numberOfDaughters();
0008 for (size_t i = 0; i != n; ++i) {
0009 addDaughter(*c.daughter(i));
0010 }
0011 }
0012
0013 CompositeCandidate::CompositeCandidate(const Candidate& c, const std::string& name, role_collection const& roles)
0014 : LeafCandidate(c), name_(name), roles_(roles) {
0015 size_t n = c.numberOfDaughters();
0016 size_t r = roles_.size();
0017 bool sameSize = (n == r);
0018 for (size_t i = 0; i != n; ++i) {
0019 if (sameSize && r > 0)
0020 addDaughter(*c.daughter(i), roles_[i]);
0021 else
0022 addDaughter(*c.daughter(i));
0023 }
0024 }
0025
0026 CompositeCandidate::~CompositeCandidate() {}
0027
0028 CompositeCandidate* CompositeCandidate::clone() const { return new CompositeCandidate(*this); }
0029
0030 const Candidate* CompositeCandidate::daughter(size_type i) const {
0031 return (i < numberOfDaughters()) ? &dau[i] : nullptr;
0032 }
0033
0034 Candidate* CompositeCandidate::daughter(size_type i) {
0035 Candidate* d = (i < numberOfDaughters()) ? &dau[i] : nullptr;
0036 return d;
0037 }
0038
0039 const Candidate* CompositeCandidate::mother(size_type i) const { return nullptr; }
0040
0041 size_t CompositeCandidate::numberOfDaughters() const { return dau.size(); }
0042
0043 size_t CompositeCandidate::numberOfMothers() const { return 0; }
0044
0045 bool CompositeCandidate::overlap(const Candidate& c2) const {
0046 throw cms::Exception("Error") << "can't check overlap internally for CompositeCanddate";
0047 }
0048
0049 void CompositeCandidate::applyRoles() {
0050 if (roles_.empty())
0051 return;
0052
0053
0054 int N1 = roles_.size();
0055 int N2 = numberOfDaughters();
0056 if (N1 != N2) {
0057 throw cms::Exception("InvalidReference")
0058 << "CompositeCandidate::applyRoles : Number of roles and daughters differ, this is an error.\n";
0059 }
0060
0061 for (int i = 0; i < N1; ++i) {
0062 std::string role = roles_[i];
0063 Candidate* c = CompositeCandidate::daughter(i);
0064
0065 CompositeCandidate* c1 = dynamic_cast<CompositeCandidate*>(c);
0066 if (c1 != nullptr) {
0067 c1->setName(role);
0068 }
0069 }
0070 }
0071
0072 Candidate* CompositeCandidate::daughter(const std::string& s) {
0073 int ret = -1;
0074 int i = 0, N = roles_.size();
0075 bool found = false;
0076 for (; i < N && !found; ++i) {
0077 if (s == roles_[i]) {
0078 found = true;
0079 ret = i;
0080 }
0081 }
0082
0083 if (ret < 0) {
0084 throw cms::Exception("InvalidReference") << "CompositeCandidate::daughter: Cannot find role " << s << "\n";
0085 }
0086
0087 return daughter(ret);
0088 }
0089
0090 const Candidate* CompositeCandidate::daughter(const std::string& s) const {
0091 int ret = -1;
0092 int i = 0, N = roles_.size();
0093 bool found = false;
0094 for (; i < N && !found; ++i) {
0095 if (s == roles_[i]) {
0096 found = true;
0097 ret = i;
0098 }
0099 }
0100
0101 if (ret < 0) {
0102 throw cms::Exception("InvalidReference") << "CompositeCandidate::daughter: Cannot find role " << s << "\n";
0103 }
0104
0105 return daughter(ret);
0106 }
0107
0108 void CompositeCandidate::addDaughter(const Candidate& cand, const std::string& s) {
0109 Candidate* c = cand.clone();
0110 if (!s.empty()) {
0111 role_collection::iterator begin = roles_.begin(), end = roles_.end();
0112 bool isFound = (find(begin, end, s) != end);
0113 if (isFound) {
0114 throw cms::Exception("InvalidReference") << "CompositeCandidate::addDaughter: Already have role with name \"" << s
0115 << "\", please clearDaughters, or use a new name\n";
0116 }
0117 roles_.push_back(s);
0118 CompositeCandidate* c1 = dynamic_cast<CompositeCandidate*>(&*c);
0119 if (c1 != nullptr) {
0120 c1->setName(s);
0121 }
0122 }
0123 dau.push_back(c);
0124 }
0125
0126 void CompositeCandidate::addDaughter(std::unique_ptr<Candidate> cand, const std::string& s) {
0127 if (!s.empty()) {
0128 role_collection::iterator begin = roles_.begin(), end = roles_.end();
0129 bool isFound = (find(begin, end, s) != end);
0130 if (isFound) {
0131 throw cms::Exception("InvalidReference") << "CompositeCandidate::addDaughter: Already have role with name \"" << s
0132 << "\", please clearDaughters, or use a new name\n";
0133 }
0134 roles_.push_back(s);
0135 CompositeCandidate* c1 = dynamic_cast<CompositeCandidate*>(&*cand);
0136 if (c1 != nullptr) {
0137 c1->setName(s);
0138 }
0139 }
0140 dau.push_back(std::move(cand));
0141 }