AbsVeto

CountAlgo

IsoDeposit

MaxAlgo

MeanDRAlgo

Sum2Algo

SumAlgo

SumDRAlgo

Veto

const_iterator

Macros

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
#ifndef RecoCandidate_IsoDeposit_H
#define RecoCandidate_IsoDeposit_H

/** \class IsoDeposit
 *  Class representing the dR profile of deposits around a muon, i.e.
 *  the differential deposits around the muon as a function of dR.
 *  
 *  Each instance should describe deposits of homogeneous type (e.g. ECAL,
 *  HCAL...); it carries information about
 *  the cone axis, the muon pT.
 *
 *  \author N. Amapane - M. Konecki
 *  Ported with an alternative interface to CMSSW by J. Alcaraz
 *  AbsVetos added by G. Petrucciani
 *  Moved to RecoCandidate  by S. Krutelyov
 *  Adding more generic Algos  and radial Algorithms by M.Bachtis
 */

#include "DataFormats/RecoCandidate/interface/IsoDepositDirection.h"
#include "DataFormats/Math/interface/Vector3D.h"
#include "FWCore/Utilities/interface/Exception.h"
#include "FWCore/Utilities/interface/thread_safety_macros.h"
#include <map>
#include <cmath>
#include <string>
#include <vector>
#include <typeinfo>
#include <atomic>

namespace reco {
  namespace isodeposit {
    struct AbsVeto {
      virtual ~AbsVeto() {}
      //! Return "true" if a deposit at specific (eta,phi) with that value must be vetoed in the sum
      virtual bool veto(double eta, double phi, float value) const = 0;
      /** Relocates this veto so that the new center is at some (eta,phi).
	  Must be implemented on the specific AbsVeto subclass: in this mother class it just throws exception */
      virtual void centerOn(double eta, double phi) {
        throw cms::Exception("Not Implemented") << "This AbsVeto implementation (" << typeid(this).name()
                                                << ") does not support the centerOn(eta,phi) method";
      }
    };
    typedef std::vector<AbsVeto*> AbsVetos;
  }  // namespace isodeposit
}  // namespace reco

namespace reco {

  class IsoDeposit {
  public:
    typedef isodeposit::Direction Direction;
    typedef isodeposit::AbsVeto AbsVeto;
    typedef isodeposit::AbsVetos AbsVetos;
    typedef Direction::Distance Distance;
    typedef std::multimap<Distance, float> DepositsMultimap;
    typedef DepositsMultimap::const_iterator DepIterator;

    // old style vetos
    struct Veto {
      Direction vetoDir;
      float dR;
      Veto() {}
      Veto(Direction dir, double d) : vetoDir(dir), dR(d) {}
    };
    typedef std::vector<Veto> Vetos;

    //! Constructor
    IsoDeposit(double eta = 0, double phi = 0);
    IsoDeposit(const Direction& candDirection);

    //! Destructor
    virtual ~IsoDeposit() {}

    //! Get direction of isolation cone
    const Direction& direction() const { return theDirection; }
    double eta() const { return theDirection.eta(); }
    double phi() const { return theDirection.phi(); }

    //! Get veto area
    const Veto& veto() const { return theVeto; }
    //! Set veto
    void setVeto(const Veto& aVeto) { theVeto = aVeto; }

    //! Add deposit (ie. transverse energy or pT)
    void addDeposit(double dr, double deposit);  // FIXME - temporary for backward compatibility
    void addDeposit(const Direction& depDir, double deposit);

    //! Get deposit
    double depositWithin(double coneSize,               //dR in which deposit is computed
                         const Vetos& vetos = Vetos(),  //additional vetos
                         bool skipDepositVeto = false   //skip exclusion of veto
    ) const;

    //! Get deposit wrt other direction
    double depositWithin(Direction dir,
                         double coneSize,               //dR in which deposit is computed
                         const Vetos& vetos = Vetos(),  //additional vetos
                         bool skipDepositVeto = false   //skip exclusion of veto
    ) const;

    //! Get deposit
    std::pair<double, int> depositAndCountWithin(double coneSize,               //dR in which deposit is computed
                                                 const Vetos& vetos = Vetos(),  //additional vetos
                                                 double threshold = -1e+36,     //threshold on counted deposits
                                                 bool skipDepositVeto = false   //skip exclusion of veto
    ) const;

    //! Get deposit wrt other direction
    std::pair<double, int> depositAndCountWithin(Direction dir,                 //wrt another direction
                                                 double coneSize,               //dR in which deposit is computed
                                                 const Vetos& vetos = Vetos(),  //additional vetos
                                                 double threshold = -1e+36,     //threshold on deposits
                                                 bool skipDepositVeto = false   //skip exclusion of veto
    ) const;

