Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:03:49

0001 #include "DataFormats/Candidate/interface/NamedCompositeCandidate.h"
0002 #include "FWCore/Utilities/interface/Exception.h"
0003 
0004 #include <iostream>
0005 
0006 using namespace reco;
0007 
0008 NamedCompositeCandidate::NamedCompositeCandidate(std::string name,
0009                                                  const NamedCompositeCandidate::role_collection& roles,
0010                                                  const Candidate& c)
0011     : CompositeCandidate(c), name_(name), roles_(roles) {
0012   // Check if there are the same number of daughters and roles

0013   int N1 = roles_.size();
0014   int N2 = numberOfDaughters();
0015 
0016   if (N1 != N2) {
0017     throw cms::Exception("InvalidReference")
0018         << "NamedCompositeCandidate constructor: Number of roles and daughters differ, this is an error. Name = "
0019         << name << "\n";
0020   }
0021 }
0022 
0023 NamedCompositeCandidate::~NamedCompositeCandidate() {
0024   clearDaughters();
0025   clearRoles();
0026 }
0027 
0028 NamedCompositeCandidate* NamedCompositeCandidate::clone() const { return new NamedCompositeCandidate(*this); }
0029 
0030 void NamedCompositeCandidate::applyRoles() {
0031   // Check if there are the same number of daughters and roles

0032   int N1 = roles_.size();
0033   int N2 = numberOfDaughters();
0034   if (N1 != N2) {
0035     throw cms::Exception("InvalidReference")
0036         << "NamedCompositeCandidate::applyRoles : Number of roles and daughters differ, this is an error.\n";
0037   }
0038   // Set up the daughter roles

0039   for (int i = 0; i < N1; ++i) {
0040     std::string role = roles_[i];
0041     Candidate* c = CompositeCandidate::daughter(i);
0042 
0043     NamedCompositeCandidate* c1 = dynamic_cast<NamedCompositeCandidate*>(c);
0044     if (c1 != nullptr) {
0045       c1->setName(role);
0046     }
0047   }
0048 }
0049 
0050 Candidate* NamedCompositeCandidate::daughter(const std::string& s) {
0051   int ret = -1;
0052   int i = 0, N = roles_.size();
0053   bool found = false;
0054   for (; i < N && !found; ++i) {
0055     if (s == roles_[i]) {
0056       found = true;
0057       ret = i;
0058     }
0059   }
0060 
0061   if (ret < 0) {
0062     throw cms::Exception("InvalidReference") << "NamedCompositeCandidate::daughter: Cannot find role " << s << "\n";
0063   }
0064 
0065   return daughter(ret);
0066 }
0067 
0068 const Candidate* NamedCompositeCandidate::daughter(const std::string& s) const {
0069   int ret = -1;
0070   int i = 0, N = roles_.size();
0071   bool found = false;
0072   for (; i < N && !found; ++i) {
0073     if (s == roles_[i]) {
0074       found = true;
0075       ret = i;
0076     }
0077   }
0078 
0079   if (ret < 0) {
0080     throw cms::Exception("InvalidReference") << "NamedCompositeCandidate::daughter: Cannot find role " << s << "\n";
0081   }
0082 
0083   return daughter(ret);
0084 }
0085 
0086 void NamedCompositeCandidate::addDaughter(const Candidate& cand, const std::string& s) {
0087   role_collection::iterator begin = roles_.begin(), end = roles_.end();
0088   bool isFound = (find(begin, end, s) != end);
0089   if (isFound) {
0090     throw cms::Exception("InvalidReference") << "NamedCompositeCandidate::addDaughter: Already have role with name "
0091                                              << s << ", please clearDaughters, or use a new name\n";
0092   }
0093 
0094   roles_.push_back(s);
0095   std::unique_ptr<Candidate> c(cand.clone());
0096   NamedCompositeCandidate* c1 = dynamic_cast<NamedCompositeCandidate*>(&*c);
0097   if (c1 != nullptr) {
0098     c1->setName(s);
0099   }
0100   CompositeCandidate::addDaughter(std::move(c));
0101 }
0102 
0103 void NamedCompositeCandidate::addDaughter(std::unique_ptr<Candidate> cand, const std::string& s) {
0104   role_collection::iterator begin = roles_.begin(), end = roles_.end();
0105   bool isFound = (find(begin, end, s) != end);
0106   if (isFound) {
0107     throw cms::Exception("InvalidReference") << "NamedCompositeCandidate::addDaughter: Already have role with name "
0108                                              << s << ", please clearDaughters, or use a new name\n";
0109   }
0110 
0111   roles_.push_back(s);
0112   NamedCompositeCandidate* c1 = dynamic_cast<NamedCompositeCandidate*>(&*cand);
0113   if (c1 != nullptr) {
0114     c1->setName(s);
0115   }
0116   CompositeCandidate::addDaughter(std::move(cand));
0117 }