CMS 3D CMS Logo

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 }
9 
12  dauCharge_[0] = q1;
13  dauCharge_[1] = q2;
14 }
15 
18  dauCharge_[0] = q1;
19  dauCharge_[1] = q2;
20  dauCharge_[2] = q3;
21 }
22 
25  dauCharge_[0] = q1;
26  dauCharge_[1] = q2;
27  dauCharge_[2] = q3;
28  dauCharge_[3] = q4;
29 }
30 
32  checkCharge_(checkCharge), checkOverlap_(checkOverlap), dauCharge_(dauCharge), overlap_() {
33 }
34 
36 }
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) return false;
43  }
44  if (checkOverlap_ && overlap_(c1, c2)) return false;
45  return selectPair(c1, c2);
46 }
47 
49  std::string n1, std::string n2 ) const {
50  addDaughter(cmp, c1, n1);
51  addDaughter(cmp, c2, n2);
52  setup(cmp);
53 }
54 
55 auto_ptr<NamedCompositeCandidateCollection>
56 NamedCandCombinerBase::combine(const vector<CandidatePtrVector> & src,
57  string_coll const & names) const {
58  size_t srcSize = src.size();
59  if (checkCharge_ && dauCharge_.size() != srcSize)
61  << "NamedCandCombiner: trying to combine " << srcSize << " collections"
62  << " but configured to check against " << dauCharge_.size() << " charges.";
63 
64  if ( names.size() < 2 )
66  << "NamedCandCombiner: need to add 2 names, but size is " << names.size();
67 
68  auto_ptr<NamedCompositeCandidateCollection> comps(new NamedCompositeCandidateCollection);
69  if(srcSize == 2) {
70  CandidatePtrVector src1 = src[0], src2 = src[1];
71  if(src1 == src2) {
72  const int n = src1.size();
73  for(int i1 = 0; i1 < n; ++i1) {
74  const Candidate & c1 = *(src1[i1]);
75  for(int i2 = i1 + 1; i2 < n; ++i2) {
76  const Candidate & c2 = *(src1[i2]);
77  if (preselect(c1, c2)) {
79  combine(c, src1[i1], src1[i2], names[0], names[1]);
80  if(select(c))
81  comps->push_back(c);
82  }
83  }
84  }
85  } else {
86  const int n1 = src1.size(), n2 = src2.size();
87  for(int i1 = 0; i1 < n1; ++i1) {
88  const Candidate & c1 = *(src1[i1]);
89  for(int i2 = 0; i2 < n2; ++i2) {
90  const Candidate & c2 = *(src2[i2]);
91  if(preselect(c1, c2)) {
93  combine(c, src1[i1], src2[i2], names[0], names[1]);
94  if(select(c))
95  comps->push_back(c);
96  }
97  }
98  }
99  }
100  } else {
102  ChargeStack qStack;
103  combine(0, stack, qStack, names, src.begin(), src.end(), comps);
104  }
105 
106  return comps;
107 }
108 
109 auto_ptr<NamedCompositeCandidateCollection>
111  if(checkCharge_ && dauCharge_.size() != 2)
113  << "NamedCandCombiner: trying to combine 2 collections"
114  << " but configured to check against " << dauCharge_.size() << " charges.";
115 
116  if ( names.size() < 2 )
118  << "NamedCandCombiner: need to add 2 names, but size is " << names.size();
119 
120  auto_ptr<NamedCompositeCandidateCollection> comps(new NamedCompositeCandidateCollection);
121  const int n = src.size();
122  for(int i1 = 0; i1 < n; ++i1) {
123  const Candidate & c1 = *(src[i1]);
124  for(int i2 = i1 + 1; i2 < n; ++i2) {
125  const Candidate & c2 = *(src[i2]);
126  if(preselect(c1, c2)) {
128  combine(c, src[i1], src[i2], names[0], names[1]);
129  if(select(c))
130  comps->push_back(c);
131  }
132  }
133  }
134 
135  return comps;
136 }
137 
138 auto_ptr<NamedCompositeCandidateCollection>
140  vector<CandidatePtrVector> src;
141  src.push_back(src1);
142  src.push_back(src2);
143  return combine(src, names);
144 }
145 
146 auto_ptr<NamedCompositeCandidateCollection>
148  string_coll const & names) const {
149  vector<CandidatePtrVector> src;
150  src.push_back(src1);
151  src.push_back(src2);
152  src.push_back(src3);
153  return combine(src, names);
154 }
155 
156 auto_ptr<NamedCompositeCandidateCollection>
158  const CandidatePtrVector & src3, const CandidatePtrVector & src4,
159  string_coll const & names) const {
160  vector<CandidatePtrVector> src;
161  src.push_back(src1);
162  src.push_back(src2);
163  src.push_back(src3);
164  src.push_back(src4);
165  return combine(src, names);
166 }
167 
168 void NamedCandCombinerBase::combine(size_t collectionIndex, CandStack & stack, ChargeStack & qStack,
169  string_coll const & names,
170  vector<CandidatePtrVector>::const_iterator collBegin,
171  vector<CandidatePtrVector>::const_iterator collEnd,
172  auto_ptr<NamedCompositeCandidateCollection> & comps) const {
173  if(collBegin == collEnd) {
174  static const int undetermined = 0, sameDecay = 1, conjDecay = -1, wrongDecay = 2;
175  int decayType = undetermined;
176  if(checkCharge_) {
177  assert(qStack.size() == stack.size());
178  for(size_t i = 0; i < qStack.size(); ++i) {
179  int q = qStack[i], dq = dauCharge_[i];
180  if(decayType == undetermined) {
181  if(q != 0 && dq != 0) {
182  if(q == dq) decayType = sameDecay;
183  else if(q == -dq) decayType = conjDecay;
184  else decayType = wrongDecay;
185  }
186  } else if((decayType == sameDecay && q != dq) ||
187  (decayType == conjDecay && q != -dq)) {
188  decayType = wrongDecay;
189  }
190  if(decayType == wrongDecay) break;
191  }
192  }
193  if(decayType != wrongDecay) {
195  int ii = 0;
196  for(CandStack::const_iterator i = stack.begin(); i != stack.end(); ++i, ++ii) {
197  addDaughter(c, i->first.first, names[ii]);
198  }
199  setup(c);
200  if(select(c))
201  comps->push_back(c);
202  }
203  } else {
204  const CandidatePtrVector & src = * collBegin;
205  size_t candBegin = 0, candEnd = src.size();
206  for(CandStack::const_iterator i = stack.begin(); i != stack.end(); ++i)
207  if(src == * i->second)
208  candBegin = i->first.second + 1;
209  for(size_t candIndex = candBegin; candIndex != candEnd; ++ candIndex) {
210  const CandidatePtr & candPtr(src[candIndex]);
211 
212  bool noOverlap = true;
213  const Candidate & cand = *candPtr;
214  for(CandStack::const_iterator i = stack.begin(); i != stack.end(); ++i)
215  if(checkOverlap_ && overlap_(cand, *(i->first.first))) {
216  noOverlap = false;
217  break;
218  }
219  if(noOverlap) {
220  stack.push_back(make_pair(make_pair(candPtr, candIndex), collBegin));
221  if(checkCharge_) qStack.push_back(cand.charge());
222  combine(collectionIndex + 1, stack, qStack, names, collBegin + 1, collEnd, comps);
223  stack.pop_back();
224  qStack.pop_back();
225  }
226  }
227  }
228 }
NamedCandCombinerBase(std::string name)
default construct
size_type size() const
Size of the RefVector.
Definition: PtrVectorBase.h:74
std::vector< NamedCompositeCandidate > NamedCompositeCandidateCollection
collection of Candidate objects
const std::string names[nVars_]
double q2[4]
Definition: TauolaWrapper.h:88
virtual void setup(reco::NamedCompositeCandidate &) const =0
set kinematics to reconstructed composite
std::vector< int > ChargeStack
virtual bool select(const reco::Candidate &) const =0
select a candidate
std::vector< std::pair< std::pair< reco::CandidatePtr, size_t >, std::vector< reco::CandidatePtrVector >::const_iterator > > CandStack
temporary candidate stack
stack
Definition: svgfig.py:558
OverlapChecker overlap_
utility to check candidate daughters overlap
std::auto_ptr< reco::NamedCompositeCandidateCollection > combine(const std::vector< reco::CandidatePtrVector > &, string_coll const &) const
return all selected candidate pairs
ii
Definition: cuy.py:589
virtual bool selectPair(const reco::Candidate &c1, const reco::Candidate &c2) const =0
select a candidate pair
double q1[4]
Definition: TauolaWrapper.h:87
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
def checkOverlap(process)
virtual int charge() const =0
electric charge
fixed size matrix
std::vector< std::string > string_coll
virtual void addDaughter(reco::NamedCompositeCandidate &cmp, const reco::CandidatePtr &c, std::string name) const =0
add candidate daughter
virtual ~NamedCandCombinerBase()
destructor