CMS 3D CMS Logo

CompositeCandidate.cc
Go to the documentation of this file.
3 
4 using namespace reco;
5 
7  const std::string& name) :
8  LeafCandidate(c), name_(name) {
9  size_t n = c.numberOfDaughters();
10  for(size_t i = 0; i != n; ++i) {
11  addDaughter(*c.daughter(i));
12  }
13 }
14 
16  const std::string& name,
17  role_collection const & roles) :
18  LeafCandidate(c), name_(name), roles_(roles) {
19  size_t n = c.numberOfDaughters();
20  size_t r = roles_.size();
21  bool sameSize = (n == r);
22  for(size_t i = 0; i != n; ++i) {
23  if (sameSize && r > 0)
24  addDaughter(*c.daughter(i), roles_[i]);
25  else
26  addDaughter(*c.daughter(i));
27  }
28 }
29 
31 
33 
35  return (i < numberOfDaughters()) ? & dau[ i ] : nullptr; // i >= 0, since i is unsigned
36 }
37 
39  Candidate * d = (i < numberOfDaughters()) ? & dau[ i ] : nullptr; // i >= 0, since i is unsigned
40  return d;
41 }
42 
44  return nullptr;
45 }
46 
47 size_t CompositeCandidate::numberOfDaughters() const { return dau.size(); }
48 
49 size_t CompositeCandidate::numberOfMothers() const { return 0; }
50 
51 bool CompositeCandidate::overlap(const Candidate & c2) const {
52  throw cms::Exception("Error") << "can't check overlap internally for CompositeCanddate";
53 }
54 
56 
57  if (roles_.empty())
58  return;
59 
60  // Check if there are the same number of daughters and roles
61  int N1 = roles_.size();
62  int N2 = numberOfDaughters();
63  if (N1 != N2) {
64  throw cms::Exception("InvalidReference")
65  << "CompositeCandidate::applyRoles : Number of roles and daughters differ, this is an error.\n";
66  }
67  // Set up the daughter roles
68  for (int i = 0 ; i < N1; ++i) {
69  std::string role = roles_[i];
71 
72  CompositeCandidate * c1 = dynamic_cast<CompositeCandidate *>(c);
73  if (c1 != nullptr) {
74  c1->setName(role);
75  }
76  }
77 }
78 
80  int ret = -1;
81  int i = 0, N = roles_.size();
82  bool found = false;
83  for (; i < N && !found; ++i) {
84  if (s == roles_[i]) {
85  found = true;
86  ret = i;
87  }
88  }
89 
90  if (ret < 0) {
91  throw cms::Exception("InvalidReference")
92  << "CompositeCandidate::daughter: Cannot find role " << s << "\n";
93  }
94 
95  return daughter(ret);
96 }
97 
99  int ret = -1;
100  int i = 0, N = roles_.size();
101  bool found = false;
102  for (; i < N && !found; ++i) {
103  if (s == roles_[i]) {
104  found = true;
105  ret = i;
106  }
107  }
108 
109  if (ret < 0) {
110  throw cms::Exception("InvalidReference")
111  << "CompositeCandidate::daughter: Cannot find role " << s << "\n";
112  }
113 
114  return daughter(ret);
115 }
116 
118  Candidate * c = cand.clone();
119  if (s != "") {
120  role_collection::iterator begin = roles_.begin(), end = roles_.end();
121  bool isFound = (find(begin, end, s) != end);
122  if (isFound) {
123  throw cms::Exception("InvalidReference")
124  << "CompositeCandidate::addDaughter: Already have role with name \"" << s
125  << "\", please clearDaughters, or use a new name\n";
126  }
127  roles_.push_back(s);
128  CompositeCandidate * c1 = dynamic_cast<CompositeCandidate*>(&*c);
129  if (c1 != nullptr) {
130  c1->setName(s);
131  }
132  }
133  dau.push_back(c);
134 }
135 
136 void CompositeCandidate::addDaughter(std::unique_ptr<Candidate> cand, const std::string& s) {
137  if (s != "") {
138  role_collection::iterator begin = roles_.begin(), end = roles_.end();
139  bool isFound = (find(begin, end, s) != end);
140  if (isFound) {
141  throw cms::Exception("InvalidReference")
142  << "CompositeCandidate::addDaughter: Already have role with name \"" << s
143  << "\", please clearDaughters, or use a new name\n";
144  }
145  roles_.push_back(s);
146  CompositeCandidate * c1 = dynamic_cast<CompositeCandidate*>(&*cand);
147  if (c1 != nullptr) {
148  c1->setName(s);
149  }
150  }
151  dau.push_back(std::move(cand));
152 }
153 
154 
~CompositeCandidate() override
destructor
std::string name_
candidate name
size_t size_type
Definition: Candidate.h:30
role_collection const & roles() const
get the roles
CompositeCandidate * clone() const override
returns a clone of the candidate
role_collection roles_
candidate roles
virtual const Candidate * daughter(size_type i) const =0
return daughter at a given position, i = 0, ... numberOfDaughters() - 1 (read only mode) ...
size_type size() const
Definition: OwnVector.h:264
const Candidate * mother(size_type i=0) const override
return pointer to mother
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:20
void push_back(D *&d)
Definition: OwnVector.h:290
virtual Candidate * clone() const =0
returns a clone of the Candidate object
const_iterator end() const
last daughter const_iterator
Definition: Candidate.h:146
CompositeCandidate(std::string name="")
default constructor
const Candidate * daughter(size_type) const override
return daughter at a given position, i = 0, ... numberOfDaughters() - 1 (read only mode) ...
void addDaughter(const Candidate &, const std::string &s="")
add a clone of the passed candidate as daughter
void setName(std::string name)
set the name of the candidate
#define N
Definition: blowfish.cc:9
fixed size matrix
const_iterator begin() const
first daughter const_iterator
Definition: Candidate.h:144
daughters dau
collection of daughters
size_type numberOfDaughters() const override
number of daughters
std::string name() const
get the name of the candidate
bool overlap(const Candidate &) const override
check overlap with another daughter
virtual size_type numberOfDaughters() const =0
number of daughters
std::vector< std::string > role_collection
def move(src, dest)
Definition: eostools.py:510
size_type numberOfMothers() const override
number of mothers (zero or one in most of but not all the cases)