CMS 3D CMS Logo

PuppiProducer.cc
Go to the documentation of this file.
1 // system include files
2 #include <memory>
3 
4 // user include files
19 //Main File
22 
23 
24 // ------------------------------------------------------------------------------------------
26  fPuppiDiagnostics = iConfig.getParameter<bool>("puppiDiagnostics");
27  fPuppiForLeptons = iConfig.getParameter<bool>("puppiForLeptons");
28  fUseDZ = iConfig.getParameter<bool>("UseDeltaZCut");
29  fDZCut = iConfig.getParameter<double>("DeltaZCut");
30  fPtMax = iConfig.getParameter<double>("PtMaxNeutrals");
31  fUseExistingWeights = iConfig.getParameter<bool>("useExistingWeights");
32  fUseWeightsNoLep = iConfig.getParameter<bool>("useWeightsNoLep");
33  fClonePackedCands = iConfig.getParameter<bool>("clonePackedCands");
34  fVtxNdofCut = iConfig.getParameter<int>("vtxNdofCut");
35  fVtxZCut = iConfig.getParameter<double>("vtxZCut");
36  fPuppiContainer = std::unique_ptr<PuppiContainer> ( new PuppiContainer(iConfig) );
37 
39  = consumes<CandidateView>(iConfig.getParameter<edm::InputTag>("candName"));
41  = consumes<VertexCollection>(iConfig.getParameter<edm::InputTag>("vertexName"));
42 
43 
44  produces<edm::ValueMap<float> > ();
45  produces<edm::ValueMap<LorentzVector> > ();
46  produces< edm::ValueMap<reco::CandidatePtr> >();
47 
48  if (fUseExistingWeights || fClonePackedCands)
49  produces<pat::PackedCandidateCollection>();
50  else
51  produces<reco::PFCandidateCollection>();
52 
53  if (fPuppiDiagnostics){
54  produces<double> ("PuppiNAlgos");
55  produces<std::vector<double>> ("PuppiRawAlphas");
56  produces<std::vector<double>> ("PuppiAlphas");
57  produces<std::vector<double>> ("PuppiAlphasMed");
58  produces<std::vector<double>> ("PuppiAlphasRms");
59  }
60 }
61 // ------------------------------------------------------------------------------------------
63 }
64 // ------------------------------------------------------------------------------------------
66 
67  // Get PFCandidate Collection
68  edm::Handle<CandidateView> hPFProduct;
69  iEvent.getByToken(tokenPFCandidates_,hPFProduct);
70  const CandidateView *pfCol = hPFProduct.product();
71 
72  // Get vertex collection w/PV as the first entry?
74  iEvent.getByToken(tokenVertices_,hVertexProduct);
75  const reco::VertexCollection *pvCol = hVertexProduct.product();
76 
77  int npv = 0;
78  const reco::VertexCollection::const_iterator vtxEnd = pvCol->end();
79  for (reco::VertexCollection::const_iterator vtxIter = pvCol->begin(); vtxEnd != vtxIter; ++vtxIter) {
80  if (!vtxIter->isFake() && vtxIter->ndof()>=fVtxNdofCut && std::abs(vtxIter->z())<=fVtxZCut)
81  npv++;
82  }
83 
84  //Fill the reco objects
85  fRecoObjCollection.clear();
86  for(CandidateView::const_iterator itPF = pfCol->begin(); itPF!=pfCol->end(); itPF++) {
87  // std::cout << "itPF->pdgId() = " << itPF->pdgId() << std::endl;
88  RecoObj pReco;
89  pReco.pt = itPF->pt();
90  pReco.eta = itPF->eta();
91  pReco.phi = itPF->phi();
92  pReco.m = itPF->mass();
93  pReco.rapidity = itPF->rapidity();
94  pReco.charge = itPF->charge();
95  const reco::Vertex *closestVtx = nullptr;
96  double pDZ = -9999;
97  double pD0 = -9999;
98  int pVtxId = -9999;
99  bool lFirst = true;
100  const pat::PackedCandidate *lPack = dynamic_cast<const pat::PackedCandidate*>(&(*itPF));
101  if(lPack == nullptr ) {
102 
103  const reco::PFCandidate *pPF = dynamic_cast<const reco::PFCandidate*>(&(*itPF));
104  double curdz = 9999;
105  int closestVtxForUnassociateds = -9999;
106  for(reco::VertexCollection::const_iterator iV = pvCol->begin(); iV!=pvCol->end(); ++iV) {
107  if(lFirst) {
108  if ( pPF->trackRef().isNonnull() ) pDZ = pPF->trackRef() ->dz(iV->position());
109  else if ( pPF->gsfTrackRef().isNonnull() ) pDZ = pPF->gsfTrackRef()->dz(iV->position());
110  if ( pPF->trackRef().isNonnull() ) pD0 = pPF->trackRef() ->d0();
111  else if ( pPF->gsfTrackRef().isNonnull() ) pD0 = pPF->gsfTrackRef()->d0();
112  lFirst = false;
113  if(pDZ > -9999) pVtxId = 0;
114  }
115  if(iV->trackWeight(pPF->trackRef())>0) {
116  closestVtx = &(*iV);
117  break;
118  }
119  // in case it's unassocciated, keep more info
120  double tmpdz = 99999;
121  if ( pPF->trackRef().isNonnull() ) tmpdz = pPF->trackRef() ->dz(iV->position());
122  else if ( pPF->gsfTrackRef().isNonnull() ) tmpdz = pPF->gsfTrackRef()->dz(iV->position());
123  if (std::abs(tmpdz) < curdz){
124  curdz = std::abs(tmpdz);
125  closestVtxForUnassociateds = pVtxId;
126  }
127  pVtxId++;
128 
129  }
130  int tmpFromPV = 0;
131  // mocking the miniAOD definitions
132  if (closestVtx != nullptr && std::abs(pReco.charge) > 0 && pVtxId > 0) tmpFromPV = 0;
133  if (closestVtx != nullptr && std::abs(pReco.charge) > 0 && pVtxId == 0) tmpFromPV = 3;
134  if (closestVtx == nullptr && std::abs(pReco.charge) > 0 && closestVtxForUnassociateds == 0) tmpFromPV = 2;
135  if (closestVtx == nullptr && std::abs(pReco.charge) > 0 && closestVtxForUnassociateds != 0) tmpFromPV = 1;
136  pReco.dZ = pDZ;
137  pReco.d0 = pD0;
138  pReco.id = 0;
139  if (std::abs(pReco.charge) == 0){ pReco.id = 0; }
140  else{
141  if (tmpFromPV == 0){ pReco.id = 2; } // 0 is associated to PU vertex
142  if (tmpFromPV == 3){ pReco.id = 1; }
143  if (tmpFromPV == 1 || tmpFromPV == 2){
144  pReco.id = 0;
145  if (!fPuppiForLeptons && fUseDZ && (std::abs(pDZ) < fDZCut)) pReco.id = 1;
146  if (!fPuppiForLeptons && fUseDZ && (std::abs(pDZ) > fDZCut)) pReco.id = 2;
147  if (fPuppiForLeptons && tmpFromPV == 1) pReco.id = 2;
148  if (fPuppiForLeptons && tmpFromPV == 2) pReco.id = 1;
149  }
150  }
151  }
152  else if(lPack->vertexRef().isNonnull() ) {
153  pDZ = lPack->dz();
154  pD0 = lPack->dxy();
155  pReco.dZ = pDZ;
156  pReco.d0 = pD0;
157 
158  pReco.id = 0;
159  if (std::abs(pReco.charge) == 0){ pReco.id = 0; }
160  if (std::abs(pReco.charge) > 0){
161  if (lPack->fromPV() == 0){ pReco.id = 2; } // 0 is associated to PU vertex
162  if (lPack->fromPV() == (pat::PackedCandidate::PVUsedInFit)){ pReco.id = 1; }
163  if (lPack->fromPV() == (pat::PackedCandidate::PVTight) || lPack->fromPV() == (pat::PackedCandidate::PVLoose)){
164  pReco.id = 0;
165  if (!fPuppiForLeptons && fUseDZ && (std::abs(pDZ) < fDZCut)) pReco.id = 1;
166  if (!fPuppiForLeptons && fUseDZ && (std::abs(pDZ) > fDZCut)) pReco.id = 2;
167  if (fPuppiForLeptons && lPack->fromPV() == (pat::PackedCandidate::PVLoose)) pReco.id = 2;
168  if (fPuppiForLeptons && lPack->fromPV() == (pat::PackedCandidate::PVTight)) pReco.id = 1;
169  }
170  }
171  }
172 
173  fRecoObjCollection.push_back(pReco);
174 
175  }
176 
177  fPuppiContainer->initialize(fRecoObjCollection);
178  fPuppiContainer->setNPV( npv );
179 
180  std::vector<double> lWeights;
181  std::vector<PuppiCandidate> lCandidates;
182  if (!fUseExistingWeights){
183  //Compute the weights and get the particles
184  lWeights = fPuppiContainer->puppiWeights();
185  lCandidates = fPuppiContainer->puppiParticles();
186  }
187  else{
188  //Use the existing weights
189  int lPackCtr = 0;
190  for(CandidateView::const_iterator itPF = pfCol->begin(); itPF!=pfCol->end(); itPF++) {
191  const pat::PackedCandidate *lPack = dynamic_cast<const pat::PackedCandidate*>(&(*itPF));
192  float curpupweight = -1.;
193  if(lPack == nullptr ) {
194  // throw error
195  throw edm::Exception(edm::errors::LogicError,"PuppiProducer: cannot get weights since inputs are not PackedCandidates");
196  }
197  else{
198  if (fUseWeightsNoLep){ curpupweight = lPack->puppiWeightNoLep(); }
199  else{ curpupweight = lPack->puppiWeight(); }
200  }
201  lWeights.push_back(curpupweight);
202  PuppiCandidate curjet( curpupweight*lPack->px(), curpupweight*lPack->py(), curpupweight*lPack->pz(), curpupweight*lPack->energy());
203  curjet.set_user_index(lPackCtr);
204  lCandidates.push_back(curjet);
205  lPackCtr++;
206  }
207  }
208 
209  //Fill it into the event
210  std::unique_ptr<edm::ValueMap<float> > lPupOut(new edm::ValueMap<float>());
211  edm::ValueMap<float>::Filler lPupFiller(*lPupOut);
212  lPupFiller.insert(hPFProduct,lWeights.begin(),lWeights.end());
213  lPupFiller.fill();
214 
215  // This is a dummy to access the "translate" method which is a
216  // non-static member function even though it doesn't need to be.
217  // Will fix in the future.
218  static const reco::PFCandidate dummySinceTranslateIsNotStatic;
219 
220  // Fill a new PF/Packed Candidate Collection and write out the ValueMap of the new p4s.
221  // Since the size of the ValueMap must be equal to the input collection, we need
222  // to search the "puppi" particles to find a match for each input. If none is found,
223  // the input is set to have a four-vector of 0,0,0,0
226  std::unique_ptr<edm::ValueMap<LorentzVector> > p4PupOut(new edm::ValueMap<LorentzVector>());
227  LorentzVectorCollection puppiP4s;
228  std::vector<reco::CandidatePtr> values(hPFProduct->size());
229 
230  for ( auto i0 = hPFProduct->begin(),
231  i0begin = hPFProduct->begin(),
232  i0end = hPFProduct->end(); i0 != i0end; ++i0 ) {
233  std::unique_ptr<pat::PackedCandidate> pCand;
234  std::unique_ptr<reco::PFCandidate> pfCand;
236  const pat::PackedCandidate *cand = dynamic_cast<const pat::PackedCandidate*>(&(*i0));
237  if(!cand)
238  throw edm::Exception(edm::errors::LogicError,"PuppiProducer: inputs are not PackedCandidates");
239  pCand.reset( new pat::PackedCandidate(*cand) );
240  } else {
241  auto id = dummySinceTranslateIsNotStatic.translatePdgIdToType(i0->pdgId());
242  const reco::PFCandidate *cand = dynamic_cast<const reco::PFCandidate*>(&(*i0));
243  pfCand.reset( new reco::PFCandidate( cand ? *cand : reco::PFCandidate(i0->charge(), i0->p4(), id) ) );
244  }
245  LorentzVector pVec = i0->p4();
246  int val = i0 - i0begin;
247 
248  // Find the Puppi particle matched to the input collection using the "user_index" of the object.
249  auto puppiMatched = find_if( lCandidates.begin(), lCandidates.end(), [&val]( PuppiCandidate const & i ){ return i.user_index() == val; } );
250  if ( puppiMatched != lCandidates.end() ) {
251  pVec.SetPxPyPzE(puppiMatched->px(),puppiMatched->py(),puppiMatched->pz(),puppiMatched->E());
253  if(fPuppiForLeptons)
254  pCand->setPuppiWeight(pCand->puppiWeight(),lWeights[puppiMatched-lCandidates.begin()]);
255  else
256  pCand->setPuppiWeight(lWeights[puppiMatched-lCandidates.begin()],pCand->puppiWeightNoLep());
257  }
258  } else {
259  pVec.SetPxPyPzE( 0, 0, 0, 0);
261  pCand->setPuppiWeight(0,0);
262  }
263  }
264  puppiP4s.push_back( pVec );
265 
267  pCand->setP4(pVec);
268  pCand->setSourceCandidatePtr( i0->sourceCandidatePtr(0) );
269  fPackedPuppiCandidates->push_back(*pCand);
270  } else {
271  pfCand->setP4(pVec);
272  pfCand->setSourceCandidatePtr( i0->sourceCandidatePtr(0) );
273  fPuppiCandidates->push_back(*pfCand);
274  }
275  }
276 
277  //Compute the modified p4s
278  edm::ValueMap<LorentzVector>::Filler p4PupFiller(*p4PupOut);
279  p4PupFiller.insert(hPFProduct,puppiP4s.begin(), puppiP4s.end() );
280  p4PupFiller.fill();
281 
282  iEvent.put(std::move(lPupOut));
283  iEvent.put(std::move(p4PupOut));
286  for(unsigned int ic=0, nc = oh->size(); ic < nc; ++ic) {
287  reco::CandidatePtr pkref( oh, ic );
288  values[ic] = pkref;
289  }
290  } else {
292  for(unsigned int ic=0, nc = oh->size(); ic < nc; ++ic) {
293  reco::CandidatePtr pkref( oh, ic );
294  values[ic] = pkref;
295  }
296  }
297  std::unique_ptr<edm::ValueMap<reco::CandidatePtr> > pfMap_p(new edm::ValueMap<reco::CandidatePtr>());
299  filler.insert(hPFProduct, values.begin(), values.end());
300  filler.fill();
301  iEvent.put(std::move(pfMap_p));
302 
303 
306 
307  // all the different alphas per particle
308  // THE alpha per particle
309  std::unique_ptr<std::vector<double> > theAlphas(new std::vector<double>(fPuppiContainer->puppiAlphas()));
310  std::unique_ptr<std::vector<double> > theAlphasMed(new std::vector<double>(fPuppiContainer->puppiAlphasMed()));
311  std::unique_ptr<std::vector<double> > theAlphasRms(new std::vector<double>(fPuppiContainer->puppiAlphasRMS()));
312  std::unique_ptr<std::vector<double> > alphas(new std::vector<double>(fPuppiContainer->puppiRawAlphas()));
313  std::unique_ptr<double> nalgos(new double(fPuppiContainer->puppiNAlgos()));
314 
315  iEvent.put(std::move(alphas),"PuppiRawAlphas");
316  iEvent.put(std::move(nalgos),"PuppiNAlgos");
317  iEvent.put(std::move(theAlphas),"PuppiAlphas");
318  iEvent.put(std::move(theAlphasMed),"PuppiAlphasMed");
319  iEvent.put(std::move(theAlphasRms),"PuppiAlphasRms");
320  }
321 
322 }
323 
324 // ------------------------------------------------------------------------------------------
326 }
327 // ------------------------------------------------------------------------------------------
329 }
330 // ------------------------------------------------------------------------------------------
332  //The following says we do not know what parameters are allowed so do no validation
333  // Please change this to state exactly what you do use, even if it is no parameters
335  desc.setUnknown();
336  descriptions.addDefault(desc);
337 }
338 //define this as a plug-in
float puppiWeight() const
Set both weights at once (with option for only full PUPPI)
virtual float dz(size_t ipv=0) const
dz with respect to the PV[ipv]
T getParameter(std::string const &) const
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:137
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:253
double px() const override
x coordinate of momentum vector
float puppiWeightNoLep() const
Weight from full PUPPI.
float d0
Definition: RecoObj.h:28
virtual void endJob()
bool fUseWeightsNoLep
Definition: PuppiProducer.h:54
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:579
std::vector< reco::PFCandidate > PFOutputCollection
Definition: PuppiProducer.h:34
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
Definition: RecoObj.h:4
bool fPuppiDiagnostics
Definition: PuppiProducer.h:48
void insert(const H &h, I begin, I end)
Definition: ValueMap.h:53
std::vector< pat::PackedCandidate > PackedOutputCollection
Definition: PuppiProducer.h:35
std::vector< RecoObj > fRecoObjCollection
Definition: PuppiProducer.h:59
size_type size() const
std::vector< Vertex > VertexCollection
collection of Vertex objects
Definition: VertexFwd.h:9
std::vector< LorentzVector > LorentzVectorCollection
Definition: PuppiProducer.h:30
virtual void beginJob()
math::XYZTLorentzVector LorentzVector
Definition: PuppiProducer.h:29
const reco::VertexRef vertexRef() const
std::unique_ptr< PuppiContainer > fPuppiContainer
Definition: PuppiProducer.h:58
int charge
Definition: RecoObj.h:29
double pz() const override
z coordinate of momentum vector
edm::EDGetTokenT< VertexCollection > tokenVertices_
Definition: PuppiProducer.h:44
float dZ
Definition: RecoObj.h:27
double py() const override
y coordinate of momentum vector
reco::TrackRef trackRef() const
Definition: PFCandidate.cc:442
float rapidity
Definition: RecoObj.h:17
float pt
Definition: RecoObj.h:17
int iEvent
Definition: GenABIO.cc:230
void addDefault(ParameterSetDescription const &psetDescription)
const_iterator begin() const
int id
Definition: RecoObj.h:18
float phi
Definition: RecoObj.h:17
const PVAssoc fromPV(size_t ipv=0) const
std::unique_ptr< PackedOutputCollection > fPackedPuppiCandidates
Definition: PuppiProducer.h:61
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
bool fClonePackedCands
Definition: PuppiProducer.h:55
double energy() const override
energy
const LorentzVector & p4() const final
four-momentum Lorentz vector
Definition: LeafCandidate.h:99
void produce(edm::Event &, const edm::EventSetup &) override
bool fPuppiForLeptons
Definition: PuppiProducer.h:49
PuppiProducer(const edm::ParameterSet &)
float m
Definition: RecoObj.h:17
T const * product() const
Definition: Handle.h:81
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
ParticleType translatePdgIdToType(int pdgid) const
Definition: PFCandidate.cc:233
float eta
Definition: RecoObj.h:17
bool fUseExistingWeights
Definition: PuppiProducer.h:53
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:40
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:86
reco::GsfTrackRef gsfTrackRef() const
Definition: PFCandidate.cc:480
edm::EDGetTokenT< CandidateView > tokenPFCandidates_
Definition: PuppiProducer.h:43
const_iterator end() const
virtual float dxy() const
dxy with respect to the PV ref
~PuppiProducer() override
std::unique_ptr< PFOutputCollection > fPuppiCandidates
Definition: PuppiProducer.h:60
def move(src, dest)
Definition: eostools.py:510