CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 <algorithm>
9 #include <set>
15 
16 template<typename C1, typename C2 = C1>
17 class CandMatcherBase {
18 public:
26  typedef std::vector<const map_type *> map_vector;
32  explicit CandMatcherBase( const map_vector & maps );
34  explicit CandMatcherBase( const map_type & map );
36  virtual ~CandMatcherBase();
38  ref_type operator()( const reco::Candidate & ) const;
39 
40 protected:
42  virtual std::vector<const reco::Candidate *> getDaughters( const reco::Candidate * ) const = 0;
44  virtual bool compositePreselect( const reco::Candidate & c, const reco::Candidate & m ) const = 0;
46  void initMaps();
47 
48 protected:
49  const std::vector<const map_type *> & maps() const { return maps_; }
50 private:
52  std::vector<const map_type *> maps_;
56  typedef std::map<const reco::Candidate *, reference_type> CandRefMap;
58  typedef std::map<const reco::Candidate *, ref_type> MatchedRefMap;
64  std::vector<std::set<size_t> > matchedMothers_;
66  void init();
67 };
68 
69 template<typename C1, typename C2 = C1>
70 class CandMatcher : public CandMatcherBase<C1, C2> {
71 public:
73  explicit CandMatcher( const typename CandMatcherBase<C1, C2>::map_vector & maps );
75  explicit CandMatcher( const typename CandMatcherBase<C1, C2>::map_type & map );
77  virtual ~CandMatcher();
78 
79 protected:
81  virtual std::vector<const reco::Candidate *> getDaughters( const reco::Candidate * ) const;
83  virtual bool compositePreselect( const reco::Candidate & c, const reco::Candidate & m ) const;
84 };
85 
86 #include <algorithm>
87 #include <iterator>
88 
89 template<typename C1, typename C2>
91  matched_ = maps_.front()->refProd().val;
92  for( typename map_vector::const_iterator m = maps_.begin() + 1;
93  m != maps_.end(); ++ m ) {
94  if( (*m)->refProd().val != matched_ )
96  << "Multiple match maps specified matching different MC truth collections.\n"
97  << "Please, specify maps all matching to the same MC truth collection.\n"
98  << "In most of the cases you may want to match to genParticleCandidate.";
99  }
100 }
101 
102 template<typename C1, typename C2>
104  maps_( maps ) {
105  init();
106 }
107 
108 template<typename C1, typename C2>
110  maps_( 1, & map ) {
111  init();
112 }
113 
114 template<typename C1, typename C2>
116  for( typename map_vector::const_iterator m = maps_.begin();
117  m != maps_.end(); ++ m ) {
118  typename CandMatcherBase<C1, C2>::map_type::ref_type::key_type cands = (*m)->refProd().key;
119  for( size_t i = 0; i < cands->size(); ++ i ) {
120  candRefs_[ & (*cands)[ i ] ] = reference_type( cands, i );
121  }
122  const C2 & matched = * matched_;
123  size_t matchedSize = matched.size();
124  for( size_t i = 0; i < matchedSize; ++ i )
125  matchedRefs_[ & matched[ i ] ] = ref_type( matched_, i );
126  matchedMothers_.resize( matchedSize );
127  for( size_t i = 0; i < matchedSize; ++ i ) {
128  const Candidate & c = matched[ i ];
129  for( Candidate::const_iterator d = c.begin(); d != c.end(); ++ d ) {
130  std;:vector<const Candidate *> daus = getDaughters( & * d );
131  for( size_t j = 0; j < daus.size(); ++ j ) {
132  const Candidate * daughter = daus[ j ];
133  typename MatchedRefMap::const_iterator f = matchedRefs_.find( daughter );
134  if ( f == matchedRefs_.end() ) continue;
135  size_t k = f->second.key();
136  assert( k < matchedMothers_.size() );
137  matchedMothers_[ k ].insert( i );
138  }
139  }
140  }
141  }
142 }
143 
144 template<typename C1, typename C2>
146 }
147 
148 template<typename C1, typename C2>
150  if ( c.hasMasterClone() )
151  return (*this)( * c.masterClone() );
152  unsigned int nDau = c.numberOfDaughters();
153  const C2 & matched = * matched_;
154  if ( nDau > 0 ) {
155  // check for composite candidate c
156  // navigate to daughters and find parent matches
157  std::set<size_t> momsIntersection, momDaughters, tmp;
158  for( Candidate::const_iterator d = c.begin(); d != c.end(); ++ d ) {
159  // check here generically if status == 3, then descend down to one more level
160  ref_type m = (*this)( * d );
161  // if a daughter does not match, return a null ref.
162  if ( m.isNull() ) return ref_type();
163  // get matched mother indices (fetched previously)
164  const set<size_t> & allMomDaughters = matchedMothers_[ m.key() ];
165  momDaughters.clear();
166  for( std::set<size_t>::const_iterator k = allMomDaughters.begin();
167  k != allMomDaughters.end(); ++ k ) {
168  size_t m = * k;
169  if( compositePreselect( c, matched[ m ] ) )
170  momDaughters.insert( m );
171  }
172  // if no mother was found return null reference
173  if ( momDaughters.size() == 0 ) return ref_type();
174  // the first time, momsIntersection is set to momDaughters
175  if ( momsIntersection.size() == 0 ) momsIntersection = momDaughters;
176  else {
177  tmp.clear();
178  set_intersection( momsIntersection.begin(), momsIntersection.end(),
179  momDaughters.begin(), momDaughters.end(),
180  inserter( tmp, tmp.begin() ) );
181  swap( momsIntersection, tmp );
182  }
183  if ( momsIntersection.size() == 0 ) return ref_type();
184  }
185  // if multiple mothers are found, return a null reference
186  if ( momsIntersection.size() > 1 ) return ref_type();
187  // return a reference to the unique mother
188  return ref_type( matched_, * momsIntersection.begin() );
189  } else {
190  // check for non-composite (leaf) candidate
191  // if one of the maps contains the candidate c
192  for( typename std::vector<const map_type *>::const_iterator m = maps_.begin();
193  m != maps_.end(); ++ m ) {
194  typename CandRefMap::const_iterator f = candRefs_.find( & c );
195  if ( f != candRefs_.end() ) {
196  reference_type ref = f->second;
197  typename map_type::const_iterator f = (*m)->find( ref );
198  if ( f != (*m)->end() ) {
199  return f->val;
200  }
201  }
202  }
203  return ref_type();
204  }
205 }
206 
207 template<typename C1, typename C2>
209  CandMatcherBase<C1, C2>( maps ) {
211 }
212 
213 template<typename C1, typename C2>
215  CandMatcherBase<C1, C2>( map ) {
217 }
218 
219 template<typename C1, typename C2>
221 }
222 
223 template<typename C1, typename C2>
224 std::vector<const reco::Candidate *> CandMatcher<C1, C2>::getDaughters( const reco::Candidate * c ) const {
225  std::vector<const reco::Candidate *> v;
226  v.push_back( c );
227  return v;
228 }
229 
230 template<typename C1, typename C2>
232  // By default, check that the number of daughters is identical
233  return( c.numberOfDaughters() == m.numberOfDaughters() );
234 }
235 
236 #endif
void swap(ora::Record &rh, ora::Record &lh)
Definition: Record.h:70
void init()
init at constructor
Definition: CandMatcher.h:89
std::vector< const map_type * > map_vector
map vector
Definition: CandMatcher.h:26
std::map< const reco::Candidate *, reference_type > CandRefMap
pointer map type
Definition: CandMatcher.h:56
int i
Definition: DBlmapReader.cc:9
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:30
reco::helper::CandRefTrait< C2 >::refProd_type refProd_type
refProd type
Definition: CandMatcher.h:24
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
std::vector< const map_type * > maps_
pointers to stored maps
Definition: CandMatcher.h:51
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:58
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:22
virtual size_type numberOfDaughters() const =0
number of daughters
virtual bool hasMasterClone() const =0
reco::helper::CandMapTrait< C1, C2 >::type map_type
map type
Definition: CandMatcher.h:20
virtual const_iterator end() const =0
last daughter const_iterator
std::vector< std::set< size_t > > matchedMothers_
mother + n.daughters indices from matched
Definition: CandMatcher.h:63
MatchedRefMap matchedRefs_
pointer map of matched candidates (e.g.: MC truth)
Definition: CandMatcher.h:61
CandMatcher(const typename CandMatcherBase< C1, C2 >::map_vector &maps)
constructor
Definition: CandMatcher.h:211
int j
Definition: DBlmapReader.cc:9
CandMatcherBase(const map_vector &maps)
constructor
double f[11][100]
virtual ~CandMatcher()
destructor
Definition: CandMatcher.h:223
Container::value_type value_type
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 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
virtual const_iterator begin() const =0
first daughter const_iterator
map_type::key_type reference_type
concrete candidate reference type
Definition: CandMatcher.h:28
std::vector< SimpleParticle > * getDaughters(HepMC::GenParticle *x)
mathSSE::Vec4< T > v
virtual std::vector< const reco::Candidate * > getDaughters(const reco::Candidate *) const =0
get ultimate daughter (can skip status = 3 in MC)
virtual bool compositePreselect(const reco::Candidate &c, const reco::Candidate &m) const =0
composite candidate preselection
virtual const CandidateBaseRef & masterClone() const =0