CMS 3D CMS Logo

PuppiPhoton.cc
Go to the documentation of this file.
1 // system include files
2 #include <memory>
3 
4 // user include files
26 //Main File
28 
29 // ------------------------------------------------------------------------------------------
31  tokenPFCandidates_ = consumes<CandidateView>(iConfig.getParameter<edm::InputTag>("candName"));
32  tokenPuppiCandidates_ = consumes<CandidateView>(iConfig.getParameter<edm::InputTag>("puppiCandName"));
33  usePFphotons_ = iConfig.getParameter<bool>("usePFphotons");
34  if(!usePFphotons_)
35  tokenPhotonCandidates_ = consumes<CandidateView>(iConfig.getParameter<edm::InputTag>("photonName"));
36  usePhotonId_ = !(iConfig.getParameter<edm::InputTag>("photonId")).label().empty();
37  if(usePhotonId_)
38  tokenPhotonId_ = consumes<edm::ValueMap<bool> >(iConfig.getParameter<edm::InputTag>("photonId"));
39  runOnMiniAOD_ = iConfig.getParameter<bool>("runOnMiniAOD");
40  if(!runOnMiniAOD_)
41  reco2pf_ = consumes<edm::ValueMap<std::vector<reco::PFCandidateRef> > >(iConfig.getParameter<edm::InputTag>("recoToPFMap"));
42  useValueMap_ = iConfig.getParameter<bool>("useValueMap");
43  if(useValueMap_)
44  tokenWeights_ = consumes<edm::ValueMap<float> >(iConfig.getParameter<edm::InputTag>("weightsName"));
45 
46  pt_ = iConfig.getParameter<double>("pt");
47  eta_ = iConfig.getParameter<double>("eta");
48  dRMatch_ = iConfig.getParameter<std::vector<double> > ("dRMatch");
49  pdgIds_ = iConfig.getParameter<std::vector<int32_t> >("pdgids");
50  usePFRef_ = iConfig.getParameter<bool>("useRefs");
51  weight_ = iConfig.getParameter<double>("weight");
52  produces<PFOutputCollection>();
53  produces< edm::ValueMap<reco::CandidatePtr> >();
54 }
55 // ------------------------------------------------------------------------------------------
57 // ------------------------------------------------------------------------------------------
59  int iC = -1;
60  std::vector<const reco::Candidate*> phoCands;
61  std::vector<uint16_t> phoIndx;
62 
64  if(!runOnMiniAOD_) iEvent.getByToken(reco2pf_, reco2pf);
65 
66  // Get PFCandidate Collection
67  edm::Handle<CandidateView> hPFProduct;
68  iEvent.getByToken(tokenPFCandidates_,hPFProduct);
69  const CandidateView *pfCol = hPFProduct.product();
70 
71  edm::Handle<CandidateView> hPuppiProduct;
72  iEvent.getByToken(tokenPuppiCandidates_,hPuppiProduct);
73  const CandidateView *pupCol = hPuppiProduct.product();
74  if(usePFphotons_) {
75  for(const auto & pho : *pfCol) {
76  iC++;
77  if(pho.pt() < pt_) continue;
78  if(std::abs(pho.pdgId())!=22) continue;
79  if(fabs(pho.eta()) < eta_ ) {
80  phoIndx.push_back(iC);
81  phoCands.push_back(&pho);
82  }
83  }
84  } else {
85  edm::Handle<CandidateView> hPhoProduct;
86  iEvent.getByToken(tokenPhotonCandidates_,hPhoProduct);
87  const CandidateView *phoCol = hPhoProduct.product();
88 
90  if(usePhotonId_) iEvent.getByToken(tokenPhotonId_,photonId);
91 
92  for(CandidateView::const_iterator itPho = phoCol->begin(); itPho!=phoCol->end(); itPho++) {
93  iC++;
94  bool passObject = false;
95  if(itPho->isPhoton() && usePhotonId_) passObject = (*photonId) [phoCol->ptrAt(iC)];
96  if(itPho->pt() < pt_) continue;
97  if(!passObject && usePhotonId_) continue;
98  if(runOnMiniAOD_) {
99  const pat::Photon *pPho = dynamic_cast<const pat::Photon*>(&(*itPho));
100  if(pPho != nullptr) {
102  if(fabs(ref->eta()) < eta_ ) {
103  phoIndx.push_back(ref.key());
104  phoCands.push_back(&(*(pfCol->ptrAt(ref.key()))));
105  }
106  }
107  continue;
108  }
109  const pat::Electron *pElectron = dynamic_cast<const pat::Electron*>(&(*itPho));
110  if(pElectron != nullptr) {
112  if(fabs(ref->eta()) < eta_ ) {
113  phoIndx.push_back(ref.key());
114  phoCands.push_back(&(*(pfCol->ptrAt(ref.key()))));
115  }
116  }
117  } else {
118  for( const edm::Ref<std::vector<reco::PFCandidate> > & ref : (*reco2pf)[phoCol->ptrAt(iC)] ) {
119  if(fabs(ref->eta()) < eta_ ) {
120  phoIndx.push_back(ref.key());
121  phoCands.push_back(&(*(pfCol->ptrAt(ref.key()))));
122  }
123  }
124  }
125  }
126  }
127  //Get Weights
128  edm::Handle<edm::ValueMap<float> > pupWeights;
129  if(useValueMap_)
130  iEvent.getByToken(tokenWeights_,pupWeights);
131  std::unique_ptr<edm::ValueMap<LorentzVector> > p4PupOut(new edm::ValueMap<LorentzVector>());
132  LorentzVectorCollection puppiP4s;
133  std::vector<reco::CandidatePtr> values(hPFProduct->size());
134  int iPF = 0;
135  std::vector<float> lWeights;
136  static const reco::PFCandidate dummySinceTranslateIsNotStatic;
137  corrCandidates_.reset( new PFOutputCollection );
138  std::set<int> foundPhoIndex;
139  for(CandidateView::const_iterator itPF = pupCol->begin(); itPF!=pupCol->end(); itPF++) {
140  auto id = dummySinceTranslateIsNotStatic.translatePdgIdToType(itPF->pdgId());
141  const reco::PFCandidate *pPF = dynamic_cast<const reco::PFCandidate*>(&(*itPF));
142  reco::PFCandidate pCand( pPF ? *pPF : reco::PFCandidate(itPF->charge(), itPF->p4(), id) );
143  LorentzVector pVec = itPF->p4();
144  float pWeight = 1.;
145  if(useValueMap_) pWeight = (*pupWeights)[pupCol->ptrAt(iPF)];
146  if(!usePFRef_) {
147  int iPho = -1;
148  for(std::vector<const reco::Candidate*>::iterator itPho = phoCands.begin(); itPho!=phoCands.end(); itPho++) {
149  iPho++;
150  if((!matchPFCandidate(&(*itPF),*itPho))||(foundPhoIndex.count(iPho)!=0)) continue;
151  pWeight = weight_;
152  if(!useValueMap_ && itPF->pt() != 0) pWeight = pWeight*(phoCands[iPho]->pt()/itPF->pt());
153  if(!useValueMap_ && itPF->pt() == 0) pVec.SetPxPyPzE(phoCands[iPho]->px()*pWeight,phoCands[iPho]->py()*pWeight,phoCands[iPho]->pz()*pWeight,phoCands[iPho]->energy()*pWeight);
154  foundPhoIndex.insert(iPho);
155  }
156  } else {
157  int iPho = -1;
158  for(std::vector<uint16_t>::const_iterator itPho = phoIndx.begin(); itPho!=phoIndx.end(); itPho++) {
159  iPho++;
160  if(pupCol->refAt(iPF).key() != *itPho) continue;
161  pWeight = weight_;
162  if(!useValueMap_ && itPF->pt() != 0) pWeight = pWeight*(phoCands[iPho]->pt()/itPF->pt());
163  if(!useValueMap_ && itPF->pt() == 0) pVec.SetPxPyPzE(phoCands[iPho]->px()*pWeight,phoCands[iPho]->py()*pWeight,phoCands[iPho]->pz()*pWeight,phoCands[iPho]->energy()*pWeight);
164  foundPhoIndex.insert(iPho);
165  }
166  }
167  if(itPF->pt() != 0) pVec.SetPxPyPzE(itPF->px()*pWeight,itPF->py()*pWeight,itPF->pz()*pWeight,itPF->energy()*pWeight);
168 
169  lWeights.push_back(pWeight);
170  pCand.setP4(pVec);
171  puppiP4s.push_back( pVec );
172  pCand.setSourceCandidatePtr( itPF->sourceCandidatePtr(0) );
173  corrCandidates_->push_back(pCand);
174  iPF++;
175  }
176  //Add the missing pfcandidates
177  for(unsigned int iPho = 0; iPho < phoCands.size(); iPho++) {
178  if(foundPhoIndex.count(iPho)!=0) continue;
179  auto id = dummySinceTranslateIsNotStatic.translatePdgIdToType(phoCands[iPho]->pdgId());
180  reco::PFCandidate pCand(reco::PFCandidate(phoCands[iPho]->charge(), phoCands[iPho]->p4(),id) );
181  pCand.setSourceCandidatePtr( phoCands[iPho]->sourceCandidatePtr(0) );
182  LorentzVector pVec = phoCands[iPho]->p4();
183  pVec.SetPxPyPzE(phoCands[iPho]->px()*weight_,phoCands[iPho]->py()*weight_,phoCands[iPho]->pz()*weight_,phoCands[iPho]->energy()*weight_);
184  pCand.setP4(pVec);
185  lWeights.push_back(weight_);
186  puppiP4s.push_back( pVec );
187  corrCandidates_->push_back(pCand);
188  }
189  //Fill it into the event
191  for(unsigned int ic=0, nc = pupCol->size(); ic < nc; ++ic) {
192  reco::CandidatePtr pkref( oh, ic );
193  values[ic] = pkref;
194  }
195  std::unique_ptr<edm::ValueMap<reco::CandidatePtr> > pfMap_p(new edm::ValueMap<reco::CandidatePtr>());
197  filler.insert(hPFProduct, values.begin(), values.end());
198  filler.fill();
199  iEvent.put(std::move(pfMap_p));
200 }
201 // ------------------------------------------------------------------------------------------
203  if(iPF->pdgId() != iPho->pdgId()) return false;
204  double lDR = deltaR(iPF->eta(),iPF->phi(),iPho->eta(),iPho->phi());
205  for(unsigned int i0 = 0; i0 < pdgIds_.size(); i0++) {
206  if(std::abs(iPF->pdgId()) == pdgIds_[i0] && lDR < dRMatch_[i0]) return true;
207  }
208  return false;
209 }
210 // ------------------------------------------------------------------------------------------
212  //The following says we do not know what parameters are allowed so do no validation
213  // Please change this to state exactly what you do use, even if it is no parameters
215  desc.setUnknown();
216  descriptions.addDefault(desc);
217 }
218 //define this as a plug-in
std::vector< int32_t > pdgIds_
Definition: PuppiPhoton.h:47
double pt_
Definition: PuppiPhoton.h:40
T getParameter(std::string const &) const
Analysis-level Photon class.
Definition: Photon.h:47
edm::EDGetTokenT< CandidateView > tokenPhotonCandidates_
Definition: PuppiPhoton.h:36
PuppiPhoton(const edm::ParameterSet &)
Definition: PuppiPhoton.cc:30
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:127
~PuppiPhoton() override
Definition: PuppiPhoton.cc:56
bool runOnMiniAOD_
Definition: PuppiPhoton.h:44
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:508
Ptr< value_type > ptrAt(size_type i) const
std::vector< reco::PFCandidate > PFOutputCollection
Definition: PuppiPhoton.h:28
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
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...
size_type size() const
void setSourceCandidatePtr(const PFCandidatePtr &ptr)
Definition: PFCandidate.h:125
edm::EDGetTokenT< CandidateView > tokenPuppiCandidates_
Definition: PuppiPhoton.h:35
bool usePFRef_
Definition: PuppiPhoton.h:42
std::unique_ptr< PFOutputCollection > corrCandidates_
Definition: PuppiPhoton.h:48
edm::EDGetTokenT< edm::ValueMap< std::vector< reco::PFCandidateRef > > > reco2pf_
Definition: PuppiPhoton.h:37
RefToBase< value_type > refAt(size_type i) const
bool usePFphotons_
Definition: PuppiPhoton.h:43
int iEvent
Definition: GenABIO.cc:230
void addDefault(ParameterSetDescription const &psetDescription)
const_iterator begin() const
virtual int pdgId() const =0
PDG identifier.
double p4[4]
Definition: TauolaWrapper.h:92
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
bool matchPFCandidate(const reco::Candidate *iPF, const reco::Candidate *iPho)
Definition: PuppiPhoton.cc:202
bool usePhotonId_
Definition: PuppiPhoton.h:45
double weight_
Definition: PuppiPhoton.h:49
double deltaR(double eta1, double eta2, double phi1, double phi2)
Definition: TreeUtility.cc:17
double eta_
Definition: PuppiPhoton.h:41
void produce(edm::Event &, const edm::EventSetup &) override
Definition: PuppiPhoton.cc:58
virtual double eta() const =0
momentum pseudorapidity
T const * product() const
Definition: Handle.h:81
edm::EDGetTokenT< edm::ValueMap< float > > tokenWeights_
Definition: PuppiPhoton.h:38
Analysis-level electron class.
Definition: Electron.h:52
ParticleType translatePdgIdToType(int pdgid) const
Definition: PFCandidate.cc:229
std::vector< double > dRMatch_
Definition: PuppiPhoton.h:46
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:39
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:86
edm::EDGetTokenT< edm::ValueMap< bool > > tokenPhotonId_
Definition: PuppiPhoton.h:39
const_iterator end() const
math::XYZTLorentzVector LorentzVector
Definition: PuppiPhoton.h:25
edm::EDGetTokenT< CandidateView > tokenPFCandidates_
Definition: PuppiPhoton.h:34
virtual double phi() const =0
momentum azimuthal angle
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.h:26
void setP4(const LorentzVector &p4) final
set 4-momentum
def move(src, dest)
Definition: eostools.py:510
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Definition: PuppiPhoton.cc:211
bool useValueMap_
Definition: PuppiPhoton.h:50