CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
FastCandMatcher.h
Go to the documentation of this file.
1 #ifndef CandUtils_FastCandMatcher_h
2 #define CandUtils_FastCandMatcher_h
3 /* class CandMatcher
4  *
5  * \author Luca Lista, INFN
6  *
7  */
8 #include <set>
13 
14 template<typename C>
16 public:
20  typedef std::vector<const map_type *> map_vector;
22  explicit FastCandMatcher( const map_vector & maps );
24  explicit FastCandMatcher( const map_type & map );
26  const reco::Candidate * operator()( const reco::Candidate & ) const;
27 
28 protected:
29  const std::vector<const map_type *> & maps() const { return maps_; }
30 private:
32  std::vector<const map_type *> maps_;
33 };
34 
35 template<typename C>
37  maps_( maps ) {
38 }
39 
40 template<typename C>
42  maps_( 1, & map ) {
43 }
44 
45 template<typename C>
47  using namespace reco;
48  using namespace std;
49  if ( c.hasMasterClone() )
50  return (*this)( * c.masterClone() );
51  unsigned int nDau = c.numberOfDaughters();
52  if ( nDau > 0 ) {
53  // check for composite candidate c
54  // navigate to daughters and find parent matches
55  set<const reco::Candidate *> momsIntersection, momDaughters, tmp;
56  for( Candidate::const_iterator dau = c.begin(); dau != c.end(); ++ dau ) {
57  // check here generically if status == 3, then descend down to one more level
58  const Candidate * dauMatch = (*this)( * dau );
59  // if a daughter does not match, return a null ref.
60  if ( dauMatch == 0 ) return 0;
61  // get matched mothers
62  size_t mothers = dauMatch->numberOfMothers();
63  for( size_t i = 0; i < mothers; ++ i ) {
64  const reco::Candidate * mom = dauMatch->mother( i );
65  if ( mom != 0 && mom->pdgId() == dauMatch->pdgId() &&
66  mom->status() == 3 && dauMatch->status() == 1 ) {
67  // assume a single mother at this point...
68  mom = mom->mother( 0 );
69  }
70  momDaughters.insert( mom );
71  }
72  // if no mother was found return null reference
73  if ( momDaughters.size() == 0 ) return 0;
74  // the first time, momsIntersection is set to momDaughters
75  if ( momsIntersection.size() == 0 ) momsIntersection = momDaughters;
76  else {
77  tmp.clear();
78  set_intersection( momsIntersection.begin(), momsIntersection.end(),
79  momDaughters.begin(), momDaughters.end(),
80  inserter( tmp, tmp.begin() ) );
81  swap( momsIntersection, tmp );
82  }
83  if ( momsIntersection.size() == 0 ) return 0;
84  }
85  // if multiple mothers are found, return a null reference
86  if ( momsIntersection.size() > 1 ) return 0;
87  // return a reference to the unique mother
88  return * momsIntersection.begin();
89  } else {
90  // check for non-composite (leaf) candidate
91  // if one of the maps contains the candidate c
92  for( typename std::vector<const map_type *>::const_iterator m = maps_.begin();
93  m != maps_.end(); ++ m ) {
94  for( typename map_type::const_iterator i = (*m)->begin(); i != (*m)->end(); ++ i ) {
95  if ( & * i->key == & c )
96  return & * i->val;
97  }
98  }
99  return 0;
100  }
101 }
102 
103 #endif
int i
Definition: DBlmapReader.cc:9
virtual const Candidate * mother(size_type i=0) const =0
return pointer to mother
virtual int status() const =0
status word
virtual size_type numberOfMothers() const =0
number of mothers (zero or one in most of but not all the cases)
FastCandMatcher(const map_vector &maps)
constructor
virtual size_type numberOfDaughters() const =0
number of daughters
virtual bool hasMasterClone() const =0
edm::AssociationMap< edm::OneToOne< C, reco::CandidateCollection > > map_type
map type
const_iterator end() const
last daughter const_iterator
Definition: Candidate.h:146
virtual int pdgId() const =0
PDG identifier.
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
const_iterator begin() const
first daughter const_iterator
Definition: Candidate.h:144
std::vector< const map_type * > map_vector
map vector
const reco::Candidate * operator()(const reco::Candidate &) const
get match from transient reference
void swap(reco::ClusterRemovalInfo &cri1, reco::ClusterRemovalInfo &cri2)
const std::vector< const map_type * > & maps() const
std::vector< const map_type * > maps_
pointers to stored maps
virtual const CandidateBaseRef & masterClone() const =0