    //! Get deposit with new style vetos
    double depositWithin(double coneSize,              //dR in which deposit is computed
                         const AbsVetos& vetos,        //additional vetos
                         bool skipDepositVeto = false  //skip exclusion of veto
    ) const;

    //! Get deposit
    std::pair<double, int> depositAndCountWithin(double coneSize,              //dR in which deposit is computed
                                                 const AbsVetos& vetos,        //additional vetos
                                                 bool skipDepositVeto = false  //skip exclusion of veto
    ) const;

    //! Get energy or pT attached to cand trajectory
    double candEnergy() const { return theCandTag; }

    //! Set energy or pT attached to cand trajectory
    void addCandEnergy(double et) { theCandTag += et; }

    std::string print() const;

    class const_iterator {
    public:
      const const_iterator& operator++() {
        ++it_;
        cacheReady_ = false;
        return *this;
      }
      const const_iterator* operator->() const { return this; }
      float dR() const { return it_->first.deltaR; }
      float eta() const {
        if (!cacheReady_)
          doDir();
        return cache_.eta();
      }
      float phi() const {
        if (!cacheReady_)
          doDir();
        return cache_.phi();
      }
      float value() const { return it_->second; }
      bool operator!=(const const_iterator& it2) { return it2.it_ != it_; }
      friend class IsoDeposit;

    private:
      typedef Direction::Distance Distance;
      void doDir() const {
        if (!cacheReady_)
          cache_ = parent_->direction() + it_->first;
        cacheReady_ = true;
      }
      const_iterator(const IsoDeposit* parent, std::multimap<Distance, float>::const_iterator it)
          : parent_(parent), it_(it), cache_(), cacheReady_(false) {}
      const reco::IsoDeposit* parent_;
      std::multimap<Distance, float>::const_iterator it_;
      CMS_THREAD_SAFE mutable Direction cache_;
      mutable std::atomic<bool> cacheReady_;
    };
    const_iterator begin() const { return const_iterator(this, theDeposits.begin()); }
    const_iterator end() const { return const_iterator(this, theDeposits.end()); }

    class SumAlgo {
    public:
      SumAlgo() : sum_(0) {}
      void operator+=(DepIterator deposit) { sum_ += deposit->second; }
      void operator+=(double deposit) { sum_ += deposit; }
      double result() const { return sum_; }

    private:
      double sum_;
    };
    class CountAlgo {
    public:
      CountAlgo() : count_(0) {}
      void operator+=(DepIterator deposit) { count_++; }
      void operator+=(double deposit) { count_++; }
      double result() const { return count_; }

    private:
      size_t count_;
    };
    class Sum2Algo {
    public:
      Sum2Algo() : sum2_(0) {}
      void operator+=(DepIterator deposit) { sum2_ += deposit->second * deposit->second; }
      void operator+=(double deposit) { sum2_ += deposit * deposit; }
      double result() const { return sum2_; }

    private:
      double sum2_;
    };
    class MaxAlgo {
    public:
      MaxAlgo() : max_(0) {}
      void operator+=(DepIterator deposit) {
        if (deposit->second > max_)
          max_ = deposit->second;
      }
      void operator+=(double deposit) {
        if (deposit > max_)
          max_ = deposit;
      }
      double result() const { return max_; }

    private:
      double max_;
    };

    class MeanDRAlgo {
    public:
      MeanDRAlgo() : sum_(0.), count_(0.) {}
      void operator+=(DepIterator deposit) {
        sum_ += deposit->first.deltaR;
        count_ += 1.0;
      }
      double result() const { return sum_ / std::max(1., count_); }

    private:
      double sum_;
      double count_;
    };

    class SumDRAlgo {
    public:
      SumDRAlgo() : sum_(0.) {}
      void operator+=(DepIterator deposit) { sum_ += deposit->first.deltaR; }
      double result() const { return sum_; }

    private:
      double sum_;
    };

