CMS 3D CMS Logo

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> > >
22  public:
24  ~MatcherBase() override;
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::StreamID, edm::Event&, const edm::EventSetup&) const 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,
41  typename C2,
42  typename S,
45  class Matcher : public MatcherBase<C1, C2, M> {
46  public:
48  : MatcherBase<C1, C2, M>(cfg), select_(reco::modules::make<S>(cfg)), distance_(reco::modules::make<D>(cfg)) {}
49  ~Matcher() override {}
50 
51  private:
52  typedef typename MatcherBase<C1, C2, M>::T1 T1;
53  typedef typename MatcherBase<C1, C2, M>::T2 T2;
55 
56  double matchDistance(const T1& c1, const T2& c2) const override { return distance_(c1, c2); }
57  bool select(const T1& c1, const T2& c2) const override { return select_(c1, c2); }
60  };
61 
62  namespace helper {
63  typedef std::pair<size_t, double> MatchPair;
64 
65  struct SortBySecond {
66  bool operator()(const MatchPair& p1, const MatchPair& p2) const { return p1.second < p2.second; }
67  };
68  } // namespace helper
69 
70  template <typename C1, typename C2, typename M>
72  : srcToken_(consumes<C1>(cfg.template getParameter<edm::InputTag>("src"))),
73  matchedToken_(consumes<C2>(cfg.template getParameter<edm::InputTag>("matched"))),
74  distMin_(cfg.template getParameter<double>("distMin")) {
75  produces<MatchMap>();
76  }
77 
78  template <typename C1, typename C2, typename M>
80 
81  template <typename C1, typename C2, typename M>
83  using namespace edm;
84  using namespace std;
86  evt.getByToken(matchedToken_, matched);
88  evt.getByToken(srcToken_, cands);
89  typedef typename MatchMap::ref_type ref_type;
90  typedef typename ref_type::key_type key_ref_type;
91  typedef typename ref_type::value_type value_ref_type;
92  unique_ptr<MatchMap> matchMap(new MatchMap(ref_type(key_ref_type(cands), value_ref_type(matched))));
93  for (size_t c = 0; c != cands->size(); ++c) {
94  const T1& cand = (*cands)[c];
95  vector<helper::MatchPair> v;
96  for (size_t m = 0; m != matched->size(); ++m) {
97  const T2& match = (*matched)[m];
98  if (select(cand, match)) {
99  double dist = matchDistance(cand, match);
100  if (dist < distMin_)
101  v.push_back(make_pair(m, dist));
102  }
103  }
104  if (!v.empty()) {
105  size_t mMin = min_element(v.begin(), v.end(), helper::SortBySecond())->first;
106  typedef typename MatchMap::key_type key_type;
107  typedef typename MatchMap::data_type data_type;
108  matchMap->insert(edm::getRef(cands, c), edm::getRef(matched, mMin));
109  }
110  }
111  evt.put(std::move(matchMap));
112  }
113 
114  } // namespace modules
115 } // namespace reco
116 
117 #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
::ecal::reco::ComputationScalarType data_type
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:133
Definition: helper.py:1
Definition: deltaR.h:58
Matcher(const edm::ParameterSet &cfg)
Definition: Matcher.h:47
S make(const edm::ParameterSet &cfg)
bool operator()(const MatchPair &p1, const MatchPair &p2) const
Definition: Matcher.h:66
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:539
edm::EDGetTokenT< C1 > srcToken_
Definition: Matcher.h:33
MatcherBase< C1, C2, M >::T2 T2
Definition: Matcher.h:53
virtual double matchDistance(const T1 &, const T2 &) const =0
def template(fileName, svg, replaceme="REPLACEME")
Definition: svgfig.py:521
void produce(edm::StreamID, edm::Event &, const edm::EventSetup &) const override
Definition: Matcher.h:82
DecomposeProduct< arg, typename Div::arg > D
Definition: Factorize.h:141
C1::value_type T1
Definition: Matcher.h:27
~Matcher() override
Definition: Matcher.h:49
MatcherBase< C1, C2, M >::T1 T1
Definition: Matcher.h:52
C2::value_type T2
Definition: Matcher.h:28
std::pair< size_t, double > MatchPair
Definition: Matcher.h:63
edm::EDGetTokenT< C2 > matchedToken_
Definition: Matcher.h:34
fixed size matrix
HLT enums.
bool select(const T1 &c1, const T2 &c2) const override
Definition: Matcher.h:57
std::pair< typename Association::data_type::first_type, double > match(Reference key, Association association, bool bestMatchByMaxValue)
Generic matching function.
Definition: Utils.h:10
~MatcherBase() override
Definition: Matcher.h:79
MatcherBase< C1, C2, M >::MatchMap MatchMap
Definition: Matcher.h:54
def move(src, dest)
Definition: eostools.py:511
double matchDistance(const T1 &c1, const T2 &c2) const override
Definition: Matcher.h:56
MatcherBase(const edm::ParameterSet &)
Definition: Matcher.h:71