CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
RecoTauConstructor.cc
Go to the documentation of this file.
3 #include <boost/foreach.hpp>
4 #include <boost/bind.hpp>
5 
6 namespace reco { namespace tau {
7 
8 
9 
12  bool copyGammasFromPiZeros):pfCands_(pfCands) {
13 
14  // Initialize tau
15  tau_.reset(new PFTau());
16 
17  copyGammas_ = copyGammasFromPiZeros;
18  // Initialize our Accessors
19  collections_[std::make_pair(kSignal, kChargedHadron)] =
20  &tau_->selectedSignalPFChargedHadrCands_;
21  collections_[std::make_pair(kSignal, kGamma)] =
22  &tau_->selectedSignalPFGammaCands_;
23  collections_[std::make_pair(kSignal, kNeutralHadron)] =
24  &tau_->selectedSignalPFNeutrHadrCands_;
25  collections_[std::make_pair(kSignal, kAll)] =
26  &tau_->selectedSignalPFCands_;
27 
28  collections_[std::make_pair(kIsolation, kChargedHadron)] =
29  &tau_->selectedIsolationPFChargedHadrCands_;
30  collections_[std::make_pair(kIsolation, kGamma)] =
31  &tau_->selectedIsolationPFGammaCands_;
32  collections_[std::make_pair(kIsolation, kNeutralHadron)] =
33  &tau_->selectedIsolationPFNeutrHadrCands_;
34  collections_[std::make_pair(kIsolation, kAll)] =
35  &tau_->selectedIsolationPFCands_;
36 
37  // Build our temporary sorted collections, since you can't use stl sorts on
38  // RefVectors
39  BOOST_FOREACH(const CollectionMap::value_type &colkey, collections_) {
40  // Build an empty list for each collection
41  sortedCollections_[colkey.first] = SortedListPtr(
42  new SortedListPtr::element_type);
43  }
44 
45  tau_->setjetRef(jet);
46 }
47 
49  const PFCandidateRef& ref) {
50  if (region == kSignal) {
51  // Keep track of the four vector of the signal vector products added so far.
52  // If a photon add it if we are not using PiZeros to build the gammas
53  if ( (type != kGamma) || !copyGammas_ )
54  p4_ += ref->p4();
55  }
56  getSortedCollection(region, type)->push_back(ref);
57  // Add to global collection
58  getSortedCollection(region, kAll)->push_back(ref);
59 }
60 
62  getSortedCollection(region, type)->reserve(size);
63  getCollection(region, type)->reserve(size);
64  // Reserve global collection as well
65  getSortedCollection(region, kAll)->reserve(
66  getSortedCollection(region, kAll)->size() + size);
67  getCollection(region, kAll)->reserve(
68  getCollection(region, kAll)->size() + size);
69 }
70 
72  if(region == kSignal) {
73  tau_->signalPiZeroCandidates_.reserve(size);
74  // If we are building the gammas with the pizeros, resize that
75  // vector as well
76  if(copyGammas_)
77  reserve(kSignal, kGamma, 2*size);
78  } else {
79  tau_->isolationPiZeroCandidates_.reserve(size);
80  if(copyGammas_)
81  reserve(kIsolation, kGamma, 2*size);
82  }
83 }
84 
85 void RecoTauConstructor::addPiZero(Region region, const RecoTauPiZero& piZero) {
86  if(region == kSignal) {
87  tau_->signalPiZeroCandidates_.push_back(piZero);
88  // Copy the daughter gammas into the gamma collection if desired
89  if(copyGammas_) {
90  // If we are using the pizeros to build the gammas, make sure we update
91  // the four vector correctly.
92  p4_ += piZero.p4();
93  addPFCands(kSignal, kGamma, piZero.daughterPtrVector().begin(),
94  piZero.daughterPtrVector().end());
95  }
96  } else {
97  tau_->isolationPiZeroCandidates_.push_back(piZero);
98  if(copyGammas_) {
99  addPFCands(kIsolation, kGamma, piZero.daughterPtrVector().begin(),
100  piZero.daughterPtrVector().end());
101  }
102  }
103 }
104 
107  return collections_[std::make_pair(region, type)];
108 }
109 
112  return sortedCollections_[std::make_pair(region, type)];
113 }
114 
115 // Trivial converter needed for polymorphism
117  const PFCandidateRef& pfRef) const {
118  return pfRef;
119 }
120 
121 namespace {
122 // Make sure the two products come from the same EDM source
123 template<typename T1, typename T2>
124 void checkMatchedProductIds(const T1& t1, const T2& t2) {
125  if (t1.id() != t2.id()) {
126  throw cms::Exception("MismatchedPFCandSrc") << "Error: the input tag"
127  << " for the PF candidate collection provided to the RecoTauBuilder "
128  << " does not match the one that was used to build the source jets."
129  << " Please update the pfCandSrc paramters for the PFTau builders.";
130  }
131 }
132 }
133 
134 // Convert from a Ptr to a Ref
136  const PFCandidatePtr& pfPtr) const {
137  if(pfPtr.isNonnull()) {
138  checkMatchedProductIds(pfPtr, pfCands_);
139  return PFCandidateRef(pfCands_, pfPtr.key());
140  } else return PFCandidateRef();
141 }
142 
143 // Convert from a CandidatePtr to a Ref
145  const CandidatePtr& candPtr) const {
146  if(candPtr.isNonnull()) {
147  checkMatchedProductIds(candPtr, pfCands_);
148  return PFCandidateRef(pfCands_, candPtr.key());
149  } else return PFCandidateRef();
150 }
151 
152 namespace {
153 template<typename T> bool ptDescending(const T& a, const T& b) {
154  return a.pt() > b.pt();
155 }
156 template<typename T> bool ptDescendingRef(const T& a, const T& b) {
157  return a->pt() > b->pt();
158 }
159 }
160 
162  // The pizeros are a special case, as we can sort them in situ
163  std::sort(tau_->signalPiZeroCandidates_.begin(),
164  tau_->signalPiZeroCandidates_.end(),
165  ptDescending<RecoTauPiZero>);
166  std::sort(tau_->isolationPiZeroCandidates_.begin(),
167  tau_->isolationPiZeroCandidates_.end(),
168  ptDescending<RecoTauPiZero>);
169 
170  // Sort each of our sortable collections, and copy them into the final
171  // tau RefVector.
172  BOOST_FOREACH(const CollectionMap::value_type &colkey, collections_) {
173  SortedListPtr sortedCollection = sortedCollections_[colkey.first];
174  std::sort(sortedCollection->begin(),
175  sortedCollection->end(),
176  ptDescendingRef<PFCandidateRef>);
177  // Copy into the real tau collection
178  std::for_each(
179  sortedCollection->begin(), sortedCollection->end(),
180  boost::bind(&PFCandidateRefVector::push_back, colkey.second, _1));
181  }
182 }
183 
184 std::auto_ptr<reco::PFTau> RecoTauConstructor::get(bool setupLeadingObjects) {
185  // Copy the sorted collections into the interal tau refvectors
187 
188  // Setup all the important member variables of the tau
189  // Set charge of tau
190  tau_->setCharge(
193 
194  // Set PDG id
195  tau_->setPdgId(tau_->charge() < 0 ? 15 : -15);
196 
197  // Set P4
198  tau_->setP4(p4_);
199 // tau_->setP4(
200 // sumPFCandP4(
201 // getCollection(kSignal, kAll)->begin(),
202 // getCollection(kSignal, kAll)->end()
203 // )
204 // );
205 
206  // Set charged isolation quantities
207  tau_->setisolationPFChargedHadrCandsPtSum(
208  sumPFCandPt(
211  )
212  );
213 
214  // Set gamma isolation quantities
215  tau_->setisolationPFGammaCandsEtSum(
216  sumPFCandPt(
219  )
220  );
221  // Set em fraction
222  tau_->setemFraction(sumPFCandPt(
224  getCollection(kSignal, kGamma)->end()) / tau_->pt());
225 
226  if(setupLeadingObjects)
227  {
229  // Find the highest PT object in the signal cone
230  Iter leadingCand = leadPFCand(
233  );
234 
235  if(leadingCand != getCollection(kSignal, kAll)->end())
236  tau_->setleadPFCand(*leadingCand);
237 
238  // Hardest charged object in signal cone
239  Iter leadingChargedCand = leadPFCand(
242  );
243 
244  if(leadingChargedCand != getCollection(kSignal, kChargedHadron)->end())
245  tau_->setleadPFChargedHadrCand(*leadingChargedCand);
246 
247  // Hardest gamma object in signal cone
248  Iter leadingGammaCand = leadPFCand(
251  );
252 
253  if(leadingGammaCand != getCollection(kSignal, kGamma)->end())
254  tau_->setleadPFNeutralCand(*leadingGammaCand);
255  }
256  return tau_;
257 }
258 }} // end namespace reco::tau
type
Definition: HCALResponse.h:21
reco::Candidate::LorentzVector p4_
std::auto_ptr< reco::PFTau > get(bool setupLeadingCandidates=true)
void reserve(Region region, ParticleType type, size_t size)
Reserve a set amount of space for a given RefVector.
double sumPFCandPt(InputIterator begin, InputIterator end)
Sum the PT of a collection of PFCandidates.
virtual const LorentzVector & p4() const GCC11_FINAL
four-momentum Lorentz vector
void addPFCand(Region region, ParticleType type, const PFCandidateRef &ref)
Append a PFCandidateRef to a given collection.
SortedCollectionMap sortedCollections_
bool isNonnull() const
Checks for non-null.
Definition: Ptr.h:152
const edm::Handle< PFCandidateCollection > & pfCands_
PFCandidateRef convertToRef(const PFCandidatePtr &pfPtr) const
auto const T2 &decltype(t1.eta()) t2
Definition: deltaR.h:18
SortedListPtr getSortedCollection(Region region, ParticleType type)
#define end
Definition: vmac.h:38
edm::Ref< PFCandidateCollection > PFCandidateRef
persistent reference to a PFCandidate
void reserve(size_type n)
Reserve space for RefVector.
Definition: RefVector.h:95
Container::value_type value_type
void addPFCands(Region region, ParticleType type, const InputIterator &begin, const InputIterator &end)
InputIterator leadPFCand(InputIterator begin, InputIterator end)
boost::shared_ptr< std::vector< PFCandidateRef > > SortedListPtr
PFCandidateRefVector * getCollection(Region region, ParticleType type)
key_type key() const
Definition: Ptr.h:169
std::auto_ptr< reco::PFTau > tau_
void reservePiZero(Region region, size_t size)
Reserve a set amount of space for the PiZeros.
double b
Definition: hdecay.h:120
#define begin
Definition: vmac.h:31
RecoTauConstructor(const PFJetRef &jetRef, const edm::Handle< PFCandidateCollection > &pfCands, bool copyGammasFromPiZeros=false)
Constructor with PFCandidate Handle.
double a
Definition: hdecay.h:121
const daughters & daughterPtrVector() const
references to daughtes
void push_back(value_type const &ref)
Add a Ref&lt;C, T&gt; to the RefVector.
Definition: RefVector.h:64
void addPiZero(Region region, const RecoTauPiZero &piZero)
Add a PiZero to the given collection.
long double T
tuple size
Write out results.
int sumPFCandCharge(InputIterator begin, InputIterator end)
Sum the PT of a collection of PFCandidates.