CMS 3D CMS Logo

IsoDeposit.h
Go to the documentation of this file.
1 #ifndef RecoCandidate_IsoDeposit_H
2 #define RecoCandidate_IsoDeposit_H
3 
23 #include <map>
24 #include <cmath>
25 #include <string>
26 #include <vector>
27 #include <typeinfo>
28 #include <atomic>
29 
30 namespace reco {
31  namespace isodeposit {
32  struct AbsVeto {
33  virtual ~AbsVeto() {}
35  virtual bool veto(double eta, double phi, float value) const = 0;
38  virtual void centerOn(double eta, double phi) {
39  throw cms::Exception("Not Implemented") << "This AbsVeto implementation (" << typeid(this).name()
40  << ") does not support the centerOn(eta,phi) method";
41  }
42  };
43  typedef std::vector<AbsVeto*> AbsVetos;
44  } // namespace isodeposit
45 } // namespace reco
46 
47 namespace reco {
48 
49  class IsoDeposit {
50  public:
55  typedef std::multimap<Distance, float> DepositsMultimap;
56  typedef DepositsMultimap::const_iterator DepIterator;
57 
58  // old style vetos
59  struct Veto {
61  float dR;
62  Veto() {}
63  Veto(Direction dir, double d) : vetoDir(dir), dR(d) {}
64  };
65  typedef std::vector<Veto> Vetos;
66 
68  IsoDeposit(double eta = 0, double phi = 0);
69  IsoDeposit(const Direction& candDirection);
70 
72  virtual ~IsoDeposit(){};
73 
75  const Direction& direction() const { return theDirection; }
76  double eta() const { return theDirection.eta(); }
77  double phi() const { return theDirection.phi(); }
78 
80  const Veto& veto() const { return theVeto; }
82  void setVeto(const Veto& aVeto) { theVeto = aVeto; }
83 
85  void addDeposit(double dr, double deposit); // FIXME - temporary for backward compatibility
86  void addDeposit(const Direction& depDir, double deposit);
87 
89  double depositWithin(double coneSize, //dR in which deposit is computed
90  const Vetos& vetos = Vetos(), //additional vetos
91  bool skipDepositVeto = false //skip exclusion of veto
92  ) const;
93 
96  double coneSize, //dR in which deposit is computed
97  const Vetos& vetos = Vetos(), //additional vetos
98  bool skipDepositVeto = false //skip exclusion of veto
99  ) const;
100 
102  std::pair<double, int> depositAndCountWithin(double coneSize, //dR in which deposit is computed
103  const Vetos& vetos = Vetos(), //additional vetos
104  double threshold = -1e+36, //threshold on counted deposits
105  bool skipDepositVeto = false //skip exclusion of veto
106  ) const;
107 
109  std::pair<double, int> depositAndCountWithin(Direction dir, //wrt another direction
110  double coneSize, //dR in which deposit is computed
111  const Vetos& vetos = Vetos(), //additional vetos
112  double threshold = -1e+36, //threshold on deposits
113  bool skipDepositVeto = false //skip exclusion of veto
114  ) const;
115 
117  double depositWithin(double coneSize, //dR in which deposit is computed
118  const AbsVetos& vetos, //additional vetos
119  bool skipDepositVeto = false //skip exclusion of veto
120  ) const;
121 
123  std::pair<double, int> depositAndCountWithin(double coneSize, //dR in which deposit is computed
124  const AbsVetos& vetos, //additional vetos
125  bool skipDepositVeto = false //skip exclusion of veto
126  ) const;
127 
129  double candEnergy() const { return theCandTag; }
130 
132  void addCandEnergy(double et) { theCandTag += et; }
133 
134  std::string print() const;
135 
137  public:
139  ++it_;
140  cacheReady_ = false;
141  return *this;
142  }
143  const const_iterator* operator->() const { return this; }
144  float dR() const { return it_->first.deltaR; }
145  float eta() const {
146  if (!cacheReady_)
147  doDir();
148  return cache_.eta();
149  }
150  float phi() const {
151  if (!cacheReady_)
152  doDir();
153  return cache_.phi();
154  }
155  float value() const { return it_->second; }
156  bool operator!=(const const_iterator& it2) { return it2.it_ != it_; }
157  friend class IsoDeposit;
158 
159  private:
161  void doDir() const {
162  if (!cacheReady_)
163  cache_ = parent_->direction() + it_->first;
164  cacheReady_ = true;
165  }
166  const_iterator(const IsoDeposit* parent, std::multimap<Distance, float>::const_iterator it)
169  std::multimap<Distance, float>::const_iterator it_;
171  mutable std::atomic<bool> cacheReady_;
172  };
173  const_iterator begin() const { return const_iterator(this, theDeposits.begin()); }
174  const_iterator end() const { return const_iterator(this, theDeposits.end()); }
175 
176  class SumAlgo {
177  public:
178  SumAlgo() : sum_(0) {}
179  void operator+=(DepIterator deposit) { sum_ += deposit->second; }
180  void operator+=(double deposit) { sum_ += deposit; }
181  double result() const { return sum_; }
182 
183  private:
184  double sum_;
185  };
186  class CountAlgo {
187  public:
188  CountAlgo() : count_(0) {}
189  void operator+=(DepIterator deposit) { count_++; }
190  void operator+=(double deposit) { count_++; }
191  double result() const { return count_; }
192 
193  private:
194  size_t count_;
195  };
196  class Sum2Algo {
197  public:
198  Sum2Algo() : sum2_(0) {}
199  void operator+=(DepIterator deposit) { sum2_ += deposit->second * deposit->second; }
200  void operator+=(double deposit) { sum2_ += deposit * deposit; }
201  double result() const { return sum2_; }
202 
203  private:
204  double sum2_;
205  };
206  class MaxAlgo {
207  public:
208  MaxAlgo() : max_(0) {}
209  void operator+=(DepIterator deposit) {
210  if (deposit->second > max_)
211  max_ = deposit->second;
212  }
213  void operator+=(double deposit) {
214  if (deposit > max_)
215  max_ = deposit;
216  }
217  double result() const { return max_; }
218 
219  private:
220  double max_;
221  };
222 
223  class MeanDRAlgo {
224  public:
225  MeanDRAlgo() : sum_(0.), count_(0.) {}
226  void operator+=(DepIterator deposit) {
227  sum_ += deposit->first.deltaR;
228  count_ += 1.0;
229  }
230  double result() const { return sum_ / std::max(1., count_); }
231 
232  private:
233  double sum_;
234  double count_;
235  };
236 
237  class SumDRAlgo {
238  public:
239  SumDRAlgo() : sum_(0.) {}
240  void operator+=(DepIterator deposit) { sum_ += deposit->first.deltaR; }
241  double result() const { return sum_; }
242 
243  private:
244  double sum_;
245  };
246 
248  template <typename Algo>
249  double algoWithin(double coneSize, //dR in which deposit is computed
250  const AbsVetos& vetos = AbsVetos(), //additional vetos
251  bool skipDepositVeto = false //skip exclusion of veto
252  ) const;
254  template <typename Algo>
255  double algoWithin(const Direction&,
256  double coneSize, //dR in which deposit is computed
257  const AbsVetos& vetos = AbsVetos(), //additional vetos
258  bool skipDepositVeto = false //skip exclusion of veto
259  ) const;
260  // count of the non-vetoed deposits in the cone
261  double countWithin(double coneSize, //dR in which deposit is computed
262  const AbsVetos& vetos = AbsVetos(), //additional vetos
263  bool skipDepositVeto = false //skip exclusion of veto
264  ) const;
265  // sum of the non-vetoed deposits in the cone
266  double sumWithin(double coneSize, //dR in which deposit is computed
267  const AbsVetos& vetos = AbsVetos(), //additional vetos
268  bool skipDepositVeto = false //skip exclusion of veto
269  ) const;
270  // sum of the non-vetoed deposits in the cone w.r.t. other direction
271  double sumWithin(const Direction& dir,
272  double coneSize, //dR in which deposit is computed
273  const AbsVetos& vetos = AbsVetos(), //additional vetos
274  bool skipDepositVeto = false //skip exclusion of veto
275  ) const; // sum of the squares of the non-vetoed deposits in the cone
276  double sum2Within(double coneSize, //dR in which deposit is computed
277  const AbsVetos& vetos = AbsVetos(), //additional vetos
278  bool skipDepositVeto = false //skip exclusion of veto
279  ) const;
280  // maximum value among the non-vetoed deposits in the cone
281  double maxWithin(double coneSize, //dR in which deposit is computed
282  const AbsVetos& vetos = AbsVetos(), //additional vetos
283  bool skipDepositVeto = false //skip exclusion of veto
284  ) const;
285  // maximum value among the non-vetoed deposits in the cone
286  double nearestDR(double coneSize, //dR in which deposit is computed
287  const AbsVetos& vetos = AbsVetos(), //additional vetos
288  bool skipDepositVeto = false //skip exclusion of veto
289  ) const;
290 
291  private:
294 
297 
299  float theCandTag;
300 
302 
304  };
305 
306 } // namespace reco
307 
308 template <typename Algo>
309 double reco::IsoDeposit::algoWithin(double coneSize, const AbsVetos& vetos, bool skipDepositVeto) const {
310  using namespace reco::isodeposit;
311  Algo algo;
312  typedef AbsVetos::const_iterator IV;
313  IV ivEnd = vetos.end();
314 
315  Distance maxDistance = {float(coneSize), 999.f};
316  typedef DepositsMultimap::const_iterator IM;
317  IM imLoc = theDeposits.upper_bound(maxDistance);
318  for (IM im = theDeposits.begin(); im != imLoc; ++im) {
319  bool vetoed = false;
320  Direction dirDep = theDirection + im->first;
321  for (IV iv = vetos.begin(); iv < ivEnd; ++iv) {
322  if ((*iv)->veto(dirDep.eta(), dirDep.phi(), im->second)) {
323  vetoed = true;
324  break;
325  }
326  }
327  if (!vetoed) {
328  if (skipDepositVeto || (dirDep.deltaR(theVeto.vetoDir) > theVeto.dR)) {
329  algo += im;
330  }
331  }
332  }
333  return algo.result();
334 }
335 
336 template <typename Algo>
338  double coneSize,
339  const AbsVetos& vetos,
340  bool skipDepositVeto) const {
341  using namespace reco::isodeposit;
342  Algo algo;
343  typedef AbsVetos::const_iterator IV;
344  IV ivEnd = vetos.end();
345  typedef DepositsMultimap::const_iterator IM;
346  IM imLoc = theDeposits.end();
347  for (IM im = theDeposits.begin(); im != imLoc; ++im) {
348  bool vetoed = false;
349  Direction dirDep = theDirection + im->first;
350  Distance newDist = dirDep - dir;
351  if (newDist.deltaR > coneSize)
352  continue;
353  for (IV iv = vetos.begin(); iv < ivEnd; ++iv) {
354  if ((*iv)->veto(dirDep.eta(), dirDep.phi(), im->second)) {
355  vetoed = true;
356  break;
357  }
358  }
359  if (!vetoed) {
360  if (skipDepositVeto || (dirDep.deltaR(theVeto.vetoDir) > theVeto.dR)) {
361  algo += im;
362  }
363  }
364  }
365  return algo.result();
366 }
367 
368 #endif
virtual void centerOn(double eta, double phi)
Definition: IsoDeposit.h:38
double sumWithin(double coneSize, const AbsVetos &vetos=AbsVetos(), bool skipDepositVeto=false) const
Definition: IsoDeposit.cc:137
double phi() const
Definition: IsoDeposit.h:77
void operator+=(double deposit)
Definition: IsoDeposit.h:180
virtual bool veto(double eta, double phi, float value) const =0
Return "true" if a deposit at specific (eta,phi) with that value must be vetoed in the sum...
void operator+=(DepIterator deposit)
Definition: IsoDeposit.h:189
DepositsMultimap::const_iterator DepIterator
Definition: IsoDeposit.h:56
void setVeto(const Veto &aVeto)
Set veto.
Definition: IsoDeposit.h:82
const_iterator begin() const
Definition: IsoDeposit.h:173
double sum2Within(double coneSize, const AbsVetos &vetos=AbsVetos(), bool skipDepositVeto=false) const
Definition: IsoDeposit.cc:143
const reco::IsoDeposit * parent_
Definition: IsoDeposit.h:168
double nearestDR(double coneSize, const AbsVetos &vetos=AbsVetos(), bool skipDepositVeto=false) const
Definition: IsoDeposit.cc:150
Direction::Distance Distance
Definition: IsoDeposit.h:54
void operator+=(DepIterator deposit)
Definition: IsoDeposit.h:179
virtual ~IsoDeposit()
Destructor.
Definition: IsoDeposit.h:72
double maxWithin(double coneSize, const AbsVetos &vetos=AbsVetos(), bool skipDepositVeto=false) const
Definition: IsoDeposit.cc:146
Veto(Direction dir, double d)
Definition: IsoDeposit.h:63
void addDeposit(double dr, double deposit)
Add deposit (ie. transverse energy or pT)
Definition: IsoDeposit.cc:19
void operator+=(double deposit)
Definition: IsoDeposit.h:190
void operator+=(DepIterator deposit)
Definition: IsoDeposit.h:240
std::atomic< bool > cacheReady_
Definition: IsoDeposit.h:171
void operator+=(DepIterator deposit)
Definition: IsoDeposit.h:226
Veto theVeto
area to be excluded in computaion of depositWithin
Definition: IsoDeposit.h:296
void operator+=(DepIterator deposit)
Definition: IsoDeposit.h:209
DepositsMultimap theDeposits
the deposits identifed by relative position to center of cone and deposit value
Definition: IsoDeposit.h:303
double candEnergy() const
Get energy or pT attached to cand trajectory.
Definition: IsoDeposit.h:129
const const_iterator & operator++()
Definition: IsoDeposit.h:138
void addCandEnergy(double et)
Set energy or pT attached to cand trajectory.
Definition: IsoDeposit.h:132
std::multimap< Distance, float > DepositsMultimap
Definition: IsoDeposit.h:55
#define CMS_THREAD_SAFE
Definition: value.py:1
const_iterator end() const
Definition: IsoDeposit.h:174
d
Definition: ztail.py:151
double eta() const
Definition: IsoDeposit.h:76
const Veto & veto() const
Get veto area.
Definition: IsoDeposit.h:80
double depositWithin(double coneSize, const Vetos &vetos=Vetos(), bool skipDepositVeto=false) const
Get deposit.
Definition: IsoDeposit.cc:29
std::multimap< Distance, float >::const_iterator it_
Definition: IsoDeposit.h:169
double result() const
Definition: IsoDeposit.h:217
const_iterator(const IsoDeposit *parent, std::multimap< Distance, float >::const_iterator it)
Definition: IsoDeposit.h:166
double result() const
Definition: IsoDeposit.h:181
std::string print() const
Definition: IsoDeposit.cc:178
void operator+=(double deposit)
Definition: IsoDeposit.h:213
isodeposit::AbsVeto AbsVeto
Definition: IsoDeposit.h:52
std::vector< Veto > Vetos
Definition: IsoDeposit.h:65
const const_iterator * operator->() const
Definition: IsoDeposit.h:143
const Direction & direction() const
Get direction of isolation cone.
Definition: IsoDeposit.h:75
fixed size matrix
std::vector< AbsVeto * > AbsVetos
Definition: IsoDeposit.h:43
double deltaR(const Direction &dir2) const
void operator+=(DepIterator deposit)
Definition: IsoDeposit.h:199
isodeposit::Direction Direction
Definition: IsoDeposit.h:51
float theCandTag
float tagging cand, ment to be transverse energy or pT attached to cand,
Definition: IsoDeposit.h:299
double algoWithin(double coneSize, const AbsVetos &vetos=AbsVetos(), bool skipDepositVeto=false) const
Get some info about the deposit (e.g. sum, max, sum2, count)
Definition: IsoDeposit.h:309
Direction theDirection
direcion of deposit (center of isolation cone)
Definition: IsoDeposit.h:293
isodeposit::AbsVetos AbsVetos
Definition: IsoDeposit.h:53
bool operator!=(const const_iterator &it2)
Definition: IsoDeposit.h:156
void operator+=(double deposit)
Definition: IsoDeposit.h:200
double countWithin(double coneSize, const AbsVetos &vetos=AbsVetos(), bool skipDepositVeto=false) const
Definition: IsoDeposit.cc:134
IsoDeposit(double eta=0, double phi=0)
Constructor.
Definition: IsoDeposit.cc:14
Definition: fakeMenu.h:6
Direction::Distance Distance
Definition: IsoDeposit.h:160
std::pair< double, int > depositAndCountWithin(double coneSize, const Vetos &vetos=Vetos(), double threshold=-1e+36, bool skipDepositVeto=false) const
Get deposit.
Definition: IsoDeposit.cc:37