    //! Get some info about the deposit (e.g. sum, max, sum2, count)
    template <typename Algo>
    double algoWithin(double coneSize,                     //dR in which deposit is computed
                      const AbsVetos& vetos = AbsVetos(),  //additional vetos
                      bool skipDepositVeto = false         //skip exclusion of veto
    ) const;
    //! Get some info about the deposit (e.g. sum, max, sum2, count) w.r.t. other direction
    template <typename Algo>
    double algoWithin(const Direction&,
                      double coneSize,                     //dR in which deposit is computed
                      const AbsVetos& vetos = AbsVetos(),  //additional vetos
                      bool skipDepositVeto = false         //skip exclusion of veto
    ) const;
    // count of the non-vetoed deposits in the cone
    double countWithin(double coneSize,                     //dR in which deposit is computed
                       const AbsVetos& vetos = AbsVetos(),  //additional vetos
                       bool skipDepositVeto = false         //skip exclusion of veto
    ) const;
    // sum of the non-vetoed deposits in the cone
    double sumWithin(double coneSize,                     //dR in which deposit is computed
                     const AbsVetos& vetos = AbsVetos(),  //additional vetos
                     bool skipDepositVeto = false         //skip exclusion of veto
    ) const;
    // sum of the non-vetoed deposits in the cone w.r.t. other direction
    double sumWithin(const Direction& dir,
                     double coneSize,                      //dR in which deposit is computed
                     const AbsVetos& vetos = AbsVetos(),   //additional vetos
                     bool skipDepositVeto = false          //skip exclusion of veto
    ) const;                                               // sum of the squares of the non-vetoed deposits in the cone
    double sum2Within(double coneSize,                     //dR in which deposit is computed
                      const AbsVetos& vetos = AbsVetos(),  //additional vetos
                      bool skipDepositVeto = false         //skip exclusion of veto
    ) const;
    // maximum value among the non-vetoed deposits in the cone
    double maxWithin(double coneSize,                     //dR in which deposit is computed
                     const AbsVetos& vetos = AbsVetos(),  //additional vetos
                     bool skipDepositVeto = false         //skip exclusion of veto
    ) const;
    // maximum value among the non-vetoed deposits in the cone
    double nearestDR(double coneSize,                     //dR in which deposit is computed
                     const AbsVetos& vetos = AbsVetos(),  //additional vetos
                     bool skipDepositVeto = false         //skip exclusion of veto
    ) const;

  private:
    //! direcion of deposit (center of isolation cone)
    Direction theDirection;

    //! area to be excluded in computaion of depositWithin
    Veto theVeto;

    //! float tagging cand, ment to be transverse energy or pT attached to cand,
    float theCandTag;

    //! the deposits identifed by relative position to center of cone and deposit value

    DepositsMultimap theDeposits;
  };

}  // namespace reco

template <typename Algo>
double reco::IsoDeposit::algoWithin(double coneSize, const AbsVetos& vetos, bool skipDepositVeto) const {
  using namespace reco::isodeposit;
  Algo algo;
  typedef AbsVetos::const_iterator IV;
  IV ivEnd = vetos.end();

  Distance maxDistance = {float(coneSize), 999.f};
  typedef DepositsMultimap::const_iterator IM;
  IM imLoc = theDeposits.upper_bound(maxDistance);
  for (IM im = theDeposits.begin(); im != imLoc; ++im) {
    bool vetoed = false;
    Direction dirDep = theDirection + im->first;
    for (IV iv = vetos.begin(); iv < ivEnd; ++iv) {
      if ((*iv)->veto(dirDep.eta(), dirDep.phi(), im->second)) {
        vetoed = true;
        break;
      }
    }
    if (!vetoed) {
      if (skipDepositVeto || (dirDep.deltaR(theVeto.vetoDir) > theVeto.dR)) {
        algo += im;
      }
    }
  }
  return algo.result();
}

template <typename Algo>
double reco::IsoDeposit::algoWithin(const Direction& dir,
                                    double coneSize,
                                    const AbsVetos& vetos,
                                    bool skipDepositVeto) const {
  using namespace reco::isodeposit;
  Algo algo;
  typedef AbsVetos::const_iterator IV;
  IV ivEnd = vetos.end();
  typedef DepositsMultimap::const_iterator IM;
  IM imLoc = theDeposits.end();
  for (IM im = theDeposits.begin(); im != imLoc; ++im) {
    bool vetoed = false;
    Direction dirDep = theDirection + im->first;
    Distance newDist = dirDep - dir;
    if (newDist.deltaR > coneSize)
      continue;
    for (IV iv = vetos.begin(); iv < ivEnd; ++iv) {
      if ((*iv)->veto(dirDep.eta(), dirDep.phi(), im->second)) {
        vetoed = true;
        break;
      }
    }
    if (!vetoed) {
      if (skipDepositVeto || (dirDep.deltaR(theVeto.vetoDir) > theVeto.dR)) {
        algo += im;
      }
    }
  }
  return algo.result();
}

#endif