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_;
55  typedef std::map<const reco::Candidate *, reference_type> CandRefMap;
57  typedef std::map<const reco::Candidate *, ref_type> MatchedRefMap;
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
muonTagProbeFilters_cff.matched
matched
Definition: muonTagProbeFilters_cff.py:62
edm::RefProd
Definition: EDProductfwd.h:25
CandMatcherBase::MatchedRefMap
std::map< const reco::Candidate *, ref_type > MatchedRefMap
pointer map type
Definition: CandMatcher.h:57
mps_fire.i
i
Definition: mps_fire.py:355
CandMatcherBase::reference_type
map_type::key_type reference_type
concrete candidate reference type
Definition: CandMatcher.h:27
CandMatcherBase::maps_
std::vector< const map_type * > maps_
pointers to stored maps
Definition: CandMatcher.h:51
edm::errors::InvalidReference
Definition: EDMException.h:39
CandMatcherBase::~CandMatcherBase
virtual ~CandMatcherBase()
destructor
Definition: CandMatcher.h:146
f
double f[11][100]
Definition: MuScleFitUtils.cc:78
CandMatcherBase::compositePreselect
virtual bool compositePreselect(const reco::Candidate &c, const reco::Candidate &m) const =0
composite candidate preselection
CandMatcherBase::map_type
reco::helper::CandMapTrait< C1, C2 >::type map_type
map type
Definition: CandMatcher.h:19
getDaughters
std::vector< TauSpinner::SimpleParticle > * getDaughters(HepMC::GenParticle *x)
Definition: read_particles_from_HepMC.cc:7
CandMatcherBase::map_vector
std::vector< const map_type * > map_vector
map vector
Definition: CandMatcher.h:25
CandMatcherBase::matchedRefs_
MatchedRefMap matchedRefs_
pointer map of matched candidates (e.g.: MC truth)
Definition: CandMatcher.h:61
AssociationMap.h
CandMatcher::getDaughters
virtual std::vector< const reco::Candidate * > getDaughters(const reco::Candidate *) const
get ultimate daughter (get all in the general case)
Definition: CandMatcher.h:227
reco::candidate::const_iterator
Definition: const_iterator.h:14
OneToOne.h
cms::cuda::assert
assert(be >=bs)
CandMatcherBase::operator()
ref_type operator()(const reco::Candidate &) const
get match from transient reference
Definition: CandMatcher.h:150
CandMatcherBase::maps
const std::vector< const map_type * > & maps() const
Definition: CandMatcher.h:48
reco
fixed size matrix
Definition: AlignmentAlgorithmBase.h:45
findQualityFiles.v
v
Definition: findQualityFiles.py:179
createJobs.tmp
tmp
align.sh
Definition: createJobs.py:716
CandMatcherBase::ref_type
reco::helper::CandRefTrait< C2 >::ref_type ref_type
ref type
Definition: CandMatcher.h:21
edm::Ref
Definition: AssociativeIterator.h:58
CandMatcherBase::getDaughters
virtual std::vector< const reco::Candidate * > getDaughters(const reco::Candidate *) const =0
get ultimate daughter (can skip status = 3 in MC)
edm::Exception
Definition: EDMException.h:77
EDMException.h
CandMatcherBase::init
void init()
init at constructor
Definition: CandMatcher.h:89
CandMatcher
Definition: CandMatcher.h:69
visualization-live-secondInstance_cfg.m
m
Definition: visualization-live-secondInstance_cfg.py:72
CandMatcherBase::CandMatcherBase
CandMatcherBase(const map_vector &maps)
constructor
dqmdumpme.k
k
Definition: dqmdumpme.py:60
CandMatcher::~CandMatcher
virtual ~CandMatcher()
destructor
Definition: CandMatcher.h:223
CandMatcherBase::candRefs_
CandRefMap candRefs_
pointer map of candidates (e.g.: reco)
Definition: CandMatcher.h:59
edm::AssociationMap
Definition: AssociationMap.h:48
CandMatcher::CandMatcher
CandMatcher(const typename CandMatcherBase< C1, C2 >::map_vector &maps)
constructor
Definition: CandMatcher.h:211
reco::JetExtendedAssociation::value_type
Container::value_type value_type
Definition: JetExtendedAssociation.h:30
HltBtagPostValidation_cff.c
c
Definition: HltBtagPostValidation_cff.py:31
reco::Candidate
Definition: Candidate.h:27
CandMapTrait.h
CandMatcherBase::CandRefMap
std::map< const reco::Candidate *, reference_type > CandRefMap
pointer map type
Definition: CandMatcher.h:55
std
Definition: JetResolutionObject.h:76
CandMatcherBase
Definition: CandMatcher.h:16
CandMatcherBase::value_type
reference_type::value_type value_type
concrete candidate reference type
Definition: CandMatcher.h:29
CandMatcherBase::refProd_type
reco::helper::CandRefTrait< C2 >::refProd_type refProd_type
refProd type
Definition: CandMatcher.h:23
HLT_2018_cff.cands
cands
Definition: HLT_2018_cff.py:13762
edm::AssociationMap::key_type
Tag::key_type key_type
insert key type
Definition: AssociationMap.h:59
reco::swap
void swap(reco::ClusterRemovalInfo &cri1, reco::ClusterRemovalInfo &cri2)
Definition: ClusterRemovalInfo.cc:12
CandMatcher::compositePreselect
virtual bool compositePreselect(const reco::Candidate &c, const reco::Candidate &m) const
composite candidate preselection
Definition: CandMatcher.h:234
ztail.d
d
Definition: ztail.py:151
Candidate.h
genParticles_cff.map
map
Definition: genParticles_cff.py:11
dqmiolumiharvest.j
j
Definition: dqmiolumiharvest.py:66
CandMatcherBase::initMaps
void initMaps()
init maps
Definition: CandMatcher.h:114
edm::AssociationMap::const_iterator
const iterator
Definition: AssociationMap.h:76
CandMatcherBase::matched_
refProd_type matched_
reference to matched collectino
Definition: CandMatcher.h:53
CandMatcherBase::matchedMothers_
std::vector< std::set< size_t > > matchedMothers_
mother + n.daughters indices from matched
Definition: CandMatcher.h:63