CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
NamedCandCombinerBase.cc
Go to the documentation of this file.
2 #include <utility>
3 using namespace std;
4 using namespace reco;
5 
7  : checkCharge_(false), checkOverlap_(true), dauCharge_(), overlap_(), name_(name) {}
8 
10  : checkCharge_(true), checkOverlap_(true), dauCharge_(2), overlap_(), name_(name) {
11  dauCharge_[0] = q1;
12  dauCharge_[1] = q2;
13 }
14 
16  : checkCharge_(true), checkOverlap_(true), dauCharge_(3), overlap_(), name_(name) {
17  dauCharge_[0] = q1;
18  dauCharge_[1] = q2;
19  dauCharge_[2] = q3;
20 }
21 
23  : checkCharge_(true), checkOverlap_(true), dauCharge_(4), overlap_(), name_(name) {
24  dauCharge_[0] = q1;
25  dauCharge_[1] = q2;
26  dauCharge_[2] = q3;
27  dauCharge_[3] = q4;
28 }
29 
31  bool checkCharge,
32  bool checkOverlap,
33  const vector<int>& dauCharge)
34  : checkCharge_(checkCharge), checkOverlap_(checkOverlap), dauCharge_(dauCharge), overlap_() {}
35 
37 
38 bool NamedCandCombinerBase::preselect(const Candidate& c1, const Candidate& c2) const {
39  if (checkCharge_) {
40  int dq1 = dauCharge_[0], dq2 = dauCharge_[1], q1 = c1.charge(), q2 = c2.charge();
41  bool matchCharge = (q1 == dq1 && q2 == dq2) || (q1 == -dq1 && q2 == -dq2);
42  if (!matchCharge)
43  return false;
44  }
45  if (checkOverlap_ && overlap_(c1, c2))
46  return false;
47  return selectPair(c1, c2);
48 }
49 
51  const CandidatePtr& c1,
52  const CandidatePtr& c2,
53  std::string n1,
54  std::string n2) const {
55  addDaughter(cmp, c1, n1);
56  addDaughter(cmp, c2, n2);
57  setup(cmp);
58 }
59 
60 unique_ptr<NamedCompositeCandidateCollection> NamedCandCombinerBase::combine(const vector<CandidatePtrVector>& src,
61  string_coll const& names) const {
62  size_t srcSize = src.size();
63  if (checkCharge_ && dauCharge_.size() != srcSize)
65  << "NamedCandCombiner: trying to combine " << srcSize << " collections"
66  << " but configured to check against " << dauCharge_.size() << " charges.";
67 
68  if (names.size() < 2)
70  << "NamedCandCombiner: need to add 2 names, but size is " << names.size();
71 
72  unique_ptr<NamedCompositeCandidateCollection> comps(new NamedCompositeCandidateCollection);
73  if (srcSize == 2) {
74  CandidatePtrVector src1 = src[0], src2 = src[1];
75  if (src1 == src2) {
76  const int n = src1.size();
77  for (int i1 = 0; i1 < n; ++i1) {
78  const Candidate& c1 = *(src1[i1]);
79  for (int i2 = i1 + 1; i2 < n; ++i2) {
80  const Candidate& c2 = *(src1[i2]);
81  if (preselect(c1, c2)) {
83  combine(c, src1[i1], src1[i2], names[0], names[1]);
84  if (select(c))
85  comps->push_back(c);
86  }
87  }
88  }
89  } else {
90  const int n1 = src1.size(), n2 = src2.size();
91  for (int i1 = 0; i1 < n1; ++i1) {
92  const Candidate& c1 = *(src1[i1]);
93  for (int i2 = 0; i2 < n2; ++i2) {
94  const Candidate& c2 = *(src2[i2]);
95  if (preselect(c1, c2)) {
97  combine(c, src1[i1], src2[i2], names[0], names[1]);
98  if (select(c))
99  comps->push_back(c);
100  }
101  }
102  }
103  }
104  } else {
106  ChargeStack qStack;
107  combine(0, stack, qStack, names, src.begin(), src.end(), comps);
108  }
109 
110  return comps;
111 }
112 
113 unique_ptr<NamedCompositeCandidateCollection> NamedCandCombinerBase::combine(const CandidatePtrVector& src,
114  string_coll const& names) const {
115  if (checkCharge_ && dauCharge_.size() != 2)
117  << "NamedCandCombiner: trying to combine 2 collections"
118  << " but configured to check against " << dauCharge_.size() << " charges.";
119 
120  if (names.size() < 2)
122  << "NamedCandCombiner: need to add 2 names, but size is " << names.size();
123 
124  unique_ptr<NamedCompositeCandidateCollection> comps(new NamedCompositeCandidateCollection);
125  const int n = src.size();
126  for (int i1 = 0; i1 < n; ++i1) {
127  const Candidate& c1 = *(src[i1]);
128  for (int i2 = i1 + 1; i2 < n; ++i2) {
129  const Candidate& c2 = *(src[i2]);
130  if (preselect(c1, c2)) {
132  combine(c, src[i1], src[i2], names[0], names[1]);
133  if (select(c))
134  comps->push_back(c);
135  }
136  }
137  }
138 
139  return comps;
140 }
141 
142 unique_ptr<NamedCompositeCandidateCollection> NamedCandCombinerBase::combine(const CandidatePtrVector& src1,
143  const CandidatePtrVector& src2,
144  string_coll const& names) const {
145  vector<CandidatePtrVector> src;
146  src.push_back(src1);
147  src.push_back(src2);
148  return combine(src, names);
149 }
150 
151 unique_ptr<NamedCompositeCandidateCollection> NamedCandCombinerBase::combine(const CandidatePtrVector& src1,
152  const CandidatePtrVector& src2,
153  const CandidatePtrVector& src3,
154  string_coll const& names) const {
155  vector<CandidatePtrVector> src;
156  src.push_back(src1);
157  src.push_back(src2);
158  src.push_back(src3);
159  return combine(src, names);
160 }
161 
162 unique_ptr<NamedCompositeCandidateCollection> NamedCandCombinerBase::combine(const CandidatePtrVector& src1,
163  const CandidatePtrVector& src2,
164  const CandidatePtrVector& src3,
165  const CandidatePtrVector& src4,
166  string_coll const& names) const {
167  vector<CandidatePtrVector> src;
168  src.push_back(src1);
169  src.push_back(src2);
170  src.push_back(src3);
171  src.push_back(src4);
172  return combine(src, names);
173 }
174 
175 void NamedCandCombinerBase::combine(size_t collectionIndex,
176  CandStack& stack,
177  ChargeStack& qStack,
178  string_coll const& names,
179  vector<CandidatePtrVector>::const_iterator collBegin,
180  vector<CandidatePtrVector>::const_iterator collEnd,
181  unique_ptr<NamedCompositeCandidateCollection>& comps) const {
182  if (collBegin == collEnd) {
183  static const int undetermined = 0, sameDecay = 1, conjDecay = -1, wrongDecay = 2;
184  int decayType = undetermined;
185  if (checkCharge_) {
186  assert(qStack.size() == stack.size());
187  for (size_t i = 0; i < qStack.size(); ++i) {
188  int q = qStack[i], dq = dauCharge_[i];
189  if (decayType == undetermined) {
190  if (q != 0 && dq != 0) {
191  if (q == dq)
192  decayType = sameDecay;
193  else if (q == -dq)
194  decayType = conjDecay;
195  else
196  decayType = wrongDecay;
197  }
198  } else if ((decayType == sameDecay && q != dq) || (decayType == conjDecay && q != -dq)) {
199  decayType = wrongDecay;
200  }
201  if (decayType == wrongDecay)
202  break;
203  }
204  }
205  if (decayType != wrongDecay) {
207  int ii = 0;
208  for (CandStack::const_iterator i = stack.begin(); i != stack.end(); ++i, ++ii) {
209  addDaughter(c, i->first.first, names[ii]);
210  }
211  setup(c);
212  if (select(c))
213  comps->push_back(c);
214  }
215  } else {
216  const CandidatePtrVector& src = *collBegin;
217  size_t candBegin = 0, candEnd = src.size();
218  for (CandStack::const_iterator i = stack.begin(); i != stack.end(); ++i)
219  if (src == *i->second)
220  candBegin = i->first.second + 1;
221  for (size_t candIndex = candBegin; candIndex != candEnd; ++candIndex) {
222  const CandidatePtr& candPtr(src[candIndex]);
223 
224  bool noOverlap = true;
225  const Candidate& cand = *candPtr;
226  for (CandStack::const_iterator i = stack.begin(); i != stack.end(); ++i)
227  if (checkOverlap_ && overlap_(cand, *(i->first.first))) {
228  noOverlap = false;
229  break;
230  }
231  if (noOverlap) {
232  stack.push_back(make_pair(make_pair(candPtr, candIndex), collBegin));
233  if (checkCharge_)
234  qStack.push_back(cand.charge());
235  combine(collectionIndex + 1, stack, qStack, names, collBegin + 1, collEnd, comps);
236  stack.pop_back();
237  qStack.pop_back();
238  }
239  }
240  }
241 }
const edm::EventSetup & c
NamedCandCombinerBase(std::string name)
default construct
size_type size() const
Size of the RefVector.
Definition: PtrVectorBase.h:75
virtual void setup(reco::NamedCompositeCandidate &) const =0
set kinematics to reconstructed composite
std::vector< NamedCompositeCandidate > NamedCompositeCandidateCollection
collection of Candidate objects
int ii
Definition: cuy.py:589
assert(be >=bs)
const std::string names[nVars_]
std::vector< int > ChargeStack
stack
Definition: svgfig.py:559
virtual int charge() const =0
electric charge
OverlapChecker overlap_
utility to check candidate daughters overlap
virtual void addDaughter(reco::NamedCompositeCandidate &cmp, const reco::CandidatePtr &c, std::string name) const =0
add candidate daughter
bool preselect(const reco::Candidate &, const reco::Candidate &) const
verify that the two candidate don&#39;t overlap and check charge
bool checkCharge_
flag to specify the checking of electric charge
bool checkOverlap_
flag to specify the checking of overlaps
std::vector< int > dauCharge_
electric charges of the daughters
std::vector< std::pair< std::pair< reco::CandidatePtr, size_t >, std::vector< reco::CandidatePtrVector >::const_iterator > > CandStack
temporary candidate stack
virtual bool select(const reco::Candidate &) const =0
select a candidate
std::unique_ptr< reco::NamedCompositeCandidateCollection > combine(const std::vector< reco::CandidatePtrVector > &, string_coll const &) const
return all selected candidate pairs
virtual bool selectPair(const reco::Candidate &c1, const reco::Candidate &c2) const =0
select a candidate pair
std::vector< std::string > string_coll
virtual ~NamedCandCombinerBase()
destructor