CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
PuppiPhoton.cc
Go to the documentation of this file.
24 
25 #include <memory>
26 
27 // ------------------------------------------------------------------------------------------
29 public:
30  explicit PuppiPhoton(const edm::ParameterSet &);
31  ~PuppiPhoton() override;
32 
33  static void fillDescriptions(edm::ConfigurationDescriptions &descriptions);
35  typedef std::vector<LorentzVector> LorentzVectorCollection;
37  typedef std::vector<reco::PFCandidate> PFOutputCollection;
39 
40 private:
41  void produce(edm::Event &, const edm::EventSetup &) override;
42  bool matchPFCandidate(const reco::Candidate *iPF, const reco::Candidate *iPho);
49  double pt_;
50  double eta_;
51  bool usePFRef_;
55  std::vector<double> dRMatch_;
56  std::vector<int32_t> pdgIds_;
57  std::unique_ptr<PFOutputCollection> corrCandidates_;
58  double weight_;
60 };
61 
62 // ------------------------------------------------------------------------------------------
64  tokenPFCandidates_ = consumes<CandidateView>(iConfig.getParameter<edm::InputTag>("candName"));
65  tokenPuppiCandidates_ = consumes<CandidateView>(iConfig.getParameter<edm::InputTag>("puppiCandName"));
66  usePFphotons_ = iConfig.getParameter<bool>("usePFphotons");
67  if (!usePFphotons_)
68  tokenPhotonCandidates_ = consumes<CandidateView>(iConfig.getParameter<edm::InputTag>("photonName"));
69  usePhotonId_ = !(iConfig.getParameter<edm::InputTag>("photonId")).label().empty();
70  if (usePhotonId_)
71  tokenPhotonId_ = consumes<edm::ValueMap<bool>>(iConfig.getParameter<edm::InputTag>("photonId"));
72  runOnMiniAOD_ = iConfig.getParameter<bool>("runOnMiniAOD");
73  if (!runOnMiniAOD_)
74  reco2pf_ =
75  consumes<edm::ValueMap<std::vector<reco::PFCandidateRef>>>(iConfig.getParameter<edm::InputTag>("recoToPFMap"));
76  useValueMap_ = iConfig.getParameter<bool>("useValueMap");
77  if (useValueMap_)
78  tokenWeights_ = consumes<edm::ValueMap<float>>(iConfig.getParameter<edm::InputTag>("weightsName"));
79 
80  pt_ = iConfig.getParameter<double>("pt");
81  eta_ = iConfig.getParameter<double>("eta");
82  dRMatch_ = iConfig.getParameter<std::vector<double>>("dRMatch");
83  pdgIds_ = iConfig.getParameter<std::vector<int32_t>>("pdgids");
84  usePFRef_ = iConfig.getParameter<bool>("useRefs");
85  weight_ = iConfig.getParameter<double>("weight");
86  produces<PFOutputCollection>();
87  produces<edm::ValueMap<reco::CandidatePtr>>();
88 }
89 // ------------------------------------------------------------------------------------------
91 // ------------------------------------------------------------------------------------------
93  int iC = -1;
94  std::vector<const reco::Candidate *> phoCands;
95  std::vector<uint16_t> phoIndx;
96 
98  if (!runOnMiniAOD_)
99  iEvent.getByToken(reco2pf_, reco2pf);
100 
101  // Get PFCandidate Collection
102  edm::Handle<CandidateView> hPFProduct;
103  iEvent.getByToken(tokenPFCandidates_, hPFProduct);
104  const CandidateView *pfCol = hPFProduct.product();
105 
106  edm::Handle<CandidateView> hPuppiProduct;
107  iEvent.getByToken(tokenPuppiCandidates_, hPuppiProduct);
108  const CandidateView *pupCol = hPuppiProduct.product();
109  if (usePFphotons_) {
110  for (const auto &pho : *pfCol) {
111  iC++;
112  if (pho.pt() < pt_)
113  continue;
114  if (std::abs(pho.pdgId()) != 22)
115  continue;
116  if (fabs(pho.eta()) < eta_) {
117  phoIndx.push_back(iC);
118  phoCands.push_back(&pho);
119  }
120  }
121  } else {
122  edm::Handle<CandidateView> hPhoProduct;
123  iEvent.getByToken(tokenPhotonCandidates_, hPhoProduct);
124  const CandidateView *phoCol = hPhoProduct.product();
125 
127  if (usePhotonId_)
128  iEvent.getByToken(tokenPhotonId_, photonId);
129 
130  for (CandidateView::const_iterator itPho = phoCol->begin(); itPho != phoCol->end(); itPho++) {
131  iC++;
132  bool passObject = false;
133  if (itPho->isPhoton() && usePhotonId_)
134  passObject = (*photonId)[phoCol->ptrAt(iC)];
135  if (itPho->pt() < pt_)
136  continue;
137  if (!passObject && usePhotonId_)
138  continue;
139  if (runOnMiniAOD_) {
140  const pat::Photon *pPho = dynamic_cast<const pat::Photon *>(&(*itPho));
141  if (pPho != nullptr) {
143  if (fabs(ref->eta()) < eta_) {
144  phoIndx.push_back(ref.key());
145  phoCands.push_back(&(*(pfCol->ptrAt(ref.key()))));
146  }
147  }
148  continue;
149  }
150  const pat::Electron *pElectron = dynamic_cast<const pat::Electron *>(&(*itPho));
151  if (pElectron != nullptr) {
153  if (fabs(ref->eta()) < eta_) {
154  phoIndx.push_back(ref.key());
155  phoCands.push_back(&(*(pfCol->ptrAt(ref.key()))));
156  }
157  }
158  } else {
159  for (const edm::Ref<std::vector<reco::PFCandidate>> &ref : (*reco2pf)[phoCol->ptrAt(iC)]) {
160  if (fabs(ref->eta()) < eta_) {
161  phoIndx.push_back(ref.key());
162  phoCands.push_back(&(*(pfCol->ptrAt(ref.key()))));
163  }
164  }
165  }
166  }
167  }
168  //Get Weights
170  if (useValueMap_)
171  iEvent.getByToken(tokenWeights_, pupWeights);
172  std::unique_ptr<edm::ValueMap<LorentzVector>> p4PupOut(new edm::ValueMap<LorentzVector>());
173  LorentzVectorCollection puppiP4s;
174  std::vector<reco::CandidatePtr> values(hPFProduct->size());
175  int iPF = 0;
176  std::vector<float> lWeights;
177  static const reco::PFCandidate dummySinceTranslateIsNotStatic;
178  corrCandidates_ = std::make_unique<PFOutputCollection>();
179  std::set<int> foundPhoIndex;
180  for (CandidateView::const_iterator itPF = pupCol->begin(); itPF != pupCol->end(); itPF++) {
181  auto id = dummySinceTranslateIsNotStatic.translatePdgIdToType(itPF->pdgId());
182  const reco::PFCandidate *pPF = dynamic_cast<const reco::PFCandidate *>(&(*itPF));
183  reco::PFCandidate pCand(pPF ? *pPF : reco::PFCandidate(itPF->charge(), itPF->p4(), id));
184  LorentzVector pVec = itPF->p4();
185  float pWeight = 1.;
186  if (useValueMap_)
187  pWeight = (*pupWeights)[pupCol->ptrAt(iPF)];
188  if (!usePFRef_) {
189  int iPho = -1;
190  for (std::vector<const reco::Candidate *>::iterator itPho = phoCands.begin(); itPho != phoCands.end(); itPho++) {
191  iPho++;
192  if ((!matchPFCandidate(&(*itPF), *itPho)) || (foundPhoIndex.count(iPho) != 0))
193  continue;
194  pWeight = weight_;
195  if (!useValueMap_ && itPF->pt() != 0)
196  pWeight = pWeight * (phoCands[iPho]->pt() / itPF->pt());
197  if (!useValueMap_ && itPF->pt() == 0)
198  pVec.SetPxPyPzE(phoCands[iPho]->px() * pWeight,
199  phoCands[iPho]->py() * pWeight,
200  phoCands[iPho]->pz() * pWeight,
201  phoCands[iPho]->energy() * pWeight);
202  foundPhoIndex.insert(iPho);
203  }
204  } else {
205  int iPho = -1;
206  for (std::vector<uint16_t>::const_iterator itPho = phoIndx.begin(); itPho != phoIndx.end(); itPho++) {
207  iPho++;
208  if (pupCol->refAt(iPF).key() != *itPho)
209  continue;
210  pWeight = weight_;
211  if (!useValueMap_ && itPF->pt() != 0)
212  pWeight = pWeight * (phoCands[iPho]->pt() / itPF->pt());
213  if (!useValueMap_ && itPF->pt() == 0)
214  pVec.SetPxPyPzE(phoCands[iPho]->px() * pWeight,
215  phoCands[iPho]->py() * pWeight,
216  phoCands[iPho]->pz() * pWeight,
217  phoCands[iPho]->energy() * pWeight);
218  foundPhoIndex.insert(iPho);
219  }
220  }
221  if (itPF->pt() != 0)
222  pVec.SetPxPyPzE(itPF->px() * pWeight, itPF->py() * pWeight, itPF->pz() * pWeight, itPF->energy() * pWeight);
223 
224  lWeights.push_back(pWeight);
225  pCand.setP4(pVec);
226  puppiP4s.push_back(pVec);
227  pCand.setSourceCandidatePtr(itPF->sourceCandidatePtr(0));
228  corrCandidates_->push_back(pCand);
229  iPF++;
230  }
231  //Add the missing pfcandidates
232  for (unsigned int iPho = 0; iPho < phoCands.size(); iPho++) {
233  if (foundPhoIndex.count(iPho) != 0)
234  continue;
235  auto id = dummySinceTranslateIsNotStatic.translatePdgIdToType(phoCands[iPho]->pdgId());
236  reco::PFCandidate pCand(reco::PFCandidate(phoCands[iPho]->charge(), phoCands[iPho]->p4(), id));
237  pCand.setSourceCandidatePtr(phoCands[iPho]->sourceCandidatePtr(0));
238  LorentzVector pVec = phoCands[iPho]->p4();
239  pVec.SetPxPyPzE(phoCands[iPho]->px() * weight_,
240  phoCands[iPho]->py() * weight_,
241  phoCands[iPho]->pz() * weight_,
242  phoCands[iPho]->energy() * weight_);
243  pCand.setP4(pVec);
244  lWeights.push_back(weight_);
245  puppiP4s.push_back(pVec);
246  corrCandidates_->push_back(pCand);
247  }
248  //Fill it into the event
250  for (unsigned int ic = 0, nc = pupCol->size(); ic < nc; ++ic) {
251  reco::CandidatePtr pkref(oh, ic);
252  values[ic] = pkref;
253  }
254  std::unique_ptr<edm::ValueMap<reco::CandidatePtr>> pfMap_p(new edm::ValueMap<reco::CandidatePtr>());
256  filler.insert(hPFProduct, values.begin(), values.end());
257  filler.fill();
258  iEvent.put(std::move(pfMap_p));
259 }
260 // ------------------------------------------------------------------------------------------
262  if (iPF->pdgId() != iPho->pdgId())
263  return false;
264  double lDR = deltaR(iPF->eta(), iPF->phi(), iPho->eta(), iPho->phi());
265  for (unsigned int i0 = 0; i0 < pdgIds_.size(); i0++) {
266  if (std::abs(iPF->pdgId()) == pdgIds_[i0] && lDR < dRMatch_[i0])
267  return true;
268  }
269  return false;
270 }
271 // ------------------------------------------------------------------------------------------
273  //The following says we do not know what parameters are allowed so do no validation
274  // Please change this to state exactly what you do use, even if it is no parameters
276  desc.setUnknown();
277  descriptions.addDefault(desc);
278 }
279 //define this as a plug-in
std::vector< int32_t > pdgIds_
Definition: PuppiPhoton.cc:56
double pt_
Definition: PuppiPhoton.cc:49
Analysis-level Photon class.
Definition: Photon.h:46
PuppiPhoton(const edm::ParameterSet &)
Definition: PuppiPhoton.cc:63
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:133
~PuppiPhoton() override
Definition: PuppiPhoton.cc:90
uint16_t *__restrict__ id
bool runOnMiniAOD_
Definition: PuppiPhoton.cc:53
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:539
Ptr< value_type > ptrAt(size_type i) const
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
void insert(const H &h, I begin, I end)
Definition: ValueMap.h:53
edm::RefVector< pat::PackedCandidateCollection > associatedPackedPFCandidates() const
References to PFCandidates linked to this object (e.g. for isolation vetos or masking before jet recl...
edm::View< reco::PFCandidate > PFView
Definition: PuppiPhoton.cc:38
size_type size() const
edm::View< reco::Candidate > CandidateView
Definition: PuppiPhoton.cc:36
void setSourceCandidatePtr(const PFCandidatePtr &ptr)
Definition: PFCandidate.h:120
bool usePFRef_
Definition: PuppiPhoton.cc:51
RefToBase< value_type > refAt(size_type i) const
XYZTLorentzVectorD XYZTLorentzVector
Lorentz vector with cylindrical internal representation using pseudorapidity.
Definition: LorentzVector.h:29
bool usePFphotons_
Definition: PuppiPhoton.cc:52
int iEvent
Definition: GenABIO.cc:224
void addDefault(ParameterSetDescription const &psetDescription)
const_iterator begin() const
edm::EDGetTokenT< edm::ValueMap< float > > tokenWeights_
Definition: PuppiPhoton.cc:47
def move
Definition: eostools.py:511
edm::EDGetTokenT< edm::ValueMap< bool > > tokenPhotonId_
Definition: PuppiPhoton.cc:48
edm::EDGetTokenT< CandidateView > tokenPFCandidates_
Definition: PuppiPhoton.cc:43
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
bool matchPFCandidate(const reco::Candidate *iPF, const reco::Candidate *iPho)
Definition: PuppiPhoton.cc:261
bool usePhotonId_
Definition: PuppiPhoton.cc:54
double weight_
Definition: PuppiPhoton.cc:58
virtual int pdgId() const =0
PDG identifier.
double eta_
Definition: PuppiPhoton.cc:50
std::vector< reco::PFCandidate > PFOutputCollection
Definition: PuppiPhoton.cc:37
void produce(edm::Event &, const edm::EventSetup &) override
Definition: PuppiPhoton.cc:92
T const * product() const
Definition: Handle.h:70
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
Analysis-level electron class.
Definition: Electron.h:51
ParticleType translatePdgIdToType(int pdgid) const
Definition: PFCandidate.cc:231
edm::EDGetTokenT< CandidateView > tokenPuppiCandidates_
Definition: PuppiPhoton.cc:44
std::unique_ptr< PFOutputCollection > corrCandidates_
Definition: PuppiPhoton.cc:57
std::vector< double > dRMatch_
Definition: PuppiPhoton.cc:55
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:41
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:86
const_iterator end() const
math::XYZTLorentzVector LorentzVector
Definition: PuppiPhoton.cc:34
edm::EDGetTokenT< edm::ValueMap< std::vector< reco::PFCandidateRef > > > reco2pf_
Definition: PuppiPhoton.cc:46
edm::RefVector< pat::PackedCandidateCollection > associatedPackedPFCandidates() const
References to PFCandidates linked to this object (e.g. for isolation vetos or masking before jet recl...
std::vector< LorentzVector > LorentzVectorCollection
Definition: PuppiPhoton.cc:35
void setP4(const LorentzVector &p4) final
set 4-momentum
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Definition: PuppiPhoton.cc:272
bool useValueMap_
Definition: PuppiPhoton.cc:59
virtual double phi() const =0
momentum azimuthal angle
virtual double eta() const =0
momentum pseudorapidity
edm::EDGetTokenT< CandidateView > tokenPhotonCandidates_
Definition: PuppiPhoton.cc:45