CMS 3D CMS Logo

PATDiObjectProxy.h
Go to the documentation of this file.
1 #ifndef PhysicsTools_PatUtils_interface_PATDiObjectProxy_h
2 #define PhysicsTools_PatUtils_interface_PATDiObjectProxy_h
3 
6 
15 
16 namespace pat {
17 
18  /* Now we implement PATDiObjectProxy with typeid & static_casts on the fly. */
19  class DiObjectProxy {
20  public:
23  : cand1_(nullptr), cand2_(nullptr), type1_(nullptr), type2_(nullptr), totalP4ok_(false), totalP4_() {}
27  : cand1_(&c1), cand2_(&c2), type1_(&typeid(c1)), type2_(&typeid(c2)), totalP4ok_(false), totalP4_() {}
28 
30  const reco::Candidate &cand1() const { return *cand1_; }
32  const reco::Candidate &cand2() const { return *cand2_; }
33 
35  double deltaR() const { return ::deltaR(*cand1_, *cand2_); }
37  double deltaPhi() const { return ::deltaPhi(cand1_->phi(), cand2_->phi()); }
38 
40  // Implementation notice: return by const reference, not by value,
41  // as it's easier for Reflex.
43  if (!totalP4ok_) {
44  totalP4_ = cand1_->p4() + cand2_->p4();
45  totalP4ok_ = true;
46  }
47  return totalP4_;
48  }
49 
51  const Electron &ele() const { return tryGetOne_<Electron>(); }
53  const Muon &mu() const { return tryGetOne_<Muon>(); }
55  const Tau &tau() const { return tryGetOne_<Tau>(); }
57  const Photon &gam() const { return tryGetOne_<Photon>(); }
59  const Jet &jet() const { return tryGetOne_<Jet>(); }
61  const MET &met() const { return tryGetOne_<MET>(); }
63  const GenericParticle &part() const { return tryGetOne_<GenericParticle>(); }
65  const PFParticle &pf() const { return tryGetOne_<PFParticle>(); }
66 
68  const Electron &ele1() const { return tryGet_<Electron>(cand1_, type1_); }
70  const Muon &mu1() const { return tryGet_<Muon>(cand1_, type1_); }
72  const Tau &tau1() const { return tryGet_<Tau>(cand1_, type1_); }
74  const Photon &gam1() const { return tryGet_<Photon>(cand1_, type1_); }
76  const Jet &jet1() const { return tryGet_<Jet>(cand1_, type1_); }
78  const MET &met1() const { return tryGet_<MET>(cand1_, type1_); }
80  const GenericParticle &part1() const { return tryGet_<GenericParticle>(cand1_, type1_); }
82  const PFParticle &pf1() const { return tryGet_<PFParticle>(cand1_, type1_); }
83 
85  const Electron &ele2() const { return tryGet_<Electron>(cand2_, type2_); }
87  const Muon &mu2() const { return tryGet_<Muon>(cand2_, type2_); }
89  const Tau &tau2() const { return tryGet_<Tau>(cand2_, type2_); }
91  const Photon &gam2() const { return tryGet_<Photon>(cand2_, type2_); }
93  const Jet &jet2() const { return tryGet_<Jet>(cand2_, type2_); }
95  const MET &met2() const { return tryGet_<MET>(cand2_, type2_); }
97  const GenericParticle &part2() const { return tryGet_<GenericParticle>(cand2_, type2_); }
99  const PFParticle &pf2() const { return tryGet_<PFParticle>(cand2_, type2_); }
100 
101  private:
102  template <typename T>
103  const T &tryGet_(const reco::Candidate *ptr, const std::type_info *type) const {
104  if (typeid(T) != *type) {
105  throw cms::Exception("Type Error")
106  << "pat::DiObjectProxy: the object of the pair is not of the type you request.\n"
107  << " Item Index in pair: " << (ptr == cand1_ ? "first" : "second") << "\n"
108  << " Requested TypeID : " << ClassName<T>::name() << "\n"
109  << " Found TypeID : " << className(*ptr) << "\n";
110  }
111  return static_cast<const T &>(*ptr);
112  }
113 
114  template <typename T>
115  const T &tryGetOne_() const {
116  if (typeid(T) == *type1_) {
117  if (typeid(T) == *type2_) {
118  throw cms::Exception("Type Error")
119  << "pat::DiObjectProxy: "
120  << "you can't get use methods that get a particle by type if the two are of the same type!\n"
121  << " Requested Type:" << ClassName<T>::name() << "\n";
122  }
123  return static_cast<const T &>(*cand1_);
124  } else {
125  if (typeid(T) != *type2_) {
126  throw cms::Exception("Type Error")
127  << "pat::DiObjectProxy: "
128  << "you can't get use methods that get a particle by type if neither of the two is of that type!\n"
129  << " Requested Type:" << ClassName<T>::name() << "\n"
130  << " Type of first :" << className(*cand1_) << "\n"
131  << " Type of second:" << className(*cand2_) << "\n";
132  }
133  return static_cast<const T &>(*cand2_);
134  }
135  }
136 
138  const std::type_info *type1_, *type2_;
139 
140  mutable bool totalP4ok_;
142  };
143 
144 } // namespace pat
145 #endif
pat::DiObjectProxy::ele
const Electron & ele() const
Get the PAT Electron, if the pair contains one and only one PAT Electron (throw exception otherwise)
Definition: PATDiObjectProxy.h:51
pat::DiObjectProxy::met2
const MET & met2() const
Get the second item, if it's a PAT MET (throw exception otherwise)
Definition: PATDiObjectProxy.h:95
pat::DiObjectProxy::cand2
const reco::Candidate & cand2() const
Gets the second Candidate.
Definition: PATDiObjectProxy.h:32
pat::DiObjectProxy::gam1
const Photon & gam1() const
Get the first item, if it's a PAT Photon (throw exception otherwise)
Definition: PATDiObjectProxy.h:74
funct::false
false
Definition: Factorize.h:34
pat::DiObjectProxy::mu1
const Muon & mu1() const
Get the first item, if it's a PAT Muon (throw exception otherwise)
Definition: PATDiObjectProxy.h:70
pat::DiObjectProxy::type1_
const std::type_info * type1_
Definition: PATDiObjectProxy.h:138
pat::DiObjectProxy::cand1_
const reco::Candidate * cand1_
Definition: PATDiObjectProxy.h:137
pat::DiObjectProxy::mu2
const Muon & mu2() const
Get the second item, if it's a PAT Muon (throw exception otherwise)
Definition: PATDiObjectProxy.h:87
pat::DiObjectProxy::part
const GenericParticle & part() const
Get the PAT GenericParticle, if the pair contains one and only one PAT GenericParticle (throw excepti...
Definition: PATDiObjectProxy.h:63
pat::DiObjectProxy::pf
const PFParticle & pf() const
Get the PAT PFParticle, if the pair contains one and only one PAT PFParticle (throw exception otherwi...
Definition: PATDiObjectProxy.h:65
Electron
Definition: Electron.py:1
Muon.h
Photon.h
GenericParticle.h
pat::DiObjectProxy::DiObjectProxy
DiObjectProxy()
Default constructor, requested by ROOT. NEVER use a default constructed item!
Definition: PATDiObjectProxy.h:22
pat::DiObjectProxy::cand1
const reco::Candidate & cand1() const
Gets the first Candidate.
Definition: PATDiObjectProxy.h:30
pat::DiObjectProxy::mu
const Muon & mu() const
Get the PAT Muon, if the pair contains one and only one PAT Muon (throw exception otherwise)
Definition: PATDiObjectProxy.h:53
pat::DiObjectProxy::met
const MET & met() const
Get the PAT MET, if the pair contains one and only one PAT MET (throw exception otherwise)
Definition: PATDiObjectProxy.h:61
pat::DiObjectProxy::deltaR
double deltaR() const
Get the angular separation.
Definition: PATDiObjectProxy.h:35
Tau
Definition: Tau.py:1
pat::DiObjectProxy::tau
const Tau & tau() const
Get the PAT Tau, if the pair contains one and only one PAT Tau (throw exception otherwise)
Definition: PATDiObjectProxy.h:55
pat::PFParticle
Analysis-level class for reconstructed particles.
Definition: PFParticle.h:32
Tau.h
Muon
Definition: Muon.py:1
deltaR.h
pat::DiObjectProxy::pf1
const PFParticle & pf1() const
Get the first item, if it's a PAT PFParticle (throw exception otherwise)
Definition: PATDiObjectProxy.h:82
Jet
Definition: Jet.py:1
SiPixelRawToDigiRegional_cfi.deltaPhi
deltaPhi
Definition: SiPixelRawToDigiRegional_cfi.py:9
pat::DiObjectProxy::totalP4ok_
bool totalP4ok_
Definition: PATDiObjectProxy.h:140
ClassName::name
static const std::string & name()
Definition: ClassName.h:38
pat::DiObjectProxy::pf2
const PFParticle & pf2() const
Get the second item, if it's a PAT PFParticle (throw exception otherwise)
Definition: PATDiObjectProxy.h:99
pat::DiObjectProxy::gam2
const Photon & gam2() const
Get the second item, if it's a PAT Photon (throw exception otherwise)
Definition: PATDiObjectProxy.h:91
pat::DiObjectProxy::ele1
const Electron & ele1() const
Get the first item, if it's a PAT Electron (throw exception otherwise)
Definition: PATDiObjectProxy.h:68
PbPb_ZMuSkimMuonDPG_cff.deltaR
deltaR
Definition: PbPb_ZMuSkimMuonDPG_cff.py:63
ClassName
Definition: ClassName.h:36
pat::GenericParticle
Analysis-level Generic Particle class (e.g. for hadron or muon not fully reconstructed)
Definition: GenericParticle.h:38
pat::DiObjectProxy::tryGet_
const T & tryGet_(const reco::Candidate *ptr, const std::type_info *type) const
Definition: PATDiObjectProxy.h:103
pat::DiObjectProxy::jet
const Jet & jet() const
Get the PAT Jet, if the pair contains one and only one PAT Jet (throw exception otherwise)
Definition: PATDiObjectProxy.h:59
pat::DiObjectProxy::tau2
const Tau & tau2() const
Get the second item, if it's a PAT Tau (throw exception otherwise)
Definition: PATDiObjectProxy.h:89
Photon
Definition: Photon.py:1
pat::DiObjectProxy::gam
const Photon & gam() const
Get the PAT Photon, if the pair contains one and only one PAT Photon (throw exception otherwise)
Definition: PATDiObjectProxy.h:57
pat::DiObjectProxy::DiObjectProxy
DiObjectProxy(const reco::Candidate &c1, const reco::Candidate &c2)
Definition: PATDiObjectProxy.h:26
alignmentValidation.c1
c1
do drawing
Definition: alignmentValidation.py:1025
pat::DiObjectProxy::part1
const GenericParticle & part1() const
Get the first item, if it's a PAT GenericParticle (throw exception otherwise)
Definition: PATDiObjectProxy.h:80
pat::MET
Analysis-level MET class.
Definition: MET.h:40
pat::DiObjectProxy::jet1
const Jet & jet1() const
Get the first item, if it's a PAT Jet (throw exception otherwise)
Definition: PATDiObjectProxy.h:76
pat
Definition: HeavyIon.h:7
PFParticle.h
pat::DiObjectProxy::part2
const GenericParticle & part2() const
Get the second item, if it's a PAT GenericParticle (throw exception otherwise)
Definition: PATDiObjectProxy.h:97
MET.h
pat::DiObjectProxy::type2_
const std::type_info * type2_
Definition: PATDiObjectProxy.h:138
Jet.h
reco::Candidate
Definition: Candidate.h:27
pat::DiObjectProxy::totalP4
const reco::Candidate::LorentzVector & totalP4() const
Get the total four momentum.
Definition: PATDiObjectProxy.h:42
pat::DiObjectProxy::met1
const MET & met1() const
Get the first item, if it's a PAT MET (throw exception otherwise)
Definition: PATDiObjectProxy.h:78
type
type
Definition: HCALResponse.h:21
pat::DiObjectProxy::ele2
const Electron & ele2() const
Get the second item, if it's a PAT Electron (throw exception otherwise)
Definition: PATDiObjectProxy.h:85
T
long double T
Definition: Basic3DVectorLD.h:48
Exception
Definition: hltDiff.cc:246
ClassName.h
pat::DiObjectProxy::tryGetOne_
const T & tryGetOne_() const
Definition: PATDiObjectProxy.h:115
reco::Candidate::p4
virtual const LorentzVector & p4() const =0
four-momentum Lorentz vector
pat::DiObjectProxy::totalP4_
reco::Candidate::LorentzVector totalP4_
Definition: PATDiObjectProxy.h:141
Electron.h
className
std::string className(const T &t)
Definition: ClassName.h:31
reco::Candidate::LorentzVector
math::XYZTLorentzVector LorentzVector
Lorentz vector.
Definition: Candidate.h:36
pat::DiObjectProxy::tau1
const Tau & tau1() const
Get the first item, if it's a PAT Tau (throw exception otherwise)
Definition: PATDiObjectProxy.h:72
pat::DiObjectProxy::jet2
const Jet & jet2() const
Get the second item, if it's a PAT Jet (throw exception otherwise)
Definition: PATDiObjectProxy.h:93
pat::DiObjectProxy
Definition: PATDiObjectProxy.h:19
reco::Candidate::phi
virtual double phi() const =0
momentum azimuthal angle
pat::DiObjectProxy::cand2_
const reco::Candidate * cand2_
Definition: PATDiObjectProxy.h:137
pat::DiObjectProxy::deltaPhi
double deltaPhi() const
Get the phi separation.
Definition: PATDiObjectProxy.h:37