CMS 3D CMS Logo

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
RecoTauDistanceFromTruthPlugin.cc
Go to the documentation of this file.
2 
8 //#include "DataFormats/Common/interface/AssociativeIterator.h"
9 
10 #include <boost/foreach.hpp>
11 #include <boost/bind.hpp>
12 #include <boost/iterator/filter_iterator.hpp>
13 
14 namespace tautools {
15 
17  public:
20  double operator()(const reco::PFTauRef&) const;
21  void beginEvent();
22  private:
26 };
27 
29  const edm::ParameterSet& pset): reco::tau::RecoTauCleanerPlugin(pset) {
30  matchingSrc_ = pset.getParameter<edm::InputTag>("matching");
31 }
32 
34  // Load the matching information
36 }
37 
38 // Helpers
39 namespace {
40  // Returns the squared momentum difference between two candidates
41  double momentumDifference(const reco::Candidate* candA,
42  const reco::Candidate* candB) {
44  candA->p4() - candB->p4();
45  return difference.P2();
46  }
47 
48  // Finds the best match for an input <cand> from an input colleciton.
49  // Only objects with matching charge are considered. The best match
50  // has the lowest [momentumDifference] with the input <cand>
51  template<typename InputIterator>
52  InputIterator findBestMatch(const reco::Candidate* cand,
53  InputIterator begin, InputIterator end) {
54 
55  typedef const reco::Candidate* CandPtr;
56  using boost::bind;
57  using boost::function;
58  using boost::filter_iterator;
59  // Build a charge matching function
60  typedef function<bool (CandPtr)> CandPtrBoolFn;
61  CandPtrBoolFn chargeMatcher =
62  bind(&reco::Candidate::charge, cand) == bind(&reco::Candidate::charge, _1);
63 
64  // Only match those objects that have the same charge
65  typedef filter_iterator<CandPtrBoolFn, InputIterator> Iterator;
66  Iterator begin_filtered(chargeMatcher, begin, end);
67  Iterator end_filtered(chargeMatcher, end, end);
68 
69  Iterator result = std::min_element(begin_filtered, end_filtered,
70  momentumDifference);
71  return result.base();
72  }
73 } // end anon. namespace
74 
76 
77  GenJetAssociation::reference_type truth = (*genTauMatch_)[tauRef];
78 
79  // Check if the matching exists, if not return +infinity
80  if (truth.isNull())
82 
83  // screw all this noise
84  return std::abs(tauRef->pt() - truth->pt());
85 
86  typedef const reco::Candidate* CandPtr;
87  typedef std::set<CandPtr> CandPtrSet;
88  typedef std::vector<CandPtr> CandPtrVector;
89  typedef std::list<CandPtr> CandPtrList;
90  // Store all of our reco and truth candidates
91  CandPtrList recoCands;
92  CandPtrSet truthCandSet;
93 
94  BOOST_FOREACH(const reco::RecoTauPiZero& piZero,
95  tauRef->signalPiZeroCandidates()) {
96  recoCands.push_back(&piZero);
97  }
98 
99  BOOST_FOREACH(const reco::PFCandidateRef& pfch,
100  tauRef->signalPFChargedHadrCands()) {
101  recoCands.push_back(pfch.get());
102  }
103 
104  // Use a set to contain the truth pointers so we ensure that no pizeros
105  // are entered twice.
106  BOOST_FOREACH(const reco::CandidatePtr& ptr,
107  truth->daughterPtrVector()) {
108  // Get mother pi zeros in the case of gammas
109  if (ptr->pdgId() == 22)
110  truthCandSet.insert(ptr->mother());
111  else
112  truthCandSet.insert(ptr.get());
113  }
114 
115  //Convert truth cands from set to vector so we can sort it.
116  CandPtrVector truthCands(truthCandSet.begin(), truthCandSet.end());
117 
118  // Sort the truth candidates by descending pt
119  std::sort(truthCands.begin(), truthCands.end(),
120  boost::bind(&reco::Candidate::pt, _1) > boost::bind(&reco::Candidate::pt, _2));
121 
122  double quality = 0.0;
123  BOOST_FOREACH(CandPtr truthCand, truthCands) {
124  // Find the best reco match for this truth cand
125  CandPtrList::iterator recoMatch = findBestMatch(truthCand,
126  recoCands.begin(), recoCands.end());
127 
128  // Check if this truth cand is matched
129  if (recoMatch != recoCands.end()) {
130  // Add a penalty factor based on how different the reconstructed
131  // is w.r.t. the true momentum
132  quality += momentumDifference(truthCand, *recoMatch);
133  // Remove this reco cand from future matches
134  recoCands.erase(recoMatch);
135  } else {
136  // this truth cand was not matched!
137  quality += truthCand->p4().P2();
138  }
139  }
140 
141  // Now add a penalty for the unmatched reco stuff
142  BOOST_FOREACH(CandPtr recoCand, recoCands) {
143  quality += recoCand->p4().P2();
144  }
145 
146  return quality;
147 }
148 
149 } // end tautools namespace
150 
151 
152 // Register our plugin
T getParameter(std::string const &) const
Definition: vlib.h:257
virtual double pt() const =0
transverse momentum
#define abs(x)
Definition: mlp_lapack.h:159
edm::Association< reco::GenJetCollection > GenJetAssociation
bool isNull() const
Checks for null.
Definition: Ref.h:244
T const * get() const
Returns C++ pointer to the item.
Definition: Ptr.h:147
helper::RootFunctionHelper< F, args >::root_function function(F &f)
Definition: rootFunction.h:14
tuple result
Definition: query.py:137
const double infinity
tuple pset
Definition: CrabTask.py:85
virtual int charge() const =0
electric charge
#define end
Definition: vmac.h:38
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:359
double operator()(const reco::PFTauRef &) const
math::XYZTLorentzVector LorentzVector
Lorentz vector.
Definition: Candidate.h:39
#define begin
Definition: vmac.h:31
#define DEFINE_EDM_PLUGIN(factory, type, name)
T const * get() const
Returns C++ pointer to the item.
Definition: Ref.h:239
RecoTauDistanceFromTruthPlugin(const edm::ParameterSet &pset)
virtual const LorentzVector & p4() const =0
four-momentum Lorentz vector