CMS 3D CMS Logo

PhotonPFIsolationWithMapBasedVeto.cc
Go to the documentation of this file.
3 
6 
9 
12 
14 
17 
20 
23 
26 
28 
29 
30 #include <unordered_map>
31 
32 namespace reco {
34 }
35 
36 namespace pat {
39 }
40 
41 namespace {
42  // This template function finds whether theCandidate is in thefootprint
43  // collection. It is templated to be able to handle both reco and pat
44  // photons (from AOD and miniAOD, respectively).
45  template <class T, class U>
46  bool isInFootprint(const T& thefootprint, const U& theCandidate)
47  {
48  for ( auto itr = thefootprint.begin(); itr != thefootprint.end(); ++itr )
49  {
50  if( itr->key() == theCandidate.key() ) return true;
51  }
52  return false;
53  }
54 
55  //This function is needed because pfNoPileUpCandidates have changed keys,
56  //and thus the solution is to use sourceCandidatePtr(0)
57  // This function *shouldn't be used for packedCandidate*
58  template <class T, class U>
59  bool isInFootprintAlternative(const T& thefootprint, const U& theCandidate)
60  {
61  for ( auto itr = thefootprint.begin(); itr != thefootprint.end(); ++itr )
62  {
63  if( itr->key() == theCandidate->sourceCandidatePtr(0).key() ) return true;
64  }
65  return false;
66  }
67 }
68 
70 public:
72  citk::IsolationConeDefinitionBase(c),
73  _isolateAgainst(c.getParameter<std::string>("isolateAgainst")), //isolate against either h+, h0 or gamma
74  _miniAODVertexCodes(c.getParameter<std::vector<unsigned> >("miniAODVertexCodes")), //quality flags to be used for association with the vertex configurable, the vertex can be chosen
75  _vertexIndex(c.getParameter<int> ("vertexIndex")),//vertex of interest
76  _particleBasedIsolation(c.getParameter<edm::InputTag>("particleBasedIsolation")){}
77 
80 
81  bool isInIsolationCone(const reco::CandidatePtr& photon,
82  const reco::CandidatePtr& pfCandidate) const override final;
83 
84 
85  // this object is needed for reco case
88 
89  virtual void getEventInfo(const edm::Event& iEvent) override
90  {
91  iEvent.getByToken(particleBasedIsolationToken_, particleBasedIsolationMap);
92  };
93 
94 
95  //As far as I understand now, the object particleBasedIsolationMap should be fixed, so we don't configure the name
97  {
98  particleBasedIsolationToken_ = iC.mayConsume<edm::ValueMap<std::vector<reco::PFCandidateRef > > >(_particleBasedIsolation);
99  }
100 
103 
104 
105 private:
106  const std::string _isolateAgainst, _vertexCollection;
107  const std::vector<unsigned> _miniAODVertexCodes;
108  const unsigned _vertexIndex;
110 
111 
112 };
113 
116  "PhotonPFIsolationWithMapBasedVeto");
117 
118 
119 //This function defines whether particular PFCandidate is inside of isolation cone of photon or not by checking deltaR and whether footprint removal for this candidate should be done. Additionally, for miniAOD charged hadrons from the PV are considered. *** For AOD this should be done by the corresponding sequence beforehand!!! ***
120 
122 
123  //convert the photon and candidate objects to the corresponding pat or reco objects. What is used depends on what is user running on: miniAOD or AOD
124  pat::patPhotonPtr aspat_photonptr(photon);
125 
126  pat::PackedCandidatePtr aspacked(pfCandidate);
127  reco::PFCandidatePtr aspf(pfCandidate);
128 
129 
130  bool inFootprint = false;
131  bool result = true;
132  const float deltar2 = reco::deltaR2(*photon,*pfCandidate); //calculate deltaR2 distance between PFCandidate and photon
133 
134  // dealing here with patObjects: miniAOD case
135  if ( aspacked.get() )
136  {
137  inFootprint = isInFootprint(aspat_photonptr ->associatedPackedPFCandidates(),aspacked);
138 
139  //checking if the charged candidates come from the appropriate vertex
140  if( aspacked->charge() != 0 )
141  {
142  bool is_vertex_allowed = false;
143  for( const unsigned vtxtype : _miniAODVertexCodes )
144  {
145  if( vtxtype == aspacked->fromPV(_vertexIndex) ) {
146  is_vertex_allowed = true;
147  break;
148  }
149  }
150 
151  result &= (is_vertex_allowed);
152  }
153  //return true if the candidate is inside the cone and not in the footprint
154  result &= deltar2 < _coneSize2 && (!inFootprint);
155 
156  }
157 
158  // dealing here with recoObjects: AOD case
159  else if ( aspf.get() && aspf.isNonnull())
160  {
161  inFootprint = isInFootprintAlternative((*particleBasedIsolationMap)[photon], aspf);
162  result &= deltar2 < _coneSize2 && (!inFootprint);
163 
164  }
165 
166  // throw exception if it is not a patObject or recoObject
167  else {
168  throw cms::Exception("InvalidIsolationInput")
169  << "The supplied candidate to be used as isolation "
170  << "was neither a reco::Photon nor a pat::Photon!";
171  }
172 
173  return result;
174 }
EDGetTokenT< ProductType > mayConsume(edm::InputTag const &tag)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:460
T const * get() const
Returns C++ pointer to the item.
Definition: Ptr.h:160
edm::Handle< edm::ValueMap< std::vector< reco::PFCandidateRef > > > particleBasedIsolationMap
edm::Ptr< pat::PackedCandidate > PackedCandidatePtr
Definition: HeavyIon.h:7
bool isInIsolationCone(const reco::CandidatePtr &photon, const reco::CandidatePtr &pfCandidate) const override final
int iEvent
Definition: GenABIO.cc:230
void setConsumes(edm::ConsumesCollector iC) override
const PVAssoc fromPV(size_t ipv=0) const
bool isInFootprint(const T &thefootprint, const U &theCandidate)
edm::EDGetTokenT< edm::ValueMap< std::vector< reco::PFCandidateRef > > > particleBasedIsolationToken_
virtual int charge() const
electric charge
bool isNonnull() const
Checks for non-null.
Definition: Ptr.h:169
edm::Ptr< reco::Photon > recoPhotonPtr
PhotonPFIsolationWithMapBasedVeto(const edm::ParameterSet &c)
T1 deltaR2(T1 eta1, T2 phi1, T3 eta2, T4 phi2)
Definition: deltaR.h:36
edm::Ptr< pat::Photon > patPhotonPtr
virtual void getEventInfo(const edm::Event &iEvent) override
fixed size matrix
HLT enums.
#define DEFINE_EDM_PLUGIN(factory, type, name)
long double T