CMS 3D CMS Logo

IsolationComputer.cc
Go to the documentation of this file.
3 
4 #include <algorithm>
5 namespace {
6  struct ByEta {
7  bool operator()(const pat::PackedCandidate *c1, const pat::PackedCandidate *c2) const {
8  return c1->eta() < c2->eta();
9  }
10  bool operator()(float c1eta, const pat::PackedCandidate *c2) const {
11  return c1eta < c2->eta();
12  }
13  bool operator()(const pat::PackedCandidate *c1, float c2eta) const {
14  return c1->eta() < c2eta;
15  }
16  };
17 }
18 void heppy::IsolationComputer::setPackedCandidates(const std::vector<pat::PackedCandidate> & all, int fromPV_thresh, float dz_thresh, float dxy_thresh, bool also_leptons)
19 {
20  allcands_ = &all;
21  charged_.clear(); neutral_.clear(); pileup_.clear();
22 
23  for (const pat::PackedCandidate &p : all) {
24  if (p.charge() == 0) {
25  neutral_.push_back(&p);
26  } else {
27 
28  if ( (abs(p.pdgId()) == 211 ) || ( also_leptons && ((abs(p.pdgId()) == 11 ) || (abs(p.pdgId()) == 13 )) ) ) {
29 
30  if (p.fromPV() > fromPV_thresh && fabs(p.dz()) < dz_thresh && fabs(p.dxy()) < dxy_thresh ) {
31  charged_.push_back(&p);
32  } else {
33  pileup_.push_back(&p);
34  }
35 
36  }
37 
38  }
39  }
40  if (weightCone_ > 0) weights_.resize(neutral_.size());
41  std::fill(weights_.begin(), weights_.end(), -1.f);
42  std::sort(charged_.begin(), charged_.end(), ByEta());
43  std::sort(neutral_.begin(), neutral_.end(), ByEta());
44  std::sort(pileup_.begin(), pileup_.end(), ByEta());
45  clearVetos();
46 }
47 
50  for (unsigned int i = 0, n = cand.numberOfSourceCandidatePtrs(); i < n; ++i) {
52  if (cp.isNonnull() && cp.isAvailable()) vetos_.push_back(&*cp);
53  }
54 }
55 
58  vetos_.clear();
59 }
60 
62 float heppy::IsolationComputer::chargedAbsIso(const reco::Candidate &cand, float dR, float innerR, float threshold, SelfVetoPolicy selfVeto) const {
63  return isoSumRaw(charged_, cand, dR, innerR, threshold, selfVeto);
64 }
65 
67 float heppy::IsolationComputer::puAbsIso(const reco::Candidate &cand, float dR, float innerR, float threshold, SelfVetoPolicy selfVeto) const {
68  return isoSumRaw(pileup_, cand, dR, innerR, threshold, selfVeto);
69 }
70 
71 float heppy::IsolationComputer::neutralAbsIsoRaw(const reco::Candidate &cand, float dR, float innerR, float threshold, SelfVetoPolicy selfVeto) const {
72  return isoSumRaw(neutral_, cand, dR, innerR, threshold, selfVeto);
73 }
74 
75 float heppy::IsolationComputer::neutralAbsIsoWeighted(const reco::Candidate &cand, float dR, float innerR, float threshold, SelfVetoPolicy selfVeto) const {
76  return isoSumNeutralsWeighted(cand, dR, innerR, threshold, selfVeto);
77 }
78 
79 float heppy::IsolationComputer::neutralHadAbsIsoRaw(const reco::Candidate &cand, float dR, float innerR, float threshold, SelfVetoPolicy selfVeto) const {
80  return isoSumRaw(neutral_, cand, dR, innerR, threshold, selfVeto, 130);
81 }
82 
83 float heppy::IsolationComputer::neutralHadAbsIsoWeighted(const reco::Candidate &cand, float dR, float innerR, float threshold, SelfVetoPolicy selfVeto) const {
84  return isoSumNeutralsWeighted(cand, dR, innerR, threshold, selfVeto, 130);
85 }
86 
87 float heppy::IsolationComputer::photonAbsIsoRaw(const reco::Candidate &cand, float dR, float innerR, float threshold, SelfVetoPolicy selfVeto) const {
88  return isoSumRaw(neutral_, cand, dR, innerR, threshold, selfVeto, 22);
89 }
90 
91 float heppy::IsolationComputer::photonAbsIsoWeighted(const reco::Candidate &cand, float dR, float innerR, float threshold, SelfVetoPolicy selfVeto) const {
92  return isoSumNeutralsWeighted(cand, dR, innerR, threshold, selfVeto, 22);
93 }
94 
95 float heppy::IsolationComputer::isoSumRaw(const std::vector<const pat::PackedCandidate *> & cands, const reco::Candidate &cand, float dR, float innerR, float threshold, SelfVetoPolicy selfVeto, int pdgId) const
96 {
97  float dR2 = dR*dR, innerR2 = innerR*innerR;
98 
99  std::vector<const reco::Candidate *> vetos(vetos_);
100  for (unsigned int i = 0, n = cand.numberOfSourceCandidatePtrs(); i < n; ++i) {
101  if (selfVeto == selfVetoNone) break;
102  const reco::CandidatePtr &cp = cand.sourceCandidatePtr(i);
103  if (cp.isNonnull() && cp.isAvailable()) {
104  vetos.push_back(&*cp);
105  if (selfVeto == selfVetoFirst) break;
106  }
107  }
108 
109  typedef std::vector<const pat::PackedCandidate *>::const_iterator IT;
110  IT candsbegin = std::lower_bound(cands.begin(), cands.end(), cand.eta() - dR, ByEta());
111  IT candsend = std::upper_bound(candsbegin, cands.end(), cand.eta() + dR, ByEta());
112 
113  double isosum = 0;
114  for (IT icharged = candsbegin; icharged < candsend; ++icharged) {
115  // pdgId
116  if (pdgId > 0 && abs((*icharged)->pdgId()) != pdgId) continue;
117  // threshold
118  if (threshold > 0 && (*icharged)->pt() < threshold) continue;
119  // cone
120  float mydr2 = reco::deltaR2(**icharged, cand);
121  if (mydr2 >= dR2 || mydr2 < innerR2) continue;
122  // veto
123  if (std::find(vetos.begin(), vetos.end(), *icharged) != vetos.end()) {
124  continue;
125  }
126  // add to sum
127  isosum += (*icharged)->pt();
128  }
129  return isosum;
130 }
131 
132 float heppy::IsolationComputer::isoSumNeutralsWeighted(const reco::Candidate &cand, float dR, float innerR, float threshold, SelfVetoPolicy selfVeto, int pdgId) const
133 {
134  if (weightCone_ <= 0) throw cms::Exception("LogicError", "you must set a valid weight cone to use this method");
135  float dR2 = dR*dR, innerR2 = innerR*innerR, weightCone2 = weightCone_*weightCone_;
136 
137  std::vector<const reco::Candidate *> vetos(vetos_);
138  for (unsigned int i = 0, n = cand.numberOfSourceCandidatePtrs(); i < n; ++i) {
139  if (selfVeto == selfVetoNone) break;
140  const reco::CandidatePtr &cp = cand.sourceCandidatePtr(i);
141  if (cp.isNonnull() && cp.isAvailable()) {
142  vetos.push_back(&*cp);
143  if (selfVeto == selfVetoFirst) break;
144  }
145  }
146 
147  typedef std::vector<const pat::PackedCandidate *>::const_iterator IT;
148  IT charged_begin = std::lower_bound(charged_.begin(), charged_.end(), cand.eta() - dR - weightCone_, ByEta());
149  IT charged_end = std::upper_bound(charged_begin, charged_.end(), cand.eta() + dR + weightCone_, ByEta());
150  IT pileup_begin = std::lower_bound(pileup_.begin(), pileup_.end(), cand.eta() - dR - weightCone_, ByEta());
151  IT pileup_end = std::upper_bound(pileup_begin, pileup_.end(), cand.eta() + dR + weightCone_, ByEta());
152  IT neutral_begin = std::lower_bound(neutral_.begin(), neutral_.end(), cand.eta() - dR, ByEta());
153  IT neutral_end = std::upper_bound(neutral_begin, neutral_.end(), cand.eta() + dR, ByEta());
154 
155  double isosum = 0.0;
156  for (IT ineutral = neutral_begin; ineutral < neutral_end; ++ineutral) {
157  // pdgId
158  if (pdgId > 0 && abs((*ineutral)->pdgId()) != pdgId) continue;
159  // threshold
160  if (threshold > 0 && (*ineutral)->pt() < threshold) continue;
161  // cone
162  float mydr2 = reco::deltaR2(**ineutral, cand);
163  if (mydr2 >= dR2 || mydr2 < innerR2) continue;
164  // veto
165  if (std::find(vetos.begin(), vetos.end(), *ineutral) != vetos.end()) {
166  continue;
167  }
168  // weight
169  float &w = weights_[ineutral-neutral_.begin()];
170  if (w == -1.f) {
171  double sumc = 0, sump = 0.0;
172  for (IT icharged = charged_begin; icharged < charged_end; ++icharged) {
173  float hisdr2 = std::max<float>(reco::deltaR2(**icharged, **ineutral), 0.01f);
174  if (hisdr2 > weightCone2) continue;
175  if (std::find(vetos_.begin(), vetos_.end(), *icharged) != vetos_.end()) {
176  continue;
177  }
178  sumc += std::log( (*icharged)->pt() / std::sqrt(hisdr2) );
179  }
180  for (IT ipileup = pileup_begin; ipileup < pileup_end; ++ipileup) {
181  float hisdr2 = std::max<float>(reco::deltaR2(**ipileup, **ineutral), 0.01f);
182  if (hisdr2 > weightCone2) continue;
183  if (std::find(vetos_.begin(), vetos_.end(), *ipileup) != vetos_.end()) {
184  continue;
185  }
186  sumc += std::log( (*ipileup)->pt() / std::sqrt(hisdr2) );
187  }
188  w = (sump == 0 ? 1 : sumc/(sump+sumc));
189  }
190  // add to sum
191  isosum += w * (*ineutral)->pt();
192  }
193  return isosum;
194 }
float isoSumNeutralsWeighted(const reco::Candidate &cand, float dR, float innerR, float threshold, SelfVetoPolicy selfVeto, int pdgId=-1) const
const double w
Definition: UKUtility.cc:23
virtual CandidatePtr sourceCandidatePtr(size_type i) const
Definition: Candidate.h:170
void clearVetos()
clear all vetos
bool isAvailable() const
Definition: Ptr.h:258
void addVetos(const reco::Candidate &cand)
veto footprint from this candidate, for the isolation of all candidates and also for calculation of n...
float neutralAbsIsoWeighted(const reco::Candidate &cand, float dR, float innerR=0, float threshold=0, SelfVetoPolicy selfVeto=selfVetoAll) const
Isolation from all neutrals (with weights)
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:20
virtual size_t numberOfSourceCandidatePtrs() const =0
float neutralHadAbsIsoWeighted(const reco::Candidate &cand, float dR, float innerR=0, float threshold=0, SelfVetoPolicy selfVeto=selfVetoAll) const
Isolation from neutral hadrons (with weights)
float isoSumRaw(const std::vector< const pat::PackedCandidate * > &cands, const reco::Candidate &cand, float dR, float innerR, float threshold, SelfVetoPolicy selfVeto, int pdgId=-1) const
T sqrt(T t)
Definition: SSEVec.h:18
float chargedAbsIso(const reco::Candidate &cand, float dR, float innerR=0, float threshold=0, SelfVetoPolicy selfVeto=selfVetoAll) const
Isolation from charged from the PV.
SelfVetoPolicy
Self-veto policy.
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
double f[11][100]
std::vector< LinkConnSpec >::const_iterator IT
bool isNonnull() const
Checks for non-null.
Definition: Ptr.h:168
float neutralHadAbsIsoRaw(const reco::Candidate &cand, float dR, float innerR=0, float threshold=0, SelfVetoPolicy selfVeto=selfVetoAll) const
Isolation from neutral hadrons (uncorrected)
double eta() const override
momentum pseudorapidity
constexpr auto deltaR2(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
Definition: deltaR.h:16
virtual double eta() const =0
momentum pseudorapidity
float neutralAbsIsoRaw(const reco::Candidate &cand, float dR, float innerR=0, float threshold=0, SelfVetoPolicy selfVeto=selfVetoAll) const
Isolation from all neutrals (uncorrected)
float photonAbsIsoRaw(const reco::Candidate &cand, float dR, float innerR=0, float threshold=0, SelfVetoPolicy selfVeto=selfVetoAll) const
Isolation from photons (uncorrected)
float puAbsIso(const reco::Candidate &cand, float dR, float innerR=0, float threshold=0, SelfVetoPolicy selfVeto=selfVetoAll) const
Isolation from charged from PU.
float photonAbsIsoWeighted(const reco::Candidate &cand, float dR, float innerR=0, float threshold=0, SelfVetoPolicy selfVeto=selfVetoAll) const
Isolation from photons (with weights)
void setPackedCandidates(const std::vector< pat::PackedCandidate > &all, int fromPV_thresh=1, float dz_thresh=9999., float dxy_thresh=9999., bool also_leptons=false)
Initialize with the list of packed candidates (note: clears also all vetos)