Go to the documentation of this file.00001
00002 #include "DataFormats/Candidate/interface/CompositeCandidate.h"
00003 #include "FWCore/Utilities/interface/Exception.h"
00004
00005 using namespace reco;
00006
00007 CompositeCandidate::CompositeCandidate(const Candidate & c,
00008 const std::string& name) :
00009 LeafCandidate(c), name_(name) {
00010 size_t n = c.numberOfDaughters();
00011 for(size_t i = 0; i != n; ++i) {
00012 addDaughter(*c.daughter(i));
00013 }
00014 }
00015
00016 CompositeCandidate::CompositeCandidate(const Candidate & c,
00017 const std::string& name,
00018 role_collection const & roles) :
00019 LeafCandidate(c), name_(name), roles_(roles) {
00020 size_t n = c.numberOfDaughters();
00021 size_t r = roles_.size();
00022 bool sameSize = (n == r);
00023 for(size_t i = 0; i != n; ++i) {
00024 if (sameSize && r > 0)
00025 addDaughter(*c.daughter(i), roles_[i]);
00026 else
00027 addDaughter(*c.daughter(i));
00028 }
00029 }
00030
00031 CompositeCandidate::~CompositeCandidate() { }
00032
00033 CompositeCandidate * CompositeCandidate::clone() const { return new CompositeCandidate(* this); }
00034
00035 Candidate::const_iterator CompositeCandidate::begin() const { return const_iterator(new const_iterator_imp_specific(dau.begin())); }
00036
00037 Candidate::const_iterator CompositeCandidate::end() const { return const_iterator(new const_iterator_imp_specific(dau.end())); }
00038
00039 Candidate::iterator CompositeCandidate::begin() { return iterator(new iterator_imp_specific(dau.begin())); }
00040
00041 Candidate::iterator CompositeCandidate::end() { return iterator(new iterator_imp_specific(dau.end())); }
00042
00043 const Candidate * CompositeCandidate::daughter(size_type i) const {
00044 return (i < numberOfDaughters()) ? & dau[ i ] : 0;
00045 }
00046
00047 Candidate * CompositeCandidate::daughter(size_type i) {
00048 Candidate * d = (i < numberOfDaughters()) ? & dau[ i ] : 0;
00049 return d;
00050 }
00051
00052 const Candidate * CompositeCandidate::mother(size_type i) const {
00053 return 0;
00054 }
00055
00056 size_t CompositeCandidate::numberOfDaughters() const { return dau.size(); }
00057
00058 size_t CompositeCandidate::numberOfMothers() const { return 0; }
00059
00060 bool CompositeCandidate::overlap(const Candidate & c2) const {
00061 throw cms::Exception("Error") << "can't check overlap internally for CompositeCanddate";
00062 }
00063
00064 void CompositeCandidate::applyRoles() {
00065
00066 if (roles_.size() == 0)
00067 return;
00068
00069
00070 int N1 = roles_.size();
00071 int N2 = numberOfDaughters();
00072 if (N1 != N2) {
00073 throw cms::Exception("InvalidReference")
00074 << "CompositeCandidate::applyRoles : Number of roles and daughters differ, this is an error.\n";
00075 }
00076
00077 for (int i = 0 ; i < N1; ++i) {
00078 std::string role = roles_[i];
00079 Candidate * c = CompositeCandidate::daughter(i);
00080
00081 CompositeCandidate * c1 = dynamic_cast<CompositeCandidate *>(c);
00082 if (c1 != 0) {
00083 c1->setName(role);
00084 }
00085 }
00086 }
00087
00088 Candidate * CompositeCandidate::daughter(const std::string& s) {
00089 int ret = -1;
00090 int i = 0, N = roles_.size();
00091 bool found = false;
00092 for (; i < N && !found; ++i) {
00093 if (s == roles_[i]) {
00094 found = true;
00095 ret = i;
00096 }
00097 }
00098
00099 if (ret < 0) {
00100 throw cms::Exception("InvalidReference")
00101 << "CompositeCandidate::daughter: Cannot find role " << s << "\n";
00102 }
00103
00104 return daughter(ret);
00105 }
00106
00107 const Candidate * CompositeCandidate::daughter(const std::string& s) const {
00108 int ret = -1;
00109 int i = 0, N = roles_.size();
00110 bool found = false;
00111 for (; i < N && !found; ++i) {
00112 if (s == roles_[i]) {
00113 found = true;
00114 ret = i;
00115 }
00116 }
00117
00118 if (ret < 0) {
00119 throw cms::Exception("InvalidReference")
00120 << "CompositeCandidate::daughter: Cannot find role " << s << "\n";
00121 }
00122
00123 return daughter(ret);
00124 }
00125
00126 void CompositeCandidate::addDaughter(const Candidate & cand, const std::string& s) {
00127 Candidate * c = cand.clone();
00128 if (s != "") {
00129 role_collection::iterator begin = roles_.begin(), end = roles_.end();
00130 bool isFound = (find(begin, end, s) != end);
00131 if (isFound) {
00132 throw cms::Exception("InvalidReference")
00133 << "CompositeCandidate::addDaughter: Already have role with name \"" << s
00134 << "\", please clearDaughters, or use a new name\n";
00135 }
00136 roles_.push_back(s);
00137 CompositeCandidate * c1 = dynamic_cast<CompositeCandidate*>(&*c);
00138 if (c1 != 0) {
00139 c1->setName(s);
00140 }
00141 }
00142 dau.push_back(c);
00143 }
00144
00145 void CompositeCandidate::addDaughter(std::auto_ptr<Candidate> cand, const std::string& s) {
00146 if (s != "") {
00147 role_collection::iterator begin = roles_.begin(), end = roles_.end();
00148 bool isFound = (find(begin, end, s) != end);
00149 if (isFound) {
00150 throw cms::Exception("InvalidReference")
00151 << "CompositeCandidate::addDaughter: Already have role with name \"" << s
00152 << "\", please clearDaughters, or use a new name\n";
00153 }
00154 roles_.push_back(s);
00155 CompositeCandidate * c1 = dynamic_cast<CompositeCandidate*>(&*cand);
00156 if (c1 != 0) {
00157 c1->setName(s);
00158 }
00159 }
00160 dau.push_back(cand);
00161 }
00162
00163