CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Matcher.h
Go to the documentation of this file.
1 #ifndef UtilAlgos_Matcher_h
2 #define UtilAlgos_Matcher_h
3 /* \class Matcher
4  *
5  * \author Luca Lista, INFN
6  *
7  */
17 
18 namespace reco {
19  namespace modules {
20  template<typename C1, typename C2, typename M = edm::AssociationMap<edm::OneToOne<C1, C2> > >
21  class MatcherBase : public edm::EDProducer {
22  public:
23  MatcherBase( const edm::ParameterSet & );
24  ~MatcherBase();
25 
26  protected:
27  typedef typename C1::value_type T1;
28  typedef typename C2::value_type T2;
29  typedef M MatchMap;
30 
31  private:
32  void produce( edm::Event&, const edm::EventSetup&) override;
35  double distMin_;
36  virtual double matchDistance( const T1 &, const T2 & ) const = 0;
37  virtual bool select( const T1 &, const T2 & ) const = 0;
38  };
39 
40  template<typename C1, typename C2,
43  class Matcher : public MatcherBase<C1, C2, M> {
44  public:
45  Matcher( const edm::ParameterSet & cfg ) :
46  MatcherBase<C1, C2, M>( cfg ),
47  select_( reco::modules::make<S>( cfg ) ),
48  distance_( reco::modules::make<D>( cfg ) ) { }
49  ~Matcher() { }
50  private:
51  typedef typename MatcherBase<C1, C2, M>::T1 T1;
52  typedef typename MatcherBase<C1, C2, M>::T2 T2;
54 
55  double matchDistance( const T1 & c1, const T2 & c2 ) const {
56  return distance_( c1, c2 );
57  }
58  bool select( const T1 & c1, const T2 & c2 ) const {
59  return select_( c1, c2 );
60  }
63  };
64 
65  namespace helper {
66  typedef std::pair<size_t, double> MatchPair;
67 
68  struct SortBySecond {
69  bool operator()( const MatchPair & p1, const MatchPair & p2 ) const {
70  return p1.second < p2.second;
71  }
72  };
73  }
74 
75  template<typename C1, typename C2, typename M>
77  srcToken_( consumes<C1>( cfg.template getParameter<edm::InputTag>( "src" ) ) ),
78  matchedToken_( consumes<C2>( cfg.template getParameter<edm::InputTag>( "matched" ) ) ),
79  distMin_( cfg.template getParameter<double>( "distMin" ) ) {
80  produces<MatchMap>();
81  }
82 
83  template<typename C1, typename C2, typename M>
85 
86  template<typename C1, typename C2, typename M>
88  using namespace edm;
89  using namespace std;
90  Handle<C2> matched;
91  evt.getByToken( matchedToken_, matched );
92  Handle<C1> cands;
93  evt.getByToken( srcToken_, cands );
94  typedef typename MatchMap::ref_type ref_type;
95  typedef typename ref_type::key_type key_ref_type;
96  typedef typename ref_type::value_type value_ref_type;
97  auto_ptr<MatchMap> matchMap( new MatchMap( ref_type( key_ref_type( cands ),
98  value_ref_type( matched ) ) ) );
99  for( size_t c = 0; c != cands->size(); ++ c ) {
100  const T1 & cand = (*cands)[ c ];
101  vector<helper::MatchPair> v;
102  for( size_t m = 0; m != matched->size(); ++ m ) {
103  const T2 & match = ( * matched )[ m ];
104  if ( select( cand, match ) ) {
105  double dist = matchDistance( cand, match );
106  if ( dist < distMin_ ) v.push_back( make_pair( m, dist ) );
107  }
108  }
109  if ( v.size() > 0 ) {
110  size_t mMin = min_element( v.begin(), v.end(), helper::SortBySecond() )->first;
111  typedef typename MatchMap::key_type key_type;
112  typedef typename MatchMap::data_type data_type;
113  matchMap->insert( edm::getRef( cands, c ), edm::getRef( matched, mMin ) );
114  }
115  }
116  evt.put( matchMap );
117  }
118 
119  }
120 }
121 
122 #endif
helper::MatcherGetRef< C >::ref_type getRef(const Handle< C > &c, size_t k)
Definition: getRef.h:28
virtual bool select(const T1 &, const T2 &) const =0
Definition: deltaR.h:79
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:434
double matchDistance(const T1 &c1, const T2 &c2) const
Definition: Matcher.h:55
Matcher(const edm::ParameterSet &cfg)
Definition: Matcher.h:45
S make(const edm::ParameterSet &cfg)
edm::EDGetTokenT< C1 > srcToken_
Definition: Matcher.h:33
Float p2
Definition: deltaR.h:21
bool select(const T1 &c1, const T2 &c2) const
Definition: Matcher.h:58
MatcherBase< C1, C2, M >::T2 T2
Definition: Matcher.h:52
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:116
Float p1
Definition: deltaR.h:20
virtual double matchDistance(const T1 &, const T2 &) const =0
bool first
Definition: L1TdeRCT.cc:75
Container::value_type value_type
C1::value_type T1
Definition: Matcher.h:27
MatcherBase< C1, C2, M >::T1 T1
Definition: Matcher.h:51
void produce(edm::Event &, const edm::EventSetup &) override
Definition: Matcher.h:87
C2::value_type T2
Definition: Matcher.h:28
std::pair< size_t, double > MatchPair
Definition: Matcher.h:66
edm::EDGetTokenT< C2 > matchedToken_
Definition: Matcher.h:34
bool operator()(const MatchPair &p1, const MatchPair &p2) const
Definition: Matcher.h:69
std::pair< typename Association::data_type::first_type, double > match(Reference key, Association association, bool bestMatchByMaxValue)
Generic matching function.
Definition: Utils.h:6
MatcherBase< C1, C2, M >::MatchMap MatchMap
Definition: Matcher.h:53
DecomposeProduct< arg, typename Div::arg > D
Definition: Factorize.h:150
def template
Definition: svgfig.py:520
MatcherBase(const edm::ParameterSet &)
Definition: Matcher.h:76