CMS 3D CMS Logo

CandMatcher.h
Go to the documentation of this file.
1 #ifndef CandUtils_CandMatcher_h
2 #define CandUtils_CandMatcher_h
3 /* class CandMatcher
4  *
5  * \author Luca Lista, INFN
6  *
7  */
8 #include <set>
14 
15 template<typename C1, typename C2 = C1>
17 public:
25  typedef std::vector<const map_type *> map_vector;
31  explicit CandMatcherBase( const map_vector & maps );
33  explicit CandMatcherBase( const map_type & map );
35  virtual ~CandMatcherBase();
37  ref_type operator()( const reco::Candidate & ) const;
38 
39 protected:
41  virtual std::vector<const reco::Candidate *> getDaughters( const reco::Candidate * ) const = 0;
43  virtual bool compositePreselect( const reco::Candidate & c, const reco::Candidate & m ) const = 0;
45  void initMaps();
46 
47 protected:
48  const std::vector<const map_type *> & maps() const { return maps_; }
49 private:
51  std::vector<const map_type *> maps_;
53  refProd_type matched_;
55  typedef std::map<const reco::Candidate *, reference_type> CandRefMap;
57  typedef std::map<const reco::Candidate *, ref_type> MatchedRefMap;
59  CandRefMap candRefs_;
61  MatchedRefMap matchedRefs_;
63  std::vector<std::set<size_t> > matchedMothers_;
65  void init();
66 };
67 
68 template<typename C1, typename C2 = C1>
69 class CandMatcher : public CandMatcherBase<C1, C2> {
70 public:
72  explicit CandMatcher( const typename CandMatcherBase<C1, C2>::map_vector & maps );
74  explicit CandMatcher( const typename CandMatcherBase<C1, C2>::map_type & map );
76  virtual ~CandMatcher();
77 
78 protected:
80  virtual std::vector<const reco::Candidate *> getDaughters( const reco::Candidate * ) const;
82  virtual bool compositePreselect( const reco::Candidate & c, const reco::Candidate & m ) const;
83 };
84 
85 #include <algorithm>
86 #include <iterator>
87 
88 template<typename C1, typename C2>
90  matched_ = maps_.front()->refProd().val;
91  for( typename map_vector::const_iterator m = maps_.begin() + 1;
92  m != maps_.end(); ++ m ) {
93  if( (*m)->refProd().val != matched_ )
95  << "Multiple match maps specified matching different MC truth collections.\n"
96  << "Please, specify maps all matching to the same MC truth collection.\n"
97  << "In most of the cases you may want to match to genParticleCandidate.";
98  }
99 }
100 
101 template<typename C1, typename C2>
103  maps_( maps ) {
104  init();
105 }
106 
107 template<typename C1, typename C2>
109  maps_( 1, & map ) {
110  init();
111 }
112 
113 template<typename C1, typename C2>
115  using namespace reco;
116  using namespace std;
117  for( typename map_vector::const_iterator m = maps_.begin();
118  m != maps_.end(); ++ m ) {
119  typename CandMatcherBase<C1, C2>::map_type::ref_type::key_type cands = (*m)->refProd().key;
120  for( size_t i = 0; i < cands->size(); ++ i ) {
121  candRefs_[ & (*cands)[ i ] ] = reference_type( cands, i );
122  }
123  const C2 & matched = * matched_;
124  size_t matchedSize = matched.size();
125  for( size_t i = 0; i < matchedSize; ++ i )
126  matchedRefs_[ & matched[ i ] ] = ref_type( matched_, i );
127  matchedMothers_.resize( matchedSize );
128  for( size_t i = 0; i < matchedSize; ++ i ) {
129  const Candidate & c = matched[ i ];
130  for( Candidate::const_iterator d = c.begin(); d != c.end(); ++ d ) {
131  vector<const Candidate *> daus = getDaughters( & * d );
132  for( size_t j = 0; j < daus.size(); ++ j ) {
133  const Candidate * daughter = daus[ j ];
134  typename MatchedRefMap::const_iterator f = matchedRefs_.find( daughter );
135  if ( f == matchedRefs_.end() ) continue;
136  size_t k = f->second.key();
137  assert( k < matchedMothers_.size() );
138  matchedMothers_[ k ].insert( i );
139  }
140  }
141  }
142  }
143 }
144 
145 template<typename C1, typename C2>
147 }
148 
149 template<typename C1, typename C2>
151  using namespace reco;
152  using namespace std;
153  if ( c.hasMasterClone() )
154  return (*this)( * c.masterClone() );
155  unsigned int nDau = c.numberOfDaughters();
156  const C2 & matched = * matched_;
157  if ( nDau > 0 ) {
158  // check for composite candidate c
159  // navigate to daughters and find parent matches
160  set<size_t> momsIntersection, momDaughters, tmp;
161  for( Candidate::const_iterator d = c.begin(); d != c.end(); ++ d ) {
162  // check here generically if status == 3, then descend down to one more level
163  ref_type m = (*this)( * d );
164  // if a daughter does not match, return a null ref.
165  if ( m.isNull() ) return ref_type();
166  // get matched mother indices (fetched previously)
167  const set<size_t> & allMomDaughters = matchedMothers_[ m.key() ];
168  momDaughters.clear();
169  for( set<size_t>::const_iterator k = allMomDaughters.begin();
170  k != allMomDaughters.end(); ++ k ) {
171  size_t m = * k;
172  if( compositePreselect( c, matched[ m ] ) )
173  momDaughters.insert( m );
174  }
175  // if no mother was found return null reference
176  if ( momDaughters.size() == 0 ) return ref_type();
177  // the first time, momsIntersection is set to momDaughters
178  if ( momsIntersection.size() == 0 ) momsIntersection = momDaughters;
179  else {
180  tmp.clear();
181  set_intersection( momsIntersection.begin(), momsIntersection.end(),
182  momDaughters.begin(), momDaughters.end(),
183  inserter( tmp, tmp.begin() ) );
184  swap( momsIntersection, tmp );
185  }
186  if ( momsIntersection.size() == 0 ) return ref_type();
187  }
188  // if multiple mothers are found, return a null reference
189  if ( momsIntersection.size() > 1 ) return ref_type();
190  // return a reference to the unique mother
191  return ref_type( matched_, * momsIntersection.begin() );
192  } else {
193  // check for non-composite (leaf) candidate
194  // if one of the maps contains the candidate c
195  for( typename std::vector<const map_type *>::const_iterator m = maps_.begin();
196  m != maps_.end(); ++ m ) {
197  typename CandRefMap::const_iterator f = candRefs_.find( & c );
198  if ( f != candRefs_.end() ) {
199  reference_type ref = f->second;
200  typename map_type::const_iterator f = (*m)->find( ref );
201  if ( f != (*m)->end() ) {
202  return f->val;
203  }
204  }
205  }
206  return ref_type();
207  }
208 }
209 
210 template<typename C1, typename C2>
212  CandMatcherBase<C1, C2>( maps ) {
214 }
215 
216 template<typename C1, typename C2>
218  CandMatcherBase<C1, C2>( map ) {
220 }
221 
222 template<typename C1, typename C2>
224 }
225 
226 template<typename C1, typename C2>
227 std::vector<const reco::Candidate *> CandMatcher<C1, C2>::getDaughters( const reco::Candidate * c ) const {
228  std::vector<const reco::Candidate *> v;
229  v.push_back( c );
230  return v;
231 }
232 
233 template<typename C1, typename C2>
235  // By default, check that the number of daughters is identical
236  return( c.numberOfDaughters() == m.numberOfDaughters() );
237 }
238 
239 #endif
void init()
init at constructor
Definition: CandMatcher.h:89
std::vector< const map_type * > map_vector
map vector
Definition: CandMatcher.h:25
std::map< const reco::Candidate *, reference_type > CandRefMap
pointer map type
Definition: CandMatcher.h:55
virtual bool compositePreselect(const reco::Candidate &c, const reco::Candidate &m) const =0
composite candidate preselection
virtual bool compositePreselect(const reco::Candidate &c, const reco::Candidate &m) const
composite candidate preselection
Definition: CandMatcher.h:234
reference_type::value_type value_type
concrete candidate reference type
Definition: CandMatcher.h:29
reco::helper::CandRefTrait< C2 >::refProd_type refProd_type
refProd type
Definition: CandMatcher.h:23
virtual std::vector< const reco::Candidate * > getDaughters(const reco::Candidate *) const =0
get ultimate daughter (can skip status = 3 in MC)
CandRefMap candRefs_
pointer map of candidates (e.g.: reco)
Definition: CandMatcher.h:59
virtual ~CandMatcherBase()
destructor
Definition: CandMatcher.h:146
Tag::key_type key_type
insert key type
key_type key() const
Accessor for product key.
Definition: Ref.h:263
refProd_type matched_
reference to matched collectino
Definition: CandMatcher.h:53
std::map< const reco::Candidate *, ref_type > MatchedRefMap
pointer map type
Definition: CandMatcher.h:57
void swap(Association< C > &lhs, Association< C > &rhs)
Definition: Association.h:116
const std::vector< const map_type * > & maps() const
Definition: CandMatcher.h:48
reco::helper::CandRefTrait< C2 >::ref_type ref_type
ref type
Definition: CandMatcher.h:21
reco::helper::CandMapTrait< C1, C2 >::type map_type
map type
Definition: CandMatcher.h:19
MatchedRefMap matchedRefs_
pointer map of matched candidates (e.g.: MC truth)
Definition: CandMatcher.h:61
const_iterator end() const
last daughter const_iterator
Definition: Candidate.h:146
CandMatcher(const typename CandMatcherBase< C1, C2 >::map_vector &maps)
constructor
Definition: CandMatcher.h:211
CandMatcherBase(const map_vector &maps)
constructor
double f[11][100]
virtual ~CandMatcher()
destructor
Definition: CandMatcher.h:223
bool isNull() const
Checks for null.
Definition: Ref.h:248
ref_type operator()(const reco::Candidate &) const
get match from transient reference
Definition: CandMatcher.h:150
int k[5][pyjets_maxn]
void initMaps()
init maps
Definition: CandMatcher.h:114
virtual const CandidateBaseRef & masterClone() const =0
std::vector< std::set< size_t > > matchedMothers_
mother + n.daughters indices from matched
Definition: CandMatcher.h:63
virtual std::vector< const reco::Candidate * > getDaughters(const reco::Candidate *) const
get ultimate daughter (get all in the general case)
Definition: CandMatcher.h:227
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
std::vector< const map_type * > maps_
pointers to stored maps
Definition: CandMatcher.h:51
fixed size matrix
const_iterator begin() const
first daughter const_iterator
Definition: Candidate.h:144
virtual size_type numberOfDaughters() const =0
number of daughters
map_type::key_type reference_type
concrete candidate reference type
Definition: CandMatcher.h:27
virtual bool hasMasterClone() const =0