CMS 3D CMS Logo

RecoTauCommonUtilities.h
Go to the documentation of this file.
1 #ifndef RecoTauTag_RecoTau_RecoTauCommonUtilities_h
2 #define RecoTauTag_RecoTau_RecoTauCommonUtilities_h
3 
4 /*
5  * RecoTauCommonUtilities - utilities for extracting PFCandidates from
6  * PFJets and summing over collections of PFCandidates.
7  *
8  * Author: Evan K. Friis (UC Davis)
9  *
10  */
11 
16 #include <vector>
17 #include <algorithm>
18 #include <numeric>
19 
20 // Boost helpers
21 #include <boost/iterator/transform_iterator.hpp>
22 #include <boost/iterator/indirect_iterator.hpp>
23 #include <boost/mem_fn.hpp>
24 
25 #include <boost/type_traits/is_base_of.hpp>
26 
27 namespace reco { namespace tau {
28 
30  public:
31  bool operator()(const PFCandidatePtr& a, const PFCandidatePtr& b) const {
32  return a->pt() > b->pt();
33  }
34 };
35 
38 template<typename Iterator> std::vector<PFCandidatePtr>
40  int particleId, bool sort=true) {
41  std::vector<PFCandidatePtr> output;
42  for(Iterator iter = begin; iter != end; ++iter) {
43  reco::PFCandidatePtr ptr(*iter);
44  if (ptr->particleId() == particleId)
45  output.push_back(ptr);
46  }
47  if (sort) std::sort(output.begin(), output.end(), SortPFCandsDescendingPt());
48  return output;
49 }
50 
53 std::vector<PFCandidatePtr> pfCandidates(const PFJet& jet,
54  int particleId, bool sort=true);
55 
57 std::vector<PFCandidatePtr> pfCandidates(const PFJet& jet,
58  const std::vector<int>& particleIds,
59  bool sort=true);
60 
62 std::vector<PFCandidatePtr> pfChargedCands(const PFJet& jet, bool sort=true);
63 
65 std::vector<PFCandidatePtr> pfGammas(const PFJet& jet, bool sort=true);
66 
68 std::vector<PFCandidatePtr> flattenPiZeros(const std::vector<RecoTauPiZero>::const_iterator&, const std::vector<RecoTauPiZero>::const_iterator&);
69 std::vector<PFCandidatePtr> flattenPiZeros(const std::vector<RecoTauPiZero>&);
70 
72 template<typename RefVectorType, typename BaseView>
73 RefVectorType castView(const edm::Handle<BaseView>& view) {
74  typedef typename RefVectorType::value_type OutputRef;
75  // Double check at compile time that the inheritance is okay. It can still
76  // fail at runtime if you pass it the wrong collection.
77  static_assert(
78  (boost::is_base_of<typename BaseView::value_type,
79  typename RefVectorType::member_type>::value));
80  RefVectorType output;
81  size_t nElements = view->size();
82  output.reserve(nElements);
83  // Cast each of our Refs
84  for (size_t i = 0; i < nElements; ++i) {
85  output.push_back(view->refAt(i).template castTo<OutputRef>());
86  }
87  return output;
88 }
89 
90 /*
91  *Given a range over a container of type C, return a new 'end' iterator such
92  *that at max <N> elements are taken. If there are less than N elements in the
93  *array, leave the <end> as it is
94  */
95 template<typename InputIterator> InputIterator takeNElements(
96  const InputIterator& begin, const InputIterator& end, size_t N) {
97  size_t input_size = end - begin;
98  return (N > input_size) ? end : begin + N;
99 }
100 
102 template<typename InputIterator, typename FunctionPtr, typename ReturnType>
103  ReturnType sumPFVector(InputIterator begin, InputIterator end,
104  FunctionPtr func, ReturnType init) {
106  for(InputIterator cand = begin; cand != end; ++cand) {
107  //#define CALL_MEMBER_FN(object,ptrToMember) ((object).*(ptrToMember))
108  output += ((**cand).*(func))();
109  }
110  return output;
111  }
112 
113 template<typename InputIterator> reco::Candidate::LorentzVector sumPFCandP4(
114  InputIterator begin, InputIterator end) {
115  return sumPFVector(begin, end, &PFCandidate::p4,
117 }
118 
120 template<typename InputIterator> double sumPFCandPt(InputIterator begin,
121  InputIterator end) {
122  return sumPFVector(begin, end, &PFCandidate::pt, 0.0);
123  }
124 
126 template<typename InputIterator> int sumPFCandCharge(InputIterator begin,
127  InputIterator end) {
128  return sumPFVector(begin, end, &PFCandidate::charge, 0);
129  }
130 
131 template<typename InputIterator> InputIterator leadPFCand(InputIterator begin,
132  InputIterator end) {
133  double max_pt = 0;
134  InputIterator max_cand = begin;
135  for(InputIterator cand = begin; cand != end; ++cand) {
136  if( (*cand)->pt() > max_pt ) {
137  max_pt = (*cand)->pt();
138  max_cand = cand;
139  }
140  }
141  return max_cand;
142  }
143 }} // end namespace reco::tau
144 #endif
InputIterator takeNElements(const InputIterator &begin, const InputIterator &end, size_t N)
double sumPFCandPt(InputIterator begin, InputIterator end)
Sum the pT of a collection of PFCandidates.
reco::Candidate::LorentzVector sumPFCandP4(InputIterator begin, InputIterator end)
std::vector< PFCandidatePtr > pfGammas(const PFJet &jet, bool sort=true)
Extract all pfGammas from a PFJet.
RefVectorType castView(const edm::Handle< BaseView > &view)
Convert a BaseView (View<T>) to a TRefVector.
int init
Definition: HydjetWrapper.h:67
double pt() const final
transverse momentum
int charge() const final
electric charge
Definition: LeafCandidate.h:91
std::vector< PFCandidatePtr > filterPFCandidates(const Iterator &begin, const Iterator &end, int particleId, bool sort=true)
ReturnType sumPFVector(InputIterator begin, InputIterator end, FunctionPtr func, ReturnType init)
Sum the four vectors in a collection of PFCandidates.
Jets made from PFObjects.
Definition: PFJet.h:21
std::vector< PFCandidatePtr > pfCandidates(const PFJet &jet, int particleId, bool sort=true)
std::map< DetId, double > ReturnType
const LorentzVector & p4() const final
four-momentum Lorentz vector
Definition: LeafCandidate.h:99
#define end
Definition: vmac.h:39
Definition: value.py:1
InputIterator leadPFCand(InputIterator begin, InputIterator end)
std::vector< PFCandidatePtr > flattenPiZeros(const std::vector< RecoTauPiZero >::const_iterator &, const std::vector< RecoTauPiZero >::const_iterator &)
Flatten a list of pi zeros into a list of there constituent PFCandidates.
Definition: init.py:1
#define N
Definition: blowfish.cc:9
double b
Definition: hdecay.h:120
math::XYZTLorentzVector LorentzVector
Lorentz vector.
Definition: Candidate.h:37
fixed size matrix
#define begin
Definition: vmac.h:32
std::vector< PFCandidatePtr > pfChargedCands(const PFJet &jet, bool sort=true)
Extract all non-neutral candidates from a PFJet.
double a
Definition: hdecay.h:121
bool operator()(const PFCandidatePtr &a, const PFCandidatePtr &b) const
int sumPFCandCharge(InputIterator begin, InputIterator end)
Sum the charge of a collection of PFCandidates.